From 3f073a8cb98d027c1cf3e053f8a5220cc1093355 Mon Sep 17 00:00:00 2001 From: Michael Schnell Date: Thu, 26 Dec 2019 16:43:00 +0100 Subject: [PATCH] Added shared module --- shared/README.md | 17 ++ shared/pom.xml | 151 ++++++++++++ .../example/shared/CreatePersonCommand.java | 88 +++++++ .../example/shared/PersonCreatedEvent.java | 87 +++++++ .../fuin/cqrs4j/example/shared/PersonId.java | 148 ++++++++++++ .../cqrs4j/example/shared/PersonName.java | 216 ++++++++++++++++++ .../example/shared/SharedEntityIdFactory.java | 70 ++++++ .../cqrs4j/example/shared/SharedUtils.java | 189 +++++++++++++++ .../cqrs4j/example/shared/package-info.java | 17 ++ shared/src/main/resources/META-INF/beans.xml | 5 + .../shared/CreatePersonCommandTest.java | 108 +++++++++ .../shared/PersonCreatedEventTest.java | 108 +++++++++ .../cqrs4j/example/shared/PersonIdTest.java | 91 ++++++++ .../cqrs4j/example/shared/PersonNameTest.java | 134 +++++++++++ .../commands/CreatePersonCommand.json | 6 + .../resources/events/PersonCreatedEvent.json | 6 + 16 files changed, 1441 insertions(+) create mode 100644 shared/README.md create mode 100644 shared/pom.xml create mode 100644 shared/src/main/java/org/fuin/cqrs4j/example/shared/CreatePersonCommand.java create mode 100644 shared/src/main/java/org/fuin/cqrs4j/example/shared/PersonCreatedEvent.java create mode 100644 shared/src/main/java/org/fuin/cqrs4j/example/shared/PersonId.java create mode 100644 shared/src/main/java/org/fuin/cqrs4j/example/shared/PersonName.java create mode 100644 shared/src/main/java/org/fuin/cqrs4j/example/shared/SharedEntityIdFactory.java create mode 100644 shared/src/main/java/org/fuin/cqrs4j/example/shared/SharedUtils.java create mode 100644 shared/src/main/java/org/fuin/cqrs4j/example/shared/package-info.java create mode 100644 shared/src/main/resources/META-INF/beans.xml create mode 100644 shared/src/test/java/org/fuin/cqrs4j/example/shared/CreatePersonCommandTest.java create mode 100644 shared/src/test/java/org/fuin/cqrs4j/example/shared/PersonCreatedEventTest.java create mode 100644 shared/src/test/java/org/fuin/cqrs4j/example/shared/PersonIdTest.java create mode 100644 shared/src/test/java/org/fuin/cqrs4j/example/shared/PersonNameTest.java create mode 100644 shared/src/test/resources/commands/CreatePersonCommand.json create mode 100644 shared/src/test/resources/events/PersonCreatedEvent.json diff --git a/shared/README.md b/shared/README.md new file mode 100644 index 0000000..9c9c6f4 --- /dev/null +++ b/shared/README.md @@ -0,0 +1,17 @@ +# cqrs4j-example-shared +Shared code for all demo applications and client & server. + +## Commands +- [CreatePersonCommand](src/main/java/org/fuin/cqrs4j/example/shared/CreatePersonCommand.java) - A new person should be created in the system. (Example: [CreatePersonCommand.json](src/test/resources/commands/CreatePersonCommand.json)) + +## Events +- [PersonCreatedEvent](src/main/java/org/fuin/cqrs4j/example/shared/PersonCreatedEvent.java) - A new person was created in the system. (Example: [PersonCreatedEvent.json](src/test/resources/events/PersonCreatedEvent.json)) + +## Value Objects +- [PersonId](src/main/java/org/fuin/cqrs4j/example/shared/PersonId.java) - Identifies uniquely a person aggregate. +- [PersonName](src/main/java/org/fuin/cqrs4j/example/shared/PersonName.java) - Name of a person. + +## Supporting classes +- [SharedEntityIdFactory](src/main/java/org/fuin/cqrs4j/example/shared/SharedEntityIdFactory.java) - Factory that creates entity identifier instances based on the type. +- [SharedUtils](src/main/java/org/fuin/cqrs4j/example/shared/SharedUtils.java) - Utilities. + diff --git a/shared/pom.xml b/shared/pom.xml new file mode 100644 index 0000000..f89ee67 --- /dev/null +++ b/shared/pom.xml @@ -0,0 +1,151 @@ + + + 4.0.0 + + org.fuin.cqrs4j.example + cqrs4j-example-shared + 0.1.0-SNAPSHOT + cqrs4j-example-shared + Shared code for all demo applications and client & server + + + UTF-8 + 1.8 + 1.8 + 1.8 + 0.3.1-SNAPSHOT + + + + + + + + org.fuin + ddd-4-java + 0.2.1-SNAPSHOT + + + + org.fuin + cqrs-4-java + 0.2.1-SNAPSHOT + + + + org.fuin + objects4j + 0.6.9-SNAPSHOT + + + + org.fuin.esc + esc-api + ${esc.version} + + + + org.fuin.esc + esc-spi + ${esc.version} + + + + org.slf4j + slf4j-api + 1.7.25 + + + + org.hibernate.validator + hibernate-validator + 6.0.10.Final + + + jaxb-impl + com.sun.xml.bind + + + jaxb-api + javax.xml.bind + + + + + + org.glassfish + javax.json + 1.1.4 + + + + org.eclipse + yasson + 1.0.3 + + + + jakarta.persistence + jakarta.persistence-api + 2.2.3 + + + + + + junit + junit + 4.12 + test + + + + org.assertj + assertj-core + 3.10.0 + test + + + + org.fuin + units4j + 0.8.3 + test + + + + nl.jqno.equalsverifier + equalsverifier + 2.4.6 + test + + + + commons-io + commons-io + 2.6 + test + + + + + + + + sonatype.oss.snapshots + Sonatype OSS Snapshot Repository + https://oss.sonatype.org/content/repositories/snapshots + + false + + + always + true + + + + + + diff --git a/shared/src/main/java/org/fuin/cqrs4j/example/shared/CreatePersonCommand.java b/shared/src/main/java/org/fuin/cqrs4j/example/shared/CreatePersonCommand.java new file mode 100644 index 0000000..1964e19 --- /dev/null +++ b/shared/src/main/java/org/fuin/cqrs4j/example/shared/CreatePersonCommand.java @@ -0,0 +1,88 @@ +/** + * 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.cqrs4j.example.shared; + +import javax.annotation.concurrent.Immutable; +import javax.json.bind.annotation.JsonbProperty; +import javax.validation.constraints.NotNull; + +import org.fuin.cqrs4j.AbstractAggregateCommand; +import org.fuin.ddd4j.ddd.DomainEventExpectedEntityIdPath; +import org.fuin.ddd4j.ddd.EventType; +import org.fuin.esc.spi.SerializedDataType; +import org.fuin.objects4j.common.Contract; + +/** + * A new person should be created in the system. + */ +@Immutable +@DomainEventExpectedEntityIdPath(PersonId.class) +public final class CreatePersonCommand extends AbstractAggregateCommand { + + private static final long serialVersionUID = 1000L; + + /** Never changing unique event type name. */ + public static final EventType TYPE = new EventType("CreatePersonCommand"); + + /** Unique name used for marshalling/unmarshalling the event. */ + public static final SerializedDataType SER_TYPE = new SerializedDataType(CreatePersonCommand.TYPE.asBaseType()); + + @NotNull + @JsonbProperty("name") + private PersonName name; + + /** + * Protected default constructor for deserialization. + */ + protected CreatePersonCommand() { + super(); + } + + /** + * A new person was created in the system. + * + * @param id Identifies uniquely a person. + * @param name Name of a person. + */ + public CreatePersonCommand(@NotNull final PersonId id, @NotNull final PersonName name) { + super(id, null); + Contract.requireArgNotNull("name", name); + this.name = name; + } + + @Override + public final EventType getEventType() { + return CreatePersonCommand.TYPE; + } + + /** + * Returns: Name of a person. + * + * @return Current value. + */ + @NotNull + public final PersonName getName() { + return name; + } + + @Override + public final String toString() { + return "Create person '" + name + "' with identifier '" + getAggregateRootId() + "'"; + } + +} \ No newline at end of file diff --git a/shared/src/main/java/org/fuin/cqrs4j/example/shared/PersonCreatedEvent.java b/shared/src/main/java/org/fuin/cqrs4j/example/shared/PersonCreatedEvent.java new file mode 100644 index 0000000..a02261f --- /dev/null +++ b/shared/src/main/java/org/fuin/cqrs4j/example/shared/PersonCreatedEvent.java @@ -0,0 +1,87 @@ +/** + * 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.cqrs4j.example.shared; + +import javax.annotation.concurrent.Immutable; +import javax.json.bind.annotation.JsonbProperty; +import javax.validation.constraints.NotNull; + +import org.fuin.ddd4j.ddd.AbstractDomainEvent; +import org.fuin.ddd4j.ddd.EntityIdPath; +import org.fuin.ddd4j.ddd.EventType; +import org.fuin.esc.spi.SerializedDataType; +import org.fuin.objects4j.common.Contract; + +/** + * A new person was created in the system. + */ +@Immutable +public final class PersonCreatedEvent extends AbstractDomainEvent { + + private static final long serialVersionUID = 1000L; + + /** Never changing unique event type name. */ + public static final EventType TYPE = new EventType("PersonCreatedEvent"); + + /** Unique name used for marshalling/unmarshalling the event. */ + public static final SerializedDataType SER_TYPE = new SerializedDataType(PersonCreatedEvent.TYPE.asBaseType()); + + @NotNull + @JsonbProperty("name") + private PersonName name; + + /** + * Protected default constructor for deserialization. + */ + protected PersonCreatedEvent() { + super(); + } + + /** + * A new person was created in the system. + * + * @param id Identifies uniquely a person. + * @param name Name of a person. + */ + public PersonCreatedEvent(@NotNull final PersonId id, @NotNull final PersonName name) { + super(new EntityIdPath(id)); + Contract.requireArgNotNull("name", name); + this.name = name; + } + + @Override + public final EventType getEventType() { + return PersonCreatedEvent.TYPE; + } + + /** + * Returns: Name of a person. + * + * @return Current value. + */ + @NotNull + public final PersonName getName() { + return name; + } + + @Override + public final String toString() { + return "Person '" + name + "' was created"; + } + +} diff --git a/shared/src/main/java/org/fuin/cqrs4j/example/shared/PersonId.java b/shared/src/main/java/org/fuin/cqrs4j/example/shared/PersonId.java new file mode 100644 index 0000000..d060e8f --- /dev/null +++ b/shared/src/main/java/org/fuin/cqrs4j/example/shared/PersonId.java @@ -0,0 +1,148 @@ +/** + * 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.cqrs4j.example.shared; + +import java.util.UUID; + +import javax.annotation.concurrent.Immutable; +import javax.json.bind.adapter.JsonbAdapter; +import javax.validation.constraints.NotNull; + +import org.fuin.ddd4j.ddd.AggregateRootUuid; +import org.fuin.ddd4j.ddd.EntityType; +import org.fuin.ddd4j.ddd.StringBasedEntityType; +import org.fuin.objects4j.ui.Label; +import org.fuin.objects4j.ui.ShortLabel; +import org.fuin.objects4j.ui.Tooltip; +import org.fuin.objects4j.vo.ValueObjectConverter; + +/** + * Identifies uniquely a person. + */ +@ShortLabel(bundle = "ddd-cqrs-4-java-example", key = "PersonId.slabel", value = "PID") +@Label(bundle = "ddd-cqrs-4-java-example", key = "PersonId.label", value = "Person's ID") +@Tooltip(bundle = "ddd-cqrs-4-java-example", key = "PersonId.tooltip", value = "Unique identifier of a person") +@Immutable +public final class PersonId extends AggregateRootUuid { + + private static final long serialVersionUID = 1000L; + + /** Unique name of the aggregate this identifier refers to. */ + public static final EntityType TYPE = new StringBasedEntityType("PERSON"); + + /** + * Default constructor. + */ + protected PersonId() { + super(PersonId.TYPE); + } + + /** + * Constructor with all data. + * + * @param value + * Persistent value. + */ + public PersonId(@NotNull final UUID value) { + super(PersonId.TYPE, value); + } + + /** + * Verifies if the given string can be converted into a Person ID. + * + * @param value + * String with valid UUID string. A null value ris also valid. + * + * @return {@literal true} if the string is a valid UUID. + */ + public static boolean isValid(final String value) { + if (value == null) { + return true; + } + return AggregateRootUuid.isValid(value); + } + + /** + * Parses a given string and returns a new instance of PersonId. + * + * @param value + * String with valid UUID to convert. A null value returns null. + * + * @return Converted value. + */ + public static PersonId valueOf(final String value) { + if (value == null) { + return null; + } + AggregateRootUuid.requireArgValid("value", value); + return new PersonId(UUID.fromString(value)); + } + + /** + * Converts the value object from/to UUID. + */ + public static final class Converter implements ValueObjectConverter, JsonbAdapter { + + // Attribute Converter + + @Override + public final Class getBaseTypeClass() { + return UUID.class; + } + + @Override + public final Class getValueObjectClass() { + return PersonId.class; + } + + @Override + public boolean isValid(final UUID value) { + return true; + } + + @Override + public final PersonId toVO(final UUID value) { + if (value == null) { + return null; + } + return new PersonId(value); + } + + @Override + public final UUID fromVO(final PersonId value) { + if (value == null) { + return null; + } + return value.asBaseType(); + } + + // JSONB Adapter + + @Override + public final UUID adaptToJson(final PersonId obj) throws Exception { + return fromVO(obj); + } + + @Override + public final PersonId adaptFromJson(final UUID value) throws Exception { + return toVO(value); + } + + } + +} \ No newline at end of file diff --git a/shared/src/main/java/org/fuin/cqrs4j/example/shared/PersonName.java b/shared/src/main/java/org/fuin/cqrs4j/example/shared/PersonName.java new file mode 100644 index 0000000..ae100b8 --- /dev/null +++ b/shared/src/main/java/org/fuin/cqrs4j/example/shared/PersonName.java @@ -0,0 +1,216 @@ +/** + * 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.cqrs4j.example.shared; + +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; + +import javax.annotation.concurrent.Immutable; +import javax.json.bind.adapter.JsonbAdapter; +import javax.validation.Constraint; +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import javax.validation.Payload; +import javax.validation.constraints.NotNull; + +import org.fuin.objects4j.common.ConstraintViolationException; +import org.fuin.objects4j.ui.Label; +import org.fuin.objects4j.ui.ShortLabel; +import org.fuin.objects4j.ui.Tooltip; +import org.fuin.objects4j.vo.AbstractStringValueObject; +import org.fuin.objects4j.vo.ValueObjectConverter; + +/** + * Name of a person. + */ +@ShortLabel(bundle = "ddd-cqrs-4-java-example", key = "PersonName.slabel", value = "PNAME") +@Label(bundle = "ddd-cqrs-4-java-example", key = "PersonName.label", value = "Person's name") +@Tooltip(bundle = "ddd-cqrs-4-java-example", key = "PersonName.tooltip", value = "Name of a person") +@Immutable +public final class PersonName extends AbstractStringValueObject { + + private static final long serialVersionUID = 1000L; + + /** Max length of a person's name. */ + public static final int MAX_LENGTH = 100; + + @NotNull + @PersonNameStr + private String value; + + /** + * Protected default constructor for deserialization. + */ + protected PersonName() { + super(); + } + + /** + * Constructor with mandatory data. + * + * @param value Value. + */ + public PersonName(final String value) { + super(); + PersonName.requireArgValid("value", value); + this.value = value; + } + + @Override + public final String asBaseType() { + return value; + } + + @Override + public final String toString() { + return value; + } + + /** + * Verifies that a given string can be converted into the type. + * + * @param value Value to validate. + * + * @return Returns true if it's a valid type else + * false. + */ + public static boolean isValid(final String value) { + if (value == null) { + return true; + } + if (value.length() == 0) { + return false; + } + final String trimmed = value.trim(); + if (trimmed.length() > PersonName.MAX_LENGTH) { + return false; + } + return true; + } + + /** + * Verifies if the argument is valid and throws an exception if this is not the + * case. + * + * @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 (!PersonName.isValid(value)) { + throw new ConstraintViolationException("The argument '" + name + "' is not valid: '" + value + "'"); + } + + } + + /** + * Ensures that the string can be converted into the type. + */ + @Target({ ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE }) + @Retention(RetentionPolicy.RUNTIME) + @Constraint(validatedBy = { Validator.class }) + @Documented + public static @interface PersonNameStr { + + String message() + + default "{org.fuin.cqrs4j.example.javasecdi.PersonName.message}"; + + Class[] groups() default {}; + + Class[] payload() default {}; + + } + + /** + * Validates if a string is compliant with the type. + */ + public static final class Validator implements ConstraintValidator { + + @Override + public final void initialize(final PersonNameStr annotation) { + // Not used + } + + @Override + public final boolean isValid(final String value, final ConstraintValidatorContext context) { + return PersonName.isValid(value); + } + + } + + /** + * Converts the value object from/to string. + */ + public static final class Converter + implements ValueObjectConverter, JsonbAdapter { + + // Attribute Converter + + @Override + public final Class getBaseTypeClass() { + return String.class; + } + + @Override + public final Class getValueObjectClass() { + return PersonName.class; + } + + @Override + public boolean isValid(final String value) { + return PersonName.isValid(value); + } + + @Override + public final PersonName toVO(final String value) { + if (value == null) { + return null; + } + return new PersonName(value); + } + + @Override + public final String fromVO(final PersonName value) { + if (value == null) { + return null; + } + return value.asBaseType(); + } + + // JSONB Adapter + + @Override + public final String adaptToJson(final PersonName obj) throws Exception { + return fromVO(obj); + } + + @Override + public final PersonName adaptFromJson(final String str) throws Exception { + return toVO(str); + } + + } + +} diff --git a/shared/src/main/java/org/fuin/cqrs4j/example/shared/SharedEntityIdFactory.java b/shared/src/main/java/org/fuin/cqrs4j/example/shared/SharedEntityIdFactory.java new file mode 100644 index 0000000..f7a7d77 --- /dev/null +++ b/shared/src/main/java/org/fuin/cqrs4j/example/shared/SharedEntityIdFactory.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.cqrs4j.example.shared; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +import org.fuin.ddd4j.ddd.EntityId; +import org.fuin.ddd4j.ddd.EntityIdFactory; + +/** + * Factory that creates entity identifier instances based on the type. + */ +public final class SharedEntityIdFactory implements EntityIdFactory { + + private Map> valueOfMap; + + private Map> isValidMap; + + /** + * Default constructor. + */ + public SharedEntityIdFactory() { + super(); + valueOfMap = new HashMap<>(); + isValidMap = new HashMap<>(); + valueOfMap.put(PersonId.TYPE.asString(), PersonId::valueOf); + isValidMap.put(PersonId.TYPE.asString(), PersonId::isValid); + } + + @Override + public EntityId createEntityId(final String type, final String id) { + final Function factory = valueOfMap.get(type); + if (factory == null) { + throw new IllegalArgumentException("Unknown type: " + type); + } + return factory.apply(id); + } + + @Override + public boolean containsType(final String type) { + return valueOfMap.containsKey(type); + } + + @Override + public boolean isValid(String type, String id) { + final Function func = isValidMap.get(type); + if (func == null) { + return false; + } + return func.apply(id); + } + +} diff --git a/shared/src/main/java/org/fuin/cqrs4j/example/shared/SharedUtils.java b/shared/src/main/java/org/fuin/cqrs4j/example/shared/SharedUtils.java new file mode 100644 index 0000000..52d642d --- /dev/null +++ b/shared/src/main/java/org/fuin/cqrs4j/example/shared/SharedUtils.java @@ -0,0 +1,189 @@ +/** + * 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.cqrs4j.example.shared; + +import java.nio.charset.Charset; + +import javax.json.bind.adapter.JsonbAdapter; + +import org.eclipse.yasson.FieldAccessStrategy; +import org.fuin.ddd4j.ddd.AggregateVersionConverter; +import org.fuin.ddd4j.ddd.EntityIdConverter; +import org.fuin.ddd4j.ddd.EntityIdPathConverter; +import org.fuin.ddd4j.ddd.EventIdConverter; +import org.fuin.esc.spi.Base64Data; +import org.fuin.esc.spi.EscEvent; +import org.fuin.esc.spi.EscEvents; +import org.fuin.esc.spi.EscMeta; +import org.fuin.esc.spi.EscSpiUtils; +import org.fuin.esc.spi.JsonbDeSerializer; +import org.fuin.esc.spi.SerDeserializerRegistry; +import org.fuin.esc.spi.SerializedDataType; +import org.fuin.esc.spi.SerializedDataTypeRegistry; +import org.fuin.esc.spi.SimpleSerializedDataTypeRegistry; +import org.fuin.esc.spi.SimpleSerializerDeserializerRegistry; + +/** + * Utility code shared between command (write) and query (read) module. + */ +public final class SharedUtils { + + /** All types that will be written into and read from the event store. */ + private static TypeClass[] USER_DEFINED_TYPES = new TypeClass[] { + new TypeClass(PersonCreatedEvent.SER_TYPE, PersonCreatedEvent.class) }; + + /** All JSON-B adapters from this module. */ + public static JsonbAdapter[] JSONB_ADAPTERS = new JsonbAdapter[] { new EventIdConverter(), + new EntityIdPathConverter(new SharedEntityIdFactory()), new EntityIdConverter(new SharedEntityIdFactory()), + new AggregateVersionConverter(), new PersonId.Converter(), new PersonName.Converter() }; + + private SharedUtils() { + throw new UnsupportedOperationException("It is not allowed to create an instance of a utiliy class"); + } + + /** + * Create a registry that allows finding types (classes) based on their unique type name. + * + * @return New instance. + */ + public static SerializedDataTypeRegistry createTypeRegistry() { + + // Contains all types for usage with JSON-B + final SimpleSerializedDataTypeRegistry typeRegistry = new SimpleSerializedDataTypeRegistry(); + + // Base types always needed + typeRegistry.add(EscEvent.SER_TYPE, EscEvent.class); + typeRegistry.add(EscEvents.SER_TYPE, EscEvents.class); + typeRegistry.add(EscMeta.SER_TYPE, EscMeta.class); + typeRegistry.add(Base64Data.SER_TYPE, Base64Data.class); + + // User defined types + for (final TypeClass tc : USER_DEFINED_TYPES) { + typeRegistry.add(tc.getType(), tc.getClasz()); + } + return typeRegistry; + + } + + /** + * Creates a registry that connects the type with the appropriate serializer and de-serializer. + * + * @param typeRegistry + * Type registry (Mapping from type name to class). + * @param jsonbDeSer + * JSON-B serializer/deserializer to use. + * + * @return New instance. + */ + public static SerDeserializerRegistry createSerDeserializerRegistry(final SerializedDataTypeRegistry typeRegistry, + final JsonbDeSerializer jsonbDeSer) { + + final SimpleSerializerDeserializerRegistry registry = new SimpleSerializerDeserializerRegistry(); + + // Base types always needed + registry.add(EscEvents.SER_TYPE, "application/json", jsonbDeSer); + registry.add(EscEvent.SER_TYPE, "application/json", jsonbDeSer); + registry.add(EscMeta.SER_TYPE, "application/json", jsonbDeSer); + registry.add(Base64Data.SER_TYPE, "application/json", jsonbDeSer); + + // User defined types + for (final TypeClass tc : USER_DEFINED_TYPES) { + registry.add(tc.getType(), "application/json", jsonbDeSer); + } + jsonbDeSer.init(typeRegistry, registry, registry); + + return registry; + } + + /** + * Creates a registry that connects the type with the appropriate serializer and de-serializer. + * + * @return New instance. + */ + public static SerDeserializerRegistry createRegistry() { + + // Knows about all types for usage with JSON-B + final SerializedDataTypeRegistry typeRegistry = SharedUtils.createTypeRegistry(); + + // Does the actual marshalling/unmarshalling + final JsonbDeSerializer jsonbDeSer = SharedUtils.createJsonbDeSerializer(); + + // Registry connects the type with the appropriate serializer and de-serializer + final SerDeserializerRegistry serDeserRegistry = SharedUtils.createSerDeserializerRegistry(typeRegistry, jsonbDeSer); + + return serDeserRegistry; + + } + + /** + * Creates an instance of the JSON-B serializer/deserializer. + * + * @return New instance that is fully initialized with al necessary settings. + */ + public static JsonbDeSerializer createJsonbDeSerializer() { + + return JsonbDeSerializer.builder().withSerializers(EscSpiUtils.createEscJsonbSerializers()) + .withDeserializers(EscSpiUtils.createEscJsonbDeserializers()).withAdapters(JSONB_ADAPTERS) + .withPropertyVisibilityStrategy(new FieldAccessStrategy()).withEncoding(Charset.forName("utf-8")).build(); + + } + + /** + * Helper class for type/class combination. + */ + private static final class TypeClass { + + private final SerializedDataType type; + + private final Class clasz; + + /** + * Constructor with all data. + * + * @param type + * Type. + * @param clasz + * Class. + */ + public TypeClass(final SerializedDataType type, final Class clasz) { + super(); + this.type = type; + this.clasz = clasz; + } + + /** + * Returns the type. + * + * @return Type. + */ + public SerializedDataType getType() { + return type; + } + + /** + * Returns the class. + * + * @return Class. + */ + public Class getClasz() { + return clasz; + } + + } + +} \ No newline at end of file diff --git a/shared/src/main/java/org/fuin/cqrs4j/example/shared/package-info.java b/shared/src/main/java/org/fuin/cqrs4j/example/shared/package-info.java new file mode 100644 index 0000000..1ba4d68 --- /dev/null +++ b/shared/src/main/java/org/fuin/cqrs4j/example/shared/package-info.java @@ -0,0 +1,17 @@ +/** + * 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.cqrs4j.example.shared; + +/** + * Domain specific code to be shared between all modules. + */ diff --git a/shared/src/main/resources/META-INF/beans.xml b/shared/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000..afd81d2 --- /dev/null +++ b/shared/src/main/resources/META-INF/beans.xml @@ -0,0 +1,5 @@ + + + diff --git a/shared/src/test/java/org/fuin/cqrs4j/example/shared/CreatePersonCommandTest.java b/shared/src/test/java/org/fuin/cqrs4j/example/shared/CreatePersonCommandTest.java new file mode 100644 index 0000000..4cf338e --- /dev/null +++ b/shared/src/test/java/org/fuin/cqrs4j/example/shared/CreatePersonCommandTest.java @@ -0,0 +1,108 @@ +/** + * 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.cqrs4j.example.shared; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.time.ZonedDateTime; +import java.util.UUID; + +import javax.json.bind.Jsonb; +import javax.json.bind.JsonbBuilder; +import javax.json.bind.JsonbConfig; + +import org.apache.commons.io.IOUtils; +import org.eclipse.yasson.FieldAccessStrategy; +import org.fuin.utils4j.Utils4J; +import org.junit.Test; + +// CHECKSTYLE:OFF +public final class CreatePersonCommandTest { + + private static final String PERSON_UUID = "84565d62-115e-4502-b7c9-38ad69c64b05"; + + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final CreatePersonCommand original = createTestee(); + + // TEST + final CreatePersonCommand copy = Utils4J.deserialize(Utils4J.serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getAggregateRootId()).isEqualTo(original.getAggregateRootId()); + assertThat(copy.getName()).isEqualTo(original.getName()); + + } + + @Test + public final void testMarshalUnmarshalJson() { + + // PREPARE + final CreatePersonCommand original = createTestee(); + + final JsonbConfig config = new JsonbConfig().withAdapters(SharedUtils.JSONB_ADAPTERS) + .withPropertyVisibilityStrategy(new FieldAccessStrategy()); + final Jsonb jsonb = JsonbBuilder.create(config); + + // TEST + final String json = jsonb.toJson(original, CreatePersonCommand.class); + final CreatePersonCommand copy = jsonb.fromJson(json, CreatePersonCommand.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getAggregateRootId()).isEqualTo(original.getAggregateRootId()); + assertThat(copy.getName()).isEqualTo(original.getName()); + + } + + @Test + public final void testUnmarshalJsonFromFile() throws IOException { + + // PREPARE + final String json = IOUtils.toString(this.getClass().getResourceAsStream("/commands/CreatePersonCommand.json"), + Charset.forName("utf-8")); + final JsonbConfig config = new JsonbConfig().withAdapters(SharedUtils.JSONB_ADAPTERS) + .withPropertyVisibilityStrategy(new FieldAccessStrategy()); + final Jsonb jsonb = JsonbBuilder.create(config); + + + // TEST + final CreatePersonCommand copy = jsonb.fromJson(json, CreatePersonCommand.class); + + // VERIFY + assertThat(copy.getEventId().asBaseType()).isEqualTo(UUID.fromString("109a77b2-1de2-46fc-aee1-97fa7740a552")); + assertThat(copy.getTimestamp()).isEqualTo(ZonedDateTime.parse("2019-11-17T10:27:13.183+01:00[Europe/Berlin]")); + assertThat(copy.getAggregateRootId().asString()).isEqualTo(PERSON_UUID); + assertThat(copy.getName().asString()).isEqualTo("Peter Parker"); + + } + + private CreatePersonCommand createTestee() { + final PersonId personId = new PersonId(UUID.fromString(PERSON_UUID)); + final PersonName personName = new PersonName("Peter Parker"); + return new CreatePersonCommand(personId, personName); + } + +} +// CHECKSTYLE:ON diff --git a/shared/src/test/java/org/fuin/cqrs4j/example/shared/PersonCreatedEventTest.java b/shared/src/test/java/org/fuin/cqrs4j/example/shared/PersonCreatedEventTest.java new file mode 100644 index 0000000..aa0902d --- /dev/null +++ b/shared/src/test/java/org/fuin/cqrs4j/example/shared/PersonCreatedEventTest.java @@ -0,0 +1,108 @@ +/** + * 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.cqrs4j.example.shared; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.fuin.utils4j.Utils4J.deserialize; +import static org.fuin.utils4j.Utils4J.serialize; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.UUID; + +import javax.json.bind.Jsonb; +import javax.json.bind.JsonbBuilder; +import javax.json.bind.JsonbConfig; + +import org.apache.commons.io.IOUtils; +import org.eclipse.yasson.FieldAccessStrategy; +import org.junit.Test; + + +// CHECKSTYLE:OFF +public final class PersonCreatedEventTest { + + @Test + public final void testSerializeDeserialize() { + + // PREPARE + final PersonCreatedEvent original = createTestee(); + + // TEST + final PersonCreatedEvent copy = deserialize(serialize(original)); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getName()).isEqualTo(original.getName()); + + } + + @Test + public final void testMarshalUnmarshalJson() { + + // PREPARE + final PersonCreatedEvent original = createTestee(); + + final JsonbConfig config = new JsonbConfig().withAdapters(SharedUtils.JSONB_ADAPTERS) + .withPropertyVisibilityStrategy(new FieldAccessStrategy()); + final Jsonb jsonb = JsonbBuilder.create(config); + + // TEST + final String json = jsonb.toJson(original, PersonCreatedEvent.class); + final PersonCreatedEvent copy = jsonb.fromJson(json, PersonCreatedEvent.class); + + // VERIFY + assertThat(copy).isEqualTo(original); + assertThat(copy.getName()).isEqualTo(original.getName()); + + } + + @Test + public final void testUnmarshalJson() throws IOException { + + // PREPARE + final PersonCreatedEvent original = createTestee(); + final JsonbConfig config = new JsonbConfig().withAdapters(SharedUtils.JSONB_ADAPTERS) + .withPropertyVisibilityStrategy(new FieldAccessStrategy()); + final Jsonb jsonb = JsonbBuilder.create(config); + + // TEST + final String json = IOUtils.toString(this.getClass().getResourceAsStream("/events/PersonCreatedEvent.json"), + Charset.forName("utf-8")); + final PersonCreatedEvent copy = jsonb.fromJson(json, PersonCreatedEvent.class); + + // VERIFY + assertThat(copy.getEntityIdPath()).isEqualTo(original.getEntityIdPath()); + assertThat(copy.getName()).isEqualTo(original.getName()); + + } + + + @Test + public final void testToString() { + assertThat(createTestee().toString()) + .isEqualTo("Person 'Peter Parker' was created"); + } + + private PersonCreatedEvent createTestee() { + final PersonId personId = new PersonId(UUID.fromString("f645969a-402d-41a9-882b-d2d8000d0f43")); + final PersonName personName = new PersonName("Peter Parker"); + return new PersonCreatedEvent(personId, personName); + } + +} diff --git a/shared/src/test/java/org/fuin/cqrs4j/example/shared/PersonIdTest.java b/shared/src/test/java/org/fuin/cqrs4j/example/shared/PersonIdTest.java new file mode 100644 index 0000000..bc32843 --- /dev/null +++ b/shared/src/test/java/org/fuin/cqrs4j/example/shared/PersonIdTest.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.cqrs4j.example.shared; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.fail; + +import java.util.UUID; + +import org.fuin.ddd4j.ddd.EntityType; +import org.fuin.ddd4j.ddd.StringBasedEntityType; +import org.fuin.objects4j.common.ConstraintViolationException; +import org.junit.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; +import nl.jqno.equalsverifier.Warning; + +/** + * Test for {@link PersonId}. + */ +public final class PersonIdTest { + + private static final String PERSON_UUID = "84565d62-115e-4502-b7c9-38ad69c64b05"; + + @Test + public void testEquals() { + EqualsVerifier.forClass(PersonId.class).suppress(Warning.NONFINAL_FIELDS) + .withNonnullFields("entityType", "uuid") + .withPrefabValues(EntityType.class, new StringBasedEntityType("A"), new StringBasedEntityType("B")) + .verify(); + } + + @Test + public void testValueOf() { + final PersonId personId = PersonId.valueOf(PERSON_UUID); + + assertThat(personId.asString()).isEqualTo(PERSON_UUID); + + } + + @Test + public void testValueOfIllegalArgumentCharacter() { + try { + PersonId.valueOf("abc"); + fail(); + } catch (final ConstraintViolationException ex) { + assertThat(ex.getMessage()).isEqualTo("The argument 'value' is not valid: 'abc'"); + } + } + + @Test + public final void testConverterUnmarshal() throws Exception { + + // PREPARE + final String personIdValue = PERSON_UUID; + + // TEST + final PersonId personId = new PersonId.Converter().adaptFromJson(UUID.fromString(PERSON_UUID)); + + // VERIFY + assertThat(personId.asString()).isEqualTo(personIdValue); + } + + @Test + public void testConverterMarshal() throws Exception { + + final PersonId personId = PersonId.valueOf(PERSON_UUID); + + // TEST + final UUID uuid = new PersonId.Converter().adaptToJson(personId); + + // VERIFY + assertThat(uuid).isEqualTo(UUID.fromString(PERSON_UUID)); + } + +} diff --git a/shared/src/test/java/org/fuin/cqrs4j/example/shared/PersonNameTest.java b/shared/src/test/java/org/fuin/cqrs4j/example/shared/PersonNameTest.java new file mode 100644 index 0000000..1b3ae3b --- /dev/null +++ b/shared/src/test/java/org/fuin/cqrs4j/example/shared/PersonNameTest.java @@ -0,0 +1,134 @@ +/** + * 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.cqrs4j.example.shared; + + +import static org.assertj.core.api.Assertions.assertThat; + +import org.fuin.objects4j.common.ConstraintViolationException; +import org.fuin.utils4j.Utils4J; +import org.junit.Assert; +import org.junit.Test; + +import nl.jqno.equalsverifier.EqualsVerifier; +import nl.jqno.equalsverifier.Warning; + +// CHECKSTYLE:OFF +public final class PersonNameTest { + + @Test + public void testSerialize() { + final PersonName original = new PersonName("Peter Parker"); + final PersonName copy = Utils4J.deserialize(Utils4J.serialize(original)); + assertThat(original).isEqualTo(copy); + } + + @Test + public void testHashCodeEquals() { + EqualsVerifier.forClass(PersonName.class).suppress(Warning.NULL_FIELDS).withRedefinedSuperclass().verify(); + } + + @Test + public void testMarshalJson() throws Exception { + + // PREPARE + final String str = "Peter Parker"; + final PersonName testee = new PersonName(str); + + // TEST & VERIFY + assertThat(new PersonName.Converter().adaptToJson(testee)).isEqualTo(str); + assertThat(new PersonName.Converter().adaptToJson(null)).isNull(); + + } + + @Test + public void testUnmarshalJson() throws Exception { + + // PREPARE + final String str = "Peter Parker"; + final PersonName testee = new PersonName(str); + + // TEST & VERIFY + assertThat(new PersonName.Converter().adaptFromJson(str)).isEqualTo(testee); + assertThat(new PersonName.Converter().adaptFromJson(null)).isNull(); + + } + + @Test + public void testIsValid() { + + assertThat(PersonName.isValid(null)).isTrue(); + assertThat(PersonName.isValid("Peter Parker")).isTrue(); + + assertThat(PersonName.isValid("")).isFalse(); + assertThat(PersonName.isValid("123456789.123456789.123456789.123456789.123456789." + + "123456789.123456789.123456789.123456789.123456789." + "12345")).isFalse(); + + } + + @Test + public void testRequireArgValid() { + + PersonName.requireArgValid("a", "Peter Parker"); + PersonName.requireArgValid("b", null); + + try { + PersonName.requireArgValid("c", ""); + Assert.fail(); + } catch (final ConstraintViolationException ex) { + assertThat(ex.getMessage()).isEqualTo("The argument 'c' is not valid: ''"); + } + + try { + PersonName.requireArgValid("d", "123456789.123456789.123456789.123456789.123456789." + + "123456789.123456789.123456789.123456789.123456789." + "12345"); + Assert.fail(); + } catch (final ConstraintViolationException ex) { + assertThat(ex.getMessage()) + .isEqualTo("The argument 'd' is not valid: '" + "123456789.123456789.123456789.123456789.123456789." + + "123456789.123456789.123456789.123456789.123456789." + "12345" + "'"); + } + + } + + @Test + public void testValidator() { + + assertThat(new PersonName.Validator().isValid(null, null)).isTrue(); + assertThat(new PersonName.Validator().isValid("Peter Parker", null)).isTrue(); + + assertThat(new PersonName.Validator().isValid("", null)).isFalse(); + assertThat(new PersonName.Validator().isValid("123456789.123456789.123456789.123456789.123456789." + + "123456789.123456789.123456789.123456789.123456789." + "12345", null)).isFalse(); + + } + + @Test + public void testValueObjectConverter() { + + assertThat(new PersonName.Converter().getBaseTypeClass()).isEqualTo(String.class); + assertThat(new PersonName.Converter().getValueObjectClass()).isEqualTo(PersonName.class); + assertThat(new PersonName.Converter().isValid(null)).isTrue(); + assertThat(new PersonName.Converter().isValid("Peter Parker")).isTrue(); + + assertThat(new PersonName.Converter().isValid("123456789.123456789.123456789.123456789.123456789." + + "123456789.123456789.123456789.123456789.123456789." + "12345")).isFalse(); + + } + +} diff --git a/shared/src/test/resources/commands/CreatePersonCommand.json b/shared/src/test/resources/commands/CreatePersonCommand.json new file mode 100644 index 0000000..92ceb71 --- /dev/null +++ b/shared/src/test/resources/commands/CreatePersonCommand.json @@ -0,0 +1,6 @@ +{ + "event-id": "109a77b2-1de2-46fc-aee1-97fa7740a552", + "event-timestamp": "2019-11-17T10:27:13.183+01:00[Europe/Berlin]", + "entity-id-path": "PERSON 84565d62-115e-4502-b7c9-38ad69c64b05", + "name": "Peter Parker" +} \ No newline at end of file diff --git a/shared/src/test/resources/events/PersonCreatedEvent.json b/shared/src/test/resources/events/PersonCreatedEvent.json new file mode 100644 index 0000000..fd283ba --- /dev/null +++ b/shared/src/test/resources/events/PersonCreatedEvent.json @@ -0,0 +1,6 @@ +{ + "event-id": "a7b88543-ce32-40eb-a3fe-f49aec39b570", + "event-timestamp": "2019-11-02T09:56:40.669Z[Etc/UTC]", + "entity-id-path": "PERSON f645969a-402d-41a9-882b-d2d8000d0f43", + "name": "Peter Parker" +}