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 default user analytics service writing
Browse files Browse the repository at this point in the history
  • Loading branch information
vlsidlyarevich committed Jun 24, 2017
1 parent dbb3df6 commit 936a957
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 51 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +16,7 @@
import static org.hamcrest.Matchers.*;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = UnityDatabaseIT.class)
@SpringBootTest
public class DefaultUserAnalyticsServiceIT {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,7 +17,7 @@
import static org.hamcrest.core.IsNull.notNullValue;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = UnityDatabaseIT.class)
@SpringBootTest
public class DefaultUserServiceIT {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,7 +17,7 @@
import static org.hamcrest.core.IsNull.notNullValue;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = UnityDatabaseIT.class)
@SpringBootTest
public class DefaultUserSocialServiceIT {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,7 +14,7 @@
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = UnityDatabaseIT.class)
@SpringBootTest
public class FileSystemStorageServiceIT {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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));
}
}

0 comments on commit 936a957

Please sign in to comment.