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.eq;
32 import static org.mockito.Matchers.anyString;
33 import static org.mockito.Mockito.doReturn;
34 import static org.mockito.Mockito.when;
35 import static org.mockito.Mockito.mock;
38 import java.util.HashMap;
40 import java.util.stream.Stream;
41 import javax.ws.rs.HttpMethod;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.mockito.Spy;
47 import org.onap.appc.flow.controller.data.Transaction;
49 import javax.ws.rs.client.Client;
50 import javax.ws.rs.client.WebTarget;
51 import javax.ws.rs.client.Invocation;
52 import javax.ws.rs.core.Response;
53 import javax.ws.rs.client.Entity;
54 import javax.ws.rs.core.MediaType;
56 public class RestExecutorTest {
58 private static final String ANY = "notNullString";
59 private static final String REST_RESPONSE = "restResponse";
61 private Transaction transaction;
62 private Map<String, String> outputMessage = new HashMap<>();
65 private RestExecutor restExecutor = new RestExecutor();
67 private Client client;
69 private WebTarget webResource;
71 private Invocation.Builder webResourceBuilder;
73 final private Response clientResponse = mock(Response.class);
78 transaction = new Transaction();
79 transaction.setuId(ANY);
80 transaction.setPswd(ANY);
81 transaction.setExecutionEndPoint(ANY);
82 transaction.setPayload(ANY);
84 MockitoAnnotations.initMocks(this);
85 doReturn(client).when(restExecutor).createClient(any());
86 when(client.target(any(URI.class))).thenReturn(webResource);
88 when(webResource.request(eq("Content-Type"),anyString())).thenReturn(webResourceBuilder);
89 when(webResource.request(anyString())).thenReturn(webResourceBuilder);
91 when(webResourceBuilder.get(eq(Response.class))).thenReturn(clientResponse);
92 when(webResourceBuilder.post(any(Entity.class),eq(Response.class))).thenReturn(clientResponse);
93 when(webResourceBuilder.put(any(Entity.class),eq(Response.class))).thenReturn(clientResponse);
94 when(webResource.request(anyString()).delete(eq(Response.class))).thenReturn(clientResponse);
95 when(webResourceBuilder.delete(eq(Response.class))).thenReturn(clientResponse);
97 when(clientResponse.getStatus()).thenReturn(OK.getStatusCode());
98 when(clientResponse.readEntity(String.class)).thenReturn(OK.getReasonPhrase());
102 public void checkClientResponse_whenHTTPMethodIsGET() throws Exception {
104 transaction.setExecutionRPC(HttpMethod.GET);
106 outputMessage = restExecutor.execute(transaction, null);
112 public void checkClientResponse_whenHTTPMethodIsPOST() throws Exception {
114 transaction.setExecutionRPC(HttpMethod.POST);
116 outputMessage = restExecutor.execute(transaction, null);
122 public void checkClientResponse_whenHTTPMethodIsPUT() throws Exception {
124 transaction.setExecutionRPC(HttpMethod.PUT);
126 outputMessage = restExecutor.execute(transaction, null);
132 public void checkClientResponse_whenHTTPMethodIsDELETE() throws Exception {
134 transaction.setExecutionRPC(HttpMethod.DELETE);
136 outputMessage = restExecutor.execute(transaction, null);
141 @Test(expected=Exception.class)
142 public void checkClienResponse_whenStatusNOK() throws Exception {
144 when(clientResponse.getStatus()).thenReturn(FORBIDDEN.getStatusCode());
145 when(clientResponse.readEntity(String.class)).thenReturn(FORBIDDEN.getReasonPhrase());
146 transaction.setExecutionRPC(HttpMethod.GET);
148 outputMessage = restExecutor.execute(transaction, null);
150 } catch(Exception e) {
151 assertResponseNOK(e);
156 @Test(expected=Exception.class)
157 public void checkIfExceptionIsThrown_whenHTTPMethodIsNotSupported() throws Exception {
159 transaction.setExecutionRPC(HttpMethod.HEAD);
161 outputMessage = restExecutor.execute(transaction, null);
164 assertNotSupportedHTTPMethod();
168 private void assertResponseOK() {
169 assertFalse("Output Message is empty", outputMessage.isEmpty());
170 assertTrue("Output Message does not contain " + REST_RESPONSE, outputMessage.containsKey(REST_RESPONSE));
171 assertTrue("restResponse is not " + OK.getReasonPhrase(),
172 (OK.getReasonPhrase()).equals(outputMessage.get(REST_RESPONSE)));
173 assertTrue("HTTP_Response in NOK", transaction.getResponses().stream()
174 .anyMatch(response -> Integer.toString(OK.getStatusCode()).equals(response.getResponseCode())));
177 private void assertResponseNOK(Exception e) {
178 assertTrue("Output Message is not empty as it should", outputMessage.isEmpty());
179 assertTrue("Expected HTTP error code: " + FORBIDDEN.getStatusCode() + " is not present",
180 e.getCause().getMessage().contains(Integer.toString(FORBIDDEN.getStatusCode())));
183 private void assertNotSupportedHTTPMethod() {
184 assertTrue("Output Message is not empty as it should", outputMessage.isEmpty());
185 assertTrue("HTTP Method: " + transaction.getExecutionRPC() + " is supported but was not handled",
186 Stream.of(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE)
187 .noneMatch(httpMethod -> httpMethod.equals(transaction.getExecutionRPC())));