Skip to content
Permalink
Browse files
Merge pull request #2 from crain/develop
Bugfix/Improvements
  • Loading branch information
crain committed Apr 12, 2017
2 parents a952ee3 + a17e82e commit f3f662fca5f6bc5b5b863bbf272233700c8c64d7
Showing 6 changed files with 95 additions and 18 deletions.
@@ -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(
@@ -24,15 +24,14 @@
import io.mifos.core.test.listener.EnableEventRecording;
import io.mifos.core.test.listener.EventRecorder;
import io.mifos.office.api.v1.EventConstants;
import io.mifos.office.api.v1.client.AlreadyExistsException;
import io.mifos.office.api.v1.client.BadRequestException;
import io.mifos.office.api.v1.client.OrganizationManager;
import io.mifos.office.api.v1.client.NotFoundException;
import io.mifos.office.api.v1.client.*;
import io.mifos.office.api.v1.domain.Address;
import io.mifos.office.api.v1.domain.Employee;
import io.mifos.office.api.v1.domain.Office;
import io.mifos.office.api.v1.domain.OfficePage;
import io.mifos.office.rest.config.OfficeRestConfiguration;
import io.mifos.office.util.AddressFactory;
import io.mifos.office.util.EmployeeFactory;
import io.mifos.office.util.OfficeFactory;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.*;
@@ -202,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();
@@ -281,6 +296,46 @@ public void shouldReturnParentOfBranch() throws Exception {
Assert.assertEquals(parent.getIdentifier(), savedBranch.getParentIdentifier());
}

@Test(expected = NotFoundException.class)
public void shouldDeleteOffice() throws Exception{
final Office office = OfficeFactory.createRandomOffice();
this.organizationManager.createOffice(office);
this.eventRecorder.wait(EventConstants.OPERATION_POST_OFFICE, office.getIdentifier());

this.organizationManager.deleteOffice(office.getIdentifier());

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

this.organizationManager.findOfficeByIdentifier(office.getIdentifier());
}

@Test(expected = ChildrenExistException.class)
public void shouldNotDeleteOfficeWithBranches() throws Exception{
final Office parent = OfficeFactory.createRandomOffice();
this.organizationManager.createOffice(parent);
this.eventRecorder.wait(EventConstants.OPERATION_POST_OFFICE, parent.getIdentifier());

final Office branch = OfficeFactory.createRandomOffice();
this.organizationManager.addBranch(parent.getIdentifier(), branch);
this.eventRecorder.wait(EventConstants.OPERATION_POST_OFFICE, branch.getIdentifier());

this.organizationManager.deleteOffice(parent.getIdentifier());
}

@Test(expected = ChildrenExistException.class)
public void shouldNotDeleteOfficeWithEmployees() throws Exception{
final Office office = OfficeFactory.createRandomOffice();
this.organizationManager.createOffice(office);
this.eventRecorder.wait(EventConstants.OPERATION_POST_OFFICE, office.getIdentifier());

final Employee employee = EmployeeFactory.createRandomEmployee();
employee.setAssignedOffice(office.getIdentifier());
this.organizationManager.createEmployee(employee);
this.eventRecorder.wait(EventConstants.OPERATION_POST_EMPLOYEE, employee.getIdentifier());

this.organizationManager.deleteOffice(office.getIdentifier());
}

@Configuration
@ComponentScan(
basePackages = "io.mifos.office.listener"
@@ -33,4 +33,7 @@ public interface EmployeeRepository extends JpaRepository<EmployeeEntity, Long>
Page<EmployeeEntity> findByAssignedOffice(final OfficeEntity assignedOffice, final Pageable pageable);

Page<EmployeeEntity> findByIdentifierContaining(String term, Pageable pageRequest);

@Query("SELECT CASE WHEN COUNT(e) > 0 THEN 'true' ELSE 'false' END FROM EmployeeEntity e WHERE e.assignedOffice = :office")
Boolean existsByAssignedOffice(@Param("office") final OfficeEntity assignedOffice);
}
@@ -22,10 +22,7 @@
import io.mifos.office.api.v1.domain.OfficePage;
import io.mifos.office.internal.mapper.AddressMapper;
import io.mifos.office.internal.mapper.OfficeMapper;
import io.mifos.office.internal.repository.AddressEntity;
import io.mifos.office.internal.repository.AddressRepository;
import io.mifos.office.internal.repository.OfficeEntity;
import io.mifos.office.internal.repository.OfficeRepository;
import io.mifos.office.internal.repository.*;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -44,15 +41,18 @@ public class OfficeService {
private final Logger logger;
private final OfficeRepository officeRepository;
private final AddressRepository addressRepository;
private final EmployeeRepository employeeRepository;

@Autowired
public OfficeService(@Qualifier(ServiceConstants.SERVICE_LOGGER_NAME) final Logger logger,
final OfficeRepository officeRepository,
final AddressRepository addressRepository) {
final AddressRepository addressRepository,
final EmployeeRepository employeeRepository) {
super();
this.logger = logger;
this.officeRepository = officeRepository;
this.addressRepository = addressRepository;
this.employeeRepository = employeeRepository;
}

public boolean officeExists(final String identifier) {
@@ -61,11 +61,13 @@ public boolean officeExists(final String identifier) {

public boolean branchExists(final String identifier) {
final Optional<OfficeEntity> officeEntityOptional = this.officeRepository.findByIdentifier(identifier);
if (officeEntityOptional.isPresent()) {
return this.officeRepository.existsByParentOfficeId(officeEntityOptional.get().getId());
} else {
throw ServiceException.notFound("Office {0} not found.", identifier);
}
return officeEntityOptional.map(officeEntity -> this.officeRepository.existsByParentOfficeId(officeEntity.getId())).orElse(false);
}

public boolean hasEmployees(final String officeIdentifier){
return this.officeRepository.findByIdentifier(officeIdentifier)
.map(this.employeeRepository::existsByAssignedOffice)
.orElse(false);
}

@Transactional(readOnly = true)
@@ -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();
}
@@ -208,14 +216,20 @@ ResponseEntity<OfficePage> getBranches(@PathVariable("identifier") final String
@ResponseBody
ResponseEntity<Void> deleteOffice(@PathVariable("identifier") final String identifier)
throws InterruptedException {
if (!this.officeService.officeExists(identifier)) {
throw ServiceException.notFound("Office {0} not found.", identifier);
}

if (this.officeService.branchExists(identifier)) {
throw ServiceException.conflict("Office {0} has children.", identifier);
}

if (this.officeService.officeExists(identifier)) {
this.commandGateway.process(new DeleteOfficeCommand(identifier));
if(this.officeService.hasEmployees(identifier)){
throw ServiceException.conflict("Office {0} has employees.", identifier);
}

this.commandGateway.process(new DeleteOfficeCommand(identifier));

return ResponseEntity.accepted().build();
}

@@ -39,7 +39,7 @@ CREATE TABLE horus_addresses (
street VARCHAR(256) NOT NULL,
city VARCHAR(256) NOT NULL,
region VARCHAR(256) NULL,
postal_code VARCHAR(32) NOT NULL,
postal_code VARCHAR(32) NULL,
country_code VARCHAR(2) NOT NULL,
country VARCHAR(256) NOT NULL,
CONSTRAINT address_pk

0 comments on commit f3f662f

Please sign in to comment.