From 936a9570f26404dd1060314e56bacc6a022452b0 Mon Sep 17 00:00:00 2001 From: vlsidlyarevich Date: Sat, 24 Jun 2017 13:38:25 +0300 Subject: [PATCH] #77 - unit test for default user analytics service writing --- .../unity/db/UnityDatabaseIT.java | 11 ----- .../repository/UserAnalyticsRepositoryIT.java | 7 +-- .../unity/db/repository/UserRepositoryIT.java | 7 +-- .../db/repository/UserSocialRepositoryIT.java | 7 +-- .../DefaultUserAnalyticsServiceIT.java | 3 +- .../db/service/DefaultUserServiceIT.java | 3 +- .../service/DefaultUserSocialServiceIT.java | 3 +- .../service/FileSystemStorageServiceIT.java | 3 +- .../DefaultUserAnalyticsServiceTest.java | 43 +++++++++++-------- 9 files changed, 36 insertions(+), 51 deletions(-) delete mode 100644 src/integration-test/java/com/github/vlsidlyarevich/unity/db/UnityDatabaseIT.java diff --git a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/UnityDatabaseIT.java b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/UnityDatabaseIT.java deleted file mode 100644 index cd810f0..0000000 --- a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/UnityDatabaseIT.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.github.vlsidlyarevich.unity.db; - -import com.github.vlsidlyarevich.unity.db.config.StorageProperties; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.context.properties.EnableConfigurationProperties; - -@EnableConfigurationProperties(StorageProperties.class) -@SpringBootApplication -public class UnityDatabaseIT { - -} diff --git a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/repository/UserAnalyticsRepositoryIT.java b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/repository/UserAnalyticsRepositoryIT.java index 3155045..3bbef37 100644 --- a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/repository/UserAnalyticsRepositoryIT.java +++ b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/repository/UserAnalyticsRepositoryIT.java @@ -1,21 +1,18 @@ package com.github.vlsidlyarevich.unity.db.repository; -import com.github.vlsidlyarevich.unity.db.UnityDatabaseIT; import com.github.vlsidlyarevich.unity.db.domain.UserAnalytics; import org.junit.After; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; -import org.springframework.test.context.ContextConfiguration; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import static com.github.vlsidlyarevich.unity.TestUtils.createUserAnalytics; @RunWith(SpringRunner.class) -@DataMongoTest -@ContextConfiguration(classes = UnityDatabaseIT.class) +@SpringBootTest public class UserAnalyticsRepositoryIT { @Autowired diff --git a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/repository/UserRepositoryIT.java b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/repository/UserRepositoryIT.java index 4ac99e4..1800d45 100644 --- a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/repository/UserRepositoryIT.java +++ b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/repository/UserRepositoryIT.java @@ -1,21 +1,18 @@ package com.github.vlsidlyarevich.unity.db.repository; -import com.github.vlsidlyarevich.unity.db.UnityDatabaseIT; import com.github.vlsidlyarevich.unity.db.domain.User; import org.junit.After; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; -import org.springframework.test.context.ContextConfiguration; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import static com.github.vlsidlyarevich.unity.TestUtils.createUser; @RunWith(SpringRunner.class) -@DataMongoTest -@ContextConfiguration(classes = UnityDatabaseIT.class) +@SpringBootTest public class UserRepositoryIT { @Autowired diff --git a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/repository/UserSocialRepositoryIT.java b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/repository/UserSocialRepositoryIT.java index 9695f25..b9c1d1b 100644 --- a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/repository/UserSocialRepositoryIT.java +++ b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/repository/UserSocialRepositoryIT.java @@ -1,21 +1,18 @@ package com.github.vlsidlyarevich.unity.db.repository; -import com.github.vlsidlyarevich.unity.db.UnityDatabaseIT; import com.github.vlsidlyarevich.unity.db.domain.UserSocial; import org.junit.After; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; -import org.springframework.test.context.ContextConfiguration; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import static com.github.vlsidlyarevich.unity.TestUtils.createUserSocial; @RunWith(SpringRunner.class) -@DataMongoTest -@ContextConfiguration(classes = UnityDatabaseIT.class) +@SpringBootTest public class UserSocialRepositoryIT { @Autowired diff --git a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserAnalyticsServiceIT.java b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserAnalyticsServiceIT.java index fd46566..538a2f6 100644 --- a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserAnalyticsServiceIT.java +++ b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserAnalyticsServiceIT.java @@ -1,7 +1,6 @@ package com.github.vlsidlyarevich.unity.db.service; import com.github.vlsidlyarevich.unity.common.model.AnalysisReport; -import com.github.vlsidlyarevich.unity.db.UnityDatabaseIT; import com.github.vlsidlyarevich.unity.db.domain.UserAnalytics; import org.junit.After; import org.junit.Assert; @@ -17,7 +16,7 @@ import static org.hamcrest.Matchers.*; @RunWith(SpringRunner.class) -@SpringBootTest(classes = UnityDatabaseIT.class) +@SpringBootTest public class DefaultUserAnalyticsServiceIT { @Autowired diff --git a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserServiceIT.java b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserServiceIT.java index d31c784..5bbb7da 100644 --- a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserServiceIT.java +++ b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserServiceIT.java @@ -1,6 +1,5 @@ package com.github.vlsidlyarevich.unity.db.service; -import com.github.vlsidlyarevich.unity.db.UnityDatabaseIT; import com.github.vlsidlyarevich.unity.db.domain.User; import com.github.vlsidlyarevich.unity.db.repository.UserRepository; import org.junit.After; @@ -18,7 +17,7 @@ import static org.hamcrest.core.IsNull.notNullValue; @RunWith(SpringRunner.class) -@SpringBootTest(classes = UnityDatabaseIT.class) +@SpringBootTest public class DefaultUserServiceIT { @Autowired diff --git a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserSocialServiceIT.java b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserSocialServiceIT.java index b418032..83a4dfd 100644 --- a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserSocialServiceIT.java +++ b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserSocialServiceIT.java @@ -1,6 +1,5 @@ package com.github.vlsidlyarevich.unity.db.service; -import com.github.vlsidlyarevich.unity.db.UnityDatabaseIT; import com.github.vlsidlyarevich.unity.db.domain.UserSocial; import com.github.vlsidlyarevich.unity.db.repository.UserSocialRepository; import org.junit.After; @@ -18,7 +17,7 @@ import static org.hamcrest.core.IsNull.notNullValue; @RunWith(SpringRunner.class) -@SpringBootTest(classes = UnityDatabaseIT.class) +@SpringBootTest public class DefaultUserSocialServiceIT { @Autowired diff --git a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/FileSystemStorageServiceIT.java b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/FileSystemStorageServiceIT.java index 9939bd0..7e478ca 100644 --- a/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/FileSystemStorageServiceIT.java +++ b/src/integration-test/java/com/github/vlsidlyarevich/unity/db/service/FileSystemStorageServiceIT.java @@ -1,6 +1,5 @@ package com.github.vlsidlyarevich.unity.db.service; -import com.github.vlsidlyarevich.unity.db.UnityDatabaseIT; import org.junit.After; import org.junit.Assert; import org.junit.Test; @@ -15,7 +14,7 @@ import java.util.List; @RunWith(SpringRunner.class) -@SpringBootTest(classes = UnityDatabaseIT.class) +@SpringBootTest public class FileSystemStorageServiceIT { @Autowired diff --git a/src/test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserAnalyticsServiceTest.java b/src/test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserAnalyticsServiceTest.java index ed7c49f..9cbc4c5 100644 --- a/src/test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserAnalyticsServiceTest.java +++ b/src/test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserAnalyticsServiceTest.java @@ -10,9 +10,14 @@ import org.mockito.Mock; import org.springframework.test.context.junit4.SpringRunner; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import static com.github.vlsidlyarevich.unity.TestUtils.createAnalysisReport; import static com.github.vlsidlyarevich.unity.TestUtils.createUserAnalytics; import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.*; @@ -55,8 +60,6 @@ public void find_Success_IfPresent() throws Exception { assertThat(userAnalyticsService.find(userAnalytics.getId()), equalTo(userAnalytics)); verify(userAnalyticsRepository).findOne(userAnalytics.getId()); - - userAnalyticsService.add(userAnalytics); } @Test @@ -72,39 +75,46 @@ public void find_Null_IfNotPresent() throws Exception { public void findByUserId_Success_IfPresent() throws Exception { UserAnalytics userAnalytics = createUserAnalytics(); - userAnalyticsService.add(userAnalytics); + doReturn(userAnalytics).when(userAnalyticsRepository).findByUserId(userAnalytics.getUserId()); assertThat(userAnalyticsService.findByUserId(userAnalytics.getUserId()), is(userAnalytics)); + verify(userAnalyticsRepository).findByUserId(userAnalytics.getUserId()); } @Test public void findByUserId_Null_IfNotPresent() throws Exception { assertThat(userAnalyticsService.findByUserId("userid"), nullValue()); + verify(userAnalyticsRepository).findByUserId("userid"); } @Test public void findAll_Success_IfPresent() throws Exception { UserAnalytics userAnalytics = createUserAnalytics(); + ArrayList userAnalyticsList = new ArrayList() {{ + add(userAnalytics); + }}; - userAnalyticsService.add(userAnalytics); + doReturn(userAnalyticsList).when(userAnalyticsRepository).findAll(); - assertThat(userAnalyticsService.findAll().size(), is(1)); - Assert.assertTrue(userAnalyticsService.findAll().contains(userAnalytics)); + assertThat(userAnalyticsService.findAll(), containsInAnyOrder(userAnalyticsList.toArray())); + verify(userAnalyticsRepository).findAll(); } @Test public void findAll_Empty_IfNotPresent() throws Exception { - Assert.assertTrue(userAnalyticsService.findAll().isEmpty()); + doReturn(Collections.emptyList()).when(userAnalyticsRepository).findAll(); + + assertThat(userAnalyticsService.findAll(), emptyCollectionOf(UserAnalytics.class)); + verify(userAnalyticsRepository).findAll(); } @Test public void delete_Success_IfPresent() throws Exception { UserAnalytics userAnalytics = createUserAnalytics(); - userAnalyticsService.add(userAnalytics); userAnalyticsService.delete(userAnalytics.getId()); - Assert.assertTrue(userAnalyticsService.findAll().isEmpty()); + verify(userAnalyticsRepository).delete(userAnalytics.getId()); } @Test @@ -113,20 +123,18 @@ public void deleteReport_Success_IfPresent() throws Exception { AnalysisReport analysisReport = createAnalysisReport(); userAnalytics.getReports().add(analysisReport); - userAnalyticsService.add(userAnalytics); + doReturn(userAnalytics).when(userAnalyticsRepository).findByUserId(userAnalytics.getUserId()); + userAnalyticsService.deleteReport(userAnalytics.getUserId(), analysisReport.getId()); - Assert.assertTrue(userAnalyticsService.find(userAnalytics.getId()).getReports().isEmpty()); + assertThat(userAnalyticsService.findByUserId(userAnalytics.getUserId()).getReports().size(), is(0)); } @Test public void deleteAll_Success_IfPresent() throws Exception { - UserAnalytics userAnalytics = createUserAnalytics(); - - userAnalyticsService.add(userAnalytics); userAnalyticsService.deleteAll(); - Assert.assertTrue(userAnalyticsService.findAll().isEmpty()); + verify(userAnalyticsRepository).deleteAll(); } @Test @@ -135,9 +143,10 @@ public void deleteAllReports_Success_IfPresent() throws Exception { AnalysisReport analysisReport = createAnalysisReport(); userAnalytics.getReports().add(analysisReport); - userAnalyticsService.add(userAnalytics); + doReturn(userAnalytics).when(userAnalyticsRepository).findByUserId(userAnalytics.getUserId()); + userAnalyticsService.deleteAllReports(userAnalytics.getUserId()); - Assert.assertTrue(userAnalyticsService.find(userAnalytics.getId()).getReports().isEmpty()); + assertThat(userAnalyticsService.findByUserId(userAnalytics.getUserId()).getReports().size(), is(0)); } }