a6efbd59891aaee6a8c47908b811f44e503f4b62
[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.nio.file.Files;
31 import java.nio.file.Paths;
32
33 import org.camunda.bpm.engine.delegate.BpmnError;
34 import org.camunda.bpm.engine.delegate.DelegateExecution;
35
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
55 import com.fasterxml.jackson.core.JsonParseException;
56 import com.fasterxml.jackson.databind.JsonMappingException;
57 import com.fasterxml.jackson.databind.ObjectMapper;
58
59 @RunWith(MockitoJUnitRunner.class)
60 public class SDNCRequestTasksTest extends SDNCRequestTasks{
61         
62         @Rule
63         public ExpectedException expectedException = ExpectedException.none();
64         
65         @InjectMocks
66         SDNCRequestTasks sndcRequestTasks = new SDNCRequestTasks();
67         
68         @Mock
69         SDNCClient sdncClient;
70         
71         @Spy
72         private ExceptionBuilder exceptionBuilder;
73         
74         protected DelegateExecution delegateExecution;
75         
76         
77         @Before
78         public void setup(){            
79         delegateExecution = new DelegateExecutionFake();
80         delegateExecution.setVariable("SDNCRequest", createSDNCRequest());      
81         }
82         
83         @Test
84         public void createCorrelationVariables_Test(){                  
85                 sndcRequestTasks.createCorrelationVariables(delegateExecution);
86                 assertEquals("correlationValue",delegateExecution.getVariable("correlationName_CORRELATOR"));
87         }
88         
89         @Test
90         public void callSDNC_Final_Test() throws MapperException, BadResponseException, IOException{     
91                 final String sdncResponse = 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(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(),SDNCTopology.CONFIGURATION);
108                 doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
109                 expectedException.expect(BpmnError.class);
110                 sndcRequestTasks.callSDNC(delegateExecution);           
111         }
112         
113         @Test
114         public void convertIndicatorToBoolean_True_Test() throws MapperException, BadResponseException{                 
115                 boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("Y");            
116                 assertEquals(true,testValue);
117         }
118         
119         @Test
120         public void convertIndicatorToBoolean_False_Test() throws MapperException, BadResponseException{                        
121                 boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("N");            
122                 assertEquals(false,testValue);
123         }
124         
125         @Test
126         public void HandleTimeout_Test() throws MapperException, BadResponseException{                                  
127                 doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
128                 expectedException.expect(BpmnError.class);
129                 sndcRequestTasks.handleTimeOutException(delegateExecution);             
130         }
131         
132         
133         @Test
134         public void HandleSyncError_Test() throws MapperException, BadResponseException{                
135                 delegateExecution.setVariable("SDNCSyncError", "Error in SDNC Request");        
136                 doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
137                 expectedException.expect(BpmnError.class);
138                 sndcRequestTasks.handleSyncError(delegateExecution);            
139         }
140         
141         @Test
142         public void processCallBack_Final_Test() throws MapperException, BadResponseException, IOException{
143                 final String sdncResponse = new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_ASYNC_Request.json")));    
144                 delegateExecution.setVariable("correlationName_MESSAGE", sdncResponse);                 
145                 sndcRequestTasks.processCallback(delegateExecution);            
146                 assertEquals(true,delegateExecution.getVariable(IS_CALLBACK_COMPLETED));
147         }
148         
149         public SDNCRequest createSDNCRequest(){
150                 SDNCRequest request = new SDNCRequest();
151                 request.setCorrelationName("correlationName");
152                 request.setCorrelationValue("correlationValue");                
153                 request.setTopology(SDNCTopology.CONFIGURATION);
154                 ObjectMapper mapper = new ObjectMapper();
155                 try {
156                         GenericResourceApiServiceOperationInformation sdncReq = 
157                                         mapper.readValue(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Client_Request.json")), GenericResourceApiServiceOperationInformation.class);
158                         request.setSDNCPayload(sdncReq);
159                 } catch (JsonParseException e) {                        
160                         
161                 } catch (JsonMappingException e) {
162                         
163                 } catch (IOException e) {
164                         
165                 }
166                 
167                 return request;
168         }
169         
170 }