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