changed integration tests to unit tests and moved integration tests to own package

This commit is contained in:
Eberhard Heber 2017-07-03 10:34:38 +02:00
parent ae54ce512f
commit 2261466496
5 changed files with 357 additions and 68 deletions

View File

@ -1,46 +1,50 @@
package org.taskana.impl;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.taskana.CategoryService;
import org.taskana.TaskanaEngine;
import org.taskana.configuration.TaskanaEngineConfiguration;
import org.taskana.model.Category;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import javax.security.auth.login.LoginException;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.taskana.model.Category;
import org.taskana.model.mappings.CategoryMapper;
@RunWith(MockitoJUnitRunner.class)
public class CategoryServiceImplTest {
static int counter = 0;
private CategoryService categoryService;
@Before
public void setup() throws FileNotFoundException, SQLException, LoginException {
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:workbasket-test-db" + counter++);
ds.setPassword("sa");
ds.setUser("sa");
TaskanaEngineConfiguration taskEngineConfiguration = new TaskanaEngineConfiguration(ds, false);
@InjectMocks
CategoryServiceImpl categoryService;
TaskanaEngine te = taskEngineConfiguration.buildTaskanaEngine();
categoryService = te.getCategoryService();
}
@Mock
CategoryMapper categoryMapper;
@Test
public void testInsertCategory() {
doNothing().when(categoryMapper).insert(any());
Category category = new Category();
category.setId("0");
categoryService.insertCategory(category);
when(categoryMapper.findById(any())).thenReturn(category);
Assert.assertNotNull(categoryService.selectCategoryById(category.getId()));
}
@Test
public void testFindAllCategories() {
doNothing().when(categoryMapper).insert(any());
Category category0 = new Category();
category0.setId("0");
category0.setParentCategoryId("");
@ -49,12 +53,19 @@ public class CategoryServiceImplTest {
category1.setId("1");
category1.setParentCategoryId("");
categoryService.insertCategory(category1);
List<Category> categories = new ArrayList<>();
categories.add(category0);
when(categoryMapper.findByParentId("")).thenReturn(categories);
Assert.assertEquals(2, categoryService.selectCategories().size());
verify(categoryMapper, atLeast(2)).insert(any());
Assert.assertEquals(1, categoryService.selectCategories().size());
}
@Test
public void testFindByParentCategory() {
doNothing().when(categoryMapper).insert(any());
Category category0 = new Category();
category0.setId("0");
category0.setParentCategoryId("0");
@ -64,11 +75,21 @@ public class CategoryServiceImplTest {
category1.setParentCategoryId("0");
categoryService.insertCategory(category1);
List<Category> categories = new ArrayList<>();
categories.add(category0);
categories.add(category1);
when(categoryMapper.findByParentId(any())).thenReturn(categories);
verify(categoryMapper, atLeast(2)).insert(any());
Assert.assertEquals(2, categoryService.selectCategoriesByParentId("0").size());
}
@Test
public void testModifiedCategory() {
doNothing().when(categoryMapper).insert(any());
doNothing().when(categoryMapper).update(any());
Category category = new Category();
categoryService.insertCategory(category);
category.setDescription("TEST EVERYTHING");

View File

@ -1,53 +1,55 @@
package org.taskana.impl;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import javax.security.auth.login.LoginException;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.taskana.TaskanaEngine;
import org.taskana.WorkbasketService;
import org.taskana.configuration.TaskanaEngineConfiguration;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.taskana.exceptions.NotAuthorizedException;
import org.taskana.exceptions.WorkbasketNotFoundException;
import org.taskana.model.Workbasket;
import org.taskana.model.WorkbasketAccessItem;
import org.taskana.model.mappings.DistributionTargetMapper;
import org.taskana.model.mappings.WorkbasketAccessMapper;
import org.taskana.model.mappings.WorkbasketMapper;
@RunWith(MockitoJUnitRunner.class)
public class WorkbasketServiceImplTest {
WorkbasketService workbasketServiceImpl;
static int counter = 0;
@InjectMocks
WorkbasketServiceImpl workbasketServiceImpl;
@Before
public void setup() throws FileNotFoundException, SQLException, LoginException {
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:workbasket-test-db2" + counter++);
ds.setPassword("sa");
ds.setUser("sa");
TaskanaEngineConfiguration taskEngineConfiguration = new TaskanaEngineConfiguration(ds, false);
TaskanaEngine te = taskEngineConfiguration.buildTaskanaEngine();
workbasketServiceImpl = te.getWorkbasketService();
}
@Mock
WorkbasketMapper workbasketMapper;
@Mock
DistributionTargetMapper distributionTargetMapper;
@Mock
WorkbasketAccessMapper workbasketAccessMapper;
@Test
public void testInsertWorkbasket() throws NotAuthorizedException {
int before = workbasketServiceImpl.getWorkbaskets().size();
doNothing().when(workbasketMapper).insert(any());
Workbasket workbasket = new Workbasket();
workbasket.setId("1");
workbasketServiceImpl.createWorkbasket(workbasket);
Assert.assertEquals(before + 1, workbasketServiceImpl.getWorkbaskets().size());
Assert.assertEquals("1", workbasket.getId());
}
@Test
public void testSelectAllWorkbaskets() throws NotAuthorizedException {
int before = workbasketServiceImpl.getWorkbaskets().size();
doNothing().when(workbasketMapper).insert(any());
Workbasket workbasket0 = new Workbasket();
workbasket0.setId("0");
workbasketServiceImpl.createWorkbasket(workbasket0);
@ -57,11 +59,14 @@ public class WorkbasketServiceImplTest {
Workbasket workbasket2 = new Workbasket();
workbasket2.setId("2");
workbasketServiceImpl.createWorkbasket(workbasket2);
Assert.assertEquals(before + 3, workbasketServiceImpl.getWorkbaskets().size());
verify(workbasketMapper, atLeast(3)).insert(any());
}
@Test
public void testSelectWorkbasket() throws WorkbasketNotFoundException, NotAuthorizedException {
doNothing().when(workbasketMapper).insert(any());
Workbasket workbasket0 = new Workbasket();
workbasket0.setId("0");
workbasketServiceImpl.createWorkbasket(workbasket0);
@ -71,6 +76,11 @@ public class WorkbasketServiceImplTest {
Workbasket workbasket2 = new Workbasket();
workbasket2.setId("2");
workbasketServiceImpl.createWorkbasket(workbasket2);
verify(workbasketMapper, atLeast(3)).insert(any());
when(workbasketMapper.findById(any())).thenReturn(workbasket2);
Workbasket foundWorkbasket = workbasketServiceImpl.getWorkbasket("2");
Assert.assertEquals("2", foundWorkbasket.getId());
}
@ -82,6 +92,8 @@ public class WorkbasketServiceImplTest {
@Test
public void testSelectWorkbasketWithDistribution() throws WorkbasketNotFoundException, NotAuthorizedException {
doNothing().when(workbasketMapper).insert(any());
Workbasket workbasket0 = new Workbasket();
workbasket0.setId("0");
Workbasket workbasket1 = new Workbasket();
@ -92,6 +104,9 @@ public class WorkbasketServiceImplTest {
workbasket2.getDistributionTargets().add(workbasket0);
workbasket2.getDistributionTargets().add(workbasket1);
workbasketServiceImpl.createWorkbasket(workbasket2);
when(workbasketMapper.findById(any())).thenReturn(workbasket2);
Workbasket foundWorkbasket = workbasketServiceImpl.getWorkbasket("2");
Assert.assertEquals("2", foundWorkbasket.getId());
Assert.assertEquals(2, foundWorkbasket.getDistributionTargets().size());
@ -99,6 +114,8 @@ public class WorkbasketServiceImplTest {
@Test
public void testUpdateWorkbasket() throws Exception {
doNothing().when(workbasketMapper).insert(any());
Workbasket workbasket0 = new Workbasket();
workbasket0.setId("0");
Workbasket workbasket1 = new Workbasket();
@ -114,48 +131,57 @@ public class WorkbasketServiceImplTest {
workbasket2.getDistributionTargets().clear();
workbasket2.getDistributionTargets().add(workbasket3);
Thread.sleep(100);
doNothing().when(workbasketMapper).update(any());
workbasketServiceImpl.updateWorkbasket(workbasket2);
when(workbasketMapper.findById("2")).thenReturn(workbasket2);
Workbasket foundBasket = workbasketServiceImpl.getWorkbasket(workbasket2.getId());
when(workbasketMapper.findById("1")).thenReturn(workbasket1);
when(workbasketMapper.findById("3")).thenReturn(workbasket1);
List<Workbasket> distributionTargets = foundBasket.getDistributionTargets();
Assert.assertEquals(1, distributionTargets.size());
Assert.assertEquals("3", distributionTargets.get(0).getId());
Assert.assertNotEquals(workbasketServiceImpl.getWorkbasket("2").getCreated(),
workbasketServiceImpl.getWorkbasket("2").getModified());
Assert.assertEquals(workbasketServiceImpl.getWorkbasket("1").getCreated(),
workbasketServiceImpl.getWorkbasket("1").getModified());
Assert.assertEquals(workbasketServiceImpl.getWorkbasket("3").getCreated(),
workbasketServiceImpl.getWorkbasket("3").getModified());
Assert.assertNotEquals(workbasketServiceImpl.getWorkbasket("2").getCreated(), workbasketServiceImpl.getWorkbasket("2").getModified());
Assert.assertEquals(workbasketServiceImpl.getWorkbasket("1").getCreated(), workbasketServiceImpl.getWorkbasket("1").getModified());
Assert.assertEquals(workbasketServiceImpl.getWorkbasket("3").getCreated(), workbasketServiceImpl.getWorkbasket("3").getModified());
}
@Test
public void testInsertWorkbasketAccessUser() throws NotAuthorizedException {
doNothing().when(workbasketAccessMapper).insert(any());
WorkbasketAccessItem accessItem = new WorkbasketAccessItem();
accessItem.setWorkbasketId("1");
accessItem.setUserId("Arthur Dent");
accessItem.setOpen(true);
accessItem.setRead(true);
workbasketServiceImpl.createWorkbasketAuthorization(accessItem);
accessItem = workbasketServiceImpl.createWorkbasketAuthorization(accessItem);
Assert.assertEquals(1, workbasketServiceImpl.getAllAuthorizations().size());
Assert.assertNotNull(accessItem.getId());
}
@Test
public void testUpdateWorkbasketAccessUser() throws NotAuthorizedException {
doNothing().when(workbasketAccessMapper).insert(any());
WorkbasketAccessItem accessItem = new WorkbasketAccessItem();
accessItem.setWorkbasketId("1");
accessItem.setUserId("Arthur Dent");
accessItem.setOpen(true);
accessItem.setRead(true);
workbasketServiceImpl.createWorkbasketAuthorization(accessItem);
accessItem = workbasketServiceImpl.createWorkbasketAuthorization(accessItem);
Assert.assertEquals(1, workbasketServiceImpl.getAllAuthorizations().size());
Assert.assertNotNull(accessItem.getId());
doNothing().when(workbasketAccessMapper).update(any());
accessItem.setUserId("Zaphod Beeblebrox");
workbasketServiceImpl.updateWorkbasketAuthorization(accessItem);
Assert.assertEquals("Zaphod Beeblebrox", workbasketServiceImpl.getWorkbasketAuthorization(accessItem.getId()).getUserId());
Assert.assertEquals("Zaphod Beeblebrox", accessItem.getUserId());
}
}

View File

@ -0,0 +1,79 @@
package org.taskana.impl.integration;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.taskana.CategoryService;
import org.taskana.TaskanaEngine;
import org.taskana.configuration.TaskanaEngineConfiguration;
import org.taskana.model.Category;
import javax.security.auth.login.LoginException;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.time.LocalDate;
public class CategoryServiceImplIntTest {
static int counter = 0;
private CategoryService categoryService;
@Before
public void setup() throws FileNotFoundException, SQLException, LoginException {
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:test-db-category" + counter++);
ds.setPassword("sa");
ds.setUser("sa");
TaskanaEngineConfiguration taskEngineConfiguration = new TaskanaEngineConfiguration(ds, false);
TaskanaEngine te = taskEngineConfiguration.buildTaskanaEngine();
categoryService = te.getCategoryService();
}
@Test
public void testInsertCategory() {
Category category = new Category();
category.setId("0");
categoryService.insertCategory(category);
Assert.assertNotNull(categoryService.selectCategoryById(category.getId()));
}
@Test
public void testFindAllCategories() {
Category category0 = new Category();
category0.setId("0");
category0.setParentCategoryId("");
categoryService.insertCategory(category0);
Category category1 = new Category();
category1.setId("1");
category1.setParentCategoryId("");
categoryService.insertCategory(category1);
Assert.assertEquals(2, categoryService.selectCategories().size());
}
@Test
public void testFindByParentCategory() {
Category category0 = new Category();
category0.setId("0");
category0.setParentCategoryId("0");
categoryService.insertCategory(category0);
Category category1 = new Category();
category1.setId("1");
category1.setParentCategoryId("0");
categoryService.insertCategory(category1);
Assert.assertEquals(2, categoryService.selectCategoriesByParentId("0").size());
}
@Test
public void testModifiedCategory() {
Category category = new Category();
categoryService.insertCategory(category);
category.setDescription("TEST EVERYTHING");
categoryService.updateCategory(category);
Assert.assertEquals(category.getModified().toString(), LocalDate.now().toString());
}
}

View File

@ -1,4 +1,4 @@
package org.taskana.impl;
package org.taskana.impl.integration;
import java.io.FileNotFoundException;
import java.sql.SQLException;
@ -10,6 +10,8 @@ import org.taskana.TaskanaEngine;
import org.taskana.configuration.TaskanaEngineConfiguration;
import org.taskana.exceptions.NotAuthorizedException;
import org.taskana.exceptions.TaskNotFoundException;
import org.taskana.impl.TaskServiceImpl;
import org.taskana.impl.TaskanaEngineImpl;
import org.taskana.model.Task;
public class TaskServiceImplTransactionTest {
@ -17,7 +19,7 @@ public class TaskServiceImplTransactionTest {
@Test
public void testStart() throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException {
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:workbasket-test-db45");
ds.setURL("jdbc:h2:mem:test-db-taskservice-int1");
ds.setPassword("sa");
ds.setUser("sa");
TaskanaEngineConfiguration taskanaEngineConfiguration = new TaskanaEngineConfiguration(ds, true, false);
@ -42,7 +44,7 @@ public class TaskServiceImplTransactionTest {
public void testStartTransactionFail()
throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException {
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:workbasket-test-db46");
ds.setURL("jdbc:h2:mem:test-db-taskservice-int2");
ds.setPassword("sa");
ds.setUser("sa");
TaskanaEngineConfiguration taskanaEngineConfiguration = new TaskanaEngineConfiguration(ds, false, false);

View File

@ -0,0 +1,161 @@
package org.taskana.impl.integration;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.security.auth.login.LoginException;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.taskana.TaskanaEngine;
import org.taskana.WorkbasketService;
import org.taskana.configuration.TaskanaEngineConfiguration;
import org.taskana.exceptions.NotAuthorizedException;
import org.taskana.exceptions.WorkbasketNotFoundException;
import org.taskana.model.Workbasket;
import org.taskana.model.WorkbasketAccessItem;
public class WorkbasketServiceImplIntTest {
WorkbasketService workbasketServiceImpl;
static int counter = 0;
@Before
public void setup() throws FileNotFoundException, SQLException, LoginException {
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:test-db-workbasket" + counter++);
ds.setPassword("sa");
ds.setUser("sa");
TaskanaEngineConfiguration taskEngineConfiguration = new TaskanaEngineConfiguration(ds, false);
TaskanaEngine te = taskEngineConfiguration.buildTaskanaEngine();
workbasketServiceImpl = te.getWorkbasketService();
}
@Test
public void testInsertWorkbasket() throws NotAuthorizedException {
int before = workbasketServiceImpl.getWorkbaskets().size();
Workbasket workbasket = new Workbasket();
workbasket.setId("1");
workbasketServiceImpl.createWorkbasket(workbasket);
Assert.assertEquals(before + 1, workbasketServiceImpl.getWorkbaskets().size());
}
@Test
public void testSelectAllWorkbaskets() throws NotAuthorizedException {
int before = workbasketServiceImpl.getWorkbaskets().size();
Workbasket workbasket0 = new Workbasket();
workbasket0.setId("0");
workbasketServiceImpl.createWorkbasket(workbasket0);
Workbasket workbasket1 = new Workbasket();
workbasket1.setId("1");
workbasketServiceImpl.createWorkbasket(workbasket1);
Workbasket workbasket2 = new Workbasket();
workbasket2.setId("2");
workbasketServiceImpl.createWorkbasket(workbasket2);
Assert.assertEquals(before + 3, workbasketServiceImpl.getWorkbaskets().size());
}
@Test
public void testSelectWorkbasket() throws WorkbasketNotFoundException, NotAuthorizedException {
Workbasket workbasket0 = new Workbasket();
workbasket0.setId("0");
workbasketServiceImpl.createWorkbasket(workbasket0);
Workbasket workbasket1 = new Workbasket();
workbasket1.setId("1");
workbasketServiceImpl.createWorkbasket(workbasket1);
Workbasket workbasket2 = new Workbasket();
workbasket2.setId("2");
workbasketServiceImpl.createWorkbasket(workbasket2);
Workbasket foundWorkbasket = workbasketServiceImpl.getWorkbasket("2");
Assert.assertEquals("2", foundWorkbasket.getId());
}
@Test(expected = WorkbasketNotFoundException.class)
public void testGetWorkbasketFail() throws WorkbasketNotFoundException {
workbasketServiceImpl.getWorkbasket("fail");
}
@Test
public void testSelectWorkbasketWithDistribution() throws WorkbasketNotFoundException, NotAuthorizedException {
Workbasket workbasket0 = new Workbasket();
workbasket0.setId("0");
Workbasket workbasket1 = new Workbasket();
workbasket1.setId("1");
Workbasket workbasket2 = new Workbasket();
workbasket2.setId("2");
workbasket2.setDistributionTargets(new ArrayList<>());
workbasket2.getDistributionTargets().add(workbasket0);
workbasket2.getDistributionTargets().add(workbasket1);
workbasketServiceImpl.createWorkbasket(workbasket2);
Workbasket foundWorkbasket = workbasketServiceImpl.getWorkbasket("2");
Assert.assertEquals("2", foundWorkbasket.getId());
Assert.assertEquals(2, foundWorkbasket.getDistributionTargets().size());
}
@Test
public void testUpdateWorkbasket() throws Exception {
Workbasket workbasket0 = new Workbasket();
workbasket0.setId("0");
Workbasket workbasket1 = new Workbasket();
workbasket1.setId("1");
Workbasket workbasket2 = new Workbasket();
workbasket2.setId("2");
workbasket2.getDistributionTargets().add(workbasket0);
workbasket2.getDistributionTargets().add(workbasket1);
workbasketServiceImpl.createWorkbasket(workbasket2);
Workbasket workbasket3 = new Workbasket();
workbasket3.setId("3");
workbasket2.getDistributionTargets().clear();
workbasket2.getDistributionTargets().add(workbasket3);
Thread.sleep(100);
workbasketServiceImpl.updateWorkbasket(workbasket2);
Workbasket foundBasket = workbasketServiceImpl.getWorkbasket(workbasket2.getId());
List<Workbasket> distributionTargets = foundBasket.getDistributionTargets();
Assert.assertEquals(1, distributionTargets.size());
Assert.assertEquals("3", distributionTargets.get(0).getId());
Assert.assertNotEquals(workbasketServiceImpl.getWorkbasket("2").getCreated(),
workbasketServiceImpl.getWorkbasket("2").getModified());
Assert.assertEquals(workbasketServiceImpl.getWorkbasket("1").getCreated(),
workbasketServiceImpl.getWorkbasket("1").getModified());
Assert.assertEquals(workbasketServiceImpl.getWorkbasket("3").getCreated(),
workbasketServiceImpl.getWorkbasket("3").getModified());
}
@Test
public void testInsertWorkbasketAccessUser() throws NotAuthorizedException {
WorkbasketAccessItem accessItem = new WorkbasketAccessItem();
accessItem.setWorkbasketId("1");
accessItem.setUserId("Arthur Dent");
accessItem.setOpen(true);
accessItem.setRead(true);
workbasketServiceImpl.createWorkbasketAuthorization(accessItem);
Assert.assertEquals(1, workbasketServiceImpl.getAllAuthorizations().size());
}
@Test
public void testUpdateWorkbasketAccessUser() throws NotAuthorizedException {
WorkbasketAccessItem accessItem = new WorkbasketAccessItem();
accessItem.setWorkbasketId("1");
accessItem.setUserId("Arthur Dent");
accessItem.setOpen(true);
accessItem.setRead(true);
workbasketServiceImpl.createWorkbasketAuthorization(accessItem);
Assert.assertEquals(1, workbasketServiceImpl.getAllAuthorizations().size());
accessItem.setUserId("Zaphod Beeblebrox");
workbasketServiceImpl.updateWorkbasketAuthorization(accessItem);
Assert.assertEquals("Zaphod Beeblebrox", workbasketServiceImpl.getWorkbasketAuthorization(accessItem.getId()).getUserId());
}
}