diff --git a/data-test/build.gradle b/data-test/build.gradle deleted file mode 100644 index 99016438..00000000 --- a/data-test/build.gradle +++ /dev/null @@ -1,94 +0,0 @@ -def dataLayerProject = ':data' -def outputDirectory = "${project.buildDir}/classes/test" - -evaluationDependsOn(dataLayerProject) - -apply plugin: 'java' - -sourceCompatibility = JavaVersion.VERSION_1_7 -targetCompatibility = JavaVersion.VERSION_1_7 - -configurations { - removableDependency -} - -dependencies { - def dataLayer = project(dataLayerProject) - testCompile project(path: dataLayerProject, configuration: 'debugCompile') - - def debugVariant = dataLayer.android.libraryVariants.find({it.name == 'debug'}) - //noinspection GroovyAssignabilityCheck - testCompile debugVariant.javaCompile.classpath - //noinspection GroovyAssignabilityCheck - testCompile debugVariant.javaCompile.outputs.files - //noinspection GroovyAssignabilityCheck - testCompile files(dataLayer.android.bootClasspath) - - def dataTestDependencies = rootProject.ext.dataTestDependencies - testCompile dataTestDependencies.junit - testCompile dataTestDependencies.mockito - testCompile dataTestDependencies.robolectric -} - -sourceSets { - test { - def testCompileClasspath = compileClasspath - def testRuntimeClasspath = runtimeClasspath - - java.srcDirs = ['src/test/java'] - resources.srcDirs = ['src/test/resources'] - output.resourcesDir = outputDirectory - output.classesDir = outputDirectory - - compileClasspath = setupClasspath(testCompileClasspath) - runtimeClasspath = setupClasspath(testRuntimeClasspath) - } -} - -tasks.withType(Test) { - //If we don't do this, Gradle always thinks this task is up to date and skips the tests - outputs.upToDateWhen { false } - - jvmArgs '-Dfile.encoding=UTF-8' - - //To avoid OutOfMemoryError - jvmArgs '-XX:MaxPermSize=2048m' - minHeapSize = '512m' - maxHeapSize = '1024m' - - //Robolectric issue: - //https://github.com/robolectric/robolectric/issues/1332 - jvmArgs '-XX:-UseSplitVerifier' - jvmArgs '-noverify' - - scanForTestClasses = false - - //This has to be done like this because, for some reason, - //the IDE and command line are having different working directories. - workingDir = new File('../') - - include "**/*Should.class" - include "**/*Test.class" - include "**/*Tests.class" - exclude "**/*IT.class" -} - -Object findRuntimeDependency(String name) { - project.configurations.testRuntime.find { it.name.startsWith(name) } -} - -FileCollection setupClasspath(FileCollection classPath) { - //Gradle does not guarantee dependency order, so we must make sure - //Robolectric artifact appears before android artifact - def robolectricDependency = findRuntimeDependency('robolectric') - def androidDependency = findRuntimeDependency('android.jar') - - //Remove unused dependencies from classpath - classPath -= configurations.removableDependency - - //Remove android dependency: must be put at the end - //to avoid Stub! Runtime Exception - classPath -= files(androidDependency) - - return files(robolectricDependency) + classPath + files(androidDependency) -} \ No newline at end of file diff --git a/data-test/src/test/java/com/fernandocejas/android10/sample/data/ApplicationTestCase.java b/data-test/src/test/java/com/fernandocejas/android10/sample/data/ApplicationTestCase.java deleted file mode 100644 index ab82dd2e..00000000 --- a/data-test/src/test/java/com/fernandocejas/android10/sample/data/ApplicationTestCase.java +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.data; - -import java.lang.reflect.Field; -import org.junit.runner.RunWith; - -/** - * Base class for Robolectric data layer tests running with a custom Test Runner. - * Inherit from this class to create a test. - */ -@RunWith(ApplicationTestRunner.class) -public abstract class ApplicationTestCase { - - /** - * Resets a Singleton class. - * This works using reflection and looking for a private field in the singleton called - * "INSTANCE". - * It is actually a workaround (hack?) to avoid global state when testing in isolation. - * - * @param clazz The class to reset. - */ - protected void resetSingleton(Class clazz) { - Field instance; - try { - instance = clazz.getDeclaredField("INSTANCE"); - instance.setAccessible(true); - instance.set(null, null); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/data-test/src/test/java/com/fernandocejas/android10/sample/data/ApplicationTestRunner.java b/data-test/src/test/java/com/fernandocejas/android10/sample/data/ApplicationTestRunner.java deleted file mode 100644 index b92730e7..00000000 --- a/data-test/src/test/java/com/fernandocejas/android10/sample/data/ApplicationTestRunner.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.data; - -import org.junit.runners.model.InitializationError; -import org.robolectric.AndroidManifest; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.annotation.Config; -import org.robolectric.res.Fs; - -/** - * Custom test runner for robolectric unit/integration tests. - */ -public class ApplicationTestRunner extends RobolectricTestRunner { - - //Maximun SDK Robolectric will compile (issues with SDK > 18) - private static final int MAX_SDK_SUPPORTED_BY_ROBOLECTRIC = 18; - - private static final String ANDROID_MANIFEST_PATH = "data/src/main/AndroidManifest.xml"; - private static final String ANDROID_MANIFEST_RES_PATH = "data/src/main/res"; - - /** - * Call this constructor to specify the location of resources and AndroidManifest.xml. - * - * @throws org.junit.runners.model.InitializationError - */ - public ApplicationTestRunner(Class testClass) throws InitializationError { - super(testClass); - } - - @Override protected AndroidManifest getAppManifest(Config config) { - return new AndroidManifest(Fs.fileFromPath(ANDROID_MANIFEST_PATH), - Fs.fileFromPath(ANDROID_MANIFEST_RES_PATH)) { - @Override - public int getTargetSdkVersion() { - return MAX_SDK_SUPPORTED_BY_ROBOLECTRIC; - } - }; - } -} diff --git a/data-test/src/test/java/com/fernandocejas/android10/sample/data/cache/FileManagerTest.java b/data-test/src/test/java/com/fernandocejas/android10/sample/data/cache/FileManagerTest.java deleted file mode 100644 index 5e5b4b09..00000000 --- a/data-test/src/test/java/com/fernandocejas/android10/sample/data/cache/FileManagerTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.data.cache; - -import com.fernandocejas.android10.sample.data.ApplicationTestCase; -import java.io.File; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.robolectric.Robolectric; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - -public class FileManagerTest extends ApplicationTestCase { - - private FileManager fileManager; - private File cacheDir; - - @Before - public void setUp() { - fileManager = new FileManager(); - cacheDir = Robolectric.application.getCacheDir(); - } - - @After - public void tearDown() { - if (cacheDir != null) { - fileManager.clearDirectory(cacheDir); - } - } - - @Test - public void testWriteToFile() { - File fileToWrite = createDummyFile(); - String fileContent = "content"; - - fileManager.writeToFile(fileToWrite, fileContent); - - assertThat(fileToWrite.exists(), is(true)); - } - - @Test - public void testFileContent() { - File fileToWrite = createDummyFile(); - String fileContent = "content\n"; - - fileManager.writeToFile(fileToWrite, fileContent); - String expectedContent = fileManager.readFileContent(fileToWrite); - - assertThat(expectedContent, is(equalTo(fileContent))); - } - - private File createDummyFile() { - String dummyFilePath = cacheDir.getPath() + File.separator + "dumyFile"; - File dummyFile = new File(dummyFilePath); - - return dummyFile; - } -} diff --git a/data-test/src/test/java/com/fernandocejas/android10/sample/data/cache/serializer/JsonSerializerTest.java b/data-test/src/test/java/com/fernandocejas/android10/sample/data/cache/serializer/JsonSerializerTest.java deleted file mode 100644 index da15891a..00000000 --- a/data-test/src/test/java/com/fernandocejas/android10/sample/data/cache/serializer/JsonSerializerTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.data.cache.serializer; - -import com.fernandocejas.android10.sample.data.ApplicationTestCase; -import com.fernandocejas.android10.sample.data.entity.UserEntity; -import org.junit.Before; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -public class JsonSerializerTest extends ApplicationTestCase { - - private static final String JSON_RESPONSE = "{\n" - + " \"id\": 1,\n" - + " \"cover_url\": \"http://www.android10.org/myapi/cover_1.jpg\",\n" - + " \"full_name\": \"Simon Hill\",\n" - + " \"description\": \"Curabitur gravida nisi at nibh. In hac habitasse platea dictumst. Aliquam augue quam, sollicitudin vitae, consectetuer eget, rutrum at, lorem.\\n\\nInteger tincidunt ante vel ipsum. Praesent blandit lacinia erat. Vestibulum sed magna at nunc commodo placerat.\\n\\nPraesent blandit. Nam nulla. Integer pede justo, lacinia eget, tincidunt eget, tempus vel, pede.\",\n" - + " \"followers\": 7484,\n" - + " \"email\": \"jcooper@babbleset.edu\"\n" - + "}"; - - private JsonSerializer jsonSerializer; - - @Before - public void setUp() { - jsonSerializer = new JsonSerializer(); - } - - @Test - public void testSerializeHappyCase() { - UserEntity userEntityOne = jsonSerializer.deserialize(JSON_RESPONSE); - String jsonString = jsonSerializer.serialize(userEntityOne); - UserEntity userEntityTwo = jsonSerializer.deserialize(jsonString); - - assertThat(userEntityOne.getUserId(), is(userEntityTwo.getUserId())); - assertThat(userEntityOne.getFullname(), is(equalTo(userEntityTwo.getFullname()))); - assertThat(userEntityOne.getFollowers(), is(userEntityTwo.getFollowers())); - } - - @Test - public void testDesearializeHappyCase() { - UserEntity userEntity = jsonSerializer.deserialize(JSON_RESPONSE); - - assertThat(userEntity.getUserId(), is(1)); - assertThat(userEntity.getFullname(), is("Simon Hill")); - assertThat(userEntity.getFollowers(), is(7484)); - } -} diff --git a/data-test/src/test/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityDataMapperTest.java b/data-test/src/test/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityDataMapperTest.java deleted file mode 100644 index 19a4db56..00000000 --- a/data-test/src/test/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityDataMapperTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.data.entity.mapper; - -import com.fernandocejas.android10.sample.data.ApplicationTestCase; -import com.fernandocejas.android10.sample.data.entity.UserEntity; -import com.fernandocejas.android10.sample.domain.User; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import org.junit.Before; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.mock; - -public class UserEntityDataMapperTest extends ApplicationTestCase { - - private static final int FAKE_USER_ID = 123; - private static final String FAKE_FULLNAME = "Tony Stark"; - - private UserEntityDataMapper userEntityDataMapper; - - @Before - public void setUp() throws Exception { - userEntityDataMapper = new UserEntityDataMapper(); - } - - @Test - public void testTransformUserEntity() { - UserEntity userEntity = createFakeUserEntity(); - User user = userEntityDataMapper.transform(userEntity); - - assertThat(user, is(instanceOf(User.class))); - assertThat(user.getUserId(), is(FAKE_USER_ID)); - assertThat(user.getFullName(), is(FAKE_FULLNAME)); - } - - @Test - public void testTransformUserEntityCollection() { - UserEntity mockUserEntityOne = mock(UserEntity.class); - UserEntity mockUserEntityTwo = mock(UserEntity.class); - - List userEntityList = new ArrayList(5); - userEntityList.add(mockUserEntityOne); - userEntityList.add(mockUserEntityTwo); - - Collection userCollection = userEntityDataMapper.transform(userEntityList); - - assertThat(userCollection.toArray()[0], is(instanceOf(User.class))); - assertThat(userCollection.toArray()[1], is(instanceOf(User.class))); - assertThat(userCollection.size(), is(2)); - } - - private UserEntity createFakeUserEntity() { - UserEntity userEntity = new UserEntity(); - userEntity.setUserId(FAKE_USER_ID); - userEntity.setFullname(FAKE_FULLNAME); - - return userEntity; - } -} diff --git a/data-test/src/test/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityJsonMapperTest.java b/data-test/src/test/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityJsonMapperTest.java deleted file mode 100644 index f834f52d..00000000 --- a/data-test/src/test/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityJsonMapperTest.java +++ /dev/null @@ -1,93 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.data.entity.mapper; - -import com.fernandocejas.android10.sample.data.ApplicationTestCase; -import com.fernandocejas.android10.sample.data.entity.UserEntity; -import com.google.gson.JsonSyntaxException; -import java.util.Collection; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - -public class UserEntityJsonMapperTest extends ApplicationTestCase { - - private static final String JSON_RESPONSE_USER_DETAILS = "{\n" - + " \"id\": 1,\n" - + " \"cover_url\": \"http://www.android10.org/myapi/cover_1.jpg\",\n" - + " \"full_name\": \"Simon Hill\",\n" - + " \"description\": \"Curabitur gravida nisi at nibh. In hac habitasse platea dictumst. Aliquam augue quam, sollicitudin vitae, consectetuer eget, rutrum at, lorem.\\n\\nInteger tincidunt ante vel ipsum. Praesent blandit lacinia erat. Vestibulum sed magna at nunc commodo placerat.\\n\\nPraesent blandit. Nam nulla. Integer pede justo, lacinia eget, tincidunt eget, tempus vel, pede.\",\n" - + " \"followers\": 7484,\n" - + " \"email\": \"jcooper@babbleset.edu\"\n" - + "}"; - - private static final String JSON_RESPONSE_USER_COLLECTION = "[{\n" - + " \"id\": 1,\n" - + " \"full_name\": \"Simon Hill\",\n" - + " \"followers\": 7484\n" - + "}, {\n" - + " \"id\": 12,\n" - + " \"full_name\": \"Pedro Garcia\",\n" - + " \"followers\": 1381\n" - + "}]"; - - private UserEntityJsonMapper userEntityJsonMapper; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Before - public void setUp() { - userEntityJsonMapper = new UserEntityJsonMapper(); - } - - @Test - public void testTransformUserEntityHappyCase() { - UserEntity userEntity = userEntityJsonMapper.transformUserEntity(JSON_RESPONSE_USER_DETAILS); - - assertThat(userEntity.getUserId(), is(1)); - assertThat(userEntity.getFullname(), is(equalTo("Simon Hill"))); - assertThat(userEntity.getEmail(), is(equalTo("jcooper@babbleset.edu"))); - } - - @Test - public void testTransformUserEntityCollectionHappyCase() { - Collection userEntityCollection = - userEntityJsonMapper.transformUserEntityCollection( - JSON_RESPONSE_USER_COLLECTION); - - assertThat(((UserEntity) userEntityCollection.toArray()[0]).getUserId(), is(1)); - assertThat(((UserEntity) userEntityCollection.toArray()[1]).getUserId(), is(12)); - assertThat(userEntityCollection.size(), is(2)); - } - - @Test - public void testTransformUserEntityNotValidResponse() { - expectedException.expect(JsonSyntaxException.class); - userEntityJsonMapper.transformUserEntity("ironman"); - } - - @Test - public void testTransformUserEntityCollectionNotValidResponse() { - expectedException.expect(JsonSyntaxException.class); - userEntityJsonMapper.transformUserEntityCollection("Tony Stark"); - } -} diff --git a/data-test/src/test/java/com/fernandocejas/android10/sample/data/exception/RepositoryErrorBundleTest.java b/data-test/src/test/java/com/fernandocejas/android10/sample/data/exception/RepositoryErrorBundleTest.java deleted file mode 100644 index 277f7ad7..00000000 --- a/data-test/src/test/java/com/fernandocejas/android10/sample/data/exception/RepositoryErrorBundleTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.data.exception; - -import com.fernandocejas.android10.sample.data.ApplicationTestCase; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import static org.mockito.Mockito.verify; - -public class RepositoryErrorBundleTest extends ApplicationTestCase { - - private RepositoryErrorBundle repositoryErrorBundle; - - @Mock - private Exception mockException; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - repositoryErrorBundle = new RepositoryErrorBundle(mockException); - } - - @Test - public void testGetErrorMessageInteraction() { - repositoryErrorBundle.getErrorMessage(); - - verify(mockException).getMessage(); - } -} diff --git a/data-test/src/test/java/com/fernandocejas/android10/sample/data/repository/UserDataRepositoryTest.java b/data-test/src/test/java/com/fernandocejas/android10/sample/data/repository/UserDataRepositoryTest.java deleted file mode 100644 index 88f85556..00000000 --- a/data-test/src/test/java/com/fernandocejas/android10/sample/data/repository/UserDataRepositoryTest.java +++ /dev/null @@ -1,85 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.data.repository; - -import com.fernandocejas.android10.sample.data.ApplicationTestCase; -import com.fernandocejas.android10.sample.data.entity.UserEntity; -import com.fernandocejas.android10.sample.data.entity.mapper.UserEntityDataMapper; -import com.fernandocejas.android10.sample.data.repository.datasource.UserDataStore; -import com.fernandocejas.android10.sample.data.repository.datasource.UserDataStoreFactory; -import com.fernandocejas.android10.sample.domain.User; -import java.util.ArrayList; -import java.util.List; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import rx.Observable; - -import static org.mockito.BDDMockito.given; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Mockito.verify; - -public class UserDataRepositoryTest extends ApplicationTestCase { - - private static final int FAKE_USER_ID = 123; - - private UserDataRepository userDataRepository; - - @Mock private UserDataStoreFactory mockUserDataStoreFactory; - @Mock private UserEntityDataMapper mockUserEntityDataMapper; - @Mock private UserDataStore mockUserDataStore; - @Mock private UserEntity mockUserEntity; - @Mock private User mockUser; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - resetSingleton(UserDataRepository.class); - userDataRepository = new UserDataRepository(mockUserDataStoreFactory, - mockUserEntityDataMapper); - - given(mockUserDataStoreFactory.create(anyInt())).willReturn(mockUserDataStore); - given(mockUserDataStoreFactory.createCloudDataStore()).willReturn(mockUserDataStore); - } - - @Test - public void testGetUsersHappyCase() { - List usersList = new ArrayList<>(); - usersList.add(new UserEntity()); - given(mockUserDataStore.getUserEntityList()).willReturn(Observable.just(usersList)); - - userDataRepository.getUsers(); - - verify(mockUserDataStoreFactory).createCloudDataStore(); - verify(mockUserDataStore).getUserEntityList(); - } - - @Test - public void testGetUserHappyCase() { - UserEntity userEntity = new UserEntity(); - given(mockUserDataStore.getUserEntityDetails(FAKE_USER_ID)).willReturn(Observable.just(userEntity)); - userDataRepository.getUser(FAKE_USER_ID); - - verify(mockUserDataStoreFactory).create(FAKE_USER_ID); - verify(mockUserDataStore).getUserEntityDetails(FAKE_USER_ID); - } -} diff --git a/data-test/src/test/java/com/fernandocejas/android10/sample/data/repository/datasource/CloudUserDataStoreTest.java b/data-test/src/test/java/com/fernandocejas/android10/sample/data/repository/datasource/CloudUserDataStoreTest.java deleted file mode 100644 index 7a96d07c..00000000 --- a/data-test/src/test/java/com/fernandocejas/android10/sample/data/repository/datasource/CloudUserDataStoreTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.data.repository.datasource; - -import com.fernandocejas.android10.sample.data.ApplicationTestCase; -import com.fernandocejas.android10.sample.data.cache.UserCache; -import com.fernandocejas.android10.sample.data.entity.UserEntity; -import com.fernandocejas.android10.sample.data.net.RestApi; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import rx.Observable; - -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.verify; - -public class CloudUserDataStoreTest extends ApplicationTestCase { - - private static final int FAKE_USER_ID = 765; - - private CloudUserDataStore cloudUserDataStore; - - @Mock private RestApi mockRestApi; - @Mock private UserCache mockUserCache; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - cloudUserDataStore = new CloudUserDataStore(mockRestApi, mockUserCache); - } - - @Test - public void testGetUserEntityListFromApi() { - cloudUserDataStore.getUserEntityList(); - verify(mockRestApi).getUserEntityList(); - } - - @Test - public void testGetUserEntityDetailsFromApi() { - UserEntity fakeUserEntity = new UserEntity(); - Observable fakeObservable = Observable.just(fakeUserEntity); - given(mockRestApi.getUserEntityById(FAKE_USER_ID)).willReturn(fakeObservable); - - cloudUserDataStore.getUserEntityDetails(FAKE_USER_ID); - - verify(mockRestApi).getUserEntityById(FAKE_USER_ID); - } -} diff --git a/data-test/src/test/java/com/fernandocejas/android10/sample/data/repository/datasource/DiskUserDataStoreTest.java b/data-test/src/test/java/com/fernandocejas/android10/sample/data/repository/datasource/DiskUserDataStoreTest.java deleted file mode 100644 index 8cea6b3b..00000000 --- a/data-test/src/test/java/com/fernandocejas/android10/sample/data/repository/datasource/DiskUserDataStoreTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.data.repository.datasource; - -import com.fernandocejas.android10.sample.data.ApplicationTestCase; -import com.fernandocejas.android10.sample.data.cache.UserCache; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import static org.mockito.Mockito.verify; - -public class DiskUserDataStoreTest extends ApplicationTestCase { - - private static final int FAKE_USER_ID = 11; - - private DiskUserDataStore diskUserDataStore; - - @Mock private UserCache mockUserCache; - - @Rule public ExpectedException expectedException = ExpectedException.none(); - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - diskUserDataStore = new DiskUserDataStore(mockUserCache); - } - - @Test - public void testGetUserEntityListUnsupported() { - expectedException.expect(UnsupportedOperationException.class); - diskUserDataStore.getUserEntityList(); - } - - @Test - public void testGetUserEntityDetailesFromCache() { - diskUserDataStore.getUserEntityDetails(FAKE_USER_ID); - verify(mockUserCache).get(FAKE_USER_ID); - } -} diff --git a/data-test/src/test/java/com/fernandocejas/android10/sample/data/repository/datasource/UserDataStoreFactoryTest.java b/data-test/src/test/java/com/fernandocejas/android10/sample/data/repository/datasource/UserDataStoreFactoryTest.java deleted file mode 100644 index 55632564..00000000 --- a/data-test/src/test/java/com/fernandocejas/android10/sample/data/repository/datasource/UserDataStoreFactoryTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Copyright (C) 2015 Fernando Cejas Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.fernandocejas.android10.sample.data.repository.datasource; - -import com.fernandocejas.android10.sample.data.ApplicationTestCase; -import com.fernandocejas.android10.sample.data.cache.UserCache; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.Robolectric; - -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.verify; - -public class UserDataStoreFactoryTest extends ApplicationTestCase { - - private static final int FAKE_USER_ID = 11; - - private UserDataStoreFactory userDataStoreFactory; - - @Mock - private UserCache mockUserCache; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - userDataStoreFactory = - new UserDataStoreFactory(Robolectric.application, mockUserCache); - } - - @Test - public void testCreateDiskDataStore() { - given(mockUserCache.isCached(FAKE_USER_ID)).willReturn(true); - given(mockUserCache.isExpired()).willReturn(false); - - UserDataStore userDataStore = userDataStoreFactory.create(FAKE_USER_ID); - - assertThat(userDataStore, is(notNullValue())); - assertThat(userDataStore, is(instanceOf(DiskUserDataStore.class))); - - verify(mockUserCache).isCached(FAKE_USER_ID); - verify(mockUserCache).isExpired(); - } - - @Test - public void testCreateCloudDataStore() { - given(mockUserCache.isExpired()).willReturn(true); - given(mockUserCache.isCached(FAKE_USER_ID)).willReturn(false); - - UserDataStore userDataStore = userDataStoreFactory.create(FAKE_USER_ID); - - assertThat(userDataStore, is(notNullValue())); - assertThat(userDataStore, is(instanceOf(CloudUserDataStore.class))); - - verify(mockUserCache).isExpired(); - } -} diff --git a/settings.gradle b/settings.gradle index 5a4d035d..c5e0ddae 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,3 @@ include ':presentation' include ':domain' include ':data' -include ':data-test'