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

Merge pull request 'OZG-6681 Versand der Nachricht erfolgt nicht' (#18) from...

Merge pull request 'OZG-6681 Versand der Nachricht erfolgt nicht' (#18) from OZG-5681-Versand-der-Nachricht-erfolgt-nicht into master

Reviewed-on: https://git.ozg-sh.de/ozgcloud-lib/api-lib/pulls/18


Reviewed-by: default avatarOZGCloud <ozgcloud@mgm-tp.com>
parents cfacdedb 823a934b
No related branches found
No related tags found
No related merge requests found
package de.ozgcloud.apilib.common.command;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import de.ozgcloud.common.datatype.StringBasedValue;
import lombok.EqualsAndHashCode;
import lombok.extern.log4j.Log4j2;
@Log4j2
@EqualsAndHashCode(callSuper = true)
public class OzgCloudCommandStatus extends StringBasedValue {
......@@ -14,14 +19,24 @@ public class OzgCloudCommandStatus extends StringBasedValue {
public static final OzgCloudCommandStatus REVOKE_PENDING = new OzgCloudCommandStatus("REVOKE_PENDING");
public static final OzgCloudCommandStatus REVOKED = new OzgCloudCommandStatus("REVOKED");
private static final Set<OzgCloudCommandStatus> ALL_STATES = new HashSet<>(Set.of(PENDING, FINISHED, ERROR, REVOKE_PENDING, REVOKED));
private static final Set<OzgCloudCommandStatus> FINAL_STATES = Set.of(FINISHED, ERROR, REVOKED);
OzgCloudCommandStatus(String status) {
super(status);
}
public static OzgCloudCommandStatus from(String status) {
return new OzgCloudCommandStatus(status);
public static OzgCloudCommandStatus from(String statusString) {
return ALL_STATES.stream()
.filter(status -> StringUtils.equals(statusString, status.toString())).findAny()
.orElseGet(() -> unknownStatus(statusString));
}
private static OzgCloudCommandStatus unknownStatus(String statusString) {
LOG.warn("Unknown status string given: {}", statusString);
var newStatus = new OzgCloudCommandStatus(statusString);
ALL_STATES.add(newStatus);
return newStatus;
}
public boolean isFinalState() {
......
......@@ -64,7 +64,7 @@ public class GrpcOzgCloudCommandService implements OzgCloudCommandService {
}
void verifyCommand(OzgCloudCommand command) {
if (command.getStatus() == OzgCloudCommandStatus.ERROR) {
if (OzgCloudCommandStatus.ERROR.equals(command.getStatus())) {
throw new TechnicalException("Command (id=%s) failed: %s".formatted(command.getId(), command.getErrorMessage()));
}
}
......
package de.ozgcloud.apilib.common.command;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class OzgCloudCommandStatusTest {
@Nested
class From {
@Test
void shouldReturnSameInstance() {
OzgCloudCommandStatus result = OzgCloudCommandStatus.from("ERROR");
assertThat(OzgCloudCommandStatus.ERROR).isSameAs(result);
}
@ParameterizedTest
@ValueSource(strings = { "PENDING", "FINISHED", "ERROR", "REVOKE_PENDING", "REVOKED" })
void shouldReturnSameInstance(String status) {
OzgCloudCommandStatus result = OzgCloudCommandStatus.from(status);
assertThat(result).isSameAs(OzgCloudCommandStatus.from(status));
}
@Test
void shouldAllowUnkownStatusString() {
var result = OzgCloudCommandStatus.from("SOMETHING_NEW");
assertThat(result).isNotNull();
}
@Test
void shouldLearnNewStatus() {
var result = OzgCloudCommandStatus.from("SOMETHING_NEW");
assertThat(result).isSameAs(OzgCloudCommandStatus.from("SOMETHING_NEW"));
}
}
}
......@@ -124,7 +124,7 @@ class GrpcOzgCloudCommandServiceTest {
@Test
void shouldThrowException() {
var command = OzgCloudCommandTestFactory.createBuilder().status(OzgCloudCommandStatus.ERROR).build();
var command = OzgCloudCommandTestFactory.createBuilder().status(OzgCloudCommandStatus.from("ERROR")).build();
assertThrows(TechnicalException.class, () -> service.verifyCommand(command));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment