Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / sdnc / tasks / SDNCRequestTasksTest.java
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 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;
59
60 @RunWith(MockitoJUnitRunner.class)
61 public class SDNCRequestTasksTest extends SDNCRequestTasks {
62
63     @Rule
64     public ExpectedException expectedException = ExpectedException.none();
65
66     @InjectMocks
67     SDNCRequestTasks sndcRequestTasks = new SDNCRequestTasks();
68
69     @Mock
70     SDNCClient sdncClient;
71
72     @Spy
73     private ExceptionBuilder exceptionBuilder;
74
75     protected DelegateExecution delegateExecution;
76
77
78     @Before
79     public void setup() {
80         delegateExecution = new DelegateExecutionFake();
81         delegateExecution.setVariable("SDNCRequest", createSDNCRequest());
82     }
83
84     @Test
85     public void createCorrelationVariables_Test() {
86         sndcRequestTasks.createCorrelationVariables(delegateExecution);
87         assertEquals("correlationValue", delegateExecution.getVariable("correlationName_CORRELATOR"));
88     }
89
90     @Test
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"));
97     }
98
99     @Test
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"));
106     }
107
108     @Test
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);
115     }
116
117     @Test
118     public void convertIndicatorToBoolean_True_Test() throws MapperException, BadResponseException {
119         boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("Y");
120         assertEquals(true, testValue);
121     }
122
123     @Test
124     public void convertIndicatorToBoolean_False_Test() throws MapperException, BadResponseException {
125         boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("N");
126         assertEquals(false, testValue);
127     }
128
129     @Test
130     public void HandleTimeout_Test() throws MapperException, BadResponseException {
131         doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
132         expectedException.expect(BpmnError.class);
133         sndcRequestTasks.handleTimeOutException(delegateExecution);
134     }
135
136     @Test
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));
143     }
144
145     @Test
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();
150         DocumentBuilder db;
151         db = dbf.newDocumentBuilder();
152         Document doc = db.parse(new InputSource(new StringReader(sdncResponse)));
153
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");
157
158         assertEquals("Y", finalMessageIndicator);
159         assertEquals("200", responseCode);
160         assertEquals("Success", responseMessage);
161     }
162
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();
169         try {
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) {
175
176         } catch (JsonMappingException e) {
177
178         } catch (IOException e) {
179
180         }
181
182         return request;
183     }
184
185 }