Added shared module

This commit is contained in:
Michael Schnell
2019-12-26 16:43:00 +01:00
parent 1f569ce5cf
commit 3f073a8cb9
16 changed files with 1441 additions and 0 deletions

17
shared/README.md Normal file
View File

@@ -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.

151
shared/pom.xml Normal file
View File

@@ -0,0 +1,151 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.fuin.cqrs4j.example</groupId>
<artifactId>cqrs4j-example-shared</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>cqrs4j-example-shared</name>
<description>Shared code for all demo applications and client &amp; server</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
<esc.version>0.3.1-SNAPSHOT</esc.version>
</properties>
<dependencies>
<!-- Compile -->
<dependency>
<groupId>org.fuin</groupId>
<artifactId>ddd-4-java</artifactId>
<version>0.2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.fuin</groupId>
<artifactId>cqrs-4-java</artifactId>
<version>0.2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.fuin</groupId>
<artifactId>objects4j</artifactId>
<version>0.6.9-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.fuin.esc</groupId>
<artifactId>esc-api</artifactId>
<version>${esc.version}</version>
</dependency>
<dependency>
<groupId>org.fuin.esc</groupId>
<artifactId>esc-spi</artifactId>
<version>${esc.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.10.Final</version>
<exclusions>
<exclusion>
<artifactId>jaxb-impl</artifactId>
<groupId>com.sun.xml.bind</groupId>
</exclusion>
<exclusion>
<artifactId>jaxb-api</artifactId>
<groupId>javax.xml.bind</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>2.2.3</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.fuin</groupId>
<artifactId>units4j</artifactId>
<version>0.8.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>2.4.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>sonatype.oss.snapshots</id>
<name>Sonatype OSS Snapshot Repository</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<updatePolicy>always</updatePolicy>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>

View File

@@ -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<PersonId, PersonId> {
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() + "'";
}
}

View File

@@ -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<PersonId> {
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";
}
}

View File

@@ -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 <code>null</code> 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 <code>null</code> value returns <code>null</code>.
*
* @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<UUID, PersonId>, JsonbAdapter<PersonId, UUID> {
// Attribute Converter
@Override
public final Class<UUID> getBaseTypeClass() {
return UUID.class;
}
@Override
public final Class<PersonId> 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);
}
}
}

View File

@@ -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 <code>true</code> if it's a valid type else
* <code>false</code>.
*/
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<? extends Payload>[] payload() default {};
}
/**
* Validates if a string is compliant with the type.
*/
public static final class Validator implements ConstraintValidator<PersonNameStr, String> {
@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<String, PersonName>, JsonbAdapter<PersonName, String> {
// Attribute Converter
@Override
public final Class<String> getBaseTypeClass() {
return String.class;
}
@Override
public final Class<PersonName> 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);
}
}
}

View File

@@ -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<String, Function<String, EntityId>> valueOfMap;
private Map<String, Function<String, Boolean>> 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<String, EntityId> 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<String, Boolean> func = isValidMap.get(type);
if (func == null) {
return false;
}
return func.apply(id);
}
}

View File

@@ -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;
}
}
}

View File

@@ -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.
*/

View File

@@ -0,0 +1,5 @@
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd" >
</beans>

View File

@@ -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

View File

@@ -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);
}
}

View File

@@ -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));
}
}

View File

@@ -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();
}
}

View File

@@ -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"
}

View File

@@ -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"
}