This repository has been archived by the owner on Jun 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#77 - unit test for db services added
- Loading branch information
1 parent
936a957
commit 655928c
Showing
5 changed files
with
395 additions
and
3 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
...in/java/com/github/vlsidlyarevich/unity/db/exception/FileSystemFileNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
src/test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
163 changes: 163 additions & 0 deletions
163
src/test/java/com/github/vlsidlyarevich/unity/db/service/DefaultUserSocialServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
Oops, something went wrong.