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.doReturn;
25 import static org.mockito.Mockito.doThrow;
26 import static org.mockito.Mockito.times;
27 import static org.mockito.Mockito.verify;
29 import java.io.IOException;
30 import java.io.StringReader;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
34 import javax.xml.parsers.DocumentBuilder;
35 import javax.xml.parsers.DocumentBuilderFactory;
37 import org.camunda.bpm.engine.delegate.BpmnError;
38 import org.camunda.bpm.engine.delegate.DelegateExecution;
40 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
41 import org.junit.Before;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.rules.ExpectedException;
45 import org.junit.runner.RunWith;
46 import org.mockito.ArgumentCaptor;
47 import org.mockito.InjectMocks;
48 import org.mockito.Mock;
49 import org.mockito.Spy;
50 import org.mockito.runners.MockitoJUnitRunner;
51 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
52 import org.onap.so.client.exception.BadResponseException;
53 import org.onap.so.client.exception.ExceptionBuilder;
54 import org.onap.so.client.exception.MapperException;
55 import org.onap.so.client.sdnc.SDNCClient;
56 import org.onap.so.client.sdnc.beans.SDNCRequest;
57 import org.onap.so.client.sdnc.endpoint.SDNCTopology;
58 import org.w3c.dom.Document;
59 import org.xml.sax.InputSource;
61 import com.fasterxml.jackson.core.JsonParseException;
62 import com.fasterxml.jackson.databind.JsonMappingException;
63 import com.fasterxml.jackson.databind.ObjectMapper;
65 @RunWith(MockitoJUnitRunner.class)
66 public class SDNCRequestTasksTest extends SDNCRequestTasks{
69 public ExpectedException expectedException = ExpectedException.none();
72 SDNCRequestTasks sndcRequestTasks = new SDNCRequestTasks();
75 SDNCClient sdncClient;
78 private ExceptionBuilder exceptionBuilder;
80 protected DelegateExecution delegateExecution;
85 delegateExecution = new DelegateExecutionFake();
86 delegateExecution.setVariable("SDNCRequest", createSDNCRequest());
90 public void createCorrelationVariables_Test(){
91 sndcRequestTasks.createCorrelationVariables(delegateExecution);
92 assertEquals("correlationValue",delegateExecution.getVariable("correlationName_CORRELATOR"));
96 public void callSDNC_Final_Test() throws MapperException, BadResponseException, IOException{
97 final String sdncResponse = new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientPut200Response.json")));
98 doReturn(sdncResponse).when(sdncClient).post(createSDNCRequest().getSDNCPayload(),SDNCTopology.CONFIGURATION);
99 sndcRequestTasks.callSDNC(delegateExecution);
100 assertEquals(true,delegateExecution.getVariable("isSDNCCompleted"));
104 public void callSDNC_Not_Final_Test() throws MapperException, BadResponseException, IOException{
105 final String sdncResponse = new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientPut200ResponseNotFinal.json")));
106 doReturn(sdncResponse).when(sdncClient).post(createSDNCRequest().getSDNCPayload(),SDNCTopology.CONFIGURATION);
107 sndcRequestTasks.callSDNC(delegateExecution);
108 assertEquals(false,delegateExecution.getVariable("isSDNCCompleted"));
112 public void callSDNC_Error_Test() throws MapperException, BadResponseException{
113 doThrow(MapperException.class).when(sdncClient).post(createSDNCRequest().getSDNCPayload(),SDNCTopology.CONFIGURATION);
114 doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
115 expectedException.expect(BpmnError.class);
116 sndcRequestTasks.callSDNC(delegateExecution);
120 public void convertIndicatorToBoolean_True_Test() throws MapperException, BadResponseException{
121 boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("Y");
122 assertEquals(true,testValue);
126 public void convertIndicatorToBoolean_False_Test() throws MapperException, BadResponseException{
127 boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("N");
128 assertEquals(false,testValue);
132 public void HandleTimeout_Test() throws MapperException, BadResponseException{
133 doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
134 expectedException.expect(BpmnError.class);
135 sndcRequestTasks.handleTimeOutException(delegateExecution);
139 public void processCallBack_Final_Test() throws MapperException, BadResponseException, IOException{
140 final String sdncResponse = new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Async_Request2.xml")));
141 delegateExecution.setVariable("correlationName_MESSAGE", sdncResponse);
142 sndcRequestTasks.processCallback(delegateExecution);
143 assertEquals(true,delegateExecution.getVariable(IS_CALLBACK_COMPLETED));
147 public void getXmlElementTest() throws Exception {
148 final String sdncResponse = new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Async_Request2.xml")));
149 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
151 db = dbf.newDocumentBuilder ();
152 Document doc = db.parse (new InputSource(new StringReader(sdncResponse)));
154 String finalMessageIndicator = getXmlElement(doc, "/input/ack-final-indicator");
155 String responseCode = getXmlElement(doc, "/input/response-code");
156 String responseMessage = getXmlElement(doc, "/input/response-message");
158 assertEquals("Y", finalMessageIndicator);
159 assertEquals("200", responseCode);
160 assertEquals("Success", responseMessage);
163 public SDNCRequest createSDNCRequest(){
164 SDNCRequest request = new SDNCRequest();
165 request.setCorrelationName("correlationName");
166 request.setCorrelationValue("correlationValue");
167 request.setTopology(SDNCTopology.CONFIGURATION);
168 ObjectMapper mapper = new ObjectMapper();
170 GenericResourceApiServiceOperationInformation sdncReq =
171 mapper.readValue(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Client_Request.json")), GenericResourceApiServiceOperationInformation.class);
172 request.setSDNCPayload(sdncReq);
173 } catch (JsonParseException e) {
175 } catch (JsonMappingException e) {
177 } catch (IOException e) {