TSK-1355: Adjust all single() methods to use mybatis selectOne

This commit is contained in:
Joerg Heffner 2020-11-18 15:06:50 +01:00 committed by gitgoodjhe
parent cc2c5eb58c
commit 2a6d97132b
6 changed files with 59 additions and 24 deletions

View File

@ -493,14 +493,10 @@ public class ClassificationHistoryQueryImpl implements ClassificationHistoryQuer
LOGGER.debug("entry to single(), this = {}", this); LOGGER.debug("entry to single(), this = {}", this);
ClassificationHistoryEvent result = null; ClassificationHistoryEvent result = null;
try { try {
taskanaHistoryEngine.openConnection(); taskanaHistoryEngine.openConnection();
List<ClassificationHistoryEvent> results = result = taskanaHistoryEngine.getSqlSession().selectOne(LINK_TO_MAPPER, this);
taskanaHistoryEngine.getSqlSession().selectList(LINK_TO_MAPPER, this);
if (results.isEmpty()) {
return result;
} else {
result = results.get(0);
}
return result; return result;
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.error(SQL_EXCEPTION_MESSAGE, e.getCause()); LOGGER.error(SQL_EXCEPTION_MESSAGE, e.getCause());

View File

@ -702,14 +702,10 @@ public class TaskHistoryQueryImpl implements TaskHistoryQuery {
TaskHistoryEvent result = null; TaskHistoryEvent result = null;
try { try {
taskanaHistoryEngine.openConnection(); taskanaHistoryEngine.openConnection();
List<TaskHistoryEvent> results = result = taskanaHistoryEngine.getSqlSession().selectOne(LINK_TO_MAPPER, this);
taskanaHistoryEngine.getSqlSession().selectList(LINK_TO_MAPPER, this);
if (results.isEmpty()) {
return result;
} else {
result = results.get(0);
}
return result; return result;
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.error(SQL_EXCEPTION_MESSAGE, e.getCause()); LOGGER.error(SQL_EXCEPTION_MESSAGE, e.getCause());
return result; return result;

View File

@ -546,13 +546,8 @@ public class WorkbasketHistoryQueryImpl implements WorkbasketHistoryQuery {
WorkbasketHistoryEvent result = null; WorkbasketHistoryEvent result = null;
try { try {
taskanaHistoryEngine.openConnection(); taskanaHistoryEngine.openConnection();
List<WorkbasketHistoryEvent> results = result = taskanaHistoryEngine.getSqlSession().selectOne(LINK_TO_MAPPER, this);
taskanaHistoryEngine.getSqlSession().selectList(LINK_TO_MAPPER, this);
if (results.isEmpty()) {
return result;
} else {
result = results.get(0);
}
return result; return result;
} catch (SQLException e) { } catch (SQLException e) {
LOGGER.error(SQL_EXCEPTION_MESSAGE, e.getCause()); LOGGER.error(SQL_EXCEPTION_MESSAGE, e.getCause());

View File

@ -1,11 +1,13 @@
package acceptance.query; package acceptance.query;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import acceptance.AbstractAccTest; import acceptance.AbstractAccTest;
import java.time.Instant; import java.time.Instant;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import pro.taskana.classification.api.ClassificationCustomField; import pro.taskana.classification.api.ClassificationCustomField;
@ -96,17 +98,31 @@ class QueryClassificationHistoryAccTest extends AbstractAccTest {
@Test @Test
void should_ReturnSingleHistoryEvent_When_UsingSingleMethod() { void should_ReturnSingleHistoryEvent_When_UsingSingleMethod() {
ClassificationHistoryEvent single = ClassificationHistoryEvent single =
historyService.createClassificationHistoryQuery().userIdIn("peter").single(); historyService
.createClassificationHistoryQuery()
.userIdIn("peter")
.classificationIdIn("CLI:000000000000000000000000000000000001")
.single();
assertThat(single.getEventType()).isEqualTo(ClassificationHistoryEventType.CREATED.getName()); assertThat(single.getEventType()).isEqualTo(ClassificationHistoryEventType.CREATED.getName());
single = single =
historyService historyService
.createClassificationHistoryQuery() .createClassificationHistoryQuery()
.eventTypeIn(ClassificationHistoryEventType.CREATED.getName(), "xy") .eventTypeIn(ClassificationHistoryEventType.CREATED.getName(), "xy")
.classificationIdIn("CLI:000000000000000000000000000000000001")
.single(); .single();
assertThat(single.getUserId()).isEqualTo("peter"); assertThat(single.getUserId()).isEqualTo("peter");
} }
@Test
void should_ThrowException_When_SingleMethodRetrievesMoreThanOneEventFromDatabase() {
ClassificationHistoryQuery query =
getHistoryService().createClassificationHistoryQuery().userIdIn("peter");
assertThatThrownBy(() -> query.single()).isInstanceOf(TooManyResultsException.class);
}
@Test @Test
void should_ReturnCountOfEvents_When_UsingCountMethod() { void should_ReturnCountOfEvents_When_UsingCountMethod() {
long count = historyService.createClassificationHistoryQuery().userIdIn("peter").count(); long count = historyService.createClassificationHistoryQuery().userIdIn("peter").count();

View File

@ -2,11 +2,13 @@ package acceptance.query;
import static java.lang.String.CASE_INSENSITIVE_ORDER; import static java.lang.String.CASE_INSENSITIVE_ORDER;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import acceptance.AbstractAccTest; import acceptance.AbstractAccTest;
import java.time.Instant; import java.time.Instant;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import pro.taskana.common.api.BaseQuery.SortDirection; import pro.taskana.common.api.BaseQuery.SortDirection;
@ -86,17 +88,31 @@ class QueryTaskHistoryAccTest extends AbstractAccTest {
@Test @Test
void should_ReturnSingleHistoryEvent_When_UsingSingleMethod() { void should_ReturnSingleHistoryEvent_When_UsingSingleMethod() {
TaskHistoryEvent single = TaskHistoryEvent single =
getHistoryService().createTaskHistoryQuery().userIdIn("peter").single(); getHistoryService()
.createTaskHistoryQuery()
.userIdIn("peter")
.taskIdIn("TKI:000000000000000000000000000000000036")
.single();
assertThat(single.getEventType()).isEqualTo(TaskHistoryEventType.CREATED.getName()); assertThat(single.getEventType()).isEqualTo(TaskHistoryEventType.CREATED.getName());
single = single =
getHistoryService() getHistoryService()
.createTaskHistoryQuery() .createTaskHistoryQuery()
.eventTypeIn(TaskHistoryEventType.CREATED.getName(), "xy") .eventTypeIn(TaskHistoryEventType.CREATED.getName(), "xy")
.idIn("THI:000000000000000000000000000000000003")
.single(); .single();
assertThat(single.getUserId()).isEqualTo("peter"); assertThat(single.getUserId()).isEqualTo("peter");
} }
@Test
void should_ThrowException_When_SingleMethodRetrievesMoreThanOneEventFromDatabase() {
TaskHistoryQuery query = getHistoryService().createTaskHistoryQuery().userIdIn("peter");
assertThatThrownBy(() -> query.single()).isInstanceOf(TooManyResultsException.class);
}
@Test @Test
void should_ReturnCountOfEvents_When_UsingCountMethod() { void should_ReturnCountOfEvents_When_UsingCountMethod() {
long count = getHistoryService().createTaskHistoryQuery().userIdIn("peter").count(); long count = getHistoryService().createTaskHistoryQuery().userIdIn("peter").count();

View File

@ -1,11 +1,13 @@
package acceptance.query; package acceptance.query;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import acceptance.AbstractAccTest; import acceptance.AbstractAccTest;
import java.time.Instant; import java.time.Instant;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import pro.taskana.common.api.BaseQuery.SortDirection; import pro.taskana.common.api.BaseQuery.SortDirection;
@ -96,17 +98,31 @@ class QueryWorkbasketHistoryAccTest extends AbstractAccTest {
@Test @Test
void should_ReturnSingleHistoryEvent_When_UsingSingleMethod() { void should_ReturnSingleHistoryEvent_When_UsingSingleMethod() {
WorkbasketHistoryEvent single = WorkbasketHistoryEvent single =
historyService.createWorkbasketHistoryQuery().userIdIn("peter").single(); historyService
.createWorkbasketHistoryQuery()
.userIdIn("peter")
.idIn("WHI:000000000000000000000000000000000000")
.single();
assertThat(single.getEventType()).isEqualTo(WorkbasketHistoryEventType.CREATED.getName()); assertThat(single.getEventType()).isEqualTo(WorkbasketHistoryEventType.CREATED.getName());
single = single =
historyService historyService
.createWorkbasketHistoryQuery() .createWorkbasketHistoryQuery()
.eventTypeIn(WorkbasketHistoryEventType.CREATED.getName(), "xy") .eventTypeIn(WorkbasketHistoryEventType.CREATED.getName(), "xy")
.idIn("WHI:000000000000000000000000000000000000")
.single(); .single();
assertThat(single.getUserId()).isEqualTo("peter"); assertThat(single.getUserId()).isEqualTo("peter");
} }
@Test
void should_ThrowException_When_SingleMethodRetrievesMoreThanOneEventFromDatabase() {
WorkbasketHistoryQuery query =
getHistoryService().createWorkbasketHistoryQuery().userIdIn("peter");
assertThatThrownBy(() -> query.single()).isInstanceOf(TooManyResultsException.class);
}
@Test @Test
void should_ReturnCountOfEvents_When_UsingCountMethod() { void should_ReturnCountOfEvents_When_UsingCountMethod() {
long count = historyService.createWorkbasketHistoryQuery().userIdIn("peter").count(); long count = historyService.createWorkbasketHistoryQuery().userIdIn("peter").count();