2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.so.bpmn.infrastructure.sdnc.tasks;
 
  23 import static org.junit.Assert.assertEquals;
 
  24 import static org.mockito.Mockito.doNothing;
 
  25 import static org.mockito.Mockito.doReturn;
 
  26 import static org.mockito.Mockito.doThrow;
 
  27 import java.io.IOException;
 
  28 import java.io.StringReader;
 
  29 import java.nio.file.Files;
 
  30 import java.nio.file.Paths;
 
  31 import javax.xml.parsers.DocumentBuilder;
 
  32 import javax.xml.parsers.DocumentBuilderFactory;
 
  33 import org.camunda.bpm.engine.delegate.BpmnError;
 
  34 import org.camunda.bpm.engine.delegate.DelegateExecution;
 
  35 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
 
  36 import org.junit.Before;
 
  37 import org.junit.Rule;
 
  38 import org.junit.Test;
 
  39 import org.junit.rules.ExpectedException;
 
  40 import org.junit.runner.RunWith;
 
  41 import org.mockito.InjectMocks;
 
  42 import org.mockito.Mock;
 
  43 import org.mockito.Spy;
 
  44 import org.mockito.runners.MockitoJUnitRunner;
 
  45 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
 
  46 import org.onap.so.client.exception.BadResponseException;
 
  47 import org.onap.so.client.exception.ExceptionBuilder;
 
  48 import org.onap.so.client.exception.MapperException;
 
  49 import org.onap.so.client.sdnc.SDNCClient;
 
  50 import org.onap.so.client.sdnc.beans.SDNCRequest;
 
  51 import org.onap.so.client.sdnc.endpoint.SDNCTopology;
 
  52 import org.onap.so.utils.TargetEntity;
 
  53 import org.w3c.dom.Document;
 
  54 import org.xml.sax.InputSource;
 
  55 import com.fasterxml.jackson.core.JsonParseException;
 
  56 import com.fasterxml.jackson.databind.JsonMappingException;
 
  57 import com.fasterxml.jackson.databind.ObjectMapper;
 
  59 @RunWith(MockitoJUnitRunner.class)
 
  60 public class SDNCRequestTasksTest extends SDNCRequestTasks {
 
  63     public ExpectedException expectedException = ExpectedException.none();
 
  66     SDNCRequestTasks sndcRequestTasks = new SDNCRequestTasks();
 
  69     SDNCClient sdncClient;
 
  72     private ExceptionBuilder exceptionBuilder;
 
  74     protected DelegateExecution delegateExecution;
 
  79         delegateExecution = new DelegateExecutionFake();
 
  80         delegateExecution.setVariable("SDNCRequest", createSDNCRequest());
 
  84     public void createCorrelationVariables_Test() {
 
  85         sndcRequestTasks.createCorrelationVariables(delegateExecution);
 
  86         assertEquals("correlationValue", delegateExecution.getVariable("correlationName_CORRELATOR"));
 
  90     public void callSDNC_Final_Test() throws MapperException, BadResponseException, IOException {
 
  91         final String sdncResponse =
 
  92                 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientPut200Response.json")));
 
  93         doReturn(sdncResponse).when(sdncClient).post(createSDNCRequest().getSDNCPayload(), SDNCTopology.CONFIGURATION);
 
  94         sndcRequestTasks.callSDNC(delegateExecution);
 
  95         assertEquals(true, delegateExecution.getVariable("isSDNCCompleted"));
 
  99     public void callSDNC_Not_Final_Test() throws MapperException, BadResponseException, IOException {
 
 100         final String sdncResponse = new String(
 
 101                 Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientPut200ResponseNotFinal.json")));
 
 102         doReturn(sdncResponse).when(sdncClient).post(createSDNCRequest().getSDNCPayload(), SDNCTopology.CONFIGURATION);
 
 103         sndcRequestTasks.callSDNC(delegateExecution);
 
 104         assertEquals(false, delegateExecution.getVariable("isSDNCCompleted"));
 
 108     public void callSDNC_Error_Test() throws MapperException, BadResponseException {
 
 109         doThrow(MapperException.class).when(sdncClient).post(createSDNCRequest().getSDNCPayload(),
 
 110                 SDNCTopology.CONFIGURATION);
 
 111         doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
 
 112         expectedException.expect(BpmnError.class);
 
 113         sndcRequestTasks.callSDNC(delegateExecution);
 
 117     public void convertIndicatorToBoolean_True_Test() throws MapperException, BadResponseException {
 
 118         boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("Y");
 
 119         assertEquals(true, testValue);
 
 123     public void convertIndicatorToBoolean_False_Test() throws MapperException, BadResponseException {
 
 124         boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("N");
 
 125         assertEquals(false, testValue);
 
 129     public void HandleTimeout_Test() throws MapperException, BadResponseException {
 
 130         doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
 
 131         expectedException.expect(BpmnError.class);
 
 132         sndcRequestTasks.handleTimeOutException(delegateExecution);
 
 136     public void processCallBack_Final_Test() throws MapperException, BadResponseException, IOException {
 
 137         final String sdncResponse =
 
 138                 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Async_Request2.xml")));
 
 139         delegateExecution.setVariable("correlationName_MESSAGE", sdncResponse);
 
 140         sndcRequestTasks.processCallback(delegateExecution);
 
 141         assertEquals(true, delegateExecution.getVariable(IS_CALLBACK_COMPLETED));
 
 145     public void getXmlElementTest() throws Exception {
 
 146         final String sdncResponse =
 
 147                 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Async_Request2.xml")));
 
 148         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 
 150         db = dbf.newDocumentBuilder();
 
 151         Document doc = db.parse(new InputSource(new StringReader(sdncResponse)));
 
 153         String finalMessageIndicator = getXmlElement(doc, "//*:ack-final-indicator");
 
 154         String responseCode = getXmlElement(doc, "/input/response-code");
 
 155         String responseMessage = getXmlElement(doc, "/input/response-message");
 
 157         assertEquals("Y", finalMessageIndicator);
 
 158         assertEquals("200", responseCode);
 
 159         assertEquals("Success", responseMessage);
 
 162     public SDNCRequest createSDNCRequest() {
 
 163         SDNCRequest request = new SDNCRequest();
 
 164         request.setCorrelationName("correlationName");
 
 165         request.setCorrelationValue("correlationValue");
 
 166         request.setTopology(SDNCTopology.CONFIGURATION);
 
 167         ObjectMapper mapper = new ObjectMapper();
 
 169             GenericResourceApiServiceOperationInformation sdncReq = mapper.readValue(
 
 170                     Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Client_Request.json")),
 
 171                     GenericResourceApiServiceOperationInformation.class);
 
 172             request.setSDNCPayload(sdncReq);
 
 173         } catch (JsonParseException e) {
 
 175         } catch (JsonMappingException e) {
 
 177         } catch (IOException e) {