596d4ad159799c1dfe2965d9dd0bfe08c50b6650
[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.junit.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 {
59
60     @Rule
61     public ExpectedException expectedException = ExpectedException.none();
62
63     @InjectMocks
64     SDNCRequestTasks sndcRequestTasks;
65
66     @Mock
67     SDNCClient sdncClient;
68
69     @Spy
70     private ExceptionBuilder exceptionBuilder;
71
72     protected DelegateExecution delegateExecution;
73
74
75     @Before
76     public void setup() {
77         delegateExecution = new DelegateExecutionFake();
78         delegateExecution.setVariable("SDNCRequest", createSDNCRequest());
79     }
80
81     @Test
82     public void createCorrelationVariables_Test() {
83         sndcRequestTasks.createCorrelationVariables(delegateExecution);
84         assertEquals("correlationValue", delegateExecution.getVariable("correlationName_CORRELATOR"));
85     }
86
87     @Test
88     public void callSDNC_Final_Test() throws MapperException, BadResponseException, IOException {
89         final String sdncResponse =
90                 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientPut200Response.json")));
91         doReturn(sdncResponse).when(sdncClient).post(createSDNCRequest().getSDNCPayload(), SDNCTopology.CONFIGURATION);
92         sndcRequestTasks.callSDNC(delegateExecution);
93         assertEquals(true, delegateExecution.getVariable("isSDNCCompleted"));
94     }
95
96     @Test
97     public void callSDNC_Not_Final_Test() throws MapperException, BadResponseException, IOException {
98         final String sdncResponse = new String(
99                 Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientPut200ResponseNotFinal.json")));
100         doReturn(sdncResponse).when(sdncClient).post(createSDNCRequest().getSDNCPayload(), SDNCTopology.CONFIGURATION);
101         sndcRequestTasks.callSDNC(delegateExecution);
102         assertEquals(false, delegateExecution.getVariable("isSDNCCompleted"));
103     }
104
105     @Test
106     public void callSDNC_Error_Test() throws MapperException, BadResponseException {
107         doThrow(MapperException.class).when(sdncClient).post(createSDNCRequest().getSDNCPayload(),
108                 SDNCTopology.CONFIGURATION);
109         doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
110         expectedException.expect(BpmnError.class);
111         sndcRequestTasks.callSDNC(delegateExecution);
112     }
113
114     @Test
115     public void convertIndicatorToBoolean_True_Test() throws MapperException, BadResponseException {
116         boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("Y");
117         assertEquals(true, testValue);
118     }
119
120     @Test
121     public void convertIndicatorToBoolean_False_Test() throws MapperException, BadResponseException {
122         boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("N");
123         assertEquals(false, testValue);
124     }
125
126     @Test
127     public void HandleTimeout_Test() throws MapperException, BadResponseException {
128         doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
129         expectedException.expect(BpmnError.class);
130         sndcRequestTasks.handleTimeOutException(delegateExecution);
131     }
132
133     @Test
134     public void processCallBack_Final_Test() throws MapperException, BadResponseException, IOException {
135         final String sdncResponse =
136                 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Async_Request2.xml")));
137         delegateExecution.setVariable("correlationName_MESSAGE", sdncResponse);
138         sndcRequestTasks.processCallback(delegateExecution);
139         assertEquals(true, delegateExecution.getVariable("isCallbackCompleted"));
140     }
141
142     @Test
143     public void getXmlElementTest() throws Exception {
144         final String sdncResponse =
145                 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Async_Request2.xml")));
146         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
147         DocumentBuilder db;
148         db = dbf.newDocumentBuilder();
149         Document doc = db.parse(new InputSource(new StringReader(sdncResponse)));
150
151         String finalMessageIndicator = SDNCRequestTasks.getXmlElement(doc, "//*:ack-final-indicator");
152         String responseCode = SDNCRequestTasks.getXmlElement(doc, "/input/response-code");
153         String responseMessage = SDNCRequestTasks.getXmlElement(doc, "/input/response-message");
154
155         assertEquals("Y", finalMessageIndicator);
156         assertEquals("200", responseCode);
157         assertEquals("Success", responseMessage);
158     }
159
160     public SDNCRequest createSDNCRequest() {
161         SDNCRequest request = new SDNCRequest();
162         request.setCorrelationName("correlationName");
163         request.setCorrelationValue("correlationValue");
164         request.setTopology(SDNCTopology.CONFIGURATION);
165         ObjectMapper mapper = new ObjectMapper();
166         try {
167             GenericResourceApiServiceOperationInformation sdncReq = mapper.readValue(
168                     Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Client_Request.json")),
169                     GenericResourceApiServiceOperationInformation.class);
170             request.setSDNCPayload(sdncReq);
171         } catch (JsonParseException e) {
172
173         } catch (JsonMappingException e) {
174
175         } catch (IOException e) {
176
177         }
178
179         return request;
180     }
181
182 }