summaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/sdnc/tasks/SDNCRequestTasksTest.java
blob: f1779cf119e975faa727b9773ddcab54280da8b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.bpmn.infrastructure.sdnc.tasks;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.camunda.bpm.engine.delegate.BpmnError;
import org.camunda.bpm.engine.delegate.DelegateExecution;

import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
import org.onap.so.client.exception.BadResponseException;
import org.onap.so.client.exception.ExceptionBuilder;
import org.onap.so.client.exception.MapperException;
import org.onap.so.client.sdnc.SDNCClient;
import org.onap.so.client.sdnc.beans.SDNCRequest;
import org.onap.so.client.sdnc.endpoint.SDNCTopology;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@RunWith(MockitoJUnitRunner.class)
public class SDNCRequestTasksTest extends SDNCRequestTasks{
	
	@Rule
	public ExpectedException expectedException = ExpectedException.none();
	
	@InjectMocks
	SDNCRequestTasks sndcRequestTasks = new SDNCRequestTasks();
	
	@Mock
	SDNCClient sdncClient;
	
	@Spy
	private ExceptionBuilder exceptionBuilder;
	
	protected DelegateExecution delegateExecution;
	
	
	@Before
	public void setup(){    	
    	delegateExecution = new DelegateExecutionFake();
    	delegateExecution.setVariable("SDNCRequest", createSDNCRequest());    	
	}
	
	@Test
	public void createCorrelationVariables_Test(){	 		
		sndcRequestTasks.createCorrelationVariables(delegateExecution);
		assertEquals("correlationValue",delegateExecution.getVariable("correlationName_CORRELATOR"));
	}
	
	@Test
	public void callSDNC_Final_Test() throws MapperException, BadResponseException, IOException{	 
		final String sdncResponse = new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientPut200Response.json")));	
		doReturn(sdncResponse).when(sdncClient).post(createSDNCRequest().getSDNCPayload(),SDNCTopology.CONFIGURATION);		
		sndcRequestTasks.callSDNC(delegateExecution);
		assertEquals(true,delegateExecution.getVariable("isSDNCCompleted"));
	}
	
	@Test
	public void callSDNC_Not_Final_Test() throws MapperException, BadResponseException, IOException{	 
		final String sdncResponse = new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientPut200ResponseNotFinal.json")));	
		doReturn(sdncResponse).when(sdncClient).post(createSDNCRequest().getSDNCPayload(),SDNCTopology.CONFIGURATION);		
		sndcRequestTasks.callSDNC(delegateExecution);
		assertEquals(false,delegateExecution.getVariable("isSDNCCompleted"));
	}
	
	@Test
	public void callSDNC_Error_Test() throws MapperException, BadResponseException{	 
		doThrow(MapperException.class).when(sdncClient).post(createSDNCRequest().getSDNCPayload(),SDNCTopology.CONFIGURATION);
		doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
		expectedException.expect(BpmnError.class);
		sndcRequestTasks.callSDNC(delegateExecution);		
	}
	
	@Test
	public void convertIndicatorToBoolean_True_Test() throws MapperException, BadResponseException{			
		boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("Y");		
		assertEquals(true,testValue);
	}
	
	@Test
	public void convertIndicatorToBoolean_False_Test() throws MapperException, BadResponseException{			
		boolean testValue = sndcRequestTasks.convertIndicatorToBoolean("N");		
		assertEquals(false,testValue);
	}
	
	@Test
	public void HandleTimeout_Test() throws MapperException, BadResponseException{					
		doReturn("processKey").when(exceptionBuilder).getProcessKey(delegateExecution);
		expectedException.expect(BpmnError.class);
		sndcRequestTasks.handleTimeOutException(delegateExecution);		
	}
	
	@Test
	public void processCallBack_Final_Test() throws MapperException, BadResponseException, IOException{
		final String sdncResponse = new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_ASYNC_Request.json")));	
		delegateExecution.setVariable("correlationName_MESSAGE", sdncResponse);   		
		sndcRequestTasks.processCallback(delegateExecution);		
		assertEquals(true,delegateExecution.getVariable(IS_CALLBACK_COMPLETED));
	}
	
	public SDNCRequest createSDNCRequest(){
		SDNCRequest request = new SDNCRequest();
		request.setCorrelationName("correlationName");
		request.setCorrelationValue("correlationValue");		
		request.setTopology(SDNCTopology.CONFIGURATION);
		ObjectMapper mapper = new ObjectMapper();
		try {
			GenericResourceApiServiceOperationInformation sdncReq = 
					mapper.readValue(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNC_Client_Request.json")), GenericResourceApiServiceOperationInformation.class);
			request.setSDNCPayload(sdncReq);
		} catch (JsonParseException e) {			
			
		} catch (JsonMappingException e) {
			
		} catch (IOException e) {
			
		}
		
		return request;
	}
	
}