Skip to content
Permalink
Browse files
Add check if duplicate identifier is used when creating a branch office
  • Loading branch information
crain committed Apr 12, 2017
1 parent de1b1ac commit 346a8a1d68bf63aa42655daff56524e8a11da52e
Showing 3 changed files with 28 additions and 1 deletion.
@@ -90,7 +90,10 @@ OfficePage fetchOffices(@RequestParam(value = "term", required = false) final St
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
@ThrowsException(status = HttpStatus.NOT_FOUND, exception = NotFoundException.class)
@ThrowsExceptions({
@ThrowsException(status = HttpStatus.NOT_FOUND, exception = NotFoundException.class),
@ThrowsException(status = HttpStatus.CONFLICT, exception = AlreadyExistsException.class)
})
void addBranch(@PathVariable("identifier") final String identifier, @RequestBody final Office office);

@RequestMapping(
@@ -201,6 +201,22 @@ public void shouldNotAddBranchParentNotFound() throws Exception {
}
}

@Test
public void shouldNotAddBranchDuplicate() throws Exception {
final Office office = OfficeFactory.createRandomOffice();
this.organizationManager.createOffice(office);
this.eventRecorder.wait(EventConstants.OPERATION_POST_OFFICE, office.getIdentifier());
try {
this.organizationManager.addBranch(office.getIdentifier(), office);
Assert.fail();
} catch (final AlreadyExistsException ex) {
// do nothing, expected
}

this.organizationManager.deleteOffice(office.getIdentifier());
this.eventRecorder.wait(EventConstants.OPERATION_DELETE_OFFICE, office.getIdentifier());
}

@Test
public void shouldSetAddressOfOffice() throws Exception {
final Office office = OfficeFactory.createRandomOffice();
@@ -173,6 +173,14 @@ ResponseEntity<Void> addBranch(@PathVariable("identifier") final String identifi
throw ServiceException.notFound("Parent office {0} not found.", identifier);
}

if (office == null) {
throw ServiceException.badRequest("An office must be given.");
}

if (this.officeService.officeExists(office.getIdentifier())) {
throw ServiceException.conflict("Office {0} already exists.", office.getIdentifier());
}

this.commandGateway.process(new AddBranchCommand(identifier, office));
return ResponseEntity.accepted().build();
}

0 comments on commit 346a8a1

Please sign in to comment.