// Path: app/src/test/java/io/petros/posts/GeneralTestHelper.java
// public class GeneralTestHelper {
// 
//     // RxJava specific fields.
//     protected RxSchedulers rxSchedulers = getTestRxSchedulers();
//     protected Throwable observableErrorThrowable = new RuntimeException("Test emulated error!");
// 
//     // Datastore specific fields.
//     @Mock protected Datastore datastoreMock;
//     @Mock protected DatastoreSaveActions datastoreSaveActionsMock;
//     @Mock protected DatastoreAddActions datastoreAddActionsMock;
//     @Mock protected DatastoreGetActions datastoreGetActionsMock;
//     @Mock protected DatastoreUpdateActions datastoreUpdateActionsMock;
// 
//     // Model specific fields.
//     protected User user = getTestUser();
//     protected List<User> users = getTestUsers();
//     protected Post post = getTestPost();
//     protected List<Post> posts = getTestPosts();
//     protected Comment comment = getTestComment();
//     protected List<Comment> comments = getTestComments();
// 
//     // MOCKS // *************************************************************************************************************
// 
//     protected void setUpMocks() {
//         MockitoAnnotations.initMocks(this);
//         setUpDatastoreMocks();
//     }
// 
//     private void setUpDatastoreMocks() {
//         when(datastoreMock.save()).thenReturn(datastoreSaveActionsMock);
//         when(datastoreMock.add()).thenReturn(datastoreAddActionsMock);
//         when(datastoreMock.get()).thenReturn(datastoreGetActionsMock);
//         when(datastoreMock.update()).thenReturn(datastoreUpdateActionsMock);
//     }
// 
//     // RX JAVA // ***********************************************************************************************************
// 
//     private RxSchedulers getTestRxSchedulers() {
//         return new RxSchedulers(Schedulers.single(), Schedulers.single(), Schedulers.single(),
//                 Schedulers.single());
//     }
// 
//     // USER // **************************************************************************************************************
// 
//     private User getTestUser() {
//         final User testUser = new User();
//         testUser.setId(1);
//         testUser.setName("Leanne Graham");
//         testUser.setUsername("Bret");
//         testUser.setEmail("Sincere@april.biz");
//         return testUser;
//     }
// 
//     private List<User> getTestUsers() {
//         final List<User> testUsers = new ArrayList<>(1);
//         testUsers.add(user);
//         return testUsers;
//     }
// 
//     // POST // **************************************************************************************************************
// 
//     private Post getTestPost() {
//         final Post testPost = new Post();
//         testPost.setUserId(1);
//         testPost.setId(1);
//         testPost.setTitle("sunt aut facere repellat provident occaecati excepturi optio reprehenderit");
//         testPost.setBody("quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae "
//                 + "ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto");
//         return testPost;
//     }
// 
//     private List<Post> getTestPosts() {
//         final List<Post> testPosts = new ArrayList<>(1);
//         testPosts.add(post);
//         return testPosts;
//     }
// 
//     // COMMENT // **************************************************************************************************************
// 
//     private Comment getTestComment() {
//         final Comment testComment = new Comment();
//         testComment.setPostId(1);
//         testComment.setId(1);
//         testComment.setEmail("Eliseo@gardner.biz");
//         testComment.setBody("laudantium enim quasi est quidem magnam voluptate ipsam eos\\ntempora quo necessitatibus\\ndolor "
//                 + "quam autem quasi\\nreiciendis et nam sapiente accusantium");
//         return testComment;
//     }
// 
//     private List<Comment> getTestComments() {
//         final List<Comment> testComments = new ArrayList<>(1);
//         testComments.add(comment);
//         return testComments;
//     }
// 
// }


import org.junit.Before;
import org.junit.Test;
import io.petros.posts.GeneralTestHelper;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
package io.petros.posts.datastore;




public class DatastoreSaveActionsTest extends GeneralTestHelper {

    private DatastoreSaveActions datastoreSaveActions;

    @Before
    public void setUp() {
        setUpMocks();
        setUpDatastoreActions();
    }

    private void setUpDatastoreActions() {
        datastoreSaveActions = new DatastoreSaveActions(datastoreAddActionsMock, datastoreGetActionsMock,
                datastoreUpdateActionsMock);
    }

    @Test
    public void givenTheUserDoesNotExist_WhenItIsSaved_ThenANewUserIsAdded() {
        when(datastoreGetActionsMock.user(user.getId())).thenReturn(null);

        final boolean isSuccessful = datastoreSaveActions.users(users);

        verify(datastoreGetActionsMock, times(1)).user(user.getId());
        verifyNoMoreInteractions(datastoreGetActionsMock);
        verify(datastoreAddActionsMock, times(1)).user(user);
        verifyNoMoreInteractions(datastoreAddActionsMock);
        assertThat(isSuccessful).isTrue();
    }

    @Test
    public void givenTheUserExists_WhenItIsSaved_ThenTheExistingUserIsUpdated() {
        when(datastoreGetActionsMock.user(user.getId())).thenReturn(user);

        final boolean isSuccessful = datastoreSaveActions.users(users);

        verify(datastoreGetActionsMock, times(1)).user(user.getId());
        verifyNoMoreInteractions(datastoreGetActionsMock);
        verify(datastoreUpdateActionsMock, times(1)).user(user, user);
// Path: app/src/test/java/io/petros/posts/GeneralTestHelper.java
// public class GeneralTestHelper {
// 
//     // RxJava specific fields.
//     protected RxSchedulers rxSchedulers = getTestRxSchedulers();
//     protected Throwable observableErrorThrowable = new RuntimeException("Test emulated error!");
// 
//     // Datastore specific fields.
//     @Mock protected Datastore datastoreMock;
//     @Mock protected DatastoreSaveActions datastoreSaveActionsMock;
//     @Mock protected DatastoreAddActions datastoreAddActionsMock;
//     @Mock protected DatastoreGetActions datastoreGetActionsMock;
//     @Mock protected DatastoreUpdateActions datastoreUpdateActionsMock;
// 
//     // Model specific fields.
//     protected User user = getTestUser();
//     protected List<User> users = getTestUsers();
//     protected Post post = getTestPost();
//     protected List<Post> posts = getTestPosts();
//     protected Comment comment = getTestComment();
//     protected List<Comment> comments = getTestComments();
// 
//     // MOCKS // *************************************************************************************************************
// 
//     protected void setUpMocks() {
//         MockitoAnnotations.initMocks(this);
//         setUpDatastoreMocks();
//     }
// 
//     private void setUpDatastoreMocks() {
//         when(datastoreMock.save()).thenReturn(datastoreSaveActionsMock);
//         when(datastoreMock.add()).thenReturn(datastoreAddActionsMock);
//         when(datastoreMock.get()).thenReturn(datastoreGetActionsMock);
//         when(datastoreMock.update()).thenReturn(datastoreUpdateActionsMock);
//     }
// 
//     // RX JAVA // ***********************************************************************************************************
// 
//     private RxSchedulers getTestRxSchedulers() {
//         return new RxSchedulers(Schedulers.single(), Schedulers.single(), Schedulers.single(),
//                 Schedulers.single());
//     }
// 
//     // USER // **************************************************************************************************************
// 
//     private User getTestUser() {
//         final User testUser = new User();
//         testUser.setId(1);
//         testUser.setName("Leanne Graham");
//         testUser.setUsername("Bret");
//         testUser.setEmail("Sincere@april.biz");
//         return testUser;
//     }
// 
//     private List<User> getTestUsers() {
//         final List<User> testUsers = new ArrayList<>(1);
//         testUsers.add(user);
//         return testUsers;
//     }
// 
//     // POST // **************************************************************************************************************
// 
//     private Post getTestPost() {
//         final Post testPost = new Post();
//         testPost.setUserId(1);
//         testPost.setId(1);
//         testPost.setTitle("sunt aut facere repellat provident occaecati excepturi optio reprehenderit");
//         testPost.setBody("quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae "
//                 + "ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto");
//         return testPost;
//     }
// 
//     private List<Post> getTestPosts() {
//         final List<Post> testPosts = new ArrayList<>(1);
//         testPosts.add(post);
//         return testPosts;
//     }
// 
//     // COMMENT // **************************************************************************************************************
// 
//     private Comment getTestComment() {
//         final Comment testComment = new Comment();
//         testComment.setPostId(1);
//         testComment.setId(1);
//         testComment.setEmail("Eliseo@gardner.biz");
//         testComment.setBody("laudantium enim quasi est quidem magnam voluptate ipsam eos\\ntempora quo necessitatibus\\ndolor "
//                 + "quam autem quasi\\nreiciendis et nam sapiente accusantium");
//         return testComment;
//     }
// 
//     private List<Comment> getTestComments() {
//         final List<Comment> testComments = new ArrayList<>(1);
//         testComments.add(comment);
//         return testComments;
//     }
// 
// }


// Path: app/src/test/java/io/petros/posts/datastore/DatastoreSaveActionsTest.java
import org.junit.Before;
import org.junit.Test;
import io.petros.posts.GeneralTestHelper;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

package io.petros.posts.datastore;




public class DatastoreSaveActionsTest extends GeneralTestHelper {

    private DatastoreSaveActions datastoreSaveActions;

    @Before
    public void setUp() {
        setUpMocks();
        setUpDatastoreActions();
    }

    private void setUpDatastoreActions() {
        datastoreSaveActions = new DatastoreSaveActions(datastoreAddActionsMock, datastoreGetActionsMock,
                datastoreUpdateActionsMock);
    }

    @Test
    public void givenTheUserDoesNotExist_WhenItIsSaved_ThenANewUserIsAdded() {
        when(datastoreGetActionsMock.user(user.getId())).thenReturn(null);

        final boolean isSuccessful = datastoreSaveActions.users(users);

        verify(datastoreGetActionsMock, times(1)).user(user.getId());
        verifyNoMoreInteractions(datastoreGetActionsMock);
        verify(datastoreAddActionsMock, times(1)).user(user);
        verifyNoMoreInteractions(datastoreAddActionsMock);
        assertThat(isSuccessful).isTrue();
    }

    @Test
    public void givenTheUserExists_WhenItIsSaved_ThenTheExistingUserIsUpdated() {
        when(datastoreGetActionsMock.user(user.getId())).thenReturn(user);

        final boolean isSuccessful = datastoreSaveActions.users(users);

        verify(datastoreGetActionsMock, times(1)).user(user.getId());
        verifyNoMoreInteractions(datastoreGetActionsMock);
        verify(datastoreUpdateActionsMock, times(1)).user(user, user);
        verifyNoMoreInteractions(datastoreUpdateActionsMock);