2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.flow.controller.executorImpl;
26 import static javax.ws.rs.core.Response.Status.FORBIDDEN;
27 import static javax.ws.rs.core.Response.Status.OK;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.anyString;
32 import static org.mockito.Mockito.doReturn;
33 import static org.mockito.Mockito.when;
35 import com.sun.jersey.api.client.Client;
36 import com.sun.jersey.api.client.ClientResponse;
37 import com.sun.jersey.api.client.WebResource;
39 import java.util.HashMap;
41 import java.util.stream.Stream;
42 import javax.ws.rs.HttpMethod;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.mockito.Spy;
48 import org.onap.appc.flow.controller.data.Transaction;
50 public class RestExecutorTest {
52 private static final String ANY = "notNullString";
53 private static final String REST_RESPONSE = "restResponse";
55 private Transaction transaction;
56 private Map<String, String> outputMessage = new HashMap<>();
59 private RestExecutor restExecutor = new RestExecutor();
61 private Client client;
63 private WebResource webResource;
65 private WebResource.Builder webResourceBuilder;
67 private ClientResponse clientResponse;
72 transaction = new Transaction();
73 transaction.setuId(ANY);
74 transaction.setPswd(ANY);
75 transaction.setExecutionEndPoint(ANY);
76 transaction.setPayload(ANY);
78 MockitoAnnotations.initMocks(this);
79 doReturn(client).when(restExecutor).createClient(any());
80 when(client.resource(any(URI.class))).thenReturn(webResource);
82 when(webResource.accept(anyString())).thenReturn(webResourceBuilder);
83 when(webResource.type(anyString())).thenReturn(webResourceBuilder);
85 when(webResourceBuilder.get(ClientResponse.class)).thenReturn(clientResponse);
86 when(webResourceBuilder.post(ClientResponse.class, ANY)).thenReturn(clientResponse);
87 when(webResourceBuilder.put(ClientResponse.class, ANY)).thenReturn(clientResponse);
88 when(webResource.delete(ClientResponse.class)).thenReturn(clientResponse);
90 when(clientResponse.getStatus()).thenReturn(OK.getStatusCode());
91 when(clientResponse.getEntity(String.class)).thenReturn(OK.getReasonPhrase());
95 public void checkClientResponse_whenHTTPMethodIsGET() throws Exception {
97 transaction.setExecutionRPC(HttpMethod.GET);
99 outputMessage = restExecutor.execute(transaction, null);
105 public void checkClientResponse_whenHTTPMethodIsPOST() throws Exception {
107 transaction.setExecutionRPC(HttpMethod.POST);
109 outputMessage = restExecutor.execute(transaction, null);
115 public void checkClientResponse_whenHTTPMethodIsPUT() throws Exception {
117 transaction.setExecutionRPC(HttpMethod.PUT);
119 outputMessage = restExecutor.execute(transaction, null);
125 public void checkClientResponse_whenHTTPMethodIsDELETE() throws Exception {
127 transaction.setExecutionRPC(HttpMethod.DELETE);
129 outputMessage = restExecutor.execute(transaction, null);
134 @Test(expected=Exception.class)
135 public void checkClienResponse_whenStatusNOK() throws Exception {
137 when(clientResponse.getStatus()).thenReturn(FORBIDDEN.getStatusCode());
138 when(clientResponse.getEntity(String.class)).thenReturn(FORBIDDEN.getReasonPhrase());
139 transaction.setExecutionRPC(HttpMethod.GET);
141 outputMessage = restExecutor.execute(transaction, null);
143 } catch(Exception e) {
144 assertResponseNOK(e);
149 @Test(expected=Exception.class)
150 public void checkIfExceptionIsThrown_whenHTTPMethodIsNotSupported() throws Exception {
152 transaction.setExecutionRPC(HttpMethod.HEAD);
154 outputMessage = restExecutor.execute(transaction, null);
157 assertNotSupportedHTTPMethod();
161 private void assertResponseOK() {
162 assertFalse("Output Message is empty", outputMessage.isEmpty());
163 assertTrue("Output Message does not contain " + REST_RESPONSE, outputMessage.containsKey(REST_RESPONSE));
164 assertTrue("restResponse is not " + OK.getReasonPhrase(),
165 (OK.getReasonPhrase()).equals(outputMessage.get(REST_RESPONSE)));
166 assertTrue("HTTP_Response in NOK", transaction.getResponses().stream()
167 .anyMatch(response -> Integer.toString(OK.getStatusCode()).equals(response.getResponseCode())));
170 private void assertResponseNOK(Exception e) {
171 assertTrue("Output Message is not empty as it should", outputMessage.isEmpty());
172 assertTrue("Expected HTTP error code: " + FORBIDDEN.getStatusCode() + " is not present",
173 e.getCause().getMessage().contains(Integer.toString(FORBIDDEN.getStatusCode())));
176 private void assertNotSupportedHTTPMethod() {
177 assertTrue("Output Message is not empty as it should", outputMessage.isEmpty());
178 assertTrue("HTTP Method: " + transaction.getExecutionRPC() + " is supported but was not handled",
179 Stream.of(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE)
180 .noneMatch(httpMethod -> httpMethod.equals(transaction.getExecutionRPC())));