From 85f8826741856040cf6705a36b2046abbb6cede2 Mon Sep 17 00:00:00 2001 From: dartpopikyardo Date: Tue, 2 Feb 2016 19:16:29 +0300 Subject: [PATCH 01/10] added customer backend module added auth module added customerId to Account aggregate --- .../backend/commandside/accounts/Account.java | 8 ++- .../commandside/accounts/AccountService.java | 4 +- .../accounts/OpenAccountCommand.java | 7 +- .../commandside/accounts/AccountTest.java | 4 +- java-spring/common-auth/build.gradle | 11 +++ .../banking/commonauth/AuthConfiguration.java | 72 +++++++++++++++++++ .../banking/commonauth/AuthProperties.java | 28 ++++++++ .../TokenAuthenticationService.java | 43 +++++++++++ .../filter/StatelessAuthenticationFilter.java | 32 +++++++++ .../banking/commonauth/model/User.java | 71 ++++++++++++++++++ .../commonauth/model/UserAuthentication.java | 54 ++++++++++++++ .../common/accounts/AccountOpenedEvent.java | 8 ++- .../backend/common/customers/Address.java | 64 +++++++++++++++++ .../customers/CustomerCreatedEvent.java | 46 ++++++++++++ .../common/customers/package-info.java | 2 + .../build.gradle | 11 +++ .../customers/CreateCustomerCommand.java | 31 ++++++++ .../commandside/customers/Customer.java | 29 ++++++++ .../customers/CustomerCommand.java | 6 ++ java-spring/settings.gradle | 3 +- 20 files changed, 527 insertions(+), 7 deletions(-) create mode 100644 java-spring/common-auth/build.gradle create mode 100755 java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/AuthConfiguration.java create mode 100755 java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/AuthProperties.java create mode 100755 java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/TokenAuthenticationService.java create mode 100755 java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/filter/StatelessAuthenticationFilter.java create mode 100755 java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/User.java create mode 100755 java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/UserAuthentication.java create mode 100644 java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/Address.java create mode 100644 java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java create mode 100644 java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/package-info.java create mode 100644 java-spring/customer-command-side-backend/build.gradle create mode 100644 java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java create mode 100644 java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java create mode 100644 java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerCommand.java diff --git a/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/Account.java b/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/Account.java index 882e80a..95982c5 100644 --- a/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/Account.java +++ b/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/Account.java @@ -13,10 +13,11 @@ import java.util.List; public class Account extends ReflectiveMutableCommandProcessingAggregate { + private String customerId; private BigDecimal balance; public List process(OpenAccountCommand cmd) { - return EventUtil.events(new AccountOpenedEvent(cmd.getInitialBalance())); + return EventUtil.events(new AccountOpenedEvent(cmd.getCustomerId(), cmd.getInitialBalance())); } public List process(DebitAccountCommand cmd) { @@ -31,6 +32,7 @@ public class Account extends ReflectiveMutableCommandProcessingAggregate> openAccount(BigDecimal initialBalance) { - return accountRepository.save(new OpenAccountCommand(initialBalance)); + public rx.Observable> openAccount(String customerId, BigDecimal initialBalance) { + return accountRepository.save(new OpenAccountCommand(customerId, initialBalance)); } } diff --git a/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/OpenAccountCommand.java b/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/OpenAccountCommand.java index 5c59192..428bf55 100644 --- a/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/OpenAccountCommand.java +++ b/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/OpenAccountCommand.java @@ -5,13 +5,18 @@ import java.math.BigDecimal; public class OpenAccountCommand implements AccountCommand { + private String customerId; private BigDecimal initialBalance; - public OpenAccountCommand(BigDecimal initialBalance) { + public OpenAccountCommand(String customerId, BigDecimal initialBalance) { this.initialBalance = initialBalance; } public BigDecimal getInitialBalance() { return initialBalance; } + + public String getCustomerId() { + return customerId; + } } diff --git a/java-spring/accounts-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/AccountTest.java b/java-spring/accounts-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/AccountTest.java index f3246e3..867382b 100644 --- a/java-spring/accounts-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/AccountTest.java +++ b/java-spring/accounts-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/AccountTest.java @@ -14,14 +14,16 @@ public class AccountTest { @Test public void testSomething() { Account account = new Account(); + String customerId = "00000000-00000000"; BigDecimal initialBalance = new BigDecimal(512); - List events = CommandProcessingAggregates.processToList(account, (AccountCommand)new OpenAccountCommand(initialBalance)); + List events = CommandProcessingAggregates.processToList(account, (AccountCommand)new OpenAccountCommand(customerId, initialBalance)); Assert.assertEquals(1, events.size()); Assert.assertEquals(AccountOpenedEvent.class, events.get(0).getClass()); account.applyEvent(events.get(0)); + Assert.assertEquals(customerId, account.getCustomerId()); Assert.assertEquals(initialBalance, account.getBalance()); } } diff --git a/java-spring/common-auth/build.gradle b/java-spring/common-auth/build.gradle new file mode 100644 index 0000000..a6d3704 --- /dev/null +++ b/java-spring/common-auth/build.gradle @@ -0,0 +1,11 @@ +apply plugin: 'java' + +dependencies { + compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion" + + compile "org.springframework.security:spring-security-config:4.0.2.RELEASE" + compile "org.springframework.security:spring-security-web:4.0.2.RELEASE" + + + testCompile "junit:junit:4.11" +} \ No newline at end of file diff --git a/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/AuthConfiguration.java b/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/AuthConfiguration.java new file mode 100755 index 0000000..2a12c35 --- /dev/null +++ b/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/AuthConfiguration.java @@ -0,0 +1,72 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.commonauth; + +import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.filter.StatelessAuthenticationFilter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpMethod; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.core.token.KeyBasedPersistenceTokenService; +import org.springframework.security.core.token.TokenService; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; + +import java.security.SecureRandom; + +/** + * Created by popikyardo on 21.09.15. + */ +@Configuration +@ComponentScan +@EnableWebSecurity +@EnableConfigurationProperties({AuthProperties.class}) +public class AuthConfiguration extends WebSecurityConfigurerAdapter { + + @Autowired + private AuthProperties securityProperties; + + @Autowired + private TokenAuthenticationService tokenAuthenticationService; + + @Bean + @Override + public AuthenticationManager authenticationManagerBean() throws Exception { + return super.authenticationManagerBean(); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication(); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.csrf().disable() + .formLogin().loginPage("/index.html").and() + .authorizeRequests() + .antMatchers("/health").permitAll() + .antMatchers("/swagger-ui.html").permitAll() + .antMatchers("/v2/api-docs").permitAll() + .antMatchers("/js/**").permitAll() + .antMatchers("/styles/**").permitAll() + .antMatchers("/views/**").permitAll() + .antMatchers(HttpMethod.POST, "/authenticate").permitAll() + .anyRequest().authenticated().and() + .addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService), UsernamePasswordAuthenticationFilter.class); + } + + @Bean + public TokenService tokenService() { + KeyBasedPersistenceTokenService res = new KeyBasedPersistenceTokenService(); + res.setSecureRandom(new SecureRandom()); + res.setServerSecret(securityProperties.getServerSecret()); + res.setServerInteger(securityProperties.getServerInteger()); + + return res; + } +} diff --git a/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/AuthProperties.java b/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/AuthProperties.java new file mode 100755 index 0000000..155b0c9 --- /dev/null +++ b/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/AuthProperties.java @@ -0,0 +1,28 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.commonauth; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Created by popikyardo on 21.09.15. + */ +@ConfigurationProperties(locations = "classpath:auth.properties", ignoreUnknownFields = false, prefix = "auth") +public class AuthProperties { + private String serverSecret; + private Integer serverInteger; + + public String getServerSecret() { + return serverSecret; + } + + public void setServerSecret(String serverSecret) { + this.serverSecret = serverSecret; + } + + public Integer getServerInteger() { + return serverInteger; + } + + public void setServerInteger(Integer serverInteger) { + this.serverInteger = serverInteger; + } +} diff --git a/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/TokenAuthenticationService.java b/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/TokenAuthenticationService.java new file mode 100755 index 0000000..5f6b7de --- /dev/null +++ b/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/TokenAuthenticationService.java @@ -0,0 +1,43 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.commonauth; + +import com.fasterxml.jackson.databind.ObjectMapper; +import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model.User; +import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model.UserAuthentication; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.token.Token; +import org.springframework.security.core.token.TokenService; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; + +/** + * Created by popikyardo on 23.09.15. + */ +@Service +public class TokenAuthenticationService { + + @Autowired + private TokenService tokenService; + + private static final String AUTH_HEADER_NAME = "x-access-token"; + private static final long DAY = 1000 * 60 * 60 * 24; + + private ObjectMapper mapper = new ObjectMapper(); + + public Authentication getAuthentication(HttpServletRequest request) throws IOException { + final String tokenString = request.getHeader(AUTH_HEADER_NAME); + + if (tokenString != null) { + Token token = tokenService.verifyToken(tokenString); + final User user = mapper.readValue(token.getExtendedInformation(), User.class); + + if (user != null && (System.currentTimeMillis() - token.getKeyCreationTime()) < DAY) { + return new UserAuthentication(user); + } + } + return null; + } + +} diff --git a/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/filter/StatelessAuthenticationFilter.java b/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/filter/StatelessAuthenticationFilter.java new file mode 100755 index 0000000..327cfb1 --- /dev/null +++ b/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/filter/StatelessAuthenticationFilter.java @@ -0,0 +1,32 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.commonauth.filter; + +import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.TokenAuthenticationService; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.filter.GenericFilterBean; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; + +/** + * Created by popikyardo on 23.09.15. + */ +public class StatelessAuthenticationFilter extends GenericFilterBean { + + private final TokenAuthenticationService tokenAuthenticationService; + + public StatelessAuthenticationFilter(TokenAuthenticationService taService) { + this.tokenAuthenticationService = taService; + } + + @Override + public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, + ServletException { + SecurityContextHolder.getContext().setAuthentication( + tokenAuthenticationService.getAuthentication((HttpServletRequest) req)); + chain.doFilter(req, res); + } +} \ No newline at end of file diff --git a/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/User.java b/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/User.java new file mode 100755 index 0000000..9e793b4 --- /dev/null +++ b/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/User.java @@ -0,0 +1,71 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +/** + * Created by popikyardo on 23.09.15. + */ +@JsonIgnoreProperties(ignoreUnknown = true) +public class User implements UserDetails { + + private String email; + + public void setUsername(String username) { + this.email = username; + } + + @Override + @JsonIgnore + public Collection getAuthorities() { + SimpleGrantedAuthority authority = new SimpleGrantedAuthority("USER"); + Set res = new HashSet(); + res.add(authority); + return res; + } + + @Override + public String getPassword() { + return ""; + } + + @Override + public String getUsername() { + return this.email; + } + + @Override + public boolean isAccountNonExpired() { + return false; + } + + @Override + public boolean isAccountNonLocked() { + return false; + } + + @Override + public boolean isCredentialsNonExpired() { + return false; + } + + @Override + public boolean isEnabled() { + return false; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/UserAuthentication.java b/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/UserAuthentication.java new file mode 100755 index 0000000..51d8079 --- /dev/null +++ b/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/UserAuthentication.java @@ -0,0 +1,54 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model; + +import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; + +import java.util.Collection; + +/** + * Created by popikyardo on 23.09.15. + */ +public class UserAuthentication implements Authentication { + + private final User user; + private boolean authenticated = true; + + public UserAuthentication(User user) { + this.user = user; + } + + @Override + public String getName() { + return user.getUsername(); + } + + @Override + public Collection getAuthorities() { + return user.getAuthorities(); + } + + @Override + public Object getCredentials() { + return user.getPassword(); + } + + @Override + public User getDetails() { + return user; + } + + @Override + public Object getPrincipal() { + return user.getUsername(); + } + + @Override + public boolean isAuthenticated() { + return authenticated; + } + + @Override + public void setAuthenticated(boolean authenticated) { + this.authenticated = authenticated; + } +} \ No newline at end of file diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/accounts/AccountOpenedEvent.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/accounts/AccountOpenedEvent.java index 6c494bd..1d05768 100644 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/accounts/AccountOpenedEvent.java +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/accounts/AccountOpenedEvent.java @@ -7,15 +7,21 @@ import java.math.BigDecimal; public class AccountOpenedEvent implements Event { + private String customerId; private BigDecimal initialBalance; private AccountOpenedEvent() { } - public AccountOpenedEvent(BigDecimal initialBalance) { + public AccountOpenedEvent(String customerId, BigDecimal initialBalance) { + this.customerId = customerId; this.initialBalance = initialBalance; } + public String getCustomerId() { + return customerId; + } + public BigDecimal getInitialBalance() { return initialBalance; } diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/Address.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/Address.java new file mode 100644 index 0000000..156a105 --- /dev/null +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/Address.java @@ -0,0 +1,64 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers; + +/** + * Created by popikyardo on 02.02.16. + */ +public class Address { + + private String street1; + private String street2; + private String city; + private String state; + private int zipCode; + + public Address() { + } + + public Address(String street1, String street2, String city, String state, int zipCode) { + this.street1 = street1; + this.street2 = street2; + this.city = city; + this.state = state; + this.zipCode = zipCode; + } + + public String getStreet1() { + return street1; + } + + public void setStreet1(String street1) { + this.street1 = street1; + } + + public String getStreet2() { + return street2; + } + + public void setStreet2(String street2) { + this.street2 = street2; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public int getZipCode() { + return zipCode; + } + + public void setZipCode(int zipCode) { + this.zipCode = zipCode; + } +} diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java new file mode 100644 index 0000000..09d15fe --- /dev/null +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java @@ -0,0 +1,46 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers; + +import net.chrisrichardson.eventstore.Event; + +/** + * Created by popikyardo on 02.02.16. + */ +public class CustomerCreatedEvent implements Event { + + private String socialSecurityNum; + private String phoneNum; + private Address address; + + public CustomerCreatedEvent() { + } + + public CustomerCreatedEvent(String socialSecurityNum, String phoneNum, Address address) { + this.socialSecurityNum = socialSecurityNum; + this.phoneNum = phoneNum; + this.address = address; + } + + public String getSocialSecurityNum() { + return socialSecurityNum; + } + + public void setSocialSecurityNum(String socialSecurityNum) { + this.socialSecurityNum = socialSecurityNum; + } + + public String getPhoneNum() { + return phoneNum; + } + + public void setPhoneNum(String phoneNum) { + this.phoneNum = phoneNum; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } +} diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/package-info.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/package-info.java new file mode 100644 index 0000000..ba4aa66 --- /dev/null +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/package-info.java @@ -0,0 +1,2 @@ +@net.chrisrichardson.eventstore.EventEntity(entity="net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.Customer") +package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers; \ No newline at end of file diff --git a/java-spring/customer-command-side-backend/build.gradle b/java-spring/customer-command-side-backend/build.gradle new file mode 100644 index 0000000..1a4bd0d --- /dev/null +++ b/java-spring/customer-command-side-backend/build.gradle @@ -0,0 +1,11 @@ +apply plugin: 'java' + +dependencies { + compile project(":common-backend") + compile "net.chrisrichardson.eventstore.client:eventstore-java-client_2.10:$eventStoreClientVersion" + + testCompile project(":testutil") + testCompile "junit:junit:4.11" + testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVersion" + testCompile "net.chrisrichardson.eventstore.client:eventstore-jdbc_2.10:$eventStoreClientVersion" +} \ No newline at end of file diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java new file mode 100644 index 0000000..96b1ca4 --- /dev/null +++ b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java @@ -0,0 +1,31 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers; + +import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.Address; + +/** + * Created by popikyardo on 02.02.16. + */ +public class CreateCustomerCommand implements CustomerCommand { + + private String socialSecurityNum; + private String phoneNum; + private Address address; + + public CreateCustomerCommand(String socialSecurityNum, String phoneNum, Address address) { + this.socialSecurityNum = socialSecurityNum; + this.phoneNum = phoneNum; + this.address = address; + } + + public String getSocialSecurityNum() { + return socialSecurityNum; + } + + public String getPhoneNum() { + return phoneNum; + } + + public Address getAddress() { + return address; + } +} diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java new file mode 100644 index 0000000..e6a9a28 --- /dev/null +++ b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java @@ -0,0 +1,29 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers; + +import net.chrisrichardson.eventstore.Event; +import net.chrisrichardson.eventstore.EventUtil; +import net.chrisrichardson.eventstore.ReflectiveMutableCommandProcessingAggregate; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.Address; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerCreatedEvent; + +import java.util.List; + +/** + * Created by popikyardo on 02.02.16. + */ +public class Customer extends ReflectiveMutableCommandProcessingAggregate { + private String socialSecurityNum; + private String phoneNum; + private Address address; + + public List process(CreateCustomerCommand cmd) { + return EventUtil.events(new CustomerCreatedEvent(cmd.getSocialSecurityNum(), cmd.getPhoneNum(), cmd.getAddress())); + } + + + public void apply(CustomerCreatedEvent event) { + socialSecurityNum = event.getSocialSecurityNum(); + phoneNum = event.getPhoneNum(); + address = event.getAddress(); + } +} diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerCommand.java b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerCommand.java new file mode 100644 index 0000000..ada1555 --- /dev/null +++ b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerCommand.java @@ -0,0 +1,6 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers; + +import net.chrisrichardson.eventstore.Command; + +interface CustomerCommand extends Command { +} diff --git a/java-spring/settings.gradle b/java-spring/settings.gradle index f69c5a4..a6b3b3a 100644 --- a/java-spring/settings.gradle +++ b/java-spring/settings.gradle @@ -6,6 +6,7 @@ include 'common-backend' include 'accounts-command-side-backend' include 'transactions-command-side-backend' +include 'customer-command-side-backend' include 'accounts-command-side-web' include 'transactions-command-side-web' @@ -24,5 +25,5 @@ include 'transactions-command-side-service' include 'e2e-test' rootProject.name = 'java-spring-event-sourcing-example' - +include 'common-auth' From cbee50658e405abdd68d9cc937c0f32b17f7917c Mon Sep 17 00:00:00 2001 From: dartpopikyardo Date: Wed, 3 Feb 2016 21:03:52 +0300 Subject: [PATCH 02/10] fixed fields names added swagger definition for new endpoints --- .../backend/common/customers/Address.java | 8 +- .../customers/CustomerCreatedEvent.java | 26 +- .../customers/CreateCustomerCommand.java | 18 +- .../commandside/customers/Customer.java | 10 +- .../schemas/java-mt-demo-extended-api.json | 298 ++++++++++++++++++ 5 files changed, 329 insertions(+), 31 deletions(-) create mode 100644 java-spring/schemas/java-mt-demo-extended-api.json diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/Address.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/Address.java index 156a105..92b0b49 100644 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/Address.java +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/Address.java @@ -9,12 +9,12 @@ public class Address { private String street2; private String city; private String state; - private int zipCode; + private String zipCode; public Address() { } - public Address(String street1, String street2, String city, String state, int zipCode) { + public Address(String street1, String street2, String city, String state, String zipCode) { this.street1 = street1; this.street2 = street2; this.city = city; @@ -54,11 +54,11 @@ public class Address { this.state = state; } - public int getZipCode() { + public String getZipCode() { return zipCode; } - public void setZipCode(int zipCode) { + public void setZipCode(String zipCode) { this.zipCode = zipCode; } } diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java index 09d15fe..20e28fc 100644 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java @@ -7,33 +7,33 @@ import net.chrisrichardson.eventstore.Event; */ public class CustomerCreatedEvent implements Event { - private String socialSecurityNum; - private String phoneNum; + private String ssn; + private String phoneNumber; private Address address; public CustomerCreatedEvent() { } - public CustomerCreatedEvent(String socialSecurityNum, String phoneNum, Address address) { - this.socialSecurityNum = socialSecurityNum; - this.phoneNum = phoneNum; + public CustomerCreatedEvent(String ssn, String phoneNumber, Address address) { + this.ssn = ssn; + this.phoneNumber = phoneNumber; this.address = address; } - public String getSocialSecurityNum() { - return socialSecurityNum; + public String getSsn() { + return ssn; } - public void setSocialSecurityNum(String socialSecurityNum) { - this.socialSecurityNum = socialSecurityNum; + public void setSsn(String ssn) { + this.ssn = ssn; } - public String getPhoneNum() { - return phoneNum; + public String getPhoneNumber() { + return phoneNumber; } - public void setPhoneNum(String phoneNum) { - this.phoneNum = phoneNum; + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; } public Address getAddress() { diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java index 96b1ca4..9ac8c0e 100644 --- a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java +++ b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java @@ -7,22 +7,22 @@ import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.custom */ public class CreateCustomerCommand implements CustomerCommand { - private String socialSecurityNum; - private String phoneNum; + private String ssn; + private String phoneNumber; private Address address; - public CreateCustomerCommand(String socialSecurityNum, String phoneNum, Address address) { - this.socialSecurityNum = socialSecurityNum; - this.phoneNum = phoneNum; + public CreateCustomerCommand(String socialSecurityNum, String phoneNumber, Address address) { + this.ssn = ssn; + this.phoneNumber = phoneNumber; this.address = address; } - public String getSocialSecurityNum() { - return socialSecurityNum; + public String getSsn() { + return ssn; } - public String getPhoneNum() { - return phoneNum; + public String getPhoneNumber() { + return phoneNumber; } public Address getAddress() { diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java index e6a9a28..a50b1e7 100644 --- a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java +++ b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java @@ -12,18 +12,18 @@ import java.util.List; * Created by popikyardo on 02.02.16. */ public class Customer extends ReflectiveMutableCommandProcessingAggregate { - private String socialSecurityNum; - private String phoneNum; + private String ssn; + private String phoneNumber; private Address address; public List process(CreateCustomerCommand cmd) { - return EventUtil.events(new CustomerCreatedEvent(cmd.getSocialSecurityNum(), cmd.getPhoneNum(), cmd.getAddress())); + return EventUtil.events(new CustomerCreatedEvent(cmd.getSsn(), cmd.getPhoneNumber(), cmd.getAddress())); } public void apply(CustomerCreatedEvent event) { - socialSecurityNum = event.getSocialSecurityNum(); - phoneNum = event.getPhoneNum(); + ssn = event.getSsn(); + phoneNumber = event.getPhoneNumber(); address = event.getAddress(); } } diff --git a/java-spring/schemas/java-mt-demo-extended-api.json b/java-spring/schemas/java-mt-demo-extended-api.json new file mode 100644 index 0000000..6663d7a --- /dev/null +++ b/java-spring/schemas/java-mt-demo-extended-api.json @@ -0,0 +1,298 @@ +{ + "swagger": "2.0", + "info": { + "description": "Api Documentation", + "version": "1.0", + "title": "Api Documentation", + "termsOfService": "urn:tos", + "contact": { + "name": "Contact Email" + }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0" + } + }, + "host": "localhost:8080", + "basePath": "/", + "tags": [ + { + "name": "customer-service-command-side-controller", + "description": "Customer Service Commandside Controller" + }, + { + "name": "customer-service-query-side-controller", + "description": "Customer Service Queryside Controller" + }, + { + "name": "account-query-side-controller", + "description": "Account Service Queryside Controller" + }, + { + "name": "auth-controller", + "description": "Authentication Controller" + } + ], + "paths": { + "/authenticate": { + "post": { + "tags": [ + "auth-controller" + ], + "summary": "doAuth", + "operationId": "doAuthUsingPOST", + "consumes": [ + "application/json" + ], + "produces": [ + "*/*" + ], + "parameters": [ + { + "in": "body", + "name": "request", + "description": "request", + "required": true, + "schema": { + "$ref": "#/definitions/AuthRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/AuthResponse" + } + } + } + } + }, + "/accounts": { + "get": { + "tags": [ + "account-query-side-controller" + ], + "summary": "getAllAccountsByCustomer", + "operationId": "getAllAccountsByCustomerUsingGET", + "consumes": [ + "application/json" + ], + "produces": [ + "*/*" + ], + "parameters": [ + { + "name": "customerId", + "in": "query", + "description": "customer id", + "required": true, + "type": "string" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/CustomersQueryResponse" + } + } + } + } + }, + "/customers": { + "get": { + "tags": [ + "customer-service-query-side-controller" + ], + "summary": "getAllCustomersByEmail", + "operationId": "getAllCustomersByEmailUsingGET", + "consumes": [ + "application/json" + ], + "produces": [ + "*/*" + ], + "parameters": [ + { + "name": "email", + "in": "query", + "description": "customer's email", + "required": true, + "type": "string" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/CustomersQueryResponse" + } + } + } + }, + "post": { + "tags": [ + "customer-service-command-side-controller" + ], + "summary": "saveCustomer", + "operationId": "saveCustomerUsingPOST", + "consumes": [ + "application/json" + ], + "produces": [ + "*/*" + ], + "parameters": [ + { + "in": "body", + "name": "customer", + "description": "customer", + "required": true, + "schema": { + "$ref": "#/definitions/CustomerInfo" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/CustomerResponse" + } + }, + "400": { + "description": "Validation error" + } + } + } + }, + "/customers/{id}": { + "get": { + "tags": [ + "customer-service-query-side-controller" + ], + "summary": "getBoard", + "operationId": "getBoardUsingGET", + "consumes": [ + "application/json" + ], + "produces": [ + "*/*" + ], + "parameters": [ + { + "name": "id", + "in": "path", + "description": "id", + "required": true, + "type": "string" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/CustomerResponse" + } + } + } + } + } + }, + "definitions": { + "AuthRequest": { + "required": [ "email" ], + "properties": { + "email": { + "type": "string" + } + } + }, + "AuthResponse": { + "properties": { + "token": { + "type": "string" + } + } + }, + "CustomerInfo": { + "required": [ "email", "ssn", "phoneNumber", "address" ], + "properties": { + "email": { + "type": "string" + }, + "ssn": { + "type": "string" + }, + "phoneNumber": { + "type": "string" + }, + "address": { + "$ref": "#/definitions/Address" + } + } + }, + "CustomersQueryResponse": { + "properties": { + "customers": { + "type": "array", + "items": { + "$ref": "#/definitions/CustomerResponse" + } + } + } + }, + "CustomerResponse": { + "required": [ "id", "customerInfo" ], + "properties": { + "id": { + "type": "string" + }, + "customerInfo": { + "$ref": "#/definitions/CustomerInfo" + } + } + }, + "AccountsQueryResponse": { + "properties": { + "customers": { + "type": "array", + "items": { + "$ref": "#/definitions/GetAccountResponse" + } + } + } + }, + "GetAccountResponse": { + "properties": { + "accountId": { + "type": "string" + }, + "balance": { + "type": "number" + } + } + }, + "Address": { + "properties": { + "street1": { + "type": "string" + }, + "street2": { + "type": "string" + }, + "city": { + "type": "string" + }, + "state": { + "type": "string" + }, + "zipCode": { + "type": "string" + }, + } + } + } +} + From e0691a61a25e72fc717ca7c5b8929fce0fcea5cc Mon Sep 17 00:00:00 2001 From: dartpopikyardo Date: Wed, 3 Feb 2016 23:04:15 +0300 Subject: [PATCH 03/10] added customer-command-side --- .../accounts/AccountController.java | 2 +- .../accounts/CreateAccountRequest.java | 13 ++++- .../customers/CustomerCreatedEvent.java | 34 +++--------- .../common/customers/CustomerInfo.java | 53 +++++++++++++++++++ .../customers/CreateCustomerCommand.java | 24 +++------ .../commandside/customers/Customer.java | 13 ++--- .../customers/CustomerConfiguration.java | 25 +++++++++ .../customers/CustomerService.java | 21 ++++++++ .../customer-command-side-web/build.gradle | 11 ++++ .../CommandSideWebCustomersConfiguration.java | 35 ++++++++++++ .../customers/CustomerController.java | 32 +++++++++++ .../customers/CustomerResponse.java | 36 +++++++++++++ java-spring/settings.gradle | 1 + 13 files changed, 245 insertions(+), 55 deletions(-) create mode 100644 java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerInfo.java create mode 100644 java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerConfiguration.java create mode 100644 java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java create mode 100644 java-spring/customer-command-side-web/build.gradle create mode 100644 java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java create mode 100644 java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java create mode 100644 java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerResponse.java diff --git a/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/AccountController.java b/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/AccountController.java index 0e8294b..0236b2a 100644 --- a/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/AccountController.java +++ b/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/AccountController.java @@ -22,7 +22,7 @@ public class AccountController { @RequestMapping(method = RequestMethod.POST) public Observable createAccount(@Validated @RequestBody CreateAccountRequest request) { - return accountService.openAccount(request.getInitialBalance()) + return accountService.openAccount(request.getCustomerId(), request.getInitialBalance()) .map(entityAndEventInfo -> new CreateAccountResponse(entityAndEventInfo.getEntityIdentifier().getId())); } } diff --git a/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/CreateAccountRequest.java b/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/CreateAccountRequest.java index 923b748..2e43d05 100644 --- a/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/CreateAccountRequest.java +++ b/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/CreateAccountRequest.java @@ -7,6 +7,9 @@ import java.math.BigDecimal; public class CreateAccountRequest { + @NotNull + private String customerId; + @NotNull @DecimalMin("0") private BigDecimal initialBalance; @@ -14,11 +17,19 @@ public class CreateAccountRequest { public CreateAccountRequest() { } - public CreateAccountRequest(BigDecimal initialBalance) { + public CreateAccountRequest(String customerId, BigDecimal initialBalance) { this.initialBalance = initialBalance; } + public String getCustomerId() { + return customerId; + } + + public void setCustomerId(String customerId) { + this.customerId = customerId; + } + public BigDecimal getInitialBalance() { return initialBalance; } diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java index 20e28fc..1ec87c4 100644 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java @@ -7,40 +7,20 @@ import net.chrisrichardson.eventstore.Event; */ public class CustomerCreatedEvent implements Event { - private String ssn; - private String phoneNumber; - private Address address; + private CustomerInfo customerInfo; public CustomerCreatedEvent() { } - public CustomerCreatedEvent(String ssn, String phoneNumber, Address address) { - this.ssn = ssn; - this.phoneNumber = phoneNumber; - this.address = address; + public CustomerCreatedEvent(CustomerInfo customerInfo) { + this.customerInfo = customerInfo; } - public String getSsn() { - return ssn; + public CustomerInfo getCustomerInfo() { + return customerInfo; } - public void setSsn(String ssn) { - this.ssn = ssn; - } - - public String getPhoneNumber() { - return phoneNumber; - } - - public void setPhoneNumber(String phoneNumber) { - this.phoneNumber = phoneNumber; - } - - public Address getAddress() { - return address; - } - - public void setAddress(Address address) { - this.address = address; + public void setCustomerInfo(CustomerInfo customerInfo) { + this.customerInfo = customerInfo; } } diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerInfo.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerInfo.java new file mode 100644 index 0000000..6622ebd --- /dev/null +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerInfo.java @@ -0,0 +1,53 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers; + +/** + * Created by popikyardo on 03.02.16. + */ +public class CustomerInfo { + private String email; + private String ssn; + private String phoneNumber; + private Address address; + + public CustomerInfo() { + } + + public CustomerInfo(String email, String ssn, String phoneNumber, Address address) { + this.email = email; + this.ssn = ssn; + this.phoneNumber = phoneNumber; + this.address = address; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getSsn() { + return ssn; + } + + public void setSsn(String ssn) { + this.ssn = ssn; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } +} diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java index 9ac8c0e..97fb7df 100644 --- a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java +++ b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java @@ -1,31 +1,19 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers; -import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.Address; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo; /** * Created by popikyardo on 02.02.16. */ public class CreateCustomerCommand implements CustomerCommand { - private String ssn; - private String phoneNumber; - private Address address; + private CustomerInfo customerInfo; - public CreateCustomerCommand(String socialSecurityNum, String phoneNumber, Address address) { - this.ssn = ssn; - this.phoneNumber = phoneNumber; - this.address = address; + public CreateCustomerCommand(CustomerInfo customerInfo) { + this.customerInfo = customerInfo; } - public String getSsn() { - return ssn; - } - - public String getPhoneNumber() { - return phoneNumber; - } - - public Address getAddress() { - return address; + public CustomerInfo getCustomerInfo() { + return customerInfo; } } diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java index a50b1e7..ff3a0a8 100644 --- a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java +++ b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java @@ -3,8 +3,8 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside. import net.chrisrichardson.eventstore.Event; import net.chrisrichardson.eventstore.EventUtil; import net.chrisrichardson.eventstore.ReflectiveMutableCommandProcessingAggregate; -import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.Address; import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerCreatedEvent; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo; import java.util.List; @@ -12,18 +12,15 @@ import java.util.List; * Created by popikyardo on 02.02.16. */ public class Customer extends ReflectiveMutableCommandProcessingAggregate { - private String ssn; - private String phoneNumber; - private Address address; + + private CustomerInfo customerInfo; public List process(CreateCustomerCommand cmd) { - return EventUtil.events(new CustomerCreatedEvent(cmd.getSsn(), cmd.getPhoneNumber(), cmd.getAddress())); + return EventUtil.events(new CustomerCreatedEvent(cmd.getCustomerInfo())); } public void apply(CustomerCreatedEvent event) { - ssn = event.getSsn(); - phoneNumber = event.getPhoneNumber(); - address = event.getAddress(); + customerInfo = event.getCustomerInfo(); } } diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerConfiguration.java b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerConfiguration.java new file mode 100644 index 0000000..c8336bb --- /dev/null +++ b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerConfiguration.java @@ -0,0 +1,25 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers; + +import net.chrisrichardson.eventstore.EventStore; +import net.chrisrichardson.eventstore.javaapi.consumer.EnableJavaEventHandlers; +import net.chrisrichardson.eventstore.repository.AggregateRepository; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableJavaEventHandlers +public class CustomerConfiguration { + + @Bean + public CustomerService customerService(AggregateRepository customerRepository) { + return new CustomerService(customerRepository); + } + + @Bean + public AggregateRepository customerRepository(EventStore eventStore) { + return new AggregateRepository(Customer.class, eventStore); + } + +} + + diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java new file mode 100644 index 0000000..bbc2cb5 --- /dev/null +++ b/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java @@ -0,0 +1,21 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers; + + +import net.chrisrichardson.eventstore.EntityWithIdAndVersion; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.Address; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.repository.AggregateRepository; + +public class CustomerService { + + private final AggregateRepository accountRepository; + + public CustomerService(AggregateRepository accountRepository) { + this.accountRepository = accountRepository; + } + + public rx.Observable> createCustomer(CustomerInfo customerInfo) { + return accountRepository.save(new CreateCustomerCommand(customerInfo)); + } + +} diff --git a/java-spring/customer-command-side-web/build.gradle b/java-spring/customer-command-side-web/build.gradle new file mode 100644 index 0000000..8809165 --- /dev/null +++ b/java-spring/customer-command-side-web/build.gradle @@ -0,0 +1,11 @@ +apply plugin: 'java' + +dependencies { + compile project(":customer-command-side-backend") + compile project(":common-web") + + compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion" + + testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVersion" + testCompile "net.chrisrichardson.eventstore.client:eventstore-jdbc_2.10:$eventStoreClientVersion" +} diff --git a/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java b/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java new file mode 100644 index 0000000..e86255c --- /dev/null +++ b/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java @@ -0,0 +1,35 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers; + +import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.CustomerConfiguration; +import net.chrisrichardson.eventstore.javaexamples.banking.web.util.ObservableReturnValueHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.web.method.support.HandlerMethodReturnValueHandler; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by popikyardo on 03.02.16. + */ +@Configuration +@Import({CustomerConfiguration.class}) +@ComponentScan +public class CommandSideWebCustomersConfiguration extends WebMvcConfigurerAdapter { + + class FakeThing {} + + @Bean + public FakeThing init(RequestMappingHandlerAdapter adapter) { + // https://jira.spring.io/browse/SPR-13083 + List handlers = new ArrayList(adapter.getReturnValueHandlers()); + handlers.add(0, new ObservableReturnValueHandler()); + adapter.setReturnValueHandlers(handlers); + return new FakeThing(); + } + +} \ No newline at end of file diff --git a/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java b/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java new file mode 100644 index 0000000..0ce9ca4 --- /dev/null +++ b/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java @@ -0,0 +1,32 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers; + +import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.CustomerService; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; +import rx.Observable; + +/** + * Created by popikyardo on 03.02.16. + */ +@RestController +@RequestMapping("/accounts") +public class CustomerController { + + private CustomerService customerService; + + @Autowired + public CustomerController(CustomerService customerService) { + this.customerService = customerService; + } + + @RequestMapping(method = RequestMethod.POST) + public Observable createAccount(@Validated @RequestBody CustomerInfo request) { + return customerService.createCustomer(request) + .map(entityAndEventInfo -> new CustomerResponse(entityAndEventInfo.getEntityIdentifier().getId(), request)); + } +} diff --git a/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerResponse.java b/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerResponse.java new file mode 100644 index 0000000..e81c302 --- /dev/null +++ b/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerResponse.java @@ -0,0 +1,36 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers; + +import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo; + +/** + * Created by popikyardo on 03.02.16. + */ +public class CustomerResponse { + + private String id; + private CustomerInfo customerInfo; + + public CustomerResponse() { + } + + public CustomerResponse(String id, CustomerInfo customerInfo) { + this.id = id; + this.customerInfo = customerInfo; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public CustomerInfo getCustomerInfo() { + return customerInfo; + } + + public void setCustomerInfo(CustomerInfo customerInfo) { + this.customerInfo = customerInfo; + } +} diff --git a/java-spring/settings.gradle b/java-spring/settings.gradle index a6b3b3a..a00f633 100644 --- a/java-spring/settings.gradle +++ b/java-spring/settings.gradle @@ -26,4 +26,5 @@ include 'e2e-test' rootProject.name = 'java-spring-event-sourcing-example' include 'common-auth' +include 'customer-command-side-web' From afb7c9bc49bf9b63d2943274d2a56260a5c21c16 Mon Sep 17 00:00:00 2001 From: Main Date: Fri, 5 Feb 2016 01:27:17 +0300 Subject: [PATCH 04/10] added customers-query-side part added service wrappers to customers-command-side and customers-query-side --- java-spring/common-backend/build.gradle | 1 + .../customers/CustomerCreatedEvent.java | 1 + java-spring/common-customers/build.gradle | 5 ++ .../banking}/common/customers/Address.java | 2 +- .../common/customers/CustomerInfo.java | 26 ++----- .../common}/customers/CustomerResponse.java | 4 +- .../build.gradle | 1 + .../customers/CreateCustomerCommand.java | 2 +- .../commandside/customers/Customer.java | 3 +- .../customers/CustomerCommand.java | 0 .../customers/CustomerConfiguration.java | 0 .../customers/CustomerService.java | 3 +- .../build.gradle | 20 ++++++ ...tomersCommandSideServiceConfiguration.java | 29 ++++++++ .../main/CustomersCommandSideServiceMain.java | 11 +++ .../build.gradle | 3 +- .../CommandSideWebCustomersConfiguration.java | 0 .../customers/CustomerController.java | 3 +- .../customers-query-side-backend/build.gradle | 18 +++++ .../customers/CustomerInfoRepository.java | 10 +++ .../customers/CustomerInfoUpdateService.java | 35 ++++++++++ .../customers/CustomerInfoWithId.java | 20 ++++++ .../customers/CustomerNotFoundException.java | 8 +++ .../customers/CustomerQueryService.java | 31 +++++++++ .../customers/CustomerQueryWorkflow.java | 37 ++++++++++ .../customers/CustomersNotFoundException.java | 8 +++ .../QuerySideCustomerConfiguration.java | 37 ++++++++++ .../customers/QuerySideDependencyChecker.java | 41 +++++++++++ .../customers-query-side-service/build.gradle | 20 ++++++ ...ustomersQuerySideServiceConfiguration.java | 27 ++++++++ .../main/CustomersQuerySideServiceMain.java | 11 +++ .../customers-query-side-web/build.gradle | 9 +++ .../CustomersQuerySideWebConfiguration.java | 33 +++++++++ .../customers/CustomerQueryController.java | 69 +++++++++++++++++++ .../customers/CustomersQueryResponse.java | 28 ++++++++ java-spring/settings.gradle | 9 ++- 36 files changed, 532 insertions(+), 33 deletions(-) create mode 100644 java-spring/common-customers/build.gradle rename java-spring/{common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend => common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking}/common/customers/Address.java (93%) rename java-spring/{common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend => common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking}/common/customers/CustomerInfo.java (52%) rename java-spring/{customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside => common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common}/customers/CustomerResponse.java (77%) rename java-spring/{customer-command-side-backend => customers-command-side-backend}/build.gradle (91%) rename java-spring/{customer-command-side-backend => customers-command-side-backend}/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java (81%) rename java-spring/{customer-command-side-backend => customers-command-side-backend}/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java (89%) rename java-spring/{customer-command-side-backend => customers-command-side-backend}/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerCommand.java (100%) rename java-spring/{customer-command-side-backend => customers-command-side-backend}/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerConfiguration.java (100%) rename java-spring/{customer-command-side-backend => customers-command-side-backend}/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java (77%) create mode 100644 java-spring/customers-command-side-service/build.gradle create mode 100644 java-spring/customers-command-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceConfiguration.java create mode 100644 java-spring/customers-command-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/main/CustomersCommandSideServiceMain.java rename java-spring/{customer-command-side-web => customers-command-side-web}/build.gradle (78%) rename java-spring/{customer-command-side-web => customers-command-side-web}/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java (100%) rename java-spring/{customer-command-side-web => customers-command-side-web}/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java (87%) create mode 100644 java-spring/customers-query-side-backend/build.gradle create mode 100644 java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoRepository.java create mode 100644 java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java create mode 100644 java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoWithId.java create mode 100644 java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerNotFoundException.java create mode 100644 java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryService.java create mode 100644 java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java create mode 100644 java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomersNotFoundException.java create mode 100644 java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java create mode 100644 java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideDependencyChecker.java create mode 100644 java-spring/customers-query-side-service/build.gradle create mode 100644 java-spring/customers-query-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceConfiguration.java create mode 100644 java-spring/customers-query-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/main/CustomersQuerySideServiceMain.java create mode 100644 java-spring/customers-query-side-web/build.gradle create mode 100644 java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/CustomersQuerySideWebConfiguration.java create mode 100644 java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java create mode 100644 java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomersQueryResponse.java diff --git a/java-spring/common-backend/build.gradle b/java-spring/common-backend/build.gradle index f63260a..92eec48 100644 --- a/java-spring/common-backend/build.gradle +++ b/java-spring/common-backend/build.gradle @@ -1,6 +1,7 @@ apply plugin: 'java' dependencies { + compile project(":common-customers") compile "net.chrisrichardson.eventstore.client:eventstore-java-client_2.10:$eventStoreClientVersion" testCompile "junit:junit:4.11" diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java index 1ec87c4..8e51505 100644 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java @@ -1,6 +1,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers; import net.chrisrichardson.eventstore.Event; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; /** * Created by popikyardo on 02.02.16. diff --git a/java-spring/common-customers/build.gradle b/java-spring/common-customers/build.gradle new file mode 100644 index 0000000..6563876 --- /dev/null +++ b/java-spring/common-customers/build.gradle @@ -0,0 +1,5 @@ +apply plugin: 'java' + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.11' +} diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/Address.java b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Address.java similarity index 93% rename from java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/Address.java rename to java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Address.java index 92b0b49..158ec05 100644 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/Address.java +++ b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Address.java @@ -1,4 +1,4 @@ -package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers; +package net.chrisrichardson.eventstore.javaexamples.banking.common.customers; /** * Created by popikyardo on 02.02.16. diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerInfo.java b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java similarity index 52% rename from java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerInfo.java rename to java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java index 6622ebd..5c68f2c 100644 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerInfo.java +++ b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java @@ -1,13 +1,13 @@ -package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers; +package net.chrisrichardson.eventstore.javaexamples.banking.common.customers; /** * Created by popikyardo on 03.02.16. */ public class CustomerInfo { - private String email; - private String ssn; - private String phoneNumber; - private Address address; + protected String email; + protected String ssn; + protected String phoneNumber; + protected Address address; public CustomerInfo() { } @@ -23,31 +23,15 @@ public class CustomerInfo { return email; } - public void setEmail(String email) { - this.email = email; - } - public String getSsn() { return ssn; } - public void setSsn(String ssn) { - this.ssn = ssn; - } - public String getPhoneNumber() { return phoneNumber; } - public void setPhoneNumber(String phoneNumber) { - this.phoneNumber = phoneNumber; - } - public Address getAddress() { return address; } - - public void setAddress(Address address) { - this.address = address; - } } diff --git a/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerResponse.java b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerResponse.java similarity index 77% rename from java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerResponse.java rename to java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerResponse.java index e81c302..3373fbb 100644 --- a/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerResponse.java +++ b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerResponse.java @@ -1,6 +1,4 @@ -package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers; - -import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo; +package net.chrisrichardson.eventstore.javaexamples.banking.common.customers; /** * Created by popikyardo on 03.02.16. diff --git a/java-spring/customer-command-side-backend/build.gradle b/java-spring/customers-command-side-backend/build.gradle similarity index 91% rename from java-spring/customer-command-side-backend/build.gradle rename to java-spring/customers-command-side-backend/build.gradle index 1a4bd0d..753a65c 100644 --- a/java-spring/customer-command-side-backend/build.gradle +++ b/java-spring/customers-command-side-backend/build.gradle @@ -1,6 +1,7 @@ apply plugin: 'java' dependencies { + compile project(":common-customers") compile project(":common-backend") compile "net.chrisrichardson.eventstore.client:eventstore-java-client_2.10:$eventStoreClientVersion" diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java similarity index 81% rename from java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java rename to java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java index 97fb7df..3fe5a8c 100644 --- a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java +++ b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java @@ -1,6 +1,6 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers; -import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; /** * Created by popikyardo on 02.02.16. diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java similarity index 89% rename from java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java rename to java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java index ff3a0a8..7084b20 100644 --- a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java +++ b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java @@ -4,7 +4,7 @@ import net.chrisrichardson.eventstore.Event; import net.chrisrichardson.eventstore.EventUtil; import net.chrisrichardson.eventstore.ReflectiveMutableCommandProcessingAggregate; import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerCreatedEvent; -import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; import java.util.List; @@ -15,6 +15,7 @@ public class Customer extends ReflectiveMutableCommandProcessingAggregate process(CreateCustomerCommand cmd) { return EventUtil.events(new CustomerCreatedEvent(cmd.getCustomerInfo())); } diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerCommand.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerCommand.java similarity index 100% rename from java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerCommand.java rename to java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerCommand.java diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerConfiguration.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerConfiguration.java similarity index 100% rename from java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerConfiguration.java rename to java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerConfiguration.java diff --git a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java similarity index 77% rename from java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java rename to java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java index bbc2cb5..c312a36 100644 --- a/java-spring/customer-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java +++ b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java @@ -2,8 +2,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside. import net.chrisrichardson.eventstore.EntityWithIdAndVersion; -import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.Address; -import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; import net.chrisrichardson.eventstore.repository.AggregateRepository; public class CustomerService { diff --git a/java-spring/customers-command-side-service/build.gradle b/java-spring/customers-command-side-service/build.gradle new file mode 100644 index 0000000..034e4f7 --- /dev/null +++ b/java-spring/customers-command-side-service/build.gradle @@ -0,0 +1,20 @@ +apply plugin: VerifyMongoDBConfigurationPlugin +apply plugin: VerifyEventStoreEnvironmentPlugin + +apply plugin: 'spring-boot' + +dependencies { + compile project(":customers-command-side-web") + compile project(":common-swagger") + + compile "org.springframework.boot:spring-boot-starter-web" + compile "org.springframework.boot:spring-boot-starter-actuator" + + compile "net.chrisrichardson.eventstore.client:eventstore-http-stomp-client_2.10:$eventStoreClientVersion" + + testCompile "org.springframework.boot:spring-boot-starter-test" +} + +test { + ignoreFailures true +} diff --git a/java-spring/customers-command-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceConfiguration.java b/java-spring/customers-command-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceConfiguration.java new file mode 100644 index 0000000..eef4d14 --- /dev/null +++ b/java-spring/customers-command-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceConfiguration.java @@ -0,0 +1,29 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web; + +import net.chrisrichardson.eventstore.client.config.EventStoreHttpClientConfiguration; +import net.chrisrichardson.eventstore.javaexamples.banking.commonswagger.CommonSwaggerConfiguration; +import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers.CommandSideWebCustomersConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.web.HttpMessageConverters; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; + +@Configuration +@Import({CommandSideWebCustomersConfiguration.class, EventStoreHttpClientConfiguration.class, CommonSwaggerConfiguration.class}) +@EnableAutoConfiguration +@ComponentScan +public class CustomersCommandSideServiceConfiguration { + + + @Bean + public HttpMessageConverters customConverters() { + HttpMessageConverter additional = new MappingJackson2HttpMessageConverter(); + return new HttpMessageConverters(additional); + } + + +} diff --git a/java-spring/customers-command-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/main/CustomersCommandSideServiceMain.java b/java-spring/customers-command-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/main/CustomersCommandSideServiceMain.java new file mode 100644 index 0000000..77353d9 --- /dev/null +++ b/java-spring/customers-command-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/main/CustomersCommandSideServiceMain.java @@ -0,0 +1,11 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web.main; + +import net.chrisrichardson.eventstore.javaexamples.banking.web.CustomersCommandSideServiceConfiguration; +import org.springframework.boot.SpringApplication; + +public class CustomersCommandSideServiceMain { + + public static void main(String[] args) { + SpringApplication.run(CustomersCommandSideServiceConfiguration.class, args); + } +} diff --git a/java-spring/customer-command-side-web/build.gradle b/java-spring/customers-command-side-web/build.gradle similarity index 78% rename from java-spring/customer-command-side-web/build.gradle rename to java-spring/customers-command-side-web/build.gradle index 8809165..cda197f 100644 --- a/java-spring/customer-command-side-web/build.gradle +++ b/java-spring/customers-command-side-web/build.gradle @@ -1,7 +1,8 @@ apply plugin: 'java' dependencies { - compile project(":customer-command-side-backend") + compile project(":common-customers") + compile project(":customers-command-side-backend") compile project(":common-web") compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion" diff --git a/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java similarity index 100% rename from java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java rename to java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java diff --git a/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java similarity index 87% rename from java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java rename to java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java index 0ce9ca4..1d6d231 100644 --- a/java-spring/customer-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java +++ b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java @@ -1,7 +1,8 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers; import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.CustomerService; -import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestBody; diff --git a/java-spring/customers-query-side-backend/build.gradle b/java-spring/customers-query-side-backend/build.gradle new file mode 100644 index 0000000..bf57fec --- /dev/null +++ b/java-spring/customers-query-side-backend/build.gradle @@ -0,0 +1,18 @@ +apply plugin: 'java' + +dependencies { + compile project(":common-backend") + + compile "net.chrisrichardson.eventstore.client:eventstore-java-client_2.10:$eventStoreClientVersion" + compile "org.springframework.boot:spring-boot-starter-data-mongodb:$springBootVersion" + + compile 'com.fasterxml.jackson.core:jackson-core:2.4.3' + compile 'com.fasterxml.jackson.core:jackson-databind:2.4.3' + compile 'com.fasterxml.jackson.module:jackson-module-scala_2.10:2.4.3' + + testCompile project(":testutil") + testCompile "junit:junit:4.11" + testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVersion" + testCompile "net.chrisrichardson.eventstore.client:eventstore-jdbc_2.10:$eventStoreClientVersion" + +} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoRepository.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoRepository.java new file mode 100644 index 0000000..2111406 --- /dev/null +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoRepository.java @@ -0,0 +1,10 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import java.util.List; + +interface CustomerInfoRepository extends MongoRepository { + + List findByEmailLike(String email); +} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java new file mode 100644 index 0000000..7acb109 --- /dev/null +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java @@ -0,0 +1,35 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; + +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by Main on 04.02.2016. + */ +public class CustomerInfoUpdateService { + + private Logger logger = LoggerFactory.getLogger(getClass()); + + private CustomerInfoRepository accountInfoRepository; + + public CustomerInfoUpdateService(CustomerInfoRepository accountInfoRepository) { + this.accountInfoRepository = accountInfoRepository; + } + + public void create(String id, CustomerInfo customerInfo) { + try { + accountInfoRepository.save(new CustomerInfoWithId(id, + customerInfo.getEmail(), + customerInfo.getSsn(), + customerInfo.getPhoneNumber(), + customerInfo.getAddress()) + ); + logger.info("Saved in mongo"); + } catch (Throwable t) { + logger.error("Error during saving: ", t); + throw new RuntimeException(t); + } + } + +} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoWithId.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoWithId.java new file mode 100644 index 0000000..3aacdd9 --- /dev/null +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoWithId.java @@ -0,0 +1,20 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; + +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; + +/** + * Created by Main on 05.02.2016. + */ +public class CustomerInfoWithId extends CustomerInfo { + private String id; + + public CustomerInfoWithId(String id, String email, String ssn, String phoneNumber, Address address) { + super(email, ssn, phoneNumber, address); + this.id = id; + } + + public String getId() { + return id; + } +} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerNotFoundException.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerNotFoundException.java new file mode 100644 index 0000000..f003262 --- /dev/null +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerNotFoundException.java @@ -0,0 +1,8 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; + +public class CustomerNotFoundException extends RuntimeException { + + public CustomerNotFoundException(String customerId) { + super("Customer not found " + customerId); + } +} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryService.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryService.java new file mode 100644 index 0000000..2c873ef --- /dev/null +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryService.java @@ -0,0 +1,31 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; + +import net.chrisrichardson.eventstore.EntityIdentifier; +import rx.Observable; + +import java.util.List; + +public class CustomerQueryService { + + private CustomerInfoRepository customerInfoRepository; + + public CustomerQueryService(CustomerInfoRepository customerInfoRepository) { + this.customerInfoRepository = customerInfoRepository; + } + + public Observable findByCustomerId(EntityIdentifier customerId) { + CustomerInfoWithId customer = customerInfoRepository.findOne(customerId.getId()); + if (customer == null) + return Observable.error(new CustomerNotFoundException(customerId.getId())); + else + return Observable.just(customer); + } + + public Observable> findByEmail(String email){ + List customers = customerInfoRepository.findByEmailLike(email); + if (customers.isEmpty()) + return Observable.error(new CustomersNotFoundException()); + else + return Observable.just(customers); + } +} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java new file mode 100644 index 0000000..e68a6d7 --- /dev/null +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java @@ -0,0 +1,37 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; + +import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerCreatedEvent; +import net.chrisrichardson.eventstore.subscriptions.CompoundEventHandler; +import net.chrisrichardson.eventstore.subscriptions.DispatchedEvent; +import net.chrisrichardson.eventstore.subscriptions.EventHandlerMethod; +import net.chrisrichardson.eventstore.subscriptions.EventSubscriber; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import rx.Observable; + +/** + * Created by Main on 04.02.2016. + */ +@EventSubscriber(id="querySideEventHandlers") +public class CustomerQueryWorkflow implements CompoundEventHandler { + + private Logger logger = LoggerFactory.getLogger(getClass()); + + private CustomerInfoUpdateService customerInfoUpdateService; + + + public CustomerQueryWorkflow(CustomerInfoUpdateService customerInfoUpdateService) { + this.customerInfoUpdateService = customerInfoUpdateService; + } + + @EventHandlerMethod + public Observable create(DispatchedEvent de) { + CustomerCreatedEvent event = de.event(); + String id = de.getEntityIdentifier().getId(); + String eventId = de.eventId().asString(); + logger.info("**************** customer version=" + id + ", " + eventId); + + customerInfoUpdateService.create(id, event.getCustomerInfo()); + return Observable.just(null); + } +} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomersNotFoundException.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomersNotFoundException.java new file mode 100644 index 0000000..765a93a --- /dev/null +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomersNotFoundException.java @@ -0,0 +1,8 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; + +public class CustomersNotFoundException extends RuntimeException { + + public CustomersNotFoundException() { + super("Customers not found"); + } +} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java new file mode 100644 index 0000000..9be1fe0 --- /dev/null +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java @@ -0,0 +1,37 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; + +import net.chrisrichardson.eventstore.javaapi.consumer.EnableJavaEventHandlers; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; + +/** + * Created by Main on 04.02.2016. + */ +@Configuration +@EnableMongoRepositories +@EnableJavaEventHandlers +public class QuerySideCustomerConfiguration { + @Bean + public CustomerQueryWorkflow customerQueryWorkflow(CustomerInfoUpdateService accountInfoUpdateService) { + return new CustomerQueryWorkflow(accountInfoUpdateService); + } + + @Bean + public CustomerInfoUpdateService customerInfoUpdateService(CustomerInfoRepository customerInfoRepository) { + return new CustomerInfoUpdateService(customerInfoRepository); + } + + @Bean + public CustomerQueryService customerQueryService(CustomerInfoRepository accountInfoRepository) { + return new CustomerQueryService(accountInfoRepository); + } + + + @Bean + public QuerySideDependencyChecker querysideDependencyChecker(MongoTemplate mongoTemplate) { + return new QuerySideDependencyChecker(mongoTemplate); + } + +} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideDependencyChecker.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideDependencyChecker.java new file mode 100644 index 0000000..5715760 --- /dev/null +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideDependencyChecker.java @@ -0,0 +1,41 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.data.mongodb.core.MongoTemplate; +import rx.Observable; +import rx.Subscriber; + +import javax.annotation.PostConstruct; +import java.util.concurrent.TimeUnit; + +public class QuerySideDependencyChecker { + private Logger logger = LoggerFactory.getLogger(getClass()); + private MongoTemplate mongoTemplate; + + public QuerySideDependencyChecker(MongoTemplate mongoTemplate) { + this.mongoTemplate = mongoTemplate; + } + + @PostConstruct + public void checkDependencies() { + try { + logger.info("Checking mongodb connectivity {}", System.getenv("SPRING_DATA_MONGODB_URI")); + + Observable.create(new Observable.OnSubscribe() { + @Override + public void call(Subscriber subscriber) { + try { + subscriber.onNext(mongoTemplate.getDb().getCollectionNames()); + subscriber.onCompleted(); + } catch (Throwable t) { + subscriber.onError(t); + } + } + }).timeout(5, TimeUnit.SECONDS).toBlocking().first(); + + } catch (Throwable e) { + throw new RuntimeException("Error connecting to Mongo - have you set SPRING_DATA_MONGODB_URI or --spring.data.mongodb_uri?", e); + } + } +} diff --git a/java-spring/customers-query-side-service/build.gradle b/java-spring/customers-query-side-service/build.gradle new file mode 100644 index 0000000..d248c6c --- /dev/null +++ b/java-spring/customers-query-side-service/build.gradle @@ -0,0 +1,20 @@ +apply plugin: VerifyMongoDBConfigurationPlugin +apply plugin: VerifyEventStoreEnvironmentPlugin + +apply plugin: 'spring-boot' + +dependencies { + compile project(":customers-query-side-web") + compile project(":common-swagger") + + compile "org.springframework.boot:spring-boot-starter-web" + compile "org.springframework.boot:spring-boot-starter-actuator" + + compile "net.chrisrichardson.eventstore.client:eventstore-http-stomp-client_2.10:$eventStoreClientVersion" + + testCompile "org.springframework.boot:spring-boot-starter-test" +} + +test { + ignoreFailures true +} diff --git a/java-spring/customers-query-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceConfiguration.java b/java-spring/customers-query-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceConfiguration.java new file mode 100644 index 0000000..2ba0f24 --- /dev/null +++ b/java-spring/customers-query-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceConfiguration.java @@ -0,0 +1,27 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web; + +import net.chrisrichardson.eventstore.client.config.EventStoreHttpClientConfiguration; +import net.chrisrichardson.eventstore.javaexamples.banking.commonswagger.CommonSwaggerConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.web.HttpMessageConverters; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; + +@Configuration +@Import({CustomersQuerySideServiceConfiguration.class, EventStoreHttpClientConfiguration.class, CommonSwaggerConfiguration.class}) +@EnableAutoConfiguration +@ComponentScan +public class CustomersQuerySideServiceConfiguration { + + + @Bean + public HttpMessageConverters customConverters() { + HttpMessageConverter additional = new MappingJackson2HttpMessageConverter(); + return new HttpMessageConverters(additional); + } + +} diff --git a/java-spring/customers-query-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/main/CustomersQuerySideServiceMain.java b/java-spring/customers-query-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/main/CustomersQuerySideServiceMain.java new file mode 100644 index 0000000..759d0f3 --- /dev/null +++ b/java-spring/customers-query-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/main/CustomersQuerySideServiceMain.java @@ -0,0 +1,11 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web.main; + +import net.chrisrichardson.eventstore.javaexamples.banking.web.CustomersQuerySideServiceConfiguration; +import org.springframework.boot.SpringApplication; + +public class CustomersQuerySideServiceMain { + + public static void main(String[] args) { + SpringApplication.run(CustomersQuerySideServiceConfiguration.class, args); + } +} diff --git a/java-spring/customers-query-side-web/build.gradle b/java-spring/customers-query-side-web/build.gradle new file mode 100644 index 0000000..ec01ce8 --- /dev/null +++ b/java-spring/customers-query-side-web/build.gradle @@ -0,0 +1,9 @@ +apply plugin: 'java' + +dependencies { + compile project(":customers-query-side-backend") + compile project(":common-web") + + compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion" + compile "org.springframework.boot:spring-boot-starter-actuator:$springBootVersion" +} diff --git a/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/CustomersQuerySideWebConfiguration.java b/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/CustomersQuerySideWebConfiguration.java new file mode 100644 index 0000000..66a92b4 --- /dev/null +++ b/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/CustomersQuerySideWebConfiguration.java @@ -0,0 +1,33 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web.queryside; + +import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.QuerySideCustomerConfiguration; +import net.chrisrichardson.eventstore.javaexamples.banking.web.util.ObservableReturnValueHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.web.method.support.HandlerMethodReturnValueHandler; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; + +import java.util.ArrayList; +import java.util.List; + +@Configuration +@Import({QuerySideCustomerConfiguration.class}) +@ComponentScan +public class CustomersQuerySideWebConfiguration extends WebMvcConfigurerAdapter { + + class FakeThing { + } + + @Bean + public FakeThing init(RequestMappingHandlerAdapter adapter) { + // https://jira.spring.io/browse/SPR-13083 + List handlers = new ArrayList(adapter.getReturnValueHandlers()); + handlers.add(0, new ObservableReturnValueHandler()); + adapter.setReturnValueHandlers(handlers); + return new FakeThing(); + } + +} diff --git a/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java b/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java new file mode 100644 index 0000000..a0151c2 --- /dev/null +++ b/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java @@ -0,0 +1,69 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web.queryside.customers; + +import net.chrisrichardson.eventstore.EntityIdentifier; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomerInfoWithId; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomerNotFoundException; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomerQueryService; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomersNotFoundException; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import rx.Observable; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * Created by Main on 05.02.2016. + */ +@RestController +public class CustomerQueryController { + + private CustomerQueryService customerQueryService; + + @Autowired + public CustomerQueryController(CustomerQueryService customerQueryService) { + this.customerQueryService = customerQueryService; + } + + @RequestMapping(value="/customers/{customerId}", method = RequestMethod.GET) + public Observable getCustomer(@PathVariable String customerId) { + return customerQueryService.findByCustomerId(new EntityIdentifier(customerId)) + .map(this::getCustomerResponse); + } + + @RequestMapping(value="/customers", method = RequestMethod.GET) + public Observable getCustomersByEmail(@RequestParam String email) { + return customerQueryService.findByEmail(email) + .map(this::getCustomersQueryResponse); + } + + @ResponseStatus(value= HttpStatus.NOT_FOUND, reason="customer not found") + @ExceptionHandler(CustomerNotFoundException.class) + public void customerNotFound() { + + } + + @ResponseStatus(value= HttpStatus.NOT_FOUND, reason="customers not found") + @ExceptionHandler(CustomersNotFoundException.class) + public void customersNotFound() { + + } + + private CustomerResponse getCustomerResponse(CustomerInfoWithId customerInfoWithId) { + return new CustomerResponse(customerInfoWithId.getId(), new CustomerInfo(customerInfoWithId.getEmail(), + customerInfoWithId.getSsn(), + customerInfoWithId.getPhoneNumber(), + customerInfoWithId.getAddress())); + } + + private CustomersQueryResponse getCustomersQueryResponse(List customersList) { + return new CustomersQueryResponse(customersList + .stream() + .map(this::getCustomerResponse) + .collect(Collectors.toList()) + ); + } +} diff --git a/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomersQueryResponse.java b/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomersQueryResponse.java new file mode 100644 index 0000000..d89e8df --- /dev/null +++ b/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomersQueryResponse.java @@ -0,0 +1,28 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web.queryside.customers; + +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse; + +import java.util.List; + +/** + * Created by Main on 05.02.2016. + */ +public class CustomersQueryResponse { + + private List customers; + + public CustomersQueryResponse() { + } + + public CustomersQueryResponse(List customers) { + this.customers = customers; + } + + public List getCustomers() { + return customers; + } + + public void setCustomers(List customers) { + this.customers = customers; + } +} diff --git a/java-spring/settings.gradle b/java-spring/settings.gradle index a00f633..98b538a 100644 --- a/java-spring/settings.gradle +++ b/java-spring/settings.gradle @@ -6,7 +6,6 @@ include 'common-backend' include 'accounts-command-side-backend' include 'transactions-command-side-backend' -include 'customer-command-side-backend' include 'accounts-command-side-web' include 'transactions-command-side-web' @@ -26,5 +25,11 @@ include 'e2e-test' rootProject.name = 'java-spring-event-sourcing-example' include 'common-auth' -include 'customer-command-side-web' +include 'customers-command-side-backend' +include 'customers-command-side-web' +include 'customers-query-side-backend' +include 'customers-query-side-web' +include 'common-customers' +include 'customers-command-side-service' +include 'customers-query-side-service' From 06f14a54bfb394a2091e0ea63442b85896e7d39d Mon Sep 17 00:00:00 2001 From: Main Date: Fri, 5 Feb 2016 20:11:53 +0300 Subject: [PATCH 05/10] removed customerId from Account aggregate added tests to customers-query-side and customers-command-side --- .../backend/commandside/accounts/Account.java | 6 -- .../commandside/accounts/AccountTest.java | 2 - ...untsCommandSideServiceIntegrationTest.java | 5 +- .../AccountControllerIntegrationTest.java | 2 +- .../queryside/accounts/AccountInfo.java | 8 +- .../accounts/AccountInfoUpdateService.java | 3 +- .../accounts/AccountQueryWorkflow.java | 3 +- .../backend/MoneyTransferIntegrationTest.java | 8 +- .../AccountQuerySideIntegrationTest.java | 4 +- java-spring/common-customers/build.gradle | 2 + .../banking/common/customers/Address.java | 13 +++ .../common/customers/CustomerInfo.java | 13 +++ .../commandside/customers/Customer.java | 5 +- .../customers/CustomerEventTest.java | 12 +++ .../commandside/customers/CustomerTest.java | 43 ++++++++++ ...mersCommandSideServiceIntegrationTest.java | 58 +++++++++++++ ...rsCommandSideServiceTestConfiguration.java | 26 ++++++ .../customers-query-side-service/build.gradle | 1 + ...tomersQuerySideServiceIntegrationTest.java | 82 +++++++++++++++++++ ...mersQuerySideServiceTestConfiguration.java | 26 ++++++ 20 files changed, 301 insertions(+), 21 deletions(-) create mode 100644 java-spring/customers-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerEventTest.java create mode 100644 java-spring/customers-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerTest.java create mode 100644 java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceIntegrationTest.java create mode 100644 java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceTestConfiguration.java create mode 100644 java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceIntegrationTest.java create mode 100644 java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceTestConfiguration.java diff --git a/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/Account.java b/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/Account.java index 95982c5..a8bbad3 100644 --- a/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/Account.java +++ b/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/Account.java @@ -13,7 +13,6 @@ import java.util.List; public class Account extends ReflectiveMutableCommandProcessingAggregate { - private String customerId; private BigDecimal balance; public List process(OpenAccountCommand cmd) { @@ -32,7 +31,6 @@ public class Account extends ReflectiveMutableCommandProcessingAggregate changes; private List transactions; @@ -16,9 +17,10 @@ public class AccountInfo { private AccountInfo() { } - public AccountInfo(String id, long balance, List changes, List transactions, String version) { + public AccountInfo(String id, String customerId, long balance, List changes, List transactions, String version) { this.id = id; + this.customerId = customerId; this.balance = balance; this.changes = changes; this.transactions = transactions; @@ -29,6 +31,10 @@ public class AccountInfo { return id; } + public String getCustomerId() { + return customerId; + } + public long getBalance() { return balance; } diff --git a/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountInfoUpdateService.java b/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountInfoUpdateService.java index f123229..b5ad651 100644 --- a/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountInfoUpdateService.java +++ b/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountInfoUpdateService.java @@ -26,10 +26,11 @@ public class AccountInfoUpdateService { - public void create(String accountId, BigDecimal initialBalance, String version) { + public void create(String accountId, String customerId, BigDecimal initialBalance, String version) { try { accountInfoRepository.save(new AccountInfo( accountId, + customerId, toIntegerRepr(initialBalance), Collections.emptyList(), Collections.emptyList(), diff --git a/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQueryWorkflow.java b/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQueryWorkflow.java index 1e40637..2ec9039 100644 --- a/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQueryWorkflow.java +++ b/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQueryWorkflow.java @@ -37,7 +37,8 @@ public class AccountQueryWorkflow implements CompoundEventHandler { String eventId = de.eventId().asString(); logger.info("**************** account version=" + id + ", " + eventId); BigDecimal initialBalance = event.getInitialBalance(); - accountInfoUpdateService.create(id, initialBalance, eventId); + String customerId = event.getCustomerId(); + accountInfoUpdateService.create(id, customerId, initialBalance, eventId); return Observable.just(null); } diff --git a/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/MoneyTransferIntegrationTest.java b/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/MoneyTransferIntegrationTest.java index 4c9e08e..9c9c54c 100644 --- a/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/MoneyTransferIntegrationTest.java +++ b/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/MoneyTransferIntegrationTest.java @@ -43,9 +43,9 @@ public class MoneyTransferIntegrationTest { @Test public void shouldTransferMoney() { - final EntityWithIdAndVersion fromAccount= await(accountService.openAccount(new BigDecimal(150))); + final EntityWithIdAndVersion fromAccount= await(accountService.openAccount("00000000-00000000", new BigDecimal(150))); - final EntityWithIdAndVersion toAccount = await(accountService.openAccount(new BigDecimal(300))); + final EntityWithIdAndVersion toAccount = await(accountService.openAccount("00000000-00000000", new BigDecimal(300))); final EntityWithIdAndVersion transaction = await( moneyTransferService.transferMoney(new TransferDetails(fromAccount.getEntityIdentifier(), @@ -98,9 +98,9 @@ public class MoneyTransferIntegrationTest { @Test public void shouldFailDueToInsufficientFunds() { - final EntityWithIdAndVersion fromAccount= await(accountService.openAccount(new BigDecimal(150))); + final EntityWithIdAndVersion fromAccount= await(accountService.openAccount("00000000-00000000", new BigDecimal(150))); - final EntityWithIdAndVersion toAccount = await(accountService.openAccount(new BigDecimal(300))); + final EntityWithIdAndVersion toAccount = await(accountService.openAccount("00000000-00000000", new BigDecimal(300))); final EntityWithIdAndVersion transaction = await( moneyTransferService.transferMoney(new TransferDetails(fromAccount.getEntityIdentifier(), diff --git a/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQuerySideIntegrationTest.java b/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQuerySideIntegrationTest.java index 6a0b0c3..6979bf7 100644 --- a/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQuerySideIntegrationTest.java +++ b/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQuerySideIntegrationTest.java @@ -45,9 +45,9 @@ public class AccountQuerySideIntegrationTest { @Test public void shouldUpdateQuerySide() throws Exception { - final EntityWithIdAndVersion fromAccount = await(accountService.openAccount(new BigDecimal(150))); + final EntityWithIdAndVersion fromAccount = await(accountService.openAccount("00000000-00000000", new BigDecimal(150))); - final EntityWithIdAndVersion toAccount = await(accountService.openAccount(new BigDecimal(300))); + final EntityWithIdAndVersion toAccount = await(accountService.openAccount("00000000-00000000", new BigDecimal(300))); final EntityWithIdAndVersion transaction = await( moneyTransferService.transferMoney(new TransferDetails(fromAccount.getEntityIdentifier(), diff --git a/java-spring/common-customers/build.gradle b/java-spring/common-customers/build.gradle index 6563876..ae3f264 100644 --- a/java-spring/common-customers/build.gradle +++ b/java-spring/common-customers/build.gradle @@ -1,5 +1,7 @@ apply plugin: 'java' dependencies { + compile "net.chrisrichardson.eventstore.client:eventstore-java-client_2.10:$eventStoreClientVersion" + testCompile group: 'junit', name: 'junit', version: '4.11' } diff --git a/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Address.java b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Address.java index 158ec05..814a577 100644 --- a/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Address.java +++ b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Address.java @@ -1,5 +1,8 @@ package net.chrisrichardson.eventstore.javaexamples.banking.common.customers; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; + /** * Created by popikyardo on 02.02.16. */ @@ -61,4 +64,14 @@ public class Address { public void setZipCode(String zipCode) { this.zipCode = zipCode; } + + @Override + public boolean equals(Object o) { + return EqualsBuilder.reflectionEquals(this, o); + } + + @Override + public int hashCode() { + return HashCodeBuilder.reflectionHashCode(this); + } } diff --git a/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java index 5c68f2c..dc8e8d4 100644 --- a/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java +++ b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java @@ -1,5 +1,8 @@ package net.chrisrichardson.eventstore.javaexamples.banking.common.customers; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; + /** * Created by popikyardo on 03.02.16. */ @@ -34,4 +37,14 @@ public class CustomerInfo { public Address getAddress() { return address; } + + @Override + public boolean equals(Object o) { + return EqualsBuilder.reflectionEquals(this, o); + } + + @Override + public int hashCode() { + return HashCodeBuilder.reflectionHashCode(this); + } } diff --git a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java index 7084b20..6a98bb7 100644 --- a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java +++ b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java @@ -20,8 +20,11 @@ public class Customer extends ReflectiveMutableCommandProcessingAggregate entityClass() { + return Customer.class; + } +} \ No newline at end of file diff --git a/java-spring/customers-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerTest.java b/java-spring/customers-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerTest.java new file mode 100644 index 0000000..cdb9f46 --- /dev/null +++ b/java-spring/customers-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerTest.java @@ -0,0 +1,43 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers; + +import net.chrisrichardson.eventstore.CommandProcessingAggregates; +import net.chrisrichardson.eventstore.Event; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.accounts.AccountOpenedEvent; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; +import org.junit.Assert; +import org.junit.Test; + +import java.math.BigDecimal; +import java.util.List; + +public class CustomerTest { + + @Test + public void testSomething() { + Customer customer = new Customer(); + + CustomerInfo customerInfo = generateCustomerInfo(); + + List events = CommandProcessingAggregates.processToList(customer, new CreateCustomerCommand(customerInfo)); + + Assert.assertEquals(1, events.size()); + Assert.assertEquals(AccountOpenedEvent.class, events.get(0).getClass()); + + customer.applyEvent(events.get(0)); + Assert.assertEquals(customerInfo, customer.getCustomerInfo()); + } + + private CustomerInfo generateCustomerInfo() { + return new CustomerInfo( + "current@email.com", + "000-00-0000", + "1-111-111-1111", + new Address("street 1", + "street 2", + "City", + "State", + "1111111") + ); + } +} diff --git a/java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceIntegrationTest.java b/java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceIntegrationTest.java new file mode 100644 index 0000000..f586ac8 --- /dev/null +++ b/java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceIntegrationTest.java @@ -0,0 +1,58 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web; + +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.client.RestTemplate; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = CustomersCommandSideServiceTestConfiguration.class) +@WebAppConfiguration +@IntegrationTest({"server.port=0", "management.port=0"}) +public class CustomersCommandSideServiceIntegrationTest { + + @Value("${local.server.port}") + private int port; + + private String baseUrl(String path) { + return "http://localhost:" + port + "/" + path; + } + + @Autowired + RestTemplate restTemplate; + + + @Test + public void shouldCreateCustomer() { + CustomerInfo customerInfo = generateCustomerInfo(); + + final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"),customerInfo, CustomerResponse.class).getBody(); + final String customerId = customerResponse.getId(); + + Assert.assertNotNull(customerId); + Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo()); + } + + private CustomerInfo generateCustomerInfo() { + return new CustomerInfo( + "current@email.com", + "000-00-0000", + "1-111-111-1111", + new Address("street 1", + "street 2", + "City", + "State", + "1111111") + ); + } + +} diff --git a/java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceTestConfiguration.java b/java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceTestConfiguration.java new file mode 100644 index 0000000..e44cc69 --- /dev/null +++ b/java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceTestConfiguration.java @@ -0,0 +1,26 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web; + +import org.springframework.boot.autoconfigure.web.HttpMessageConverters; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.web.client.RestTemplate; + +import java.util.Arrays; +import java.util.List; + +@Configuration +@Import(CustomersCommandSideServiceConfiguration.class) +public class CustomersCommandSideServiceTestConfiguration { + + @Bean + public RestTemplate restTemplate(HttpMessageConverters converters) { + RestTemplate restTemplate = new RestTemplate(); + HttpMessageConverter httpMessageConverter = converters.getConverters().get(0); + List> httpMessageConverters = Arrays.asList(new MappingJackson2HttpMessageConverter()); + restTemplate.setMessageConverters((List>) httpMessageConverters); + return restTemplate; + } +} diff --git a/java-spring/customers-query-side-service/build.gradle b/java-spring/customers-query-side-service/build.gradle index d248c6c..164f5ac 100644 --- a/java-spring/customers-query-side-service/build.gradle +++ b/java-spring/customers-query-side-service/build.gradle @@ -12,6 +12,7 @@ dependencies { compile "net.chrisrichardson.eventstore.client:eventstore-http-stomp-client_2.10:$eventStoreClientVersion" + testCompile project(":testutil") testCompile "org.springframework.boot:spring-boot-starter-test" } diff --git a/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceIntegrationTest.java b/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceIntegrationTest.java new file mode 100644 index 0000000..302d739 --- /dev/null +++ b/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceIntegrationTest.java @@ -0,0 +1,82 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web; + +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse; +import net.chrisrichardson.eventstorestore.javaexamples.testutil.Producer; +import net.chrisrichardson.eventstorestore.javaexamples.testutil.Verifier; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.client.RestTemplate; +import rx.Observable; + +import java.math.BigDecimal; + +import static net.chrisrichardson.eventstorestore.javaexamples.testutil.TestUtil.eventually; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = CustomersQuerySideServiceTestConfiguration.class) +@WebAppConfiguration +@IntegrationTest({"server.port=0", "management.port=0"}) +public class CustomersQuerySideServiceIntegrationTest { + + @Value("${local.server.port}") + private int port; + + private String baseUrl(String path) { + return "http://localhost:" + port + "/" + path; + } + + @Autowired + RestTemplate restTemplate; + + + @Test + public void shouldGetCustomerById() { + + CustomerInfo customerInfo = generateCustomerInfo(); + + final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"),customerInfo, CustomerResponse.class).getBody(); + final String customerId = customerResponse.getId(); + + assertCustomerResponse(customerId, customerInfo); + } + + private void assertCustomerResponse(final String customerId, final CustomerInfo customerInfo) { + eventually( + new Producer() { + @Override + public Observable produce() { + return Observable.just(restTemplate.getForEntity(baseUrl("/customers/" + customerId), CustomerResponse.class).getBody()); + } + }, + new Verifier() { + @Override + public void verify(CustomerResponse customerResponse) { + Assert.assertEquals(customerId, customerResponse.getId()); + Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo()); + } + }); + } + + private CustomerInfo generateCustomerInfo() { + return new CustomerInfo( + "current@email.com", + "000-00-0000", + "1-111-111-1111", + new Address("street 1", + "street 2", + "City", + "State", + "1111111") + ); + } + +} diff --git a/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceTestConfiguration.java b/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceTestConfiguration.java new file mode 100644 index 0000000..f040e32 --- /dev/null +++ b/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceTestConfiguration.java @@ -0,0 +1,26 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web; + +import org.springframework.boot.autoconfigure.web.HttpMessageConverters; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.web.client.RestTemplate; + +import java.util.Arrays; +import java.util.List; + +@Configuration +@Import(CustomersQuerySideServiceConfiguration.class) +public class CustomersQuerySideServiceTestConfiguration { + + @Bean + public RestTemplate restTemplate(HttpMessageConverters converters) { + RestTemplate restTemplate = new RestTemplate(); + HttpMessageConverter httpMessageConverter = converters.getConverters().get(0); + List> httpMessageConverters = Arrays.asList(new MappingJackson2HttpMessageConverter()); + restTemplate.setMessageConverters((List>) httpMessageConverters); + return restTemplate; + } +} From 59ecaa804ae20e0d32b0ea5493f3eb31db86d581 Mon Sep 17 00:00:00 2001 From: Main Date: Mon, 8 Feb 2016 13:25:09 +0300 Subject: [PATCH 06/10] updated swagger description --- .../schemas/java-mt-demo-extended-api.json | 106 +++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/java-spring/schemas/java-mt-demo-extended-api.json b/java-spring/schemas/java-mt-demo-extended-api.json index 6663d7a..0287e73 100644 --- a/java-spring/schemas/java-mt-demo-extended-api.json +++ b/java-spring/schemas/java-mt-demo-extended-api.json @@ -34,7 +34,81 @@ } ], "paths": { - "/authenticate": { + "/register/step_1": { + "post": { + "tags": [ + "auth-controller" + ], + "summary": "doRegisterStep1", + "operationId": "doRegisterStep1UsingPOST", + "consumes": [ + "application/json" + ], + "produces": [ + "*/*" + ], + "parameters": [ + { + "in": "body", + "name": "request", + "description": "request", + "required": true, + "schema": { + "$ref": "#/definitions/RegisterStep1Request" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/CustomerResponse" + } + }, + "400": { + "description": "Validation error" + } + } + } + }, + "/register/step_2": { + "post": { + "tags": [ + "auth-controller" + ], + "summary": "doRegisterStep1", + "operationId": "doRegisterStep1UsingPOST", + "consumes": [ + "application/json" + ], + "produces": [ + "*/*" + ], + "parameters": [ + { + "in": "body", + "name": "request", + "description": "request", + "required": true, + "schema": { + "$ref": "#/definitions/RegisterStep2Request" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/CustomerResponse" + } + }, + "400": { + "description": "Validation error" + } + } + } + }, + "/login": { "post": { "tags": [ "auth-controller" @@ -217,7 +291,7 @@ } }, "CustomerInfo": { - "required": [ "email", "ssn", "phoneNumber", "address" ], + "required": [ "email" ], "properties": { "email": { "type": "string" @@ -274,6 +348,34 @@ } } }, + "RegisterStep1Request": { + "required": [ "email" ], + "properties": { + "firstName": { + "type": "string" + }, + "lastName": { + "type": "string" + }, + "email": { + "type": "string" + } + } + }, + "RegisterStep2Request": { + "required": [ "ssn", "phoneNumber" ], + "properties": { + "ssn": { + "type": "string" + }, + "phoneNumber": { + "type": "string" + }, + "address": { + "$ref": "#/definitions/Address" + } + } + }, "Address": { "properties": { "street1": { From cd35ac3d31ca829fd0652e3110a5121b78e06e81 Mon Sep 17 00:00:00 2001 From: Main Date: Mon, 8 Feb 2016 22:54:03 +0300 Subject: [PATCH 07/10] - updated swagger description - added /customers/{id}/toaccounts endpoint --- .../backend/commandside/accounts/Account.java | 2 +- .../commandside/accounts/AccountService.java | 4 +- .../accounts/OpenAccountCommand.java | 9 ++- .../commandside/accounts/AccountTest.java | 3 +- ...untsCommandSideServiceIntegrationTest.java | 5 +- .../accounts/AccountController.java | 2 +- .../accounts/CreateAccountRequest.java | 15 ++++- .../queryside/accounts/AccountInfo.java | 8 ++- .../accounts/AccountInfoUpdateService.java | 3 +- .../accounts/AccountQueryWorkflow.java | 3 +- .../backend/MoneyTransferIntegrationTest.java | 8 +-- .../AccountQuerySideIntegrationTest.java | 4 +- .../common-auth-controller/build.gradle | 13 +++++ .../commonauth/controller/AuthController.java | 45 +++++++++++++++ .../banking/commonauth/model/AuthRequest.java | 29 ++++++++++ .../commonauth/model/AuthResponse.java | 23 ++++++++ .../banking/commonauth/AuthConfiguration.java | 4 +- .../common/accounts/AccountOpenedEvent.java | 8 ++- .../customers/CustomerAddedToAccount.java | 46 +++++++++++++++ .../customers/AddToAccountCommand.java | 29 ++++++++++ .../customers/CustomerService.java | 5 ++ .../customers/CustomerController.java | 17 ++++-- .../customers/ToAccountsRequest.java | 43 ++++++++++++++ .../customers/CustomerInfoUpdateService.java | 34 ++++++++--- .../customers/CustomerInfoWithId.java | 20 ------- .../customers/CustomerQueryService.java | 14 ++--- .../customers/CustomerQueryWorkflow.java | 16 +++++- .../customers/QuerySideCustomer.java | 40 +++++++++++++ .../QuerySideCustomerConfiguration.java | 6 +- ....java => QuerySideCustomerRepository.java} | 4 +- .../queryside/customers/ToAccountInfo.java | 29 ++++++++++ .../customers/CustomerQueryController.java | 14 ++--- .../schemas/java-mt-demo-extended-api.json | 57 ++++++++++++++++++- java-spring/settings.gradle | 1 + 34 files changed, 487 insertions(+), 76 deletions(-) create mode 100644 java-spring/common-auth-controller/build.gradle create mode 100644 java-spring/common-auth-controller/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/controller/AuthController.java create mode 100644 java-spring/common-auth-controller/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/AuthRequest.java create mode 100644 java-spring/common-auth-controller/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/AuthResponse.java create mode 100644 java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerAddedToAccount.java create mode 100644 java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/AddToAccountCommand.java create mode 100644 java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/ToAccountsRequest.java delete mode 100644 java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoWithId.java create mode 100644 java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomer.java rename java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/{CustomerInfoRepository.java => QuerySideCustomerRepository.java} (55%) create mode 100644 java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/ToAccountInfo.java diff --git a/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/Account.java b/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/Account.java index a8bbad3..090df31 100644 --- a/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/Account.java +++ b/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/Account.java @@ -16,7 +16,7 @@ public class Account extends ReflectiveMutableCommandProcessingAggregate process(OpenAccountCommand cmd) { - return EventUtil.events(new AccountOpenedEvent(cmd.getCustomerId(), cmd.getInitialBalance())); + return EventUtil.events(new AccountOpenedEvent(cmd.getCustomerId(), cmd.getTitle(), cmd.getInitialBalance())); } public List process(DebitAccountCommand cmd) { diff --git a/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/AccountService.java b/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/AccountService.java index 3c52964..c0ae752 100644 --- a/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/AccountService.java +++ b/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/AccountService.java @@ -14,8 +14,8 @@ public class AccountService { this.accountRepository = accountRepository; } - public rx.Observable> openAccount(String customerId, BigDecimal initialBalance) { - return accountRepository.save(new OpenAccountCommand(customerId, initialBalance)); + public rx.Observable> openAccount(String customerId, String title, BigDecimal initialBalance) { + return accountRepository.save(new OpenAccountCommand(customerId, title, initialBalance)); } } diff --git a/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/OpenAccountCommand.java b/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/OpenAccountCommand.java index 428bf55..4bd4216 100644 --- a/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/OpenAccountCommand.java +++ b/java-spring/accounts-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/OpenAccountCommand.java @@ -6,9 +6,12 @@ import java.math.BigDecimal; public class OpenAccountCommand implements AccountCommand { private String customerId; + private String title; private BigDecimal initialBalance; - public OpenAccountCommand(String customerId, BigDecimal initialBalance) { + public OpenAccountCommand(String customerId, String title, BigDecimal initialBalance) { + this.customerId = customerId; + this.title = title; this.initialBalance = initialBalance; } @@ -19,4 +22,8 @@ public class OpenAccountCommand implements AccountCommand { public String getCustomerId() { return customerId; } + + public String getTitle() { + return title; + } } diff --git a/java-spring/accounts-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/AccountTest.java b/java-spring/accounts-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/AccountTest.java index 7696265..15a139d 100644 --- a/java-spring/accounts-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/AccountTest.java +++ b/java-spring/accounts-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/accounts/AccountTest.java @@ -14,9 +14,10 @@ public class AccountTest { @Test public void testSomething() { Account account = new Account(); + String title = "My Account"; String customerId = "00000000-00000000"; BigDecimal initialBalance = new BigDecimal(512); - List events = CommandProcessingAggregates.processToList(account, (AccountCommand)new OpenAccountCommand(customerId, initialBalance)); + List events = CommandProcessingAggregates.processToList(account, (AccountCommand)new OpenAccountCommand(customerId, title, initialBalance)); Assert.assertEquals(1, events.size()); Assert.assertEquals(AccountOpenedEvent.class, events.get(0).getClass()); diff --git a/java-spring/accounts-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/AccountsCommandSideServiceIntegrationTest.java b/java-spring/accounts-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/AccountsCommandSideServiceIntegrationTest.java index c635ed4..fc0e60e 100644 --- a/java-spring/accounts-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/AccountsCommandSideServiceIntegrationTest.java +++ b/java-spring/accounts-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/AccountsCommandSideServiceIntegrationTest.java @@ -38,11 +38,12 @@ public class AccountsCommandSideServiceIntegrationTest { BigDecimal initialToAccountBalance = new BigDecimal(100); BigDecimal amountToTransfer = new BigDecimal(150); String customerId = "00000000-00000000"; + String title = "My Account"; - final CreateAccountResponse fromAccount = restTemplate.postForEntity(baseUrl("/accounts"), new CreateAccountRequest(customerId, initialFromAccountBalance), CreateAccountResponse.class).getBody(); + final CreateAccountResponse fromAccount = restTemplate.postForEntity(baseUrl("/accounts"), new CreateAccountRequest(customerId, title, initialFromAccountBalance), CreateAccountResponse.class).getBody(); final String fromAccountId = fromAccount.getAccountId(); - CreateAccountResponse toAccount = restTemplate.postForEntity(baseUrl("/accounts"), new CreateAccountRequest(customerId, initialToAccountBalance), CreateAccountResponse.class).getBody(); + CreateAccountResponse toAccount = restTemplate.postForEntity(baseUrl("/accounts"), new CreateAccountRequest(customerId, title, initialToAccountBalance), CreateAccountResponse.class).getBody(); String toAccountId = toAccount.getAccountId(); Assert.assertNotNull(fromAccountId); diff --git a/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/AccountController.java b/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/AccountController.java index 0236b2a..29c2b57 100644 --- a/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/AccountController.java +++ b/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/AccountController.java @@ -22,7 +22,7 @@ public class AccountController { @RequestMapping(method = RequestMethod.POST) public Observable createAccount(@Validated @RequestBody CreateAccountRequest request) { - return accountService.openAccount(request.getCustomerId(), request.getInitialBalance()) + return accountService.openAccount(request.getCustomerId(), request.getTitle(), request.getInitialBalance()) .map(entityAndEventInfo -> new CreateAccountResponse(entityAndEventInfo.getEntityIdentifier().getId())); } } diff --git a/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/CreateAccountRequest.java b/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/CreateAccountRequest.java index 2e43d05..202e62f 100644 --- a/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/CreateAccountRequest.java +++ b/java-spring/accounts-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/accounts/CreateAccountRequest.java @@ -10,6 +10,8 @@ public class CreateAccountRequest { @NotNull private String customerId; + private String title; + @NotNull @DecimalMin("0") private BigDecimal initialBalance; @@ -17,8 +19,9 @@ public class CreateAccountRequest { public CreateAccountRequest() { } - public CreateAccountRequest(String customerId, BigDecimal initialBalance) { - + public CreateAccountRequest(String customerId, String title, BigDecimal initialBalance) { + this.customerId = customerId; + this.title = title; this.initialBalance = initialBalance; } @@ -37,4 +40,12 @@ public class CreateAccountRequest { public void setInitialBalance(BigDecimal initialBalance) { this.initialBalance = initialBalance; } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } } diff --git a/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountInfo.java b/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountInfo.java index 6c53270..49ec50a 100644 --- a/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountInfo.java +++ b/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountInfo.java @@ -9,6 +9,7 @@ public class AccountInfo { private String id; private String customerId; + private String title; private long balance; private List changes; private List transactions; @@ -17,10 +18,11 @@ public class AccountInfo { private AccountInfo() { } - public AccountInfo(String id, String customerId, long balance, List changes, List transactions, String version) { + public AccountInfo(String id, String customerId, String title, long balance, List changes, List transactions, String version) { this.id = id; this.customerId = customerId; + this.title = title; this.balance = balance; this.changes = changes; this.transactions = transactions; @@ -35,6 +37,10 @@ public class AccountInfo { return customerId; } + public String getTitle() { + return title; + } + public long getBalance() { return balance; } diff --git a/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountInfoUpdateService.java b/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountInfoUpdateService.java index b5ad651..02cb584 100644 --- a/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountInfoUpdateService.java +++ b/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountInfoUpdateService.java @@ -26,11 +26,12 @@ public class AccountInfoUpdateService { - public void create(String accountId, String customerId, BigDecimal initialBalance, String version) { + public void create(String accountId, String customerId, String title, BigDecimal initialBalance, String version) { try { accountInfoRepository.save(new AccountInfo( accountId, customerId, + title, toIntegerRepr(initialBalance), Collections.emptyList(), Collections.emptyList(), diff --git a/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQueryWorkflow.java b/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQueryWorkflow.java index 2ec9039..934de55 100644 --- a/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQueryWorkflow.java +++ b/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQueryWorkflow.java @@ -38,7 +38,8 @@ public class AccountQueryWorkflow implements CompoundEventHandler { logger.info("**************** account version=" + id + ", " + eventId); BigDecimal initialBalance = event.getInitialBalance(); String customerId = event.getCustomerId(); - accountInfoUpdateService.create(id, customerId, initialBalance, eventId); + String title = event.getTitle(); + accountInfoUpdateService.create(id, customerId, title, initialBalance, eventId); return Observable.just(null); } diff --git a/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/MoneyTransferIntegrationTest.java b/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/MoneyTransferIntegrationTest.java index 9c9c54c..11c192e 100644 --- a/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/MoneyTransferIntegrationTest.java +++ b/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/MoneyTransferIntegrationTest.java @@ -43,9 +43,9 @@ public class MoneyTransferIntegrationTest { @Test public void shouldTransferMoney() { - final EntityWithIdAndVersion fromAccount= await(accountService.openAccount("00000000-00000000", new BigDecimal(150))); + final EntityWithIdAndVersion fromAccount= await(accountService.openAccount("00000000-00000000", "My Account", new BigDecimal(150))); - final EntityWithIdAndVersion toAccount = await(accountService.openAccount("00000000-00000000", new BigDecimal(300))); + final EntityWithIdAndVersion toAccount = await(accountService.openAccount("00000000-00000000", "My Account", new BigDecimal(300))); final EntityWithIdAndVersion transaction = await( moneyTransferService.transferMoney(new TransferDetails(fromAccount.getEntityIdentifier(), @@ -98,9 +98,9 @@ public class MoneyTransferIntegrationTest { @Test public void shouldFailDueToInsufficientFunds() { - final EntityWithIdAndVersion fromAccount= await(accountService.openAccount("00000000-00000000", new BigDecimal(150))); + final EntityWithIdAndVersion fromAccount= await(accountService.openAccount("00000000-00000000", "My Account", new BigDecimal(150))); - final EntityWithIdAndVersion toAccount = await(accountService.openAccount("00000000-00000000", new BigDecimal(300))); + final EntityWithIdAndVersion toAccount = await(accountService.openAccount("00000000-00000000", "My Account", new BigDecimal(300))); final EntityWithIdAndVersion transaction = await( moneyTransferService.transferMoney(new TransferDetails(fromAccount.getEntityIdentifier(), diff --git a/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQuerySideIntegrationTest.java b/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQuerySideIntegrationTest.java index 6979bf7..647aadf 100644 --- a/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQuerySideIntegrationTest.java +++ b/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQuerySideIntegrationTest.java @@ -45,9 +45,9 @@ public class AccountQuerySideIntegrationTest { @Test public void shouldUpdateQuerySide() throws Exception { - final EntityWithIdAndVersion fromAccount = await(accountService.openAccount("00000000-00000000", new BigDecimal(150))); + final EntityWithIdAndVersion fromAccount = await(accountService.openAccount("00000000-00000000", "My Account", new BigDecimal(150))); - final EntityWithIdAndVersion toAccount = await(accountService.openAccount("00000000-00000000", new BigDecimal(300))); + final EntityWithIdAndVersion toAccount = await(accountService.openAccount("00000000-00000000", "My Account", new BigDecimal(300))); final EntityWithIdAndVersion transaction = await( moneyTransferService.transferMoney(new TransferDetails(fromAccount.getEntityIdentifier(), diff --git a/java-spring/common-auth-controller/build.gradle b/java-spring/common-auth-controller/build.gradle new file mode 100644 index 0000000..fe0d772 --- /dev/null +++ b/java-spring/common-auth-controller/build.gradle @@ -0,0 +1,13 @@ +apply plugin: 'java' + +dependencies { + compile project(":common-auth") + + compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion" + + compile "org.springframework.security:spring-security-config:4.0.2.RELEASE" + compile "org.springframework.security:spring-security-web:4.0.2.RELEASE" + + + testCompile "junit:junit:4.11" +} diff --git a/java-spring/common-auth-controller/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/controller/AuthController.java b/java-spring/common-auth-controller/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/controller/AuthController.java new file mode 100644 index 0000000..9af29fd --- /dev/null +++ b/java-spring/common-auth-controller/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/controller/AuthController.java @@ -0,0 +1,45 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.commonauth.controller; + +import com.fasterxml.jackson.databind.ObjectMapper; +import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model.AuthRequest; +import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model.AuthResponse; +import net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.token.Token; +import org.springframework.security.core.token.TokenService; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.validation.Valid; +import java.io.IOException; + +import static org.springframework.web.bind.annotation.RequestMethod.POST; + +/** + * Created by popikyardo on 21.09.15. + */ +@RestController +@Validated +public class AuthController { + + @Autowired + private TokenService tokenService; + + private static ObjectMapper objectMapper = new ObjectMapper(); + + @RequestMapping(value = "/login", method = POST) + public ResponseEntity doAuth(@RequestBody @Valid AuthRequest request) throws IOException { + User user = new User(); + user.setEmail(request.getEmail()); + + Token token = tokenService.allocateToken(objectMapper.writeValueAsString(user)); + return ResponseEntity.status(HttpStatus.OK) + .body(new AuthResponse(token.getKey())); + } + + +} diff --git a/java-spring/common-auth-controller/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/AuthRequest.java b/java-spring/common-auth-controller/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/AuthRequest.java new file mode 100644 index 0000000..0b0f3a6 --- /dev/null +++ b/java-spring/common-auth-controller/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/AuthRequest.java @@ -0,0 +1,29 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model; + +import org.hibernate.validator.constraints.Email; +import org.hibernate.validator.constraints.NotBlank; + +/** + * Created by popikyardo on 19.10.15. + */ +public class AuthRequest { + + @NotBlank + @Email + private String email; + + public AuthRequest() { + } + + public AuthRequest(String email) { + this.email = email; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/java-spring/common-auth-controller/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/AuthResponse.java b/java-spring/common-auth-controller/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/AuthResponse.java new file mode 100644 index 0000000..0ef4336 --- /dev/null +++ b/java-spring/common-auth-controller/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/model/AuthResponse.java @@ -0,0 +1,23 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.commonauth.model; + +/** + * Created by popikyardo on 21.09.15. + */ +public class AuthResponse { + private String token; + + public AuthResponse() { + } + + public AuthResponse(String token) { + this.token = token; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } +} diff --git a/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/AuthConfiguration.java b/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/AuthConfiguration.java index 2a12c35..6a0808c 100755 --- a/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/AuthConfiguration.java +++ b/java-spring/common-auth/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/commonauth/AuthConfiguration.java @@ -55,7 +55,9 @@ public class AuthConfiguration extends WebSecurityConfigurerAdapter { .antMatchers("/js/**").permitAll() .antMatchers("/styles/**").permitAll() .antMatchers("/views/**").permitAll() - .antMatchers(HttpMethod.POST, "/authenticate").permitAll() + .antMatchers(HttpMethod.POST, "/register/step_1").permitAll() + .antMatchers(HttpMethod.POST, "/register/step_2").permitAll() + .antMatchers(HttpMethod.POST, "/login").permitAll() .anyRequest().authenticated().and() .addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService), UsernamePasswordAuthenticationFilter.class); } diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/accounts/AccountOpenedEvent.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/accounts/AccountOpenedEvent.java index 1d05768..af89e5b 100644 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/accounts/AccountOpenedEvent.java +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/accounts/AccountOpenedEvent.java @@ -8,13 +8,15 @@ import java.math.BigDecimal; public class AccountOpenedEvent implements Event { private String customerId; + private String title; private BigDecimal initialBalance; private AccountOpenedEvent() { } - public AccountOpenedEvent(String customerId, BigDecimal initialBalance) { + public AccountOpenedEvent(String customerId, String title, BigDecimal initialBalance) { this.customerId = customerId; + this.title = title; this.initialBalance = initialBalance; } @@ -22,6 +24,10 @@ public class AccountOpenedEvent implements Event { return customerId; } + public String getTitle() { + return title; + } + public BigDecimal getInitialBalance() { return initialBalance; } diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerAddedToAccount.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerAddedToAccount.java new file mode 100644 index 0000000..d1a17b5 --- /dev/null +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerAddedToAccount.java @@ -0,0 +1,46 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers; + +import net.chrisrichardson.eventstore.Event; + +/** + * Created by Main on 08.02.2016. + */ +public class CustomerAddedToAccount implements Event { + + private String accountId; + private String accountOwner; + private String title; + + public CustomerAddedToAccount() { + } + + public CustomerAddedToAccount(String accountId, String accountOwner, String title) { + this.accountId = accountId; + this.accountOwner = accountOwner; + this.title = title; + } + + public String getAccountId() { + return accountId; + } + + public void setAccountId(String accountId) { + this.accountId = accountId; + } + + public String getAccountOwner() { + return accountOwner; + } + + public void setAccountOwner(String accountOwner) { + this.accountOwner = accountOwner; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } +} diff --git a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/AddToAccountCommand.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/AddToAccountCommand.java new file mode 100644 index 0000000..7cb06c9 --- /dev/null +++ b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/AddToAccountCommand.java @@ -0,0 +1,29 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers; + +/** + * Created by Main on 08.02.2016. + */ +public class AddToAccountCommand implements CustomerCommand { + + private String accountId; + private String accountOwner; + private String title; + + public AddToAccountCommand(String accountId, String accountOwner, String title) { + this.accountId = accountId; + this.accountOwner = accountOwner; + this.title = title; + } + + public String getAccountId() { + return accountId; + } + + public String getAccountOwner() { + return accountOwner; + } + + public String getTitle() { + return title; + } +} diff --git a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java index c312a36..1e7a493 100644 --- a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java +++ b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java @@ -1,6 +1,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers; +import net.chrisrichardson.eventstore.EntityIdentifier; import net.chrisrichardson.eventstore.EntityWithIdAndVersion; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; import net.chrisrichardson.eventstore.repository.AggregateRepository; @@ -17,4 +18,8 @@ public class CustomerService { return accountRepository.save(new CreateCustomerCommand(customerInfo)); } + public rx.Observable> addToAccount(String customerId, String accountId, String title, String owner) { + return accountRepository.update(new EntityIdentifier(customerId), new AddToAccountCommand(accountId, title, owner)); + } + } diff --git a/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java index 1d6d231..6a19b99 100644 --- a/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java +++ b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java @@ -4,18 +4,16 @@ import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.c import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import rx.Observable; /** * Created by popikyardo on 03.02.16. */ @RestController -@RequestMapping("/accounts") +@RequestMapping("/customer") public class CustomerController { private CustomerService customerService; @@ -26,8 +24,15 @@ public class CustomerController { } @RequestMapping(method = RequestMethod.POST) - public Observable createAccount(@Validated @RequestBody CustomerInfo request) { + public Observable createCustomer(@Validated @RequestBody CustomerInfo request) { return customerService.createCustomer(request) .map(entityAndEventInfo -> new CustomerResponse(entityAndEventInfo.getEntityIdentifier().getId(), request)); } + + @RequestMapping(value = "/{id}/toaccounts", method = RequestMethod.POST) + public Observable> addToAccount(@PathVariable String id, @Validated @RequestBody ToAccountsRequest request) { + return customerService.addToAccount(id, request.getId(), request.getTitle(), request.getOwner()) + .map(entityAndEventInfo -> ResponseEntity.ok().build()); + } + } diff --git a/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/ToAccountsRequest.java b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/ToAccountsRequest.java new file mode 100644 index 0000000..1c218f3 --- /dev/null +++ b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/ToAccountsRequest.java @@ -0,0 +1,43 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers; + +/** + * Created by Main on 08.02.2016. + */ +public class ToAccountsRequest { + private String id; + private String title; + private String owner; + + public ToAccountsRequest() { + } + + public ToAccountsRequest(String id, String title, String owner) { + this.id = id; + this.title = title; + this.owner = owner; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } +} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java index 7acb109..07ec6bd 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java @@ -3,6 +3,13 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.cu import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.mongodb.core.query.Update; + +import java.util.Collections; + +import static org.springframework.data.mongodb.core.query.Criteria.where; /** * Created by Main on 04.02.2016. @@ -11,19 +18,25 @@ public class CustomerInfoUpdateService { private Logger logger = LoggerFactory.getLogger(getClass()); - private CustomerInfoRepository accountInfoRepository; + private QuerySideCustomerRepository accountInfoRepository; + private MongoTemplate mongoTemplate; - public CustomerInfoUpdateService(CustomerInfoRepository accountInfoRepository) { + public CustomerInfoUpdateService(QuerySideCustomerRepository accountInfoRepository, MongoTemplate mongoTemplate) { this.accountInfoRepository = accountInfoRepository; + this.mongoTemplate = mongoTemplate; } public void create(String id, CustomerInfo customerInfo) { try { - accountInfoRepository.save(new CustomerInfoWithId(id, - customerInfo.getEmail(), - customerInfo.getSsn(), - customerInfo.getPhoneNumber(), - customerInfo.getAddress()) + accountInfoRepository.save(new QuerySideCustomer(id, + "", + "", + customerInfo.getEmail(), + customerInfo.getSsn(), + customerInfo.getPhoneNumber(), + customerInfo.getAddress(), + Collections.emptyList() + ) ); logger.info("Saved in mongo"); } catch (Throwable t) { @@ -32,4 +45,11 @@ public class CustomerInfoUpdateService { } } + public void addToAccount(String id, ToAccountInfo accountInfo) { + mongoTemplate.updateMulti(new Query(where("id").is(id)), + new Update(). + push("toAccounts", accountInfo), + QuerySideCustomer.class); + } + } diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoWithId.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoWithId.java deleted file mode 100644 index 3aacdd9..0000000 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoWithId.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; - -import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address; -import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; - -/** - * Created by Main on 05.02.2016. - */ -public class CustomerInfoWithId extends CustomerInfo { - private String id; - - public CustomerInfoWithId(String id, String email, String ssn, String phoneNumber, Address address) { - super(email, ssn, phoneNumber, address); - this.id = id; - } - - public String getId() { - return id; - } -} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryService.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryService.java index 2c873ef..2862184 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryService.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryService.java @@ -7,22 +7,22 @@ import java.util.List; public class CustomerQueryService { - private CustomerInfoRepository customerInfoRepository; + private QuerySideCustomerRepository querySideCustomerRepository; - public CustomerQueryService(CustomerInfoRepository customerInfoRepository) { - this.customerInfoRepository = customerInfoRepository; + public CustomerQueryService(QuerySideCustomerRepository querySideCustomerRepository) { + this.querySideCustomerRepository = querySideCustomerRepository; } - public Observable findByCustomerId(EntityIdentifier customerId) { - CustomerInfoWithId customer = customerInfoRepository.findOne(customerId.getId()); + public Observable findByCustomerId(EntityIdentifier customerId) { + QuerySideCustomer customer = querySideCustomerRepository.findOne(customerId.getId()); if (customer == null) return Observable.error(new CustomerNotFoundException(customerId.getId())); else return Observable.just(customer); } - public Observable> findByEmail(String email){ - List customers = customerInfoRepository.findByEmailLike(email); + public Observable> findByEmail(String email){ + List customers = querySideCustomerRepository.findByEmailLike(email); if (customers.isEmpty()) return Observable.error(new CustomersNotFoundException()); else diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java index e68a6d7..2f49dd3 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java @@ -1,5 +1,6 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerAddedToAccount; import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerCreatedEvent; import net.chrisrichardson.eventstore.subscriptions.CompoundEventHandler; import net.chrisrichardson.eventstore.subscriptions.DispatchedEvent; @@ -28,10 +29,21 @@ public class CustomerQueryWorkflow implements CompoundEventHandler { public Observable create(DispatchedEvent de) { CustomerCreatedEvent event = de.event(); String id = de.getEntityIdentifier().getId(); - String eventId = de.eventId().asString(); - logger.info("**************** customer version=" + id + ", " + eventId); customerInfoUpdateService.create(id, event.getCustomerInfo()); return Observable.just(null); } + @EventHandlerMethod + public Observable addToAccount(DispatchedEvent de) { + CustomerAddedToAccount event = de.event(); + String id = de.getEntityIdentifier().getId(); + + ToAccountInfo toAccountInfo = new ToAccountInfo(event.getAccountId(), + event.getTitle(), + event.getAccountOwner()); + + customerInfoUpdateService.addToAccount(id, toAccountInfo); + return Observable.just(null); + } + } diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomer.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomer.java new file mode 100644 index 0000000..8bffb20 --- /dev/null +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomer.java @@ -0,0 +1,40 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; + +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; + +import java.util.List; + +/** + * Created by Main on 05.02.2016. + */ +public class QuerySideCustomer extends CustomerInfo { + private String id; + protected String firstName; + protected String lastName; + private List toAccounts; + + public QuerySideCustomer(String id, String firstName, String lastName, String email, String ssn, String phoneNumber, Address address, List toAccounts) { + super(email, ssn, phoneNumber, address); + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + this.toAccounts = toAccounts; + } + + public String getId() { + return id; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public List getToAccounts() { + return toAccounts; + } +} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java index 9be1fe0..6b9fdf2 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java @@ -19,12 +19,12 @@ public class QuerySideCustomerConfiguration { } @Bean - public CustomerInfoUpdateService customerInfoUpdateService(CustomerInfoRepository customerInfoRepository) { - return new CustomerInfoUpdateService(customerInfoRepository); + public CustomerInfoUpdateService customerInfoUpdateService(QuerySideCustomerRepository querySideCustomerRepository) { + return new CustomerInfoUpdateService(querySideCustomerRepository); } @Bean - public CustomerQueryService customerQueryService(CustomerInfoRepository accountInfoRepository) { + public CustomerQueryService customerQueryService(QuerySideCustomerRepository accountInfoRepository) { return new CustomerQueryService(accountInfoRepository); } diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoRepository.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerRepository.java similarity index 55% rename from java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoRepository.java rename to java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerRepository.java index 2111406..258a33a 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoRepository.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerRepository.java @@ -4,7 +4,7 @@ import org.springframework.data.mongodb.repository.MongoRepository; import java.util.List; -interface CustomerInfoRepository extends MongoRepository { +interface QuerySideCustomerRepository extends MongoRepository { - List findByEmailLike(String email); + List findByEmailLike(String email); } diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/ToAccountInfo.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/ToAccountInfo.java new file mode 100644 index 0000000..c47f39c --- /dev/null +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/ToAccountInfo.java @@ -0,0 +1,29 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; + + +/** + * Created by Main on 08.02.2016. + */ +public class ToAccountInfo { + private String id; + private String title; + private String owner; + + public ToAccountInfo(String id, String title, String owner) { + this.id = id; + this.title = title; + this.owner = owner; + } + + public String getId() { + return id; + } + + public String getTitle() { + return title; + } + + public String getOwner() { + return owner; + } +} diff --git a/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java b/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java index a0151c2..5035759 100644 --- a/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java +++ b/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java @@ -1,7 +1,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web.queryside.customers; import net.chrisrichardson.eventstore.EntityIdentifier; -import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomerInfoWithId; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.QuerySideCustomer; import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomerNotFoundException; import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomerQueryService; import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomersNotFoundException; @@ -52,14 +52,14 @@ public class CustomerQueryController { } - private CustomerResponse getCustomerResponse(CustomerInfoWithId customerInfoWithId) { - return new CustomerResponse(customerInfoWithId.getId(), new CustomerInfo(customerInfoWithId.getEmail(), - customerInfoWithId.getSsn(), - customerInfoWithId.getPhoneNumber(), - customerInfoWithId.getAddress())); + private CustomerResponse getCustomerResponse(QuerySideCustomer querySideCustomer) { + return new CustomerResponse(querySideCustomer.getId(), new CustomerInfo(querySideCustomer.getEmail(), + querySideCustomer.getSsn(), + querySideCustomer.getPhoneNumber(), + querySideCustomer.getAddress())); } - private CustomersQueryResponse getCustomersQueryResponse(List customersList) { + private CustomersQueryResponse getCustomersQueryResponse(List customersList) { return new CustomersQueryResponse(customersList .stream() .map(this::getCustomerResponse) diff --git a/java-spring/schemas/java-mt-demo-extended-api.json b/java-spring/schemas/java-mt-demo-extended-api.json index 0287e73..cb5fa93 100644 --- a/java-spring/schemas/java-mt-demo-extended-api.json +++ b/java-spring/schemas/java-mt-demo-extended-api.json @@ -272,6 +272,47 @@ } } } + }, + "/customers/{id}/toaccounts": { + "post": { + "tags": [ + "customer-service-command-side-controller" + ], + "summary": "addToAccount", + "operationId": "addToAccountUsingPOST", + "consumes": [ + "application/json" + ], + "produces": [ + "*/*" + ], + "parameters": [ + { + "name": "id", + "in": "path", + "description": "id", + "required": true, + "type": "string" + }, + { + "in": "body", + "name": "request", + "description": "request", + "required": true, + "schema": { + "$ref": "#/definitions/ToAccountsRequest" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Validation error" + } + } + } } }, "definitions": { @@ -376,6 +417,20 @@ } } }, + "ToAccountsRequest":{ + "required": [ "id", "owner" ], + "properties": { + "id": { + "type": "string" + }, + "title": { + "type": "string" + }, + "owner": { + "type": "string" + } + } + }, "Address": { "properties": { "street1": { @@ -392,7 +447,7 @@ }, "zipCode": { "type": "string" - }, + } } } } diff --git a/java-spring/settings.gradle b/java-spring/settings.gradle index 98b538a..bd1aad8 100644 --- a/java-spring/settings.gradle +++ b/java-spring/settings.gradle @@ -32,4 +32,5 @@ include 'customers-query-side-web' include 'common-customers' include 'customers-command-side-service' include 'customers-query-side-service' +include 'common-auth-controller' From 92d094022232191fc1cc6e6831cc5075c1ebb00b Mon Sep 17 00:00:00 2001 From: Main Date: Tue, 9 Feb 2016 22:17:27 +0300 Subject: [PATCH 08/10] - updated swagger description - changed customer registration endpoint - updated tests --- .../customers/CustomerCreatedEvent.java | 22 ++++- .../AccountOpenEventSerializationTest.java | 2 +- java-spring/common-customers/build.gradle | 1 + .../banking/common/customers/Address.java | 7 +- .../common/customers/CustomerInfo.java | 5 ++ .../customers/CreateCustomerCommand.java | 15 +++- .../commandside/customers/Customer.java | 2 +- .../customers/CustomerService.java | 4 +- .../commandside/customers/CustomerTest.java | 5 +- ...mersCommandSideServiceIntegrationTest.java | 3 +- .../customers/CreateCustomerRequest.java | 49 +++++++++++ .../customers/CustomerController.java | 7 +- .../customers/CustomerInfoUpdateService.java | 6 +- .../customers/CustomerQueryWorkflow.java | 2 +- .../QuerySideCustomerConfiguration.java | 4 +- ...ustomersQuerySideServiceConfiguration.java | 3 +- .../examples/bank/web/EndToEndTest.java | 4 +- .../web/BankingWebIntegrationTest.java | 4 +- .../schemas/java-mt-demo-extended-api.json | 88 ++----------------- 19 files changed, 124 insertions(+), 109 deletions(-) create mode 100644 java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CreateCustomerRequest.java diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java index 8e51505..bfec502 100644 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java @@ -8,12 +8,16 @@ import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Cust */ public class CustomerCreatedEvent implements Event { + private String firstName; + private String lastName; private CustomerInfo customerInfo; public CustomerCreatedEvent() { } - public CustomerCreatedEvent(CustomerInfo customerInfo) { + public CustomerCreatedEvent( String firstName, String lastName, CustomerInfo customerInfo) { + this.firstName = firstName; + this.lastName = lastName; this.customerInfo = customerInfo; } @@ -24,4 +28,20 @@ public class CustomerCreatedEvent implements Event { public void setCustomerInfo(CustomerInfo customerInfo) { this.customerInfo = customerInfo; } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } } diff --git a/java-spring/common-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/common/accounts/AccountOpenEventSerializationTest.java b/java-spring/common-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/common/accounts/AccountOpenEventSerializationTest.java index 72f8706..c9e401c 100644 --- a/java-spring/common-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/common/accounts/AccountOpenEventSerializationTest.java +++ b/java-spring/common-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/common/accounts/AccountOpenEventSerializationTest.java @@ -13,7 +13,7 @@ public class AccountOpenEventSerializationTest { @Test public void shouldSerde() { - AccountOpenedEvent event = new AccountOpenedEvent(new BigDecimal(55)); + AccountOpenedEvent event = new AccountOpenedEvent("00000000-00000000", "My Account", new BigDecimal(55)); String json = JSonMapper.toJson(event, EventStoreCommonObjectMapping.getObjectMapper()); System.out.println("json=" + json); diff --git a/java-spring/common-customers/build.gradle b/java-spring/common-customers/build.gradle index ae3f264..414e83f 100644 --- a/java-spring/common-customers/build.gradle +++ b/java-spring/common-customers/build.gradle @@ -1,6 +1,7 @@ apply plugin: 'java' dependencies { + compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion" compile "net.chrisrichardson.eventstore.client:eventstore-java-client_2.10:$eventStoreClientVersion" testCompile group: 'junit', name: 'junit', version: '4.11' diff --git a/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Address.java b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Address.java index 814a577..cb035f3 100644 --- a/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Address.java +++ b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Address.java @@ -3,15 +3,20 @@ package net.chrisrichardson.eventstore.javaexamples.banking.common.customers; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; +import javax.validation.constraints.NotNull; + /** * Created by popikyardo on 02.02.16. */ public class Address { - + @NotNull private String street1; private String street2; + @NotNull private String city; + @NotNull private String state; + @NotNull private String zipCode; public Address() { diff --git a/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java index dc8e8d4..5ff2097 100644 --- a/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java +++ b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java @@ -3,12 +3,17 @@ package net.chrisrichardson.eventstore.javaexamples.banking.common.customers; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; +import javax.validation.constraints.NotNull; + /** * Created by popikyardo on 03.02.16. */ public class CustomerInfo { + @NotNull protected String email; + @NotNull protected String ssn; + @NotNull protected String phoneNumber; protected Address address; diff --git a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java index 3fe5a8c..2d51b83 100644 --- a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java +++ b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java @@ -6,14 +6,25 @@ import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Cust * Created by popikyardo on 02.02.16. */ public class CreateCustomerCommand implements CustomerCommand { - + private String firstName; + private String lastName; private CustomerInfo customerInfo; - public CreateCustomerCommand(CustomerInfo customerInfo) { + public CreateCustomerCommand(String firstName, String lastName, CustomerInfo customerInfo) { + this.firstName = firstName; + this.lastName = lastName; this.customerInfo = customerInfo; } public CustomerInfo getCustomerInfo() { return customerInfo; } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } } diff --git a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java index 6a98bb7..7aa74e6 100644 --- a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java +++ b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java @@ -17,7 +17,7 @@ public class Customer extends ReflectiveMutableCommandProcessingAggregate process(CreateCustomerCommand cmd) { - return EventUtil.events(new CustomerCreatedEvent(cmd.getCustomerInfo())); + return EventUtil.events(new CustomerCreatedEvent(cmd.getFirstName(), cmd.getLastName(), cmd.getCustomerInfo())); } public void apply(CustomerCreatedEvent event) { diff --git a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java index 1e7a493..17bca9f 100644 --- a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java +++ b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java @@ -14,8 +14,8 @@ public class CustomerService { this.accountRepository = accountRepository; } - public rx.Observable> createCustomer(CustomerInfo customerInfo) { - return accountRepository.save(new CreateCustomerCommand(customerInfo)); + public rx.Observable> createCustomer(String firstName, String lastName, CustomerInfo customerInfo) { + return accountRepository.save(new CreateCustomerCommand(firstName, lastName, customerInfo)); } public rx.Observable> addToAccount(String customerId, String accountId, String title, String owner) { diff --git a/java-spring/customers-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerTest.java b/java-spring/customers-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerTest.java index cdb9f46..46ef41f 100644 --- a/java-spring/customers-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerTest.java +++ b/java-spring/customers-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerTest.java @@ -3,6 +3,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside. import net.chrisrichardson.eventstore.CommandProcessingAggregates; import net.chrisrichardson.eventstore.Event; import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.accounts.AccountOpenedEvent; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerCreatedEvent; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; import org.junit.Assert; @@ -19,10 +20,10 @@ public class CustomerTest { CustomerInfo customerInfo = generateCustomerInfo(); - List events = CommandProcessingAggregates.processToList(customer, new CreateCustomerCommand(customerInfo)); + List events = CommandProcessingAggregates.processToList(customer, new CreateCustomerCommand("John", "Doe", customerInfo)); Assert.assertEquals(1, events.size()); - Assert.assertEquals(AccountOpenedEvent.class, events.get(0).getClass()); + Assert.assertEquals(CustomerCreatedEvent.class, events.get(0).getClass()); customer.applyEvent(events.get(0)); Assert.assertEquals(customerInfo, customer.getCustomerInfo()); diff --git a/java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceIntegrationTest.java b/java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceIntegrationTest.java index f586ac8..3f42c87 100644 --- a/java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceIntegrationTest.java +++ b/java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceIntegrationTest.java @@ -3,6 +3,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse; +import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers.CreateCustomerRequest; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -35,7 +36,7 @@ public class CustomersCommandSideServiceIntegrationTest { public void shouldCreateCustomer() { CustomerInfo customerInfo = generateCustomerInfo(); - final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"),customerInfo, CustomerResponse.class).getBody(); + final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"),new CreateCustomerRequest("John", "Doe", customerInfo), CustomerResponse.class).getBody(); final String customerId = customerResponse.getId(); Assert.assertNotNull(customerId); diff --git a/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CreateCustomerRequest.java b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CreateCustomerRequest.java new file mode 100644 index 0000000..5657d1c --- /dev/null +++ b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CreateCustomerRequest.java @@ -0,0 +1,49 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers; + +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; + +import javax.validation.constraints.NotNull; + +/** + * Created by Main on 09.02.2016. + */ +public class CreateCustomerRequest { + @NotNull + private String firstName; + @NotNull + private String lastName; + private CustomerInfo customerInfo; + + public CreateCustomerRequest() { + } + + public CreateCustomerRequest(String firstName, String lastName, CustomerInfo customerInfo) { + this.firstName = firstName; + this.lastName = lastName; + this.customerInfo = customerInfo; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public CustomerInfo getCustomerInfo() { + return customerInfo; + } + + public void setCustomerInfo(CustomerInfo customerInfo) { + this.customerInfo = customerInfo; + } +} diff --git a/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java index 6a19b99..78086ca 100644 --- a/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java +++ b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java @@ -1,7 +1,6 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers; import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.CustomerService; -import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; @@ -24,9 +23,9 @@ public class CustomerController { } @RequestMapping(method = RequestMethod.POST) - public Observable createCustomer(@Validated @RequestBody CustomerInfo request) { - return customerService.createCustomer(request) - .map(entityAndEventInfo -> new CustomerResponse(entityAndEventInfo.getEntityIdentifier().getId(), request)); + public Observable createCustomer(@Validated @RequestBody CreateCustomerRequest request) { + return customerService.createCustomer(request.getFirstName(), request.getLastName(), request.getCustomerInfo()) + .map(entityAndEventInfo -> new CustomerResponse(entityAndEventInfo.getEntityIdentifier().getId(), request.getCustomerInfo())); } @RequestMapping(value = "/{id}/toaccounts", method = RequestMethod.POST) diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java index 07ec6bd..267910b 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java @@ -26,11 +26,11 @@ public class CustomerInfoUpdateService { this.mongoTemplate = mongoTemplate; } - public void create(String id, CustomerInfo customerInfo) { + public void create(String id, String firstName, String lastName, CustomerInfo customerInfo) { try { accountInfoRepository.save(new QuerySideCustomer(id, - "", - "", + firstName, + lastName, customerInfo.getEmail(), customerInfo.getSsn(), customerInfo.getPhoneNumber(), diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java index 2f49dd3..c1f62ca 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java @@ -30,7 +30,7 @@ public class CustomerQueryWorkflow implements CompoundEventHandler { CustomerCreatedEvent event = de.event(); String id = de.getEntityIdentifier().getId(); - customerInfoUpdateService.create(id, event.getCustomerInfo()); + customerInfoUpdateService.create(id, event.getFirstName(), event.getLastName(), event.getCustomerInfo()); return Observable.just(null); } @EventHandlerMethod diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java index 6b9fdf2..9afa06a 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java @@ -19,8 +19,8 @@ public class QuerySideCustomerConfiguration { } @Bean - public CustomerInfoUpdateService customerInfoUpdateService(QuerySideCustomerRepository querySideCustomerRepository) { - return new CustomerInfoUpdateService(querySideCustomerRepository); + public CustomerInfoUpdateService customerInfoUpdateService(QuerySideCustomerRepository querySideCustomerRepository, MongoTemplate mongoTemplate) { + return new CustomerInfoUpdateService(querySideCustomerRepository, mongoTemplate); } @Bean diff --git a/java-spring/customers-query-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceConfiguration.java b/java-spring/customers-query-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceConfiguration.java index 2ba0f24..e4e5986 100644 --- a/java-spring/customers-query-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceConfiguration.java +++ b/java-spring/customers-query-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceConfiguration.java @@ -1,6 +1,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web; import net.chrisrichardson.eventstore.client.config.EventStoreHttpClientConfiguration; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.QuerySideCustomerConfiguration; import net.chrisrichardson.eventstore.javaexamples.banking.commonswagger.CommonSwaggerConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.web.HttpMessageConverters; @@ -12,7 +13,7 @@ import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; @Configuration -@Import({CustomersQuerySideServiceConfiguration.class, EventStoreHttpClientConfiguration.class, CommonSwaggerConfiguration.class}) +@Import({QuerySideCustomerConfiguration.class, EventStoreHttpClientConfiguration.class, CommonSwaggerConfiguration.class}) @EnableAutoConfiguration @ComponentScan public class CustomersQuerySideServiceConfiguration { diff --git a/java-spring/e2e-test/src/test/java/net/chrisrichardson/eventstore/examples/bank/web/EndToEndTest.java b/java-spring/e2e-test/src/test/java/net/chrisrichardson/eventstore/examples/bank/web/EndToEndTest.java index f8ccf7e..5f99260 100644 --- a/java-spring/e2e-test/src/test/java/net/chrisrichardson/eventstore/examples/bank/web/EndToEndTest.java +++ b/java-spring/e2e-test/src/test/java/net/chrisrichardson/eventstore/examples/bank/web/EndToEndTest.java @@ -63,10 +63,10 @@ public class EndToEndTest { BigDecimal finalFromAccountBalance = initialFromAccountBalance.subtract(amountToTransfer); BigDecimal finalToAccountBalance = initialToAccountBalance.add(amountToTransfer); - final CreateAccountResponse fromAccount = restTemplate.postForEntity(accountsCommandSideBaseUrl("/accounts"), new CreateAccountRequest(initialFromAccountBalance), CreateAccountResponse.class).getBody(); + final CreateAccountResponse fromAccount = restTemplate.postForEntity(accountsCommandSideBaseUrl("/accounts"), new CreateAccountRequest("00000000-00000000", "My Account", initialFromAccountBalance), CreateAccountResponse.class).getBody(); final String fromAccountId = fromAccount.getAccountId(); - CreateAccountResponse toAccount = restTemplate.postForEntity(accountsCommandSideBaseUrl("/accounts"), new CreateAccountRequest(initialToAccountBalance), CreateAccountResponse.class).getBody(); + CreateAccountResponse toAccount = restTemplate.postForEntity(accountsCommandSideBaseUrl("/accounts"), new CreateAccountRequest("00000000-00000000", "My Account", initialToAccountBalance), CreateAccountResponse.class).getBody(); String toAccountId = toAccount.getAccountId(); Assert.assertNotNull(fromAccountId); diff --git a/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebIntegrationTest.java b/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebIntegrationTest.java index 2b0c05f..d6a8f89 100644 --- a/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebIntegrationTest.java +++ b/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebIntegrationTest.java @@ -50,10 +50,10 @@ public class BankingWebIntegrationTest { BigDecimal finalFromAccountBalance = initialFromAccountBalance.subtract(amountToTransfer); BigDecimal finalToAccountBalance = initialToAccountBalance.add(amountToTransfer); - final CreateAccountResponse fromAccount = restTemplate.postForEntity(baseUrl("/accounts"), new CreateAccountRequest(initialFromAccountBalance), CreateAccountResponse.class).getBody(); + final CreateAccountResponse fromAccount = restTemplate.postForEntity(baseUrl("/accounts"), new CreateAccountRequest("00000000-00000000", "My Account", initialFromAccountBalance), CreateAccountResponse.class).getBody(); final String fromAccountId = fromAccount.getAccountId(); - CreateAccountResponse toAccount = restTemplate.postForEntity(baseUrl("/accounts"), new CreateAccountRequest(initialToAccountBalance), CreateAccountResponse.class).getBody(); + CreateAccountResponse toAccount = restTemplate.postForEntity(baseUrl("/accounts"), new CreateAccountRequest("00000000-00000000", "My Account", initialToAccountBalance), CreateAccountResponse.class).getBody(); String toAccountId = toAccount.getAccountId(); Assert.assertNotNull(fromAccountId); diff --git a/java-spring/schemas/java-mt-demo-extended-api.json b/java-spring/schemas/java-mt-demo-extended-api.json index cb5fa93..7d1dd99 100644 --- a/java-spring/schemas/java-mt-demo-extended-api.json +++ b/java-spring/schemas/java-mt-demo-extended-api.json @@ -34,80 +34,6 @@ } ], "paths": { - "/register/step_1": { - "post": { - "tags": [ - "auth-controller" - ], - "summary": "doRegisterStep1", - "operationId": "doRegisterStep1UsingPOST", - "consumes": [ - "application/json" - ], - "produces": [ - "*/*" - ], - "parameters": [ - { - "in": "body", - "name": "request", - "description": "request", - "required": true, - "schema": { - "$ref": "#/definitions/RegisterStep1Request" - } - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/CustomerResponse" - } - }, - "400": { - "description": "Validation error" - } - } - } - }, - "/register/step_2": { - "post": { - "tags": [ - "auth-controller" - ], - "summary": "doRegisterStep1", - "operationId": "doRegisterStep1UsingPOST", - "consumes": [ - "application/json" - ], - "produces": [ - "*/*" - ], - "parameters": [ - { - "in": "body", - "name": "request", - "description": "request", - "required": true, - "schema": { - "$ref": "#/definitions/RegisterStep2Request" - } - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/CustomerResponse" - } - }, - "400": { - "description": "Validation error" - } - } - } - }, "/login": { "post": { "tags": [ @@ -224,7 +150,7 @@ "description": "customer", "required": true, "schema": { - "$ref": "#/definitions/CustomerInfo" + "$ref": "#/definitions/CreateCustomerRequest" } } ], @@ -389,8 +315,8 @@ } } }, - "RegisterStep1Request": { - "required": [ "email" ], + "CreateCustomerRequest": { + "required": [ "email", "ssn", "phoneNumber" ], "properties": { "firstName": { "type": "string" @@ -400,12 +326,7 @@ }, "email": { "type": "string" - } - } - }, - "RegisterStep2Request": { - "required": [ "ssn", "phoneNumber" ], - "properties": { + }, "ssn": { "type": "string" }, @@ -432,6 +353,7 @@ } }, "Address": { + "required": [ "street1", "city", "state", "zipCode" ], "properties": { "street1": { "type": "string" From c0a9d6ed7d74e1744e49a193af1736258a0a1120 Mon Sep 17 00:00:00 2001 From: Main Date: Wed, 10 Feb 2016 23:28:35 +0300 Subject: [PATCH 09/10] refactoring --- .../backend-integration-tests/build.gradle | 2 + .../CustomerQuerySideIntegrationTest.java | 80 +++++++++++++++++++ .../CustomerQuerySideTestConfiguration.java | 11 +++ .../customers/CustomerAddedToAccount.java | 35 ++------ .../customers/CustomerCreatedEvent.java | 23 +----- .../common/customers/CustomerInfo.java | 8 +- .../banking/common/customers/Name.java | 50 ++++++++++++ .../common}/customers/ToAccountInfo.java | 2 +- .../customers/AddToAccountCommand.java | 24 ++---- .../customers/CreateCustomerCommand.java | 14 +--- .../commandside/customers/Customer.java | 11 ++- .../customers/CustomerService.java | 9 ++- .../commandside/customers/CustomerTest.java | 48 +++++------ .../build.gradle | 6 +- ...mersCommandSideServiceIntegrationTest.java | 57 ++++++------- .../customers/CreateCustomerRequest.java | 49 ------------ .../customers/CustomerController.java | 14 ++-- .../customers/ToAccountsRequest.java | 43 ---------- .../customers/CustomerInfoUpdateService.java | 6 +- .../customers/CustomerQueryWorkflow.java | 10 +-- .../customers/QuerySideCustomer.java | 18 +---- .../customers-query-side-service/build.gradle | 6 ++ ...tomersQuerySideServiceIntegrationTest.java | 4 +- ...mersQuerySideServiceTestConfiguration.java | 2 +- .../customers/CustomerQueryController.java | 16 ++-- .../javaexamples/testutil/TestUtil.java | 2 +- 26 files changed, 276 insertions(+), 274 deletions(-) create mode 100644 java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQuerySideIntegrationTest.java create mode 100644 java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQuerySideTestConfiguration.java create mode 100644 java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Name.java rename java-spring/{customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside => common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common}/customers/ToAccountInfo.java (84%) delete mode 100644 java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CreateCustomerRequest.java delete mode 100644 java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/ToAccountsRequest.java diff --git a/java-spring/backend-integration-tests/build.gradle b/java-spring/backend-integration-tests/build.gradle index 64a8f56..60d4b97 100644 --- a/java-spring/backend-integration-tests/build.gradle +++ b/java-spring/backend-integration-tests/build.gradle @@ -5,6 +5,8 @@ dependencies { testCompile project(":accounts-command-side-backend") testCompile project(":transactions-command-side-backend") testCompile project(":accounts-query-side-backend") + testCompile project(":customers-command-side-backend") + testCompile project(":customers-query-side-backend") testCompile project(":testutil") testCompile "junit:junit:4.11" testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVersion" diff --git a/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQuerySideIntegrationTest.java b/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQuerySideIntegrationTest.java new file mode 100644 index 0000000..e92b13a --- /dev/null +++ b/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQuerySideIntegrationTest.java @@ -0,0 +1,80 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; + +import net.chrisrichardson.eventstore.EntityWithIdAndVersion; +import net.chrisrichardson.eventstore.EventStore; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.Customer; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.CustomerService; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts.AccountInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Name; +import net.chrisrichardson.eventstorestore.javaexamples.testutil.Producer; +import net.chrisrichardson.eventstorestore.javaexamples.testutil.Verifier; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import rx.Observable; + +import static net.chrisrichardson.eventstorestore.javaexamples.testutil.TestUtil.await; +import static net.chrisrichardson.eventstorestore.javaexamples.testutil.TestUtil.eventually; + +/** + * Created by Main on 10.02.2016. + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = CustomerQuerySideTestConfiguration.class) +@IntegrationTest +public class CustomerQuerySideIntegrationTest { + + @Autowired + private CustomerService customerService; + + @Autowired + private CustomerQueryService customerQueryService; + + @Autowired + private EventStore eventStore; + + @Test + public void shouldCreateCustomer() throws Exception { + CustomerInfo customerInfo = generateCustomerInfo(); + EntityWithIdAndVersion customer = await(customerService.createCustomer(customerInfo)); + + Thread.sleep(10000); + eventually( + new Producer() { + @Override + public Observable produce() { + return customerQueryService.findByCustomerId(customer.getEntityIdentifier()); + } + }, + new Verifier() { + @Override + public void verify(QuerySideCustomer querySideCustomer) { + Assert.assertEquals(customerInfo.getName(), querySideCustomer.getName()); + Assert.assertEquals(customerInfo.getSsn(), querySideCustomer.getSsn()); + Assert.assertEquals(customerInfo.getEmail(), querySideCustomer.getEmail()); + Assert.assertEquals(customerInfo.getPhoneNumber(), querySideCustomer.getPhoneNumber()); + Assert.assertEquals(customerInfo.getAddress(), querySideCustomer.getAddress()); + } + }); + } + + private CustomerInfo generateCustomerInfo() { + return new CustomerInfo( + new Name("John", "Doe"), + "current@email.com", + "000-00-0000", + "1-111-111-1111", + new Address("street 1", + "street 2", + "City", + "State", + "1111111") + ); + } +} diff --git a/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQuerySideTestConfiguration.java b/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQuerySideTestConfiguration.java new file mode 100644 index 0000000..070c50e --- /dev/null +++ b/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQuerySideTestConfiguration.java @@ -0,0 +1,11 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; + +import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.CustomerConfiguration; +import net.chrisrichardson.eventstore.jdbc.config.JdbcEventStoreConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; + +@Configuration +@Import({CustomerConfiguration.class, JdbcEventStoreConfiguration.class, QuerySideCustomerConfiguration.class}) +public class CustomerQuerySideTestConfiguration { +} diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerAddedToAccount.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerAddedToAccount.java index d1a17b5..b8dc08c 100644 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerAddedToAccount.java +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerAddedToAccount.java @@ -1,46 +1,27 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers; import net.chrisrichardson.eventstore.Event; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.ToAccountInfo; /** * Created by Main on 08.02.2016. */ public class CustomerAddedToAccount implements Event { - private String accountId; - private String accountOwner; - private String title; + private ToAccountInfo toAccountInfo; public CustomerAddedToAccount() { } - public CustomerAddedToAccount(String accountId, String accountOwner, String title) { - this.accountId = accountId; - this.accountOwner = accountOwner; - this.title = title; + public CustomerAddedToAccount( ToAccountInfo toAccountInfo) { + this.toAccountInfo = toAccountInfo; } - public String getAccountId() { - return accountId; + public ToAccountInfo getToAccountInfo() { + return toAccountInfo; } - public void setAccountId(String accountId) { - this.accountId = accountId; - } - - public String getAccountOwner() { - return accountOwner; - } - - public void setAccountOwner(String accountOwner) { - this.accountOwner = accountOwner; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; + public void setToAccountInfo(ToAccountInfo toAccountInfo) { + this.toAccountInfo = toAccountInfo; } } diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java index bfec502..ab719c9 100644 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java @@ -2,22 +2,19 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.custo import net.chrisrichardson.eventstore.Event; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Name; /** * Created by popikyardo on 02.02.16. */ public class CustomerCreatedEvent implements Event { - private String firstName; - private String lastName; private CustomerInfo customerInfo; public CustomerCreatedEvent() { } - public CustomerCreatedEvent( String firstName, String lastName, CustomerInfo customerInfo) { - this.firstName = firstName; - this.lastName = lastName; + public CustomerCreatedEvent(CustomerInfo customerInfo) { this.customerInfo = customerInfo; } @@ -28,20 +25,4 @@ public class CustomerCreatedEvent implements Event { public void setCustomerInfo(CustomerInfo customerInfo) { this.customerInfo = customerInfo; } - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } } diff --git a/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java index 5ff2097..4f85c42 100644 --- a/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java +++ b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/CustomerInfo.java @@ -9,6 +9,7 @@ import javax.validation.constraints.NotNull; * Created by popikyardo on 03.02.16. */ public class CustomerInfo { + private Name name; @NotNull protected String email; @NotNull @@ -20,13 +21,18 @@ public class CustomerInfo { public CustomerInfo() { } - public CustomerInfo(String email, String ssn, String phoneNumber, Address address) { + public CustomerInfo(Name name, String email, String ssn, String phoneNumber, Address address) { + this.name = name; this.email = email; this.ssn = ssn; this.phoneNumber = phoneNumber; this.address = address; } + public Name getName() { + return name; + } + public String getEmail() { return email; } diff --git a/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Name.java b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Name.java new file mode 100644 index 0000000..312363d --- /dev/null +++ b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/Name.java @@ -0,0 +1,50 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.common.customers; + +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; + +import javax.validation.constraints.NotNull; + +/** + * Created by Main on 10.02.2016. + */ +public class Name { + @NotNull + private String firstName; + @NotNull + private String lastName; + + public Name() { + } + + public Name(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + @Override + public boolean equals(Object o) { + return EqualsBuilder.reflectionEquals(this, o); + } + + @Override + public int hashCode() { + return HashCodeBuilder.reflectionHashCode(this); + } +} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/ToAccountInfo.java b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/ToAccountInfo.java similarity index 84% rename from java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/ToAccountInfo.java rename to java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/ToAccountInfo.java index c47f39c..5ca03fe 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/ToAccountInfo.java +++ b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/ToAccountInfo.java @@ -1,4 +1,4 @@ -package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; +package net.chrisrichardson.eventstore.javaexamples.banking.common.customers; /** diff --git a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/AddToAccountCommand.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/AddToAccountCommand.java index 7cb06c9..5365b2c 100644 --- a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/AddToAccountCommand.java +++ b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/AddToAccountCommand.java @@ -1,29 +1,19 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.ToAccountInfo; + /** * Created by Main on 08.02.2016. */ public class AddToAccountCommand implements CustomerCommand { - private String accountId; - private String accountOwner; - private String title; + private ToAccountInfo toAccountInfo; - public AddToAccountCommand(String accountId, String accountOwner, String title) { - this.accountId = accountId; - this.accountOwner = accountOwner; - this.title = title; + public AddToAccountCommand(ToAccountInfo toAccountInfo) { + this.toAccountInfo = toAccountInfo; } - public String getAccountId() { - return accountId; - } - - public String getAccountOwner() { - return accountOwner; - } - - public String getTitle() { - return title; + public ToAccountInfo getToAccountInfo() { + return toAccountInfo; } } diff --git a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java index 2d51b83..31ea7f8 100644 --- a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java +++ b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CreateCustomerCommand.java @@ -6,25 +6,13 @@ import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Cust * Created by popikyardo on 02.02.16. */ public class CreateCustomerCommand implements CustomerCommand { - private String firstName; - private String lastName; private CustomerInfo customerInfo; - public CreateCustomerCommand(String firstName, String lastName, CustomerInfo customerInfo) { - this.firstName = firstName; - this.lastName = lastName; + public CreateCustomerCommand(CustomerInfo customerInfo) { this.customerInfo = customerInfo; } public CustomerInfo getCustomerInfo() { return customerInfo; } - - public String getFirstName() { - return firstName; - } - - public String getLastName() { - return lastName; - } } diff --git a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java index 7aa74e6..90e6104 100644 --- a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java +++ b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/Customer.java @@ -3,6 +3,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside. import net.chrisrichardson.eventstore.Event; import net.chrisrichardson.eventstore.EventUtil; import net.chrisrichardson.eventstore.ReflectiveMutableCommandProcessingAggregate; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerAddedToAccount; import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerCreatedEvent; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; @@ -15,15 +16,21 @@ public class Customer extends ReflectiveMutableCommandProcessingAggregate process(CreateCustomerCommand cmd) { - return EventUtil.events(new CustomerCreatedEvent(cmd.getFirstName(), cmd.getLastName(), cmd.getCustomerInfo())); + return EventUtil.events(new CustomerCreatedEvent(cmd.getCustomerInfo())); + } + + public List process(AddToAccountCommand cmd) { + return EventUtil.events(new CustomerAddedToAccount(cmd.getToAccountInfo())); } public void apply(CustomerCreatedEvent event) { customerInfo = event.getCustomerInfo(); } + public void apply(CustomerAddedToAccount event) { + } + public CustomerInfo getCustomerInfo() { return customerInfo; } diff --git a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java index 17bca9f..0a7dfdb 100644 --- a/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java +++ b/java-spring/customers-command-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerService.java @@ -4,6 +4,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside. import net.chrisrichardson.eventstore.EntityIdentifier; import net.chrisrichardson.eventstore.EntityWithIdAndVersion; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.ToAccountInfo; import net.chrisrichardson.eventstore.repository.AggregateRepository; public class CustomerService { @@ -14,12 +15,12 @@ public class CustomerService { this.accountRepository = accountRepository; } - public rx.Observable> createCustomer(String firstName, String lastName, CustomerInfo customerInfo) { - return accountRepository.save(new CreateCustomerCommand(firstName, lastName, customerInfo)); + public rx.Observable> createCustomer(CustomerInfo customerInfo) { + return accountRepository.save(new CreateCustomerCommand(customerInfo)); } - public rx.Observable> addToAccount(String customerId, String accountId, String title, String owner) { - return accountRepository.update(new EntityIdentifier(customerId), new AddToAccountCommand(accountId, title, owner)); + public rx.Observable> addToAccount(String customerId, ToAccountInfo toAccountInfo) { + return accountRepository.update(new EntityIdentifier(customerId), new AddToAccountCommand(toAccountInfo)); } } diff --git a/java-spring/customers-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerTest.java b/java-spring/customers-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerTest.java index 46ef41f..19ae916 100644 --- a/java-spring/customers-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerTest.java +++ b/java-spring/customers-command-side-backend/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/commandside/customers/CustomerTest.java @@ -2,43 +2,43 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside. import net.chrisrichardson.eventstore.CommandProcessingAggregates; import net.chrisrichardson.eventstore.Event; -import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.accounts.AccountOpenedEvent; import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerCreatedEvent; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Name; import org.junit.Assert; import org.junit.Test; -import java.math.BigDecimal; import java.util.List; public class CustomerTest { - @Test - public void testSomething() { - Customer customer = new Customer(); + @Test + public void testSomething() { + Customer customer = new Customer(); - CustomerInfo customerInfo = generateCustomerInfo(); + CustomerInfo customerInfo = generateCustomerInfo(); - List events = CommandProcessingAggregates.processToList(customer, new CreateCustomerCommand("John", "Doe", customerInfo)); + List events = CommandProcessingAggregates.processToList(customer, new CreateCustomerCommand(customerInfo)); - Assert.assertEquals(1, events.size()); - Assert.assertEquals(CustomerCreatedEvent.class, events.get(0).getClass()); + Assert.assertEquals(1, events.size()); + Assert.assertEquals(CustomerCreatedEvent.class, events.get(0).getClass()); - customer.applyEvent(events.get(0)); - Assert.assertEquals(customerInfo, customer.getCustomerInfo()); - } + customer.applyEvent(events.get(0)); + Assert.assertEquals(customerInfo, customer.getCustomerInfo()); + } - private CustomerInfo generateCustomerInfo() { - return new CustomerInfo( - "current@email.com", - "000-00-0000", - "1-111-111-1111", - new Address("street 1", - "street 2", - "City", - "State", - "1111111") - ); - } + private CustomerInfo generateCustomerInfo() { + return new CustomerInfo( + new Name("John", "Doe"), + "current@email.com", + "000-00-0000", + "1-111-111-1111", + new Address("street 1", + "street 2", + "City", + "State", + "1111111") + ); + } } diff --git a/java-spring/customers-command-side-service/build.gradle b/java-spring/customers-command-side-service/build.gradle index 034e4f7..cf208cd 100644 --- a/java-spring/customers-command-side-service/build.gradle +++ b/java-spring/customers-command-side-service/build.gradle @@ -15,6 +15,8 @@ dependencies { testCompile "org.springframework.boot:spring-boot-starter-test" } -test { - ignoreFailures true +run { + environment 'EVENTUATE_API_KEY_ID', '20CAXGPA3DE56WXO2SFBUDGZ9' + environment 'EVENTUATE_API_KEY_SECRET', 'gU6n78drWCIgkgzVStvI3BhV3MfzDyjWKCN7p0PBimI' + environment 'SPRING_DATA_MONGODB_URI', 'mongodb://198.50.218.51/mydb' } diff --git a/java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceIntegrationTest.java b/java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceIntegrationTest.java index 3f42c87..5f6062a 100644 --- a/java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceIntegrationTest.java +++ b/java-spring/customers-command-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceIntegrationTest.java @@ -3,7 +3,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse; -import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers.CreateCustomerRequest; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Name; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -21,39 +21,40 @@ import org.springframework.web.client.RestTemplate; @IntegrationTest({"server.port=0", "management.port=0"}) public class CustomersCommandSideServiceIntegrationTest { - @Value("${local.server.port}") - private int port; + @Value("${local.server.port}") + private int port; - private String baseUrl(String path) { - return "http://localhost:" + port + "/" + path; - } + private String baseUrl(String path) { + return "http://localhost:" + port + "/" + path; + } - @Autowired - RestTemplate restTemplate; + @Autowired + RestTemplate restTemplate; - @Test - public void shouldCreateCustomer() { - CustomerInfo customerInfo = generateCustomerInfo(); + @Test + public void shouldCreateCustomer() { + CustomerInfo customerInfo = generateCustomerInfo(); - final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"),new CreateCustomerRequest("John", "Doe", customerInfo), CustomerResponse.class).getBody(); - final String customerId = customerResponse.getId(); + final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody(); + final String customerId = customerResponse.getId(); - Assert.assertNotNull(customerId); - Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo()); - } + Assert.assertNotNull(customerId); + Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo()); + } - private CustomerInfo generateCustomerInfo() { - return new CustomerInfo( - "current@email.com", - "000-00-0000", - "1-111-111-1111", - new Address("street 1", - "street 2", - "City", - "State", - "1111111") - ); - } + private CustomerInfo generateCustomerInfo() { + return new CustomerInfo( + new Name("John", "Doe"), + "current@email.com", + "000-00-0000", + "1-111-111-1111", + new Address("street 1", + "street 2", + "City", + "State", + "1111111") + ); + } } diff --git a/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CreateCustomerRequest.java b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CreateCustomerRequest.java deleted file mode 100644 index 5657d1c..0000000 --- a/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CreateCustomerRequest.java +++ /dev/null @@ -1,49 +0,0 @@ -package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers; - -import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; - -import javax.validation.constraints.NotNull; - -/** - * Created by Main on 09.02.2016. - */ -public class CreateCustomerRequest { - @NotNull - private String firstName; - @NotNull - private String lastName; - private CustomerInfo customerInfo; - - public CreateCustomerRequest() { - } - - public CreateCustomerRequest(String firstName, String lastName, CustomerInfo customerInfo) { - this.firstName = firstName; - this.lastName = lastName; - this.customerInfo = customerInfo; - } - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - public CustomerInfo getCustomerInfo() { - return customerInfo; - } - - public void setCustomerInfo(CustomerInfo customerInfo) { - this.customerInfo = customerInfo; - } -} diff --git a/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java index 78086ca..5590f29 100644 --- a/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java +++ b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomerController.java @@ -1,7 +1,9 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers; import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.CustomerService; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.ToAccountInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; @@ -12,7 +14,7 @@ import rx.Observable; * Created by popikyardo on 03.02.16. */ @RestController -@RequestMapping("/customer") +@RequestMapping("/customers") public class CustomerController { private CustomerService customerService; @@ -23,14 +25,14 @@ public class CustomerController { } @RequestMapping(method = RequestMethod.POST) - public Observable createCustomer(@Validated @RequestBody CreateCustomerRequest request) { - return customerService.createCustomer(request.getFirstName(), request.getLastName(), request.getCustomerInfo()) - .map(entityAndEventInfo -> new CustomerResponse(entityAndEventInfo.getEntityIdentifier().getId(), request.getCustomerInfo())); + public Observable createCustomer(@Validated @RequestBody CustomerInfo request) { + return customerService.createCustomer(request) + .map(entityAndEventInfo -> new CustomerResponse(entityAndEventInfo.getEntityIdentifier().getId(), request)); } @RequestMapping(value = "/{id}/toaccounts", method = RequestMethod.POST) - public Observable> addToAccount(@PathVariable String id, @Validated @RequestBody ToAccountsRequest request) { - return customerService.addToAccount(id, request.getId(), request.getTitle(), request.getOwner()) + public Observable> addToAccount(@PathVariable String id, @Validated @RequestBody ToAccountInfo request) { + return customerService.addToAccount(id, request) .map(entityAndEventInfo -> ResponseEntity.ok().build()); } diff --git a/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/ToAccountsRequest.java b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/ToAccountsRequest.java deleted file mode 100644 index 1c218f3..0000000 --- a/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/ToAccountsRequest.java +++ /dev/null @@ -1,43 +0,0 @@ -package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers; - -/** - * Created by Main on 08.02.2016. - */ -public class ToAccountsRequest { - private String id; - private String title; - private String owner; - - public ToAccountsRequest() { - } - - public ToAccountsRequest(String id, String title, String owner) { - this.id = id; - this.title = title; - this.owner = owner; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getOwner() { - return owner; - } - - public void setOwner(String owner) { - this.owner = owner; - } -} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java index 267910b..1de7c77 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java @@ -1,6 +1,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.ToAccountInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.mongodb.core.MongoTemplate; @@ -26,11 +27,10 @@ public class CustomerInfoUpdateService { this.mongoTemplate = mongoTemplate; } - public void create(String id, String firstName, String lastName, CustomerInfo customerInfo) { + public void create(String id, CustomerInfo customerInfo) { try { accountInfoRepository.save(new QuerySideCustomer(id, - firstName, - lastName, + customerInfo.getName(), customerInfo.getEmail(), customerInfo.getSsn(), customerInfo.getPhoneNumber(), diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java index c1f62ca..800f19b 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java @@ -2,6 +2,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.cu import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerAddedToAccount; import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers.CustomerCreatedEvent; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.ToAccountInfo; import net.chrisrichardson.eventstore.subscriptions.CompoundEventHandler; import net.chrisrichardson.eventstore.subscriptions.DispatchedEvent; import net.chrisrichardson.eventstore.subscriptions.EventHandlerMethod; @@ -13,7 +14,7 @@ import rx.Observable; /** * Created by Main on 04.02.2016. */ -@EventSubscriber(id="querySideEventHandlers") +@EventSubscriber(id = "querySideEventHandlers") public class CustomerQueryWorkflow implements CompoundEventHandler { private Logger logger = LoggerFactory.getLogger(getClass()); @@ -30,17 +31,16 @@ public class CustomerQueryWorkflow implements CompoundEventHandler { CustomerCreatedEvent event = de.event(); String id = de.getEntityIdentifier().getId(); - customerInfoUpdateService.create(id, event.getFirstName(), event.getLastName(), event.getCustomerInfo()); + customerInfoUpdateService.create(id, event.getCustomerInfo()); return Observable.just(null); } + @EventHandlerMethod public Observable addToAccount(DispatchedEvent de) { CustomerAddedToAccount event = de.event(); String id = de.getEntityIdentifier().getId(); - ToAccountInfo toAccountInfo = new ToAccountInfo(event.getAccountId(), - event.getTitle(), - event.getAccountOwner()); + ToAccountInfo toAccountInfo = event.getToAccountInfo(); customerInfoUpdateService.addToAccount(id, toAccountInfo); return Observable.just(null); diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomer.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomer.java index 8bffb20..6dd8dc2 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomer.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomer.java @@ -2,6 +2,8 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.cu import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Name; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.ToAccountInfo; import java.util.List; @@ -10,15 +12,11 @@ import java.util.List; */ public class QuerySideCustomer extends CustomerInfo { private String id; - protected String firstName; - protected String lastName; private List toAccounts; - public QuerySideCustomer(String id, String firstName, String lastName, String email, String ssn, String phoneNumber, Address address, List toAccounts) { - super(email, ssn, phoneNumber, address); + public QuerySideCustomer(String id, Name name, String email, String ssn, String phoneNumber, Address address, List toAccounts) { + super(name, email, ssn, phoneNumber, address); this.id = id; - this.firstName = firstName; - this.lastName = lastName; this.toAccounts = toAccounts; } @@ -26,14 +24,6 @@ public class QuerySideCustomer extends CustomerInfo { return id; } - public String getFirstName() { - return firstName; - } - - public String getLastName() { - return lastName; - } - public List getToAccounts() { return toAccounts; } diff --git a/java-spring/customers-query-side-service/build.gradle b/java-spring/customers-query-side-service/build.gradle index 164f5ac..56fcd4a 100644 --- a/java-spring/customers-query-side-service/build.gradle +++ b/java-spring/customers-query-side-service/build.gradle @@ -13,9 +13,15 @@ dependencies { compile "net.chrisrichardson.eventstore.client:eventstore-http-stomp-client_2.10:$eventStoreClientVersion" testCompile project(":testutil") + testCompile project(":customers-command-side-service") testCompile "org.springframework.boot:spring-boot-starter-test" } test { ignoreFailures true + + environment 'EVENTUATE_API_KEY_ID', '20CAXGPA3DE56WXO2SFBUDGZ9' + environment 'EVENTUATE_API_KEY_SECRET', 'gU6n78drWCIgkgzVStvI3BhV3MfzDyjWKCN7p0PBimI' + environment 'SPRING_DATA_MONGODB_URI', 'mongodb://198.50.218.51/mydb' } + diff --git a/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceIntegrationTest.java b/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceIntegrationTest.java index 302d739..b92b13a 100644 --- a/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceIntegrationTest.java +++ b/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceIntegrationTest.java @@ -3,6 +3,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Name; import net.chrisrichardson.eventstorestore.javaexamples.testutil.Producer; import net.chrisrichardson.eventstorestore.javaexamples.testutil.Verifier; import org.junit.Assert; @@ -17,8 +18,6 @@ import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.web.client.RestTemplate; import rx.Observable; -import java.math.BigDecimal; - import static net.chrisrichardson.eventstorestore.javaexamples.testutil.TestUtil.eventually; @RunWith(SpringJUnit4ClassRunner.class) @@ -68,6 +67,7 @@ public class CustomersQuerySideServiceIntegrationTest { private CustomerInfo generateCustomerInfo() { return new CustomerInfo( + new Name("John", "Doe"), "current@email.com", "000-00-0000", "1-111-111-1111", diff --git a/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceTestConfiguration.java b/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceTestConfiguration.java index f040e32..021d692 100644 --- a/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceTestConfiguration.java +++ b/java-spring/customers-query-side-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersQuerySideServiceTestConfiguration.java @@ -12,7 +12,7 @@ import java.util.Arrays; import java.util.List; @Configuration -@Import(CustomersQuerySideServiceConfiguration.class) +@Import({CustomersQuerySideServiceConfiguration.class, CustomersCommandSideServiceConfiguration.class}) public class CustomersQuerySideServiceTestConfiguration { @Bean diff --git a/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java b/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java index 5035759..7e3f1b6 100644 --- a/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java +++ b/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java @@ -1,11 +1,10 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web.queryside.customers; import net.chrisrichardson.eventstore.EntityIdentifier; -import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.QuerySideCustomer; import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomerNotFoundException; import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomerQueryService; import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomersNotFoundException; -import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; +import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.QuerySideCustomer; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -28,35 +27,32 @@ public class CustomerQueryController { this.customerQueryService = customerQueryService; } - @RequestMapping(value="/customers/{customerId}", method = RequestMethod.GET) + @RequestMapping(value = "/customers/{customerId}", method = RequestMethod.GET) public Observable getCustomer(@PathVariable String customerId) { return customerQueryService.findByCustomerId(new EntityIdentifier(customerId)) .map(this::getCustomerResponse); } - @RequestMapping(value="/customers", method = RequestMethod.GET) + @RequestMapping(value = "/customers", method = RequestMethod.GET) public Observable getCustomersByEmail(@RequestParam String email) { return customerQueryService.findByEmail(email) .map(this::getCustomersQueryResponse); } - @ResponseStatus(value= HttpStatus.NOT_FOUND, reason="customer not found") + @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "customer not found") @ExceptionHandler(CustomerNotFoundException.class) public void customerNotFound() { } - @ResponseStatus(value= HttpStatus.NOT_FOUND, reason="customers not found") + @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "customers not found") @ExceptionHandler(CustomersNotFoundException.class) public void customersNotFound() { } private CustomerResponse getCustomerResponse(QuerySideCustomer querySideCustomer) { - return new CustomerResponse(querySideCustomer.getId(), new CustomerInfo(querySideCustomer.getEmail(), - querySideCustomer.getSsn(), - querySideCustomer.getPhoneNumber(), - querySideCustomer.getAddress())); + return new CustomerResponse(querySideCustomer.getId(), querySideCustomer); } private CustomersQueryResponse getCustomersQueryResponse(List customersList) { diff --git a/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/TestUtil.java b/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/TestUtil.java index 21fbe85..7dc39db 100644 --- a/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/TestUtil.java +++ b/java-spring/testutil/src/main/java/net/chrisrichardson/eventstorestore/javaexamples/testutil/TestUtil.java @@ -47,7 +47,7 @@ public class TestUtil { public static void eventually(final Producer producer, final Verifier verifier) { final int n = 50; - Object possibleException = Observable.timer(0, 100, TimeUnit.MILLISECONDS).flatMap(new Func1>>() { + Object possibleException = Observable.timer(0, 200, TimeUnit.MILLISECONDS).flatMap(new Func1>>() { @Override public Observable> call(Long aLong) { From 5c85418cc43318eb2a9c3fa1a4c94c124a17676c Mon Sep 17 00:00:00 2001 From: Main Date: Thu, 11 Feb 2016 21:47:48 +0300 Subject: [PATCH 10/10] - completed moving "toAccounts" from List to Map - created CustomerEvent superclass - removed redundant CustomersNotFoundException - added integration tests for customers --- .../accounts/AccountQueryWorkflow.java | 2 +- .../CustomerQuerySideIntegrationTest.java | 15 +++++- .../customers/CustomerAddedToAccount.java | 5 +- .../customers/CustomerCreatedEvent.java | 4 +- .../common/customers/CustomerEvent.java | 11 +++++ .../common/customers/package-info.java | 2 - .../common/customers/ToAccountInfo.java | 28 +++++++++++ ...tomersCommandSideServiceConfiguration.java | 4 +- ...CustomersCommandSideWebConfiguration.java} | 2 +- .../customers/CustomerInfoUpdateService.java | 18 ++----- .../customers/CustomerNotFoundException.java | 8 ---- .../customers/CustomerQueryService.java | 5 +- .../customers/CustomerQueryWorkflow.java | 2 +- .../customers/CustomersNotFoundException.java | 8 ---- .../customers/QuerySideCustomer.java | 8 ++-- .../QuerySideCustomerConfiguration.java | 4 +- .../customers/CustomerQueryController.java | 10 +--- .../examples/bank/web/EndToEndTest.java | 4 +- java-spring/monolithic-service/build.gradle | 2 + .../banking/web/BankingWebConfiguration.java | 4 +- .../web/BankingWebIntegrationTest.java | 48 +++++++++++++++++++ 21 files changed, 131 insertions(+), 63 deletions(-) create mode 100644 java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerEvent.java delete mode 100644 java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/package-info.java rename java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/{CommandSideWebCustomersConfiguration.java => CustomersCommandSideWebConfiguration.java} (95%) delete mode 100644 java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerNotFoundException.java delete mode 100644 java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomersNotFoundException.java diff --git a/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQueryWorkflow.java b/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQueryWorkflow.java index 934de55..64ed8f4 100644 --- a/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQueryWorkflow.java +++ b/java-spring/accounts-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/accounts/AccountQueryWorkflow.java @@ -19,7 +19,7 @@ import java.math.BigDecimal; import static net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.accounts.MoneyUtil.toIntegerRepr; -@EventSubscriber(id="querySideEventHandlers") +@EventSubscriber(id="accountQuerySideEventHandlers") public class AccountQueryWorkflow implements CompoundEventHandler { private Logger logger = LoggerFactory.getLogger(getClass()); diff --git a/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQuerySideIntegrationTest.java b/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQuerySideIntegrationTest.java index e92b13a..3398c51 100644 --- a/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQuerySideIntegrationTest.java +++ b/java-spring/backend-integration-tests/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQuerySideIntegrationTest.java @@ -8,6 +8,7 @@ import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.acc import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Address; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Name; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.ToAccountInfo; import net.chrisrichardson.eventstorestore.javaexamples.testutil.Producer; import net.chrisrichardson.eventstorestore.javaexamples.testutil.Verifier; import org.junit.Assert; @@ -40,11 +41,13 @@ public class CustomerQuerySideIntegrationTest { private EventStore eventStore; @Test - public void shouldCreateCustomer() throws Exception { + public void shouldCreateCustomerAndAddToAccount() throws Exception { CustomerInfo customerInfo = generateCustomerInfo(); EntityWithIdAndVersion customer = await(customerService.createCustomer(customerInfo)); - Thread.sleep(10000); + ToAccountInfo toAccountInfo = generateToAccountInfo(); + EntityWithIdAndVersion customerWithNewAccount = await(customerService.addToAccount(customer.getEntityIdentifier().getId(), toAccountInfo)); + eventually( new Producer() { @Override @@ -60,6 +63,10 @@ public class CustomerQuerySideIntegrationTest { Assert.assertEquals(customerInfo.getEmail(), querySideCustomer.getEmail()); Assert.assertEquals(customerInfo.getPhoneNumber(), querySideCustomer.getPhoneNumber()); Assert.assertEquals(customerInfo.getAddress(), querySideCustomer.getAddress()); + + Assert.assertNotNull(querySideCustomer.getToAccounts()); + Assert.assertFalse(querySideCustomer.getToAccounts().isEmpty()); + Assert.assertEquals(querySideCustomer.getToAccounts().get("11111111-11111111"), toAccountInfo); } }); } @@ -77,4 +84,8 @@ public class CustomerQuerySideIntegrationTest { "1111111") ); } + + private ToAccountInfo generateToAccountInfo() { + return new ToAccountInfo("11111111-11111111", "New Account", "John Doe"); + } } diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerAddedToAccount.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerAddedToAccount.java index b8dc08c..c487e86 100644 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerAddedToAccount.java +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerAddedToAccount.java @@ -1,19 +1,18 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers; -import net.chrisrichardson.eventstore.Event; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.ToAccountInfo; /** * Created by Main on 08.02.2016. */ -public class CustomerAddedToAccount implements Event { +public class CustomerAddedToAccount extends CustomerEvent { private ToAccountInfo toAccountInfo; public CustomerAddedToAccount() { } - public CustomerAddedToAccount( ToAccountInfo toAccountInfo) { + public CustomerAddedToAccount(ToAccountInfo toAccountInfo) { this.toAccountInfo = toAccountInfo; } diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java index ab719c9..723d6e6 100644 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerCreatedEvent.java @@ -1,13 +1,11 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers; -import net.chrisrichardson.eventstore.Event; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerInfo; -import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Name; /** * Created by popikyardo on 02.02.16. */ -public class CustomerCreatedEvent implements Event { +public class CustomerCreatedEvent extends CustomerEvent { private CustomerInfo customerInfo; diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerEvent.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerEvent.java new file mode 100644 index 0000000..14cbdfa --- /dev/null +++ b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/CustomerEvent.java @@ -0,0 +1,11 @@ +package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers; + +import net.chrisrichardson.eventstore.Event; +import net.chrisrichardson.eventstore.EventEntity; + +/** + * Created by Main on 11.02.2016. + */ +@EventEntity(entity="net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.Customer") +public abstract class CustomerEvent implements Event { +} \ No newline at end of file diff --git a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/package-info.java b/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/package-info.java deleted file mode 100644 index ba4aa66..0000000 --- a/java-spring/common-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/common/customers/package-info.java +++ /dev/null @@ -1,2 +0,0 @@ -@net.chrisrichardson.eventstore.EventEntity(entity="net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.customers.Customer") -package net.chrisrichardson.eventstore.javaexamples.banking.backend.common.customers; \ No newline at end of file diff --git a/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/ToAccountInfo.java b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/ToAccountInfo.java index 5ca03fe..c90b6c1 100644 --- a/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/ToAccountInfo.java +++ b/java-spring/common-customers/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/common/customers/ToAccountInfo.java @@ -1,6 +1,9 @@ package net.chrisrichardson.eventstore.javaexamples.banking.common.customers; +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; + /** * Created by Main on 08.02.2016. */ @@ -9,6 +12,9 @@ public class ToAccountInfo { private String title; private String owner; + public ToAccountInfo() { + } + public ToAccountInfo(String id, String title, String owner) { this.id = id; this.title = title; @@ -19,11 +25,33 @@ public class ToAccountInfo { return id; } + public void setId(String id) { + this.id = id; + } + public String getTitle() { return title; } + public void setTitle(String title) { + this.title = title; + } + public String getOwner() { return owner; } + + public void setOwner(String owner) { + this.owner = owner; + } + + @Override + public boolean equals(Object o) { + return EqualsBuilder.reflectionEquals(this, o); + } + + @Override + public int hashCode() { + return HashCodeBuilder.reflectionHashCode(this); + } } diff --git a/java-spring/customers-command-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceConfiguration.java b/java-spring/customers-command-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceConfiguration.java index eef4d14..ca0767b 100644 --- a/java-spring/customers-command-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceConfiguration.java +++ b/java-spring/customers-command-side-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/CustomersCommandSideServiceConfiguration.java @@ -2,7 +2,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web; import net.chrisrichardson.eventstore.client.config.EventStoreHttpClientConfiguration; import net.chrisrichardson.eventstore.javaexamples.banking.commonswagger.CommonSwaggerConfiguration; -import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers.CommandSideWebCustomersConfiguration; +import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers.CustomersCommandSideWebConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.web.HttpMessageConverters; import org.springframework.context.annotation.Bean; @@ -13,7 +13,7 @@ import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; @Configuration -@Import({CommandSideWebCustomersConfiguration.class, EventStoreHttpClientConfiguration.class, CommonSwaggerConfiguration.class}) +@Import({CustomersCommandSideWebConfiguration.class, EventStoreHttpClientConfiguration.class, CommonSwaggerConfiguration.class}) @EnableAutoConfiguration @ComponentScan public class CustomersCommandSideServiceConfiguration { diff --git a/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomersCommandSideWebConfiguration.java similarity index 95% rename from java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java rename to java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomersCommandSideWebConfiguration.java index e86255c..054d7ff 100644 --- a/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CommandSideWebCustomersConfiguration.java +++ b/java-spring/customers-command-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/commandside/customers/CustomersCommandSideWebConfiguration.java @@ -19,7 +19,7 @@ import java.util.List; @Configuration @Import({CustomerConfiguration.class}) @ComponentScan -public class CommandSideWebCustomersConfiguration extends WebMvcConfigurerAdapter { +public class CustomersCommandSideWebConfiguration extends WebMvcConfigurerAdapter { class FakeThing {} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java index 1de7c77..f71b3ac 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerInfoUpdateService.java @@ -4,14 +4,9 @@ import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Cust import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.ToAccountInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.query.Query; -import org.springframework.data.mongodb.core.query.Update; import java.util.Collections; -import static org.springframework.data.mongodb.core.query.Criteria.where; - /** * Created by Main on 04.02.2016. */ @@ -20,11 +15,9 @@ public class CustomerInfoUpdateService { private Logger logger = LoggerFactory.getLogger(getClass()); private QuerySideCustomerRepository accountInfoRepository; - private MongoTemplate mongoTemplate; - public CustomerInfoUpdateService(QuerySideCustomerRepository accountInfoRepository, MongoTemplate mongoTemplate) { + public CustomerInfoUpdateService(QuerySideCustomerRepository accountInfoRepository) { this.accountInfoRepository = accountInfoRepository; - this.mongoTemplate = mongoTemplate; } public void create(String id, CustomerInfo customerInfo) { @@ -35,7 +28,7 @@ public class CustomerInfoUpdateService { customerInfo.getSsn(), customerInfo.getPhoneNumber(), customerInfo.getAddress(), - Collections.emptyList() + Collections.emptyMap() ) ); logger.info("Saved in mongo"); @@ -46,10 +39,9 @@ public class CustomerInfoUpdateService { } public void addToAccount(String id, ToAccountInfo accountInfo) { - mongoTemplate.updateMulti(new Query(where("id").is(id)), - new Update(). - push("toAccounts", accountInfo), - QuerySideCustomer.class); + QuerySideCustomer customer = accountInfoRepository.findOne(id); + customer.getToAccounts().put(accountInfo.getId(), accountInfo); + accountInfoRepository.save(customer); } } diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerNotFoundException.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerNotFoundException.java deleted file mode 100644 index f003262..0000000 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerNotFoundException.java +++ /dev/null @@ -1,8 +0,0 @@ -package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; - -public class CustomerNotFoundException extends RuntimeException { - - public CustomerNotFoundException(String customerId) { - super("Customer not found " + customerId); - } -} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryService.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryService.java index 2862184..e17b643 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryService.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryService.java @@ -1,6 +1,7 @@ package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; import net.chrisrichardson.eventstore.EntityIdentifier; +import org.springframework.dao.EmptyResultDataAccessException; import rx.Observable; import java.util.List; @@ -16,7 +17,7 @@ public class CustomerQueryService { public Observable findByCustomerId(EntityIdentifier customerId) { QuerySideCustomer customer = querySideCustomerRepository.findOne(customerId.getId()); if (customer == null) - return Observable.error(new CustomerNotFoundException(customerId.getId())); + return Observable.error(new EmptyResultDataAccessException(1)); else return Observable.just(customer); } @@ -24,7 +25,7 @@ public class CustomerQueryService { public Observable> findByEmail(String email){ List customers = querySideCustomerRepository.findByEmailLike(email); if (customers.isEmpty()) - return Observable.error(new CustomersNotFoundException()); + return Observable.error(new EmptyResultDataAccessException(1)); else return Observable.just(customers); } diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java index 800f19b..df37825 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomerQueryWorkflow.java @@ -14,7 +14,7 @@ import rx.Observable; /** * Created by Main on 04.02.2016. */ -@EventSubscriber(id = "querySideEventHandlers") +@EventSubscriber(id = "customerQuerySideEventHandlers") public class CustomerQueryWorkflow implements CompoundEventHandler { private Logger logger = LoggerFactory.getLogger(getClass()); diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomersNotFoundException.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomersNotFoundException.java deleted file mode 100644 index 765a93a..0000000 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/CustomersNotFoundException.java +++ /dev/null @@ -1,8 +0,0 @@ -package net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers; - -public class CustomersNotFoundException extends RuntimeException { - - public CustomersNotFoundException() { - super("Customers not found"); - } -} diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomer.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomer.java index 6dd8dc2..e5dde3c 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomer.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomer.java @@ -5,16 +5,16 @@ import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Cust import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.Name; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.ToAccountInfo; -import java.util.List; +import java.util.Map; /** * Created by Main on 05.02.2016. */ public class QuerySideCustomer extends CustomerInfo { private String id; - private List toAccounts; + private Map toAccounts; - public QuerySideCustomer(String id, Name name, String email, String ssn, String phoneNumber, Address address, List toAccounts) { + public QuerySideCustomer(String id, Name name, String email, String ssn, String phoneNumber, Address address, Map toAccounts) { super(name, email, ssn, phoneNumber, address); this.id = id; this.toAccounts = toAccounts; @@ -24,7 +24,7 @@ public class QuerySideCustomer extends CustomerInfo { return id; } - public List getToAccounts() { + public Map getToAccounts() { return toAccounts; } } diff --git a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java index 9afa06a..6b9fdf2 100644 --- a/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java +++ b/java-spring/customers-query-side-backend/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/backend/queryside/customers/QuerySideCustomerConfiguration.java @@ -19,8 +19,8 @@ public class QuerySideCustomerConfiguration { } @Bean - public CustomerInfoUpdateService customerInfoUpdateService(QuerySideCustomerRepository querySideCustomerRepository, MongoTemplate mongoTemplate) { - return new CustomerInfoUpdateService(querySideCustomerRepository, mongoTemplate); + public CustomerInfoUpdateService customerInfoUpdateService(QuerySideCustomerRepository querySideCustomerRepository) { + return new CustomerInfoUpdateService(querySideCustomerRepository); } @Bean diff --git a/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java b/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java index 7e3f1b6..06e2a09 100644 --- a/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java +++ b/java-spring/customers-query-side-web/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/queryside/customers/CustomerQueryController.java @@ -1,12 +1,11 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web.queryside.customers; import net.chrisrichardson.eventstore.EntityIdentifier; -import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomerNotFoundException; import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomerQueryService; -import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.CustomersNotFoundException; import net.chrisrichardson.eventstore.javaexamples.banking.backend.queryside.customers.QuerySideCustomer; import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.CustomerResponse; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import rx.Observable; @@ -39,14 +38,9 @@ public class CustomerQueryController { .map(this::getCustomersQueryResponse); } - @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "customer not found") - @ExceptionHandler(CustomerNotFoundException.class) - public void customerNotFound() { - - } @ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "customers not found") - @ExceptionHandler(CustomersNotFoundException.class) + @ExceptionHandler(EmptyResultDataAccessException.class) public void customersNotFound() { } diff --git a/java-spring/e2e-test/src/test/java/net/chrisrichardson/eventstore/examples/bank/web/EndToEndTest.java b/java-spring/e2e-test/src/test/java/net/chrisrichardson/eventstore/examples/bank/web/EndToEndTest.java index 5f99260..074cb85 100644 --- a/java-spring/e2e-test/src/test/java/net/chrisrichardson/eventstore/examples/bank/web/EndToEndTest.java +++ b/java-spring/e2e-test/src/test/java/net/chrisrichardson/eventstore/examples/bank/web/EndToEndTest.java @@ -63,10 +63,10 @@ public class EndToEndTest { BigDecimal finalFromAccountBalance = initialFromAccountBalance.subtract(amountToTransfer); BigDecimal finalToAccountBalance = initialToAccountBalance.add(amountToTransfer); - final CreateAccountResponse fromAccount = restTemplate.postForEntity(accountsCommandSideBaseUrl("/accounts"), new CreateAccountRequest("00000000-00000000", "My Account", initialFromAccountBalance), CreateAccountResponse.class).getBody(); + final CreateAccountResponse fromAccount = restTemplate.postForEntity(accountsCommandSideBaseUrl("/accounts"), new CreateAccountRequest("00000000-00000000", "My #1 Account", initialFromAccountBalance), CreateAccountResponse.class).getBody(); final String fromAccountId = fromAccount.getAccountId(); - CreateAccountResponse toAccount = restTemplate.postForEntity(accountsCommandSideBaseUrl("/accounts"), new CreateAccountRequest("00000000-00000000", "My Account", initialToAccountBalance), CreateAccountResponse.class).getBody(); + CreateAccountResponse toAccount = restTemplate.postForEntity(accountsCommandSideBaseUrl("/accounts"), new CreateAccountRequest("00000000-00000000", "My #2 Account", initialToAccountBalance), CreateAccountResponse.class).getBody(); String toAccountId = toAccount.getAccountId(); Assert.assertNotNull(fromAccountId); diff --git a/java-spring/monolithic-service/build.gradle b/java-spring/monolithic-service/build.gradle index 71d9f26..0129c38 100644 --- a/java-spring/monolithic-service/build.gradle +++ b/java-spring/monolithic-service/build.gradle @@ -6,6 +6,8 @@ dependencies { compile project(":accounts-query-side-web") compile project(":accounts-command-side-web") compile project(":transactions-command-side-web") + compile project(":customers-command-side-web") + compile project(":customers-query-side-web") compile "org.springframework.boot:spring-boot-starter-web" compile "org.springframework.boot:spring-boot-starter-actuator" diff --git a/java-spring/monolithic-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebConfiguration.java b/java-spring/monolithic-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebConfiguration.java index a3e2253..8693281 100644 --- a/java-spring/monolithic-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebConfiguration.java +++ b/java-spring/monolithic-service/src/main/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebConfiguration.java @@ -1,7 +1,9 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web; import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.accounts.CommandSideWebAccountsConfiguration; +import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.customers.CustomersCommandSideWebConfiguration; import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.transactions.CommandSideWebTransactionsConfiguration; +import net.chrisrichardson.eventstore.javaexamples.banking.web.queryside.CustomersQuerySideWebConfiguration; import net.chrisrichardson.eventstore.javaexamples.banking.web.queryside.QuerySideWebConfiguration; import net.chrisrichardson.eventstore.jdbc.config.JdbcEventStoreConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -14,7 +16,7 @@ import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; @Configuration -@Import({CommandSideWebAccountsConfiguration.class, CommandSideWebTransactionsConfiguration.class, JdbcEventStoreConfiguration.class, QuerySideWebConfiguration.class}) +@Import({CommandSideWebAccountsConfiguration.class, CommandSideWebTransactionsConfiguration.class, JdbcEventStoreConfiguration.class, QuerySideWebConfiguration.class, CustomersQuerySideWebConfiguration.class, CustomersCommandSideWebConfiguration.class}) @EnableAutoConfiguration @ComponentScan public class BankingWebConfiguration { diff --git a/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebIntegrationTest.java b/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebIntegrationTest.java index d6a8f89..6abfcec 100644 --- a/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebIntegrationTest.java +++ b/java-spring/monolithic-service/src/test/java/net/chrisrichardson/eventstore/javaexamples/banking/web/BankingWebIntegrationTest.java @@ -1,5 +1,6 @@ package net.chrisrichardson.eventstore.javaexamples.banking.web; +import net.chrisrichardson.eventstore.javaexamples.banking.common.customers.*; import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.transactions.CreateMoneyTransferRequest; import net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.transactions.CreateMoneyTransferResponse; import net.chrisrichardson.eventstore.javaexamples.banking.web.queryside.accounts.GetAccountResponse; @@ -71,6 +72,19 @@ public class BankingWebIntegrationTest { } + @Test + public void shouldCreateCustomers() { + CustomerInfo customerInfo = generateCustomerInfo(); + + final CustomerResponse customerResponse = restTemplate.postForEntity(baseUrl("/customers"), customerInfo, CustomerResponse.class).getBody(); + final String customerId = customerResponse.getId(); + + Assert.assertNotNull(customerId); + Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo()); + + assertCustomerResponse(customerId, customerInfo); + } + private BigDecimal toCents(BigDecimal dollarAmount) { return dollarAmount.multiply(new BigDecimal(100)); } @@ -93,4 +107,38 @@ public class BankingWebIntegrationTest { }); } + private void assertCustomerResponse(final String customerId, final CustomerInfo customerInfo) { + eventually( + new Producer() { + @Override + public Observable produce() { + return Observable.just(restTemplate.getForEntity(baseUrl("/customers/" + customerId), CustomerResponse.class).getBody()); + } + }, + new Verifier() { + @Override + public void verify(CustomerResponse customerResponse) { + Assert.assertEquals(customerId, customerResponse.getId()); + Assert.assertEquals(customerInfo, customerResponse.getCustomerInfo()); + } + }); + } + + private CustomerInfo generateCustomerInfo() { + return new CustomerInfo( + new Name("John", "Doe"), + "current@email.com", + "000-00-0000", + "1-111-111-1111", + new Address("street 1", + "street 2", + "City", + "State", + "1111111") + ); + } + + private ToAccountInfo generateToAccountInfo() { + return new ToAccountInfo("11111111-11111111", "New Account", "John Doe"); + } }