From 655928cc7beb1d3b31caf6a85a4e178e2b160c13 Mon Sep 17 00:00:00 2001 From: vlsidlyarevich Date: Sun, 25 Jun 2017 21:01:06 +0300 Subject: [PATCH] #77 - unit test for db services added --- .../FileSystemFileNotFoundException.java | 2 + .../DefaultUserAnalyticsServiceTest.java | 7 +- .../db/service/DefaultUserServiceTest.java | 159 +++++++++++++++++ .../service/DefaultUserSocialServiceTest.java | 163 ++++++++++++++++++ .../service/FileSystemStorageServiceTest.java | 67 +++++++ 5 files changed, 395 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserServiceTest.java create mode 100644 src/test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserSocialServiceTest.java create mode 100644 src/test/java/com/github/vlsidlyarevich/unity/db/service/FileSystemStorageServiceTest.java diff --git a/src/main/java/com/github/vlsidlyarevich/unity/db/exception/FileSystemFileNotFoundException.java b/src/main/java/com/github/vlsidlyarevich/unity/db/exception/FileSystemFileNotFoundException.java index d88ef19..df589f7 100644 --- a/src/main/java/com/github/vlsidlyarevich/unity/db/exception/FileSystemFileNotFoundException.java +++ b/src/main/java/com/github/vlsidlyarevich/unity/db/exception/FileSystemFileNotFoundException.java @@ -1,8 +1,10 @@ package com.github.vlsidlyarevich.unity.db.exception; import lombok.Data; +import lombok.EqualsAndHashCode; @Data +@EqualsAndHashCode(callSuper = true) public class FileSystemFileNotFoundException extends RuntimeException { private static final long serialVersionUID = 3194206510987651825L; 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 9cbc4c5..1f34ad4 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 @@ -3,7 +3,6 @@ import com.github.vlsidlyarevich.unity.common.model.AnalysisReport; import com.github.vlsidlyarevich.unity.db.domain.UserAnalytics; import com.github.vlsidlyarevich.unity.db.repository.UserAnalyticsRepository; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -12,12 +11,10 @@ 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.*; @@ -78,12 +75,14 @@ public void findByUserId_Success_IfPresent() throws Exception { 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"); } @@ -97,6 +96,7 @@ public void findAll_Success_IfPresent() throws Exception { doReturn(userAnalyticsList).when(userAnalyticsRepository).findAll(); assertThat(userAnalyticsService.findAll(), containsInAnyOrder(userAnalyticsList.toArray())); + verify(userAnalyticsRepository).findAll(); } @@ -105,6 +105,7 @@ public void findAll_Empty_IfNotPresent() throws Exception { doReturn(Collections.emptyList()).when(userAnalyticsRepository).findAll(); assertThat(userAnalyticsService.findAll(), emptyCollectionOf(UserAnalytics.class)); + verify(userAnalyticsRepository).findAll(); } diff --git a/src/test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserServiceTest.java b/src/test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserServiceTest.java new file mode 100644 index 0000000..7a21cd2 --- /dev/null +++ b/src/test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserServiceTest.java @@ -0,0 +1,159 @@ +package com.github.vlsidlyarevich.unity.db.service; + +import com.github.vlsidlyarevich.unity.db.domain.User; +import com.github.vlsidlyarevich.unity.db.repository.UserRepository; +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.ArrayList; +import java.util.Collections; + +import static com.github.vlsidlyarevich.unity.TestUtils.createUser; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.collection.IsEmptyCollection.emptyCollectionOf; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +@RunWith(SpringRunner.class) +public class DefaultUserServiceTest { + + private UserService userService; + + @Mock + private UserRepository userRepository; + + @Mock + private UserSocialService userSocialService; + + @Mock + private UserAnalyticsService userAnalyticsService; + + @Before + public void setUp() { + this.userService = new DefaultUserService(userRepository, userSocialService, userAnalyticsService); + } + + @Test + public void create_Success_IfValid() throws Exception { + User user = createUser(); + + userService.create(user); + + verify(userRepository).save(user); + } + + @Test(expected = IllegalArgumentException.class) + public void create_ExceptionThrown_ifNull() throws Exception { + userService.create(null); + } + + @Test + public void find_Success_IfPresent() throws Exception { + User user = createUser(); + + doReturn(user).when(userRepository).findOne(user.getId()); + + assertThat(userService.find(user.getId()), is(user)); + + verify(userRepository).findOne(user.getId()); + } + + @Test + public void find_Null_IfNotPresent() throws Exception { + doReturn(null).when(userRepository).findOne(anyString()); + + assertThat(userService.find("id"), Matchers.nullValue()); + + verify(userRepository).findOne("id"); + } + + @Test + public void findByUsername_Success_IfPresent() throws Exception { + User user = createUser(); + + doReturn(user).when(userRepository).findByUsername(user.getUsername()); + + assertThat(userService.findByUsername(user.getUsername()), is(user)); + + verify(userRepository).findByUsername(user.getUsername()); + } + + @Test + public void findByUsername_Null_IfNotPresent() throws Exception { + doReturn(null).when(userRepository).findByUsername(anyString()); + + assertThat(userService.find("username"), Matchers.nullValue()); + + verify(userRepository).findOne("username"); + } + + @Test + public void findAll_Success_IfPresent() throws Exception { + User user = createUser(); + ArrayList userList = new ArrayList() {{ + add(user); + }}; + + doReturn(userList).when(userRepository).findAll(); + + assertThat(userService.findAll(), containsInAnyOrder(userList.toArray())); + + verify(userRepository).findAll(); + } + + @Test + public void findAll_Empty_IfNotPresent() throws Exception { + doReturn(Collections.emptyList()).when(userRepository).findAll(); + + assertThat(userService.findAll(), emptyCollectionOf(User.class)); + + verify(userRepository).findAll(); + } + + @Test + public void update_Success_IfPresent() throws Exception { + User user = createUser(); + + doReturn(user).when(userRepository).findOne(user.getId()); + + userService.update(user.getId(), user); + + verify(userRepository).save(user); + } + + @Test(expected = IllegalArgumentException.class) + public void update_ExceptionThrown_ifNull() throws Exception { + User user = createUser(); + + doReturn(null).when(userRepository).findOne(user.getId()); + + userService.update(user.getId(), null); + } + + @Test + public void update_NewCreated_ifNotPresent() throws Exception { + User user = createUser(); + + doReturn(user).when(userRepository).findOne(user.getId()); + + userService.update("not present", user); + + verify(userRepository).save(user); + } + + @Test + public void delete_Success_IfPresent() throws Exception { + User user = createUser(); + + userService.delete(user.getId()); + + verify(userRepository).delete(user.getId()); + } +} diff --git a/src/test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserSocialServiceTest.java b/src/test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserSocialServiceTest.java new file mode 100644 index 0000000..2c98be6 --- /dev/null +++ b/src/test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserSocialServiceTest.java @@ -0,0 +1,163 @@ +package com.github.vlsidlyarevich.unity.db.service; + +import com.github.vlsidlyarevich.unity.db.domain.UserSocial; +import com.github.vlsidlyarevich.unity.db.repository.UserSocialRepository; +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.ArrayList; +import java.util.Collections; + +import static com.github.vlsidlyarevich.unity.TestUtils.createUserSocial; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.emptyCollectionOf; +import static org.hamcrest.core.Is.is; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; + +@RunWith(SpringRunner.class) +public class DefaultUserSocialServiceTest { + + private UserSocialService userSocialService; + + @Mock + private UserSocialRepository userSocialRepository; + + @Before + public void setUp() { + userSocialService = new DefaultUserSocialService(userSocialRepository); + } + + @Test + public void create_Success_IfValid() throws Exception { + UserSocial userSocial = createUserSocial(); + + userSocialService.create(userSocial); + + verify(userSocialRepository).save(userSocial); + } + + @Test(expected = IllegalArgumentException.class) + public void create_ExceptionThrown_ifNull() throws Exception { + userSocialService.create(null); + } + + @Test + public void find_Success_IfPresent() throws Exception { + UserSocial userSocial = createUserSocial(); + + doReturn(userSocial).when(userSocialRepository).findOne(userSocial.getId()); + + assertThat(userSocialService.find(userSocial.getId()), is(userSocial)); + + verify(userSocialRepository).findOne(userSocial.getId()); + } + + @Test + public void find_Null_IfNotPresent() throws Exception { + doReturn(null).when(userSocialRepository).findOne(anyString()); + + assertThat(userSocialService.find("id"), Matchers.nullValue()); + + verify(userSocialRepository).findOne("id"); + } + + @Test + public void findByUserId_Success_IfPresent() throws Exception { + UserSocial userSocial = createUserSocial(); + + doReturn(userSocial).when(userSocialRepository).findByUserId(userSocial.getUserId()); + + assertThat(userSocialService.findByUserId(userSocial.getUserId()), is(userSocial)); + + verify(userSocialRepository).findByUserId(userSocial.getUserId()); + } + + @Test + public void findByUserId_Null_IfNotPresent() throws Exception { + assertThat(userSocialService.findByUserId("userid"), nullValue()); + + verify(userSocialRepository).findByUserId("userid"); + } + + @Test + public void findAll_Success_IfPresent() throws Exception { + UserSocial userSocial = createUserSocial(); + ArrayList userList = new ArrayList() {{ + add(userSocial); + }}; + + doReturn(userList).when(userSocialRepository).findAll(); + + assertThat(userSocialService.findAll(), containsInAnyOrder(userList.toArray())); + + verify(userSocialRepository).findAll(); + } + + @Test + public void findAll_Empty_IfNotPresent() throws Exception { + doReturn(Collections.emptyList()).when(userSocialRepository).findAll(); + + assertThat(userSocialService.findAll(), emptyCollectionOf(UserSocial.class)); + + verify(userSocialRepository).findAll(); + } + + @Test + public void update_Success_IfPresent() throws Exception { + UserSocial userSocial = createUserSocial(); + + doReturn(userSocial).when(userSocialRepository).findOne(userSocial.getId()); + + userSocialService.update(userSocial.getId(), userSocial); + + verify(userSocialRepository).save(userSocial); + } + + @Test(expected = IllegalArgumentException.class) + public void update_ExceptionThrown_ifNull() throws Exception { + UserSocial userSocial = createUserSocial(); + + doReturn(null).when(userSocialRepository).findOne(userSocial.getId()); + + userSocialService.update(userSocial.getId(), null); + } + + @Test + public void update_NewCreated_ifNotPresent() throws Exception { + UserSocial userSocial = createUserSocial(); + + doReturn(userSocial).when(userSocialRepository).findOne(userSocial.getId()); + + userSocialService.update("not present", userSocial); + + verify(userSocialRepository).save(userSocial); + } + + @Test + public void delete_Success_IfPresent() throws Exception { + UserSocial userSocial = createUserSocial(); + + userSocialService.delete(userSocial.getId()); + + verify(userSocialRepository).delete(userSocial.getId()); + } + + @Test + public void deleteByUserId_Success_IfPresent() throws Exception { + UserSocial userSocial = createUserSocial(); + + userSocialService.create(userSocial); + userSocialService.deleteByUserId(userSocial.getUserId()); + + Assert.assertTrue(userSocialService.findAll().isEmpty()); + } +} diff --git a/src/test/java/com/github/vlsidlyarevich/unity/db/service/FileSystemStorageServiceTest.java b/src/test/java/com/github/vlsidlyarevich/unity/db/service/FileSystemStorageServiceTest.java new file mode 100644 index 0000000..4a53a9c --- /dev/null +++ b/src/test/java/com/github/vlsidlyarevich/unity/db/service/FileSystemStorageServiceTest.java @@ -0,0 +1,67 @@ +package com.github.vlsidlyarevich.unity.db.service; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.multipart.MultipartFile; + +import java.nio.file.Path; +import java.util.List; + +@RunWith(SpringRunner.class) +public class FileSystemStorageServiceTest { + + private StorageService storageService; + + @After + public void cleanUp() { + storageService.deleteAll(); + } + + @Test + public void storeTest() throws Exception { + MultipartFile file = new MockMultipartFile("file", "file.gif", "image/png", "nonsensecontent".getBytes()); + + String id = storageService.store(file); + + Assert.assertEquals(storageService.load(id).toFile().getName(), id); + Assert.assertEquals(storageService.load(id).toFile().length(), file.getSize()); + } + + @Test + public void loadAllTest() throws Exception { + MultipartFile file1 = new MockMultipartFile("file1", "file.gif", "image/png", "nonsensecontent 1".getBytes()); + MultipartFile file2 = new MockMultipartFile("file2", "file.gif", "image/png", "nonsensecontent 2".getBytes()); + + String id1 = storageService.store(file1); + String id2 = storageService.store(file2); + + List resolvedFiles = storageService.loadAll(); + resolvedFiles.forEach(Path::toFile); + + Assert.assertTrue(storageService.loadAll().size() == 2); + } + + @Test + public void loadAsResourceTest() throws Exception { + MultipartFile file = new MockMultipartFile("file", "file.gif", "image/png", "nonsensecontent".getBytes()); + + String id = storageService.store(file); + + Assert.assertEquals(storageService.loadAsResource(id).getFilename(), id); + Assert.assertEquals(storageService.loadAsResource(id).contentLength(), file.getSize()); + } + + @Test + public void deleteTest() throws Exception { + MultipartFile file = new MockMultipartFile("file", "file.gif", "image/png", "nonsensecontent".getBytes()); + + String id = storageService.store(file); + + storageService.delete(id); + Assert.assertEquals(storageService.loadAll().size(), 0); + } +}