Search in sources :

Example 1 with TasksRequest

use of org.onap.so.apihandlerinfra.tasksbeans.TasksRequest in project so by onap.

the class ManualTasks method completeTask.

@POST
@Path("/{version:[vV]1}/{taskId}/complete")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Operation(description = "Complete specified task", responses = @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
@Transactional
public Response completeTask(String request, @PathParam("version") String version, @PathParam("taskId") String taskId, @Context ContainerRequestContext requestContext) throws ApiException {
    String requestId = requestContext.getProperty("requestId").toString();
    logger.info(LoggingAnchor.TWO, MessageEnum.APIH_GENERATED_REQUEST_ID.toString(), requestId);
    logger.debug("requestId is: {}", requestId);
    TasksRequest taskRequest = null;
    String apiVersion = version.substring(1);
    try {
        ObjectMapper mapper = new ObjectMapper();
        taskRequest = mapper.readValue(request, TasksRequest.class);
        if (taskRequest.getRequestDetails() == null) {
            throw new ValidationException("requestDetails");
        }
        if (taskRequest.getRequestDetails().getRequestInfo() == null) {
            throw new ValidationException("requestInfo");
        }
        if (empty(taskRequest.getRequestDetails().getRequestInfo().getSource())) {
            throw new ValidationException("source");
        }
        if (empty(taskRequest.getRequestDetails().getRequestInfo().getRequestorId())) {
            throw new ValidationException("requestorId");
        }
    } catch (IOException e) {
        ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError).build();
        ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON object failed: " + e.getMessage(), HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
        throw validateException;
    } catch (ValidationException e) {
        ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError).errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
        ValidateException validateException = new ValidateException.Builder("Mapping of request to JSON Object failed. " + e.getMessage(), HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo).build();
        throw validateException;
    }
    // Transform the request to Camunda-style Complete request
    Variables variablesForComplete = new Variables();
    Value sourceValue = new Value();
    sourceValue.setValue(taskRequest.getRequestDetails().getRequestInfo().getSource());
    Value responseValue = new Value();
    responseValue.setValue(taskRequest.getRequestDetails().getRequestInfo().getResponseValue().name());
    Value requestorIdValue = new Value();
    requestorIdValue.setValue(taskRequest.getRequestDetails().getRequestInfo().getRequestorId());
    variablesForComplete.setSource(sourceValue);
    variablesForComplete.setResponseValue(responseValue);
    variablesForComplete.setRequestorId(requestorIdValue);
    String camundaJsonReq;
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
        camundaJsonReq = mapper.writeValueAsString(variablesForComplete);
    } catch (JsonProcessingException e) {
        logger.error("Mapping of JSON object to Camunda request failed");
        ValidateException validateException = new ValidateException.Builder("Mapping of JSON object to Camunda request failed", HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_GENERAL_SERVICE_ERROR).build();
        throw validateException;
    }
    String requestUrl = taskUri + "/" + taskId + "/complete";
    ResponseEntity<String> response = camundaClient.post(camundaJsonReq, requestUrl);
    int bpelStatus = responseHandler.setStatus(response.getStatusCodeValue());
    responseHandler.acceptedOrNoContentResponse(response);
    logger.debug("Received good response from Camunda");
    TaskRequestReference trr = new TaskRequestReference();
    trr.setTaskId(taskId);
    String completeResp = null;
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
        completeResp = mapper.writeValueAsString(trr);
    } catch (JsonProcessingException e) {
        logger.error("Unable to map response from Camunda");
        ValidateException validateException = new ValidateException.Builder("Request Failed due to bad response format", bpelStatus, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).build();
        throw validateException;
    }
    logger.debug("Response to the caller: {}", completeResp);
    logger.debug("End of the transaction, the final response is: {}", completeResp);
    return builder.buildResponse(HttpStatus.SC_ACCEPTED, requestId, completeResp, apiVersion);
}
Also used : ValidateException(org.onap.so.apihandlerinfra.exceptions.ValidateException) ValidationException(org.onap.so.exceptions.ValidationException) ResponseBuilder(org.onap.so.apihandler.common.ResponseBuilder) IOException(java.io.IOException) TaskRequestReference(org.onap.so.apihandlerinfra.tasksbeans.TaskRequestReference) ErrorLoggerInfo(org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo) Variables(org.onap.so.apihandlerinfra.tasksbeans.Variables) Value(org.onap.so.apihandlerinfra.tasksbeans.Value) TasksRequest(org.onap.so.apihandlerinfra.tasksbeans.TasksRequest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(io.swagger.v3.oas.annotations.Operation) Transactional(javax.transaction.Transactional)

Example 2 with TasksRequest

use of org.onap.so.apihandlerinfra.tasksbeans.TasksRequest in project so by onap.

the class ManualTasksTest method completeTaskBpelResponseError.

@Test
public void completeTaskBpelResponseError() throws IOException {
    wireMockServer.stubFor(post(urlPathEqualTo("/sobpmnengine/task/55/complete")).willReturn(aResponse().withHeader("Content-Type", "application/json").withFault(Fault.EMPTY_RESPONSE)));
    String taskId = "55";
    TasksRequest taskReq = new TasksRequest();
    RequestDetails reqDetail = new RequestDetails();
    RequestInfo reqInfo = new RequestInfo();
    reqInfo.setRequestorId("testId");
    reqInfo.setSource("testSource");
    reqInfo.setResponseValue(ValidResponses.skip);
    reqDetail.setRequestInfo(reqInfo);
    taskReq.setRequestDetails(reqDetail);
    RequestError expectedResponse = new RequestError();
    ServiceException se = new ServiceException();
    se.setMessageId("SVC1000");
    se.setText("Client from http://localhost:" + env.getProperty("wiremock.server.port") + "/sobpmnengine/task/55/complete failed to connect or respond");
    expectedResponse.setServiceException(se);
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON);
    headers.set("Content-Type", MediaType.APPLICATION_JSON);
    headers.set(ECOMP_REQUEST_ID, "987654321");
    headers.set(ONAP_PARTNER_NAME, "VID");
    HttpEntity<TasksRequest> entity = new HttpEntity<TasksRequest>(taskReq, headers);
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(basePath) + taskId + "/complete");
    ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
    assertEquals(Response.Status.BAD_GATEWAY.getStatusCode(), response.getStatusCode().value());
    assertThat(realResponse, sameBeanAs(expectedResponse));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) RequestError(org.onap.so.serviceinstancebeans.RequestError) ServiceException(org.onap.so.serviceinstancebeans.ServiceException) HttpEntity(org.springframework.http.HttpEntity) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) TasksRequest(org.onap.so.apihandlerinfra.tasksbeans.TasksRequest) RequestInfo(org.onap.so.apihandlerinfra.tasksbeans.RequestInfo) RequestDetails(org.onap.so.apihandlerinfra.tasksbeans.RequestDetails) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with TasksRequest

use of org.onap.so.apihandlerinfra.tasksbeans.TasksRequest in project so by onap.

the class ManualTasksTest method testCreateOpEnvObjectMapperError.

@Test
public void testCreateOpEnvObjectMapperError() throws IOException {
    TestAppender.events.clear();
    wireMockServer.stubFor(post(urlPathEqualTo("/sobpmnengine/task/55/complete")).willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_OK)));
    String taskId = "55";
    TasksRequest taskReq = new TasksRequest();
    RequestDetails reqDetail = new RequestDetails();
    RequestInfo reqInfo = new RequestInfo();
    reqInfo.setRequestorId("testId");
    reqInfo.setSource("testSource");
    reqInfo.setResponseValue(ValidResponses.skip);
    reqDetail.setRequestInfo(reqInfo);
    taskReq.setRequestDetails(reqDetail);
    // expected response
    TaskRequestReference expectedResponse = new TaskRequestReference();
    expectedResponse.setTaskId(taskId);
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON);
    headers.set("Content-Type", MediaType.APPLICATION_JSON);
    headers.set(ECOMP_REQUEST_ID, "987654321");
    headers.set(ONAP_PARTNER_NAME, "VID");
    HttpEntity<TasksRequest> entity = new HttpEntity<TasksRequest>(taskReq, headers);
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(basePath) + taskId + "/complete");
    ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    TaskRequestReference realResponse = mapper.readValue(response.getBody(), TaskRequestReference.class);
    // then
    assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatusCode().value());
    assertThat(realResponse, sameBeanAs(expectedResponse));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) TasksRequest(org.onap.so.apihandlerinfra.tasksbeans.TasksRequest) TaskRequestReference(org.onap.so.apihandlerinfra.tasksbeans.TaskRequestReference) RequestInfo(org.onap.so.apihandlerinfra.tasksbeans.RequestInfo) RequestDetails(org.onap.so.apihandlerinfra.tasksbeans.RequestDetails) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 4 with TasksRequest

use of org.onap.so.apihandlerinfra.tasksbeans.TasksRequest in project so by onap.

the class ManualTasksTest method completeTaskValidationError.

@Test
public void completeTaskValidationError() throws IOException {
    String taskId = "55";
    TasksRequest taskReq = new TasksRequest();
    RequestDetails reqDetail = new RequestDetails();
    RequestInfo reqInfo = new RequestInfo();
    reqInfo.setSource("testSource");
    reqInfo.setResponseValue(ValidResponses.skip);
    reqDetail.setRequestInfo(reqInfo);
    taskReq.setRequestDetails(reqDetail);
    RequestError expectedResponse = new RequestError();
    ServiceException se = new ServiceException();
    se.setMessageId("SVC0002");
    se.setText("Mapping of request to JSON Object failed. No valid requestorId is specified");
    expectedResponse.setServiceException(se);
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON);
    headers.set("Content-Type", MediaType.APPLICATION_JSON);
    headers.set(ECOMP_REQUEST_ID, "987654321");
    headers.set(ONAP_PARTNER_NAME, "VID");
    HttpEntity<TasksRequest> entity = new HttpEntity<TasksRequest>(taskReq, headers);
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(basePath) + taskId + "/complete");
    ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(), HttpMethod.POST, entity, String.class);
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    RequestError realResponse = mapper.readValue(response.getBody(), RequestError.class);
    assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatusCode().value());
    assertThat(realResponse, sameBeanAs(expectedResponse));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) RequestError(org.onap.so.serviceinstancebeans.RequestError) ServiceException(org.onap.so.serviceinstancebeans.ServiceException) HttpEntity(org.springframework.http.HttpEntity) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) TasksRequest(org.onap.so.apihandlerinfra.tasksbeans.TasksRequest) RequestInfo(org.onap.so.apihandlerinfra.tasksbeans.RequestInfo) RequestDetails(org.onap.so.apihandlerinfra.tasksbeans.RequestDetails) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 TasksRequest (org.onap.so.apihandlerinfra.tasksbeans.TasksRequest)4 Test (org.junit.Test)3 RequestDetails (org.onap.so.apihandlerinfra.tasksbeans.RequestDetails)3 RequestInfo (org.onap.so.apihandlerinfra.tasksbeans.RequestInfo)3 HttpEntity (org.springframework.http.HttpEntity)3 HttpHeaders (org.springframework.http.HttpHeaders)3 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)3 TaskRequestReference (org.onap.so.apihandlerinfra.tasksbeans.TaskRequestReference)2 RequestError (org.onap.so.serviceinstancebeans.RequestError)2 ServiceException (org.onap.so.serviceinstancebeans.ServiceException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Operation (io.swagger.v3.oas.annotations.Operation)1 IOException (java.io.IOException)1 Transactional (javax.transaction.Transactional)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 ResponseBuilder (org.onap.so.apihandler.common.ResponseBuilder)1