Skip to content
Snippets Groups Projects
Commit 19527b90 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-4713 OZG-4926 Refactor CommandWithPrevious

parent c5fbbeb5
Branches
Tags
No related merge requests found
Showing
with 195 additions and 268 deletions
package de.ozgcloud.alfa.historie;
import java.util.Optional;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
......@@ -30,21 +31,20 @@ class AktenzeichenChangeHistoryBuilder extends ChangeHistoryBuilder<Aktenzeichen
@Override
CommandWithChangeValues toCommandWithChangeValues(CommandWithPrevious commandWithPrevious) {
return new CommandWithChangeValues(
commandWithPrevious.command(),
commandWithPrevious.getCommand(),
getAktenzeichenBeforeChange(commandWithPrevious),
getAktenzeichenAfterChange(commandWithPrevious)
);
}
String getAktenzeichenBeforeChange(CommandWithPrevious commandWithPrevious) {
return commandWithPrevious.previous()
.map(CommandWithPrevious::command)
return Optional.ofNullable(commandWithPrevious.getPrevious())
.map(this::getAktenzeichen)
.orElse(StringUtils.EMPTY);
}
String getAktenzeichenAfterChange(CommandWithPrevious commandWithPrevious) {
return getAktenzeichen(commandWithPrevious.command());
return getAktenzeichen(commandWithPrevious.getCommand());
}
String getAktenzeichen(Command command) {
......
......@@ -8,7 +8,6 @@ import org.apache.commons.lang3.StringUtils;
import de.ozgcloud.alfa.common.command.Command;
import de.ozgcloud.alfa.common.command.CommandOrder;
import de.ozgcloud.alfa.common.user.UserId;
import de.ozgcloud.alfa.common.user.UserProfile;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
......@@ -40,31 +39,23 @@ class AssignedUserChangeHistoryBuilder extends ChangeHistoryBuilder<AssignedUser
@Override
CommandWithChangeValues toCommandWithChangeValues(CommandWithPrevious commandWithPrevious) {
return new CommandWithChangeValues(
commandWithPrevious.command(),
getUserText(getAssignedUserBeforeChange(commandWithPrevious)),
getUserText(getAssignedUserAfterChange(commandWithPrevious)));
commandWithPrevious.getCommand(),
getAssignedUserBeforeChange(commandWithPrevious),
getAssignedUserAfterChange(commandWithPrevious));
}
String getAssignedUserBeforeChange(CommandWithPrevious commandWithPrevious) {
return commandWithPrevious.previous()
.map(CommandWithPrevious::command)
return Optional.ofNullable(commandWithPrevious.getPrevious())
.map(this::getAssignedUserFullNameFromCommand)
.orElse(StringUtils.EMPTY);
}
String getAssignedUserAfterChange(CommandWithPrevious commandWithPrevious) {
return getAssignedUserFullNameFromCommand(commandWithPrevious.command());
return getAssignedUserFullNameFromCommand(commandWithPrevious.getCommand());
}
String getAssignedUserFullNameFromCommand(Command command) {
return getAssignedUserId(command)
.map(userProfileCache::getUserProfile)
.map(UserProfile::getFullName)
.orElse(StringUtils.EMPTY);
}
Optional<UserId> getAssignedUserId(Command command) {
var value = getValueFromCommandBody(BODY_PROPERTY_ASSIGNED_USER, command);
return StringUtils.isNotBlank(value) ? Optional.of(UserId.from(value)) : Optional.empty();
var assignedUserId = UserId.from(getValueFromCommandBody(BODY_PROPERTY_ASSIGNED_USER, command));
return userProfileCache.getUserProfile(assignedUserId).getFullName();
}
}
......@@ -10,6 +10,7 @@ import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import de.ozgcloud.alfa.common.command.Command;
import de.ozgcloud.alfa.historie.CommandWithPrevious.CommandWithPreviousBuilder;
abstract class ChangeHistoryBuilder<T extends ChangeHistoryBuilder<T>> {
......@@ -46,7 +47,11 @@ abstract class ChangeHistoryBuilder<T extends ChangeHistoryBuilder<T>> {
var commandsList = commands.toList();
var result = new ArrayList<CommandWithPrevious>(commandsList.size());
for (int i = 0; i < commandsList.size(); i++) {
result.add(new CommandWithPrevious(commandsList.get(i), i > 0 ? Optional.of(result.get(i - 1)) : Optional.empty()));
CommandWithPreviousBuilder builder = new CommandWithPreviousBuilder().command(commandsList.get(i));
if (i > 0) {
builder.previous(commandsList.get(i - 1));
}
result.add(builder.build());
}
return result.stream();
}
......
package de.ozgcloud.alfa.historie;
import java.util.Optional;
import de.ozgcloud.alfa.common.command.Command;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Builder(toBuilder = true)
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
class CommandWithPrevious {
record CommandWithPrevious(Command command, Optional<CommandWithPrevious> previous) {
private Command command;
private Command previous;
}
package de.ozgcloud.alfa.historie;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
......@@ -50,18 +51,18 @@ public class StatusChangeHistoryBuilder extends ChangeHistoryBuilder<StatusChang
@Override
CommandWithChangeValues toCommandWithChangeValues(CommandWithPrevious commandWithPrevious) {
return new CommandWithChangeValues(commandWithPrevious.command(),
return new CommandWithChangeValues(commandWithPrevious.getCommand(),
getStatusBeforeChange(commandWithPrevious),
getStatusAfterChange(commandWithPrevious));
}
String getStatusBeforeChange(CommandWithPrevious commandWithPrevious) {
return commandWithPrevious.previous().map(CommandWithPrevious::command).map(this::getStatus)
return Optional.ofNullable(commandWithPrevious.getPrevious()).map(this::getStatus)
.orElse(VORGANG_STATUS_TO_NAME.get(VorgangStatus.NEU));
}
String getStatusAfterChange(CommandWithPrevious commandWithPrevious) {
return getStatus(commandWithPrevious.command());
return getStatus(commandWithPrevious.getCommand());
}
String getStatus(Command command) {
......
......@@ -7,7 +7,6 @@ import static org.mockito.Mockito.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
......@@ -25,12 +24,12 @@ public class AktenzeichenChangeHistoryBuilderTest {
private static final String ORGANISATIONSEINHEITEN_ID = UUID.randomUUID().toString();
private final Command command0105 = commandFinishedAt(LocalDateTime.of(2023, 5, 1, 12, 0));
private final Command command0106 = commandFinishedAt(LocalDateTime.of(2023, 6, 1, 12, 0));
private final List<Command> commands = List.of(command0105, command0106);
private final Command previousCommand = commandFinishedAt(LocalDateTime.of(2023, 5, 1, 12, 0));
private final Command command = commandFinishedAt(LocalDateTime.of(2023, 6, 1, 12, 0));
private final List<Command> commands = List.of(previousCommand, command);
private final CommandWithPrevious command0105WithPrevious = new CommandWithPrevious(command0105, Optional.empty());
private final CommandWithPrevious command0106WithPrevious = new CommandWithPrevious(command0106, Optional.of(command0105WithPrevious));
private final CommandWithPrevious commandWithoutPrevious = CommandWithPreviousTestFactory.createBuilder().command(previousCommand).previous(null).build();
private final CommandWithPrevious commandWithPrevious = CommandWithPreviousTestFactory.createBuilder().command(command).previous(previousCommand).build();
@Spy
private AktenzeichenChangeHistoryBuilder builder = AktenzeichenChangeHistoryBuilder.builder()
......@@ -44,18 +43,18 @@ public class AktenzeichenChangeHistoryBuilderTest {
void shouldCallIsSetAktenzeichenCommand() {
builder.retrieveRelevantCommands().toList();
verify(builder).isSetAktenzeichenCommand(command0105);
verify(builder).isSetAktenzeichenCommand(command0106);
verify(builder).isSetAktenzeichenCommand(previousCommand);
verify(builder).isSetAktenzeichenCommand(command);
}
@Test
void shouldFilterOutIrrelevantCommands() {
doReturn(false).when(builder).isSetAktenzeichenCommand(command0105);
doReturn(true).when(builder).isSetAktenzeichenCommand(command0106);
doReturn(false).when(builder).isSetAktenzeichenCommand(previousCommand);
doReturn(true).when(builder).isSetAktenzeichenCommand(command);
var result = builder.retrieveRelevantCommands().toList();
assertThat(result).containsExactly(command0106);
assertThat(result).containsExactly(command);
}
}
......@@ -93,15 +92,15 @@ public class AktenzeichenChangeHistoryBuilderTest {
@BeforeEach
void init() {
when(builder.getAktenzeichenBeforeChange(command0106WithPrevious)).thenReturn(EXPECTED_AKTENZEICHEN_BEFORE_CHANGE);
when(builder.getAktenzeichenAfterChange(command0106WithPrevious)).thenReturn(EXPECTED_AKTENZEICHEN_AFTER_CHANGE);
when(builder.getAktenzeichenBeforeChange(commandWithPrevious)).thenReturn(EXPECTED_AKTENZEICHEN_BEFORE_CHANGE);
when(builder.getAktenzeichenAfterChange(commandWithPrevious)).thenReturn(EXPECTED_AKTENZEICHEN_AFTER_CHANGE);
}
@Test
void shouldSetCommand() {
var commandWithChangeValues = callBuilder();
assertThat(commandWithChangeValues.command()).isEqualTo(command0106);
assertThat(commandWithChangeValues.command()).isEqualTo(command);
}
@Test
......@@ -119,7 +118,7 @@ public class AktenzeichenChangeHistoryBuilderTest {
}
private ChangeHistoryBuilder.CommandWithChangeValues callBuilder() {
return builder.toCommandWithChangeValues(command0106WithPrevious);
return builder.toCommandWithChangeValues(commandWithPrevious);
}
}
......@@ -128,7 +127,7 @@ public class AktenzeichenChangeHistoryBuilderTest {
@Test
void shouldReturnEmptyIfPreviousIsNull() {
var aktenzeichen = builder.getAktenzeichenBeforeChange(command0105WithPrevious);
var aktenzeichen = builder.getAktenzeichenBeforeChange(commandWithoutPrevious);
assertThat(aktenzeichen).isEmpty();
}
......@@ -136,9 +135,9 @@ public class AktenzeichenChangeHistoryBuilderTest {
@Test
void shouldGetAktenzeichenFromPreviousCommand() {
var expectedValue = LoremIpsum.getInstance().getWords(2);
when(builder.getAktenzeichen(command0105)).thenReturn(expectedValue);
when(builder.getAktenzeichen(previousCommand)).thenReturn(expectedValue);
var aktenzeichen = builder.getAktenzeichenBeforeChange(command0106WithPrevious);
var aktenzeichen = builder.getAktenzeichenBeforeChange(commandWithPrevious);
assertThat(aktenzeichen).isEqualTo(expectedValue);
}
......@@ -151,19 +150,19 @@ public class AktenzeichenChangeHistoryBuilderTest {
@BeforeEach
void init() {
doReturn(EXPECTED_AKTENZEICHEN).when(builder).getAktenzeichen(command0106);
doReturn(EXPECTED_AKTENZEICHEN).when(builder).getAktenzeichen(command);
}
@Test
void shouldGetAktenzeichenFromCommand() {
builder.getAktenzeichenAfterChange(command0106WithPrevious);
builder.getAktenzeichenAfterChange(commandWithPrevious);
verify(builder).getAktenzeichen(command0106);
verify(builder).getAktenzeichen(command);
}
@Test
void shouldReturnAktenzeichen() {
var result = builder.getAktenzeichenAfterChange(command0106WithPrevious);
var result = builder.getAktenzeichenAfterChange(commandWithPrevious);
assertThat(result).isEqualTo(EXPECTED_AKTENZEICHEN);
}
......@@ -176,19 +175,19 @@ public class AktenzeichenChangeHistoryBuilderTest {
@BeforeEach
void init() {
when(builder.getValueFromCommandBody(BODY_PROPERTY_AKTENZEICHEN, command0105)).thenReturn(AKTENZEICHEN);
when(builder.getValueFromCommandBody(BODY_PROPERTY_AKTENZEICHEN, previousCommand)).thenReturn(AKTENZEICHEN);
}
@Test
void shouldGetValueFromCommandBody() {
builder.getAktenzeichen(command0105);
builder.getAktenzeichen(previousCommand);
verify(builder).getValueFromCommandBody(BODY_PROPERTY_AKTENZEICHEN, command0105);
verify(builder).getValueFromCommandBody(BODY_PROPERTY_AKTENZEICHEN, previousCommand);
}
@Test
void shouldReturnValueFromCommandBody() {
var value = builder.getAktenzeichen(command0105);
var value = builder.getAktenzeichen(previousCommand);
assertThat(value).isEqualTo(AKTENZEICHEN);
}
......
......@@ -7,10 +7,8 @@ import static org.mockito.Mockito.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
......@@ -31,12 +29,12 @@ public class AssignedUserChangeHistoryBuilderTest {
private static final String ORGANISATIONSEINHEITEN_ID = UUID.randomUUID().toString();
private final Command command0105 = commandFinishedAt(LocalDateTime.of(2023, 5, 1, 12, 0));
private final Command command0106 = commandFinishedAt(LocalDateTime.of(2023, 6, 1, 12, 0));
private final List<Command> commands = List.of(command0105, command0106);
private final Command previousCommand = commandFinishedAt(LocalDateTime.of(2023, 5, 1, 12, 0));
private final Command command = commandFinishedAt(LocalDateTime.of(2023, 6, 1, 12, 0));
private final List<Command> commands = List.of(previousCommand, command);
private final CommandWithPrevious command0105WithPrevious = new CommandWithPrevious(command0105, Optional.empty());
private final CommandWithPrevious command0106WithPrevious = new CommandWithPrevious(command0106, Optional.of(command0105WithPrevious));
private final CommandWithPrevious commandWithoutPrevious = CommandWithPreviousTestFactory.createBuilder().command(previousCommand).previous(null).build();
private final CommandWithPrevious commandWithPrevious = CommandWithPreviousTestFactory.createBuilder().command(command).previous(previousCommand).build();
@Mock
private UserProfileCache userProfileCache;
......@@ -54,18 +52,18 @@ public class AssignedUserChangeHistoryBuilderTest {
void shouldCallIsAssignUserCommand() {
builder.retrieveRelevantCommands().toList();
verify(builder).isAssignUserCommand(command0105);
verify(builder).isAssignUserCommand(command0106);
verify(builder).isAssignUserCommand(previousCommand);
verify(builder).isAssignUserCommand(command);
}
@Test
void shouldFilterOutIrrelevantCommands() {
doReturn(false).when(builder).isAssignUserCommand(command0105);
doReturn(true).when(builder).isAssignUserCommand(command0106);
doReturn(false).when(builder).isAssignUserCommand(previousCommand);
doReturn(true).when(builder).isAssignUserCommand(command);
var result = builder.retrieveRelevantCommands().toList();
assertThat(result).containsExactly(command0106);
assertThat(result).containsExactly(command);
}
}
......@@ -100,43 +98,36 @@ public class AssignedUserChangeHistoryBuilderTest {
private static final String ASSIGNED_USER_BEFORE_CHANGE = LoremIpsum.getInstance().getWords(2);
private static final String ASSIGNED_USER_AFTER_CHANGE = LoremIpsum.getInstance().getWords(2);
private static final String USER_TEXT_BEFORE_CHANGE = LoremIpsum.getInstance().getWords(3);
private static final String USER_TEXT_AFTER_CHANGE = LoremIpsum.getInstance().getWords(3);
@Mock
private UserProfileCache userProfileCache;
@BeforeEach
void init() {
when(builder.getAssignedUserBeforeChange(command0106WithPrevious)).thenReturn(ASSIGNED_USER_BEFORE_CHANGE);
when(builder.getAssignedUserAfterChange(command0106WithPrevious)).thenReturn(ASSIGNED_USER_AFTER_CHANGE);
when(builder.getUserText(ASSIGNED_USER_BEFORE_CHANGE)).thenReturn(USER_TEXT_BEFORE_CHANGE);
when(builder.getUserText(ASSIGNED_USER_AFTER_CHANGE)).thenReturn(USER_TEXT_AFTER_CHANGE);
doReturn(ASSIGNED_USER_BEFORE_CHANGE).when(builder).getAssignedUserBeforeChange(commandWithPrevious);
doReturn(ASSIGNED_USER_AFTER_CHANGE).when(builder).getAssignedUserAfterChange(commandWithPrevious);
}
@Test
void shouldSetCommand() {
var commandWithChangeValues = callBuilder();
assertThat(commandWithChangeValues.command()).isEqualTo(command0106);
assertThat(commandWithChangeValues.command()).isEqualTo(command);
}
@Test
void shouldSetValueBeforeChange() {
var commandWithChangeValues = callBuilder();
assertThat(commandWithChangeValues.valueBeforeChange()).isEqualTo(USER_TEXT_BEFORE_CHANGE);
assertThat(commandWithChangeValues.valueBeforeChange()).isEqualTo(ASSIGNED_USER_BEFORE_CHANGE);
}
@Test
void shouldSetValueAfterChange() {
var commandWithChangeValues = callBuilder();
assertThat(commandWithChangeValues.valueAfterChange()).isEqualTo(USER_TEXT_AFTER_CHANGE);
assertThat(commandWithChangeValues.valueAfterChange()).isEqualTo(ASSIGNED_USER_AFTER_CHANGE);
}
private ChangeHistoryBuilder.CommandWithChangeValues callBuilder() {
return builder.toCommandWithChangeValues(command0106WithPrevious);
return builder.toCommandWithChangeValues(commandWithPrevious);
}
}
......@@ -145,53 +136,37 @@ public class AssignedUserChangeHistoryBuilderTest {
private static final String EXPECTED_ASSIGNED_USER = LoremIpsum.getInstance().getWords(2);
@Mock
private UserProfileCache userProfileCache;
@Test
void shouldReturnEmptyStringIfPreviousIsNull() {
var assignedUser = builder.getAssignedUserBeforeChange(command0105WithPrevious);
var assignedUser = builder.getAssignedUserBeforeChange(commandWithoutPrevious);
assertThat(assignedUser).isEmpty();
}
@Test
void shouldGetAssignedUserFromPreviousCommand() {
givenGetAssignedUserAfterChangeReturnsExpectedValue();
givenAssignedUserFromPreviousCommand();
callBuilder();
verify(builder).getAssignedUserFullNameFromCommand(command0105);
verify(builder).getAssignedUserFullNameFromCommand(previousCommand);
}
@Test
void shouldReturnAssignedUserRetrievedFromPreviousCommand() {
givenGetAssignedUserAfterChangeReturnsExpectedValue();
givenAssignedUserFromPreviousCommand();
var assignedUser = callBuilder();
assertThat(assignedUser).isEqualTo(EXPECTED_ASSIGNED_USER);
}
@Test
void shouldReturnEmptyStringIfAssignedUserIsNull() {
givenGetAssignedUserAfterChangeReturnsNull();
var assignedUser = callBuilder();
assertThat(assignedUser).isEmpty();
}
private void givenGetAssignedUserAfterChangeReturnsExpectedValue() {
doReturn(EXPECTED_ASSIGNED_USER).when(builder).getAssignedUserFullNameFromCommand(command0105);
}
private void givenGetAssignedUserAfterChangeReturnsNull() {
when(builder.getAssignedUserFullNameFromCommand(command0105)).thenReturn(null);
private void givenAssignedUserFromPreviousCommand() {
doReturn(EXPECTED_ASSIGNED_USER).when(builder).getAssignedUserFullNameFromCommand(previousCommand);
}
private String callBuilder() {
return builder.getAssignedUserBeforeChange(command0106WithPrevious);
return builder.getAssignedUserBeforeChange(commandWithPrevious);
}
}
......@@ -205,41 +180,28 @@ public class AssignedUserChangeHistoryBuilderTest {
@Test
void shouldGetAssignedUserFromCommand() {
givenGetAssignedUserAfterChangeReturnsExpectedValue();
givenAssignedUserFromCurrentCommand();
callBuilder();
verify(builder).getAssignedUserFullNameFromCommand(command0106);
verify(builder).getAssignedUserFullNameFromCommand(command);
}
@Test
void shouldReturnAssignedUserRetrievedFromCommand() {
givenGetAssignedUserAfterChangeReturnsExpectedValue();
givenAssignedUserFromCurrentCommand();
var assignedUser = callBuilder();
assertThat(assignedUser).isEqualTo(EXPECTED_ASSIGNED_USER);
}
@Test
void shouldReturnNullIfAssignedUserIsNull() {
givenGetAssignedUserAfterChangeReturnsNull();
var assignedUser = callBuilder();
assertThat(assignedUser).isEqualTo(null);
}
private void givenGetAssignedUserAfterChangeReturnsExpectedValue() {
doReturn(EXPECTED_ASSIGNED_USER).when(builder).getAssignedUserFullNameFromCommand(command0106);
}
private void givenGetAssignedUserAfterChangeReturnsNull() {
when(builder.getAssignedUserFullNameFromCommand(command0106)).thenReturn(null);
private void givenAssignedUserFromCurrentCommand() {
doReturn(EXPECTED_ASSIGNED_USER).when(builder).getAssignedUserFullNameFromCommand(command);
}
private String callBuilder() {
return builder.getAssignedUserAfterChange(command0106WithPrevious);
return builder.getAssignedUserAfterChange(commandWithPrevious);
}
}
......@@ -248,90 +210,38 @@ public class AssignedUserChangeHistoryBuilderTest {
private static final UserId USER_ID = UserId.from(UUID.randomUUID());
private static final UserProfile USER_PROFILE = UserProfileTestFactory.create();
private final Command command = command0105;
@Test
void shouldGetUserIdFromCommand() {
callBuilder();
private final Command command = previousCommand;
verify(builder).getAssignedUserId(command);
@BeforeEach
void init() {
when(builder.getValueFromCommandBody(BODY_PROPERTY_ASSIGNED_USER, previousCommand)).thenReturn(USER_ID.toString());
when(userProfileCache.getUserProfile(USER_ID)).thenReturn(USER_PROFILE);
}
@Test
void shouldReturnEmptyStringIfUserIdCannotBeFound() {
when(builder.getAssignedUserId(command)).thenReturn(Optional.empty());
var assignedUser = callBuilder();
void shouldGetUserIdFromCommand() {
callBuilder();
assertThat(assignedUser).isEmpty();
verify(builder).getValueFromCommandBody(BODY_PROPERTY_ASSIGNED_USER, command);
}
@Test
void shouldGetUserProfileForUserId() {
givenHappyPathScenario();
var result = callBuilder();
callBuilder();
verify(userProfileCache).getUserProfile(USER_ID);
}
@Test
void shouldReturnUserFullName() {
givenHappyPathScenario();
var assignedUser = callBuilder();
assertThat(assignedUser).isEqualTo(UserProfileTestFactory.FULLNAME);
}
private void givenHappyPathScenario() {
when(builder.getAssignedUserId(command)).thenReturn(Optional.of(USER_ID));
when(userProfileCache.getUserProfile(USER_ID)).thenReturn(USER_PROFILE);
}
private String callBuilder() {
return builder.getAssignedUserFullNameFromCommand(command);
}
}
@Nested
class TestGetAssignedUserId {
private static final String ASSIGNED_USER = LoremIpsum.getInstance().getWords(1);
@Test
void shouldGetValueFromCommandBody() {
builder.getAssignedUserId(command0105);
verify(builder).getValueFromCommandBody(BODY_PROPERTY_ASSIGNED_USER, command0105);
}
@Test
void shouldReturnUserIdCreatedFromValueInCommandBody() {
givenNonEmptyCommandBodyValue();
var value = builder.getAssignedUserId(command0105);
assertThat(value).isPresent().get().isEqualTo(UserId.from(ASSIGNED_USER));
}
@Test
void shouldReturnEmptyIfValueInCommandBodyIsEmpty() {
givenEmptyCommandBodyValue();
var value = builder.getAssignedUserId(command0105);
assertThat(value).isEmpty();
}
private void givenNonEmptyCommandBodyValue() {
when(builder.getValueFromCommandBody(BODY_PROPERTY_ASSIGNED_USER, command0105)).thenReturn(ASSIGNED_USER);
}
private void givenEmptyCommandBodyValue() {
when(builder.getValueFromCommandBody(BODY_PROPERTY_ASSIGNED_USER, command0105)).thenReturn(StringUtils.EMPTY);
}
}
}
......@@ -8,7 +8,6 @@ import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;
......@@ -32,16 +31,18 @@ public class ChangeHistoryBuilderTest {
private static final String ORGANISATIONSEINHEITEN_ID = UUID.randomUUID().toString();
private final Command command0105 = commandFinishedAt(LocalDateTime.of(2023, 5, 1, 12, 0));
private final Command command0106 = commandFinishedAt(LocalDateTime.of(2023, 6, 1, 12, 0));
private final List<Command> commands = List.of(command0105, command0106);
private final Command previousCommand = commandFinishedAt(LocalDateTime.of(2023, 5, 1, 12, 0));
private final Command command = commandFinishedAt(LocalDateTime.of(2023, 6, 1, 12, 0));
private final List<Command> commands = List.of(previousCommand, command);
private final CommandWithPrevious command0105WithPrevious = new CommandWithPrevious(command0105, Optional.empty());
private final CommandWithPrevious command0106WithPrevious = new CommandWithPrevious(command0106, Optional.of(command0105WithPrevious));
private final List<CommandWithPrevious> commandsWithPrevious = List.of(command0105WithPrevious, command0106WithPrevious);
private final CommandWithPrevious commandWithoutPrevious = CommandWithPreviousTestFactory.createBuilder().command(previousCommand).previous(null).build();
private final CommandWithPrevious commandWithPrevious = CommandWithPreviousTestFactory.createBuilder().command(command).previous(previousCommand).build();
private final List<CommandWithPrevious> commandsWithPrevious = List.of(commandWithoutPrevious, commandWithPrevious);
private final ChangeHistoryBuilder.CommandWithChangeValues command0105WithChangeValues = new ChangeHistoryBuilder.CommandWithChangeValues(command0105, "a", "b");
private final ChangeHistoryBuilder.CommandWithChangeValues command0106WithChangeValues = new ChangeHistoryBuilder.CommandWithChangeValues(command0105, "b", "c");
private final ChangeHistoryBuilder.CommandWithChangeValues command0105WithChangeValues = new ChangeHistoryBuilder.CommandWithChangeValues(
previousCommand, "a", "b");
private final ChangeHistoryBuilder.CommandWithChangeValues command0106WithChangeValues = new ChangeHistoryBuilder.CommandWithChangeValues(
previousCommand, "b", "c");
private final List<ChangeHistoryBuilder.CommandWithChangeValues> commandsWithChangeValues = List.of(command0105WithChangeValues, command0106WithChangeValues);
@Spy
......@@ -70,13 +71,13 @@ public class ChangeHistoryBuilderTest {
@Nested
class TestInChronologicalOrder {
private final Stream<Command> unorderedCommands = Stream.of(command0106, command0105);
private final Stream<Command> unorderedCommands = Stream.of(command, previousCommand);
@Test
void shouldSortAscendingByFinishedAt() {
var orderedCommands = builder.inChronologicalOrder(unorderedCommands);
assertThat(orderedCommands).containsExactly(command0105, command0106);
assertThat(orderedCommands).containsExactly(previousCommand, command);
}
}
......@@ -84,22 +85,21 @@ public class ChangeHistoryBuilderTest {
class TestAddPreviousCommand {
@Test
void shouldSetPreviousOfFirstElementToEmpty() {
var commands = Stream.of(command0105);
void shouldSetPreviousOfFirstCommandToNull() {
var commands = Stream.of(previousCommand);
var commandsWithPrevious = builder.addPreviousCommand(commands);
assertThat(commandsWithPrevious).first().extracting("previous").isEqualTo(Optional.empty());
assertThat(commandsWithPrevious).first().extracting("previous").isEqualTo(null);
}
@Test
void shouldSetPreviousOfSecondElement() {
var commands = Stream.of(command0105, command0106);
var expectedPrevious = new CommandWithPrevious(command0105, Optional.empty());
void shouldSetPreviousOfSubsequentCommand() {
var commands = Stream.of(previousCommand, command);
var commandsWithPrevious = builder.addPreviousCommand(commands);
assertThat(commandsWithPrevious).element(1).extracting("previous").isEqualTo(Optional.of(expectedPrevious));
assertThat(commandsWithPrevious).element(1).extracting("previous").isEqualTo(previousCommand);
}
}
......@@ -108,16 +108,16 @@ public class ChangeHistoryBuilderTest {
@BeforeEach
void init() {
doReturn(command0105WithChangeValues).when(builder).toCommandWithChangeValues(command0105WithPrevious);
doReturn(command0106WithChangeValues).when(builder).toCommandWithChangeValues(command0106WithPrevious);
doReturn(command0105WithChangeValues).when(builder).toCommandWithChangeValues(commandWithoutPrevious);
doReturn(command0106WithChangeValues).when(builder).toCommandWithChangeValues(commandWithPrevious);
}
@Test
void shouldMapToCommandWithChangeValues() {
callBuilder().toList();
verify(builder).toCommandWithChangeValues(command0105WithPrevious);
verify(builder).toCommandWithChangeValues(command0106WithPrevious);
verify(builder).toCommandWithChangeValues(commandWithoutPrevious);
verify(builder).toCommandWithChangeValues(commandWithPrevious);
}
@Test
......@@ -174,14 +174,14 @@ public class ChangeHistoryBuilderTest {
void shouldSetFinishedAt() {
var historieEntry = callBuilder();
assertThat(historieEntry.getFinishedAt()).isNotNull().isEqualTo(command0105.getFinishedAt());
assertThat(historieEntry.getFinishedAt()).isNotNull().isEqualTo(previousCommand.getFinishedAt());
}
@Test
void shouldSetOrder() {
var historieEntry = callBuilder();
assertThat(historieEntry.getOrder()).isNotNull().isEqualTo(command0105.getOrder());
assertThat(historieEntry.getOrder()).isNotNull().isEqualTo(previousCommand.getOrder());
}
private VorgangChange callBuilder() {
......@@ -226,7 +226,7 @@ public class ChangeHistoryBuilderTest {
@ParameterizedTest
@NullAndEmptySource
void shouldReturnEmptyIfBodyIsNullOrEmpty(Map<String, ?> body) {
var command = command0105.toBuilder().body(body).build();
var command = previousCommand.toBuilder().body(body).build();
var value = builder.getValueFromCommandBody(PROPERTY_NAME, command);
......@@ -235,7 +235,7 @@ public class ChangeHistoryBuilderTest {
@Test
void shouldReturnEmptyIfPropertyIsNotPresentInBody() {
var command = command0105.toBuilder().body(Map.of("a", "b")).build();
var command = previousCommand.toBuilder().body(Map.of("a", "b")).build();
var value = builder.getValueFromCommandBody(PROPERTY_NAME, command);
......@@ -245,7 +245,7 @@ public class ChangeHistoryBuilderTest {
@Test
void shouldReturnValueFromBody() {
var expectedValue = LoremIpsum.getInstance().getWords(2);
var command = command0105.toBuilder().body(Map.of(PROPERTY_NAME, expectedValue)).build();
var command = previousCommand.toBuilder().body(Map.of(PROPERTY_NAME, expectedValue)).build();
var value = builder.getValueFromCommandBody(PROPERTY_NAME, command);
......
package de.ozgcloud.alfa.historie;
import de.ozgcloud.alfa.common.command.Command;
import de.ozgcloud.alfa.common.command.CommandTestFactory;
import de.ozgcloud.alfa.historie.CommandWithPrevious.CommandWithPreviousBuilder;
public class CommandWithPreviousTestFactory {
public static final Command COMMAND = CommandTestFactory.create();
public static final Command PREVIOUS_COMMAND = CommandTestFactory.create();
public static CommandWithPrevious create() {
return createBuilder().build();
}
public static CommandWithPreviousBuilder createBuilder() {
return new CommandWithPreviousBuilder()
.command(COMMAND)
.previous(PREVIOUS_COMMAND);
}
}
......@@ -6,7 +6,6 @@ import static org.mockito.Mockito.*;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
......@@ -28,12 +27,12 @@ public class StatusChangeHistoryBuilderTest {
private static final String ORGANISATIONSEINHEITEN_ID = UUID.randomUUID().toString();
private final Command command0105 = commandFinishedAt(LocalDateTime.of(2023, 5, 1, 12, 0));
private final Command command0106 = commandFinishedAt(LocalDateTime.of(2023, 6, 1, 12, 0));
private final List<Command> commands = List.of(command0105, command0106);
private final Command previousCommand = commandFinishedAt(LocalDateTime.of(2023, 5, 1, 12, 0));
private final Command command = commandFinishedAt(LocalDateTime.of(2023, 6, 1, 12, 0));
private final List<Command> commands = List.of(previousCommand, command);
private final CommandWithPrevious command0105WithPrevious = new CommandWithPrevious(command0105, Optional.empty());
private final CommandWithPrevious command0106WithPrevious = new CommandWithPrevious(command0106, Optional.of(command0105WithPrevious));
private final CommandWithPrevious commandWithoutPrevious = CommandWithPreviousTestFactory.createBuilder().command(previousCommand).previous(null).build();
private final CommandWithPrevious commandWithPrevious = CommandWithPreviousTestFactory.createBuilder().command(command).previous(previousCommand).build();
@Spy
private StatusChangeHistoryBuilder builder = StatusChangeHistoryBuilder.builder()
......@@ -47,18 +46,18 @@ public class StatusChangeHistoryBuilderTest {
void shouldCallIsStatusChangeCommand() {
builder.retrieveRelevantCommands().toList();
verify(builder).isStatusChangeCommand(command0105);
verify(builder).isStatusChangeCommand(command0106);
verify(builder).isStatusChangeCommand(previousCommand);
verify(builder).isStatusChangeCommand(command);
}
@Test
void shouldFilterOutIrrelevantCommands() {
doReturn(false).when(builder).isStatusChangeCommand(command0105);
doReturn(true).when(builder).isStatusChangeCommand(command0106);
doReturn(false).when(builder).isStatusChangeCommand(previousCommand);
doReturn(true).when(builder).isStatusChangeCommand(command);
var result = builder.retrieveRelevantCommands().toList();
assertThat(result).containsExactly(command0106);
assertThat(result).containsExactly(command);
}
}
......@@ -92,15 +91,15 @@ public class StatusChangeHistoryBuilderTest {
@BeforeEach
void init() {
doReturn(EXPECTED_STATUS_BEFORE_CHANGE).when(builder).getStatusBeforeChange(command0106WithPrevious);
doReturn(EXPECTED_STATUS_AFTER_CHANGE).when(builder).getStatusAfterChange(command0106WithPrevious);
doReturn(EXPECTED_STATUS_BEFORE_CHANGE).when(builder).getStatusBeforeChange(commandWithPrevious);
doReturn(EXPECTED_STATUS_AFTER_CHANGE).when(builder).getStatusAfterChange(commandWithPrevious);
}
@Test
void shouldSetCommand() {
var commandWithChangeValues = callBuilder();
assertThat(commandWithChangeValues.command()).isEqualTo(command0106);
assertThat(commandWithChangeValues.command()).isEqualTo(command);
}
@Test
......@@ -118,20 +117,13 @@ public class StatusChangeHistoryBuilderTest {
}
private ChangeHistoryBuilder.CommandWithChangeValues callBuilder() {
return builder.toCommandWithChangeValues(command0106WithPrevious);
return builder.toCommandWithChangeValues(commandWithPrevious);
}
}
@Nested
class TestGetStatusBeforeChange {
private final Command command = CommandTestFactory.create();
private final Command previousCommand = CommandTestFactory.create();
private final CommandWithPrevious commandWithPrevious = new CommandWithPrevious(
command,
Optional.of(new CommandWithPrevious(previousCommand, Optional.empty())));
private final CommandWithPrevious commandWithoutPrevious = new CommandWithPrevious(command, Optional.empty());
@Test
void shouldReturnStatusBeforeChange() {
var status = "Status";
......@@ -153,12 +145,10 @@ public class StatusChangeHistoryBuilderTest {
@Nested
class TestGetStatusAfterChange {
private final CommandWithPrevious commandWithPrevious = new CommandWithPrevious(command0105, Optional.empty());
@Test
void shouldReturnStatusAfterChange() {
var status = "Status";
doReturn(status).when(builder).getStatus(command0105);
doReturn(status).when(builder).getStatus(command);
var statusAfterChange = builder.getStatusAfterChange(commandWithPrevious);
......
......@@ -78,7 +78,7 @@ public class VorgangChangeHistoryServiceITCase {
@Test
void shouldSetCreatedByName() {
givenHistorieServiceReturnsCommands(List.of(new CommandFactory().sc(CommandOrder.VORGANG_ANNEHMEN)));
givenHistorieServiceReturnsCommands(List.of(new CommandFactory().statusChange(CommandOrder.VORGANG_ANNEHMEN)));
var history = service.createVorgangChangeHistory(vorgangWithEingang).getStatusChangeHistory();
......@@ -87,7 +87,7 @@ public class VorgangChangeHistoryServiceITCase {
@Test
void shouldSetFinishedAt() {
var command = new CommandFactory().sc(CommandOrder.VORGANG_ANNEHMEN);
var command = new CommandFactory().statusChange(CommandOrder.VORGANG_ANNEHMEN);
givenHistorieServiceReturnsCommands(List.of(command));
var history = service.createVorgangChangeHistory(vorgangWithEingang).getStatusChangeHistory();
......@@ -159,7 +159,7 @@ public class VorgangChangeHistoryServiceITCase {
@Test
void shouldSetOrder() {
var command = new CommandFactory().sc(CommandOrder.VORGANG_ANNEHMEN);
var command = new CommandFactory().statusChange(CommandOrder.VORGANG_ANNEHMEN);
givenHistorieServiceReturnsCommands(List.of(command));
var history = service.createVorgangChangeHistory(vorgangWithEingang).getStatusChangeHistory();
......@@ -185,7 +185,7 @@ public class VorgangChangeHistoryServiceITCase {
@Test
void shouldSetOrder() {
var command = new CommandFactory().ac(AKTENZEICHEN_1);
var command = new CommandFactory().aktenzeichenChange(AKTENZEICHEN_1);
givenHistorieServiceReturnsCommands(List.of(command));
var history = service.createVorgangChangeHistory(vorgangWithEingang).getAktenzeichenChangeHistory();
......@@ -211,7 +211,7 @@ public class VorgangChangeHistoryServiceITCase {
@Test
void shouldSetOrder() {
var command = new CommandFactory().uc(USER_1_ID.toString());
var command = new CommandFactory().userChange(USER_1_ID.toString());
givenHistorieServiceReturnsCommands(List.of(command));
givenUserServiceReturnsUser1Profile();
......@@ -252,59 +252,59 @@ public class VorgangChangeHistoryServiceITCase {
}
private List<Command> createStatusChangeCommandsUntilAbgeschlossen() {
var f = new CommandFactory();
var commandFactory = new CommandFactory();
return List.of(
f.sc(CommandOrder.VORGANG_ANNEHMEN),
f.sc(CommandOrder.VORGANG_BEARBEITEN),
f.sc(CommandOrder.VORGANG_BESCHEIDEN),
f.sc(CommandOrder.VORGANG_ABSCHLIESSEN),
f.sc(CommandOrder.VORGANG_ZUM_LOESCHEN_MARKIEREN),
f.sc(CommandOrder.VORGANG_ABSCHLIESSEN),
f.sc(CommandOrder.LOESCH_ANFORDERUNG_ZURUECKNEHMEN)
commandFactory.statusChange(CommandOrder.VORGANG_ANNEHMEN),
commandFactory.statusChange(CommandOrder.VORGANG_BEARBEITEN),
commandFactory.statusChange(CommandOrder.VORGANG_BESCHEIDEN),
commandFactory.statusChange(CommandOrder.VORGANG_ABSCHLIESSEN),
commandFactory.statusChange(CommandOrder.VORGANG_ZUM_LOESCHEN_MARKIEREN),
commandFactory.statusChange(CommandOrder.VORGANG_ABSCHLIESSEN),
commandFactory.statusChange(CommandOrder.LOESCH_ANFORDERUNG_ZURUECKNEHMEN)
);
}
private List<Command> createStatusChangeCommandsUntilVerworfen() {
var f = new CommandFactory();
var commandFactory = new CommandFactory();
return List.of(
f.sc(CommandOrder.VORGANG_VERWERFEN),
f.sc(CommandOrder.VORGANG_ZUM_LOESCHEN_MARKIEREN),
f.sc(CommandOrder.VORGANG_VERWERFEN),
f.sc(CommandOrder.LOESCH_ANFORDERUNG_ZURUECKNEHMEN)
commandFactory.statusChange(CommandOrder.VORGANG_VERWERFEN),
commandFactory.statusChange(CommandOrder.VORGANG_ZUM_LOESCHEN_MARKIEREN),
commandFactory.statusChange(CommandOrder.VORGANG_VERWERFEN),
commandFactory.statusChange(CommandOrder.LOESCH_ANFORDERUNG_ZURUECKNEHMEN)
);
}
private List<Command> createStatusChangeCommandsUntilZurueckholen() {
var f = new CommandFactory();
var commandFactory = new CommandFactory();
return List.of(
f.sc(CommandOrder.VORGANG_VERWERFEN),
f.sc(CommandOrder.VORGANG_ZURUECKHOLEN)
commandFactory.statusChange(CommandOrder.VORGANG_VERWERFEN),
commandFactory.statusChange(CommandOrder.VORGANG_ZURUECKHOLEN)
);
}
private List<Command> createAktenzeichenChangeCommands() {
var f = new CommandFactory();
var commandFactory = new CommandFactory();
return List.of(
f.ac(AKTENZEICHEN_1),
f.ac(AKTENZEICHEN_2),
f.ac(null)
commandFactory.aktenzeichenChange(AKTENZEICHEN_1),
commandFactory.aktenzeichenChange(AKTENZEICHEN_2),
commandFactory.aktenzeichenChange(null)
);
}
private List<Command> createUserChangeCommands() {
var f = new CommandFactory();
var commandFactory = new CommandFactory();
return List.of(
f.uc(USER_1_ID.toString()),
f.uc(USER_2_ID.toString())
commandFactory.userChange(USER_1_ID.toString()),
commandFactory.userChange(USER_2_ID.toString())
);
}
private List<Command> createMixedCommands() {
var f = new CommandFactory();
var commandFactory = new CommandFactory();
return List.of(
f.sc(CommandOrder.VORGANG_ANNEHMEN),
f.ac(AKTENZEICHEN_1),
f.uc(USER_1_ID.toString())
commandFactory.statusChange(CommandOrder.VORGANG_ANNEHMEN),
commandFactory.aktenzeichenChange(AKTENZEICHEN_1),
commandFactory.userChange(USER_1_ID.toString())
);
}
......@@ -312,7 +312,7 @@ public class VorgangChangeHistoryServiceITCase {
private ZonedDateTime dateTime = ZonedDateTime.now(ZoneId.of("UTC"));
Command uc(String assignedTo) {
Command userChange(String assignedTo) {
return CommandTestFactory.createBuilder()
.order(CommandOrder.ASSIGN_USER)
.body(assignedTo != null ? Map.of(AssignedUserChangeHistoryBuilder.BODY_PROPERTY_ASSIGNED_USER, assignedTo) : Map.of())
......@@ -321,7 +321,7 @@ public class VorgangChangeHistoryServiceITCase {
.build();
}
Command ac(String aktenzeichen) {
Command aktenzeichenChange(String aktenzeichen) {
return CommandTestFactory.createBuilder()
.order(CommandOrder.SET_AKTENZEICHEN)
.body(aktenzeichen != null ? Map.of(AktenzeichenChangeHistoryBuilder.BODY_PROPERTY_AKTENZEICHEN, aktenzeichen) : Map.of())
......@@ -330,7 +330,7 @@ public class VorgangChangeHistoryServiceITCase {
.build();
}
Command sc(CommandOrder order) {
Command statusChange(CommandOrder order) {
return CommandTestFactory.createBuilder()
.order(order)
.createdByName(USER_1_FULL_NAME)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment