Skip to content
This repository has been archived by the owner on Jun 20, 2018. It is now read-only.

Commit

Permalink
#77 - unit test for db services added
Browse files Browse the repository at this point in the history
  • Loading branch information
vlsidlyarevich committed Jun 25, 2017
1 parent 936a957 commit 655928c
Show file tree
Hide file tree
Showing 5 changed files with 395 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -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");
}

Expand All @@ -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();
}

Expand All @@ -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();
}

Expand Down
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());
}
}
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());
}
}
Loading

0 comments on commit 655928c

Please sign in to comment.