6a94b357e07e8780ad0257100de9b8c7e1dffc35
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.sdnc.tasks;
22
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;
56
57 @RunWith(MockitoJUnitRunner.class)
58 public class SDNCRequestTasksTest extends SDNCRequestTasks {
59
60     @Rule
61     public ExpectedException expectedException = ExpectedException.none();
62
63     @InjectMocks
64     SDNCRequestTasks sndcRequestTasks = new SDNCRequestTasks();
65
66     @Mock
67     SDNCClient sdncClient;
68
69
70     @Spy
71     private ExceptionBuilder exceptionBuilder;
72
73     protected DelegateExecution delegateExecution;
74
75
76     @Before
77     public void setup() {
78         delegateExecution = new DelegateExecutionFake();
79         delegateExecution.setVariable("SDNCRequest", createSDNCRequest());
80     }
81
82     @Test
83     public void createCorrelationVariables_Test() {
84         sndcRequestTasks.createCorrelationVariables(delegateExecution);
85         assertEquals("correlationValue", delegateExecution.getVariable("correlationName_CORRELATOR"));
86     }
87
88     @Test
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"));
95     }
96
97     @Test
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"));
104     }
105
106     @Test
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);
113     }
114
115     @Test
116     public void convertIndicatorToBoolean_True_Test() throws MapperException, BadResponseException {
117         boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("Y");
118         assertEquals(true, testValue);
119     }
120
121     @Test
122     public void convertIndicatorToBoolean_False_Test() throws MapperException, BadResponseException {
123         boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("N");
124         assertEquals(false, testValue);
125     }
126
127     @Test
128     public void HandleTimeout_Test() throws MapperException, BadResponseException {
129         doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
130         expectedException.expect(BpmnError.class);
131         sndcRequestTasks.handleTimeOutException(delegateExecution);
132     }
133
134     @Test
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));
141     }
142
143     @Test
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();
148         DocumentBuilder db;
149         db = dbf.newDocumentBuilder();
150         Document doc = db.parse(new InputSource(new StringReader(sdncResponse)));
151
152         String finalMessageIndicator = getXmlElement(doc, "//*:ack-final-indicator");
153         String responseCode = getXmlElement(doc, "/input/response-code");
154         String responseMessage = getXmlElement(doc, "/input/response-message");
155
156         assertEquals("Y", finalMessageIndicator);
157         assertEquals("200", responseCode);
158         assertEquals("Success", responseMessage);
159     }
160
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();
167         try {
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) {
173
174         } catch (JsonMappingException e) {
175
176         } catch (IOException e) {
177
178         }
179
180         return request;
181     }
182
183 }