diff --git a/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/CommandRepository.java b/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/CommandRepository.java index 799cf72d841c523ca1bbbd866ddeb4770789110d..012a8c3613bea48d6d5dae7c6beba98e3494f10f 100644 --- a/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/CommandRepository.java +++ b/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/CommandRepository.java @@ -58,6 +58,7 @@ class CommandRepository { private static final String MONGODB_ID = "id"; private static final String MONGODB_STATUS = "status"; + private static final String MONGODB_REFERENCE_STATUS = "$status"; private static final String MONGODB_FINISHED_AT = "finishedAt"; private static final String MONGODB_RELATION_VERSION = "relationVersion"; private static final String MONGODB_ERROR_MSG = "errorMessage"; @@ -269,7 +270,7 @@ class CommandRepository { CaseOperator.when(Eq.valueOf(MONGODB_STATUS).equalToValue(CommandStatus.NEW)).then(CommandStatus.CANCELED), CaseOperator.when(Eq.valueOf(MONGODB_STATUS).equalToValue(CommandStatus.PENDING)).then(CommandStatus.REVOKE_PENDING), CaseOperator.when(Eq.valueOf(MONGODB_STATUS).equalToValue(CommandStatus.FINISHED)).then(CommandStatus.REVOKE_PENDING)) - .defaultTo("$status"); + .defaultTo(MONGODB_REFERENCE_STATUS); return Aggregation.newUpdate(SetOperation.set(MONGODB_STATUS).toValue(switchOperation)); } @@ -286,7 +287,7 @@ class CommandRepository { var switchOperation = Switch.switchCases( CaseOperator.when(Eq.valueOf(MONGODB_STATUS).equalToValue(CommandStatus.NEW)).then(CommandStatus.CANCELED), CaseOperator.when(Eq.valueOf(MONGODB_STATUS).equalToValue(CommandStatus.FINISHED)).then(CommandStatus.REVOKE_PENDING) - ).defaultTo("$status"); + ).defaultTo(MONGODB_REFERENCE_STATUS); return Aggregation.newUpdate(SetOperation.set(MONGODB_STATUS).toValue(switchOperation)); } diff --git a/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/CommandService.java b/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/CommandService.java index 1be2e2f0f0c81071f5d1dc766f8cd4424c419e56..0f6b0783d58177fe71678e7483b74162751ef928 100644 --- a/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/CommandService.java +++ b/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/CommandService.java @@ -54,6 +54,7 @@ import de.ozgcloud.vorgang.callcontext.CallContextUser; import de.ozgcloud.vorgang.callcontext.CurrentUserService; import de.ozgcloud.vorgang.callcontext.User; import de.ozgcloud.vorgang.common.errorhandling.NotFoundException; +import de.ozgcloud.vorgang.common.errorhandling.RevokeFailedException; import lombok.NonNull; @Service @@ -116,7 +117,7 @@ public class CommandService { public void setCommandFinished(String commandId, String createdResource) { var command = getById(commandId); - if (isRevokeCommand(command)) { + if (shouldRevoke(command)) { repository.setRevokeStatus(commandId); publishRevokeCommandEvent(command); return; @@ -126,7 +127,7 @@ public class CommandService { getCompletableParentId(command).ifPresent(this::publishCommandExecutedEvent); } - boolean isRevokeCommand(Command command) { + boolean shouldRevoke(Command command) { return command.getStatus() == CommandStatus.REVOKE_PENDING || isParentCommandFailed(command); } @@ -149,12 +150,8 @@ public class CommandService { public void setCommandError(String commandId, String errorMessage) { repository.setErrorMessage(commandId, errorMessage); - repository.getParentId(commandId).map(this::setErrorStatus) - .filter(previousCommand -> previousCommand.getStatus() != CommandStatus.ERROR) - .map(Command::getId).ifPresent(parentId -> { - publishCommandFailedEvent(parentId, "Command %s failed because subcommand %s failed".formatted(parentId, commandId)); - revokeSubCommands(parentId); - }); + repository.getParentId(commandId).map(this::setErrorStatus).filter(this::notErrorStatus) + .map(Command::getId).ifPresent(parentId -> handleCommandError(commandId, parentId)); } Command setErrorStatus(String commandId) { @@ -162,27 +159,41 @@ public class CommandService { .orElseThrow(() -> new NotFoundException(Command.class, commandId)); } - public void revokeSubCommands(String parentId) { - repository.findSubCommandIds(parentId).forEach(this::revokeCommand); + boolean notErrorStatus(Command command) { + return command.getStatus() != CommandStatus.ERROR; + } + + void handleCommandError(String commandId, String parentId) { + publishCommandFailedEvent(parentId, "Command %s failed because subcommand %s failed".formatted(parentId, commandId)); + revokeSubCommands(parentId); } void publishCommandFailedEvent(String commandId, String errorMessage) { publisher.publishEvent(new CommandFailedEvent(commandId, errorMessage)); } + public void revokeSubCommands(String parentId) { + repository.findSubCommandIds(parentId).forEach(this::revokeCommand); + } + public void setCommandRevoked(String commandId) { repository.updateCommandStatus(commandId, CommandStatus.REVOKED); } public Command revokeCommand(String commandId) { var updatedCommand = repository.setRevokeStatusIfNotPending(commandId); - if (isNull(updatedCommand)) { + validateRevokeResult(updatedCommand, commandId); + publishRevokeCommandEvent(updatedCommand); + return updatedCommand; + } + + void validateRevokeResult(Command command, String commandId) { + if (isNull(command)) { throw new NotFoundException(Command.class, commandId); } - if (updatedCommand.getStatus() == CommandStatus.REVOKE_PENDING) { - publishRevokeCommandEvent(updatedCommand); + if (command.getStatus() != CommandStatus.REVOKE_PENDING) { + throw new RevokeFailedException(command.getId(), "Unexpected status '%s'. (expected: NEW or FINISHED)".formatted(command.getStatus())); } - return updatedCommand; } void publishRevokeCommandEvent(Command command) { diff --git a/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/GrpcCommandService.java b/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/GrpcCommandService.java index 2c766482361af3a881e54da567f32052195fa478..5e83fe5ab0aa3c5f0c85b8fe4dc04bc51ec20a34 100644 --- a/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/GrpcCommandService.java +++ b/vorgang-manager-server/src/main/java/de/ozgcloud/vorgang/command/GrpcCommandService.java @@ -38,7 +38,6 @@ import de.ozgcloud.command.CommandExecutedEvent; import de.ozgcloud.command.CommandStatus; import de.ozgcloud.vorgang.command.CommandResponse.ResponseCode; import de.ozgcloud.vorgang.common.errorhandling.NotFoundException; -import de.ozgcloud.vorgang.common.errorhandling.RevokeFailedException; import de.ozgcloud.vorgang.common.security.PolicyService; import de.ozgcloud.vorgang.grpc.command.CommandServiceGrpc.CommandServiceImplBase; import de.ozgcloud.vorgang.grpc.command.GrpcAddSubCommandsRequest; @@ -106,17 +105,10 @@ public class GrpcCommandService extends CommandServiceImplBase { public void revokeCommand(GrpcRevokeCommandRequest request, StreamObserver<GrpcCommandResponse> responseObserver) { policyService.checkPermissionByCommand(request.getId()); var updatedCommand = commandService.revokeCommand(request.getId()); - validateRevokeResult(updatedCommand); responseObserver.onNext(mapToGrpcResponse(updatedCommand)); responseObserver.onCompleted(); } - void validateRevokeResult(Command command) { - if (command.getStatus() != CommandStatus.REVOKE_PENDING) { - throw new RevokeFailedException(command.getId(), "Unexpected status '%s'. (expected: NEW or FINISHED)".formatted(command.getStatus())); - } - } - Command getCommand(String commandId) { return commandService.findById(commandId).orElseThrow(() -> new NotFoundException(Command.class, commandId)); } diff --git a/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/CommandRepositoryITCase.java b/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/CommandRepositoryITCase.java index c5d52feb6a7e0c74662328fd61d65cd9cb01f346..6f99e9b91bef7f1664e80194b2755173a0c1da37 100644 --- a/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/CommandRepositoryITCase.java +++ b/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/CommandRepositoryITCase.java @@ -659,34 +659,39 @@ class CommandRepositoryITCase { @Test void shouldSetCanceled() { - var command = mongoOperations.save(CommandTestFactory.createBuilder().id(null).status(CommandStatus.NEW).build()); + var command = createCommand(CommandStatus.NEW); repository.setRevokeStatusIfNotPending(command.getId()); - var result = repository.findById(command.getId()); - assertThat(result).isPresent().get().extracting(Command::getStatus).isEqualTo(CommandStatus.CANCELED); + assertThat(getCommandStatus(command)).isEqualTo(CommandStatus.CANCELED); } @Test void shouldSetRevokePendingWhenFinished() { - var command = mongoOperations.save(CommandTestFactory.createBuilder().id(null).status(CommandStatus.FINISHED).build()); + var command = createCommand(CommandStatus.FINISHED); repository.setRevokeStatusIfNotPending(command.getId()); - var result = repository.findById(command.getId()); - assertThat(result).isPresent().get().extracting(Command::getStatus).isEqualTo(CommandStatus.REVOKE_PENDING); + assertThat(getCommandStatus(command)).isEqualTo(CommandStatus.REVOKE_PENDING); } @DisplayName("should not update when") @ParameterizedTest(name = "status is {0}") @EnumSource(value = CommandStatus.class, names = { "NEW", "FINISHED" }, mode = EnumSource.Mode.EXCLUDE) void shouldNotUpdate(CommandStatus status) { - var command = mongoOperations.save(CommandTestFactory.createBuilder().id(null).status(status).build()); + var command = createCommand(status); repository.setRevokeStatusIfNotPending(command.getId()); - var result = repository.findById(command.getId()); - assertThat(result).isPresent().get().extracting(Command::getStatus).isEqualTo(status); + assertThat(getCommandStatus(command)).isEqualTo(status); + } + + private Command createCommand(CommandStatus status) { + return mongoOperations.save(CommandTestFactory.createBuilder().id(null).status(status).build()); + } + + private CommandStatus getCommandStatus(Command command) { + return mongoOperations.findById(command.getId(), Command.class).getStatus(); } } } \ No newline at end of file diff --git a/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/CommandServiceTest.java b/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/CommandServiceTest.java index 9af3b3021ec2c08f5d78d1709b4b1dc1b88e01c9..9023fce252fddd52337737be9588ff5a69af046e 100644 --- a/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/CommandServiceTest.java +++ b/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/CommandServiceTest.java @@ -35,7 +35,6 @@ import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -59,6 +58,7 @@ import de.ozgcloud.vorgang.callcontext.CallContextUserTestFactory; import de.ozgcloud.vorgang.callcontext.CurrentUserService; import de.ozgcloud.vorgang.callcontext.UserTestFactory; import de.ozgcloud.vorgang.common.errorhandling.NotFoundException; +import de.ozgcloud.vorgang.common.errorhandling.RevokeFailedException; import de.ozgcloud.vorgang.vorgang.VorgangTestFactory; class CommandServiceTest { @@ -218,7 +218,7 @@ class CommandServiceTest { @BeforeEach void init() { - doReturn(true).when(service).isRevokeCommand(any()); + doReturn(true).when(service).shouldRevoke(any()); doReturn(REVOKE_PENDING_COMMAND).when(service).getById(anyString()); } @@ -226,7 +226,7 @@ class CommandServiceTest { void shouldCallIsRevokeCommand() { setCommandFinished(); - verify(service).isRevokeCommand(REVOKE_PENDING_COMMAND); + verify(service).shouldRevoke(REVOKE_PENDING_COMMAND); } @Test @@ -329,13 +329,13 @@ class CommandServiceTest { } @Nested - class TestIsRevokeCommand { + class TestShouldRevoke { @Test void shouldReturnTrueWhenRevokePending() { var command = CommandTestFactory.createBuilder().status(CommandStatus.REVOKE_PENDING).build(); - var result = service.isRevokeCommand(command); + var result = service.shouldRevoke(command); assertThat(result).isTrue(); } @@ -344,7 +344,7 @@ class CommandServiceTest { void shouldRevokeTrueWhenParentFailed() { doReturn(true).when(service).isParentCommandFailed(any()); - var result = service.isRevokeCommand(CommandTestFactory.create()); + var result = service.shouldRevoke(CommandTestFactory.create()); assertThat(result).isTrue(); } @@ -363,32 +363,46 @@ class CommandServiceTest { } @Test - void shouldCallRepository() { - var parentId = "parent-id"; - doReturn(Optional.of(parentId)).when(service).getParentId(any()); + void shouldReturnFalseWhenParentMissing() { + doReturn(Optional.empty()).when(service).getParentId(any()); - isParentCommandFailed(); + var result = isParentCommandFailed(); - verify(repository).isCommandFailed(parentId); + assertThat(result).isFalse(); } - @Test - void shouldReturnTrueWhenParentNotFailed() { - doReturn(Optional.of("parent-id")).when(service).getParentId(any()); - when(repository.isCommandFailed(any())).thenReturn(true); + @Nested + class TestWithParent { - var result = isParentCommandFailed(); + private static final String PARENT_ID = "parent-id"; - assertThat(result).isTrue(); - } + @BeforeEach + void init() { + doReturn(Optional.of(PARENT_ID)).when(service).getParentId(any()); + } - @Test - void shouldReturnFalseWhenParentFailed() { - doReturn(Optional.of("parent-id")).when(service).getParentId(any()); + @Test + void shouldCallRepository() { + isParentCommandFailed(); - var result = isParentCommandFailed(); + verify(repository).isCommandFailed(PARENT_ID); + } - assertThat(result).isFalse(); + @Test + void shouldReturnTrueWhenParentNotFailed() { + when(repository.isCommandFailed(any())).thenReturn(true); + + var result = isParentCommandFailed(); + + assertThat(result).isTrue(); + } + + @Test + void shouldReturnFalseWhenParentFailed() { + var result = isParentCommandFailed(); + + assertThat(result).isFalse(); + } } boolean isParentCommandFailed() { @@ -409,54 +423,57 @@ class CommandServiceTest { } @Test - void shouldCallIsCompleteIfSubsCompleted() { - var parentId = "parent-id"; - doReturn(Optional.of(parentId)).when(service).getParentId(any()); + void shouldReturnEmptyWhenNoParentId() { + doReturn(Optional.empty()).when(service).getParentId(any()); - service.getCompletableParentId(COMMAND); + var result = service.getCompletableParentId(COMMAND); - verify(repository).isCompleteIfSubsCompleted(parentId); + assertThat(result).isEmpty(); } - @Test - void shouldCallExistsNotFinishedSubCommands() { - var parentId = "parent-id"; - doReturn(Optional.of(parentId)).when(service).getParentId(any()); - when(repository.isCompleteIfSubsCompleted(anyString())).thenReturn(true); + @Nested + class TestCompleteIfSubsCompleted { - service.getCompletableParentId(COMMAND); + private static final String PARENT_ID = "parent-id"; - verify(repository).existsNotFinishedSubCommands(parentId); - } + @BeforeEach + void init() { + doReturn(Optional.of(PARENT_ID)).when(service).getParentId(any()); + } - @Test - void shouldReturnParentId() { - var expectedParentId = "parent-id"; - doReturn(Optional.of(expectedParentId)).when(service).getParentId(any()); - when(repository.isCompleteIfSubsCompleted(anyString())).thenReturn(true); + @Test + void shouldCallIsCompleteIfSubsCompleted() { + service.getCompletableParentId(COMMAND); - var result = service.getCompletableParentId(COMMAND); + verify(repository).isCompleteIfSubsCompleted(PARENT_ID); + } - assertThat(result).contains(expectedParentId); - } + @Test + void shouldCallExistsNotFinishedSubCommands() { + when(repository.isCompleteIfSubsCompleted(anyString())).thenReturn(true); - @Test - void shouldReturnEmptyWhenNoParentId() { - doReturn(Optional.empty()).when(service).getParentId(any()); + service.getCompletableParentId(COMMAND); - var result = service.getCompletableParentId(COMMAND); + verify(repository).existsNotFinishedSubCommands(PARENT_ID); + } - assertThat(result).isEmpty(); - } + @Test + void shouldReturnParentId() { + when(repository.isCompleteIfSubsCompleted(anyString())).thenReturn(true); - @Test - void shouldReturnEmptyWhenNotCompletableBySubCommands() { - doReturn(Optional.of("parent-id")).when(service).getParentId(any()); + var result = service.getCompletableParentId(COMMAND); - var result = service.getCompletableParentId(COMMAND); + assertThat(result).contains(PARENT_ID); + } - assertThat(result).isEmpty(); + @Test + void shouldReturnEmptyWhenNotCompletableBySubCommands() { + var result = service.getCompletableParentId(COMMAND); + + assertThat(result).isEmpty(); + } } + } @Nested @@ -523,9 +540,6 @@ class CommandServiceTest { private static final String ERROR_MESSAGE = "error message"; private static final String PARENT_ID = "parent-id"; - @Captor - private ArgumentCaptor<String> errorMessageCaptor; - @Test void shouldCallRepositorySetErrorMessage() { service.setCommandError(CommandTestFactory.ID, ERROR_MESSAGE); @@ -543,10 +557,12 @@ class CommandServiceTest { @Nested class TestFailParentAndRevokeSubCommands { + private static final Command COMMAND_BEFORE_SET_ERROR_STATUS = CommandTestFactory.createBuilder().id(PARENT_ID).build(); + @BeforeEach void init() { when(repository.getParentId(anyString())).thenReturn(Optional.of(PARENT_ID)); - doReturn(CommandTestFactory.createBuilder().id(PARENT_ID).build()).when(service).setErrorStatus(anyString()); + doReturn(COMMAND_BEFORE_SET_ERROR_STATUS).when(service).setErrorStatus(anyString()); } @Test @@ -557,19 +573,17 @@ class CommandServiceTest { } @Test - void shouldCallPublishCommandFailedEvent() { + void shouldCallNotErrorStatus() { service.setCommandError(CommandTestFactory.ID, ERROR_MESSAGE); - verify(service).publishCommandFailedEvent(eq(PARENT_ID), errorMessageCaptor.capture()); - assertThat(errorMessageCaptor.getValue()).contains(PARENT_ID, CommandTestFactory.ID); + verify(service).notErrorStatus(COMMAND_BEFORE_SET_ERROR_STATUS); } @Test - void shouldCallRevokeSubCommands() { - + void shouldCallHandleCommandError() { service.setCommandError(CommandTestFactory.ID, ERROR_MESSAGE); - verify(service).revokeSubCommands(PARENT_ID); + verify(service).handleCommandError(CommandTestFactory.ID, PARENT_ID); } } @@ -582,41 +596,31 @@ class CommandServiceTest { } @Test - void shouldNotCallPublishCommandFailedEventWhenNoParentId() { + void shouldNotCallHandleCommandError() { service.setCommandError(CommandTestFactory.ID, ERROR_MESSAGE); - verify(service, never()).publishCommandFailedEvent(anyString(), anyString()); - } - - @Test - void shouldNotCallRevokeSubCommandsWhenNoParentId() { - service.setCommandError(CommandTestFactory.ID, ERROR_MESSAGE); - - verify(service, never()).revokeSubCommands(anyString()); + verify(service, never()).handleCommandError(anyString(), anyString()); } } @Nested class TestParentAlreadyFailed { + @Mock + private Command failedParentCommand; + @BeforeEach void init() { when(repository.getParentId(anyString())).thenReturn(Optional.of(PARENT_ID)); - doReturn(CommandTestFactory.createBuilder().status(CommandStatus.ERROR).build()).when(service).setErrorStatus(anyString()); + doReturn(failedParentCommand).when(service).setErrorStatus(anyString()); + doReturn(false).when(service).notErrorStatus(any()); } @Test - void shouldNotCallPublishCommandFailedEvent() { + void shouldNotCallHandleCommandError() { service.setCommandError(CommandTestFactory.ID, ERROR_MESSAGE); - verify(service, never()).publishCommandFailedEvent(anyString(), anyString()); - } - - @Test - void shouldNotCallRevokeSubCommands() { - service.setCommandError(CommandTestFactory.ID, ERROR_MESSAGE); - - verify(service, never()).revokeSubCommands(anyString()); + verify(service, never()).handleCommandError(anyString(), anyString()); } } @@ -646,7 +650,59 @@ class CommandServiceTest { @Test void shouldThrowExceptionIfNotFound() { - Assertions.assertThrows(NotFoundException.class, () -> service.setErrorStatus(CommandTestFactory.ID)); + assertThrows(NotFoundException.class, () -> service.setErrorStatus(CommandTestFactory.ID)); + } + } + + @Nested + class TestNotErrorStatus { + + @DisplayName("should return true when") + @ParameterizedTest(name = "command status is {0}") + @EnumSource(value = CommandStatus.class, names = { "ERROR" }, mode = EnumSource.Mode.EXCLUDE) + void shouldReturnTrue(CommandStatus status) { + var command = CommandTestFactory.createBuilder().status(status).build(); + + var result = service.notErrorStatus(command); + + assertThat(result).isTrue(); + } + + @Test + void shouldReturnFalse() { + var command = CommandTestFactory.createBuilder().status(CommandStatus.ERROR).build(); + + var result = service.notErrorStatus(command); + + assertThat(result).isFalse(); + } + } + + @Nested + class TestHandleCommandError { + + private static final String PARENT_ID = "parent-id"; + + @Captor + private ArgumentCaptor<String> errorMessageCaptor; + + @Test + void shouldCallPublishCommandFailedEvent() { + handleCommandError(); + + verify(service).publishCommandFailedEvent(eq(PARENT_ID), errorMessageCaptor.capture()); + assertThat(errorMessageCaptor.getValue()).contains(PARENT_ID, CommandTestFactory.ID); + } + + @Test + void shouldCallRevokeSubCommands() { + handleCommandError(); + + verify(service).revokeSubCommands(PARENT_ID); + } + + private void handleCommandError() { + service.handleCommandError(CommandTestFactory.ID, PARENT_ID); } } @@ -666,9 +722,9 @@ class CommandServiceTest { final String commandId = CommandTestFactory.ID; - @Test - void shouldThrowException() { - assertThatExceptionOfType(NotFoundException.class).isThrownBy(() -> service.revokeCommand(commandId)); + @BeforeEach + void init() { + doNothing().when(service).validateRevokeResult(any(), anyString()); } @Test @@ -680,6 +736,16 @@ class CommandServiceTest { verify(repository).setRevokeStatusIfNotPending(CommandTestFactory.ID); } + @Test + void shouldCallValidateRevokeResult() { + var command = CommandTestFactory.create(); + when(repository.setRevokeStatusIfNotPending(anyString())).thenReturn(command); + + service.revokeCommand(commandId); + + verify(service).validateRevokeResult(command, CommandTestFactory.ID); + } + @Test void shouldCallPublishRevokeCommandEvent() { var command = CommandTestFactory.createBuilder().status(CommandStatus.REVOKE_PENDING).build(); @@ -699,14 +765,21 @@ class CommandServiceTest { assertThat(result).isSameAs(command); } + } + + @Nested + class TestValidateRevokeResult { @Test - void shouldNotPublishEvent() { - when(repository.setRevokeStatusIfNotPending(anyString())).thenReturn(CommandTestFactory.create()); + void shouldThrowNotFoundException() { + assertThrows(NotFoundException.class, () -> service.validateRevokeResult(null, CommandTestFactory.ID)); + } - service.revokeCommand(commandId); + @Test + void shouldThrowRevokeFailedException() { + var command = CommandTestFactory.create(); - verify(service, never()).publishRevokeCommandEvent(any()); + assertThrows(RevokeFailedException.class, () -> service.validateRevokeResult(command, CommandTestFactory.ID)); } } diff --git a/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/GrpcCommandServiceTest.java b/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/GrpcCommandServiceTest.java index afef68d9bc130103b2bf09d0bbc6a38ab05b47cb..6807b332b138383be4bff92001a809aba224d3ac 100644 --- a/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/GrpcCommandServiceTest.java +++ b/vorgang-manager-server/src/test/java/de/ozgcloud/vorgang/command/GrpcCommandServiceTest.java @@ -24,7 +24,6 @@ package de.ozgcloud.vorgang.command; import static org.assertj.core.api.Assertions.*; -import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; @@ -47,7 +46,6 @@ import org.springframework.context.ApplicationEventPublisher; import de.ozgcloud.command.Command; import de.ozgcloud.command.CommandExecutedEvent; import de.ozgcloud.command.CommandStatus; -import de.ozgcloud.vorgang.common.errorhandling.RevokeFailedException; import de.ozgcloud.vorgang.common.security.PolicyService; import de.ozgcloud.vorgang.grpc.command.GrpcAddSubCommandsRequest; import de.ozgcloud.vorgang.grpc.command.GrpcCommand; @@ -219,74 +217,49 @@ class GrpcCommandServiceTest { service.revokeCommand(REVOKE_COMMAND_REQUEST, responseObserver); } - @Nested - class TestRevokeSucceed { - - @BeforeEach - void init() { - updatedCommand = CommandTestFactory.createBuilder().id(CommandTestFactory.ID).build(); - doNothing().when(service).validateRevokeResult(any()); - } - - @Test - void shouldCallPolicyService() { - callRevokeCommand(); - - verify(policyService).checkPermissionByCommand(CommandTestFactory.ID); - } - - @Test - void shouldCallServiceRevokeCommand() { - callRevokeCommand(); - - verify(commandService).revokeCommand(CommandTestFactory.ID); - } - - @Test - void shouldCallValidateRevokeResult() { - when(commandService.revokeCommand(any())).thenReturn(updatedCommand); + @BeforeEach + void init() { + updatedCommand = CommandTestFactory.createBuilder().id(CommandTestFactory.ID).build(); + } - callRevokeCommand(); + @Test + void shouldCallPolicyService() { + callRevokeCommand(); - verify(service).validateRevokeResult(updatedCommand); - } + verify(policyService).checkPermissionByCommand(CommandTestFactory.ID); + } - @Test - void shouldCallMapper() { - when(commandService.revokeCommand(any())).thenReturn(updatedCommand); + @Test + void shouldCallServiceRevokeCommand() { + callRevokeCommand(); - callRevokeCommand(); + verify(commandService).revokeCommand(CommandTestFactory.ID); + } - verify(commandResponseMapper).toGrpc(responseCaptor.capture()); - assertThat(responseCaptor.getValue()).extracting(CommandResponse::getCommand).isSameAs(updatedCommand); - } + @Test + void shouldCallMapper() { + when(commandService.revokeCommand(any())).thenReturn(updatedCommand); - @Test - void shouldCallOnNext() { - when(commandResponseMapper.toGrpc(any())).thenReturn(commandsResponse); + callRevokeCommand(); - callRevokeCommand(); + verify(commandResponseMapper).toGrpc(responseCaptor.capture()); + assertThat(responseCaptor.getValue()).extracting(CommandResponse::getCommand).isSameAs(updatedCommand); + } - verify(responseObserver).onNext(commandsResponse); - } + @Test + void shouldCallOnNext() { + when(commandResponseMapper.toGrpc(any())).thenReturn(commandsResponse); - @Test - void shouldCloseStream() { - callRevokeCommand(); + callRevokeCommand(); - verify(responseObserver).onCompleted(); - } + verify(responseObserver).onNext(commandsResponse); } - @Nested - class TestRevokeFails { - - @Test - void shouldThrowExceptionIfValidationFails() { - doThrow(RevokeFailedException.class).when(service).validateRevokeResult(any()); + @Test + void shouldCloseStream() { + callRevokeCommand(); - assertThrows(RevokeFailedException.class, TestRevokeCommand.this::callRevokeCommand); - } + verify(responseObserver).onCompleted(); } }