Refactored module and package structure

This commit is contained in:
Michael Schnell
2019-12-31 08:21:44 +01:00
parent 61db5c0bbe
commit eb71c77197
42 changed files with 347 additions and 1984 deletions

View File

@@ -26,8 +26,8 @@ import org.fuin.objects4j.common.Contract;
* Stores the next position to read from the projection in the event store.
*/
@Entity
@Table(name = "QRY_PERSON_PROJECTION_POS")
public class ProjectionPosition {
@Table(name = "QUARKUS_QRY_PROJECTION_POS")
public class QryProjectionPosition {
@Id
@Column(name = "STREAM_ID", nullable = false, length = 250, updatable = false)
@@ -41,7 +41,7 @@ public class ProjectionPosition {
/**
* JPA constructor.
*/
protected ProjectionPosition() {
protected QryProjectionPosition() {
super();
}
@@ -53,7 +53,7 @@ public class ProjectionPosition {
* @param nextPos
* Next position from the stream to read.
*/
public ProjectionPosition(@NotNull final StreamId streamId, @NotNull final Long nextPos) {
public QryProjectionPosition(@NotNull final StreamId streamId, @NotNull final Long nextPos) {
super();
Contract.requireArgNotNull("streamId", streamId);
Contract.requireArgNotNull("nextPos", nextPos);
@@ -94,7 +94,7 @@ public class ProjectionPosition {
@Override
public String toString() {
return "QryPersonHandlerPosition [streamId=" + streamId + ", nextPos=" + nextPos + "]";
return "QryProjectionPosition [streamId=" + streamId + ", nextPos=" + nextPos + "]";
}
}

View File

@@ -25,7 +25,7 @@ import org.fuin.objects4j.common.Contract;
* Repository that contains the position of the stream.
*/
@ApplicationScoped
public class ProjectionPositionRepository implements ProjectionService {
public class QryProjectionPositionRepository implements ProjectionService {
@Inject
EntityManager em;
@@ -33,7 +33,7 @@ public class ProjectionPositionRepository implements ProjectionService {
@Override
public void resetProjectionPosition(@NotNull final StreamId streamId) {
Contract.requireArgNotNull("streamId", streamId);
final ProjectionPosition pos = em.find(ProjectionPosition.class, streamId.asString());
final QryProjectionPosition pos = em.find(QryProjectionPosition.class, streamId.asString());
if (pos != null) {
pos.setNextPosition(0L);
}
@@ -42,7 +42,7 @@ public class ProjectionPositionRepository implements ProjectionService {
@Override
public Long readProjectionPosition(@NotNull StreamId streamId) {
Contract.requireArgNotNull("streamId", streamId);
final ProjectionPosition pos = em.find(ProjectionPosition.class, streamId.asString());
final QryProjectionPosition pos = em.find(QryProjectionPosition.class, streamId.asString());
if (pos == null) {
return 0L;
}
@@ -53,9 +53,9 @@ public class ProjectionPositionRepository implements ProjectionService {
public void updateProjectionPosition(@NotNull StreamId streamId, @NotNull Long nextEventNumber) {
Contract.requireArgNotNull("streamId", streamId);
Contract.requireArgNotNull("nextEventNumber", nextEventNumber);
final ProjectionPosition pos = em.find(ProjectionPosition.class, streamId.asString());
final QryProjectionPosition pos = em.find(QryProjectionPosition.class, streamId.asString());
if (pos == null) {
em.persist(new ProjectionPosition(streamId, nextEventNumber));
em.persist(new QryProjectionPosition(streamId, nextEventNumber));
} else {
pos.setNextPosition(nextEventNumber);
em.merge(pos);

View File

@@ -33,7 +33,7 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
* Represents a person that will be stored in the database.
*/
@Entity
@Table(name = "PERSON_LIST")
@Table(name = "QUARKUS_PERSON_LIST")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "person")
@NamedQuery(name = PersonListEntry.FIND_ALL, query = "SELECT p FROM PersonListEntry p")

View File

@@ -30,7 +30,7 @@ public class PersonListEventChunkHandler {
private static final Logger LOG = LoggerFactory.getLogger(PersonListEventChunkHandler.class);
/** Unique name of the event store projection that is used. */
public static final ProjectionStreamId PROJECTION_STREAM_ID = new ProjectionStreamId("qry-person-stream");
public static final ProjectionStreamId PROJECTION_STREAM_ID = new ProjectionStreamId("quarkus-qry-person-stream");
@Inject
EventDispatcher dispatcher;

View File

@@ -21,13 +21,25 @@
<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>
<esc.version>0.3.1</esc.version>
</properties>
<dependencies>
<!-- compile -->
<dependency>
<groupId>org.fuin.cqrs4j.example</groupId>
<artifactId>cqrs4j-example-shared</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.fuin.cqrs4j.example</groupId>
<artifactId>cqrs4j-example-aggregates</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.fuin.cqrs4j.example.spring</groupId>
<artifactId>cqrs4j-spring-example-shared</artifactId>
@@ -47,26 +59,26 @@
<dependency>
<groupId>org.fuin</groupId>
<artifactId>cqrs-4-java</artifactId>
<version>0.2.1-SNAPSHOT</version>
<version>0.2.1</version>
</dependency>
<dependency>
<groupId>org.fuin.esc</groupId>
<artifactId>esc-spi</artifactId>
<version>${esc.version}</version>
</dependency>
<dependency>
<groupId>org.fuin.esc</groupId>
<artifactId>esc-spi</artifactId>
<version>${esc.version}</version>
</dependency>
<dependency>
<groupId>org.fuin.esc</groupId>
<artifactId>esc-esjc</artifactId>
<version>${esc.version}</version>
</dependency>
<dependency>
<groupId>org.fuin.esc</groupId>
<artifactId>esc-esjc</artifactId>
<version>${esc.version}</version>
</dependency>
<dependency>
<groupId>org.fuin.esc</groupId>
<artifactId>esc-esjc</artifactId>
<version>${esc.version}</version>
</dependency>
<dependency>
<groupId>org.fuin.esc</groupId>
<artifactId>esc-esjc</artifactId>
<version>${esc.version}</version>
</dependency>
<!-- runtime -->
@@ -128,14 +140,14 @@
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
@@ -199,9 +211,9 @@
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -8,10 +8,11 @@ import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
import org.eclipse.yasson.FieldAccessStrategy;
import org.fuin.cqrs4j.example.spring.command.domain.PersonRepository;
import org.fuin.cqrs4j.example.spring.shared.SharedUtils;
import org.fuin.esc.api.EventStore;
import org.fuin.cqrs4j.example.aggregates.PersonRepository;
import org.fuin.cqrs4j.example.shared.SharedUtils;
import org.fuin.cqrs4j.example.spring.shared.Config;
import org.fuin.esc.esjc.ESJCEventStore;
import org.fuin.esc.esjc.IESJCEventStore;
import org.fuin.esc.spi.EnhancedMimeType;
import org.fuin.esc.spi.SerDeserializerRegistry;
import org.springframework.boot.SpringApplication;
@@ -21,7 +22,8 @@ import org.springframework.web.context.annotation.RequestScope;
import com.github.msemys.esjc.EventStoreBuilder;
@SpringBootApplication(scanBasePackages = "org.fuin.cqrs4j.example.spring.command")
@SpringBootApplication(scanBasePackages = { "org.fuin.cqrs4j.example.spring.command.app",
"org.fuin.cqrs4j.example.spring.command.controller", "org.fuin.cqrs4j.example.spring.shared" })
public class CmdApplication {
/**
@@ -30,49 +32,48 @@ public class CmdApplication {
* @return Fully configured instance.
*/
@Bean
public Jsonb createJsonb() {
final JsonbConfig config = new JsonbConfig()
.withAdapters(SharedUtils.JSONB_ADAPTERS)
.withPropertyVisibilityStrategy(new FieldAccessStrategy());
final Jsonb jsonb = JsonbBuilder.create(config);
return jsonb;
}
/**
* Creates a TCP based event store connection.
*
* @param config Configuration to use.
*
* @return New event store instance.
*/
@Bean(destroyMethod = "shutdown")
public com.github.msemys.esjc.EventStore getESHttpEventStore(final CmdConfig config) {
return EventStoreBuilder.newBuilder().singleNodeAddress(config.getEventStoreHost(), config.getEventStoreTcpPort())
.executor(Executors.newFixedThreadPool(10)).userCredentials(config.getEventStoreUser(), config.getEventStorePassword())
.build();
public Jsonb createJsonb() {
final JsonbConfig config = new JsonbConfig().withAdapters(SharedUtils.JSONB_ADAPTERS)
.withPropertyVisibilityStrategy(new FieldAccessStrategy());
final Jsonb jsonb = JsonbBuilder.create(config);
return jsonb;
}
/**
* Creates an event store connection.
* Creates a TCP based event store connection.
*
* @param config Configuration to use.
*
* @return New event store instance.
*/
*/
@Bean(destroyMethod = "shutdown")
public com.github.msemys.esjc.EventStore getESHttpEventStore(final Config config) {
return EventStoreBuilder.newBuilder()
.singleNodeAddress(config.getEventStoreHost(), config.getEventStoreTcpPort())
.executor(Executors.newFixedThreadPool(10))
.userCredentials(config.getEventStoreUser(), config.getEventStorePassword()).build();
}
/**
* Creates an event store connection.
*
* @param config Configuration to use.
*
* @return New event store instance.
*/
@Bean(destroyMethod = "close")
public EventStore getEventStore(final com.github.msemys.esjc.EventStore es) {
public IESJCEventStore getEventStore(final com.github.msemys.esjc.EventStore es) {
final SerDeserializerRegistry registry = SharedUtils.createRegistry();
final EventStore eventstore = new ESJCEventStore(es, registry, registry,
EnhancedMimeType.create("application", "json", Charset.forName("utf-8")));
eventstore.open();
return eventstore;
final IESJCEventStore eventstore = new ESJCEventStore(es, registry, registry,
EnhancedMimeType.create("application", "json", Charset.forName("utf-8")));
eventstore.open();
return eventstore;
}
}
/**
* Creates an event sourced repository that can store a person.
* Creates an event sourced repository that can store a person.
*
* @param eventStore Event store to use.
*
@@ -80,11 +81,11 @@ public class CmdApplication {
*/
@Bean
@RequestScope
public PersonRepository create(final EventStore eventStore) {
return new PersonRepository(eventStore);
}
public static void main(String[] args) {
public PersonRepository create(final IESJCEventStore eventStore) {
return new PersonRepository(eventStore);
}
public static void main(String[] args) {
SpringApplication.run(CmdApplication.class, args);
}

View File

@@ -20,10 +20,10 @@ import javax.validation.Validator;
import org.fuin.cqrs4j.CommandExecutionFailedException;
import org.fuin.cqrs4j.SimpleResult;
import org.fuin.cqrs4j.example.spring.command.domain.DuplicatePersonNameException;
import org.fuin.cqrs4j.example.spring.command.domain.Person;
import org.fuin.cqrs4j.example.spring.command.domain.PersonRepository;
import org.fuin.cqrs4j.example.spring.shared.CreatePersonCommand;
import org.fuin.cqrs4j.example.aggregates.DuplicatePersonNameException;
import org.fuin.cqrs4j.example.aggregates.Person;
import org.fuin.cqrs4j.example.aggregates.PersonRepository;
import org.fuin.cqrs4j.example.shared.CreatePersonCommand;
import org.fuin.ddd4j.ddd.AggregateAlreadyExistsException;
import org.fuin.ddd4j.ddd.AggregateDeletedException;
import org.springframework.beans.factory.annotation.Autowired;

View File

@@ -1,72 +0,0 @@
/**
* 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.spring.command.domain;
import javax.validation.constraints.NotNull;
import org.fuin.cqrs4j.example.spring.shared.PersonId;
import org.fuin.cqrs4j.example.spring.shared.PersonName;
import org.fuin.objects4j.common.ExceptionShortIdentifable;
/**
* A name that should be unique does already exist.
*/
public final class DuplicatePersonNameException extends Exception implements ExceptionShortIdentifable {
private static final long serialVersionUID = 1000L;
private String sid;
private PersonId personId;
private PersonName name;
/**
* Constructor with mandatory data.
*
* @param personId
* Identifier of the resource that caused the problem.
* @param name
* Name of the resource that caused the problem.
*/
public DuplicatePersonNameException(@NotNull final PersonId personId, @NotNull final PersonName name) {
super("The name '" + name + "' already exists: " + personId.asString());
this.sid = "DUPLICATE_PERSON_NAME";
this.personId = personId;
this.name = name;
}
@Override
public final String getShortId() {
return sid;
}
/**
* Returns the identifier of the entity that has the name.
*
* @return Identifier.
*/
public final PersonId getPersonId() {
return personId;
}
/**
* Returns the name that already exists.
*
* @return Name.
*/
public final PersonName getName() {
return name;
}
}

View File

@@ -1,111 +0,0 @@
/**
* 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.spring.command.domain;
import java.io.Serializable;
import javax.validation.constraints.NotNull;
import org.fuin.cqrs4j.example.spring.shared.PersonCreatedEvent;
import org.fuin.cqrs4j.example.spring.shared.PersonId;
import org.fuin.cqrs4j.example.spring.shared.PersonName;
import org.fuin.ddd4j.ddd.AbstractAggregateRoot;
import org.fuin.ddd4j.ddd.ApplyEvent;
import org.fuin.ddd4j.ddd.EntityType;
import org.fuin.objects4j.common.Contract;
/**
* A medical practitioner most likely also holder of an accredited academic
* degree.
*/
public class Person extends AbstractAggregateRoot<PersonId> implements Serializable {
private static final long serialVersionUID = 1000L;
@NotNull
private PersonId id;
@NotNull
private PersonName name;
/**
* Default constructor that is mandatory for aggregate roots.
*/
public Person() {
super();
}
/**
* Constructor with all data.
*
* @param id Unique identifier of the person.
* @param name Unique name of the person.
* @param service Service required by the method.
*
* @throws DuplicatePersonNameException The name already exists for another
* person.
*/
public Person(@NotNull final PersonId id, @NotNull final PersonName name, final PersonService service)
throws DuplicatePersonNameException {
super();
// VERIFY PRECONDITIONS
Contract.requireArgNotNull("id", id);
Contract.requireArgNotNull("name", name);
// VERIFY BUSINESS RULES
// Rule 1: The name of the person must be unique
final PersonId otherId = service.loadPersonIdByName(name);
if (otherId != null) {
throw new DuplicatePersonNameException(otherId, name);
}
// CREATE EVENT
apply(new PersonCreatedEvent(id, name));
}
@Override
public PersonId getId() {
return id;
}
@Override
public EntityType getType() {
return PersonId.TYPE;
}
@ApplyEvent
public void applyEvent(final PersonCreatedEvent event) {
this.id = event.getEntityId();
this.name = event.getName();
}
/**
* Service for the constructor.
*/
public static interface PersonService {
/**
* Loads the person's identifier for a given name.
*
* @param name Person's name.
*
* @return Office identifier or {@literal null} if not found.
*/
public PersonId loadPersonIdByName(@NotNull PersonName name);
}
}

View File

@@ -1,51 +0,0 @@
package org.fuin.cqrs4j.example.spring.command.domain;
import javax.annotation.concurrent.NotThreadSafe;
import javax.validation.constraints.NotNull;
import org.fuin.cqrs4j.example.spring.shared.PersonId;
import org.fuin.ddd4j.ddd.EntityType;
import org.fuin.ddd4j.esrepo.EventStoreRepository;
import org.fuin.esc.api.EventStore;
/**
* Event sourced repository for storing a {@link Person} aggregate.
*/
@NotThreadSafe
public class PersonRepository extends EventStoreRepository<PersonId, Person> {
/**
* Constructor all mandatory data.
*
* @param eventStore
* Event store.
*/
public PersonRepository(final EventStore eventStore) {
super(eventStore);
}
@Override
@NotNull
public Class<Person> getAggregateClass() {
return Person.class;
}
@Override
@NotNull
public EntityType getAggregateType() {
return PersonId.TYPE;
}
@Override
@NotNull
public Person create() {
return new Person();
}
@Override
@NotNull
public String getIdParamName() {
return "personId";
}
}

View File

@@ -15,11 +15,11 @@ import javax.json.bind.Jsonb;
import org.fuin.cqrs4j.ResultType;
import org.fuin.cqrs4j.SimpleResult;
import org.fuin.cqrs4j.example.shared.CreatePersonCommand;
import org.fuin.cqrs4j.example.shared.PersonCreatedEvent;
import org.fuin.cqrs4j.example.shared.PersonId;
import org.fuin.cqrs4j.example.shared.PersonName;
import org.fuin.cqrs4j.example.spring.command.app.CmdApplication;
import org.fuin.cqrs4j.example.spring.shared.CreatePersonCommand;
import org.fuin.cqrs4j.example.spring.shared.PersonCreatedEvent;
import org.fuin.cqrs4j.example.spring.shared.PersonId;
import org.fuin.cqrs4j.example.spring.shared.PersonName;
import org.fuin.esc.api.CommonEvent;
import org.fuin.esc.api.EventStore;
import org.fuin.esc.api.SimpleStreamId;

View File

@@ -21,13 +21,19 @@
<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>
<esc.version>0.3.1</esc.version>
</properties>
<dependencies>
<!-- compile -->
<dependency>
<groupId>org.fuin.cqrs4j.example</groupId>
<artifactId>cqrs4j-example-shared</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.fuin.cqrs4j.example.spring</groupId>
<artifactId>cqrs4j-spring-example-shared</artifactId>
@@ -57,7 +63,7 @@
<dependency>
<groupId>org.fuin</groupId>
<artifactId>cqrs-4-java</artifactId>
<version>0.2.1-SNAPSHOT</version>
<version>0.2.1</version>
</dependency>
<dependency>

View File

@@ -1,24 +1,7 @@
package org.fuin.cqrs4j.example.spring.query.app;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.eclipse.yasson.FieldAccessStrategy;
import org.fuin.cqrs4j.example.spring.shared.SharedUtils;
import org.fuin.esc.eshttp.ESEnvelopeType;
import org.fuin.esc.eshttp.ESHttpEventStore;
import org.fuin.esc.eshttp.IESHttpEventStore;
import org.fuin.esc.spi.SerDeserializerRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
@@ -29,54 +12,16 @@ import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@SpringBootApplication(scanBasePackages = { "org.fuin.cqrs4j.example.spring.query.app",
"org.fuin.cqrs4j.example.spring.query.controller", "org.fuin.cqrs4j.example.spring.query.domain",
"org.fuin.cqrs4j.example.spring.query.handler" })
@EnableJpaRepositories("org.fuin.cqrs4j.example.spring.query.domain")
@EntityScan({ "org.fuin.cqrs4j.example.spring.query.domain", "org.fuin.cqrs4j.example.spring.query.handler" })
"org.fuin.cqrs4j.example.spring.query.controller", "org.fuin.cqrs4j.example.spring.query.views.common",
"org.fuin.cqrs4j.example.spring.query.views.personlist", "org.fuin.cqrs4j.example.spring.shared" })
@EnableJpaRepositories("org.fuin.cqrs4j.example.spring.query.views.common")
@EntityScan({ "org.fuin.cqrs4j.example.spring.query.views.common",
"org.fuin.cqrs4j.example.spring.query.views.personlist" })
@EnableScheduling
@EnableAsync
public class QryApplication {
/**
* Creates a Jsonb instance.
*
* @return Fully configured instance.
*/
@Bean
public Jsonb createJsonb() {
final JsonbConfig config = new JsonbConfig().withAdapters(SharedUtils.JSONB_ADAPTERS)
.withPropertyVisibilityStrategy(new FieldAccessStrategy());
final Jsonb jsonb = JsonbBuilder.create(config);
return jsonb;
}
/**
* Creates a HTTP based event store connection.
*
* @param config Configuration to use.
*
* @return New event store instance.
*/
@Bean(destroyMethod = "close")
public IESHttpEventStore getESHttpEventStore(final QryConfig config) {
final String url = config.getEventStoreProtocol() + "://" + config.getEventStoreHost() + ":"
+ config.getEventStoreHttpPort();
try {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(config.getEventStoreUser(),
config.getEventStorePassword());
credentialsProvider.setCredentials(AuthScope.ANY, credentials);
final SerDeserializerRegistry registry = SharedUtils.createRegistry();
final ESHttpEventStore es = new ESHttpEventStore(Executors.defaultThreadFactory(), new URL(url),
ESEnvelopeType.JSON, registry, registry, credentialsProvider);
es.open();
return es;
} catch (final MalformedURLException ex) {
throw new RuntimeException("Failed to create URL: " + url, ex);
}
}
@Bean("personProjectorExecutor")
@Bean("projectorExecutor")
public Executor taskExecutor() {
final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(1);

View File

@@ -1,134 +0,0 @@
package org.fuin.cqrs4j.example.spring.query.app;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class QryConfig {
private static final String EVENT_STORE_PROTOCOL = "http";
private static final String EVENT_STORE_HOST = "localhost";
private static final int EVENT_STORE_HTTP_PORT = 2113;
private static final int EVENT_STORE_TCP_PORT = 1113;
private static final String EVENT_STORE_USER = "admin";
private static final String EVENT_STORE_PASSWORD = "changeit";
@Value("${EVENT_STORE_PROTOCOL:http}")
private String eventStoreProtocol;
@Value("${EVENT_STORE_HOST:localhost}")
private String eventStoreHost;
@Value("${EVENT_STORE_HTTP_PORT:2113}")
private int eventStoreHttpPort;
@Value("${EVENT_STORE_TCP_PORT:1113}")
private int eventStoreTcpPort;
@Value("${EVENT_STORE_USER:admin}")
private String eventStoreUser;
@Value("${EVENT_STORE_PASSWORD:changeit}")
private String eventStorePassword;
/**
* Constructor using default values internally.
*/
public QryConfig() {
super();
this.eventStoreProtocol = EVENT_STORE_PROTOCOL;
this.eventStoreHost = EVENT_STORE_HOST;
this.eventStoreHttpPort = EVENT_STORE_HTTP_PORT;
this.eventStoreTcpPort = EVENT_STORE_TCP_PORT;
this.eventStoreUser = EVENT_STORE_USER;
this.eventStorePassword = EVENT_STORE_PASSWORD;
}
/**
* Constructor with all data.
*
* @param eventStoreProtocol Protocol.
* @param eventStoreHost Host.
* @param eventStoreHttpPort HTTP port
* @param eventStoreTcpPort TCP port.
* @param eventStoreUser User.
* @param eventStorePassword Password.
*/
public QryConfig(final String eventStoreProtocol, final String eventStoreHost, final int eventStoreHttpPort,
final int eventStoreTcpPort, final String eventStoreUser, final String eventStorePassword) {
super();
this.eventStoreProtocol = eventStoreProtocol;
this.eventStoreHost = eventStoreHost;
this.eventStoreHttpPort = eventStoreHttpPort;
this.eventStoreTcpPort = eventStoreTcpPort;
this.eventStoreUser = eventStoreUser;
this.eventStorePassword = eventStorePassword;
}
/**
* Returns the protocol of the event store.
*
* @return Either http or https.
*/
public String getEventStoreProtocol() {
return eventStoreProtocol;
}
/**
* Returns the host name of the event store.
*
* @return Name.
*/
public String getEventStoreHost() {
return eventStoreHost;
}
/**
* Returns the HTTP port of the event store.
*
* @return Port.
*/
public int getEventStoreHttpPort() {
return eventStoreHttpPort;
}
/**
* Returns the TCP port of the event store.
*
* @return Port.
*/
public int getEventStoreTcpPort() {
return eventStoreTcpPort;
}
/**
* Returns the username of the event store.
*
* @return Username.
*/
public String getEventStoreUser() {
return eventStoreUser;
}
/**
* Returns the password of the event store.
*
* @return Password.
*/
public String getEventStorePassword() {
return eventStorePassword;
}
@Override
public String toString() {
return "QryConfig [eventStoreProtocol=" + eventStoreProtocol + ", eventStoreHost=" + eventStoreHost
+ ", eventStoreHttpPort=" + eventStoreHttpPort + ", eventStoreTcpPort=" + eventStoreTcpPort
+ ", eventStoreUser=" + eventStoreUser + "]";
}
}

View File

@@ -1,78 +0,0 @@
/**
* 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.spring.query.controller;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.fuin.cqrs4j.SimpleResult;
import org.fuin.objects4j.common.Contract;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
/**
* Maps the exceptions into a HTTP status.
*/
@ControllerAdvice
public class GlobalExceptionHandler {
private static final String CONSTRAINT_VIOLATION = "CONSTRAINT_VIOLATION";
private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(value = PersonNotFoundException.class)
public ResponseEntity<SimpleResult> exception(final PersonNotFoundException ex) {
LOG.info("{} {}", ex.getShortId(), ex.getMessage());
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<>(SimpleResult.error(ex.getShortId(), ex.getMessage()), headers,
HttpStatus.NOT_FOUND);
}
@ExceptionHandler(value = ConstraintViolationException.class)
public ResponseEntity<SimpleResult> exception(final ConstraintViolationException ex) {
LOG.info("{} {}", CONSTRAINT_VIOLATION, "ConstraintViolationException");
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<>(SimpleResult.error(CONSTRAINT_VIOLATION, asString(ex.getConstraintViolations())), headers, HttpStatus.BAD_REQUEST);
}
private static String asString(@Nullable final Set<ConstraintViolation<?>> constraintViolations) {
if (constraintViolations == null || constraintViolations.size() == 0) {
return "";
}
final List<String> list = new ArrayList<>();
for (final ConstraintViolation<?> constraintViolation : constraintViolations) {
list.add(Contract.asString(constraintViolation));
}
return list.toString();
}
}

View File

@@ -17,8 +17,9 @@ import java.util.UUID;
import javax.persistence.EntityManager;
import org.fuin.cqrs4j.example.spring.query.domain.QryPerson;
import org.fuin.cqrs4j.example.spring.shared.PersonId;
import org.fuin.cqrs4j.example.shared.PersonId;
import org.fuin.cqrs4j.example.spring.query.views.personlist.PersonListEntry;
import org.fuin.ddd4j.ddd.AggregateNotFoundException;
import org.fuin.objects4j.vo.UUIDStr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -47,8 +48,8 @@ public class PersonController {
* @return the list
*/
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<QryPerson> getAllPersons() {
final List<QryPerson> persons = em.createNamedQuery(QryPerson.FIND_ALL, QryPerson.class).getResultList();
public List<PersonListEntry> getAllPersons() {
final List<PersonListEntry> persons = em.createNamedQuery(PersonListEntry.FIND_ALL, PersonListEntry.class).getResultList();
LOG.info("getAllPersons() = {}", persons.size());
return persons;
}
@@ -60,16 +61,16 @@ public class PersonController {
*
* @return Person from database.
*
* @throws PersonNotFoundException A person with the given identifier is
* @throws AggregateNotFoundException A person with the given identifier is
* unknown.
*/
@GetMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<QryPerson> getPersonsById(@PathVariable(value = "id") @UUIDStr String personId)
throws PersonNotFoundException {
public ResponseEntity<PersonListEntry> getPersonsById(@PathVariable(value = "id") @UUIDStr String personId)
throws AggregateNotFoundException {
final QryPerson person = em.find(QryPerson.class, personId);
final PersonListEntry person = em.find(PersonListEntry.class, personId);
if (person == null) {
throw new PersonNotFoundException(new PersonId(UUID.fromString(personId)));
throw new AggregateNotFoundException(PersonId.TYPE, new PersonId(UUID.fromString(personId)));
}
LOG.info("getPersonsById({}) = {}", personId, person);
return ResponseEntity.ok().body(person);

View File

@@ -1,57 +0,0 @@
/**
* 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.spring.query.controller;
import javax.validation.constraints.NotNull;
import org.fuin.cqrs4j.example.spring.shared.PersonId;
import org.fuin.objects4j.common.ExceptionShortIdentifable;
/**
* A person with the given identifier is unknown.
*/
public final class PersonNotFoundException extends Exception implements ExceptionShortIdentifable {
private static final long serialVersionUID = 1000L;
private String sid;
private PersonId personId;
/**
* Constructor with mandatory data.
*
* @param personId
* Identifier of the resource that caused the problem.
*/
public PersonNotFoundException(@NotNull final PersonId personId) {
super("Person not found: " + personId.asString());
this.sid = "PERSON_NOT_FOUND";
this.personId = personId;
}
@Override
public final String getShortId() {
return sid;
}
/**
* Returns the identifier of the entity that has the name.
*
* @return Identifier.
*/
public final PersonId getPersonId() {
return personId;
}
}

View File

@@ -1,4 +1,4 @@
package org.fuin.cqrs4j.example.spring.query.handler;
package org.fuin.cqrs4j.example.spring.query.views.common;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -14,7 +14,7 @@ import org.fuin.objects4j.common.Contract;
* Stores the next position to read from a projection in the event store.
*/
@Entity
@Table(name = "QRY_PROJECTION_POS")
@Table(name = "SPRING_QRY_PROJECTION_POS")
public class QryProjectionPosition {
@Id

View File

@@ -1,4 +1,4 @@
package org.fuin.cqrs4j.example.spring.query.handler;
package org.fuin.cqrs4j.example.spring.query.views.common;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

View File

@@ -0,0 +1,18 @@
/**
* 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.spring.query.views;
/**
* Contains the views used in this query application. A view never uses code of another view, means all views are completely independent of
* each other. As an exception, the 'commons' package has some small classes that are not view specific.
*/

View File

@@ -1,11 +1,10 @@
package org.fuin.cqrs4j.example.spring.query.handler;
package org.fuin.cqrs4j.example.spring.query.views.personlist;
import javax.persistence.EntityManager;
import org.fuin.cqrs4j.EventHandler;
import org.fuin.cqrs4j.example.spring.query.domain.QryPerson;
import org.fuin.cqrs4j.example.spring.shared.PersonCreatedEvent;
import org.fuin.cqrs4j.example.spring.shared.PersonId;
import org.fuin.cqrs4j.example.shared.PersonCreatedEvent;
import org.fuin.cqrs4j.example.shared.PersonId;
import org.fuin.ddd4j.ddd.EventType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -32,8 +31,8 @@ public class PersonCreatedEventHandler implements EventHandler<PersonCreatedEven
public void handle(final PersonCreatedEvent event) {
LOG.info("Handle " + event);
final PersonId personId = event.getEntityId();
if (em.find(QryPerson.class, personId.asString()) == null) {
em.persist(new QryPerson(personId, event.getName()));
if (em.find(PersonListEntry.class, personId.asString()) == null) {
em.persist(new PersonListEntry(personId, event.getName()));
}
}

View File

@@ -10,7 +10,7 @@
* 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.spring.query.domain;
package org.fuin.cqrs4j.example.spring.query.views.personlist;
import javax.persistence.Column;
import javax.persistence.Entity;
@@ -19,19 +19,19 @@ import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import org.fuin.cqrs4j.example.spring.shared.PersonId;
import org.fuin.cqrs4j.example.spring.shared.PersonName;
import org.fuin.cqrs4j.example.shared.PersonId;
import org.fuin.cqrs4j.example.shared.PersonName;
import org.fuin.objects4j.common.Contract;
/**
* Represents a person that will be stored in the database.
*/
@Entity
@NamedQuery(name = QryPerson.FIND_ALL, query = "SELECT p FROM QryPerson p")
@Table(name = "QRY_PERSON")
public class QryPerson {
@NamedQuery(name = PersonListEntry.FIND_ALL, query = "SELECT p FROM PersonListEntry p")
@Table(name = "SPRING_PERSON_LIST")
public class PersonListEntry {
public static final String FIND_ALL = "QryPerson.findAll";
public static final String FIND_ALL = "PersonListEntry.findAll";
@Id
@Column(name = "ID", nullable = false, length = 36, updatable = false)
@@ -45,7 +45,7 @@ public class QryPerson {
/**
* Deserialization constructor.
*/
protected QryPerson() {
protected PersonListEntry() {
super();
}
@@ -57,7 +57,7 @@ public class QryPerson {
* @param name
* Name of the created person
*/
public QryPerson(@NotNull final PersonId id, @NotNull final PersonName name) {
public PersonListEntry(@NotNull final PersonId id, @NotNull final PersonName name) {
super();
Contract.requireArgNotNull("id", id);
Contract.requireArgNotNull("name", name);
@@ -98,7 +98,7 @@ public class QryPerson {
@Override
public String toString() {
return "QryPerson [id=" + id + ", name=" + name + "]";
return "PersonListEntry [id=" + id + ", name=" + name + "]";
}
}

View File

@@ -1,4 +1,4 @@
package org.fuin.cqrs4j.example.spring.query.handler;
package org.fuin.cqrs4j.example.spring.query.views.personlist;
import javax.annotation.concurrent.NotThreadSafe;
@@ -17,15 +17,15 @@ import org.springframework.transaction.annotation.Transactional;
*/
@NotThreadSafe
@Component
public class PersonEventChunkHandler {
public class PersonListEventChunkHandler {
private static final Logger LOG = LoggerFactory.getLogger(PersonEventChunkHandler.class);
private static final Logger LOG = LoggerFactory.getLogger(PersonListEventChunkHandler.class);
/** Unique name of the event store projection that is used. */
public static final ProjectionStreamId PROJECTION_STREAM_ID = new ProjectionStreamId("qry-person-stream");
public static final ProjectionStreamId PROJECTION_STREAM_ID = new ProjectionStreamId("spring-qry-person-stream");
@Autowired
private PersonEventDispatcher dispatcher;
private PersonListEventDispatcher dispatcher;
@Autowired
private ProjectionService projectionService;

View File

@@ -1,4 +1,4 @@
package org.fuin.cqrs4j.example.spring.query.handler;
package org.fuin.cqrs4j.example.spring.query.views.personlist;
import java.util.List;
import java.util.Set;
@@ -8,26 +8,25 @@ import javax.validation.constraints.NotNull;
import org.fuin.cqrs4j.EventDispatcher;
import org.fuin.cqrs4j.SimpleEventDispatcher;
import org.fuin.cqrs4j.example.spring.query.domain.QryPerson;
import org.fuin.ddd4j.ddd.Event;
import org.fuin.ddd4j.ddd.EventType;
import org.fuin.esc.api.CommonEvent;
import org.springframework.stereotype.Component;
/**
* Dispatches events that relate to the {@link QryPerson} entity to the appropriate
* Dispatches events that relate to the {@link PersonListEntry} entity to the appropriate
* event handers.
*/
@NotThreadSafe
@Component
public class PersonEventDispatcher implements EventDispatcher {
public class PersonListEventDispatcher implements EventDispatcher {
private SimpleEventDispatcher delegate;
/**
* Default constructor.
*/
public PersonEventDispatcher(final PersonCreatedEventHandler createdHandler) {
public PersonListEventDispatcher(final PersonCreatedEventHandler createdHandler) {
super();
delegate = new SimpleEventDispatcher(createdHandler);
}

View File

@@ -1,7 +1,7 @@
package org.fuin.cqrs4j.example.spring.query.handler;
package org.fuin.cqrs4j.example.spring.query.views.personlist;
import static org.fuin.cqrs4j.Cqrs4JUtils.tryLocked;
import static org.fuin.cqrs4j.example.spring.query.handler.PersonEventChunkHandler.PROJECTION_STREAM_ID;
import static org.fuin.cqrs4j.example.spring.query.views.personlist.PersonListEventChunkHandler.PROJECTION_STREAM_ID;
import java.util.ArrayList;
import java.util.List;
@@ -32,9 +32,9 @@ import org.springframework.stereotype.Component;
*/
@ThreadSafe
@Component
public class PersonProjector {
public class PersonListProjector {
private static final Logger LOG = LoggerFactory.getLogger(PersonProjector.class);
private static final Logger LOG = LoggerFactory.getLogger(PersonListProjector.class);
/** Prevents more than one projector thread running at a time. */
private static final Semaphore LOCK = new Semaphore(1);
@@ -51,10 +51,10 @@ public class PersonProjector {
private EventStore eventStore;
@Autowired
private PersonEventChunkHandler chunkHandler;
private PersonListEventChunkHandler chunkHandler;
@Autowired
private PersonEventDispatcher dispatcher;
private PersonListEventDispatcher dispatcher;
/**
* Runs triggered by the timer. If a second timer event occurs while the
@@ -62,7 +62,7 @@ public class PersonProjector {
* skipped.
*/
@Scheduled(fixedRate = 100)
@Async("personProjectorExecutor")
@Async("projectorExecutor")
public void execute() {
if (!APP_STARTED.get()) {
// Do nothing until application started

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.spring.query.views.personlist;
/**
* Classes building the 'person list' view.
*/

View File

@@ -14,13 +14,13 @@ import java.util.UUID;
import javax.persistence.EntityManager;
import org.fuin.cqrs4j.example.shared.PersonCreatedEvent;
import org.fuin.cqrs4j.example.shared.PersonId;
import org.fuin.cqrs4j.example.shared.PersonName;
import org.fuin.cqrs4j.example.spring.query.app.QryApplication;
import org.fuin.cqrs4j.example.spring.query.app.QryConfig;
import org.fuin.cqrs4j.example.spring.query.controller.PersonController;
import org.fuin.cqrs4j.example.spring.query.domain.QryPerson;
import org.fuin.cqrs4j.example.spring.shared.PersonCreatedEvent;
import org.fuin.cqrs4j.example.spring.shared.PersonId;
import org.fuin.cqrs4j.example.spring.shared.PersonName;
import org.fuin.cqrs4j.example.spring.query.views.personlist.PersonListEntry;
import org.fuin.cqrs4j.example.spring.shared.Config;
import org.fuin.esc.api.CommonEvent;
import org.fuin.esc.api.EventId;
import org.fuin.esc.api.EventStore;
@@ -58,7 +58,7 @@ public class PersonControllerIT {
PersonController testee;
@Autowired
QryConfig config;
Config config;
@BeforeEach
public void initRestAssuredMockMvcStandalone() {
@@ -72,7 +72,7 @@ public class PersonControllerIT {
}
public boolean findPerson(final PersonId personId) {
return em.find(QryPerson.class, personId.asString()) != null;
return em.find(PersonListEntry.class, personId.asString()) != null;
}
@Test
@@ -91,16 +91,16 @@ public class PersonControllerIT {
// TEST & VERIFY
final QryPerson person = given().pathParam("id", personId.asString()).when().get("/persons/{id}").then()
.statusCode(200).extract().as(QryPerson.class);
final PersonListEntry person = given().pathParam("id", personId.asString()).when().get("/persons/{id}").then()
.statusCode(200).extract().as(PersonListEntry.class);
assertThat(person.getId(), is(equalTo(personId)));
assertThat(person.getName(), is(equalTo(personName)));
final QryPerson[] persons = given().when().get("/persons").then().statusCode(200).extract()
.as(QryPerson[].class);
final PersonListEntry[] persons = given().when().get("/persons").then().statusCode(200).extract()
.as(PersonListEntry[].class);
assertThat(Arrays.asList(persons), is(not(empty())));
final QryPerson person0 = persons[0];
final PersonListEntry person0 = persons[0];
assertThat(person0.getId(), is(equalTo(personId)));
assertThat(person0.getName(), is(equalTo(personName)));

View File

@@ -4,6 +4,13 @@
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>org.fuin.cqrs4j.example.spring</groupId>
<artifactId>cqrs4j-spring-example-shared</artifactId>
<version>0.1.0-SNAPSHOT</version>
@@ -15,29 +22,45 @@
<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>
<esc.version>0.3.1</esc.version>
</properties>
<dependencies>
<!-- Compile -->
<dependency>
<groupId>org.fuin.cqrs4j.example</groupId>
<artifactId>cqrs4j-example-shared</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.fuin</groupId>
<artifactId>ddd-4-java</artifactId>
<version>0.2.1-SNAPSHOT</version>
<version>0.2.1</version>
</dependency>
<dependency>
<groupId>org.fuin</groupId>
<artifactId>cqrs-4-java</artifactId>
<version>0.2.1-SNAPSHOT</version>
<version>0.2.1</version>
</dependency>
<dependency>
<groupId>org.fuin</groupId>
<artifactId>objects4j</artifactId>
<version>0.6.9-SNAPSHOT</version>
<version>0.6.9</version>
</dependency>
<dependency>
@@ -52,26 +75,20 @@
<version>${esc.version}</version>
</dependency>
<dependency>
<groupId>org.fuin.esc</groupId>
<artifactId>esc-eshttp</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>
@@ -89,7 +106,6 @@
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>2.2.3</version>
</dependency>
<!-- Test -->
@@ -97,21 +113,19 @@
<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>
<version>0.8.4</version>
<scope>test</scope>
</dependency>

View File

@@ -0,0 +1,106 @@
package org.fuin.cqrs4j.example.spring.shared;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.concurrent.Executors;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.eclipse.yasson.FieldAccessStrategy;
import org.fuin.cqrs4j.example.shared.SharedUtils;
import org.fuin.esc.eshttp.ESEnvelopeType;
import org.fuin.esc.eshttp.ESHttpEventStore;
import org.fuin.esc.eshttp.IESHttpEventStore;
import org.fuin.esc.esjc.ESJCEventStore;
import org.fuin.esc.esjc.IESJCEventStore;
import org.fuin.esc.spi.EnhancedMimeType;
import org.fuin.esc.spi.SerDeserializerRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.github.msemys.esjc.EventStoreBuilder;
@Component
public class BeanFactory {
/**
* Creates a Jsonb instance.
*
* @return Fully configured instance.
*/
@Bean
public Jsonb createJsonb() {
final JsonbConfig config = new JsonbConfig().withAdapters(SharedUtils.JSONB_ADAPTERS)
.withPropertyVisibilityStrategy(new FieldAccessStrategy());
final Jsonb jsonb = JsonbBuilder.create(config);
return jsonb;
}
/**
* Creates a TCP based event store connection.
*
* @param config Configuration to use.
*
* @return New event store instance.
*/
@Bean(destroyMethod = "shutdown")
public com.github.msemys.esjc.EventStore getEventStore(final Config config) {
return EventStoreBuilder.newBuilder()
.singleNodeAddress(config.getEventStoreHost(), config.getEventStoreTcpPort())
.executor(Executors.newFixedThreadPool(10))
.userCredentials(config.getEventStoreUser(), config.getEventStorePassword()).build();
}
/**
* Creates an event store connection.
*
* @param config Configuration to use.
*
* @return New event store instance.
*/
@Bean(destroyMethod = "close")
public IESJCEventStore getESJCEventStore(final com.github.msemys.esjc.EventStore es) {
final SerDeserializerRegistry registry = SharedUtils.createRegistry();
final IESJCEventStore eventstore = new ESJCEventStore(es, registry, registry,
EnhancedMimeType.create("application", "json", Charset.forName("utf-8")));
eventstore.open();
return eventstore;
}
/**
* Creates a HTTP based event store connection.
*
* @param config Configuration to use.
*
* @return New event store instance.
*/
@Bean(destroyMethod = "close")
public IESHttpEventStore getESHttpEventStore(final Config config) {
final String url = config.getEventStoreProtocol() + "://" + config.getEventStoreHost() + ":"
+ config.getEventStoreHttpPort();
try {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(config.getEventStoreUser(),
config.getEventStorePassword());
credentialsProvider.setCredentials(AuthScope.ANY, credentials);
final SerDeserializerRegistry registry = SharedUtils.createRegistry();
final ESHttpEventStore es = new ESHttpEventStore(Executors.defaultThreadFactory(), new URL(url),
ESEnvelopeType.JSON, registry, registry, credentialsProvider);
es.open();
return es;
} catch (final MalformedURLException ex) {
throw new RuntimeException("Failed to create URL: " + url, ex);
}
}
}

View File

@@ -1,10 +1,10 @@
package org.fuin.cqrs4j.example.spring.command.app;
package org.fuin.cqrs4j.example.spring.shared;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class CmdConfig {
public class Config {
private static final String EVENT_STORE_PROTOCOL = "http";
@@ -39,7 +39,7 @@ public class CmdConfig {
/**
* Constructor using default values internally.
*/
public CmdConfig() {
public Config() {
super();
this.eventStoreProtocol = EVENT_STORE_PROTOCOL;
this.eventStoreHost = EVENT_STORE_HOST;
@@ -59,7 +59,7 @@ public class CmdConfig {
* @param eventStoreUser User.
* @param eventStorePassword Password.
*/
public CmdConfig(final String eventStoreProtocol, final String eventStoreHost, final int eventStoreHttpPort,
public Config(final String eventStoreProtocol, final String eventStoreHost, final int eventStoreHttpPort,
final int eventStoreTcpPort, final String eventStoreUser, final String eventStorePassword) {
super();
this.eventStoreProtocol = eventStoreProtocol;

View File

@@ -1,88 +0,0 @@
/**
* 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.spring.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

@@ -10,7 +10,7 @@
* 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.spring.command.controller;
package org.fuin.cqrs4j.example.spring.shared;
import java.util.ArrayList;
import java.util.List;
@@ -22,7 +22,6 @@ import javax.validation.ConstraintViolationException;
import org.fuin.cqrs4j.CommandExecutionFailedException;
import org.fuin.cqrs4j.SimpleResult;
import org.fuin.cqrs4j.example.spring.command.domain.DuplicatePersonNameException;
import org.fuin.ddd4j.ddd.AggregateAlreadyExistsException;
import org.fuin.ddd4j.ddd.AggregateDeletedException;
import org.fuin.ddd4j.ddd.AggregateNotFoundException;
@@ -123,18 +122,6 @@ public class GlobalExceptionHandler {
}
@ExceptionHandler(value = DuplicatePersonNameException.class)
public ResponseEntity<SimpleResult> exception(final DuplicatePersonNameException ex) {
LOG.info("{} {}", ex.getShortId(), ex.getMessage());
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<>(SimpleResult.error(ex.getShortId(), ex.getMessage()), headers, HttpStatus.CONFLICT);
}
@ExceptionHandler(value = ConstraintViolationException.class)
public ResponseEntity<SimpleResult> exception(final ConstraintViolationException ex) {

View File

@@ -1,87 +0,0 @@
/**
* 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.spring.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

@@ -1,148 +0,0 @@
/**
* 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.spring.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

@@ -1,216 +0,0 @@
/**
* 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.spring.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

@@ -1,70 +0,0 @@
/**
* 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.spring.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

@@ -1,189 +0,0 @@
/**
* 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.spring.shared;
import java.nio.charset.Charset;
import javax.json.bind.adapter.JsonbAdapter;
import org.eclipse.yasson.FieldAccessStrategy;
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 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

@@ -13,5 +13,5 @@
package org.fuin.cqrs4j.example.spring.shared;
/**
* Domain specific code to be shared between all modules.
* Spring Boot specific code to be shared between all modules.
*/

View File

@@ -1,108 +0,0 @@
/**
* 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.spring.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

@@ -1,108 +0,0 @@
/**
* 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.spring.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

@@ -1,91 +0,0 @@
/**
* 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.spring.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

@@ -1,134 +0,0 @@
/**
* 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.spring.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();
}
}