diff --git a/core/src/main/java/org/fuin/ddd4j/core/AbstractAggregateException.java b/core/src/main/java/org/fuin/ddd4j/core/AbstractAggregateException.java index 595871a..b373668 100644 --- a/core/src/main/java/org/fuin/ddd4j/core/AbstractAggregateException.java +++ b/core/src/main/java/org/fuin/ddd4j/core/AbstractAggregateException.java @@ -24,19 +24,19 @@ import java.io.Serial; /** - * An aggregate already exists when trying to create it. + * Base class for aggregate related exceptions. */ public abstract class AbstractAggregateException extends Exception { @Serial private static final long serialVersionUID = 1L; - private final EntityType type; + private final String type; - private final AggregateRootId id; + private final String id; /** - * Constructor with all data. + * Constructor with strongly typed data. * * @param message Error message. * @param type Type of the aggregate. @@ -49,6 +49,24 @@ public AbstractAggregateException(@NotEmpty final String message, Contract.requireArgNotEmpty("message", message); Contract.requireArgNotNull("aggregateType", type); Contract.requireArgNotNull("aggregateId", id); + this.type = type.asString(); + this.id = id.asString(); + } + + /** + * Constructor with string data. + * + * @param message Error message. + * @param type Type of the aggregate. + * @param id Unique identifier of the aggregate. + */ + public AbstractAggregateException(@NotEmpty final String message, + @NotEmpty final String type, + @NotEmpty final String id) { + super(message); + Contract.requireArgNotEmpty("message", message); + Contract.requireArgNotNull("aggregateType", type); + Contract.requireArgNotNull("aggregateId", id); this.type = type; this.id = id; } @@ -59,7 +77,7 @@ public AbstractAggregateException(@NotEmpty final String message, * @return Type. */ @NotNull - public final EntityType getType() { + public final String getType() { return type; } @@ -69,7 +87,7 @@ public final EntityType getType() { * @return Stream with version conflict. */ @NotNull - public final AggregateRootId getId() { + public final String getId() { return id; } diff --git a/core/src/main/java/org/fuin/ddd4j/core/AbstractVersionedAggregateException.java b/core/src/main/java/org/fuin/ddd4j/core/AbstractVersionedAggregateException.java index f858439..0fd4dd9 100644 --- a/core/src/main/java/org/fuin/ddd4j/core/AbstractVersionedAggregateException.java +++ b/core/src/main/java/org/fuin/ddd4j/core/AbstractVersionedAggregateException.java @@ -23,7 +23,7 @@ import java.io.Serial; /** - * An aggregate already exists when trying to create it. + * Base class for version related aggregate exceptions. */ public abstract class AbstractVersionedAggregateException extends AbstractAggregateException { @@ -33,7 +33,7 @@ public abstract class AbstractVersionedAggregateException extends AbstractAggreg private final int version; /** - * Constructor with all data. + * Constructor with strongly typed data. * * @param message Error message. * @param type Type of the aggregate. @@ -44,6 +44,21 @@ public AbstractVersionedAggregateException(@NotEmpty final String message, @NotNull final EntityType type, @NotNull final AggregateRootId id, final int version) { + this(message, type.asString(), id.asString(), version); + } + + /** + * Constructor with string data. + * + * @param message Error message. + * @param type Type of the aggregate. + * @param id Unique identifier of the aggregate. + * @param version Version of the aggregate. + */ + public AbstractVersionedAggregateException(@NotEmpty final String message, + @NotNull final String type, + @NotNull final String id, + final int version) { super(message, type, id); this.version = version; } diff --git a/core/src/main/java/org/fuin/ddd4j/core/AggregateAlreadyExistsException.java b/core/src/main/java/org/fuin/ddd4j/core/AggregateAlreadyExistsException.java index ff5bfea..3b25890 100644 --- a/core/src/main/java/org/fuin/ddd4j/core/AggregateAlreadyExistsException.java +++ b/core/src/main/java/org/fuin/ddd4j/core/AggregateAlreadyExistsException.java @@ -25,10 +25,15 @@ import static org.fuin.ddd4j.core.Ddd4JUtils.SHORT_ID_PREFIX; /** - * An aggregate already exists when trying to create it. + * An aggregate already existed when trying to create it. */ public final class AggregateAlreadyExistsException extends AbstractVersionedAggregateException implements ExceptionShortIdentifable { + /** + * Unique name of the element to use for XML and JSON marshalling/unmarshalling. + */ + public static final String ELEMENT_NAME = "aggregate-already-exists-exception"; + @Serial private static final long serialVersionUID = 1L; @@ -38,14 +43,25 @@ public final class AggregateAlreadyExistsException extends AbstractVersionedAggr public static final String SHORT_ID = SHORT_ID_PREFIX + "-AGGREGATE_ALREADY_EXISTS"; /** - * Constructor with all data. + * Constructor with typed data. * * @param type Type of the aggregate. * @param id Unique identifier of the aggregate. * @param version Actual version. */ public AggregateAlreadyExistsException(@NotNull final EntityType type, @NotNull final AggregateRootId id, final int version) { - super(type.asString() + " " + id.asString() + " already exists (version=" + version + ")", type, id, version); + this(type.asString(), id.asString(), version); + } + + /** + * Constructor with string data. + * + * @param type Type of the aggregate. + * @param id Unique identifier of the aggregate. + * @param version Actual version. + */ + public AggregateAlreadyExistsException(@NotNull final String type, @NotNull final String id, final int version) { + super(type + " " + id + " already exists (version=" + version + ")", type, id, version); } @Override diff --git a/core/src/main/java/org/fuin/ddd4j/core/AggregateDeletedException.java b/core/src/main/java/org/fuin/ddd4j/core/AggregateDeletedException.java index ef0faca..fc52926 100644 --- a/core/src/main/java/org/fuin/ddd4j/core/AggregateDeletedException.java +++ b/core/src/main/java/org/fuin/ddd4j/core/AggregateDeletedException.java @@ -29,6 +29,11 @@ */ public final class AggregateDeletedException extends AbstractAggregateException implements ExceptionShortIdentifable { + /** + * Unique name of the element to use for XML and JSON marshalling/unmarshalling. + */ + public static final String ELEMENT_NAME = "aggregate-deleted-exception"; + @Serial private static final long serialVersionUID = 1L; @@ -47,6 +52,16 @@ public AggregateDeletedException(@NotNull final EntityType type, @NotNull final super(type.asString() + " with id " + id.asString() + " already deleted", type, id); } + /** + * Constructor with string data. + * + * @param type Type of the aggregate. + * @param id Unique identifier of the aggregate. + */ + public AggregateDeletedException(@NotNull final String type, @NotNull final String id) { + super(type + " with id " + id + " already deleted", type, id); + } + @Override public final String getShortId() { return SHORT_ID; diff --git a/core/src/main/java/org/fuin/ddd4j/core/AggregateNotFoundException.java b/core/src/main/java/org/fuin/ddd4j/core/AggregateNotFoundException.java index 9c3cff7..355cfef 100644 --- a/core/src/main/java/org/fuin/ddd4j/core/AggregateNotFoundException.java +++ b/core/src/main/java/org/fuin/ddd4j/core/AggregateNotFoundException.java @@ -29,6 +29,11 @@ */ public final class AggregateNotFoundException extends AbstractAggregateException implements ExceptionShortIdentifable { + /** + * Unique name of the element to use for XML and JSON marshalling/unmarshalling. + */ + public static final String ELEMENT_NAME = "aggregate-not-found-exception"; + @Serial private static final long serialVersionUID = 1L; @@ -44,7 +49,17 @@ public final class AggregateNotFoundException extends AbstractAggregateException * @param id Unique identifier of the aggregate. */ public AggregateNotFoundException(@NotNull final EntityType type, @NotNull final AggregateRootId id) { - super(type.asString() + " with id " + id.asString() + " not found", type, id); + this(type.asString(), id.asString()); + } + + /** + * Constructor with string data. + * + * @param type Type of the aggregate. + * @param id Unique identifier of the aggregate. + */ + public AggregateNotFoundException(@NotNull final String type, @NotNull final String id) { + super(type + " with id " + id + " not found", type, id); } @Override diff --git a/core/src/main/java/org/fuin/ddd4j/core/AggregateVersionConflictException.java b/core/src/main/java/org/fuin/ddd4j/core/AggregateVersionConflictException.java index 2707e3d..6a14603 100644 --- a/core/src/main/java/org/fuin/ddd4j/core/AggregateVersionConflictException.java +++ b/core/src/main/java/org/fuin/ddd4j/core/AggregateVersionConflictException.java @@ -27,7 +27,12 @@ /** * Signals a conflict between an expected and an actual version for an aggregate. */ -public final class AggregateVersionConflictException extends AbstractVersionedAggregateException implements ExceptionShortIdentifable { +public final class AggregateVersionConflictException extends AbstractAggregateException implements ExceptionShortIdentifable { + + /** + * Unique name of the element to use for XML and JSON marshalling/unmarshalling. + */ + public static final String ELEMENT_NAME = "aggregate-version-conflict-exception"; @Serial private static final long serialVersionUID = 1L; @@ -39,6 +44,8 @@ public final class AggregateVersionConflictException extends AbstractVersionedAg private final int expected; + private final int actual; + /** * Constructor with all data. * @@ -51,12 +58,28 @@ public AggregateVersionConflictException(@NotNull final EntityType type, @NotNull final AggregateRootId id, final int expected, final int actual) { - super("Expected version " + expected + " for " + type.asString() + " (" + id.asString() + "), but was " + actual, type, id, actual); + this(type.asString(), id.asString(), expected, actual); + } + + /** + * Constructor with string. + * + * @param type Type of the aggregate. + * @param id Unique identifier of the aggregate. + * @param expected Expected version. + * @param actual Actual version. + */ + public AggregateVersionConflictException(@NotNull final String type, + @NotNull final String id, + final int expected, + final int actual) { + super("Expected version " + expected + " for " + type + " (" + id + "), but was " + actual, type, id); this.expected = expected; + this.actual = actual; } @Override - public final String getShortId() { + public String getShortId() { return SHORT_ID; } @@ -65,7 +88,7 @@ public final String getShortId() { * * @return Expected version. */ - public final int getExpected() { + public int getExpected() { return expected; } @@ -74,8 +97,8 @@ public final int getExpected() { * * @return Actual version. */ - public final int getActual() { - return getVersion(); + public int getActual() { + return actual; } } diff --git a/core/src/main/java/org/fuin/ddd4j/core/AggregateVersionNotFoundException.java b/core/src/main/java/org/fuin/ddd4j/core/AggregateVersionNotFoundException.java index 9111fa0..ceb189c 100644 --- a/core/src/main/java/org/fuin/ddd4j/core/AggregateVersionNotFoundException.java +++ b/core/src/main/java/org/fuin/ddd4j/core/AggregateVersionNotFoundException.java @@ -29,6 +29,11 @@ */ public final class AggregateVersionNotFoundException extends AbstractVersionedAggregateException implements ExceptionShortIdentifable { + /** + * Unique name of the element to use for XML and JSON marshalling/unmarshalling. + */ + public static final String ELEMENT_NAME = "aggregate-version-not-found-exception"; + @Serial private static final long serialVersionUID = 1L; @@ -47,7 +52,20 @@ public final class AggregateVersionNotFoundException extends AbstractVersionedAg public AggregateVersionNotFoundException(@NotNull final EntityType type, @NotNull final AggregateRootId id, final int version) { - super("Requested version " + version + " for " + type.asString() + " (" + id.asString() + ") does not exist", type, id, version); + this(type.asString(), id.asString(), version); + } + + /** + * Constructor with string arguments. + * + * @param type Type of the aggregate. + * @param id Unique identifier of the aggregate. + * @param version Requested version. + */ + public AggregateVersionNotFoundException(@NotNull final String type, + @NotNull final String id, + final int version) { + super("Requested version " + version + " for " + type + " (" + id + ") does not exist", type, id, version); } @Override diff --git a/core/src/main/java/org/fuin/ddd4j/core/DecryptionFailedException.java b/core/src/main/java/org/fuin/ddd4j/core/DecryptionFailedException.java index 3be6c26..19a338c 100644 --- a/core/src/main/java/org/fuin/ddd4j/core/DecryptionFailedException.java +++ b/core/src/main/java/org/fuin/ddd4j/core/DecryptionFailedException.java @@ -28,6 +28,11 @@ */ public final class DecryptionFailedException extends Exception implements ExceptionShortIdentifable { + /** + * Unique name of the element to use for XML and JSON marshalling/unmarshalling. + */ + public static final String ELEMENT_NAME = "decryption-failed-exception"; + @Serial private static final long serialVersionUID = 1L; @@ -42,7 +47,16 @@ public final class DecryptionFailedException extends Exception implements Except * @param cause Original exception that caused the failure. */ public DecryptionFailedException(final Exception cause) { - super("Decryption failed", cause); + super("Decryption failed: " + cause.getMessage(), cause); + } + + /** + * Constructor with message. Should only be used for re-creating the exception on the client side. + * + * @param msg Exception message. + */ + public DecryptionFailedException(final String msg) { + super(msg); } @Override diff --git a/core/src/main/java/org/fuin/ddd4j/core/DuplicateEncryptionKeyIdException.java b/core/src/main/java/org/fuin/ddd4j/core/DuplicateEncryptionKeyIdException.java index 0bd058d..dd36c92 100644 --- a/core/src/main/java/org/fuin/ddd4j/core/DuplicateEncryptionKeyIdException.java +++ b/core/src/main/java/org/fuin/ddd4j/core/DuplicateEncryptionKeyIdException.java @@ -30,6 +30,11 @@ */ public final class DuplicateEncryptionKeyIdException extends Exception implements ExceptionShortIdentifable { + /** + * Unique name of the element to use for XML and JSON marshalling/unmarshalling. + */ + public static final String ELEMENT_NAME = "duplicate-encryption-key-id-exception"; + @Serial private static final long serialVersionUID = 1L; diff --git a/core/src/main/java/org/fuin/ddd4j/core/DuplicateEntityException.java b/core/src/main/java/org/fuin/ddd4j/core/DuplicateEntityException.java index 72c01fb..b4e7a09 100644 --- a/core/src/main/java/org/fuin/ddd4j/core/DuplicateEntityException.java +++ b/core/src/main/java/org/fuin/ddd4j/core/DuplicateEntityException.java @@ -30,6 +30,11 @@ */ public final class DuplicateEntityException extends Exception implements ExceptionShortIdentifable { + /** + * Unique name of the element to use for XML and JSON marshalling/unmarshalling. + */ + public static final String ELEMENT_NAME = "duplicate-entity-exception"; + @Serial private static final long serialVersionUID = 1L; @@ -38,9 +43,9 @@ public final class DuplicateEntityException extends Exception implements Excepti */ public static final String SHORT_ID = SHORT_ID_PREFIX + "-DUPLICATE_ENTITY"; - private final EntityIdPath parentIdPath; + private final String parentIdPath; - private final EntityId entityId; + private final String entityId; /** * Constructor with all data. @@ -49,9 +54,7 @@ public final class DuplicateEntityException extends Exception implements Excepti * @param entityId Unique identifier of the entity that already existed. */ public DuplicateEntityException(@NotNull final EntityIdPath parentIdPath, @NotNull final EntityId entityId) { - super(entityId.asTypedString() + " already exists in " + parentIdPath.asString()); - this.parentIdPath = parentIdPath; - this.entityId = entityId; + this(parentIdPath.asString(), entityId.asTypedString()); } /** @@ -60,7 +63,19 @@ public DuplicateEntityException(@NotNull final EntityIdPath parentIdPath, @NotNu * @param entityIdPath Entity identifier path (from root to entity). Required to contain at least two elements. */ public DuplicateEntityException(@NotNull final EntityIdPath entityIdPath) { - this(Objects.requireNonNull(entityIdPath.parent()), entityIdPath.last()); + this(Objects.requireNonNull(entityIdPath.parent(), "entityIdPath has not parent"), entityIdPath.last()); + } + + /** + * Constructor with string data. + * + * @param parentIdPath Path from root to parent. + * @param entityId Unique identifier of the entity that already existed. + */ + public DuplicateEntityException(@NotNull final String parentIdPath, @NotNull final String entityId) { + super(entityId + " already exists in " + parentIdPath); + this.parentIdPath = parentIdPath; + this.entityId = entityId; } @Override @@ -73,7 +88,7 @@ public final String getShortId() { * * @return Path. */ - public final EntityIdPath getParentIdPath() { + public final String getParentIdPath() { return parentIdPath; } @@ -82,7 +97,7 @@ public final EntityIdPath getParentIdPath() { * * @return Unknown entity identifier. */ - public final EntityId getEntityId() { + public final String getEntityId() { return entityId; } diff --git a/core/src/main/java/org/fuin/ddd4j/core/EncryptionKeyIdUnknownException.java b/core/src/main/java/org/fuin/ddd4j/core/EncryptionKeyIdUnknownException.java index 530f113..9e34928 100644 --- a/core/src/main/java/org/fuin/ddd4j/core/EncryptionKeyIdUnknownException.java +++ b/core/src/main/java/org/fuin/ddd4j/core/EncryptionKeyIdUnknownException.java @@ -30,6 +30,11 @@ */ public final class EncryptionKeyIdUnknownException extends Exception implements ExceptionShortIdentifable { + /** + * Unique name of the element to use for XML and JSON marshalling/unmarshalling. + */ + public static final String ELEMENT_NAME = "encryption-key-id-unknown-exception"; + @Serial private static final long serialVersionUID = 1L; diff --git a/core/src/main/java/org/fuin/ddd4j/core/EncryptionKeyVersionUnknownException.java b/core/src/main/java/org/fuin/ddd4j/core/EncryptionKeyVersionUnknownException.java index ada174c..9ff6cc6 100644 --- a/core/src/main/java/org/fuin/ddd4j/core/EncryptionKeyVersionUnknownException.java +++ b/core/src/main/java/org/fuin/ddd4j/core/EncryptionKeyVersionUnknownException.java @@ -30,6 +30,11 @@ */ public final class EncryptionKeyVersionUnknownException extends Exception implements ExceptionShortIdentifable { + /** + * Unique name of the element to use for XML and JSON marshalling/unmarshalling. + */ + public static final String ELEMENT_NAME = "encryption-key-version-unknown-exception"; + @Serial private static final long serialVersionUID = 1L; diff --git a/core/src/main/java/org/fuin/ddd4j/core/EntityNotFoundException.java b/core/src/main/java/org/fuin/ddd4j/core/EntityNotFoundException.java index 6d07d0b..7bf9856 100644 --- a/core/src/main/java/org/fuin/ddd4j/core/EntityNotFoundException.java +++ b/core/src/main/java/org/fuin/ddd4j/core/EntityNotFoundException.java @@ -23,6 +23,7 @@ import org.fuin.objects4j.common.ExceptionShortIdentifable; import java.io.Serial; +import java.util.Objects; import static org.fuin.ddd4j.core.Ddd4JUtils.SHORT_ID_PREFIX; @@ -31,6 +32,11 @@ */ public final class EntityNotFoundException extends Exception implements ExceptionShortIdentifable { + /** + * Unique name of the element to use for XML and JSON marshalling/unmarshalling. + */ + public static final String ELEMENT_NAME = "entity-not-found-exception"; + @Serial private static final long serialVersionUID = 1L; @@ -39,9 +45,9 @@ public final class EntityNotFoundException extends Exception implements Exceptio */ public static final String SHORT_ID = SHORT_ID_PREFIX + "-ENTITY_NOT_FOUND"; - private final EntityIdPath parentIdPath; + private final String parentIdPath; - private final EntityId entityId; + private final String entityId; /** * Constructor with all data. @@ -50,11 +56,7 @@ public final class EntityNotFoundException extends Exception implements Exceptio * @param entityId Unique identifier of the entity that was not found. */ public EntityNotFoundException(@Nullable final EntityIdPath parentIdPath, @NotNull final EntityId entityId) { - super(parentIdPath == null ? entityId.asTypedString() + " not found" - : entityId.asTypedString() + " not found in " + parentIdPath.asString()); - Contract.requireArgNotNull("entityId", entityId); - this.parentIdPath = parentIdPath; - this.entityId = entityId; + this(parentIdPath == null ? null : parentIdPath.asString(), entityId.asTypedString()); } /** @@ -63,7 +65,21 @@ public EntityNotFoundException(@Nullable final EntityIdPath parentIdPath, @NotNu * @param entityIdPath Entity identifier path (from root to entity). */ public EntityNotFoundException(@NotNull final EntityIdPath entityIdPath) { - this(entityIdPath.parent(), entityIdPath.last()); + this(Objects.requireNonNull(entityIdPath, "entityIdPath==null").parent(), entityIdPath.last()); + } + + /** + * Constructor with string data. + * + * @param parentIdPath Path from root to parent or {@literal null} if the entity identifier is a root aggregate ID. + * @param entityId Unique identifier of the entity that was not found. + */ + public EntityNotFoundException(@Nullable final String parentIdPath, @NotNull final String entityId) { + super(parentIdPath == null ? entityId + " not found" + : entityId + " not found in " + parentIdPath); + Contract.requireArgNotNull("entityId", entityId); + this.parentIdPath = parentIdPath; + this.entityId = entityId; } @Override @@ -77,7 +93,7 @@ public String getShortId() { * @return Path. */ @Nullable - public EntityIdPath getParentIdPath() { + public String getParentIdPath() { return parentIdPath; } @@ -87,7 +103,7 @@ public EntityIdPath getParentIdPath() { * @return Unknown entity identifier. */ @NotNull - public EntityId getEntityId() { + public String getEntityId() { return entityId; } diff --git a/core/src/main/java/org/fuin/ddd4j/core/ExceptionData.java b/core/src/main/java/org/fuin/ddd4j/core/ExceptionData.java new file mode 100644 index 0000000..0243fae --- /dev/null +++ b/core/src/main/java/org/fuin/ddd4j/core/ExceptionData.java @@ -0,0 +1,23 @@ +package org.fuin.ddd4j.core; + +import org.fuin.objects4j.common.ToExceptionCapable; +import org.fuin.objects4j.common.ValueObject; + +import java.io.Serializable; + +/** + * Base for all classes that store data from an exception for marshalling and allowing to recreate it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + * + * @param Concrete type of wrapped exception. + */ +public interface ExceptionData extends Serializable, ValueObject, ToExceptionCapable { + + /** + * Returns the name of the data attribute. + * + * @return Data element name. + */ + public String getDataElement(); + +} diff --git a/core/src/main/java/org/fuin/ddd4j/core/MethodExecutor.java b/core/src/main/java/org/fuin/ddd4j/core/MethodExecutor.java index 8652926..59ef56a 100644 --- a/core/src/main/java/org/fuin/ddd4j/core/MethodExecutor.java +++ b/core/src/main/java/org/fuin/ddd4j/core/MethodExecutor.java @@ -19,8 +19,8 @@ import jakarta.validation.constraints.NotNull; import org.fuin.objects4j.common.Contract; -import javax.annotation.concurrent.ThreadSafe; +import javax.annotation.concurrent.ThreadSafe; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; diff --git a/esc/src/test/java/org/fuin/ddd4j/esc/AggregateStreamIdTest.java b/esc/src/test/java/org/fuin/ddd4j/esc/AggregateStreamIdTest.java index 81d9038..355392b 100644 --- a/esc/src/test/java/org/fuin/ddd4j/esc/AggregateStreamIdTest.java +++ b/esc/src/test/java/org/fuin/ddd4j/esc/AggregateStreamIdTest.java @@ -3,7 +3,6 @@ import nl.jqno.equalsverifier.EqualsVerifier; import org.fuin.ddd4j.core.AggregateRootId; import org.fuin.ddd4j.core.EntityType; -import org.fuin.ddd4j.core.StringBasedEntityType; import org.fuin.ddd4j.jsonbtestmodel.VendorId; import org.fuin.objects4j.core.KeyValue; import org.junit.jupiter.api.Test; diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AbstractAggregateExceptionData.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AbstractAggregateExceptionData.java new file mode 100644 index 0000000..077d19a --- /dev/null +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AbstractAggregateExceptionData.java @@ -0,0 +1,80 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.XmlElement; +import org.fuin.ddd4j.core.AbstractAggregateException; +import org.fuin.ddd4j.core.ExceptionData; +import org.fuin.objects4j.common.ToExceptionCapable; +import org.fuin.objects4j.common.ValueObject; + +import java.io.Serial; +import java.io.Serializable; +import java.util.Objects; + +/** + * Base class for storing the data from a {@link AbstractAggregateException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + * + * @param Concrete type of wrapped exception. + */ +public abstract class AbstractAggregateExceptionData implements ExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @XmlElement(name = "msg") + private String message; + + @XmlElement(name = "aggregate-type") + private String aggregateType; + + @XmlElement(name = "aggregate-id") + private String aggregateId; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected AbstractAggregateExceptionData() { + super(); + } + + /** + * Constructor with exception. + * + * @param ex Exception to copy data from. + */ + public AbstractAggregateExceptionData(final AbstractAggregateException ex) { + super(); + Objects.requireNonNull(ex, "ex==null"); + this.message = ex.getMessage(); + this.aggregateType = ex.getType(); + this.aggregateId = ex.getId(); + } + + /** + * Returns the exception message. + * + * @return Message. + */ + public final String getMessage() { + return message; + } + + /** + * Returns the type of the aggregate. + * + * @return Aggregate type. + */ + public final String getAggregateType() { + return aggregateType; + } + + /** + * Returns the unique aggregate identifier. + * + * @return Aggregate ID. + */ + public final String getAggregateId() { + return aggregateId; + } + +} diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AbstractVersionedAggregateExceptionData.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AbstractVersionedAggregateExceptionData.java new file mode 100644 index 0000000..bd36d8e --- /dev/null +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AbstractVersionedAggregateExceptionData.java @@ -0,0 +1,46 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.XmlElement; +import org.fuin.ddd4j.core.AbstractVersionedAggregateException; + +import java.io.Serial; + +/** + * Base class for storing the data from a {@link AbstractVersionedAggregateException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +public abstract class AbstractVersionedAggregateExceptionData extends AbstractAggregateExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @XmlElement(name = "version") + private int version; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected AbstractVersionedAggregateExceptionData() { + super(); + } + + /** + * Constructor with exception. + * + * @param ex Exception to copy data from. + */ + public AbstractVersionedAggregateExceptionData(final AbstractVersionedAggregateException ex) { + super(ex); + this.version = ex.getVersion(); + } + + /** + * Returns the aggregate version that caused the problem. + * + * @return Aggregate version. + */ + public final int getVersion() { + return version; + } + +} diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateAlreadyExistsExceptionData.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateAlreadyExistsExceptionData.java new file mode 100644 index 0000000..67bdf63 --- /dev/null +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateAlreadyExistsExceptionData.java @@ -0,0 +1,115 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; +import org.fuin.ddd4j.core.AggregateAlreadyExistsException; + +import java.io.Serial; + +import static org.fuin.ddd4j.core.AggregateAlreadyExistsException.ELEMENT_NAME; + +/** + * Stores the data from a {@link AggregateAlreadyExistsException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +@XmlRootElement(name = ELEMENT_NAME) +@XmlAccessorType(XmlAccessType.FIELD) +public final class AggregateAlreadyExistsExceptionData extends AbstractVersionedAggregateExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @XmlElement(name = "sid") + private String sid; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected AggregateAlreadyExistsExceptionData() { + super(); + } + + /** + * Constructor with exception. + * + * @param ex Exception to copy data from. + */ + public AggregateAlreadyExistsExceptionData(final AggregateAlreadyExistsException ex) { + super(ex); + this.sid = ex.getShortId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the unique short identifier od the exception. + * + * @return Unique and human readable identifer. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getAggregateId() == null) ? 0 : getAggregateId().hashCode()); + result = prime * result + ((getAggregateType() == null) ? 0 : getAggregateType().hashCode()); + result = prime * result + ((getMessage() == null) ? 0 : getMessage().hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + result = prime * result + getVersion(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + AggregateAlreadyExistsExceptionData other = (AggregateAlreadyExistsExceptionData) obj; + if (getAggregateId() == null) { + if (other.getAggregateId() != null) + return false; + } else if (!getAggregateId().equals(other.getAggregateId())) + return false; + if (getAggregateType() == null) { + if (other.getAggregateType() != null) + return false; + } else if (!getAggregateType().equals(other.getAggregateType())) + return false; + if (getMessage() == null) { + if (other.getMessage() != null) + return false; + } else if (!getMessage().equals(other.getMessage())) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + if (getVersion() != other.getVersion()) + return false; + return true; + } + + @Override + public String toString() { + return "Data [message=" + getMessage() + ", sid=" + sid + ", aggregateType=" + getAggregateType() + + ", aggregateId=" + getAggregateId() + ", version=" + getVersion() + "]"; + } + + @Override + public AggregateAlreadyExistsException toException() { + return new AggregateAlreadyExistsException(getAggregateType(), getAggregateId(), getVersion()); + } + +} diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateDeletedExceptionData.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateDeletedExceptionData.java new file mode 100644 index 0000000..da913d8 --- /dev/null +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateDeletedExceptionData.java @@ -0,0 +1,131 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jaxb; + +import jakarta.validation.constraints.NotNull; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; +import org.fuin.ddd4j.core.AggregateAlreadyExistsException; +import org.fuin.ddd4j.core.AggregateDeletedException; + +import java.io.Serial; + +import static org.fuin.ddd4j.core.AggregateDeletedException.ELEMENT_NAME; + +/** + * Stores the data from a {@link AggregateDeletedException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +@XmlRootElement(name = ELEMENT_NAME) +@XmlAccessorType(XmlAccessType.NONE) +public final class AggregateDeletedExceptionData extends AbstractAggregateExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @XmlElement(name = "sid") + private String sid; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected AggregateDeletedExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public AggregateDeletedExceptionData(@NotNull final AggregateDeletedException ex) { + super(ex); + this.sid = ex.getShortId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getAggregateId() == null) ? 0 : getAggregateId().hashCode()); + result = prime * result + ((getAggregateType() == null) ? 0 : getAggregateType().hashCode()); + result = prime * result + ((getMessage() == null) ? 0 : getMessage().hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + AggregateDeletedExceptionData other = (AggregateDeletedExceptionData) obj; + if (getAggregateId() == null) { + if (other.getAggregateId() != null) + return false; + } else if (!getAggregateId().equals(other.getAggregateId())) + return false; + if (getAggregateType() == null) { + if (other.getAggregateType() != null) + return false; + } else if (!getAggregateType().equals(other.getAggregateType())) + return false; + if (getMessage() == null) { + if (other.getMessage() != null) + return false; + } else if (!getMessage().equals(other.getMessage())) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + return true; + } + + @Override + public String toString() { + return "AggregateDeletedExceptionData [getMessage()=" + getMessage() + ", sid=" + sid + ", getAggregateType()=" + getAggregateType() + ", getAggregateId()=" + getAggregateId() + "]"; + } + + @Override + public AggregateDeletedException toException() { + return new AggregateDeletedException(getAggregateType(), getAggregateId()); + } + +} + diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateNotFoundExceptionData.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateNotFoundExceptionData.java new file mode 100644 index 0000000..897f1cf --- /dev/null +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateNotFoundExceptionData.java @@ -0,0 +1,111 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; +import org.fuin.ddd4j.core.AggregateNotFoundException; + +import java.io.Serial; + +import static org.fuin.ddd4j.core.AggregateNotFoundException.ELEMENT_NAME; + +/** + * Stores the data from a {@link AggregateNotFoundException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +@XmlRootElement(name = ELEMENT_NAME) +@XmlAccessorType(XmlAccessType.NONE) +public final class AggregateNotFoundExceptionData extends AbstractAggregateExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @XmlElement(name = "sid") + private String sid; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected AggregateNotFoundExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public AggregateNotFoundExceptionData(final AggregateNotFoundException ex) { + super(ex); + this.sid = ex.getShortId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getAggregateId() == null) ? 0 : getAggregateId().hashCode()); + result = prime * result + ((getAggregateType() == null) ? 0 : getAggregateType().hashCode()); + result = prime * result + ((getMessage() == null) ? 0 : getMessage().hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + AggregateNotFoundExceptionData other = (AggregateNotFoundExceptionData) obj; + if (getAggregateId() == null) { + if (other.getAggregateId() != null) + return false; + } else if (!getAggregateId().equals(other.getAggregateId())) + return false; + if (getAggregateType() == null) { + if (other.getAggregateType() != null) + return false; + } else if (!getAggregateType().equals(other.getAggregateType())) + return false; + if (getMessage() == null) { + if (other.getMessage() != null) + return false; + } else if (!getMessage().equals(other.getMessage())) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + return true; + } + + @Override + public String toString() { + return "AggregateNotFoundExceptionData [getMessage()=" + getMessage() + ", sid=" + sid + ", getAggregateType()=" + getAggregateType() + ", getAggregateId()=" + getAggregateId() + "]"; + } + + @Override + public AggregateNotFoundException toException() { + return new AggregateNotFoundException(getAggregateType(), getAggregateId()); + } + +} diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateVersionConflictExceptionData.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateVersionConflictExceptionData.java new file mode 100644 index 0000000..22470d2 --- /dev/null +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateVersionConflictExceptionData.java @@ -0,0 +1,127 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.validation.constraints.NotNull; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; +import org.fuin.ddd4j.core.AggregateVersionConflictException; + +import java.io.Serial; + +import static org.fuin.ddd4j.core.AggregateVersionConflictException.ELEMENT_NAME; + +/** + * Stores the data from a {@link AggregateVersionConflictException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +@XmlRootElement(name = ELEMENT_NAME) +@XmlAccessorType(XmlAccessType.NONE) +public final class AggregateVersionConflictExceptionData extends AbstractAggregateExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @XmlElement(name = "sid") + private String sid; + + @XmlElement(name = "expected-version") + private int expected; + + @XmlElement(name = "actual-version") + private int actual; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected AggregateVersionConflictExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public AggregateVersionConflictExceptionData(@NotNull final AggregateVersionConflictException ex) { + super(ex); + this.sid = ex.getShortId(); + this.expected = ex.getExpected(); + this.actual = ex.getActual(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + actual; + result = prime * result + ((getAggregateId() == null) ? 0 : getAggregateId().hashCode()); + result = prime * result + ((getAggregateType() == null) ? 0 : getAggregateType().hashCode()); + result = prime * result + expected; + result = prime * result + ((getMessage() == null) ? 0 : getMessage().hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + AggregateVersionConflictExceptionData other = (AggregateVersionConflictExceptionData) obj; + if (actual != other.actual) + return false; + if (getAggregateId() == null) { + if (other.getAggregateId() != null) + return false; + } else if (!getAggregateId().equals(other.getAggregateId())) + return false; + if (getAggregateType() == null) { + if (other.getAggregateType() != null) + return false; + } else if (!getAggregateType().equals(other.getAggregateType())) + return false; + if (expected != other.expected) + return false; + if (getMessage() == null) { + if (other.getMessage() != null) + return false; + } else if (!getMessage().equals(other.getMessage())) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + return true; + } + + @Override + public String toString() { + return "AggregateVersionConflictExceptionData [getMessage()=" + getMessage() + ", sid=" + sid + ", getAggregateType()=" + getAggregateType() + ", getAggregateId()=" + getAggregateId() + + ", expected=" + expected + ", actual=" + actual + "]"; + } + + @Override + public AggregateVersionConflictException toException() { + return new AggregateVersionConflictException(getAggregateType(), getAggregateId(), expected, actual); + } + +} diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateVersionNotFoundExceptionData.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateVersionNotFoundExceptionData.java new file mode 100644 index 0000000..f71f201 --- /dev/null +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateVersionNotFoundExceptionData.java @@ -0,0 +1,116 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.validation.constraints.NotNull; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; +import org.fuin.ddd4j.core.AggregateVersionNotFoundException; + +import java.io.Serial; + +import static org.fuin.ddd4j.core.AggregateVersionNotFoundException.ELEMENT_NAME; + +/** + * Stores the data from a {@link AggregateVersionNotFoundException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +@XmlRootElement(name = ELEMENT_NAME) +@XmlAccessorType(XmlAccessType.NONE) +public final class AggregateVersionNotFoundExceptionData extends AbstractVersionedAggregateExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @XmlElement(name = "sid") + private String sid; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected AggregateVersionNotFoundExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public AggregateVersionNotFoundExceptionData(@NotNull final AggregateVersionNotFoundException ex) { + super(ex); + this.sid = ex.getShortId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getAggregateId() == null) ? 0 : getAggregateId().hashCode()); + result = prime * result + ((getAggregateType() == null) ? 0 : getAggregateType().hashCode()); + result = prime * result + ((getMessage() == null) ? 0 : getMessage().hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + result = prime * result + getVersion(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + AggregateVersionNotFoundExceptionData other = (AggregateVersionNotFoundExceptionData) obj; + if (getAggregateId() == null) { + if (other.getAggregateId() != null) + return false; + } else if (!getAggregateId().equals(other.getAggregateId())) + return false; + if (getAggregateType() == null) { + if (other.getAggregateType() != null) + return false; + } else if (!getAggregateType().equals(other.getAggregateType())) + return false; + if (getMessage() == null) { + if (other.getMessage() != null) + return false; + } else if (!getMessage().equals(other.getMessage())) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + if (getVersion() != other.getVersion()) + return false; + return true; + } + + @Override + public String toString() { + return "AggregateVersionNotFoundExceptionData [getMessage()=" + getMessage() + ", sid=" + sid + ", getAggregateType()=" + getAggregateType() + ", getAggregateId()=" + getAggregateId() + + ", getVersion()=" + getVersion() + "]"; + } + + @Override + public AggregateVersionNotFoundException toException() { + return new AggregateVersionNotFoundException(getAggregateType(), getAggregateId(), getVersion()); + } + +} diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateVersionXmlAdapter.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateVersionXmlAdapter.java index d1673ed..f5c1da0 100644 --- a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateVersionXmlAdapter.java +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/AggregateVersionXmlAdapter.java @@ -19,6 +19,7 @@ import jakarta.xml.bind.annotation.adapters.XmlAdapter; import org.fuin.ddd4j.core.AggregateVersion; + import javax.annotation.concurrent.ThreadSafe; /** diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/DecryptionFailedExceptionData.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/DecryptionFailedExceptionData.java new file mode 100644 index 0000000..7083e09 --- /dev/null +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/DecryptionFailedExceptionData.java @@ -0,0 +1,114 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.validation.constraints.NotNull; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; +import org.fuin.ddd4j.core.DecryptionFailedException; +import org.fuin.ddd4j.core.ExceptionData; + +import java.io.Serial; + +import static org.fuin.ddd4j.core.DecryptionFailedException.ELEMENT_NAME; + +/** + * Stores the data from a {@link DecryptionFailedException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +@XmlRootElement(name = ELEMENT_NAME) +@XmlAccessorType(XmlAccessType.NONE) +public final class DecryptionFailedExceptionData implements ExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @XmlElement(name = "msg") + private String message; + + @XmlElement(name = "sid") + private String sid; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected DecryptionFailedExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public DecryptionFailedExceptionData(@NotNull final DecryptionFailedException ex) { + super(); + this.message = ex.getMessage(); + this.sid = ex.getShortId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the exception message. + * + * @return Message. + */ + public final String getMessage() { + return message; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((message == null) ? 0 : message.hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DecryptionFailedExceptionData other = (DecryptionFailedExceptionData) obj; + if (message == null) { + if (other.message != null) + return false; + } else if (!message.equals(other.message)) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + return true; + } + + @Override + public String toString() { + return "DecryptionFailedExceptionData [message=" + message + ", sid=" + sid + "]"; + } + + @Override + public DecryptionFailedException toException() { + return new DecryptionFailedException(message); + } + +} diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/DuplicateEncryptionKeyIdExceptionData.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/DuplicateEncryptionKeyIdExceptionData.java new file mode 100644 index 0000000..c94cd12 --- /dev/null +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/DuplicateEncryptionKeyIdExceptionData.java @@ -0,0 +1,130 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.validation.constraints.NotNull; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; +import org.fuin.ddd4j.core.DuplicateEncryptionKeyIdException; +import org.fuin.ddd4j.core.ExceptionData; + +import static org.fuin.ddd4j.core.DuplicateEncryptionKeyIdException.ELEMENT_NAME; + +/** + * Stores the data from a {@link DuplicateEncryptionKeyIdException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +@XmlRootElement(name = ELEMENT_NAME) +@XmlAccessorType(XmlAccessType.NONE) +public final class DuplicateEncryptionKeyIdExceptionData implements ExceptionData { + + private static final long serialVersionUID = 1000L; + + @XmlElement(name = "msg") + private String message; + + @XmlElement(name = "sid") + private String sid; + + @XmlElement(name = "key-id") + private String keyId; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected DuplicateEncryptionKeyIdExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public DuplicateEncryptionKeyIdExceptionData(@NotNull final DuplicateEncryptionKeyIdException ex) { + super(); + this.message = ex.getMessage(); + this.sid = ex.getShortId(); + this.keyId = ex.getKeyId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the exception message. + * + * @return Message. + */ + public String getMessage() { + return message; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + /** + * Returns the key identifier that caused the problem. + * + * @return Key ID. + */ + public String getKeyId() { + return keyId; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((message == null) ? 0 : message.hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + result = prime * result + ((keyId == null) ? 0 : keyId.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DuplicateEncryptionKeyIdExceptionData other = (DuplicateEncryptionKeyIdExceptionData) obj; + if (message == null) { + if (other.message != null) + return false; + } else if (!message.equals(other.message)) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + if (keyId == null) { + if (other.keyId != null) + return false; + } else if (!keyId.equals(other.keyId)) + return false; + return true; + } + + @Override + public String toString() { + return "DuplicateEncryptionKeyIdExceptionData [message=" + message + ", sid=" + sid + ", keyId=" + keyId + "]"; + } + + @Override + public DuplicateEncryptionKeyIdException toException() { + return new DuplicateEncryptionKeyIdException(keyId); + } + +} \ No newline at end of file diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/DuplicateEntityExceptionData.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/DuplicateEntityExceptionData.java new file mode 100644 index 0000000..a85c40a --- /dev/null +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/DuplicateEntityExceptionData.java @@ -0,0 +1,149 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.validation.constraints.NotNull; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; +import org.fuin.ddd4j.core.DuplicateEntityException; +import org.fuin.ddd4j.core.ExceptionData; + +import static org.fuin.ddd4j.core.DuplicateEntityException.ELEMENT_NAME; + +/** + * Stores the data from a {@link DuplicateEntityException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +@XmlRootElement(name = ELEMENT_NAME) +@XmlAccessorType(XmlAccessType.NONE) +public class DuplicateEntityExceptionData implements ExceptionData { + + private static final long serialVersionUID = 1000L; + + @XmlElement(name = "msg") + private String message; + + @XmlElement(name = "sid") + private String sid; + + @XmlElement(name = "parent-id-path") + private String parentIdPath; + + @XmlElement(name = "entity-id") + private String entityId; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected DuplicateEntityExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public DuplicateEntityExceptionData(@NotNull final DuplicateEntityException ex) { + super(); + this.message = ex.getMessage(); + this.sid = ex.getShortId(); + this.parentIdPath = ex.getParentIdPath(); + this.entityId = ex.getEntityId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the exception message. + * + * @return Message. + */ + public final String getMessage() { + return message; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + /** + * Returns the path of parent (without entity ID itself). + * + * @return Path. + */ + public String getParentIdPath() { + return parentIdPath; + } + + /** + * Returns the unique identifier of the entity. + * + * @return Entity ID. + */ + public String getEntityId() { + return entityId; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((entityId == null) ? 0 : entityId.hashCode()); + result = prime * result + ((message == null) ? 0 : message.hashCode()); + result = prime * result + ((parentIdPath == null) ? 0 : parentIdPath.hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DuplicateEntityExceptionData other = (DuplicateEntityExceptionData) obj; + if (entityId == null) { + if (other.entityId != null) + return false; + } else if (!entityId.equals(other.entityId)) + return false; + if (message == null) { + if (other.message != null) + return false; + } else if (!message.equals(other.message)) + return false; + if (parentIdPath == null) { + if (other.parentIdPath != null) + return false; + } else if (!parentIdPath.equals(other.parentIdPath)) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + return true; + } + + @Override + public String toString() { + return "DuplicateEntityExceptionData [message=" + message + ", sid=" + sid + ", parentIdPath=" + parentIdPath + ", entityId=" + entityId + "]"; + } + + @Override + public DuplicateEntityException toException() { + return new DuplicateEntityException(parentIdPath, entityId); + } + +} diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EncryptionKeyIdUnknownExceptionData.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EncryptionKeyIdUnknownExceptionData.java new file mode 100644 index 0000000..78cc8a1 --- /dev/null +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EncryptionKeyIdUnknownExceptionData.java @@ -0,0 +1,130 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.validation.constraints.NotNull; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; +import org.fuin.ddd4j.core.EncryptionKeyIdUnknownException; +import org.fuin.ddd4j.core.ExceptionData; + +import static org.fuin.ddd4j.core.EncryptionKeyIdUnknownException.ELEMENT_NAME; + +/** + * Stores the data from a {@link EncryptionKeyIdUnknownException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +@XmlRootElement(name = ELEMENT_NAME) +@XmlAccessorType(XmlAccessType.NONE) +public class EncryptionKeyIdUnknownExceptionData implements ExceptionData { + + private static final long serialVersionUID = 1000L; + + @XmlElement(name = "msg") + private String message; + + @XmlElement(name = "sid") + private String sid; + + @XmlElement(name = "key-id") + private String keyId; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected EncryptionKeyIdUnknownExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public EncryptionKeyIdUnknownExceptionData(@NotNull final EncryptionKeyIdUnknownException ex) { + super(); + this.message = ex.getMessage(); + this.sid = ex.getShortId(); + this.keyId = ex.getKeyId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the exception message. + * + * @return Message. + */ + public final String getMessage() { + return message; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + /** + * Returns the key identifier that caused the problem. + * + * @return Key ID. + */ + public String getKeyId() { + return keyId; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((message == null) ? 0 : message.hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + result = prime * result + ((keyId == null) ? 0 : keyId.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + EncryptionKeyIdUnknownExceptionData other = (EncryptionKeyIdUnknownExceptionData) obj; + if (message == null) { + if (other.message != null) + return false; + } else if (!message.equals(other.message)) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + if (keyId == null) { + if (other.keyId != null) + return false; + } else if (!keyId.equals(other.keyId)) + return false; + return true; + } + + @Override + public String toString() { + return "EncryptionKeyIdUnknownExceptionData [message=" + message + ", sid=" + sid + ", keyId=" + keyId + "]"; + } + + @Override + public EncryptionKeyIdUnknownException toException() { + return new EncryptionKeyIdUnknownException(keyId); + } + +} diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EncryptionKeyVersionUnknownExceptionData.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EncryptionKeyVersionUnknownExceptionData.java new file mode 100644 index 0000000..424aa65 --- /dev/null +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EncryptionKeyVersionUnknownExceptionData.java @@ -0,0 +1,130 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.validation.constraints.NotNull; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; +import org.fuin.ddd4j.core.EncryptionKeyVersionUnknownException; +import org.fuin.ddd4j.core.ExceptionData; + +import static org.fuin.ddd4j.core.EncryptionKeyVersionUnknownException.ELEMENT_NAME; + +/** + * Stores the data from a {@link EncryptionKeyVersionUnknownException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +@XmlRootElement(name = ELEMENT_NAME) +@XmlAccessorType(XmlAccessType.NONE) +public class EncryptionKeyVersionUnknownExceptionData implements ExceptionData { + + private static final long serialVersionUID = 1000L; + + @XmlElement(name = "msg") + private String message; + + @XmlElement(name = "sid") + private String sid; + + @XmlElement(name = "key-version") + private String keyVersion; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected EncryptionKeyVersionUnknownExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public EncryptionKeyVersionUnknownExceptionData(@NotNull final EncryptionKeyVersionUnknownException ex) { + super(); + this.message = ex.getMessage(); + this.sid = ex.getShortId(); + this.keyVersion = ex.getKeyVersion(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the exception message. + * + * @return Message. + */ + public final String getMessage() { + return message; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + /** + * Returns the key version that caused the problem. + * + * @return Key version. + */ + public String getKeyVersion() { + return keyVersion; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((message == null) ? 0 : message.hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + result = prime * result + ((keyVersion == null) ? 0 : keyVersion.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + EncryptionKeyVersionUnknownExceptionData other = (EncryptionKeyVersionUnknownExceptionData) obj; + if (message == null) { + if (other.message != null) + return false; + } else if (!message.equals(other.message)) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + if (keyVersion == null) { + if (other.keyVersion != null) + return false; + } else if (!keyVersion.equals(other.keyVersion)) + return false; + return true; + } + + @Override + public String toString() { + return "EncryptionKeyVersionUnknownExceptionData [message=" + message + ", sid=" + sid + ", keyVersion=" + keyVersion + "]"; + } + + @Override + public EncryptionKeyVersionUnknownException toException() { + return new EncryptionKeyVersionUnknownException(keyVersion); + } + +} diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EntityIdPathXmlAdapter.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EntityIdPathXmlAdapter.java index 764ec0c..d851456 100644 --- a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EntityIdPathXmlAdapter.java +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EntityIdPathXmlAdapter.java @@ -19,9 +19,10 @@ import org.fuin.ddd4j.core.EntityIdFactory; import org.fuin.ddd4j.core.EntityIdPath; -import javax.annotation.concurrent.ThreadSafe; import org.fuin.objects4j.jaxb.ValueObjectStringXmlAdapter; +import javax.annotation.concurrent.ThreadSafe; + /** * JAXB adapter for an entity identifier path. */ diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EntityIdXmlAdapter.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EntityIdXmlAdapter.java index bba4b09..a21750b 100644 --- a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EntityIdXmlAdapter.java +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EntityIdXmlAdapter.java @@ -20,9 +20,10 @@ import jakarta.xml.bind.annotation.adapters.XmlAdapter; import org.fuin.ddd4j.core.EntityId; import org.fuin.ddd4j.core.EntityIdFactory; -import javax.annotation.concurrent.ThreadSafe; import org.fuin.objects4j.common.ValueOfCapable; +import javax.annotation.concurrent.ThreadSafe; + /** * JAXB adapter for an entity identifier. */ diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EntityNotFoundExceptionData.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EntityNotFoundExceptionData.java new file mode 100644 index 0000000..cb6daab --- /dev/null +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EntityNotFoundExceptionData.java @@ -0,0 +1,149 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.validation.constraints.NotNull; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; +import org.fuin.ddd4j.core.EntityNotFoundException; +import org.fuin.ddd4j.core.ExceptionData; + +import static org.fuin.ddd4j.core.EntityNotFoundException.ELEMENT_NAME; + +/** + * Stores the data from a {@link EntityNotFoundException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +@XmlRootElement(name = ELEMENT_NAME) +@XmlAccessorType(XmlAccessType.NONE) +public class EntityNotFoundExceptionData implements ExceptionData { + + private static final long serialVersionUID = 1000L; + + @XmlElement(name = "msg") + private String message; + + @XmlElement(name = "sid") + private String sid; + + @XmlElement(name = "parent-id-path") + private String parentIdPath; + + @XmlElement(name = "entity-id") + private String entityId; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected EntityNotFoundExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public EntityNotFoundExceptionData(@NotNull final EntityNotFoundException ex) { + super(); + this.message = ex.getMessage(); + this.sid = ex.getShortId(); + this.parentIdPath = ex.getParentIdPath(); + this.entityId = ex.getEntityId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the exception message. + * + * @return Message. + */ + public final String getMessage() { + return message; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + /** + * Returns the path of parent (without entity ID itself). + * + * @return Path. + */ + public String getParentIdPath() { + return parentIdPath; + } + + /** + * Returns the unique identifier of the entity. + * + * @return Entity ID. + */ + public String getEntityId() { + return entityId; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((entityId == null) ? 0 : entityId.hashCode()); + result = prime * result + ((message == null) ? 0 : message.hashCode()); + result = prime * result + ((parentIdPath == null) ? 0 : parentIdPath.hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + EntityNotFoundExceptionData other = (EntityNotFoundExceptionData) obj; + if (entityId == null) { + if (other.entityId != null) + return false; + } else if (!entityId.equals(other.entityId)) + return false; + if (message == null) { + if (other.message != null) + return false; + } else if (!message.equals(other.message)) + return false; + if (parentIdPath == null) { + if (other.parentIdPath != null) + return false; + } else if (!parentIdPath.equals(other.parentIdPath)) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + return true; + } + + @Override + public String toString() { + return "EntityNotFoundExceptionData [message=" + message + ", sid=" + sid + ", parentIdPath=" + parentIdPath + ", entityId=" + entityId + "]"; + } + + @Override + public EntityNotFoundException toException() { + return new EntityNotFoundException(parentIdPath, entityId); + } + +} diff --git a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EventIdXmlAdapter.java b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EventIdXmlAdapter.java index 01069ce..e95d460 100644 --- a/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EventIdXmlAdapter.java +++ b/jaxb/src/main/java/org/fuin/ddd4j/jaxb/EventIdXmlAdapter.java @@ -18,9 +18,10 @@ package org.fuin.ddd4j.jaxb; import org.fuin.ddd4j.core.EventId; -import javax.annotation.concurrent.ThreadSafe; import org.fuin.objects4j.jaxb.ValueObjectStringXmlAdapter; +import javax.annotation.concurrent.ThreadSafe; + /** * JAXB and JPA converter for an entity identifier. */ diff --git a/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateAlreadyExistsExceptionDataTest.java b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateAlreadyExistsExceptionDataTest.java new file mode 100644 index 0000000..f4e862b --- /dev/null +++ b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateAlreadyExistsExceptionDataTest.java @@ -0,0 +1,112 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.adapters.XmlAdapter; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.AggregateAlreadyExistsException; +import org.fuin.ddd4j.jaxbtest.JaxbTestEntityIdFactory; +import org.fuin.ddd4j.jaxbtest.VendorId; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; +import static org.fuin.utils4j.jaxb.JaxbUtils.marshal; +import static org.fuin.utils4j.jaxb.JaxbUtils.unmarshal; + +/** + * Test for the {@link AggregateAlreadyExistsExceptionData} class. + */ +class AggregateAlreadyExistsExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(AggregateAlreadyExistsExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final AggregateAlreadyExistsException originalEx = new AggregateAlreadyExistsException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102); + final AggregateAlreadyExistsExceptionData original = new AggregateAlreadyExistsExceptionData(originalEx); + + // TEST + final AggregateAlreadyExistsExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + final AggregateAlreadyExistsException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getVersion()).isEqualTo(originalEx.getVersion()); + + } + + @Test + public final void testMarshalUnmarshal() { + + // PREPARE + final AggregateAlreadyExistsException originalEx = new AggregateAlreadyExistsException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102); + final AggregateAlreadyExistsExceptionData original = new AggregateAlreadyExistsExceptionData(originalEx); + + // TEST + final String xml = marshal(original, createXmlAdapter(), AggregateAlreadyExistsExceptionData.class); + final AggregateAlreadyExistsExceptionData copy = unmarshal(xml, createXmlAdapter(), AggregateAlreadyExistsExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + final AggregateAlreadyExistsException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getVersion()).isEqualTo(originalEx.getVersion()); + + } + + @Test + public final void testUnmarshal() { + + // PREPARE + final AggregateAlreadyExistsException originalEx = new AggregateAlreadyExistsException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102); + final String xml = """ + + + Vendor 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 already exists (version=102) + DDD4J-AGGREGATE_ALREADY_EXISTS + Vendor + 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 + 102 + + """; + + // TEST + final AggregateAlreadyExistsExceptionData copy = unmarshal(xml, createXmlAdapter(), AggregateAlreadyExistsExceptionData.class); + + // VERIFY + final AggregateAlreadyExistsException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getVersion()).isEqualTo(originalEx.getVersion()); + + } + + private static XmlAdapter[] createXmlAdapter() { + return new XmlAdapter[]{new EntityIdPathXmlAdapter(new JaxbTestEntityIdFactory())}; + } + +} diff --git a/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateDeletedExceptionDataTest.java b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateDeletedExceptionDataTest.java new file mode 100644 index 0000000..a408354 --- /dev/null +++ b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateDeletedExceptionDataTest.java @@ -0,0 +1,109 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.adapters.XmlAdapter; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.AggregateDeletedException; +import org.fuin.ddd4j.jaxbtest.JaxbTestEntityIdFactory; +import org.fuin.ddd4j.jaxbtest.VendorId; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; +import static org.fuin.utils4j.jaxb.JaxbUtils.marshal; +import static org.fuin.utils4j.jaxb.JaxbUtils.unmarshal; + +/** + * Test for the {@link AggregateDeletedExceptionData} class. + */ +class AggregateDeletedExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(AggregateDeletedExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final AggregateDeletedException originalEx = new AggregateDeletedException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final AggregateDeletedExceptionData original = new AggregateDeletedExceptionData(originalEx); + + // TEST + final AggregateDeletedExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateDeletedException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testMarshalUnmarshal() { + + // PREPARE + final AggregateDeletedException originalEx = new AggregateDeletedException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final AggregateDeletedExceptionData original = new AggregateDeletedExceptionData(originalEx); + + // TEST + final String xml = marshal(original, createXmlAdapter(), AggregateDeletedExceptionData.class); + final AggregateDeletedExceptionData copy = unmarshal(xml, createXmlAdapter(), AggregateDeletedExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateDeletedException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testUnmarshal() { + + // PREPARE + final AggregateDeletedException originalEx = new AggregateDeletedException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final String xml = """ + + + Vendor with id 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 already deleted + DDD4J-AGGREGATE_DELETED + Vendor + 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 + + """; + + // TEST + final AggregateDeletedExceptionData copy = unmarshal(xml, createXmlAdapter(), AggregateDeletedExceptionData.class); + + // VERIFY + final AggregateDeletedException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + private static XmlAdapter[] createXmlAdapter() { + return new XmlAdapter[]{new EntityIdPathXmlAdapter(new JaxbTestEntityIdFactory())}; + } + +} diff --git a/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateNotFoundExceptionDataTest.java b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateNotFoundExceptionDataTest.java new file mode 100644 index 0000000..d080786 --- /dev/null +++ b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateNotFoundExceptionDataTest.java @@ -0,0 +1,109 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.adapters.XmlAdapter; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.AggregateNotFoundException; +import org.fuin.ddd4j.jaxbtest.JaxbTestEntityIdFactory; +import org.fuin.ddd4j.jaxbtest.VendorId; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; +import static org.fuin.utils4j.jaxb.JaxbUtils.marshal; +import static org.fuin.utils4j.jaxb.JaxbUtils.unmarshal; + +/** + * Test for the {@link AggregateNotFoundExceptionData} class. + */ +class AggregateNotFoundExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(AggregateNotFoundExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final AggregateNotFoundException originalEx = new AggregateNotFoundException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final AggregateNotFoundExceptionData original = new AggregateNotFoundExceptionData(originalEx); + + // TEST + final AggregateNotFoundExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateNotFoundException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testMarshalUnmarshal() { + + // PREPARE + final AggregateNotFoundException originalEx = new AggregateNotFoundException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final AggregateNotFoundExceptionData original = new AggregateNotFoundExceptionData(originalEx); + + // TEST + final String xml = marshal(original, createXmlAdapter(), AggregateNotFoundExceptionData.class); + final AggregateNotFoundExceptionData copy = unmarshal(xml, createXmlAdapter(), AggregateNotFoundExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateNotFoundException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testUnmarshal() { + + // PREPARE + final AggregateNotFoundException originalEx = new AggregateNotFoundException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final String xml = """ + + + Vendor with id 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 not found + DDD4J-AGGREGATE_NOT_FOUND + Vendor + 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 + + """; + + // TEST + final AggregateNotFoundExceptionData copy = unmarshal(xml, createXmlAdapter(), AggregateNotFoundExceptionData.class); + + // VERIFY + final AggregateNotFoundException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + private static XmlAdapter[] createXmlAdapter() { + return new XmlAdapter[]{new EntityIdPathXmlAdapter(new JaxbTestEntityIdFactory())}; + } + +} diff --git a/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateVersionConflictExceptionDataTest.java b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateVersionConflictExceptionDataTest.java new file mode 100644 index 0000000..68b67ed --- /dev/null +++ b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateVersionConflictExceptionDataTest.java @@ -0,0 +1,117 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.adapters.XmlAdapter; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.AggregateVersionConflictException; +import org.fuin.ddd4j.jaxbtest.JaxbTestEntityIdFactory; +import org.fuin.ddd4j.jaxbtest.VendorId; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; +import static org.fuin.utils4j.jaxb.JaxbUtils.marshal; +import static org.fuin.utils4j.jaxb.JaxbUtils.unmarshal; + +/** + * Test for the {@link AggregateVersionConflictExceptionData} class. + */ +class AggregateVersionConflictExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(AggregateVersionConflictExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final AggregateVersionConflictException originalEx = new AggregateVersionConflictException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102, 103); + final AggregateVersionConflictExceptionData original = new AggregateVersionConflictExceptionData(originalEx); + + // TEST + final AggregateVersionConflictExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateVersionConflictException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getExpected()).isEqualTo(originalEx.getExpected()); + assertThat(copyEx.getActual()).isEqualTo(originalEx.getActual()); + + } + + @Test + public final void testMarshalUnmarshal() { + + // PREPARE + final AggregateVersionConflictException originalEx = new AggregateVersionConflictException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102, 103); + final AggregateVersionConflictExceptionData original = new AggregateVersionConflictExceptionData(originalEx); + + // TEST + final String xml = marshal(original, createXmlAdapter(), AggregateVersionConflictExceptionData.class); + final AggregateVersionConflictExceptionData copy = unmarshal(xml, createXmlAdapter(), AggregateVersionConflictExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateVersionConflictException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getExpected()).isEqualTo(originalEx.getExpected()); + assertThat(copyEx.getActual()).isEqualTo(originalEx.getActual()); + + } + + @Test + public final void testUnmarshal() { + + // PREPARE + final AggregateVersionConflictException originalEx = new AggregateVersionConflictException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102, 103); + final String xml = """ + + + Expected version 102 for Vendor (4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119), but was 103 + DDD4J-AGGREGATE_VERSION_CONFLICT + Vendor + 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 + 102 + 103 + + """; + + // TEST + final AggregateVersionConflictExceptionData copy = unmarshal(xml, createXmlAdapter(), AggregateVersionConflictExceptionData.class); + + // VERIFY + final AggregateVersionConflictException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getExpected()).isEqualTo(originalEx.getExpected()); + assertThat(copyEx.getActual()).isEqualTo(originalEx.getActual()); + + } + + private static XmlAdapter[] createXmlAdapter() { + return new XmlAdapter[]{new EntityIdPathXmlAdapter(new JaxbTestEntityIdFactory())}; + } + +} diff --git a/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateVersionNotFoundExceptionDataTest.java b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateVersionNotFoundExceptionDataTest.java new file mode 100644 index 0000000..0b31f87 --- /dev/null +++ b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/AggregateVersionNotFoundExceptionDataTest.java @@ -0,0 +1,113 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.adapters.XmlAdapter; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.AggregateVersionNotFoundException; +import org.fuin.ddd4j.jaxbtest.JaxbTestEntityIdFactory; +import org.fuin.ddd4j.jaxbtest.VendorId; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; +import static org.fuin.utils4j.jaxb.JaxbUtils.marshal; +import static org.fuin.utils4j.jaxb.JaxbUtils.unmarshal; + +/** + * Test for the {@link AggregateVersionNotFoundExceptionData} class. + */ +class AggregateVersionNotFoundExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(AggregateVersionNotFoundExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final AggregateVersionNotFoundException originalEx = new AggregateVersionNotFoundException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102); + final AggregateVersionNotFoundExceptionData original = new AggregateVersionNotFoundExceptionData(originalEx); + + // TEST + final AggregateVersionNotFoundExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateVersionNotFoundException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getVersion()).isEqualTo(originalEx.getVersion()); + + } + + @Test + public final void testMarshalUnmarshal() { + + // PREPARE + final AggregateVersionNotFoundException originalEx = new AggregateVersionNotFoundException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102); + final AggregateVersionNotFoundExceptionData original = new AggregateVersionNotFoundExceptionData(originalEx); + + // TEST + final String xml = marshal(original, createXmlAdapter(), AggregateVersionNotFoundExceptionData.class); + final AggregateVersionNotFoundExceptionData copy = unmarshal(xml, createXmlAdapter(), AggregateVersionNotFoundExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateVersionNotFoundException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getVersion()).isEqualTo(originalEx.getVersion()); + + } + + @Test + public final void testUnmarshal() { + + // PREPARE + final AggregateVersionNotFoundException originalEx = new AggregateVersionNotFoundException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102); + final String xml = """ + + + Requested version 102 for Vendor (4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119) does not exist + DDD4J-AGGREGATE_VERSION_NOT_FOUND + Vendor + 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 + 102 + + """; + + // TEST + final AggregateVersionNotFoundExceptionData copy = unmarshal(xml, createXmlAdapter(), AggregateVersionNotFoundExceptionData.class); + + // VERIFY + final AggregateVersionNotFoundException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getVersion()).isEqualTo(originalEx.getVersion()); + + } + + private static XmlAdapter[] createXmlAdapter() { + return new XmlAdapter[]{new EntityIdPathXmlAdapter(new JaxbTestEntityIdFactory())}; + } + +} diff --git a/jaxb/src/test/java/org/fuin/ddd4j/jaxb/DecryptionFailedExceptionDataTest.java b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/DecryptionFailedExceptionDataTest.java new file mode 100644 index 0000000..3c3f3ff --- /dev/null +++ b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/DecryptionFailedExceptionDataTest.java @@ -0,0 +1,98 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.adapters.XmlAdapter; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.DecryptionFailedException; +import org.fuin.ddd4j.jaxbtest.JaxbTestEntityIdFactory; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; +import static org.fuin.utils4j.jaxb.JaxbUtils.marshal; +import static org.fuin.utils4j.jaxb.JaxbUtils.unmarshal; + +/** + * Test for the {@link DecryptionFailedExceptionData} class. + */ +class DecryptionFailedExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(DecryptionFailedExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final RuntimeException cause = new RuntimeException("Foo Bar"); + final DecryptionFailedException originalEx = new DecryptionFailedException(cause); + final DecryptionFailedExceptionData original = new DecryptionFailedExceptionData(originalEx); + + // TEST + final DecryptionFailedExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final DecryptionFailedException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testMarshalUnmarshal() { + + // PREPARE + final RuntimeException cause = new RuntimeException("Foo Bar"); + final DecryptionFailedException originalEx = new DecryptionFailedException(cause); + final DecryptionFailedExceptionData original = new DecryptionFailedExceptionData(originalEx); + + // TEST + final String xml = marshal(original, createXmlAdapter(), DecryptionFailedExceptionData.class); + final DecryptionFailedExceptionData copy = unmarshal(xml, createXmlAdapter(), DecryptionFailedExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final DecryptionFailedException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testUnmarshal() { + + // PREPARE + final RuntimeException cause = new RuntimeException("Foo Bar"); + final DecryptionFailedException originalEx = new DecryptionFailedException(cause); + final String xml = """ + + + Decryption failed: Foo Bar + DDD4J-DECRYPTION_FAILED + + """; + + // TEST + final DecryptionFailedExceptionData copy = unmarshal(xml, createXmlAdapter(), DecryptionFailedExceptionData.class); + + // VERIFY + final DecryptionFailedException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + private static XmlAdapter[] createXmlAdapter() { + return new XmlAdapter[]{new EntityIdPathXmlAdapter(new JaxbTestEntityIdFactory())}; + } + +} diff --git a/jaxb/src/test/java/org/fuin/ddd4j/jaxb/DuplicateEncryptionKeyIdExceptionDataTest.java b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/DuplicateEncryptionKeyIdExceptionDataTest.java new file mode 100644 index 0000000..179e2c5 --- /dev/null +++ b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/DuplicateEncryptionKeyIdExceptionDataTest.java @@ -0,0 +1,99 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.adapters.XmlAdapter; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.DuplicateEncryptionKeyIdException; +import org.fuin.ddd4j.jaxbtest.JaxbTestEntityIdFactory; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; +import static org.fuin.utils4j.jaxb.JaxbUtils.marshal; +import static org.fuin.utils4j.jaxb.JaxbUtils.unmarshal; + +/** + * Test for the {@link DuplicateEncryptionKeyIdExceptionData} class. + */ +class DuplicateEncryptionKeyIdExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(DuplicateEncryptionKeyIdExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final DuplicateEncryptionKeyIdException originalEx = new DuplicateEncryptionKeyIdException("xyz"); + final DuplicateEncryptionKeyIdExceptionData original = new DuplicateEncryptionKeyIdExceptionData(originalEx); + + // TEST + final DuplicateEncryptionKeyIdExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final DuplicateEncryptionKeyIdException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyId()).isEqualTo(originalEx.getKeyId()); + + } + + @Test + public final void testMarshalUnmarshal() { + + // PREPARE + final DuplicateEncryptionKeyIdException originalEx = new DuplicateEncryptionKeyIdException("xyz"); + final DuplicateEncryptionKeyIdExceptionData original = new DuplicateEncryptionKeyIdExceptionData(originalEx); + + // TEST + final String xml = marshal(original, createXmlAdapter(), DuplicateEncryptionKeyIdExceptionData.class); + final DuplicateEncryptionKeyIdExceptionData copy = unmarshal(xml, createXmlAdapter(), DuplicateEncryptionKeyIdExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final DuplicateEncryptionKeyIdException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyId()).isEqualTo(originalEx.getKeyId()); + + } + + @Test + public final void testUnmarshal() { + + // PREPARE + final DuplicateEncryptionKeyIdException originalEx = new DuplicateEncryptionKeyIdException("xyz"); + final String xml = """ + + + Duplicate keyId: xyz + DDD4J-DUPLICATE-ENCRYPTION_KEY_ID + xyz + + """; + + // TEST + final DuplicateEncryptionKeyIdExceptionData copy = unmarshal(xml, createXmlAdapter(), DuplicateEncryptionKeyIdExceptionData.class); + + // VERIFY + final DuplicateEncryptionKeyIdException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyId()).isEqualTo(originalEx.getKeyId()); + + } + + private static XmlAdapter[] createXmlAdapter() { + return new XmlAdapter[]{new EntityIdPathXmlAdapter(new JaxbTestEntityIdFactory())}; + } + +} diff --git a/jaxb/src/test/java/org/fuin/ddd4j/jaxb/DuplicateEntityExceptionDataTest.java b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/DuplicateEntityExceptionDataTest.java new file mode 100644 index 0000000..a3e1ca5 --- /dev/null +++ b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/DuplicateEntityExceptionDataTest.java @@ -0,0 +1,114 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.adapters.XmlAdapter; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.DuplicateEntityException; +import org.fuin.ddd4j.core.EntityIdPath; +import org.fuin.ddd4j.jaxbtest.JaxbTestEntityIdFactory; +import org.fuin.ddd4j.jaxbtest.PersonId; +import org.fuin.ddd4j.jaxbtest.VendorId; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; +import static org.fuin.utils4j.jaxb.JaxbUtils.marshal; +import static org.fuin.utils4j.jaxb.JaxbUtils.unmarshal; + +/** + * Test for the {@link DuplicateEntityExceptionData} class. + */ +class DuplicateEntityExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(DuplicateEntityExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final EntityIdPath parentPath = new EntityIdPath(new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final PersonId personId = new PersonId(1); + final DuplicateEntityException originalEx = new DuplicateEntityException(parentPath, personId); + final DuplicateEntityExceptionData original = new DuplicateEntityExceptionData(originalEx); + + // TEST + final DuplicateEntityExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final DuplicateEntityException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getParentIdPath()).isEqualTo(originalEx.getParentIdPath()); + assertThat(copyEx.getEntityId()).isEqualTo(originalEx.getEntityId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testMarshalUnmarshal() { + + // PREPARE + final EntityIdPath parentPath = new EntityIdPath(new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final PersonId personId = new PersonId(1); + final DuplicateEntityException originalEx = new DuplicateEntityException(parentPath, personId); + final DuplicateEntityExceptionData original = new DuplicateEntityExceptionData(originalEx); + + // TEST + final String xml = marshal(original, createXmlAdapter(), DuplicateEntityExceptionData.class); + final DuplicateEntityExceptionData copy = unmarshal(xml, createXmlAdapter(), DuplicateEntityExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final DuplicateEntityException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getParentIdPath()).isEqualTo(originalEx.getParentIdPath()); + assertThat(copyEx.getEntityId()).isEqualTo(originalEx.getEntityId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testUnmarshal() { + + // PREPARE + final EntityIdPath parentPath = new EntityIdPath(new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final PersonId personId = new PersonId(1); + final DuplicateEntityException originalEx = new DuplicateEntityException(parentPath, personId); + final String xml = """ + + + Person 1 already exists in Vendor 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 + DDD4J-DUPLICATE_ENTITY + Vendor 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 + Person 1 + + """; + + // TEST + final DuplicateEntityExceptionData copy = unmarshal(xml, createXmlAdapter(), DuplicateEntityExceptionData.class); + + // VERIFY + final DuplicateEntityException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getParentIdPath()).isEqualTo(originalEx.getParentIdPath()); + assertThat(copyEx.getEntityId()).isEqualTo(originalEx.getEntityId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + private static XmlAdapter[] createXmlAdapter() { + return new XmlAdapter[]{new EntityIdPathXmlAdapter(new JaxbTestEntityIdFactory())}; + } + +} diff --git a/jaxb/src/test/java/org/fuin/ddd4j/jaxb/EncryptionKeyIdUnknownExceptionDataTest.java b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/EncryptionKeyIdUnknownExceptionDataTest.java new file mode 100644 index 0000000..32778cd --- /dev/null +++ b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/EncryptionKeyIdUnknownExceptionDataTest.java @@ -0,0 +1,99 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.adapters.XmlAdapter; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.EncryptionKeyIdUnknownException; +import org.fuin.ddd4j.jaxbtest.JaxbTestEntityIdFactory; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; +import static org.fuin.utils4j.jaxb.JaxbUtils.marshal; +import static org.fuin.utils4j.jaxb.JaxbUtils.unmarshal; + +/** + * Test for the {@link EncryptionKeyIdUnknownExceptionData} class. + */ +class EncryptionKeyIdUnknownExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(EncryptionKeyIdUnknownExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final EncryptionKeyIdUnknownException originalEx = new EncryptionKeyIdUnknownException("xyz"); + final EncryptionKeyIdUnknownExceptionData original = new EncryptionKeyIdUnknownExceptionData(originalEx); + + // TEST + final EncryptionKeyIdUnknownExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final EncryptionKeyIdUnknownException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyId()).isEqualTo(originalEx.getKeyId()); + + } + + @Test + public final void testMarshalUnmarshal() { + + // PREPARE + final EncryptionKeyIdUnknownException originalEx = new EncryptionKeyIdUnknownException("xyz"); + final EncryptionKeyIdUnknownExceptionData original = new EncryptionKeyIdUnknownExceptionData(originalEx); + + // TEST + final String xml = marshal(original, createXmlAdapter(), EncryptionKeyIdUnknownExceptionData.class); + final EncryptionKeyIdUnknownExceptionData copy = unmarshal(xml, createXmlAdapter(), EncryptionKeyIdUnknownExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final EncryptionKeyIdUnknownException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyId()).isEqualTo(originalEx.getKeyId()); + + } + + @Test + public final void testUnmarshal() { + + // PREPARE + final EncryptionKeyIdUnknownException originalEx = new EncryptionKeyIdUnknownException("xyz"); + final String xml = """ + + + Unknown keyId: xyz + DDD4J-ENCRYPTION_KEY_ID_UNKNOWN + xyz + + """; + + // TEST + final EncryptionKeyIdUnknownExceptionData copy = unmarshal(xml, createXmlAdapter(), EncryptionKeyIdUnknownExceptionData.class); + + // VERIFY + final EncryptionKeyIdUnknownException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyId()).isEqualTo(originalEx.getKeyId()); + + } + + private static XmlAdapter[] createXmlAdapter() { + return new XmlAdapter[]{new EntityIdPathXmlAdapter(new JaxbTestEntityIdFactory())}; + } + +} diff --git a/jaxb/src/test/java/org/fuin/ddd4j/jaxb/EncryptionKeyVersionUnknownExceptionDataTest.java b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/EncryptionKeyVersionUnknownExceptionDataTest.java new file mode 100644 index 0000000..7948822 --- /dev/null +++ b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/EncryptionKeyVersionUnknownExceptionDataTest.java @@ -0,0 +1,99 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.adapters.XmlAdapter; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.EncryptionKeyVersionUnknownException; +import org.fuin.ddd4j.jaxbtest.JaxbTestEntityIdFactory; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; +import static org.fuin.utils4j.jaxb.JaxbUtils.marshal; +import static org.fuin.utils4j.jaxb.JaxbUtils.unmarshal; + +/** + * Test for the {@link EncryptionKeyVersionUnknownExceptionData} class. + */ +class EncryptionKeyVersionUnknownExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(EncryptionKeyVersionUnknownExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final EncryptionKeyVersionUnknownException originalEx = new EncryptionKeyVersionUnknownException("1"); + final EncryptionKeyVersionUnknownExceptionData original = new EncryptionKeyVersionUnknownExceptionData(originalEx); + + // TEST + final EncryptionKeyVersionUnknownExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final EncryptionKeyVersionUnknownException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyVersion()).isEqualTo(originalEx.getKeyVersion()); + + } + + @Test + public final void testMarshalUnmarshal() { + + // PREPARE + final EncryptionKeyVersionUnknownException originalEx = new EncryptionKeyVersionUnknownException("1"); + final EncryptionKeyVersionUnknownExceptionData original = new EncryptionKeyVersionUnknownExceptionData(originalEx); + + // TEST + final String xml = marshal(original, createXmlAdapter(), EncryptionKeyVersionUnknownExceptionData.class); + final EncryptionKeyVersionUnknownExceptionData copy = unmarshal(xml, createXmlAdapter(), EncryptionKeyVersionUnknownExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final EncryptionKeyVersionUnknownException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyVersion()).isEqualTo(originalEx.getKeyVersion()); + + } + + @Test + public final void testUnmarshal() { + + // PREPARE + final EncryptionKeyVersionUnknownException originalEx = new EncryptionKeyVersionUnknownException("1"); + final String xml = """ + + + Unknown keyVersion: 1 + DDD4J-ENCRYPTION_KEY_VERSION_UNKNOWN + 1 + + """; + + // TEST + final EncryptionKeyVersionUnknownExceptionData copy = unmarshal(xml, createXmlAdapter(), EncryptionKeyVersionUnknownExceptionData.class); + + // VERIFY + final EncryptionKeyVersionUnknownException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyVersion()).isEqualTo(originalEx.getKeyVersion()); + + } + + private static XmlAdapter[] createXmlAdapter() { + return new XmlAdapter[]{new EntityIdPathXmlAdapter(new JaxbTestEntityIdFactory())}; + } + +} diff --git a/jaxb/src/test/java/org/fuin/ddd4j/jaxb/EntityNotFoundExceptionDataTest.java b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/EntityNotFoundExceptionDataTest.java new file mode 100644 index 0000000..7ea917d --- /dev/null +++ b/jaxb/src/test/java/org/fuin/ddd4j/jaxb/EntityNotFoundExceptionDataTest.java @@ -0,0 +1,114 @@ +package org.fuin.ddd4j.jaxb; + +import jakarta.xml.bind.annotation.adapters.XmlAdapter; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.EntityIdPath; +import org.fuin.ddd4j.core.EntityNotFoundException; +import org.fuin.ddd4j.jaxbtest.JaxbTestEntityIdFactory; +import org.fuin.ddd4j.jaxbtest.PersonId; +import org.fuin.ddd4j.jaxbtest.VendorId; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; +import static org.fuin.utils4j.jaxb.JaxbUtils.marshal; +import static org.fuin.utils4j.jaxb.JaxbUtils.unmarshal; + +/** + * Test for the {@link EntityNotFoundExceptionData} class. + */ +class EntityNotFoundExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(EntityNotFoundExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final EntityIdPath parentPath = new EntityIdPath(new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final PersonId personId = new PersonId(1); + final EntityNotFoundException originalEx = new EntityNotFoundException(parentPath, personId); + final EntityNotFoundExceptionData original = new EntityNotFoundExceptionData(originalEx); + + // TEST + final EntityNotFoundExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final EntityNotFoundException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getParentIdPath()).isEqualTo(originalEx.getParentIdPath()); + assertThat(copyEx.getEntityId()).isEqualTo(originalEx.getEntityId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testMarshalUnmarshal() { + + // PREPARE + final EntityIdPath parentPath = new EntityIdPath(new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final PersonId personId = new PersonId(1); + final EntityNotFoundException originalEx = new EntityNotFoundException(parentPath, personId); + final EntityNotFoundExceptionData original = new EntityNotFoundExceptionData(originalEx); + + // TEST + final String xml = marshal(original, createXmlAdapter(), EntityNotFoundExceptionData.class); + final EntityNotFoundExceptionData copy = unmarshal(xml, createXmlAdapter(), EntityNotFoundExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final EntityNotFoundException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getParentIdPath()).isEqualTo(originalEx.getParentIdPath()); + assertThat(copyEx.getEntityId()).isEqualTo(originalEx.getEntityId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testUnmarshal() { + + // PREPARE + final EntityIdPath parentPath = new EntityIdPath(new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final PersonId personId = new PersonId(1); + final EntityNotFoundException originalEx = new EntityNotFoundException(parentPath, personId); + final String xml = """ + + + Person 1 not found in Vendor 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 + DDD4J-ENTITY_NOT_FOUND + Vendor 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 + Person 1 + + """; + + // TEST + final EntityNotFoundExceptionData copy = unmarshal(xml, createXmlAdapter(), EntityNotFoundExceptionData.class); + + // VERIFY + final EntityNotFoundException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getParentIdPath()).isEqualTo(originalEx.getParentIdPath()); + assertThat(copyEx.getEntityId()).isEqualTo(originalEx.getEntityId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + private static XmlAdapter[] createXmlAdapter() { + return new XmlAdapter[]{new EntityIdPathXmlAdapter(new JaxbTestEntityIdFactory())}; + } + +} diff --git a/jaxb/src/test/java/org/fuin/ddd4j/jaxbtest/VendorIdConverter.java b/jaxb/src/test/java/org/fuin/ddd4j/jaxbtest/VendorIdConverter.java index 38d5414..b5150f4 100644 --- a/jaxb/src/test/java/org/fuin/ddd4j/jaxbtest/VendorIdConverter.java +++ b/jaxb/src/test/java/org/fuin/ddd4j/jaxbtest/VendorIdConverter.java @@ -17,9 +17,10 @@ */ package org.fuin.ddd4j.jaxbtest; -import javax.annotation.concurrent.ThreadSafe; import org.fuin.objects4j.jaxb.ValueObjectStringXmlAdapter; +import javax.annotation.concurrent.ThreadSafe; + /** * Converts a vendor identifier into a string and back (JAXB). */ diff --git a/jaxb/src/test/java/org/fuin/ddd4j/jaxbtest/VendorKeyConverter.java b/jaxb/src/test/java/org/fuin/ddd4j/jaxbtest/VendorKeyConverter.java index 2a6d28a..8858df8 100644 --- a/jaxb/src/test/java/org/fuin/ddd4j/jaxbtest/VendorKeyConverter.java +++ b/jaxb/src/test/java/org/fuin/ddd4j/jaxbtest/VendorKeyConverter.java @@ -17,9 +17,10 @@ */ package org.fuin.ddd4j.jaxbtest; -import javax.annotation.concurrent.ThreadSafe; import org.fuin.objects4j.jaxb.ValueObjectStringXmlAdapter; +import javax.annotation.concurrent.ThreadSafe; + /** * Converts a vendor key into a string and back (JAXB and JPA). */ diff --git a/jaxb/src/test/java/org/fuin/ddd4j/jaxbtest/VendorNameConverter.java b/jaxb/src/test/java/org/fuin/ddd4j/jaxbtest/VendorNameConverter.java index 38e3acb..e216310 100644 --- a/jaxb/src/test/java/org/fuin/ddd4j/jaxbtest/VendorNameConverter.java +++ b/jaxb/src/test/java/org/fuin/ddd4j/jaxbtest/VendorNameConverter.java @@ -17,9 +17,10 @@ */ package org.fuin.ddd4j.jaxbtest; -import javax.annotation.concurrent.ThreadSafe; import org.fuin.objects4j.jaxb.ValueObjectStringXmlAdapter; +import javax.annotation.concurrent.ThreadSafe; + /** * Converts a vendor name into a string and back (JAXB). */ diff --git a/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/PersonIdJsonbAdapter.java b/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/PersonIdJsonbAdapter.java index 086aade..80aa251 100644 --- a/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/PersonIdJsonbAdapter.java +++ b/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/PersonIdJsonbAdapter.java @@ -1,6 +1,7 @@ package org.fuin.ddd4j.jsonbtestmodel; import jakarta.json.bind.adapter.JsonbAdapter; + import javax.annotation.concurrent.ThreadSafe; /** diff --git a/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/PersonNameJsonbAdapter.java b/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/PersonNameJsonbAdapter.java index a184a7c..01683da 100644 --- a/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/PersonNameJsonbAdapter.java +++ b/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/PersonNameJsonbAdapter.java @@ -17,9 +17,10 @@ */ package org.fuin.ddd4j.jsonbtestmodel; -import javax.annotation.concurrent.ThreadSafe; import org.fuin.objects4j.jsonb.ValueObjectStringJsonbAdapter; +import javax.annotation.concurrent.ThreadSafe; + /** * Converts a person name into a string and back (JSON-B). */ diff --git a/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/VendorIdJsonbAdapter.java b/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/VendorIdJsonbAdapter.java index baa04e1..04da530 100644 --- a/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/VendorIdJsonbAdapter.java +++ b/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/VendorIdJsonbAdapter.java @@ -17,9 +17,10 @@ */ package org.fuin.ddd4j.jsonbtestmodel; -import javax.annotation.concurrent.ThreadSafe; import org.fuin.objects4j.jsonb.ValueObjectStringJsonbAdapter; +import javax.annotation.concurrent.ThreadSafe; + /** * Converts a vendor identifier into a string and back (JSON-B). */ diff --git a/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/VendorKeyJsonbAdapter.java b/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/VendorKeyJsonbAdapter.java index b60f611..d75bf5a 100644 --- a/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/VendorKeyJsonbAdapter.java +++ b/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/VendorKeyJsonbAdapter.java @@ -17,9 +17,10 @@ */ package org.fuin.ddd4j.jsonbtestmodel; -import javax.annotation.concurrent.ThreadSafe; import org.fuin.objects4j.jsonb.ValueObjectStringJsonbAdapter; +import javax.annotation.concurrent.ThreadSafe; + /** * Converts a vendor key into a string and back (JSON-B). */ diff --git a/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/VendorNameJsonbAdapter.java b/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/VendorNameJsonbAdapter.java index 395f3e0..59e1f0e 100644 --- a/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/VendorNameJsonbAdapter.java +++ b/jsonb-testmodel/src/main/java/org/fuin/ddd4j/jsonbtestmodel/VendorNameJsonbAdapter.java @@ -17,9 +17,10 @@ */ package org.fuin.ddd4j.jsonbtestmodel; -import javax.annotation.concurrent.ThreadSafe; import org.fuin.objects4j.jsonb.ValueObjectStringJsonbAdapter; +import javax.annotation.concurrent.ThreadSafe; + /** * Converts a vendor name into a string and back (JSON-B). */ diff --git a/jsonb-testmodel/src/test/java/org/fuin/ddd4j/jsonbtestmodel/ArchitectureTest.java b/jsonb-testmodel/src/test/java/org/fuin/ddd4j/jsonbtestmodel/ArchitectureTest.java index 71c7bf8..f704fb0 100644 --- a/jsonb-testmodel/src/test/java/org/fuin/ddd4j/jsonbtestmodel/ArchitectureTest.java +++ b/jsonb-testmodel/src/test/java/org/fuin/ddd4j/jsonbtestmodel/ArchitectureTest.java @@ -1,14 +1,10 @@ package org.fuin.ddd4j.jsonbtestmodel; -import com.tngtech.archunit.core.domain.JavaClasses; import com.tngtech.archunit.core.domain.JavaModifier; -import com.tngtech.archunit.core.importer.ClassFileImporter; import com.tngtech.archunit.core.importer.ImportOption; import com.tngtech.archunit.junit.AnalyzeClasses; import com.tngtech.archunit.junit.ArchTest; import com.tngtech.archunit.lang.ArchRule; -import org.fuin.ddd4j.core.AggregateRootId; -import org.fuin.ddd4j.core.AggregateRootUuid; import org.fuin.ddd4j.core.DomainEvent; import org.fuin.ddd4j.core.EntityId; import org.fuin.ddd4j.core.EntityType; @@ -16,7 +12,6 @@ import org.fuin.esc.api.HasSerializedDataTypeConstant; import org.fuin.objects4j.common.HasPublicStaticIsValidMethod; import org.fuin.objects4j.common.HasPublicStaticValueOfMethod; -import org.junit.jupiter.api.Test; import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; import static com.tngtech.archunit.library.DependencyRules.NO_CLASSES_SHOULD_DEPEND_UPPER_PACKAGES; diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AbstractAggregateExceptionData.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AbstractAggregateExceptionData.java new file mode 100644 index 0000000..cb45721 --- /dev/null +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AbstractAggregateExceptionData.java @@ -0,0 +1,80 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.annotation.JsonbProperty; +import org.fuin.ddd4j.core.AbstractAggregateException; +import org.fuin.ddd4j.core.ExceptionData; +import org.fuin.objects4j.common.ToExceptionCapable; +import org.fuin.objects4j.common.ValueObject; + +import java.io.Serial; +import java.io.Serializable; +import java.util.Objects; + +/** + * Base class for storing the data from a {@link AbstractAggregateException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + * + * @param Concrete type of wrapped exception. + */ +public abstract class AbstractAggregateExceptionData implements ExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @JsonbProperty("msg") + private String message; + + @JsonbProperty("aggregate-type") + private String aggregateType; + + @JsonbProperty("aggregate-id") + private String aggregateId; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected AbstractAggregateExceptionData() { + super(); + } + + /** + * Constructor with exception. + * + * @param ex Exception to copy data from. + */ + public AbstractAggregateExceptionData(final AbstractAggregateException ex) { + super(); + Objects.requireNonNull(ex, "ex==null"); + this.message = ex.getMessage(); + this.aggregateType = ex.getType(); + this.aggregateId = ex.getId(); + } + + /** + * Returns the exception message. + * + * @return Message. + */ + public final String getMessage() { + return message; + } + + /** + * Returns the type of the aggregate. + * + * @return Aggregate type. + */ + public final String getAggregateType() { + return aggregateType; + } + + /** + * Returns the unique aggregate identifier. + * + * @return Aggregate ID. + */ + public final String getAggregateId() { + return aggregateId; + } + +} diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AbstractVersionedAggregateExceptionData.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AbstractVersionedAggregateExceptionData.java new file mode 100644 index 0000000..4785c91 --- /dev/null +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AbstractVersionedAggregateExceptionData.java @@ -0,0 +1,46 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.annotation.JsonbProperty; +import org.fuin.ddd4j.core.AbstractVersionedAggregateException; + +import java.io.Serial; + +/** + * Base class for storing the data from a {@link AbstractVersionedAggregateException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +public abstract class AbstractVersionedAggregateExceptionData extends AbstractAggregateExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @JsonbProperty("version") + private int version; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected AbstractVersionedAggregateExceptionData() { + super(); + } + + /** + * Constructor with exception. + * + * @param ex Exception to copy data from. + */ + public AbstractVersionedAggregateExceptionData(final AbstractVersionedAggregateException ex) { + super(ex); + this.version = ex.getVersion(); + } + + /** + * Returns the aggregate version that caused the problem. + * + * @return Aggregate version. + */ + public final int getVersion() { + return version; + } + +} diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateAlreadyExistsExceptionData.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateAlreadyExistsExceptionData.java new file mode 100644 index 0000000..80696bb --- /dev/null +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateAlreadyExistsExceptionData.java @@ -0,0 +1,110 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.annotation.JsonbProperty; +import org.fuin.ddd4j.core.AggregateAlreadyExistsException; + +import java.io.Serial; + +import static org.fuin.ddd4j.core.AggregateAlreadyExistsException.ELEMENT_NAME; + +/** + * Stores the data from a {@link AggregateAlreadyExistsException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +public final class AggregateAlreadyExistsExceptionData extends AbstractVersionedAggregateExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @JsonbProperty("sid") + private String sid; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected AggregateAlreadyExistsExceptionData() { + super(); + } + + /** + * Constructor with exception. + * + * @param ex Exception to copy data from. + */ + public AggregateAlreadyExistsExceptionData(final AggregateAlreadyExistsException ex) { + super(ex); + this.sid = ex.getShortId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the unique short identifier od the exception. + * + * @return Unique and human readable identifer. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getAggregateId() == null) ? 0 : getAggregateId().hashCode()); + result = prime * result + ((getAggregateType() == null) ? 0 : getAggregateType().hashCode()); + result = prime * result + ((getMessage() == null) ? 0 : getMessage().hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + result = prime * result + getVersion(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + AggregateAlreadyExistsExceptionData other = (AggregateAlreadyExistsExceptionData) obj; + if (getAggregateId() == null) { + if (other.getAggregateId() != null) + return false; + } else if (!getAggregateId().equals(other.getAggregateId())) + return false; + if (getAggregateType() == null) { + if (other.getAggregateType() != null) + return false; + } else if (!getAggregateType().equals(other.getAggregateType())) + return false; + if (getMessage() == null) { + if (other.getMessage() != null) + return false; + } else if (!getMessage().equals(other.getMessage())) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + if (getVersion() != other.getVersion()) + return false; + return true; + } + + @Override + public String toString() { + return "Data [message=" + getMessage() + ", sid=" + sid + ", aggregateType=" + getAggregateType() + + ", aggregateId=" + getAggregateId() + ", version=" + getVersion() + "]"; + } + + @Override + public AggregateAlreadyExistsException toException() { + return new AggregateAlreadyExistsException(getAggregateType(), getAggregateId(), getVersion()); + } + +} diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateDeletedExceptionData.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateDeletedExceptionData.java new file mode 100644 index 0000000..4fec52f --- /dev/null +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateDeletedExceptionData.java @@ -0,0 +1,125 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.AggregateDeletedException; + +import java.io.Serial; + +import static org.fuin.ddd4j.core.AggregateDeletedException.ELEMENT_NAME; + +/** + * Stores the data from a {@link AggregateDeletedException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +public final class AggregateDeletedExceptionData extends AbstractAggregateExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @JsonbProperty("sid") + private String sid; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected AggregateDeletedExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public AggregateDeletedExceptionData(@NotNull final AggregateDeletedException ex) { + super(ex); + this.sid = ex.getShortId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getAggregateId() == null) ? 0 : getAggregateId().hashCode()); + result = prime * result + ((getAggregateType() == null) ? 0 : getAggregateType().hashCode()); + result = prime * result + ((getMessage() == null) ? 0 : getMessage().hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + AggregateDeletedExceptionData other = (AggregateDeletedExceptionData) obj; + if (getAggregateId() == null) { + if (other.getAggregateId() != null) + return false; + } else if (!getAggregateId().equals(other.getAggregateId())) + return false; + if (getAggregateType() == null) { + if (other.getAggregateType() != null) + return false; + } else if (!getAggregateType().equals(other.getAggregateType())) + return false; + if (getMessage() == null) { + if (other.getMessage() != null) + return false; + } else if (!getMessage().equals(other.getMessage())) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + return true; + } + + @Override + public String toString() { + return "AggregateDeletedExceptionData [getMessage()=" + getMessage() + ", sid=" + sid + ", getAggregateType()=" + getAggregateType() + ", getAggregateId()=" + getAggregateId() + "]"; + } + + @Override + public AggregateDeletedException toException() { + return new AggregateDeletedException(getAggregateType(), getAggregateId()); + } + +} + diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateNotFoundExceptionData.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateNotFoundExceptionData.java new file mode 100644 index 0000000..92a992a --- /dev/null +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateNotFoundExceptionData.java @@ -0,0 +1,106 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.annotation.JsonbProperty; +import org.fuin.ddd4j.core.AggregateNotFoundException; + +import java.io.Serial; + +import static org.fuin.ddd4j.core.AggregateNotFoundException.ELEMENT_NAME; + +/** + * Stores the data from a {@link AggregateNotFoundException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +public final class AggregateNotFoundExceptionData extends AbstractAggregateExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @JsonbProperty("sid") + private String sid; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected AggregateNotFoundExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public AggregateNotFoundExceptionData(final AggregateNotFoundException ex) { + super(ex); + this.sid = ex.getShortId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getAggregateId() == null) ? 0 : getAggregateId().hashCode()); + result = prime * result + ((getAggregateType() == null) ? 0 : getAggregateType().hashCode()); + result = prime * result + ((getMessage() == null) ? 0 : getMessage().hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + AggregateNotFoundExceptionData other = (AggregateNotFoundExceptionData) obj; + if (getAggregateId() == null) { + if (other.getAggregateId() != null) + return false; + } else if (!getAggregateId().equals(other.getAggregateId())) + return false; + if (getAggregateType() == null) { + if (other.getAggregateType() != null) + return false; + } else if (!getAggregateType().equals(other.getAggregateType())) + return false; + if (getMessage() == null) { + if (other.getMessage() != null) + return false; + } else if (!getMessage().equals(other.getMessage())) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + return true; + } + + @Override + public String toString() { + return "AggregateNotFoundExceptionData [getMessage()=" + getMessage() + ", sid=" + sid + ", getAggregateType()=" + getAggregateType() + ", getAggregateId()=" + getAggregateId() + "]"; + } + + @Override + public AggregateNotFoundException toException() { + return new AggregateNotFoundException(getAggregateType(), getAggregateId()); + } + +} diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateVersionConflictExceptionData.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateVersionConflictExceptionData.java new file mode 100644 index 0000000..a65c640 --- /dev/null +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateVersionConflictExceptionData.java @@ -0,0 +1,122 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.AggregateVersionConflictException; + +import java.io.Serial; + +import static org.fuin.ddd4j.core.AggregateVersionConflictException.ELEMENT_NAME; + +/** + * Stores the data from a {@link AggregateVersionConflictException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +public final class AggregateVersionConflictExceptionData extends AbstractAggregateExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @JsonbProperty("sid") + private String sid; + + @JsonbProperty("expected-version") + private int expected; + + @JsonbProperty("actual-version") + private int actual; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected AggregateVersionConflictExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public AggregateVersionConflictExceptionData(@NotNull final AggregateVersionConflictException ex) { + super(ex); + this.sid = ex.getShortId(); + this.expected = ex.getExpected(); + this.actual = ex.getActual(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + actual; + result = prime * result + ((getAggregateId() == null) ? 0 : getAggregateId().hashCode()); + result = prime * result + ((getAggregateType() == null) ? 0 : getAggregateType().hashCode()); + result = prime * result + expected; + result = prime * result + ((getMessage() == null) ? 0 : getMessage().hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + AggregateVersionConflictExceptionData other = (AggregateVersionConflictExceptionData) obj; + if (actual != other.actual) + return false; + if (getAggregateId() == null) { + if (other.getAggregateId() != null) + return false; + } else if (!getAggregateId().equals(other.getAggregateId())) + return false; + if (getAggregateType() == null) { + if (other.getAggregateType() != null) + return false; + } else if (!getAggregateType().equals(other.getAggregateType())) + return false; + if (expected != other.expected) + return false; + if (getMessage() == null) { + if (other.getMessage() != null) + return false; + } else if (!getMessage().equals(other.getMessage())) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + return true; + } + + @Override + public String toString() { + return "AggregateVersionConflictExceptionData [getMessage()=" + getMessage() + ", sid=" + sid + ", getAggregateType()=" + getAggregateType() + ", getAggregateId()=" + getAggregateId() + + ", expected=" + expected + ", actual=" + actual + "]"; + } + + @Override + public AggregateVersionConflictException toException() { + return new AggregateVersionConflictException(getAggregateType(), getAggregateId(), expected, actual); + } + +} diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateVersionJsonbAdapter.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateVersionJsonbAdapter.java index 46801d9..f054d79 100644 --- a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateVersionJsonbAdapter.java +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateVersionJsonbAdapter.java @@ -19,6 +19,7 @@ import jakarta.json.bind.adapter.JsonbAdapter; import org.fuin.ddd4j.core.AggregateVersion; + import javax.annotation.concurrent.ThreadSafe; /** diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateVersionNotFoundExceptionData.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateVersionNotFoundExceptionData.java new file mode 100644 index 0000000..984402b --- /dev/null +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/AggregateVersionNotFoundExceptionData.java @@ -0,0 +1,111 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.AggregateVersionNotFoundException; + +import java.io.Serial; + +import static org.fuin.ddd4j.core.AggregateVersionNotFoundException.ELEMENT_NAME; + +/** + * Stores the data from a {@link AggregateVersionNotFoundException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +public final class AggregateVersionNotFoundExceptionData extends AbstractVersionedAggregateExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @JsonbProperty("sid") + private String sid; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected AggregateVersionNotFoundExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public AggregateVersionNotFoundExceptionData(@NotNull final AggregateVersionNotFoundException ex) { + super(ex); + this.sid = ex.getShortId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((getAggregateId() == null) ? 0 : getAggregateId().hashCode()); + result = prime * result + ((getAggregateType() == null) ? 0 : getAggregateType().hashCode()); + result = prime * result + ((getMessage() == null) ? 0 : getMessage().hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + result = prime * result + getVersion(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + AggregateVersionNotFoundExceptionData other = (AggregateVersionNotFoundExceptionData) obj; + if (getAggregateId() == null) { + if (other.getAggregateId() != null) + return false; + } else if (!getAggregateId().equals(other.getAggregateId())) + return false; + if (getAggregateType() == null) { + if (other.getAggregateType() != null) + return false; + } else if (!getAggregateType().equals(other.getAggregateType())) + return false; + if (getMessage() == null) { + if (other.getMessage() != null) + return false; + } else if (!getMessage().equals(other.getMessage())) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + if (getVersion() != other.getVersion()) + return false; + return true; + } + + @Override + public String toString() { + return "AggregateVersionNotFoundExceptionData [getMessage()=" + getMessage() + ", sid=" + sid + ", getAggregateType()=" + getAggregateType() + ", getAggregateId()=" + getAggregateId() + + ", getVersion()=" + getVersion() + "]"; + } + + @Override + public AggregateVersionNotFoundException toException() { + return new AggregateVersionNotFoundException(getAggregateType(), getAggregateId(), getVersion()); + } + +} diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/DecryptionFailedExceptionData.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/DecryptionFailedExceptionData.java new file mode 100644 index 0000000..6701d9e --- /dev/null +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/DecryptionFailedExceptionData.java @@ -0,0 +1,112 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.DecryptionFailedException; +import org.fuin.ddd4j.core.ExceptionData; +import org.fuin.objects4j.common.ToExceptionCapable; +import org.fuin.objects4j.common.ValueObject; + +import java.io.Serial; +import java.io.Serializable; + +import static org.fuin.ddd4j.core.DecryptionFailedException.ELEMENT_NAME; + +/** + * Stores the data from a {@link DecryptionFailedException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +public class DecryptionFailedExceptionData implements ExceptionData { + + @Serial + private static final long serialVersionUID = 1000L; + + @JsonbProperty("msg") + private String message; + + @JsonbProperty("sid") + private String sid; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected DecryptionFailedExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public DecryptionFailedExceptionData(@NotNull final DecryptionFailedException ex) { + super(); + this.message = ex.getMessage(); + this.sid = ex.getShortId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the exception message. + * + * @return Message. + */ + public final String getMessage() { + return message; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((message == null) ? 0 : message.hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DecryptionFailedExceptionData other = (DecryptionFailedExceptionData) obj; + if (message == null) { + if (other.message != null) + return false; + } else if (!message.equals(other.message)) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + return true; + } + + @Override + public String toString() { + return "DecryptionFailedExceptionData [message=" + message + ", sid=" + sid + "]"; + } + + @Override + public DecryptionFailedException toException() { + return new DecryptionFailedException(message); + } + +} diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/DuplicateEncryptionKeyIdExceptionData.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/DuplicateEncryptionKeyIdExceptionData.java new file mode 100644 index 0000000..179d2ae --- /dev/null +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/DuplicateEncryptionKeyIdExceptionData.java @@ -0,0 +1,120 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.DuplicateEncryptionKeyIdException; +import org.fuin.ddd4j.core.ExceptionData; +import org.fuin.objects4j.common.ToExceptionCapable; +import org.fuin.objects4j.common.ValueObject; + +import java.io.Serializable; + +import static org.fuin.ddd4j.core.DuplicateEncryptionKeyIdException.ELEMENT_NAME; + +/** + * Stores the data from a {@link DuplicateEncryptionKeyIdException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +public class DuplicateEncryptionKeyIdExceptionData implements ExceptionData { + + private static final long serialVersionUID = 1000L; + + @JsonbProperty("msg") + private String message; + + @JsonbProperty("sid") + private String sid; + + @JsonbProperty("key-id") + private String keyId; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected DuplicateEncryptionKeyIdExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public DuplicateEncryptionKeyIdExceptionData(@NotNull final DuplicateEncryptionKeyIdException ex) { + super(); + this.message = ex.getMessage(); + this.sid = ex.getShortId(); + this.keyId = ex.getKeyId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the exception message. + * + * @return Message. + */ + public String getMessage() { + return message; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((message == null) ? 0 : message.hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + result = prime * result + ((keyId == null) ? 0 : keyId.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DuplicateEncryptionKeyIdExceptionData other = (DuplicateEncryptionKeyIdExceptionData) obj; + if (message == null) { + if (other.message != null) + return false; + } else if (!message.equals(other.message)) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + if (keyId == null) { + if (other.keyId != null) + return false; + } else if (!keyId.equals(other.keyId)) + return false; + return true; + } + + @Override + public String toString() { + return "DuplicateEncryptionKeyIdExceptionData [message=" + message + ", sid=" + sid + ", keyId=" + keyId + "]"; + } + + @Override + public DuplicateEncryptionKeyIdException toException() { + return new DuplicateEncryptionKeyIdException(keyId); + } + +} \ No newline at end of file diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/DuplicateEntityExceptionData.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/DuplicateEntityExceptionData.java new file mode 100644 index 0000000..a41341f --- /dev/null +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/DuplicateEntityExceptionData.java @@ -0,0 +1,130 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.DuplicateEntityException; +import org.fuin.ddd4j.core.ExceptionData; +import org.fuin.objects4j.common.ToExceptionCapable; +import org.fuin.objects4j.common.ValueObject; + +import java.io.Serializable; + +import static org.fuin.ddd4j.core.DuplicateEntityException.ELEMENT_NAME; + +/** + * Stores the data from a {@link DuplicateEntityException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +public class DuplicateEntityExceptionData implements ExceptionData { + + private static final long serialVersionUID = 1000L; + + @JsonbProperty("msg") + private String message; + + @JsonbProperty("sid") + private String sid; + + @JsonbProperty("parent-id-path") + private String parentIdPath; + + @JsonbProperty("entity-id") + private String entityId; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected DuplicateEntityExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public DuplicateEntityExceptionData(@NotNull final DuplicateEntityException ex) { + super(); + this.message = ex.getMessage(); + this.sid = ex.getShortId(); + this.parentIdPath = ex.getParentIdPath(); + this.entityId = ex.getEntityId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the exception message. + * + * @return Message. + */ + public final String getMessage() { + return message; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((entityId == null) ? 0 : entityId.hashCode()); + result = prime * result + ((message == null) ? 0 : message.hashCode()); + result = prime * result + ((parentIdPath == null) ? 0 : parentIdPath.hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + DuplicateEntityExceptionData other = (DuplicateEntityExceptionData) obj; + if (entityId == null) { + if (other.entityId != null) + return false; + } else if (!entityId.equals(other.entityId)) + return false; + if (message == null) { + if (other.message != null) + return false; + } else if (!message.equals(other.message)) + return false; + if (parentIdPath == null) { + if (other.parentIdPath != null) + return false; + } else if (!parentIdPath.equals(other.parentIdPath)) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + return true; + } + + @Override + public String toString() { + return "DuplicateEntityExceptionData [message=" + message + ", sid=" + sid + ", parentIdPath=" + parentIdPath + ", entityId=" + entityId + "]"; + } + + @Override + public DuplicateEntityException toException() { + return new DuplicateEntityException(parentIdPath, entityId); + } + +} diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EncryptionKeyIdUnknownExceptionData.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EncryptionKeyIdUnknownExceptionData.java new file mode 100644 index 0000000..b97d060 --- /dev/null +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EncryptionKeyIdUnknownExceptionData.java @@ -0,0 +1,120 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.EncryptionKeyIdUnknownException; +import org.fuin.ddd4j.core.ExceptionData; +import org.fuin.objects4j.common.ToExceptionCapable; +import org.fuin.objects4j.common.ValueObject; + +import java.io.Serializable; + +import static org.fuin.ddd4j.core.EncryptionKeyIdUnknownException.ELEMENT_NAME; + +/** + * Stores the data from a {@link EncryptionKeyIdUnknownException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +public class EncryptionKeyIdUnknownExceptionData implements ExceptionData { + + private static final long serialVersionUID = 1000L; + + @JsonbProperty("msg") + private String message; + + @JsonbProperty("sid") + private String sid; + + @JsonbProperty("key-id") + private String keyId; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected EncryptionKeyIdUnknownExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public EncryptionKeyIdUnknownExceptionData(@NotNull final EncryptionKeyIdUnknownException ex) { + super(); + this.message = ex.getMessage(); + this.sid = ex.getShortId(); + this.keyId = ex.getKeyId(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the exception message. + * + * @return Message. + */ + public final String getMessage() { + return message; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((message == null) ? 0 : message.hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + result = prime * result + ((keyId == null) ? 0 : keyId.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + EncryptionKeyIdUnknownExceptionData other = (EncryptionKeyIdUnknownExceptionData) obj; + if (message == null) { + if (other.message != null) + return false; + } else if (!message.equals(other.message)) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + if (keyId == null) { + if (other.keyId != null) + return false; + } else if (!keyId.equals(other.keyId)) + return false; + return true; + } + + @Override + public String toString() { + return "EncryptionKeyIdUnknownExceptionData [message=" + message + ", sid=" + sid + ", keyId=" + keyId + "]"; + } + + @Override + public EncryptionKeyIdUnknownException toException() { + return new EncryptionKeyIdUnknownException(keyId); + } + +} diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EncryptionKeyVersionUnknownExceptionData.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EncryptionKeyVersionUnknownExceptionData.java new file mode 100644 index 0000000..89bd888 --- /dev/null +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EncryptionKeyVersionUnknownExceptionData.java @@ -0,0 +1,120 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.EncryptionKeyVersionUnknownException; +import org.fuin.ddd4j.core.ExceptionData; +import org.fuin.objects4j.common.ToExceptionCapable; +import org.fuin.objects4j.common.ValueObject; + +import java.io.Serializable; + +import static org.fuin.ddd4j.core.EncryptionKeyVersionUnknownException.ELEMENT_NAME; + +/** + * Stores the data from a {@link EncryptionKeyVersionUnknownException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +public class EncryptionKeyVersionUnknownExceptionData implements ExceptionData { + + private static final long serialVersionUID = 1000L; + + @JsonbProperty("msg") + private String message; + + @JsonbProperty("sid") + private String sid; + + @JsonbProperty("key-version") + private String keyVersion; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected EncryptionKeyVersionUnknownExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public EncryptionKeyVersionUnknownExceptionData(@NotNull final EncryptionKeyVersionUnknownException ex) { + super(); + this.message = ex.getMessage(); + this.sid = ex.getShortId(); + this.keyVersion = ex.getKeyVersion(); + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the exception message. + * + * @return Message. + */ + public final String getMessage() { + return message; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((message == null) ? 0 : message.hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + result = prime * result + ((keyVersion == null) ? 0 : keyVersion.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + EncryptionKeyVersionUnknownExceptionData other = (EncryptionKeyVersionUnknownExceptionData) obj; + if (message == null) { + if (other.message != null) + return false; + } else if (!message.equals(other.message)) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + if (keyVersion == null) { + if (other.keyVersion != null) + return false; + } else if (!keyVersion.equals(other.keyVersion)) + return false; + return true; + } + + @Override + public String toString() { + return "EncryptionKeyVersionUnknownExceptionData [message=" + message + ", sid=" + sid + ", keyVersion=" + keyVersion + "]"; + } + + @Override + public EncryptionKeyVersionUnknownException toException() { + return new EncryptionKeyVersionUnknownException(keyVersion); + } + +} diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EntityIdJsonbAdapter.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EntityIdJsonbAdapter.java index 86f0ccd..67aaa6e 100644 --- a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EntityIdJsonbAdapter.java +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EntityIdJsonbAdapter.java @@ -20,9 +20,10 @@ import jakarta.json.bind.adapter.JsonbAdapter; import org.fuin.ddd4j.core.EntityId; import org.fuin.ddd4j.core.EntityIdFactory; -import javax.annotation.concurrent.ThreadSafe; import org.fuin.objects4j.common.ValueOfCapable; +import javax.annotation.concurrent.ThreadSafe; + /** * JAXB and JSON converter for an entity identifier. */ diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EntityIdPathJsonbAdapter.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EntityIdPathJsonbAdapter.java index 5788c54..150974a 100644 --- a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EntityIdPathJsonbAdapter.java +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EntityIdPathJsonbAdapter.java @@ -19,9 +19,10 @@ import org.fuin.ddd4j.core.EntityIdFactory; import org.fuin.ddd4j.core.EntityIdPath; -import javax.annotation.concurrent.ThreadSafe; import org.fuin.objects4j.jsonb.ValueObjectStringJsonbAdapter; +import javax.annotation.concurrent.ThreadSafe; + /** * JAXB and JPA converter for an entity identifier path. */ diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EntityNotFoundExceptionData.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EntityNotFoundExceptionData.java new file mode 100644 index 0000000..b1c7b9a --- /dev/null +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EntityNotFoundExceptionData.java @@ -0,0 +1,130 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.EntityNotFoundException; +import org.fuin.ddd4j.core.ExceptionData; +import org.fuin.objects4j.common.ToExceptionCapable; +import org.fuin.objects4j.common.ValueObject; + +import java.io.Serializable; + +import static org.fuin.ddd4j.core.EntityNotFoundException.ELEMENT_NAME; + +/** + * Stores the data from a {@link EntityNotFoundException} for marshalling and allows recreating it after unmarshalling. + * The idea is to transport an exception from the server to the client (without stack trace) and recreate it to be thrown on the client. + */ +public class EntityNotFoundExceptionData implements ExceptionData { + + private static final long serialVersionUID = 1000L; + + @JsonbProperty("msg") + private String message; + + @JsonbProperty("sid") + private String sid; + + @JsonbProperty("parent-id-path") + private String parentIdPath; + + @JsonbProperty("entity-id") + private String entityId; + + /** + * Constructor only for marshalling/unmarshalling. + */ + protected EntityNotFoundExceptionData() { + super(); + } + + /** + * Constructor with all data. + * + * @param ex Exception to copy data from. + */ + public EntityNotFoundExceptionData(@NotNull final EntityNotFoundException ex) { + super(); + this.message = ex.getMessage(); + this.sid = ex.getShortId(); + this.parentIdPath = ex.getParentIdPath(); + this.entityId = ex.getEntityId(); + } + + /** + * Returns the exception message. + * + * @return Message. + */ + public final String getMessage() { + return message; + } + + @Override + public String getDataElement() { + return ELEMENT_NAME; + } + + /** + * Returns the unique short identifier of the contained exception. + * + * @return Unique and human readable identifier. + */ + public String getShortId() { + return sid; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((entityId == null) ? 0 : entityId.hashCode()); + result = prime * result + ((message == null) ? 0 : message.hashCode()); + result = prime * result + ((parentIdPath == null) ? 0 : parentIdPath.hashCode()); + result = prime * result + ((sid == null) ? 0 : sid.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + EntityNotFoundExceptionData other = (EntityNotFoundExceptionData) obj; + if (entityId == null) { + if (other.entityId != null) + return false; + } else if (!entityId.equals(other.entityId)) + return false; + if (message == null) { + if (other.message != null) + return false; + } else if (!message.equals(other.message)) + return false; + if (parentIdPath == null) { + if (other.parentIdPath != null) + return false; + } else if (!parentIdPath.equals(other.parentIdPath)) + return false; + if (sid == null) { + if (other.sid != null) + return false; + } else if (!sid.equals(other.sid)) + return false; + return true; + } + + @Override + public String toString() { + return "EntityNotFoundExceptionData [message=" + message + ", sid=" + sid + ", parentIdPath=" + parentIdPath + ", entityId=" + entityId + "]"; + } + + @Override + public EntityNotFoundException toException() { + return new EntityNotFoundException(parentIdPath, entityId); + } + +} diff --git a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EventIdJsonbAdapter.java b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EventIdJsonbAdapter.java index 6661763..67c5f99 100644 --- a/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EventIdJsonbAdapter.java +++ b/jsonb/src/main/java/org/fuin/ddd4j/jsonb/EventIdJsonbAdapter.java @@ -18,9 +18,10 @@ package org.fuin.ddd4j.jsonb; import org.fuin.ddd4j.core.EventId; -import javax.annotation.concurrent.ThreadSafe; import org.fuin.objects4j.jsonb.ValueObjectStringJsonbAdapter; +import javax.annotation.concurrent.ThreadSafe; + /** * JAXB and JPA converter for an entity identifier. */ diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateAlreadyExistsExceptionDataTest.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateAlreadyExistsExceptionDataTest.java new file mode 100644 index 0000000..09b2cfb --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateAlreadyExistsExceptionDataTest.java @@ -0,0 +1,108 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.Jsonb; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.AggregateAlreadyExistsException; +import org.fuin.ddd4j.jsonbtest.VendorId; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.ddd4j.jsonb.TestUtils.jsonb; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; + +/** + * Test for the {@link AggregateAlreadyExistsExceptionData} class. + */ +class AggregateAlreadyExistsExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(AggregateAlreadyExistsExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final AggregateAlreadyExistsException originalEx = new AggregateAlreadyExistsException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102); + final AggregateAlreadyExistsExceptionData original = new AggregateAlreadyExistsExceptionData(originalEx); + + // TEST + final AggregateAlreadyExistsExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + final AggregateAlreadyExistsException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getVersion()).isEqualTo(originalEx.getVersion()); + + } + + @Test + public final void testMarshalUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final AggregateAlreadyExistsException originalEx = new AggregateAlreadyExistsException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102); + final AggregateAlreadyExistsExceptionData original = new AggregateAlreadyExistsExceptionData(originalEx); + + // TEST + final String json = jsonb.toJson(original, AggregateAlreadyExistsExceptionData.class); + final AggregateAlreadyExistsExceptionData copy = jsonb.fromJson(json, AggregateAlreadyExistsExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + final AggregateAlreadyExistsException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getVersion()).isEqualTo(originalEx.getVersion()); + + } + + } + + @Test + public final void testUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final AggregateAlreadyExistsException originalEx = new AggregateAlreadyExistsException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102); + final String json = """ + { + "msg" : "Vendor 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 already exists (version=102)", + "sid" : "DDD4J-AGGREGATE_ALREADY_EXISTS", + "aggregate-type" : "Vendor", + "aggregate-id" : "4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119", + "version" : "102" + } + """; + + // TEST + final AggregateAlreadyExistsExceptionData copy = jsonb.fromJson(json, AggregateAlreadyExistsExceptionData.class); + + // VERIFY + final AggregateAlreadyExistsException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getVersion()).isEqualTo(originalEx.getVersion()); + + } + + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateDeletedExceptionDataTest.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateDeletedExceptionDataTest.java new file mode 100644 index 0000000..ee8ea81 --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateDeletedExceptionDataTest.java @@ -0,0 +1,110 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.Jsonb; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.AggregateDeletedException; +import org.fuin.ddd4j.jsonbtest.VendorId; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.ddd4j.jsonb.TestUtils.jsonb; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; + +/** + * Test for the {@link AggregateDeletedExceptionData} class. + */ +class AggregateDeletedExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(AggregateDeletedExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final AggregateDeletedException originalEx = new AggregateDeletedException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final AggregateDeletedExceptionData original = new AggregateDeletedExceptionData(originalEx); + + // TEST + final AggregateDeletedExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateDeletedException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testMarshalUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final AggregateDeletedException originalEx = new AggregateDeletedException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final AggregateDeletedExceptionData original = new AggregateDeletedExceptionData(originalEx); + + // TEST + final String json = jsonb.toJson(original, AggregateDeletedExceptionData.class); + final AggregateDeletedExceptionData copy = jsonb.fromJson(json, AggregateDeletedExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateDeletedException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + } + + @Test + public final void testUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final AggregateDeletedException originalEx = new AggregateDeletedException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final String json = """ + { + "msg" : "Vendor with id 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 already deleted", + "sid" : "DDD4J-AGGREGATE_DELETED", + "aggregate-type" : "Vendor", + "aggregate-id" : "4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119" + } + """; + + // TEST + final AggregateDeletedExceptionData copy = jsonb.fromJson(json, AggregateDeletedExceptionData.class); + + // VERIFY + final AggregateDeletedException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateNotFoundExceptionDataTest.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateNotFoundExceptionDataTest.java new file mode 100644 index 0000000..a484d5b --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateNotFoundExceptionDataTest.java @@ -0,0 +1,110 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.Jsonb; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.AggregateNotFoundException; +import org.fuin.ddd4j.jsonbtest.VendorId; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.ddd4j.jsonb.TestUtils.jsonb; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; + +/** + * Test for the {@link AggregateNotFoundExceptionData} class. + */ +class AggregateNotFoundExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(AggregateNotFoundExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final AggregateNotFoundException originalEx = new AggregateNotFoundException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final AggregateNotFoundExceptionData original = new AggregateNotFoundExceptionData(originalEx); + + // TEST + final AggregateNotFoundExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateNotFoundException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testMarshalUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final AggregateNotFoundException originalEx = new AggregateNotFoundException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final AggregateNotFoundExceptionData original = new AggregateNotFoundExceptionData(originalEx); + + // TEST + final String json = jsonb.toJson(original, AggregateNotFoundExceptionData.class); + final AggregateNotFoundExceptionData copy = jsonb.fromJson(json, AggregateNotFoundExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateNotFoundException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + } + + @Test + public final void testUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final AggregateNotFoundException originalEx = new AggregateNotFoundException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final String json = """ + { + "msg" : "Vendor with id 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119 not found", + "sid" : "DDD4J-AGGREGATE_NOT_FOUND", + "aggregate-type" : "Vendor", + "aggregate-id" : "4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119" + } + """; + + // TEST + final AggregateNotFoundExceptionData copy = jsonb.fromJson(json, AggregateNotFoundExceptionData.class); + + // VERIFY + final AggregateNotFoundException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateVersionConflictExceptionDataTest.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateVersionConflictExceptionDataTest.java new file mode 100644 index 0000000..6ca7b4c --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateVersionConflictExceptionDataTest.java @@ -0,0 +1,118 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.Jsonb; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.AggregateVersionConflictException; +import org.fuin.ddd4j.jsonbtest.VendorId; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.ddd4j.jsonb.TestUtils.jsonb; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; + +/** + * Test for the {@link AggregateVersionConflictExceptionData} class. + */ +class AggregateVersionConflictExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(AggregateVersionConflictExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final AggregateVersionConflictException originalEx = new AggregateVersionConflictException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102, 103); + final AggregateVersionConflictExceptionData original = new AggregateVersionConflictExceptionData(originalEx); + + // TEST + final AggregateVersionConflictExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateVersionConflictException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getExpected()).isEqualTo(originalEx.getExpected()); + assertThat(copyEx.getActual()).isEqualTo(originalEx.getActual()); + + } + + @Test + public final void testMarshalUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final AggregateVersionConflictException originalEx = new AggregateVersionConflictException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102, 103); + final AggregateVersionConflictExceptionData original = new AggregateVersionConflictExceptionData(originalEx); + + // TEST + final String json = jsonb.toJson(original, AggregateVersionConflictExceptionData.class); + final AggregateVersionConflictExceptionData copy = jsonb.fromJson(json, AggregateVersionConflictExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateVersionConflictException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getExpected()).isEqualTo(originalEx.getExpected()); + assertThat(copyEx.getActual()).isEqualTo(originalEx.getActual()); + + } + + } + + @Test + public final void testUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final AggregateVersionConflictException originalEx = new AggregateVersionConflictException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102, 103); + final String json = """ + { + "msg" : "Expected version 102 for Vendor (4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119), but was 103", + "sid" : "DDD4J-AGGREGATE_VERSION_CONFLICT", + "aggregate-type" : "Vendor", + "aggregate-id" : "4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119", + "expected-version" : "102", + "actual-version" : "103" + } + """; + + // TEST + final AggregateVersionConflictExceptionData copy = jsonb.fromJson(json, AggregateVersionConflictExceptionData.class); + + // VERIFY + final AggregateVersionConflictException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getExpected()).isEqualTo(originalEx.getExpected()); + assertThat(copyEx.getActual()).isEqualTo(originalEx.getActual()); + + } + + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateVersionNotFoundExceptionDataTest.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateVersionNotFoundExceptionDataTest.java new file mode 100644 index 0000000..db4f323 --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/AggregateVersionNotFoundExceptionDataTest.java @@ -0,0 +1,114 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.Jsonb; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.AggregateVersionNotFoundException; +import org.fuin.ddd4j.jsonbtest.VendorId; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.ddd4j.jsonb.TestUtils.jsonb; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; + +/** + * Test for the {@link AggregateVersionNotFoundExceptionData} class. + */ +class AggregateVersionNotFoundExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(AggregateVersionNotFoundExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final AggregateVersionNotFoundException originalEx = new AggregateVersionNotFoundException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102); + final AggregateVersionNotFoundExceptionData original = new AggregateVersionNotFoundExceptionData(originalEx); + + // TEST + final AggregateVersionNotFoundExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateVersionNotFoundException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getVersion()).isEqualTo(originalEx.getVersion()); + + } + + @Test + public final void testMarshalUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final AggregateVersionNotFoundException originalEx = new AggregateVersionNotFoundException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102); + final AggregateVersionNotFoundExceptionData original = new AggregateVersionNotFoundExceptionData(originalEx); + + // TEST + final String json = jsonb.toJson(original, AggregateVersionNotFoundExceptionData.class); + final AggregateVersionNotFoundExceptionData copy = jsonb.fromJson(json, AggregateVersionNotFoundExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final AggregateVersionNotFoundException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getVersion()).isEqualTo(originalEx.getVersion()); + + } + + } + + @Test + public final void testUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final AggregateVersionNotFoundException originalEx = new AggregateVersionNotFoundException(VendorId.TYPE, + new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119")), 102); + final String json = """ + { + "msg" : "Requested version 102 for Vendor (4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119) does not exist", + "sid" : "DDD4J-AGGREGATE_VERSION_NOT_FOUND", + "aggregate-type" : "Vendor", + "aggregate-id" : "4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119", + "version" : "102" + } + """; + + // TEST + final AggregateVersionNotFoundExceptionData copy = jsonb.fromJson(json, AggregateVersionNotFoundExceptionData.class); + + // VERIFY + final AggregateVersionNotFoundException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getType()).isEqualTo(originalEx.getType()); + assertThat(copyEx.getId()).isEqualTo(originalEx.getId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getVersion()).isEqualTo(originalEx.getVersion()); + + } + + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonb/DecryptionFailedExceptionDataTest.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/DecryptionFailedExceptionDataTest.java new file mode 100644 index 0000000..5e7c7b4 --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/DecryptionFailedExceptionDataTest.java @@ -0,0 +1,99 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.Jsonb; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.DecryptionFailedException; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.ddd4j.jsonb.TestUtils.jsonb; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; + +/** + * Test for the {@link DecryptionFailedExceptionData} class. + */ +class DecryptionFailedExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(DecryptionFailedExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() throws Exception { + + // PREPARE + final RuntimeException cause = new RuntimeException("Foo Bar"); + final DecryptionFailedException originalEx = new DecryptionFailedException(cause); + final DecryptionFailedExceptionData original = new DecryptionFailedExceptionData(originalEx); + + // TEST + final DecryptionFailedExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final DecryptionFailedException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testMarshalUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final RuntimeException cause = new RuntimeException("Foo Bar"); + final DecryptionFailedException originalEx = new DecryptionFailedException(cause); + final DecryptionFailedExceptionData original = new DecryptionFailedExceptionData(originalEx); + + // TEST + final String json = jsonb.toJson(original, DecryptionFailedExceptionData.class); + final DecryptionFailedExceptionData copy = jsonb.fromJson(json, DecryptionFailedExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final DecryptionFailedException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + } + + @Test + public final void testUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final RuntimeException cause = new RuntimeException("Foo Bar"); + final DecryptionFailedException originalEx = new DecryptionFailedException(cause); + final String json = """ + { + "msg" : "Decryption failed: Foo Bar", + "sid" : "DDD4J-DECRYPTION_FAILED" + } + """; + + // TEST + final DecryptionFailedExceptionData copy = jsonb.fromJson(json, DecryptionFailedExceptionData.class); + + // VERIFY + final DecryptionFailedException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonb/DuplicateEncryptionKeyIdExceptionDataTest.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/DuplicateEncryptionKeyIdExceptionDataTest.java new file mode 100644 index 0000000..0feba1c --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/DuplicateEncryptionKeyIdExceptionDataTest.java @@ -0,0 +1,100 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.Jsonb; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.DuplicateEncryptionKeyIdException; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.ddd4j.jsonb.TestUtils.jsonb; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; + +/** + * Test for the {@link DuplicateEncryptionKeyIdExceptionData} class. + */ +class DuplicateEncryptionKeyIdExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(DuplicateEncryptionKeyIdExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final DuplicateEncryptionKeyIdException originalEx = new DuplicateEncryptionKeyIdException("xyz"); + final DuplicateEncryptionKeyIdExceptionData original = new DuplicateEncryptionKeyIdExceptionData(originalEx); + + // TEST + final DuplicateEncryptionKeyIdExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final DuplicateEncryptionKeyIdException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyId()).isEqualTo(originalEx.getKeyId()); + + } + + @Test + public final void testMarshalUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final DuplicateEncryptionKeyIdException originalEx = new DuplicateEncryptionKeyIdException("xyz"); + final DuplicateEncryptionKeyIdExceptionData original = new DuplicateEncryptionKeyIdExceptionData(originalEx); + + // TEST + final String json = jsonb.toJson(original, DuplicateEncryptionKeyIdExceptionData.class); + final DuplicateEncryptionKeyIdExceptionData copy = jsonb.fromJson(json, DuplicateEncryptionKeyIdExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final DuplicateEncryptionKeyIdException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyId()).isEqualTo(originalEx.getKeyId()); + + } + + } + + @Test + public final void testUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final DuplicateEncryptionKeyIdException originalEx = new DuplicateEncryptionKeyIdException("xyz"); + final String json = """ + { + "msg" : "Duplicate keyId: xyz", + "sid" : "DDD4J-DUPLICATE-ENCRYPTION_KEY_ID", + "key-id" : "xyz" + } + """; + + // TEST + final DuplicateEncryptionKeyIdExceptionData copy = jsonb.fromJson(json, DuplicateEncryptionKeyIdExceptionData.class); + + // VERIFY + final DuplicateEncryptionKeyIdException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyId()).isEqualTo(originalEx.getKeyId()); + + } + + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonb/DuplicateEntityExceptionDataTest.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/DuplicateEntityExceptionDataTest.java new file mode 100644 index 0000000..87b4a90 --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/DuplicateEntityExceptionDataTest.java @@ -0,0 +1,115 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.Jsonb; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.DuplicateEntityException; +import org.fuin.ddd4j.core.EntityIdPath; +import org.fuin.ddd4j.jsonbtest.PersonId; +import org.fuin.ddd4j.jsonbtest.VendorId; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.ddd4j.jsonb.TestUtils.jsonb; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; + +/** + * Test for the {@link DuplicateEntityExceptionData} class. + */ +class DuplicateEntityExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(DuplicateEntityExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final EntityIdPath parentPath = new EntityIdPath(new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final PersonId personId = new PersonId(1); + final DuplicateEntityException originalEx = new DuplicateEntityException(parentPath, personId); + final DuplicateEntityExceptionData original = new DuplicateEntityExceptionData(originalEx); + + // TEST + final DuplicateEntityExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final DuplicateEntityException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getParentIdPath()).isEqualTo(originalEx.getParentIdPath()); + assertThat(copyEx.getEntityId()).isEqualTo(originalEx.getEntityId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testMarshalUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final EntityIdPath parentPath = new EntityIdPath(new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final PersonId personId = new PersonId(1); + final DuplicateEntityException originalEx = new DuplicateEntityException(parentPath, personId); + final DuplicateEntityExceptionData original = new DuplicateEntityExceptionData(originalEx); + + // TEST + final String json = jsonb.toJson(original, DuplicateEntityExceptionData.class); + final DuplicateEntityExceptionData copy = jsonb.fromJson(json, DuplicateEntityExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final DuplicateEntityException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getParentIdPath()).isEqualTo(originalEx.getParentIdPath()); + assertThat(copyEx.getEntityId()).isEqualTo(originalEx.getEntityId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + } + + @Test + public final void testUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final EntityIdPath parentPath = new EntityIdPath(new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final PersonId personId = new PersonId(1); + final DuplicateEntityException originalEx = new DuplicateEntityException(parentPath, personId); + final String json = """ + { + "msg" : "Person 1 already exists in Vendor 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119", + "sid" : "DDD4J-DUPLICATE_ENTITY", + "parent-id-path" : "Vendor 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119", + "entity-id" : "Person 1" + } + """; + + // TEST + final DuplicateEntityExceptionData copy = jsonb.fromJson(json, DuplicateEntityExceptionData.class); + + // VERIFY + final DuplicateEntityException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getParentIdPath()).isEqualTo(originalEx.getParentIdPath()); + assertThat(copyEx.getEntityId()).isEqualTo(originalEx.getEntityId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonb/EncryptionKeyIdUnknownExceptionDataTest.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/EncryptionKeyIdUnknownExceptionDataTest.java new file mode 100644 index 0000000..9dff8db --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/EncryptionKeyIdUnknownExceptionDataTest.java @@ -0,0 +1,100 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.Jsonb; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.EncryptionKeyIdUnknownException; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.ddd4j.jsonb.TestUtils.jsonb; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; + +/** + * Test for the {@link EncryptionKeyIdUnknownExceptionData} class. + */ +class EncryptionKeyIdUnknownExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(EncryptionKeyIdUnknownExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final EncryptionKeyIdUnknownException originalEx = new EncryptionKeyIdUnknownException("xyz"); + final EncryptionKeyIdUnknownExceptionData original = new EncryptionKeyIdUnknownExceptionData(originalEx); + + // TEST + final EncryptionKeyIdUnknownExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final EncryptionKeyIdUnknownException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyId()).isEqualTo(originalEx.getKeyId()); + + } + + @Test + public final void testMarshalUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final EncryptionKeyIdUnknownException originalEx = new EncryptionKeyIdUnknownException("xyz"); + final EncryptionKeyIdUnknownExceptionData original = new EncryptionKeyIdUnknownExceptionData(originalEx); + + // TEST + final String json = jsonb.toJson(original, EncryptionKeyIdUnknownExceptionData.class); + final EncryptionKeyIdUnknownExceptionData copy = jsonb.fromJson(json, EncryptionKeyIdUnknownExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final EncryptionKeyIdUnknownException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyId()).isEqualTo(originalEx.getKeyId()); + + } + + } + + @Test + public final void testUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final EncryptionKeyIdUnknownException originalEx = new EncryptionKeyIdUnknownException("xyz"); + final String json = """ + { + "msg" : "Unknown keyId: xyz", + "sid" : "DDD4J-ENCRYPTION_KEY_ID_UNKNOWN", + "key-id" : "xyz" + } + """; + + // TEST + final EncryptionKeyIdUnknownExceptionData copy = jsonb.fromJson(json, EncryptionKeyIdUnknownExceptionData.class); + + // VERIFY + final EncryptionKeyIdUnknownException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyId()).isEqualTo(originalEx.getKeyId()); + + } + + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonb/EncryptionKeyVersionUnknownExceptionDataTest.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/EncryptionKeyVersionUnknownExceptionDataTest.java new file mode 100644 index 0000000..263bf7e --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/EncryptionKeyVersionUnknownExceptionDataTest.java @@ -0,0 +1,100 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.Jsonb; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.EncryptionKeyVersionUnknownException; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.ddd4j.jsonb.TestUtils.jsonb; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; + +/** + * Test for the {@link EncryptionKeyVersionUnknownExceptionData} class. + */ +class EncryptionKeyVersionUnknownExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(EncryptionKeyVersionUnknownExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final EncryptionKeyVersionUnknownException originalEx = new EncryptionKeyVersionUnknownException("1"); + final EncryptionKeyVersionUnknownExceptionData original = new EncryptionKeyVersionUnknownExceptionData(originalEx); + + // TEST + final EncryptionKeyVersionUnknownExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final EncryptionKeyVersionUnknownException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyVersion()).isEqualTo(originalEx.getKeyVersion()); + + } + + @Test + public final void testMarshalUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final EncryptionKeyVersionUnknownException originalEx = new EncryptionKeyVersionUnknownException("1"); + final EncryptionKeyVersionUnknownExceptionData original = new EncryptionKeyVersionUnknownExceptionData(originalEx); + + // TEST + final String json = jsonb.toJson(original, EncryptionKeyVersionUnknownExceptionData.class); + final EncryptionKeyVersionUnknownExceptionData copy = jsonb.fromJson(json, EncryptionKeyVersionUnknownExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final EncryptionKeyVersionUnknownException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyVersion()).isEqualTo(originalEx.getKeyVersion()); + + } + + } + + @Test + public final void testUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final EncryptionKeyVersionUnknownException originalEx = new EncryptionKeyVersionUnknownException("1"); + final String json = """ + { + "msg" : "Unknown keyVersion: 1", + "sid" : "DDD4J-ENCRYPTION_KEY_VERSION_UNKNOWN", + "key-version" : "1" + } + """; + + // TEST + final EncryptionKeyVersionUnknownExceptionData copy = jsonb.fromJson(json, EncryptionKeyVersionUnknownExceptionData.class); + + // VERIFY + final EncryptionKeyVersionUnknownException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copyEx.getKeyVersion()).isEqualTo(originalEx.getKeyVersion()); + + } + + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonb/EntityNotFoundExceptionDataTest.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/EntityNotFoundExceptionDataTest.java new file mode 100644 index 0000000..d8d3d9e --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonb/EntityNotFoundExceptionDataTest.java @@ -0,0 +1,115 @@ +package org.fuin.ddd4j.jsonb; + +import jakarta.json.bind.Jsonb; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.fuin.ddd4j.core.EntityIdPath; +import org.fuin.ddd4j.core.EntityNotFoundException; +import org.fuin.ddd4j.jsonbtest.PersonId; +import org.fuin.ddd4j.jsonbtest.VendorId; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.ddd4j.jsonb.TestUtils.jsonb; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; + +/** + * Test for the {@link EntityNotFoundExceptionData} class. + */ +class EntityNotFoundExceptionDataTest { + + @Test + final void testEqualsHashCode() { + EqualsVerifier.simple().forClass(EntityNotFoundExceptionData.class).verify(); + } + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final EntityIdPath parentPath = new EntityIdPath(new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final PersonId personId = new PersonId(1); + final EntityNotFoundException originalEx = new EntityNotFoundException(parentPath, personId); + final EntityNotFoundExceptionData original = new EntityNotFoundExceptionData(originalEx); + + // TEST + final EntityNotFoundExceptionData copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final EntityNotFoundException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getParentIdPath()).isEqualTo(originalEx.getParentIdPath()); + assertThat(copyEx.getEntityId()).isEqualTo(originalEx.getEntityId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + @Test + public final void testMarshalUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final EntityIdPath parentPath = new EntityIdPath(new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final PersonId personId = new PersonId(1); + final EntityNotFoundException originalEx = new EntityNotFoundException(parentPath, personId); + final EntityNotFoundExceptionData original = new EntityNotFoundExceptionData(originalEx); + + // TEST + final String json = jsonb.toJson(original, EntityNotFoundExceptionData.class); + final EntityNotFoundExceptionData copy = jsonb.fromJson(json, EntityNotFoundExceptionData.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + final EntityNotFoundException copyEx = copy.toException(); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getParentIdPath()).isEqualTo(originalEx.getParentIdPath()); + assertThat(copyEx.getEntityId()).isEqualTo(originalEx.getEntityId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + } + + @Test + public final void testUnmarshal() throws Exception { + + try (final Jsonb jsonb = jsonb()) { + + // PREPARE + final EntityIdPath parentPath = new EntityIdPath(new VendorId(UUID.fromString("4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119"))); + final PersonId personId = new PersonId(1); + final EntityNotFoundException originalEx = new EntityNotFoundException(parentPath, personId); + final String json = """ + { + "msg" : "Person 1 not found in Vendor 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119", + "sid" : "DDD4J-ENTITY_NOT_FOUND", + "parent-id-path" : "Vendor 4dcf4c2c-10e1-4db9-ba9e-d1e644e9d119", + "entity-id" : "Person 1" + } + """; + + // TEST + final EntityNotFoundExceptionData copy = jsonb.fromJson(json, EntityNotFoundExceptionData.class); + + // VERIFY + final EntityNotFoundException copyEx = copy.toException(); + assertThat(copy.getMessage()).isEqualTo(originalEx.getMessage()); + assertThat(copy.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getShortId()).isEqualTo(originalEx.getShortId()); + assertThat(copyEx.getParentIdPath()).isEqualTo(originalEx.getParentIdPath()); + assertThat(copyEx.getEntityId()).isEqualTo(originalEx.getEntityId()); + assertThat(copyEx.getMessage()).isEqualTo(originalEx.getMessage()); + + } + + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/DuplicateVendorKeyException.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/DuplicateVendorKeyException.java new file mode 100644 index 0000000..2761a9e --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/DuplicateVendorKeyException.java @@ -0,0 +1,52 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import java.io.Serial; + +/** + * It was tried to add a vendor key that already exists. + */ +public final class DuplicateVendorKeyException extends Exception { + + @Serial + private static final long serialVersionUID = 1L; + + private final VendorKey key; + + /** + * Constructor with key. + * + * @param key + * Key. + */ + public DuplicateVendorKeyException(final VendorKey key) { + super("The vendor key already exists: " + key); + this.key = key; + } + + /** + * The key that caused the problem. + * + * @return Duplicate key. + */ + public final VendorKey getKey() { + return key; + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/Person.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/Person.java new file mode 100644 index 0000000..bc9d05a --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/Person.java @@ -0,0 +1,94 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.AbstractEntity; +import org.fuin.ddd4j.core.ApplyEvent; +import org.fuin.ddd4j.core.EntityType; +import org.fuin.objects4j.common.Contract; + +/** + * Person entity. + */ +public class Person extends AbstractEntity { + + private PersonId id; + + private PersonName name; + + /** + * Constructor with all data. + * + * @param vendor + * Aggregate root. + * @param id + * Unique identifier. + * @param name + * Name. + */ + public Person(@NotNull final Vendor vendor, final PersonId id, @NotNull final PersonName name) { + super(vendor); + + // CHECK PRECONDITIONS + Contract.requireArgNotNull("vendor", vendor); + Contract.requireArgNotNull("id", id); + Contract.requireArgNotNull("name", name); + + // NO EVENT HERE! THE EVENT FOR CONSTRUCTION IS + // ALWAYS FIRED BY THE PARENT ENTITY + this.id = id; + this.name = name; + + } + + /** + * Changes the name. + * + * @param newName + * New name. + */ + public final void changeName(@NotNull final PersonName newName) { + + // CHECK PRECONDITIONS + Contract.requireArgNotNull("newName", newName); + + // VERIFY BUSINESS RULES + // Nothing to do + + // HANDLE EVENT + apply(new PersonNameChangedEvent(getRoot().getRef(), id, name, newName)); + + } + + @ApplyEvent + private void applyEvent(final PersonNameChangedEvent event) { + this.name = event.getNewName(); + } + + @Override + public final EntityType getType() { + return PersonId.TYPE; + } + + @Override + public final PersonId getId() { + return id; + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonCreatedEvent.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonCreatedEvent.java new file mode 100644 index 0000000..f4b7f73 --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonCreatedEvent.java @@ -0,0 +1,109 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.EntityIdPath; +import org.fuin.ddd4j.core.EventType; +import org.fuin.ddd4j.jsonb.AbstractDomainEvent; +import org.fuin.esc.api.HasSerializedDataTypeConstant; +import org.fuin.esc.api.SerializedDataType; + +import java.io.Serial; + +/** + * A person entity was created. + */ +@HasSerializedDataTypeConstant +public final class PersonCreatedEvent extends AbstractDomainEvent { + + @Serial + private static final long serialVersionUID = 1000L; + + /** + * Unique name of the event used to store it - Should never change. + */ + public static final EventType TYPE = new EventType(PersonCreatedEvent.class.getSimpleName()); + + /** + * Unique name of the serialized event. + */ + public static final SerializedDataType SER_TYPE = new SerializedDataType(TYPE.asBaseType()); + + @JsonbProperty("vendor-ref") + private VendorRef vendorRef; + + @JsonbProperty("person-id") + private PersonId personId; + + @JsonbProperty("person-name") + private PersonName personName; + + /** + * Default constructor only for deserialization. + */ + protected PersonCreatedEvent() { + super(); + } + + /** + * Constructor with event data. + * + * @param vendorRef Aggregate root reference. + * @param personId Person ID. + * @param personName Person name. + */ + public PersonCreatedEvent(@NotNull final VendorRef vendorRef, @NotNull final PersonId personId, @NotNull final PersonName personName) { + super(new EntityIdPath(vendorRef.getId())); + this.vendorRef = vendorRef; + this.personId = personId; + this.personName = personName; + } + + @Override + public final EventType getEventType() { + return TYPE; + } + + /** + * Returns the person ID. + * + * @return Person ID. + */ + @NotNull + public final PersonId getPersonId() { + return personId; + } + + /** + * Returns the person name. + * + * @return Person name. + */ + @NotNull + public final PersonName getPersonName() { + return personName; + } + + @Override + public final String toString() { + return "Created person #" + personId + " with name '" + personName + "' for " + vendorRef; + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonId.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonId.java new file mode 100644 index 0000000..58357dc --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonId.java @@ -0,0 +1,145 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.EntityId; +import org.fuin.ddd4j.core.EntityType; +import org.fuin.ddd4j.core.HasEntityTypeConstant; +import org.fuin.ddd4j.core.StringBasedEntityType; +import org.fuin.objects4j.common.Contract; +import org.fuin.objects4j.common.HasPublicStaticIsValidMethod; +import org.fuin.objects4j.common.HasPublicStaticValueOfMethod; +import org.fuin.objects4j.core.AbstractIntegerValueObject; + +import javax.annotation.concurrent.Immutable; +import java.io.Serial; + +/** + * Unique identifier of a person. + */ +@Immutable +@HasPublicStaticIsValidMethod +@HasPublicStaticValueOfMethod +@HasEntityTypeConstant +public final class PersonId extends AbstractIntegerValueObject implements EntityId { + + @Serial + private static final long serialVersionUID = 1000L; + + /** Type of entity this identifier represents. */ + public static final EntityType TYPE = new StringBasedEntityType("Person"); + + private Integer value; + + /** + * Default constructor. + */ + public PersonId() { + super(); + } + + /** + * Constructor with ID. + * + * @param id + * ID. + */ + public PersonId(@NotNull final Integer id) { + super(); + Contract.requireArgNotNull("id", id); + this.value = id; + } + + @Override + public final EntityType getType() { + return TYPE; + } + + @Override + public final String asTypedString() { + return getType() + " " + asString(); + } + + @Override + public final Integer asBaseType() { + return value; + } + + @Override + public final String asString() { + return "" + value; + } + + @Override + public final String toString() { + return asString(); + } + + /** + * Returns the information if a given string is a valid person identifier. + * + * @param value + * Value to check. A null value returns true. + * + * @return TRUE if it's a valid ID, else FALSE. + */ + public static boolean isValid(final String value) { + if (value == null) { + return true; + } + try { + // TODO Use regular expression - Exception is no good style + Integer.parseInt(value); + return true; + } catch (final RuntimeException ex) { + return false; + } + } + + /** + * Parses a person identifier. + * + * @param value + * Value to convert. A null value returns null. + * + * @return Converted value. + */ + public static PersonId valueOf(final Integer value) { + if (value == null) { + return null; + } + return new PersonId(value); + } + + /** + * Parses a person identifier. + * + * @param value + * Value to convert. A null value returns null. + * + * @return Converted value. + */ + public static PersonId valueOf(final String value) { + if (value == null) { + return null; + } + return new PersonId(Integer.valueOf(value)); + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonName.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonName.java new file mode 100644 index 0000000..ba8e02a --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonName.java @@ -0,0 +1,97 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.validation.constraints.NotNull; +import org.fuin.objects4j.common.Contract; +import org.fuin.objects4j.common.ValueObjectWithBaseType; +import org.fuin.objects4j.core.AbstractStringValueObject; + +import javax.annotation.concurrent.Immutable; +import java.io.Serial; + +/** + * A person name. + */ +@Immutable +public final class PersonName extends AbstractStringValueObject implements ValueObjectWithBaseType { + + @Serial + private static final long serialVersionUID = 1000L; + + @NotNull + private final String name; + + /** + * Constructor with string. + * + * @param name + * Name. + */ + public PersonName(@NotNull final String name) { + super(); + Contract.requireArgNotNull("name", name); + Contract.requireArgMinLength("name", name, 1); + Contract.requireArgMaxLength("name", name, 100); + this.name = name; + } + + @Override + public final String asBaseType() { + return name; + } + + @Override + public final String toString() { + return name; + } + + /** + * Returns the information if a given string is a valid name. + * + * @param value + * Value to check. A null value returns true. + * + * @return TRUE if it's a valid key, else FALSE. + */ + public static boolean isValid(final String value) { + if (value == null) { + return true; + } + if (value.isEmpty() || value.length() > 100) { + return false; + } + return true; + } + + /** + * Parses a name. + * + * @param value + * Value to convert. A null value returns null. + * + * @return Converted value. + */ + public static PersonName valueOf(final String value) { + if (value == null) { + return null; + } + return new PersonName(value); + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonNameChangedEvent.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonNameChangedEvent.java new file mode 100644 index 0000000..8b22a30 --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonNameChangedEvent.java @@ -0,0 +1,130 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.EntityIdPath; +import org.fuin.ddd4j.core.EventType; +import org.fuin.ddd4j.jsonb.AbstractDomainEvent; +import org.fuin.esc.api.HasSerializedDataTypeConstant; +import org.fuin.esc.api.SerializedDataType; +import org.fuin.objects4j.common.Contract; + +import java.io.Serial; + +/** + * The name of a person entity was changed. + */ +@HasSerializedDataTypeConstant +public final class PersonNameChangedEvent extends AbstractDomainEvent { + + @Serial + private static final long serialVersionUID = 1000L; + + /** + * Unique name of the event used to store it - Should never change. + */ + public static final EventType TYPE = new EventType(PersonNameChangedEvent.class.getSimpleName()); + + /** + * Unique name of the serialized event. + */ + public static final SerializedDataType SER_TYPE = new SerializedDataType(TYPE.asBaseType()); + + @JsonbProperty("vendor-ref") + private VendorRef vendorRef; + + @JsonbProperty("person-id") + private PersonId personId; + + @JsonbProperty("old-name") + private PersonName oldName; + + @JsonbProperty("new-name") + private PersonName newName; + + /** + * Default constructor only for deserialization. + */ + protected PersonNameChangedEvent() { + super(); + } + + /** + * Constructor with event data. + * + * @param vendorRef Aggregate root reference. + * @param personId Person ID. + * @param oldName Person name. + * @param newName Person name. + */ + public PersonNameChangedEvent(@NotNull final VendorRef vendorRef, @NotNull final PersonId personId, @NotNull final PersonName oldName, + @NotNull final PersonName newName) { + super(new EntityIdPath(vendorRef.getId(), personId)); + Contract.requireArgNotNull("vendorRef", vendorRef); + Contract.requireArgNotNull("personId", personId); + Contract.requireArgNotNull("oldName", oldName); + Contract.requireArgNotNull("newName", newName); + this.vendorRef = vendorRef; + this.personId = personId; + this.oldName = oldName; + this.newName = newName; + } + + @Override + public final EventType getEventType() { + return TYPE; + } + + /** + * Returns the person ID. + * + * @return Person ID. + */ + @NotNull + public final PersonId getPersonId() { + return personId; + } + + /** + * Returns the old person name. + * + * @return Old name. + */ + @NotNull + public final PersonName getOldName() { + return oldName; + } + + /** + * Returns the new person name. + * + * @return New name. + */ + @NotNull + public final PersonName getNewName() { + return newName; + } + + @Override + public final String toString() { + return "Changed name of person #" + personId + " from '" + oldName + "' to '" + newName + "' for " + vendorRef; + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonNotFoundException.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonNotFoundException.java new file mode 100644 index 0000000..0358096 --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/PersonNotFoundException.java @@ -0,0 +1,70 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import org.fuin.objects4j.common.Contract; + +import java.io.Serial; + +/** + * The person identifier is unknown for the vendor. + */ +public final class PersonNotFoundException extends Exception { + + @Serial + private static final long serialVersionUID = 1L; + + private final VendorRef vendorRef; + + private final PersonId personId; + + /** + * Constructor with mandatory data. + * + * @param vendorRef + * Vendor reference. + * @param personId + * Person identifier. + */ + public PersonNotFoundException(final VendorRef vendorRef, final PersonId personId) { + super("No person with # " + personId + " found for: " + vendorRef); + Contract.requireArgNotNull("vendorRef", vendorRef); + Contract.requireArgNotNull("personId", personId); + this.vendorRef = vendorRef; + this.personId = personId; + } + + /** + * Returns the vendor reference. + * + * @return Reference. + */ + public final VendorRef getVendorRef() { + return vendorRef; + } + + /** + * Returns the ID that caused the problem. + * + * @return Person ID that was not found. + */ + public final PersonId getPersonId() { + return personId; + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/Vendor.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/Vendor.java new file mode 100644 index 0000000..920d64f --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/Vendor.java @@ -0,0 +1,195 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.AbstractAggregateRoot; +import org.fuin.ddd4j.core.ApplyEvent; +import org.fuin.ddd4j.core.ChildEntityLocator; +import org.fuin.ddd4j.core.EntityType; +import org.fuin.objects4j.common.Contract; + +import java.util.ArrayList; +import java.util.List; + +/** + * Vendor aggregate. + */ +public class Vendor extends AbstractAggregateRoot { + + private VendorRef ref; + + private Integer lastPersonId; + + private List persons; + + /** + * Default constructor used by the repositories. NEVER use in your application code! + */ + public Vendor() { + super(); + } + + /** + * Constructor with all data. + * + * @param id + * Unique identifier. + * @param key + * Unique key. + * @param name + * Name. + * @param service + * Service used by the method. + * + * @throws DuplicateVendorKeyException + * The given key already exists. + */ + public Vendor(@NotNull final VendorId id, @NotNull final VendorKey key, @NotNull final VendorName name, + @NotNull final ConstructorService service) throws DuplicateVendorKeyException { + super(); + + // CHECK PRECONDITIONS + Contract.requireArgNotNull("id", id); + Contract.requireArgNotNull("key", key); + Contract.requireArgNotNull("name", name); + Contract.requireArgNotNull("service", service); + + // VERIFY BUSINESS RULES + service.addVendorKey(key); + + // HANDLE EVENT + apply(new VendorCreatedEvent(new VendorRef(id, key, name))); + } + + @ApplyEvent + private void applyEvent(final VendorCreatedEvent event) { + this.ref = event.getVendorRef(); + } + + /** + * Adds a person with the given name. + * + * @param name + * Name of the person to add. + */ + public final void addPerson(@NotNull final PersonName name) { + + // CHECK PRECONDITIONS + Contract.requireArgNotNull("name", name); + + // VERIFY BUSINESS RULES + // Nothing to do here + + // HANDLE EVENT + apply(new PersonCreatedEvent(ref, new PersonId(nextId()), name)); + + } + + @ApplyEvent + private void applyEvent(final PersonCreatedEvent event) { + this.lastPersonId = event.getPersonId().asBaseType(); + if (persons == null) { + persons = new ArrayList<>(); + } + persons.add(new Person(this, event.getPersonId(), event.getPersonName())); + } + + private int nextId() { + if (lastPersonId == null) { + return 1; + } + return lastPersonId + 1; + } + + /** + * Changes the name of a person. + * + * @param personId + * Person ID. + * @param newName + * New name. + * + * @throws PersonNotFoundException + * No person with teh given ID was found. + */ + public final void changePersonName(@NotNull final PersonId personId, @NotNull final PersonName newName) throws PersonNotFoundException { + + // CHECK PRECONDITIONS + Contract.requireArgNotNull("personId", personId); + Contract.requireArgNotNull("newName", newName); + + // LOCATE PERSON + final Person person = findPerson(personId); + if (person == null) { + throw new PersonNotFoundException(getRef(), personId); + } + + // FORWARD CHANGE REQUEST TO PERSON + person.changeName(newName); + + } + + @ChildEntityLocator + private Person findPerson(final PersonId personId) { + for (final Person child : persons) { + if (child.getId().equals(personId)) { + return child; + } + } + return null; + } + + @Override + public final EntityType getType() { + return VendorId.TYPE; + } + + @Override + public final VendorId getId() { + return ref.getId(); + } + + /** + * Returns the reference. + * + * @return Vendor reference. + */ + public final VendorRef getRef() { + return ref; + } + + /** + * Interface for the constructor. + */ + public interface ConstructorService { + + /** + * Adds a new vendor key to the context. The key will be persisted or an exception will be thrown if it already exists. + * + * @param key + * Key to verify and persist. + * + * @throws DuplicateVendorKeyException + * The given key already exists. + */ + void addVendorKey(VendorKey key) throws DuplicateVendorKeyException; + + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorCreatedEvent.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorCreatedEvent.java new file mode 100644 index 0000000..759be5e --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorCreatedEvent.java @@ -0,0 +1,86 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.EntityIdPath; +import org.fuin.ddd4j.core.EventType; +import org.fuin.ddd4j.jsonb.AbstractDomainEvent; +import org.fuin.esc.api.HasSerializedDataTypeConstant; +import org.fuin.esc.api.SerializedDataType; + +import java.io.Serial; + +/** + * A vendor entity was created. + */ +@HasSerializedDataTypeConstant +public final class VendorCreatedEvent extends AbstractDomainEvent { + + @Serial + private static final long serialVersionUID = 1000L; + + /** Unique name of the event used to store it - Should never change. */ + public static final EventType TYPE = new EventType(VendorCreatedEvent.class.getSimpleName()); + + /** Unique name of the serialized event. */ + public static final SerializedDataType SER_TYPE = new SerializedDataType(TYPE.asBaseType()); + + @JsonbProperty("vendor") + private VendorRef vendorRef; + + /** + * Default constructor only for deserialization. + */ + protected VendorCreatedEvent() { + super(); + } + + /** + * Constructor with event data. + * + * @param vendorRef + * Vendor reference. + */ + public VendorCreatedEvent(@NotNull final VendorRef vendorRef) { + super(new EntityIdPath(vendorRef.getId())); + this.vendorRef = vendorRef; + } + + @Override + public final EventType getEventType() { + return TYPE; + } + + /** + * Returns the vendor reference. + * + * @return Vendor reference. + */ + @NotNull + public final VendorRef getVendorRef() { + return vendorRef; + } + + @Override + public final String toString() { + return "Created vendor '" + vendorRef + "'"; + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorEventId.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorEventId.java new file mode 100644 index 0000000..9685986 --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorEventId.java @@ -0,0 +1,111 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.validation.constraints.NotNull; +import org.fuin.objects4j.common.Contract; + +import java.io.Serial; +import java.io.Serializable; + +/** + * Identifies a stream event based on a string and a number. + */ +public final class VendorEventId implements Serializable { + + @Serial + private static final long serialVersionUID = 1000L; + + private final String vendorId; + + private final Integer eventNumber; + + /** + * Constructor with all required data. + * + * @param vendorId + * Unique name. + * @param eventNumber + * Number of the event within the stream. + */ + public VendorEventId(@NotNull final VendorId vendorId, @NotNull final Integer eventNumber) { + super(); + Contract.requireArgNotNull("vendorId", vendorId); + Contract.requireArgNotNull("nueventNumbermber", eventNumber); + this.vendorId = vendorId.asString(); + this.eventNumber = eventNumber; + } + + /** + * Returns the vendor ID. + * + * @return Unique vendor identifier. + */ + @NotNull + public final String getVendorId() { + return vendorId; + } + + /** + * Returns the number of the event within the stream. + * + * @return Order of the event in the stream. + */ + @NotNull + public final Integer getEventNumber() { + return eventNumber; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((vendorId == null) ? 0 : vendorId.hashCode()); + result = prime * result + ((eventNumber == null) ? 0 : eventNumber.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + VendorEventId other = (VendorEventId) obj; + if (vendorId == null) { + if (other.vendorId != null) + return false; + } else if (!vendorId.equals(other.vendorId)) + return false; + if (eventNumber == null) { + if (other.eventNumber != null) + return false; + } else if (!eventNumber.equals(other.eventNumber)) + return false; + return true; + } + + + @Override + public final String toString() { + return vendorId + "-" + eventNumber; + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorExampleTest.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorExampleTest.java new file mode 100644 index 0000000..b1da09a --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorExampleTest.java @@ -0,0 +1,104 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import org.fuin.ddd4j.core.DomainEvent; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; + +public class VendorExampleTest { + + @Test + public void testConstructor() throws DuplicateVendorKeyException { + + // PREPARE + VendorId id = new VendorId(UUID.randomUUID()); + VendorKey key = new VendorKey("V00001"); + VendorName name = new VendorName("Peter Parker Inc."); + Vendor.ConstructorService service = key1 -> { + // Do nothing + }; + + // TEST + Vendor vendor = new Vendor(id, key, name, service); + + // VERIFY + List> events = vendor.getUncommittedChanges(); + assertThat(events).hasSize(1); + assertThat(events.get(0)).isInstanceOf(VendorCreatedEvent.class); + + } + + @Test + public void testAddPerson() throws DuplicateVendorKeyException { + + // PREPARE + Vendor vendor = createVendor(); + final PersonName personName = new PersonName("Peter Parker"); + + // TEST + vendor.addPerson(personName); + + // VERIFY + List> events = vendor.getUncommittedChanges(); + assertThat(events).hasSize(1); + assertThat(events.get(0)).isInstanceOf(PersonCreatedEvent.class); + + } + + @Test + public void testChangePersonName() throws DuplicateVendorKeyException, PersonNotFoundException { + + // PREPARE + Vendor vendor = createVendor(); + final PersonName oldName = new PersonName("Peter Parker"); + vendor.addPerson(oldName); + final PersonId personId = ((PersonCreatedEvent) vendor.getUncommittedChanges().get(0)).getPersonId(); + vendor.markChangesAsCommitted(); + final PersonName newName = new PersonName("Harry Osborn"); + + // TEST + vendor.changePersonName(personId, newName); + + // VERIFY + List> events = vendor.getUncommittedChanges(); + assertThat(events).hasSize(1); + assertThat(events.get(0)).isInstanceOf(PersonNameChangedEvent.class); + PersonNameChangedEvent event = (PersonNameChangedEvent) events.get(0); + assertThat(event.getOldName()).isEqualTo(oldName); + assertThat(event.getNewName()).isEqualTo(newName); + + } + + private Vendor createVendor() throws DuplicateVendorKeyException { + VendorId id = new VendorId(UUID.randomUUID()); + VendorKey key = new VendorKey("V00001"); + VendorName name = new VendorName("Peter Parker Inc."); + Vendor.ConstructorService service = key1 -> { + // Do nothing + }; + final Vendor vendor = new Vendor(id, key, name, service); + vendor.markChangesAsCommitted(); + return vendor; + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorIdJsonbAdapter.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorIdJsonbAdapter.java new file mode 100644 index 0000000..81c8464 --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorIdJsonbAdapter.java @@ -0,0 +1,34 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import org.fuin.objects4j.jsonb.ValueObjectStringJsonbAdapter; + +import javax.annotation.concurrent.ThreadSafe; + +/** + * Converts a vendor identifier into a string and back (JSON-B). + */ +@ThreadSafe +public final class VendorIdJsonbAdapter extends ValueObjectStringJsonbAdapter { + + public VendorIdJsonbAdapter() { + super(VendorId::valueOf); + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorKey.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorKey.java new file mode 100644 index 0000000..da5f21e --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorKey.java @@ -0,0 +1,97 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.validation.constraints.NotNull; +import org.fuin.ddd4j.core.BusinessKey; +import org.fuin.objects4j.common.Contract; +import org.fuin.objects4j.core.AbstractStringValueObject; +import org.fuin.objects4j.ui.Label; +import org.fuin.objects4j.ui.ShortLabel; +import org.fuin.objects4j.ui.Tooltip; + +import javax.annotation.concurrent.Immutable; +import java.io.Serial; + +/** + * A vendor's human-readable business key. + */ +@Label(value = "Vendor number") +@ShortLabel(value = "VN") +@Tooltip("A human readable unique identifier for a vendor. " + "Used for example in mails or contracts as a reference.") +@Immutable +public final class VendorKey extends AbstractStringValueObject implements BusinessKey { + + @Serial + private static final long serialVersionUID = 1000L; + + @NotNull + @VendorKeyStr + private final String key; + + /** + * Constructor with string. + * + * @param key + * Key. + */ + public VendorKey(@NotNull @VendorKeyStr final String key) { + super(); + Contract.requireArgNotEmpty("key", key); + VendorKeyStrValidator.requireArgValid("key", key); + this.key = key; + } + + @Override + public final String asBaseType() { + return key; + } + + @Override + public final String toString() { + return key; + } + + /** + * Returns the information if a given string is a valid key. + * + * @param value + * Value to check. A null value returns true. + * + * @return TRUE if it's a valid key, else FALSE. + */ + public static boolean isValid(final String value) { + return VendorKeyStrValidator.isValid(value); + } + + /** + * Parses a key. + * + * @param value + * Value to convert. A null value returns null. + * + * @return Converted value. + */ + public static VendorKey valueOf(final String value) { + if (value == null) { + return null; + } + return new VendorKey(value); + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorKeyJsonbAdapter.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorKeyJsonbAdapter.java new file mode 100644 index 0000000..eed2baa --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorKeyJsonbAdapter.java @@ -0,0 +1,34 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import org.fuin.objects4j.jsonb.ValueObjectStringJsonbAdapter; + +import javax.annotation.concurrent.ThreadSafe; + +/** + * Converts a vendor key into a string and back (JAXB and JPA). + */ +@ThreadSafe +public final class VendorKeyJsonbAdapter extends ValueObjectStringJsonbAdapter { + + public VendorKeyJsonbAdapter() { + super(VendorKey::valueOf); + } + +} \ No newline at end of file diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorKeyStr.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorKeyStr.java new file mode 100644 index 0000000..23219f9 --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorKeyStr.java @@ -0,0 +1,49 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.validation.Constraint; +import jakarta.validation.Payload; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * The string has to be a valid vendor ID like "V00000". + *

    + *
  • Exact 6 characters in length
  • + *
  • First character is a 'V'
  • + *
  • Rest of the characters is numeric (0-9)
  • + *
+ */ +@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Constraint(validatedBy = {VendorKeyStrValidator.class}) +@Documented +public @interface VendorKeyStr { + + String message() default "{org.fuin.ddd4j.ddd.VendorKeyStr.message}"; + + Class[] groups() default {}; + + Class[] payload() default {}; + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorKeyStrValidator.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorKeyStrValidator.java new file mode 100644 index 0000000..2fb2f0e --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorKeyStrValidator.java @@ -0,0 +1,78 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.constraints.NotNull; +import org.fuin.objects4j.common.ConstraintViolationException; + +import java.util.regex.Pattern; + +/** + * Check that a given string is a well-formed user id. + */ +public final class VendorKeyStrValidator implements ConstraintValidator { + + private static final Pattern PATTERN = Pattern.compile("V[0-9][0-9][0-9][0-9][0-9]"); + + @Override + public final void initialize(final VendorKeyStr constraintAnnotation) { + } + + @Override + public final boolean isValid(final String value, final ConstraintValidatorContext context) { + return isValid(value); + } + + /** + * Check that a given string is a well-formed user id. + * + * @param value + * Value to check. + * + * @return Returns true if it's a valid user id else false is returned. + */ + public static boolean isValid(final String value) { + if (value == null) { + return true; + } + if (value.length() != 6) { + return false; + } + return PATTERN.matcher(value).matches(); + } + + /** + * Parses the argument and throws an exception if it's not valid. + * + * @param name + * Name of the value for a possible error message. + * @param value + * Value to check. + * + * @throws ConstraintViolationException + * The value was not valid. + */ + public static void requireArgValid(@NotNull final String name, @NotNull final String value) throws ConstraintViolationException { + if (!isValid(value)) { + throw new ConstraintViolationException("The argument '" + name + "' is not valid: '" + value + "'"); + } + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorName.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorName.java new file mode 100644 index 0000000..4ebdcae --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorName.java @@ -0,0 +1,91 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.validation.constraints.NotNull; +import org.fuin.objects4j.common.Contract; +import org.fuin.objects4j.common.ValueObjectWithBaseType; +import org.fuin.objects4j.core.AbstractStringValueObject; + +import javax.annotation.concurrent.Immutable; +import java.io.Serial; + +/** + * A vendor name. + */ +@Immutable +public final class VendorName extends AbstractStringValueObject implements ValueObjectWithBaseType { + + @Serial + private static final long serialVersionUID = 1000L; + + @VendorNameStr + @NotNull + private final String name; + + /** + * Constructor with string. + * + * @param name + * Name. + */ + public VendorName(@NotNull @VendorNameStr final String name) { + super(); + Contract.requireArgNotNull("name", name); + VendorNameStrValidator.requireArgValid("name", name); + this.name = name; + } + + @Override + public final String asBaseType() { + return name; + } + + @Override + public final String toString() { + return name; + } + + /** + * Returns the information if a given string is a valid name. + * + * @param value + * Value to check. A null value returns true. + * + * @return TRUE if it's a valid key, else FALSE. + */ + public static boolean isValid(final String value) { + return VendorNameStrValidator.isValid(value); + } + + /** + * Parses a name. + * + * @param value + * Value to convert. A null value returns null. + * + * @return Converted value. + */ + public static VendorName valueOf(final String value) { + if (value == null) { + return null; + } + return new VendorName(value); + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorNameJsonbAdapter.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorNameJsonbAdapter.java new file mode 100644 index 0000000..d3582e5 --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorNameJsonbAdapter.java @@ -0,0 +1,34 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import org.fuin.objects4j.jsonb.ValueObjectStringJsonbAdapter; + +import javax.annotation.concurrent.ThreadSafe; + +/** + * Converts a vendor name into a string and back (JAXB). + */ +@ThreadSafe +public final class VendorNameJsonbAdapter extends ValueObjectStringJsonbAdapter { + + public VendorNameJsonbAdapter() { + super(VendorName::valueOf); + } + +} \ No newline at end of file diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorNameStr.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorNameStr.java new file mode 100644 index 0000000..0cc5afc --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorNameStr.java @@ -0,0 +1,44 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.validation.Constraint; +import jakarta.validation.Payload; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * The string has to be a valid vendor name. + */ +@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Constraint(validatedBy = {VendorNameStrValidator.class}) +@Documented +public @interface VendorNameStr { + + String message() default "{org.fuin.ddd4j.ddd.VendorNameStr.message}"; + + Class[] groups() default {}; + + Class[] payload() default {}; + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorNameStrValidator.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorNameStrValidator.java new file mode 100644 index 0000000..1830837 --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorNameStrValidator.java @@ -0,0 +1,75 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.constraints.NotNull; +import org.fuin.objects4j.common.ConstraintViolationException; + +/** + * Check that a given string is a valid vendor name. + */ +public final class VendorNameStrValidator implements ConstraintValidator { + + @Override + public final void initialize(final VendorNameStr constraintAnnotation) { + } + + @Override + public final boolean isValid(final String value, final ConstraintValidatorContext context) { + return isValid(value); + } + + /** + * Check that a given string is a well-formed user id. + * + * @param value + * Value to check. + * + * @return Returns true if it's a valid user id else false is returned. + */ + public static boolean isValid(final String value) { + if (value == null) { + return true; + } + final String trimmed = value.trim(); + if ((trimmed.length() < 1) || (trimmed.length() > 100)) { + return false; + } + return true; + } + + /** + * Parses the argument and throws an exception if it's not valid. + * + * @param name + * Name of the value for a possible error message. + * @param value + * Value to check. + * + * @throws ConstraintViolationException + * The value was not valid. + */ + public static void requireArgValid(@NotNull final String name, @NotNull final String value) throws ConstraintViolationException { + if (!isValid(value)) { + throw new ConstraintViolationException("The argument '" + name + "' is not valid: '" + value + "'"); + } + } + +} diff --git a/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorRef.java b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorRef.java new file mode 100644 index 0000000..82e4c70 --- /dev/null +++ b/jsonb/src/test/java/org/fuin/ddd4j/jsonbtest/VendorRef.java @@ -0,0 +1,100 @@ +/** + * Copyright (C) 2015 Michael Schnell. All rights reserved. + * http://www.fuin.org/ + *

+ * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. + *

+ * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + *

+ * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see http://www.gnu.org/licenses/. + */ +package org.fuin.ddd4j.jsonbtest; + +import jakarta.json.bind.annotation.JsonbProperty; +import jakarta.validation.constraints.NotNull; +import org.fuin.objects4j.common.Contract; +import org.fuin.objects4j.common.ValueObject; + +/** + * References a vendor. + */ +public final class VendorRef implements ValueObject { + + @JsonbProperty("id") + private VendorId vendorId; + + @JsonbProperty("key") + private VendorKey vendorKey; + + @JsonbProperty("name") + private VendorName vendorName; + + /** + * Default constructor only for de-serialization. + */ + VendorRef() { + super(); + } + + /** + * Constructor with all data. + * + * @param vendorId Technical vendor identifier. + * @param vendorKey Vendor business key. + * @param vendorName Vendor name. + */ + public VendorRef(@NotNull final VendorId vendorId, @NotNull final VendorKey vendorKey, @NotNull final VendorName vendorName) { + super(); + + Contract.requireArgNotNull("vendorId", vendorId); + Contract.requireArgNotNull("vendorKey", vendorKey); + Contract.requireArgNotNull("vendorName", vendorName); + + this.vendorId = vendorId; + this.vendorKey = vendorKey; + this.vendorName = vendorName; + } + + /** + * Returns the technical vendor identifier. + * + * @return Vendor ID. + */ + @NotNull + public VendorId getId() { + return vendorId; + } + + /** + * Returns the vendor's business key. + * + * @return Vendor key. + */ + @NotNull + public VendorKey getKey() { + return vendorKey; + } + + /** + * Returns the name of the vendor. + * + * @return Vendor name. + */ + @NotNull + public VendorName getName() { + return vendorName; + } + + @Override + public final String toString() { + return VendorId.TYPE + " " + vendorKey; + } + +}