Cleanup Sonar issues, added Github build and formatted source
This commit is contained in:
@@ -8,24 +8,25 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.context.annotation.RequestScope;
|
||||
|
||||
@SpringBootApplication(scanBasePackages = { "org.fuin.cqrs4j.example.spring.command.app",
|
||||
"org.fuin.cqrs4j.example.spring.command.controller", "org.fuin.cqrs4j.example.spring.shared" })
|
||||
"org.fuin.cqrs4j.example.spring.command.controller", "org.fuin.cqrs4j.example.spring.shared" })
|
||||
public class CmdApplication {
|
||||
|
||||
/**
|
||||
* Creates an event sourced repository that can store a person.
|
||||
*
|
||||
* @param eventStore Event store to use.
|
||||
*
|
||||
* @return Repository only valid for the current request.
|
||||
*/
|
||||
@Bean
|
||||
@RequestScope
|
||||
public PersonRepository create(final IESJCEventStore eventStore) {
|
||||
return new PersonRepository(eventStore);
|
||||
}
|
||||
/**
|
||||
* Creates an event sourced repository that can store a person.
|
||||
*
|
||||
* @param eventStore
|
||||
* Event store to use.
|
||||
*
|
||||
* @return Repository only valid for the current request.
|
||||
*/
|
||||
@Bean
|
||||
@RequestScope
|
||||
public PersonRepository create(final IESJCEventStore eventStore) {
|
||||
return new PersonRepository(eventStore);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CmdApplication.class, args);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CmdApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,33 +40,32 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@RequestMapping("/persons")
|
||||
public class PersonController {
|
||||
|
||||
@Autowired
|
||||
private PersonRepository repo;
|
||||
@Autowired
|
||||
private PersonRepository repo;
|
||||
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
|
||||
@PostMapping(path = "/create", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public ResponseEntity<SimpleResult> create(@RequestBody final CreatePersonCommand cmd)
|
||||
throws AggregateAlreadyExistsException, AggregateDeletedException, CommandExecutionFailedException,
|
||||
DuplicatePersonNameException {
|
||||
@PostMapping(path = "/create", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
||||
public ResponseEntity<SimpleResult> create(@RequestBody final CreatePersonCommand cmd) throws AggregateAlreadyExistsException,
|
||||
AggregateDeletedException, CommandExecutionFailedException, DuplicatePersonNameException {
|
||||
|
||||
// Verify preconditions
|
||||
final Set<ConstraintViolation<CreatePersonCommand>> violations = validator.validate(cmd);
|
||||
if (!violations.isEmpty()) {
|
||||
throw new ConstraintViolationException(violations);
|
||||
}
|
||||
// Verify preconditions
|
||||
final Set<ConstraintViolation<CreatePersonCommand>> violations = validator.validate(cmd);
|
||||
if (!violations.isEmpty()) {
|
||||
throw new ConstraintViolationException(violations);
|
||||
}
|
||||
|
||||
// Create aggregate
|
||||
final Person person = new Person(cmd.getAggregateRootId(), cmd.getName(), (name) -> {
|
||||
// TODO Execute a call to the query side to verify if the name already exists
|
||||
return Optional.empty();
|
||||
});
|
||||
repo.add(person);
|
||||
// Create aggregate
|
||||
final Person person = new Person(cmd.getAggregateRootId(), cmd.getName(), name -> {
|
||||
// TODO Execute a call to the query side to verify if the name already exists
|
||||
return Optional.empty();
|
||||
});
|
||||
repo.add(person);
|
||||
|
||||
// Send OK response
|
||||
return new ResponseEntity<>(SimpleResult.ok(), HttpStatus.OK);
|
||||
// Send OK response
|
||||
return new ResponseEntity<>(SimpleResult.ok(), HttpStatus.OK);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -39,49 +39,40 @@ import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@ContextConfiguration(classes = CmdApplication.class)
|
||||
public class PersonControllerIT {
|
||||
class PersonControllerIT {
|
||||
|
||||
@LocalServerPort
|
||||
@LocalServerPort
|
||||
int port;
|
||||
|
||||
@Autowired
|
||||
WebApplicationContext wac;
|
||||
|
||||
@Autowired
|
||||
IESJCEventStore eventStore;
|
||||
|
||||
@Autowired
|
||||
@Autowired
|
||||
WebApplicationContext wac;
|
||||
|
||||
@Autowired
|
||||
IESJCEventStore eventStore;
|
||||
|
||||
@Autowired
|
||||
Jsonb jsonb;
|
||||
|
||||
@BeforeEach
|
||||
public void initRestAssuredMockMvcStandalone() {
|
||||
RestAssured.port = port;
|
||||
RestAssuredMockMvc.webAppContextSetup(wac);
|
||||
}
|
||||
|
||||
|
||||
@BeforeEach
|
||||
public void initRestAssuredMockMvcStandalone() {
|
||||
RestAssured.port = port;
|
||||
RestAssuredMockMvc.webAppContextSetup(wac);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreate() {
|
||||
|
||||
void testCreate() {
|
||||
|
||||
// PREPARE
|
||||
final PersonId personId = new PersonId(UUID.randomUUID());
|
||||
final PersonName personName = new PersonName("Peter Parker");
|
||||
final CreatePersonCommand cmd = new CreatePersonCommand(personId, personName);
|
||||
final String json = jsonb.toJson(cmd);
|
||||
|
||||
|
||||
// TEST & VERIFY
|
||||
final SimpleResult result =
|
||||
given()
|
||||
.accept(ContentType.JSON)
|
||||
.contentType(ContentType.JSON)
|
||||
.body(json)
|
||||
.when()
|
||||
.post("/persons/create")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract()
|
||||
.as(SimpleResult.class);
|
||||
final SimpleResult result = given().accept(ContentType.JSON).contentType(ContentType.JSON).body(json).when().post("/persons/create")
|
||||
.then().statusCode(200).extract().as(SimpleResult.class);
|
||||
assertThat(result.getType(), is(equalTo(ResultType.OK)));
|
||||
|
||||
|
||||
final SimpleStreamId personStreamId = new SimpleStreamId(PersonId.TYPE + "-" + personId);
|
||||
final StreamEventsSlice slice = eventStore.readEventsForward(personStreamId, 0, 1);
|
||||
final List<CommonEvent> events = slice.getEvents();
|
||||
@@ -91,7 +82,7 @@ public class PersonControllerIT {
|
||||
final PersonCreatedEvent event = (PersonCreatedEvent) ce.getData();
|
||||
assertThat(event.getEntityId(), is(equalTo(personId)));
|
||||
assertThat(event.getName(), is(equalTo(personName)));
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user