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 java.io.IOException;
27 import java.io.StringReader;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32 import org.camunda.bpm.engine.delegate.BpmnError;
33 import org.camunda.bpm.engine.delegate.DelegateExecution;
34 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.junit.runner.RunWith;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.Spy;
43 import org.mockito.runners.MockitoJUnitRunner;
44 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
45 import org.onap.so.client.exception.BadResponseException;
46 import org.onap.so.client.exception.ExceptionBuilder;
47 import org.onap.so.client.exception.MapperException;
48 import org.onap.so.client.sdnc.SDNCClient;
49 import org.onap.so.client.sdnc.beans.SDNCRequest;
50 import org.onap.so.client.sdnc.endpoint.SDNCTopology;
51 import org.w3c.dom.Document;
52 import org.xml.sax.InputSource;
53 import com.fasterxml.jackson.core.JsonParseException;
54 import com.fasterxml.jackson.databind.JsonMappingException;
55 import com.fasterxml.jackson.databind.ObjectMapper;
57 @RunWith(MockitoJUnitRunner.class)
58 public class SDNCRequestTasksTest extends SDNCRequestTasks {
61 public ExpectedException expectedException = ExpectedException.none();
64 SDNCRequestTasks sndcRequestTasks = new SDNCRequestTasks();
67 SDNCClient sdncClient;
71 private ExceptionBuilder exceptionBuilder;
73 protected DelegateExecution delegateExecution;
78 delegateExecution = new DelegateExecutionFake();
79 delegateExecution.setVariable("SDNCRequest", createSDNCRequest());
83 public void createCorrelationVariables_Test() {
84 sndcRequestTasks.createCorrelationVariables(delegateExecution);
85 assertEquals("correlationValue", delegateExecution.getVariable("correlationName_CORRELATOR"));
89 public void callSDNC_Final_Test() throws MapperException, BadResponseException, IOException {
90 final String sdncResponse =
91 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientPut200Response.json")));
92 doReturn(sdncResponse).when(sdncClient).post(createSDNCRequest().getSDNCPayload(), SDNCTopology.CONFIGURATION);
93 sndcRequestTasks.callSDNC(delegateExecution);
94 assertEquals(true, delegateExecution.getVariable("isSDNCCompleted"));
98 public void callSDNC_Not_Final_Test() throws MapperException, BadResponseException, IOException {
99 final String sdncResponse = new String(
100 Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientPut200ResponseNotFinal.json")));
101 doReturn(sdncResponse).when(sdncClient).post(createSDNCRequest().getSDNCPayload(), SDNCTopology.CONFIGURATION);
102 sndcRequestTasks.callSDNC(delegateExecution);
103 assertEquals(false, delegateExecution.getVariable("isSDNCCompleted"));
107 public void callSDNC_Error_Test() throws MapperException, BadResponseException {
108 doThrow(MapperException.class).when(sdncClient).post(createSDNCRequest().getSDNCPayload(),
109 SDNCTopology.CONFIGURATION);
110 doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
111 expectedException.expect(BpmnError.class);
112 sndcRequestTasks.callSDNC(delegateExecution);
116 public void convertIndicatorToBoolean_True_Test() throws MapperException, BadResponseException {
117 boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("Y");
118 assertEquals(true, testValue);
122 public void convertIndicatorToBoolean_False_Test() throws MapperException, BadResponseException {
123 boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("N");
124 assertEquals(false, testValue);
128 public void HandleTimeout_Test() throws MapperException, BadResponseException {
129 doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
130 expectedException.expect(BpmnError.class);
131 sndcRequestTasks.handleTimeOutException(delegateExecution);
135 public void processCallBack_Final_Test() throws MapperException, BadResponseException, IOException {
136 final String sdncResponse =
137 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Async_Request2.xml")));
138 delegateExecution.setVariable("correlationName_MESSAGE", sdncResponse);
139 sndcRequestTasks.processCallback(delegateExecution);
140 assertEquals(true, delegateExecution.getVariable(IS_CALLBACK_COMPLETED));
144 public void getXmlElementTest() throws Exception {
145 final String sdncResponse =
146 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Async_Request2.xml")));
147 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
149 db = dbf.newDocumentBuilder();
150 Document doc = db.parse(new InputSource(new StringReader(sdncResponse)));
152 String finalMessageIndicator = getXmlElement(doc, "//*:ack-final-indicator");
153 String responseCode = getXmlElement(doc, "/input/response-code");
154 String responseMessage = getXmlElement(doc, "/input/response-message");
156 assertEquals("Y", finalMessageIndicator);
157 assertEquals("200", responseCode);
158 assertEquals("Success", responseMessage);
161 public SDNCRequest createSDNCRequest() {
162 SDNCRequest request = new SDNCRequest();
163 request.setCorrelationName("correlationName");
164 request.setCorrelationValue("correlationValue");
165 request.setTopology(SDNCTopology.CONFIGURATION);
166 ObjectMapper mapper = new ObjectMapper();
168 GenericResourceApiServiceOperationInformation sdncReq = mapper.readValue(
169 Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Client_Request.json")),
170 GenericResourceApiServiceOperationInformation.class);
171 request.setSDNCPayload(sdncReq);
172 } catch (JsonParseException e) {
174 } catch (JsonMappingException e) {
176 } catch (IOException e) {