2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2018 IBM.
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.so.apihandler.common;
27 import static org.junit.Assert.assertEquals;
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.doThrow;
31 import static org.mockito.Mockito.when;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
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.so.apihandlerinfra.exceptions.ApiException;
45 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
46 import org.onap.so.apihandlerinfra.exceptions.ClientConnectionException;
47 import org.skyscreamer.jsonassert.JSONAssert;
48 import org.springframework.core.env.Environment;
49 import org.springframework.http.HttpEntity;
50 import org.springframework.http.HttpMethod;
51 import org.springframework.http.HttpStatus;
52 import org.springframework.web.client.HttpClientErrorException;
53 import org.springframework.web.client.HttpServerErrorException;
54 import org.springframework.web.client.ResourceAccessException;
55 import org.springframework.web.client.RestTemplate;
57 @RunWith(MockitoJUnitRunner.class)
58 public class CamundaClientTest {
61 private RestTemplate restTemplate;
64 private Environment env;
67 private ResponseHandler responseHandler;
71 private CamundaClient client;
74 public ExpectedException thrown = ExpectedException.none();
78 when(env.getRequiredProperty("mso.camundaAuth"))
79 .thenReturn("015E7ACF706C6BBF85F2079378BDD2896E226E09D13DC2784BA309E27D59AB9FAD3A5E039DF0BB8408");
80 when(env.getRequiredProperty("mso.msoKey")).thenReturn("07a7159d3bf51a0e53be7a8f89699be7");
81 when(env.getRequiredProperty("mso.camundaURL")).thenReturn("http://localhost:8080");
84 public String inputStream(String JsonInput) throws IOException {
85 JsonInput = "src/test/resources/CamundaClientTest" + JsonInput;
86 String input = new String(Files.readAllBytes(Paths.get(JsonInput)));
91 public void createBPMNFailureExceptionNoResponseBodyTest() {
92 HttpServerErrorException e = new HttpServerErrorException(HttpStatus.NOT_FOUND);
93 BPMNFailureException ex = client.createBPMNFailureException(e);
94 assertEquals(HttpStatus.NOT_IMPLEMENTED.value(), ex.getHttpResponseCode());
95 assertEquals("Request Failed due to BPEL error with HTTP Status = 404 NOT_FOUND", ex.getMessage());
99 public void createBPMNFailureExceptionWithCamundaResponseTest() throws IOException {
100 HttpClientErrorException e = new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR, null,
101 inputStream("/CamundaFailure.json").getBytes(), null);
102 BPMNFailureException ex = client.createBPMNFailureException(e);
103 assertEquals(HttpStatus.BAD_GATEWAY.value(), ex.getHttpResponseCode());
105 "Request Failed due to BPEL error with HTTP Status = 500 INTERNAL_SERVER_ERROR <aetgt:WorkflowException xmlns:aetgt=\"http://org.onap/so/workflow/schema/v1\"><aetgt:ErrorMessage>Exception in create execution list 500 </aetgt:ErrorMessage><aetgt:ErrorCode>7000</aetgt:ErrorCode></aetgt:WorkflowException>",
110 public void createBPMNFailureExceptionTest() throws IOException {
111 String response = "Request failed";
112 HttpClientErrorException e =
113 new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR, null, response.getBytes(), null);
114 BPMNFailureException ex = client.createBPMNFailureException(e);
115 assertEquals(HttpStatus.BAD_GATEWAY.value(), ex.getHttpResponseCode());
116 assertEquals("Request Failed due to BPEL error with HTTP Status = 500 INTERNAL_SERVER_ERROR Request failed",
121 public void wrapVIDRequestTest() throws IOException {
122 String requestId = "f7ce78bb-423b-11e7-93f8-0050569a796";
123 boolean isBaseVfModule = true;
124 int recipeTimeout = 10000;
125 String requestAction = "createInstance";
126 String serviceInstanceId = "12345679";
127 String pnfCorrelationId = "12345679";
128 String vnfId = "234567891";
129 String vfModuleId = "345678912";
130 String volumeGroupId = "456789123";
131 String networkId = "567891234";
132 String configurationId = "678912345";
133 String serviceType = "testService";
134 String vnfType = "testVnf";
135 String vfModuleType = "vfModuleType";
136 String networkType = "networkType";
137 String requestDetails = "{requestDetails: }";
138 String apiVersion = "6";
139 boolean aLaCarte = true;
140 String requestUri = "v7/serviceInstances/assign";
141 String instanceGroupId = "ff305d54-75b4-431b-adb2-eb6b9e5ff000";
143 String testResult = client.wrapVIDRequest(requestId, isBaseVfModule, recipeTimeout, requestAction,
144 serviceInstanceId, pnfCorrelationId, vnfId, vfModuleId, volumeGroupId, networkId, configurationId,
145 serviceType, vnfType, vfModuleType, networkType, requestDetails, apiVersion, aLaCarte, requestUri, "",
146 instanceGroupId, false);
147 String expected = inputStream("/WrappedVIDRequest.json");
149 JSONAssert.assertEquals(expected, testResult, false);
153 public void getClientConnectionExceptionTest() throws ApiException {
154 doThrow(ResourceAccessException.class).when(restTemplate).exchange(eq("http://localhost:8080/path"),
155 eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class));
156 thrown.expect(ClientConnectionException.class);
157 thrown.expectMessage("Client from http://localhost:8080/path failed to connect or respond");
162 public void postClientConnectionExceptionTest() throws ApiException, IOException {
163 String jsonReq = inputStream("/WrappedVIDRequest.json");
164 doThrow(ResourceAccessException.class).when(restTemplate).postForEntity(eq("http://localhost:8080/path"),
165 any(HttpEntity.class), eq(String.class));
166 thrown.expect(ClientConnectionException.class);
167 thrown.expectMessage("Client from http://localhost:8080/path failed to connect or respond");
168 client.post(jsonReq, "/path");
172 public void getHttpStatusCodeExceptionTest() throws ApiException {
173 HttpServerErrorException e = new HttpServerErrorException(HttpStatus.NOT_FOUND);
174 doThrow(e).when(restTemplate).exchange(eq("http://localhost:8080/path"), eq(HttpMethod.GET),
175 any(HttpEntity.class), eq(String.class));
176 thrown.expect(BPMNFailureException.class);
177 thrown.expectMessage("Request Failed due to BPEL error with HTTP Status = 404 NOT_FOUND");
182 public void postHttpStatusCodeExceptionTest() throws ApiException, IOException {
183 HttpServerErrorException e = new HttpServerErrorException(HttpStatus.NOT_FOUND);
184 String jsonReq = inputStream("/WrappedVIDRequest.json");
185 doThrow(e).when(restTemplate).postForEntity(eq("http://localhost:8080/path"), any(HttpEntity.class),
187 thrown.expect(BPMNFailureException.class);
188 thrown.expectMessage("Request Failed due to BPEL error with HTTP Status = 404 NOT_FOUND");
189 client.post(jsonReq, "/path");