Refactored the codebase.
This commit is contained in:
parent
bae59eade9
commit
dbbcfc2118
@ -3,7 +3,7 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.sapient.learning</groupId>
|
||||
<artifactId>java-events</artifactId>
|
||||
<artifactId>java-es-cqrs</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>java-es-cqrs</name>
|
||||
<properties>
|
||||
@ -16,5 +16,12 @@
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
@ -3,6 +3,7 @@ package com.baeldung.patterns.cqrs.projectors;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import com.baeldung.patterns.cqrs.repository.UserReadRepository;
|
||||
@ -21,36 +22,28 @@ public class UserProjector {
|
||||
}
|
||||
|
||||
public void project(User user) {
|
||||
UserContact userContact = readRepository.getUserContact(user.getUserid());
|
||||
if (userContact == null)
|
||||
userContact = new UserContact();
|
||||
userContact.setContacts(user.getContacts());
|
||||
UserContact userContact = Optional.ofNullable(readRepository.getUserContact(user.getUserid()))
|
||||
.orElse(new UserContact());
|
||||
Map<String, Set<Contact>> contactByType = new HashMap<>();
|
||||
for (Contact contact : user.getContacts()) {
|
||||
Set<Contact> contacts = contactByType.get(contact.getType());
|
||||
if (contacts == null)
|
||||
contacts = new HashSet<>();
|
||||
Set<Contact> contacts = Optional.ofNullable(contactByType.get(contact.getType()))
|
||||
.orElse(new HashSet<>());
|
||||
contacts.add(contact);
|
||||
contactByType.put(contact.getType(), contacts);
|
||||
}
|
||||
userContact.setContactByType(contactByType);
|
||||
readRepository.addUserContact(user.getUserid(), userContact);
|
||||
|
||||
UserAddress userAddress = readRepository.getUserAddress(user.getUserid());
|
||||
if (userAddress == null)
|
||||
userAddress = new UserAddress();
|
||||
userAddress.setAddresses(user.getAddresses());
|
||||
UserAddress userAddress = Optional.ofNullable(readRepository.getUserAddress(user.getUserid()))
|
||||
.orElse(new UserAddress());
|
||||
Map<String, Set<Address>> addressByRegion = new HashMap<>();
|
||||
for (Address address : user.getAddresses()) {
|
||||
Set<Address> addresses = addressByRegion.get(address.getState());
|
||||
if (addresses == null)
|
||||
addresses = new HashSet<>();
|
||||
Set<Address> addresses = Optional.ofNullable(addressByRegion.get(address.getState()))
|
||||
.orElse(new HashSet<>());
|
||||
addresses.add(address);
|
||||
addressByRegion.put(address.getState(), addresses);
|
||||
}
|
||||
userAddress.setAddressByRegion(addressByRegion);
|
||||
readRepository.addUserAddress(user.getUserid(), userAddress);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,48 +0,0 @@
|
||||
package com.baeldung.patterns.crud;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.baeldung.patterns.crud.repository.UserRepository;
|
||||
import com.baeldung.patterns.crud.service.UserService;
|
||||
import com.baeldung.patterns.domain.Address;
|
||||
import com.baeldung.patterns.domain.Contact;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
UserRepository repository = new UserRepository();
|
||||
UserService service = new UserService(repository);
|
||||
String userId = UUID.randomUUID()
|
||||
.toString();
|
||||
|
||||
service.createUser(userId, "Tom", "Sawyer");
|
||||
service.updateUser(userId,
|
||||
Stream.of(
|
||||
new Contact("EMAIL", "tom.sawyer@gmail.com"),
|
||||
new Contact("EMAIL", "tom.sawyer@rediff.com"),
|
||||
new Contact("PHONE", "700-000-0001"))
|
||||
.collect(Collectors.toSet()),
|
||||
Stream.of(
|
||||
new Address("New York", "NY", "10001"),
|
||||
new Address("Los Angeles", "CA", "90001"),
|
||||
new Address("Housten", "TX", "77001"))
|
||||
.collect(Collectors.toSet()));
|
||||
service.updateUser(userId,
|
||||
Stream.of(
|
||||
new Contact("EMAIL", "tom.sawyer@gmail.com"),
|
||||
new Contact("PHONE", "700-000-0001"))
|
||||
.collect(Collectors.toSet()),
|
||||
Stream.of(
|
||||
new Address("New York", "NY", "10001"),
|
||||
new Address("Housten", "TX", "77001"))
|
||||
.collect(Collectors.toSet()));
|
||||
|
||||
System.out.println(service.getContactByType(userId, "EMAIL"));
|
||||
System.out.println(service.getAddressByRegion(userId, "NY"));
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package com.baeldung.patterns.domain;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ -10,7 +9,6 @@ import lombok.Data;
|
||||
@Data
|
||||
public class UserAddress {
|
||||
|
||||
private Set<Address> addresses = new HashSet<>();
|
||||
private Map<String, Set<Address>> addressByRegion = new HashMap<>();
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.baeldung.patterns.domain;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ -10,7 +9,6 @@ import lombok.Data;
|
||||
@Data
|
||||
public class UserContact {
|
||||
|
||||
private Set<Contact> contacts = new HashSet<>();
|
||||
private Map<String, Set<Contact>> contactByType = new HashMap<>();
|
||||
|
||||
}
|
||||
|
@ -1,48 +0,0 @@
|
||||
package com.baeldung.patterns.es;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.baeldung.patterns.domain.Address;
|
||||
import com.baeldung.patterns.domain.Contact;
|
||||
import com.baeldung.patterns.es.repository.EventStore;
|
||||
import com.baeldung.patterns.es.service.UserService;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
EventStore repository = new EventStore();
|
||||
UserService service = new UserService(repository);
|
||||
String userId = UUID.randomUUID()
|
||||
.toString();
|
||||
|
||||
service.createUser(userId, "Tom", "Sawyer");
|
||||
service.updateUser(userId,
|
||||
Stream.of(
|
||||
new Contact("EMAIL", "tom.sawyer@gmail.com"),
|
||||
new Contact("EMAIL", "tom.sawyer@rediff.com"),
|
||||
new Contact("PHONE", "700-000-0001"))
|
||||
.collect(Collectors.toSet()),
|
||||
Stream.of(
|
||||
new Address("New York", "NY", "10001"),
|
||||
new Address("Los Angeles", "CA", "90001"),
|
||||
new Address("Housten", "TX", "77001"))
|
||||
.collect(Collectors.toSet()));
|
||||
service.updateUser(userId,
|
||||
Stream.of(
|
||||
new Contact("EMAIL", "tom.sawyer@gmail.com"),
|
||||
new Contact("PHONE", "700-000-0001"))
|
||||
.collect(Collectors.toSet()),
|
||||
Stream.of(
|
||||
new Address("New York", "NY", "10001"),
|
||||
new Address("Housten", "TX", "77001"))
|
||||
.collect(Collectors.toSet()));
|
||||
|
||||
System.out.println(service.getContactByType(userId, "EMAIL"));
|
||||
System.out.println(service.getAddressByRegion(userId, "NY"));
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package com.baeldung.patterns.es.events;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class UserRemovedEvent extends Event {
|
||||
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
package com.baeldung.patterns.es.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -23,8 +22,7 @@ public class UserService {
|
||||
}
|
||||
|
||||
public void createUser(String userId, String firstName, String lastName) {
|
||||
UserCreatedEvent event = new UserCreatedEvent(userId, firstName, lastName);
|
||||
repository.addEvent(userId, event);
|
||||
repository.addEvent(userId, new UserCreatedEvent(userId, firstName, lastName));
|
||||
}
|
||||
|
||||
public void updateUser(String userId, Set<Contact> contacts, Set<Address> addresses) throws Exception {
|
||||
@ -32,49 +30,30 @@ public class UserService {
|
||||
if (user == null)
|
||||
throw new Exception("User does not exist.");
|
||||
|
||||
List<Contact> contactsToRemove = user.getContacts()
|
||||
user.getContacts()
|
||||
.stream()
|
||||
.filter(c -> !contacts.contains(c))
|
||||
.collect(Collectors.toList());
|
||||
for (Contact contact : contactsToRemove) {
|
||||
UserContactRemovedEvent contactRemovedEvent = new UserContactRemovedEvent(contact.getType(), contact.getDetail());
|
||||
repository.addEvent(userId, contactRemovedEvent);
|
||||
}
|
||||
|
||||
List<Contact> contactsToAdd = contacts.stream()
|
||||
.forEach(c -> repository.addEvent(userId, new UserContactRemovedEvent(c.getType(), c.getDetail())));
|
||||
contacts.stream()
|
||||
.filter(c -> !user.getContacts()
|
||||
.contains(c))
|
||||
.collect(Collectors.toList());
|
||||
for (Contact contact : contactsToAdd) {
|
||||
UserContactAddedEvent contactAddedEvent = new UserContactAddedEvent(contact.getType(), contact.getDetail());
|
||||
repository.addEvent(userId, contactAddedEvent);
|
||||
}
|
||||
|
||||
List<Address> addressesToRemove = user.getAddresses()
|
||||
.forEach(c -> repository.addEvent(userId, new UserContactAddedEvent(c.getType(), c.getDetail())));
|
||||
user.getAddresses()
|
||||
.stream()
|
||||
.filter(a -> !addresses.contains(a))
|
||||
.collect(Collectors.toList());
|
||||
for (Address address : addressesToRemove) {
|
||||
UserAddressRemovedEvent addressRemovedEvent = new UserAddressRemovedEvent(address.getCity(), address.getState(), address.getPostcode());
|
||||
repository.addEvent(userId, addressRemovedEvent);
|
||||
}
|
||||
|
||||
List<Address> addressesToAdd = addresses.stream()
|
||||
.forEach(a -> repository.addEvent(userId, new UserAddressRemovedEvent(a.getCity(), a.getState(), a.getPostcode())));
|
||||
addresses.stream()
|
||||
.filter(a -> !user.getAddresses()
|
||||
.contains(a))
|
||||
.collect(Collectors.toList());
|
||||
for (Address address : addressesToAdd) {
|
||||
UserAddressAddedEvent addressAddedEvent = new UserAddressAddedEvent(address.getCity(), address.getState(), address.getPostcode());
|
||||
repository.addEvent(userId, addressAddedEvent);
|
||||
}
|
||||
.forEach(a -> repository.addEvent(userId, new UserAddressAddedEvent(a.getCity(), a.getState(), a.getPostcode())));
|
||||
}
|
||||
|
||||
public Set<Contact> getContactByType(String userId, String contactType) throws Exception {
|
||||
User user = UserUtility.recreateUserState(repository, userId);
|
||||
if (user == null)
|
||||
throw new Exception("User does not exist.");
|
||||
Set<Contact> contacts = user.getContacts();
|
||||
return contacts.stream()
|
||||
return user.getContacts()
|
||||
.stream()
|
||||
.filter(c -> c.getType()
|
||||
.equals(contactType))
|
||||
.collect(Collectors.toSet());
|
||||
@ -84,11 +63,10 @@ public class UserService {
|
||||
User user = UserUtility.recreateUserState(repository, userId);
|
||||
if (user == null)
|
||||
throw new Exception("User does not exist.");
|
||||
Set<Address> addresses = user.getAddresses();
|
||||
return addresses.stream()
|
||||
return user.getAddresses()
|
||||
.stream()
|
||||
.filter(a -> a.getState()
|
||||
.equals(state))
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,7 +12,6 @@ import com.baeldung.patterns.es.events.UserAddressRemovedEvent;
|
||||
import com.baeldung.patterns.es.events.UserContactAddedEvent;
|
||||
import com.baeldung.patterns.es.events.UserContactRemovedEvent;
|
||||
import com.baeldung.patterns.es.events.UserCreatedEvent;
|
||||
import com.baeldung.patterns.es.events.UserRemovedEvent;
|
||||
import com.baeldung.patterns.es.repository.EventStore;
|
||||
|
||||
public class UserUtility {
|
||||
@ -27,9 +26,6 @@ public class UserUtility {
|
||||
user = new User(UUID.randomUUID()
|
||||
.toString(), e.getFirstName(), e.getLastName());
|
||||
}
|
||||
if (event instanceof UserRemovedEvent) {
|
||||
user = null;
|
||||
}
|
||||
if (event instanceof UserAddressAddedEvent) {
|
||||
UserAddressAddedEvent e = (UserAddressAddedEvent) event;
|
||||
Address address = new Address(e.getCity(), e.getState(), e.getPostCode());
|
||||
|
@ -2,6 +2,7 @@ package com.baeldung.patterns.escqrs.projectors;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import com.baeldung.patterns.cqrs.repository.UserReadRepository;
|
||||
@ -14,8 +15,6 @@ import com.baeldung.patterns.es.events.UserAddressAddedEvent;
|
||||
import com.baeldung.patterns.es.events.UserAddressRemovedEvent;
|
||||
import com.baeldung.patterns.es.events.UserContactAddedEvent;
|
||||
import com.baeldung.patterns.es.events.UserContactRemovedEvent;
|
||||
import com.baeldung.patterns.es.events.UserCreatedEvent;
|
||||
import com.baeldung.patterns.es.events.UserRemovedEvent;
|
||||
|
||||
public class UserProjector {
|
||||
|
||||
@ -28,10 +27,6 @@ public class UserProjector {
|
||||
public void project(String userId, List<Event> events) {
|
||||
|
||||
for (Event event : events) {
|
||||
if (event instanceof UserCreatedEvent)
|
||||
apply(userId, (UserCreatedEvent) event);
|
||||
if (event instanceof UserRemovedEvent)
|
||||
apply(userId, (UserRemovedEvent) event);
|
||||
if (event instanceof UserAddressAddedEvent)
|
||||
apply(userId, (UserAddressAddedEvent) event);
|
||||
if (event instanceof UserAddressRemovedEvent)
|
||||
@ -44,25 +39,13 @@ public class UserProjector {
|
||||
|
||||
}
|
||||
|
||||
public void apply(String userId, UserCreatedEvent event) {
|
||||
|
||||
}
|
||||
|
||||
public void apply(String userId, UserRemovedEvent event) {
|
||||
|
||||
}
|
||||
|
||||
public void apply(String userId, UserAddressAddedEvent event) {
|
||||
Address address = new Address(event.getCity(), event.getState(), event.getPostCode());
|
||||
UserAddress userAddress = readRepository.getUserAddress(userId);
|
||||
if (userAddress == null)
|
||||
userAddress = new UserAddress();
|
||||
userAddress.getAddresses()
|
||||
.add(address);
|
||||
Set<Address> addresses = userAddress.getAddressByRegion()
|
||||
.get(address.getState());
|
||||
if (addresses == null)
|
||||
addresses = new HashSet<>();
|
||||
UserAddress userAddress = Optional.ofNullable(readRepository.getUserAddress(userId))
|
||||
.orElse(new UserAddress());
|
||||
Set<Address> addresses = Optional.ofNullable(userAddress.getAddressByRegion()
|
||||
.get(address.getState()))
|
||||
.orElse(new HashSet<>());
|
||||
addresses.add(address);
|
||||
userAddress.getAddressByRegion()
|
||||
.put(address.getState(), addresses);
|
||||
@ -73,30 +56,21 @@ public class UserProjector {
|
||||
Address address = new Address(event.getCity(), event.getState(), event.getPostCode());
|
||||
UserAddress userAddress = readRepository.getUserAddress(userId);
|
||||
if (userAddress != null) {
|
||||
userAddress.getAddresses()
|
||||
.remove(address);
|
||||
Set<Address> addresses = userAddress.getAddressByRegion()
|
||||
.get(address.getState());
|
||||
if (addresses != null) {
|
||||
if (addresses != null)
|
||||
addresses.remove(address);
|
||||
userAddress.getAddressByRegion()
|
||||
.put(address.getState(), addresses);
|
||||
}
|
||||
readRepository.addUserAddress(userId, userAddress);
|
||||
}
|
||||
}
|
||||
|
||||
public void apply(String userId, UserContactAddedEvent event) {
|
||||
Contact contact = new Contact(event.getContactType(), event.getContactDetails());
|
||||
UserContact userContact = readRepository.getUserContact(userId);
|
||||
if (userContact == null)
|
||||
userContact = new UserContact();
|
||||
userContact.getContacts()
|
||||
.add(contact);
|
||||
Set<Contact> contacts = userContact.getContactByType()
|
||||
.get(contact.getType());
|
||||
if (contacts == null)
|
||||
contacts = new HashSet<>();
|
||||
UserContact userContact = Optional.ofNullable(readRepository.getUserContact(userId))
|
||||
.orElse(new UserContact());
|
||||
Set<Contact> contacts = Optional.ofNullable(userContact.getContactByType()
|
||||
.get(contact.getType()))
|
||||
.orElse(new HashSet<>());
|
||||
contacts.add(contact);
|
||||
userContact.getContactByType()
|
||||
.put(contact.getType(), contacts);
|
||||
@ -107,17 +81,11 @@ public class UserProjector {
|
||||
Contact contact = new Contact(event.getContactType(), event.getContactDetails());
|
||||
UserContact userContact = readRepository.getUserContact(userId);
|
||||
if (userContact != null) {
|
||||
userContact.getContacts()
|
||||
.remove(contact);
|
||||
Set<Contact> contacts = userContact.getContactByType()
|
||||
.get(contact.getType());
|
||||
if (contacts != null) {
|
||||
if (contacts != null)
|
||||
contacts.remove(contact);
|
||||
userContact.getContactByType()
|
||||
.put(contact.getType(), contacts);
|
||||
}
|
||||
readRepository.addUserContact(userId, userContact);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,9 +1,14 @@
|
||||
package com.baeldung.patterns.cqrs;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.patterns.cqrs.aggregates.UserAggregate;
|
||||
import com.baeldung.patterns.cqrs.commands.CreateUserCommand;
|
||||
import com.baeldung.patterns.cqrs.commands.UpdateUserCommand;
|
||||
@ -17,15 +22,25 @@ import com.baeldung.patterns.domain.Address;
|
||||
import com.baeldung.patterns.domain.Contact;
|
||||
import com.baeldung.patterns.domain.User;
|
||||
|
||||
public class Main {
|
||||
public class ApplicationUnitTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
UserWriteRepository writeRepository = new UserWriteRepository();
|
||||
UserReadRepository readRepository = new UserReadRepository();
|
||||
UserProjector projector = new UserProjector(readRepository);
|
||||
UserAggregate userAggregate = new UserAggregate(writeRepository);
|
||||
UserProjection userProjection = new UserProjection(readRepository);
|
||||
private UserWriteRepository writeRepository;
|
||||
private UserReadRepository readRepository;
|
||||
private UserProjector projector;
|
||||
private UserAggregate userAggregate;
|
||||
private UserProjection userProjection;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
writeRepository = new UserWriteRepository();
|
||||
readRepository = new UserReadRepository();
|
||||
projector = new UserProjector(readRepository);
|
||||
userAggregate = new UserAggregate(writeRepository);
|
||||
userProjection = new UserProjection(readRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplication() throws Exception {
|
||||
String userId = UUID.randomUUID()
|
||||
.toString();
|
||||
User user = null;
|
||||
@ -47,11 +62,13 @@ public class Main {
|
||||
user = userAggregate.handleUpdateUserCommand(updateUserCommand);
|
||||
projector.project(user);
|
||||
|
||||
AddressByRegionQuery addressByRegionQuery = new AddressByRegionQuery(user.getUserid(), "NY");
|
||||
System.out.println(userProjection.handle(addressByRegionQuery));
|
||||
ContactByTypeQuery contactByTypeQuery = new ContactByTypeQuery(userId, "EMAIL");
|
||||
assertEquals(Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"))
|
||||
.collect(Collectors.toSet()), userProjection.handle(contactByTypeQuery));
|
||||
AddressByRegionQuery addressByRegionQuery = new AddressByRegionQuery(userId, "NY");
|
||||
assertEquals(Stream.of(new Address("New York", "NY", "10001"))
|
||||
.collect(Collectors.toSet()), userProjection.handle(addressByRegionQuery));
|
||||
|
||||
ContactByTypeQuery contactByTypeQuery = new ContactByTypeQuery(user.getUserid(), "EMAIL");
|
||||
System.out.println(userProjection.handle(contactByTypeQuery));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.baeldung.patterns.crud;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.patterns.crud.repository.UserRepository;
|
||||
import com.baeldung.patterns.crud.service.UserService;
|
||||
import com.baeldung.patterns.domain.Address;
|
||||
import com.baeldung.patterns.domain.Contact;
|
||||
|
||||
public class ApplicationUnitTest {
|
||||
|
||||
private UserRepository repository;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
repository = new UserRepository();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplication() throws Exception {
|
||||
UserService service = new UserService(repository);
|
||||
String userId = UUID.randomUUID()
|
||||
.toString();
|
||||
|
||||
service.createUser(userId, "Tom", "Sawyer");
|
||||
service.updateUser(userId, Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("EMAIL", "tom.sawyer@rediff.com"), new Contact("PHONE", "700-000-0001"))
|
||||
.collect(Collectors.toSet()),
|
||||
Stream.of(new Address("New York", "NY", "10001"), new Address("Los Angeles", "CA", "90001"), new Address("Housten", "TX", "77001"))
|
||||
.collect(Collectors.toSet()));
|
||||
service.updateUser(userId, Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("PHONE", "700-000-0001"))
|
||||
.collect(Collectors.toSet()),
|
||||
Stream.of(new Address("New York", "NY", "10001"), new Address("Housten", "TX", "77001"))
|
||||
.collect(Collectors.toSet()));
|
||||
|
||||
assertEquals(Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"))
|
||||
.collect(Collectors.toSet()), service.getContactByType(userId, "EMAIL"));
|
||||
assertEquals(Stream.of(new Address("New York", "NY", "10001"))
|
||||
.collect(Collectors.toSet()), service.getAddressByRegion(userId, "NY"));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.baeldung.patterns.es;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.patterns.domain.Address;
|
||||
import com.baeldung.patterns.domain.Contact;
|
||||
import com.baeldung.patterns.es.repository.EventStore;
|
||||
import com.baeldung.patterns.es.service.UserService;
|
||||
|
||||
public class ApplicationUnitTest {
|
||||
|
||||
private EventStore repository;
|
||||
private UserService service;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
repository = new EventStore();
|
||||
service = new UserService(repository);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplication() throws Exception {
|
||||
String userId = UUID.randomUUID()
|
||||
.toString();
|
||||
|
||||
service.createUser(userId, "Tom", "Sawyer");
|
||||
service.updateUser(userId, Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("EMAIL", "tom.sawyer@rediff.com"), new Contact("PHONE", "700-000-0001"))
|
||||
.collect(Collectors.toSet()),
|
||||
Stream.of(new Address("New York", "NY", "10001"), new Address("Los Angeles", "CA", "90001"), new Address("Housten", "TX", "77001"))
|
||||
.collect(Collectors.toSet()));
|
||||
service.updateUser(userId, Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"), new Contact("PHONE", "700-000-0001"))
|
||||
.collect(Collectors.toSet()),
|
||||
Stream.of(new Address("New York", "NY", "10001"), new Address("Housten", "TX", "77001"))
|
||||
.collect(Collectors.toSet()));
|
||||
|
||||
assertEquals(Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"))
|
||||
.collect(Collectors.toSet()), service.getContactByType(userId, "EMAIL"));
|
||||
assertEquals(Stream.of(new Address("New York", "NY", "10001"))
|
||||
.collect(Collectors.toSet()), service.getAddressByRegion(userId, "NY"));
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,10 +1,15 @@
|
||||
package com.baeldung.patterns.escqrs;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.patterns.cqrs.commands.CreateUserCommand;
|
||||
import com.baeldung.patterns.cqrs.commands.UpdateUserCommand;
|
||||
import com.baeldung.patterns.cqrs.projections.UserProjection;
|
||||
@ -18,16 +23,25 @@ import com.baeldung.patterns.es.repository.EventStore;
|
||||
import com.baeldung.patterns.escqrs.aggregates.UserAggregate;
|
||||
import com.baeldung.patterns.escqrs.projectors.UserProjector;
|
||||
|
||||
public class Main {
|
||||
public class ApplicationUnitTest {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
private EventStore writeRepository;
|
||||
private UserReadRepository readRepository;
|
||||
private UserProjector projector;
|
||||
private UserAggregate userAggregate;
|
||||
private UserProjection userProjection;
|
||||
|
||||
EventStore writeRepository = new EventStore();
|
||||
UserReadRepository readRepository = new UserReadRepository();
|
||||
UserProjector projector = new UserProjector(readRepository);
|
||||
UserAggregate userAggregate = new UserAggregate(writeRepository);
|
||||
UserProjection userProjection = new UserProjection(readRepository);
|
||||
@Before
|
||||
public void setUp() {
|
||||
writeRepository = new EventStore();
|
||||
readRepository = new UserReadRepository();
|
||||
projector = new UserProjector(readRepository);
|
||||
userAggregate = new UserAggregate(writeRepository);
|
||||
userProjection = new UserProjection(readRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplication() throws Exception {
|
||||
String userId = UUID.randomUUID()
|
||||
.toString();
|
||||
List<Event> events = null;
|
||||
@ -50,11 +64,12 @@ public class Main {
|
||||
events = userAggregate.handleUpdateUserCommand(updateUserCommand);
|
||||
projector.project(userId, events);
|
||||
|
||||
AddressByRegionQuery addressByRegionQuery = new AddressByRegionQuery(userId, "NY");
|
||||
System.out.println(userProjection.handle(addressByRegionQuery));
|
||||
|
||||
ContactByTypeQuery contactByTypeQuery = new ContactByTypeQuery(userId, "EMAIL");
|
||||
System.out.println(userProjection.handle(contactByTypeQuery));
|
||||
assertEquals(Stream.of(new Contact("EMAIL", "tom.sawyer@gmail.com"))
|
||||
.collect(Collectors.toSet()), userProjection.handle(contactByTypeQuery));
|
||||
AddressByRegionQuery addressByRegionQuery = new AddressByRegionQuery(userId, "NY");
|
||||
assertEquals(Stream.of(new Address("New York", "NY", "10001"))
|
||||
.collect(Collectors.toSet()), userProjection.handle(addressByRegionQuery));
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user