Skip to content
Snippets Groups Projects
Commit 42fec1f8 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-532 refactor: use event to finish command

parent 68736e03
Branches
Tags
No related merge requests found
package de.itvsh.ozg.pluto.command.kommentar;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.Optional;
......@@ -43,7 +42,6 @@ class GrpcKommentarServiceTest {
private VorgangService vorgangService;
@Mock
private KommentarService kommentarService;
private final StreamRecorder<GrpcCommandResponse> responseObserver = StreamRecorder.create();
@Nested
class TestCreateAndGetCommand {
......
......@@ -105,7 +105,7 @@ class GrpcCommandServiceITCase {
assertThat(command.getCreatedBy()).isNotNull();
assertThat(command.getStatus()).isEqualTo(CommandStatus.FINISHED);
assertThat(command.getRelationId()).isEqualTo(persistedDocument.getId());
assertThat(command.getRelationVersion()).isEqualTo(VorgangTestFactory.VERSION + 1);
assertThat(command.getRelationVersion()).isEqualTo(VorgangTestFactory.VERSION);
assertThat(command.getPreviousStatus()).isEqualTo(VorgangTestFactory.STATUS);
}
}
......@@ -168,8 +168,8 @@ class GrpcCommandServiceITCase {
mongoOperations.dropCollection(Vorgang.class);
mongoOperations.save(VorgangTestFactory.createBuilder().status(Status.ANGENOMMEN).build());
mongoOperations.save(
CommandTestFactory.createBuilder().relationId(VorgangTestFactory.ID).build());
mongoOperations.save(CommandTestFactory.createBuilder()
.relationId(VorgangTestFactory.ID).relationVersion(VorgangTestFactory.VERSION - 1).build());
}
@Test
......@@ -183,14 +183,24 @@ class GrpcCommandServiceITCase {
void shouldUpdateVorgang() {
service.revokeCommand(request, responseObserver);
verifyVorgang();
Vorgang vorgang = mongoOperations.findById(VorgangTestFactory.ID, Vorgang.class);
assertThat(vorgang).isNotNull().extracting(Vorgang::getStatus).isEqualTo(Status.NEU);
}
@Test
void shouldUpdateCommand() {
service.revokeCommand(request, responseObserver);
verifyCommand();
Command command = mongoOperations.findById(CommandTestFactory.ID, Command.class);
assertThat(command).isNotNull().extracting(Command::getStatus).isEqualTo(CommandStatus.REVOKED);
}
@Test
void shouldNOTUpdateRelationVersion() {
service.revokeCommand(request, responseObserver);
Command command = mongoOperations.findById(CommandTestFactory.ID, Command.class);
assertThat(command).isNotNull().extracting(Command::getRelationVersion).isEqualTo(VorgangTestFactory.VERSION - 1);
}
}
......@@ -201,7 +211,8 @@ class GrpcCommandServiceITCase {
void persistVorgangWithVorgangVerwerfenCommand() {
mongoOperations.save(VorgangTestFactory.createBuilder().status(Status.VERWORFEN).build());
mongoOperations.save(CommandTestFactory.createBuilder().relationId(VorgangTestFactory.ID).order(Order.VORGANG_VERWERFEN).build());
mongoOperations.save(CommandTestFactory.createBuilder().relationId(VorgangTestFactory.ID)
.relationVersion(VorgangTestFactory.VERSION - 1).order(Order.VORGANG_VERWERFEN).build());
}
@Test
......@@ -215,14 +226,16 @@ class GrpcCommandServiceITCase {
void shouldUpdateVorgang() {
service.revokeCommand(request, responseObserver);
verifyVorgang();
Vorgang vorgang = mongoOperations.findById(VorgangTestFactory.ID, Vorgang.class);
assertThat(vorgang).isNotNull().extracting(Vorgang::getStatus).isEqualTo(Status.NEU);
}
@Test
void shouldUpdateCommand() {
service.revokeCommand(request, responseObserver);
verifyCommand();
Command command = mongoOperations.findById(CommandTestFactory.ID, Command.class);
assertThat(command).isNotNull().extracting(Command::getStatus).isEqualTo(CommandStatus.REVOKED);
}
}
......
......@@ -5,7 +5,6 @@ import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
......@@ -25,7 +24,6 @@ import de.itvsh.ozg.pluto.command.CreateCommandRequest;
import de.itvsh.ozg.pluto.command.CreateCommandRequestTestFactory;
import de.itvsh.ozg.pluto.command.GrpcCommandResponseMapper;
import de.itvsh.ozg.pluto.command.GrpcCreateCommandRequestMapper;
import de.itvsh.ozg.pluto.command.Order;
import de.itvsh.ozg.pluto.common.grpc.StreamRecorder;
import de.itvsh.ozg.pluto.grpc.command.GrpcCommand;
import de.itvsh.ozg.pluto.grpc.command.GrpcCommandResponse;
......@@ -98,13 +96,6 @@ class GrpcVorgangCommandServiceTest {
verify(commandService).persistCommand(createComandRequest, VorgangTestFactory.STATUS);
}
@Test
void shouldCallSetCommandFinish() throws Exception {
callCreateCommand();
verify(service).setCommandFinished(CreateCommandRequestTestFactory.ORDER, persistedCommand, updatedVorgang);
}
@Test
void shouldReturnValue() throws Exception {
callCreateCommand();
......@@ -124,24 +115,6 @@ class GrpcVorgangCommandServiceTest {
}
}
@Nested
class TestSetCommandFinish {
@Test
void shouldSetCommandFinish() {
service.setCommandFinished(Order.VORGANG_ANNEHMEN, CommandTestFactory.create(), VorgangTestFactory.create());
verify(commandService).setCommandFinished(CommandTestFactory.ID, VorgangTestFactory.VERSION);
}
@Test
void shouldNotSetCommandFinish() {
service.setCommandFinished(Order.REDIRECT_VORGANG, CommandTestFactory.create(), VorgangTestFactory.create());
verify(commandService, times(0)).setCommandFinished(any());
}
}
@Nested
class TestGetVorgangCommand {
......@@ -195,13 +168,12 @@ class GrpcVorgangCommandServiceTest {
@Nested
class TestRevokeCommand {
final String commandId = UUID.randomUUID().toString();
final GrpcRevokeCommandRequest request = GrpcRevokeCommandRequest.newBuilder().setId(commandId).build();
private final GrpcRevokeCommandRequest request = GrpcRevokeCommandRequest.newBuilder().setId(CommandTestFactory.ID).build();
private final StreamRecorder<GrpcCommandResponse> responseObserver = StreamRecorder.create();
@BeforeEach
void mockCommandService() {
Command vorgangCommand = CommandTestFactory.createBuilder().id(commandId).build();
Command vorgangCommand = CommandTestFactory.createBuilder().id(CommandTestFactory.ID).build();
when(commandService.findCommand(any())).thenReturn(Optional.of(vorgangCommand));
}
......@@ -214,19 +186,58 @@ class GrpcVorgangCommandServiceTest {
}
@Test
void shouldCallServiceSetCommandRevoked() throws Exception {
void shouldCallMapper() throws Exception {
callRevokeCommand();
verify(commandResponseMapper).toGrpc(any());
}
@Test
void shouldCallMapper() throws Exception {
void shouldCallServiceSetCommandRevoked() throws Exception {
callRevokeCommand();
verify(commandService).setCommandRevoked(any());
}
@Test
void shouldCallProceed() throws Exception {
callRevokeCommand();
verify(service).proceedRevokeCommand(CommandTestFactory.ID);
}
@Nested
class TestProceedRevokeCommand {
private Command command = CommandTestFactory.create();;
@BeforeEach
void init() {
when(commandService.findCommand(anyString())).thenReturn(Optional.of(command));
}
@Test
void shouldLoadCommand() {
service.proceedRevokeCommand(CommandTestFactory.ID);
verify(service).getCommand(CommandTestFactory.ID);
}
@Test
void shouldRevokeCommand() {
service.proceedRevokeCommand(CommandTestFactory.ID);
verify(vorgangService).revokeStatusChange(command);
}
@Test
void shouldSetCommandRevoked() {
service.proceedRevokeCommand(CommandTestFactory.ID);
verify(commandService).setCommandRevoked(CommandTestFactory.ID);
}
}
private void callRevokeCommand() throws Exception {
service.revokeCommand(request, responseObserver);
......
package de.itvsh.ozg.pluto.vorgang;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.*;
import java.util.Optional;
......@@ -12,6 +12,8 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.test.context.ActiveProfiles;
import de.itvsh.ozg.pluto.command.Command;
import de.itvsh.ozg.pluto.command.CommandTestFactory;
import de.itvsh.ozg.pluto.vorgang.Vorgang.Status;
@SpringBootTest
......@@ -31,17 +33,20 @@ class VorgangServiceITCase {
class TestVorgangUpdateStatus {
private Vorgang persistedVorgang;
private Command command;
@BeforeEach
void persistVorgang() {
mongoOperations.dropCollection(Vorgang.class);
persistedVorgang = repository.save(VorgangTestFactory.create());
command = CommandTestFactory.createBuilder().relationId(persistedVorgang.getId()).relationVersion(persistedVorgang.getVersion()).build();
}
@Test
void annehmenShouldUpdateStatusAndVersion() {
vorgangService.annehmen(persistedVorgang.getId(), persistedVorgang.getVersion());
vorgangService.annehmen(command);
Optional<Vorgang> vorgang = repository.findById(persistedVorgang.getId());
......@@ -52,7 +57,7 @@ class VorgangServiceITCase {
@Test
void verwerfenShouldUpdateStatusAndVersion() {
vorgangService.verwerfen(persistedVorgang.getId(), persistedVorgang.getVersion());
vorgangService.verwerfen(command);
Optional<Vorgang> vorgang = repository.findById(persistedVorgang.getId());
......@@ -63,7 +68,7 @@ class VorgangServiceITCase {
@Test
void bescheidenShouldUpdateStatusAndVersion() {
vorgangService.bescheiden(persistedVorgang.getId(), persistedVorgang.getVersion());
vorgangService.bescheiden(command);
Optional<Vorgang> vorgang = repository.findById(persistedVorgang.getId());
......@@ -74,7 +79,7 @@ class VorgangServiceITCase {
@Test
void wiedereroeffnenShouldUpdateStatusAndVersion() {
vorgangService.wiedereroeffnen(persistedVorgang.getId(), persistedVorgang.getVersion());
vorgangService.wiedereroeffnen(command);
Optional<Vorgang> vorgang = repository.findById(persistedVorgang.getId());
......
......@@ -6,7 +6,6 @@ import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.util.Optional;
import java.util.function.BiFunction;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
......@@ -17,8 +16,11 @@ import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.mongodb.core.MongoOperations;
import de.itvsh.ozg.pluto.command.Command;
import de.itvsh.ozg.pluto.command.CommandTestFactory;
import de.itvsh.ozg.pluto.common.errorhandling.NotFoundException;
import de.itvsh.ozg.pluto.vorgang.Vorgang.Status;
......@@ -30,9 +32,10 @@ class VorgangServiceTest {
@Mock
private VorgangRepository repository;
@Mock
private MongoOperations mongoOperations;
@Mock
private ApplicationEventPublisher publisher;
@Captor
private ArgumentCaptor<Vorgang> vorgangCaptor;
......@@ -93,34 +96,51 @@ class VorgangServiceTest {
@Nested
class TestVorgangStatusChange {
private Command command = CommandTestFactory.create();
@BeforeEach
void mockRepository() {
when(repository.findById(any())).thenReturn(Optional.of(VorgangTestFactory.create()));
}
@Nested
class TestUpdateStatus {
class TestExecuteStatusChangeCommand {
@BeforeEach
void doServiceCall() {
service.updateStatus(VorgangTestFactory.ID, VorgangTestFactory.VERSION, Status.ANGENOMMEN);
}
@Captor // NOSONAR
private ArgumentCaptor<StatusChangedEvent> eventCaptor;
@Test
void shouldUpdateStatus() {
doServiceCall();
verifyServiceCall(Status.ANGENOMMEN);
}
@Test
void shouldCallRepository() {
doServiceCall();
verify(repository).findById(VorgangTestFactory.ID);
}
@Test
void shouldPublishExecutedEvent() {
doServiceCall();
verify(publisher).publishEvent(eventCaptor.capture());
assertThat(eventCaptor.getValue()).isInstanceOf(StatusChangedEvent.class)
.extracting(StatusChangedEvent::getSource).isSameAs(command);
}
private void doServiceCall() {
service.executeStatusChangeCommand(command, Status.ANGENOMMEN);
}
}
@DisplayName("should update vorgang by the given status 'ANGENOMMEN")
@Test
void shouldUpdateByStatusAngenommen() {
applyServiceCallParam((id, version) -> service.annehmen(id, version));
service.annehmen(command);
verifyServiceCall(Status.ANGENOMMEN);
}
......@@ -128,7 +148,7 @@ class VorgangServiceTest {
@DisplayName("should update vorgang by the given status 'VERWORFEN")
@Test
void shouldUpdateByStatusVerworfen() {
applyServiceCallParam((id, version) -> service.verwerfen(id, version));
service.verwerfen(command);
verifyServiceCall(Status.VERWORFEN);
}
......@@ -136,7 +156,7 @@ class VorgangServiceTest {
@DisplayName("should update vorgang by the given status 'IN_BEARBEITUNG")
@Test
void shouldUpdateByStatusBearbeiten() {
applyServiceCallParam((id, version) -> service.bearbeiten(id, version));
service.bearbeiten(command);
verifyServiceCall(Status.IN_BEARBEITUNG);
}
......@@ -144,7 +164,7 @@ class VorgangServiceTest {
@DisplayName("should update vorgang by the given status 'BESCHIEDEN")
@Test
void shouldUpdateByStatusBeschieden() {
applyServiceCallParam((id, version) -> service.bescheiden(id, version));
service.bescheiden(command);
verifyServiceCall(Status.BESCHIEDEN);
}
......@@ -152,7 +172,7 @@ class VorgangServiceTest {
@DisplayName("should update vorgang by the given status 'ABGESCHLOSSEN")
@Test
void shouldUpdateByStatusAbgeschlossen() {
applyServiceCallParam((id, version) -> service.abschliessen(id, version));
service.abschliessen(command);
verifyServiceCall(Status.ABGESCHLOSSEN);
}
......@@ -160,17 +180,29 @@ class VorgangServiceTest {
@DisplayName("should update vorgang by the given status 'REDIRECT")
@Test
void shouldUpdateByStatusRedirect() {
applyServiceCallParam((id, version) -> service.redirect(id, version));
service.redirect(command);
verifyServiceCall(Status.REDIRECT);
verify(service).doUpdateStatus(CommandTestFactory.RELATION_ID, CommandTestFactory.RELATION_VERSION, Status.REDIRECT);
}
private void applyServiceCallParam(BiFunction<String, Long, Vorgang> serviceStatusFunction) {
serviceStatusFunction.apply(VorgangTestFactory.ID, VorgangTestFactory.VERSION);
private void verifyServiceCall(Status status) {
verify(service).executeStatusChangeCommand(command, status);
}
}
private void verifyServiceCall(Status status) {
verify(service).updateStatus(VorgangTestFactory.ID, VorgangTestFactory.VERSION, status);
@Nested
class TestRevokeStatusChange {
@BeforeEach
void mockRepository() {
when(repository.findById(any())).thenReturn(Optional.of(VorgangTestFactory.create()));
}
@Test
void shouldRevindToOldStatus() {
service.revokeStatusChange(CommandTestFactory.create());
verify(service).doUpdateStatus(CommandTestFactory.RELATION_ID, CommandTestFactory.RELATION_VERSION + 1, CommandTestFactory.PREV_STATUS);
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment