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;
28 import java.io.IOException;
29 import java.io.StringReader;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import javax.xml.parsers.DocumentBuilder;
33 import javax.xml.parsers.DocumentBuilderFactory;
34 import org.camunda.bpm.engine.delegate.BpmnError;
35 import org.camunda.bpm.engine.delegate.DelegateExecution;
36 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.junit.runner.RunWith;
42 import org.mockito.ArgumentCaptor;
43 import org.mockito.InjectMocks;
44 import org.mockito.Mock;
45 import org.mockito.Spy;
46 import org.mockito.runners.MockitoJUnitRunner;
47 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
48 import org.onap.so.client.exception.BadResponseException;
49 import org.onap.so.client.exception.ExceptionBuilder;
50 import org.onap.so.client.exception.MapperException;
51 import org.onap.so.client.sdnc.SDNCClient;
52 import org.onap.so.client.sdnc.beans.SDNCRequest;
53 import org.onap.so.client.sdnc.endpoint.SDNCTopology;
54 import org.w3c.dom.Document;
55 import org.xml.sax.InputSource;
56 import com.fasterxml.jackson.core.JsonParseException;
57 import com.fasterxml.jackson.databind.JsonMappingException;
58 import com.fasterxml.jackson.databind.ObjectMapper;
60 @RunWith(MockitoJUnitRunner.class)
61 public class SDNCRequestTasksTest extends SDNCRequestTasks {
64 public ExpectedException expectedException = ExpectedException.none();
67 SDNCRequestTasks sndcRequestTasks = new SDNCRequestTasks();
70 SDNCClient sdncClient;
73 private ExceptionBuilder exceptionBuilder;
75 protected DelegateExecution delegateExecution;
80 delegateExecution = new DelegateExecutionFake();
81 delegateExecution.setVariable("SDNCRequest", createSDNCRequest());
85 public void createCorrelationVariables_Test() {
86 sndcRequestTasks.createCorrelationVariables(delegateExecution);
87 assertEquals("correlationValue", delegateExecution.getVariable("correlationName_CORRELATOR"));
91 public void callSDNC_Final_Test() throws MapperException, BadResponseException, IOException {
92 final String sdncResponse =
93 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientPut200Response.json")));
94 doReturn(sdncResponse).when(sdncClient).post(createSDNCRequest().getSDNCPayload(), SDNCTopology.CONFIGURATION);
95 sndcRequestTasks.callSDNC(delegateExecution);
96 assertEquals(true, delegateExecution.getVariable("isSDNCCompleted"));
100 public void callSDNC_Not_Final_Test() throws MapperException, BadResponseException, IOException {
101 final String sdncResponse = new String(
102 Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientPut200ResponseNotFinal.json")));
103 doReturn(sdncResponse).when(sdncClient).post(createSDNCRequest().getSDNCPayload(), SDNCTopology.CONFIGURATION);
104 sndcRequestTasks.callSDNC(delegateExecution);
105 assertEquals(false, delegateExecution.getVariable("isSDNCCompleted"));
109 public void callSDNC_Error_Test() throws MapperException, BadResponseException {
110 doThrow(MapperException.class).when(sdncClient).post(createSDNCRequest().getSDNCPayload(),
111 SDNCTopology.CONFIGURATION);
112 doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
113 expectedException.expect(BpmnError.class);
114 sndcRequestTasks.callSDNC(delegateExecution);
118 public void convertIndicatorToBoolean_True_Test() throws MapperException, BadResponseException {
119 boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("Y");
120 assertEquals(true, testValue);
124 public void convertIndicatorToBoolean_False_Test() throws MapperException, BadResponseException {
125 boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("N");
126 assertEquals(false, testValue);
130 public void HandleTimeout_Test() throws MapperException, BadResponseException {
131 doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
132 expectedException.expect(BpmnError.class);
133 sndcRequestTasks.handleTimeOutException(delegateExecution);
137 public void processCallBack_Final_Test() throws MapperException, BadResponseException, IOException {
138 final String sdncResponse =
139 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Async_Request2.xml")));
140 delegateExecution.setVariable("correlationName_MESSAGE", sdncResponse);
141 sndcRequestTasks.processCallback(delegateExecution);
142 assertEquals(true, delegateExecution.getVariable(IS_CALLBACK_COMPLETED));
146 public void getXmlElementTest() throws Exception {
147 final String sdncResponse =
148 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 = mapper.readValue(
171 Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Client_Request.json")),
172 GenericResourceApiServiceOperationInformation.class);
173 request.setSDNCPayload(sdncReq);
174 } catch (JsonParseException e) {
176 } catch (JsonMappingException e) {
178 } catch (IOException e) {