Search in sources :

Example 1 with ValidReasonAssignment

use of org.openlmis.stockmanagement.domain.reason.ValidReasonAssignment in project openlmis-stockmanagement by OpenLMIS.

the class ValidReasonAssignmentController method assignReason.

/**
 * Assign a reason to program and facility type.
 * If valid reason assignment ID is specified, ID will be ignored.
 *
 * @param assignmentDto valid reason assignment.
 * @return the assigned reason and program and facility type.
 */
@RequestMapping(value = "/validReasons", method = POST)
public ResponseEntity<ValidReasonAssignmentDto> assignReason(@RequestBody ValidReasonAssignmentDto assignmentDto) {
    permissionService.canManageReasons();
    assignmentDto.setId(null);
    ValidReasonAssignment assignment = ValidReasonAssignment.newInstance(assignmentDto);
    checkIsValidRequest(assignment);
    return findExistingOrSaveNew(assignment);
}
Also used : ValidReasonAssignment(org.openlmis.stockmanagement.domain.reason.ValidReasonAssignment) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ValidReasonAssignment

use of org.openlmis.stockmanagement.domain.reason.ValidReasonAssignment in project openlmis-stockmanagement by OpenLMIS.

the class ValidReasonAssignmentDtoBuilderTest method shouldBuildDtoWithProperServiceUrls.

@Test
public void shouldBuildDtoWithProperServiceUrls() {
    ValidReasonAssignment assignment = new ValidReasonAssignmentDataBuilder().build();
    ValidReasonAssignmentDto dto = dtoBuilder.build(assignment);
    assertValidReasonAssignmentDto(assignment, dto);
}
Also used : ValidReasonAssignmentDataBuilder(org.openlmis.stockmanagement.testutils.ValidReasonAssignmentDataBuilder) ValidReasonAssignment(org.openlmis.stockmanagement.domain.reason.ValidReasonAssignment) ValidReasonAssignmentDto(org.openlmis.stockmanagement.dto.ValidReasonAssignmentDto) Test(org.junit.Test)

Example 3 with ValidReasonAssignment

use of org.openlmis.stockmanagement.domain.reason.ValidReasonAssignment in project openlmis-stockmanagement by OpenLMIS.

the class ValidReasonAssignmentControllerIntegrationTest method shouldAssignReasonToProgramFacilityTypeAndSetAsShownByDefault.

@Test
public void shouldAssignReasonToProgramFacilityTypeAndSetAsShownByDefault() throws Exception {
    // given
    UUID reasonId = UUID.randomUUID();
    ValidReasonAssignmentDto assignment = mockedValidReasonAssignment(reasonId, ReasonType.DEBIT);
    // when
    ResultActions resultActions = mvc.perform(post(VALID_REASON_API).param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE).contentType(MediaType.APPLICATION_JSON).content(objectToJsonString(assignment)));
    // then
    ArgumentCaptor<ValidReasonAssignment> assignmentCaptor = forClass(ValidReasonAssignment.class);
    resultActions.andDo(print()).andExpect(status().isCreated());
    verify(reasonAssignmentRepository, times(1)).save(assignmentCaptor.capture());
    assertThat(assignmentCaptor.getValue().getReason().getId(), is(reasonId));
    assertThat(assignmentCaptor.getValue().getHidden(), is(false));
    verify(programFacilityTypeExistenceService, times(1)).checkProgramAndFacilityTypeExist(assignment.getProgramId(), assignment.getFacilityTypeId());
    verify(permissionService, times(1)).canManageReasons();
}
Also used : ValidReasonAssignmentDto(org.openlmis.stockmanagement.dto.ValidReasonAssignmentDto) ValidReasonAssignment(org.openlmis.stockmanagement.domain.reason.ValidReasonAssignment) ResultActions(org.springframework.test.web.servlet.ResultActions) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 4 with ValidReasonAssignment

use of org.openlmis.stockmanagement.domain.reason.ValidReasonAssignment in project openlmis-stockmanagement by OpenLMIS.

the class ValidReasonAssignmentControllerIntegrationTest method getValidReasonAssignmentByProgramAndFacilityType.

@Test
public void getValidReasonAssignmentByProgramAndFacilityType() throws Exception {
    // given
    UUID programId = UUID.randomUUID();
    UUID facilityTypeId = UUID.randomUUID();
    when(validReasonAssignmentService.search(programId, facilityTypeId, null)).thenReturn(Collections.singletonList(new ValidReasonAssignment()));
    // when
    ResultActions resultActions = mvc.perform(get(VALID_REASON_API).param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE).param(PROGRAM, programId.toString()).param(FACILITY_TYPE, facilityTypeId.toString()));
    // then
    verifyZeroInteractions(permissionService);
    resultActions.andExpect(status().isOk()).andExpect(jsonPath("$", hasSize(1)));
}
Also used : ValidReasonAssignment(org.openlmis.stockmanagement.domain.reason.ValidReasonAssignment) ResultActions(org.springframework.test.web.servlet.ResultActions) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 5 with ValidReasonAssignment

use of org.openlmis.stockmanagement.domain.reason.ValidReasonAssignment in project openlmis-stockmanagement by OpenLMIS.

the class ValidReasonAssignmentControllerIntegrationTest method shouldReturn400IfReasonNotExist.

@Test
public void shouldReturn400IfReasonNotExist() throws Exception {
    // given
    UUID reasonId = UUID.randomUUID();
    StockCardLineItemReason reason = new StockCardLineItemReason();
    reason.setId(reasonId);
    when(reasonRepository.findOne(reasonId)).thenReturn(null);
    ValidReasonAssignment assignment = new ValidReasonAssignment();
    assignment.setReason(reason);
    // when
    ResultActions resultActions = mvc.perform(post(VALID_REASON_API).param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE).contentType(MediaType.APPLICATION_JSON).content(objectToJsonString(assignment)));
    // then
    resultActions.andExpect(status().isBadRequest());
    verify(reasonAssignmentRepository, never()).save(any(ValidReasonAssignment.class));
}
Also used : StockCardLineItemReason(org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason) ValidReasonAssignment(org.openlmis.stockmanagement.domain.reason.ValidReasonAssignment) ResultActions(org.springframework.test.web.servlet.ResultActions) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Aggregations

ValidReasonAssignment (org.openlmis.stockmanagement.domain.reason.ValidReasonAssignment)17 Test (org.junit.Test)12 ValidReasonAssignmentDto (org.openlmis.stockmanagement.dto.ValidReasonAssignmentDto)8 ResultActions (org.springframework.test.web.servlet.ResultActions)7 UUID (java.util.UUID)6 UUID.randomUUID (java.util.UUID.randomUUID)4 StockCardLineItemReason (org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason)4 StockCardLineItemReasonDataBuilder (org.openlmis.stockmanagement.testutils.StockCardLineItemReasonDataBuilder)2 ValidReasonAssignmentDataBuilder (org.openlmis.stockmanagement.testutils.ValidReasonAssignmentDataBuilder)2 Before (org.junit.Before)1 ResponseEntity (org.springframework.http.ResponseEntity)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1