Validated POST requests for creating accounts and transfers
This commit is contained in:
@@ -4,6 +4,9 @@ dependencies {
|
||||
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"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.transactions;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.DecimalMin;
|
||||
|
||||
public class CreateMoneyTransferRequest {
|
||||
|
||||
@NotNull
|
||||
private String fromAccountId;
|
||||
|
||||
@NotNull
|
||||
private String toAccountId;
|
||||
|
||||
@DecimalMin("0.01")
|
||||
private BigDecimal amount;
|
||||
|
||||
public CreateMoneyTransferRequest() {
|
||||
|
||||
@@ -1,20 +1,15 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.transactions;
|
||||
|
||||
import net.chrisrichardson.eventstore.EntityIdentifier;
|
||||
import net.chrisrichardson.eventstore.EntityWithIdAndVersion;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.transactions.MoneyTransfer;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.backend.commandside.transactions.MoneyTransferService;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.backend.common.transactions.TransferDetails;
|
||||
import net.chrisrichardson.eventstore.javaexamples.banking.web.util.DeferredUtils;
|
||||
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 org.springframework.web.context.request.async.DeferredResult;
|
||||
|
||||
import rx.Observable;
|
||||
import rx.functions.Func1;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/transfers")
|
||||
@@ -28,16 +23,13 @@ public class MoneyTransferController {
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST)
|
||||
public Observable<CreateMoneyTransferResponse> createMoneyTransfer(@RequestBody CreateMoneyTransferRequest request) {
|
||||
TransferDetails transferDetails = new TransferDetails(new EntityIdentifier(request.getFromAccountId()), new EntityIdentifier(request.getToAccountId()), request.getAmount());
|
||||
return moneyTransferService.transferMoney(transferDetails).map(new Func1<EntityWithIdAndVersion<MoneyTransfer>, CreateMoneyTransferResponse>() {
|
||||
|
||||
@Override
|
||||
public CreateMoneyTransferResponse call(EntityWithIdAndVersion<MoneyTransfer> entityAndEventInfo) {
|
||||
return new CreateMoneyTransferResponse(entityAndEventInfo.getEntityIdentifier().getId());
|
||||
}
|
||||
|
||||
});
|
||||
public Observable<CreateMoneyTransferResponse> createMoneyTransfer(@Validated @RequestBody CreateMoneyTransferRequest request) {
|
||||
TransferDetails transferDetails = new TransferDetails(
|
||||
new EntityIdentifier(request.getFromAccountId()),
|
||||
new EntityIdentifier(request.getToAccountId()),
|
||||
request.getAmount());
|
||||
return moneyTransferService.transferMoney(transferDetails)
|
||||
.map(entityAndEventInfo -> new CreateMoneyTransferResponse(entityAndEventInfo.getEntityIdentifier().getId()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.transactions;
|
||||
|
||||
|
||||
import org.junit.Before;
|
||||
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.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = MoneyTransferControllerIntegrationTestConfiguration.class)
|
||||
@IntegrationTest
|
||||
@WebAppConfiguration
|
||||
public class MoneyTransferControllerIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateAccount() throws Exception {
|
||||
mockMvc.perform(post("/transfers")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"fromAccountId\" : \"fromAccountId\", \"toAccountId\" : \"toAccountId\", \"amount\" : \"500\"}")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRejectBadRequest() throws Exception {
|
||||
mockMvc.perform(post("/transfers")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content("{\"fromAccountId\" : \"fromAccountIdXXXXXX\"}, {\"toAccountId\" : \"toAccountId\"}, {\"amount\" : \"500\"}")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().is(HttpStatus.BAD_REQUEST.value()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package net.chrisrichardson.eventstore.javaexamples.banking.web.commandside.transactions;
|
||||
|
||||
import net.chrisrichardson.eventstore.jdbc.config.JdbcEventStoreConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Configuration
|
||||
@Import({CommandSideWebTransactionsConfiguration.class, JdbcEventStoreConfiguration.class})
|
||||
public class MoneyTransferControllerIntegrationTestConfiguration {
|
||||
}
|
||||
Reference in New Issue
Block a user