Search in sources :

Example 6 with ValidSourceDestinationDto

use of org.openlmis.stockmanagement.dto.ValidSourceDestinationDto in project openlmis-stockmanagement by OpenLMIS.

the class SourceDestinationBaseServiceTest method shouldReturnListOfDestinationDtosWhenFindValidDestinationAssignment.

@Test
public void shouldReturnListOfDestinationDtosWhenFindValidDestinationAssignment() throws Exception {
    // given
    UUID programId = randomUUID();
    UUID facilityTypeId = randomUUID();
    doNothing().when(programFacilityTypeExistenceService).checkProgramAndFacilityTypeExist(programId, facilityTypeId);
    List<ValidDestinationAssignment> validDestinationAssignments = asList(createOrganizationDestination(mockedOrganizationNode("CHW")), createFacilityDestination(mockedFacilityNode("Balaka District Hospital")));
    when(destinationRepository.findByProgramIdAndFacilityTypeId(programId, facilityTypeId)).thenReturn(validDestinationAssignments);
    // when
    List<ValidSourceDestinationDto> validDestinations = validDestinationService.findDestinations(programId, facilityTypeId);
    // then
    assertThat(validDestinations.size(), is(2));
    ValidSourceDestinationDto organization = validDestinations.get(0);
    assertThat(organization.getName(), is("CHW"));
    assertThat(organization.getIsFreeTextAllowed(), is(true));
    ValidSourceDestinationDto facility = validDestinations.get(1);
    assertThat(facility.getName(), is("Balaka District Hospital"));
    assertThat(facility.getIsFreeTextAllowed(), is(false));
}
Also used : ValidDestinationAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment) ValidSourceDestinationDto(org.openlmis.stockmanagement.dto.ValidSourceDestinationDto) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 7 with ValidSourceDestinationDto

use of org.openlmis.stockmanagement.dto.ValidSourceDestinationDto in project openlmis-stockmanagement by OpenLMIS.

the class SourceDestinationBaseServiceTest method shouldReturnDestinationAssignmentWhenDestinationIsA_facilityWithoutNode.

@Test
public void shouldReturnDestinationAssignmentWhenDestinationIsA_facilityWithoutNode() throws Exception {
    // given
    UUID programId = randomUUID();
    UUID facilityTypeId = randomUUID();
    UUID destinationId = randomUUID();
    when(destinationRepository.save(any(ValidDestinationAssignment.class))).thenReturn(createDestinationAssignment(programId, facilityTypeId, createNode(destinationId, true)));
    FacilityDto facilityDto = new FacilityDto();
    facilityDto.setName(FACILITY_NAME);
    when(facilityReferenceDataService.exists(destinationId)).thenReturn(true);
    when(facilityReferenceDataService.findOne(destinationId)).thenReturn(facilityDto);
    when(nodeRepository.findByReferenceId(destinationId)).thenReturn(null);
    ValidDestinationAssignment assignment = createDestination(programId, facilityTypeId, destinationId);
    // when
    ValidSourceDestinationDto assignmentDto = validDestinationService.assignDestination(assignment);
    // then
    assertThat(assignmentDto.getProgramId(), is(programId));
    assertThat(assignmentDto.getFacilityTypeId(), is(facilityTypeId));
    assertThat(assignmentDto.getIsFreeTextAllowed(), is(false));
    assertThat(assignmentDto.getName(), is(FACILITY_NAME));
    assertThat(assignmentDto.getNode().getReferenceId(), is(destinationId));
    assertThat(assignmentDto.getNode().isRefDataFacility(), is(true));
}
Also used : ValidDestinationAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment) ValidSourceDestinationDto(org.openlmis.stockmanagement.dto.ValidSourceDestinationDto) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 8 with ValidSourceDestinationDto

use of org.openlmis.stockmanagement.dto.ValidSourceDestinationDto in project openlmis-stockmanagement by OpenLMIS.

the class ValidSourceDestinationController method assignDestination.

/**
 * Assign a destination to a program and facility type.
 * If valid destination assignment ID is specified, ID will be ignored.
 *
 * @return the assigned destination and program and facility type.
 */
@RequestMapping(value = "/validDestinations", method = POST)
public ResponseEntity<ValidSourceDestinationDto> assignDestination(@RequestBody ValidDestinationAssignment assignment) {
    LOGGER.debug("Try to assign destinations");
    permissionService.canManageStockDestinations();
    ValidSourceDestinationDto destinationDto = validDestinationService.findByProgramFacilityDestination(assignment);
    if (destinationDto != null) {
        return new ResponseEntity<>(destinationDto, OK);
    }
    return new ResponseEntity<>(validDestinationService.assignDestination(assignment), CREATED);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ValidSourceDestinationDto(org.openlmis.stockmanagement.dto.ValidSourceDestinationDto) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with ValidSourceDestinationDto

use of org.openlmis.stockmanagement.dto.ValidSourceDestinationDto in project openlmis-stockmanagement by OpenLMIS.

the class ValidSourceDestinationController method assignSource.

/**
 * Assign a source to program and facility type.
 * If valid source assignment ID is specified, ID will be ignored.
 *
 * @return the assigned source and program and facility type.
 */
@RequestMapping(value = "/validSources", method = POST)
public ResponseEntity<ValidSourceDestinationDto> assignSource(@RequestBody ValidSourceAssignment assignment) {
    LOGGER.debug("Try to assign source");
    permissionService.canManageStockSources();
    ValidSourceDestinationDto foundAssignmentDto = validSourceService.findByProgramFacilitySource(assignment);
    if (foundAssignmentDto != null) {
        return new ResponseEntity<>(foundAssignmentDto, OK);
    }
    return new ResponseEntity<>(validSourceService.assignSource(assignment), CREATED);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ValidSourceDestinationDto(org.openlmis.stockmanagement.dto.ValidSourceDestinationDto) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with ValidSourceDestinationDto

use of org.openlmis.stockmanagement.dto.ValidSourceDestinationDto in project openlmis-stockmanagement by OpenLMIS.

the class ValidSourceDestinationControllerIntegrationTest method shouldReturn200WhenDestinationAssignmentAlreadyExist.

@Test
public void shouldReturn200WhenDestinationAssignmentAlreadyExist() throws Exception {
    // given
    UUID programId = UUID.randomUUID();
    UUID facilityTypeId = UUID.randomUUID();
    UUID destinationId = UUID.randomUUID();
    ValidDestinationAssignment assignment = createDestination(programId, facilityTypeId, destinationId);
    ValidSourceDestinationDto validSourceDestinationDto = new ValidSourceDestinationDto();
    validSourceDestinationDto.setProgramId(programId);
    validSourceDestinationDto.setFacilityTypeId(facilityTypeId);
    validSourceDestinationDto.setNode(assignment.getNode());
    when(validDestinationService.findByProgramFacilityDestination(assignment)).thenReturn(validSourceDestinationDto);
    // when
    ResultActions resultActions = mvc.perform(post(API_VALID_DESTINATIONS).param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE).contentType(MediaType.APPLICATION_JSON).content(objectToJsonString(assignment)));
    // then
    resultActions.andExpect(status().isOk()).andExpect(jsonPath(PROGRAM_EXP, is(programId.toString()))).andExpect(jsonPath(FACILITY_TYPE_EXP, is(facilityTypeId.toString()))).andExpect(jsonPath(NODE_REFERENCE_ID_EXP, is(destinationId.toString())));
}
Also used : ValidDestinationAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment) ValidSourceDestinationDto(org.openlmis.stockmanagement.dto.ValidSourceDestinationDto) ResultActions(org.springframework.test.web.servlet.ResultActions) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Aggregations

ValidSourceDestinationDto (org.openlmis.stockmanagement.dto.ValidSourceDestinationDto)15 UUID (java.util.UUID)13 UUID.randomUUID (java.util.UUID.randomUUID)13 Test (org.junit.Test)13 ValidDestinationAssignment (org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment)6 ValidSourceAssignment (org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment)6 FacilityDto (org.openlmis.stockmanagement.dto.referencedata.FacilityDto)4 ResultActions (org.springframework.test.web.servlet.ResultActions)4 Node (org.openlmis.stockmanagement.domain.sourcedestination.Node)2 Organization (org.openlmis.stockmanagement.domain.sourcedestination.Organization)2 ResponseEntity (org.springframework.http.ResponseEntity)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2