2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.flow.controller.executorImpl;
27 import static javax.ws.rs.core.Response.Status.FORBIDDEN;
28 import static javax.ws.rs.core.Response.Status.OK;
29 import static org.junit.Assert.assertFalse;
30 import static org.junit.Assert.assertTrue;
31 import static org.mockito.Matchers.any;
32 import static org.mockito.Matchers.anyString;
33 import static org.mockito.Mockito.doReturn;
34 import static org.mockito.Mockito.when;
36 import com.sun.jersey.api.client.Client;
37 import com.sun.jersey.api.client.ClientResponse;
38 import com.sun.jersey.api.client.WebResource;
40 import java.util.HashMap;
42 import java.util.stream.Stream;
43 import javax.ws.rs.HttpMethod;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.mockito.Spy;
49 import org.onap.appc.flow.controller.data.Transaction;
51 public class RestExecutorTest {
53 private static final String ANY = "notNullString";
54 private static final String REST_RESPONSE = "restResponse";
56 private Transaction transaction;
57 private Map<String, String> outputMessage = new HashMap<>();
60 private RestExecutor restExecutor = new RestExecutor();
62 private Client client;
64 private WebResource webResource;
66 private WebResource.Builder webResourceBuilder;
68 private ClientResponse clientResponse;
73 transaction = new Transaction();
74 transaction.setuId(ANY);
75 transaction.setPswd(ANY);
76 transaction.setExecutionEndPoint(ANY);
77 transaction.setPayload(ANY);
79 MockitoAnnotations.initMocks(this);
80 doReturn(client).when(restExecutor).createClient(any());
81 when(client.resource(any(URI.class))).thenReturn(webResource);
83 when(webResource.accept(anyString())).thenReturn(webResourceBuilder);
84 when(webResource.type(anyString())).thenReturn(webResourceBuilder);
86 when(webResourceBuilder.get(ClientResponse.class)).thenReturn(clientResponse);
87 when(webResourceBuilder.post(ClientResponse.class, ANY)).thenReturn(clientResponse);
88 when(webResourceBuilder.put(ClientResponse.class, ANY)).thenReturn(clientResponse);
89 when(webResource.delete(ClientResponse.class)).thenReturn(clientResponse);
91 when(clientResponse.getStatus()).thenReturn(OK.getStatusCode());
92 when(clientResponse.getEntity(String.class)).thenReturn(OK.getReasonPhrase());
96 public void checkClientResponse_whenHTTPMethodIsGET() throws Exception {
98 transaction.setExecutionRPC(HttpMethod.GET);
100 outputMessage = restExecutor.execute(transaction, null);
106 public void checkClientResponse_whenHTTPMethodIsPOST() throws Exception {
108 transaction.setExecutionRPC(HttpMethod.POST);
110 outputMessage = restExecutor.execute(transaction, null);
116 public void checkClientResponse_whenHTTPMethodIsPUT() throws Exception {
118 transaction.setExecutionRPC(HttpMethod.PUT);
120 outputMessage = restExecutor.execute(transaction, null);
126 public void checkClientResponse_whenHTTPMethodIsDELETE() throws Exception {
128 transaction.setExecutionRPC(HttpMethod.DELETE);
130 outputMessage = restExecutor.execute(transaction, null);
135 @Test(expected=Exception.class)
136 public void checkClienResponse_whenStatusNOK() throws Exception {
138 when(clientResponse.getStatus()).thenReturn(FORBIDDEN.getStatusCode());
139 when(clientResponse.getEntity(String.class)).thenReturn(FORBIDDEN.getReasonPhrase());
140 transaction.setExecutionRPC(HttpMethod.GET);
142 outputMessage = restExecutor.execute(transaction, null);
144 } catch(Exception e) {
145 assertResponseNOK(e);
150 @Test(expected=Exception.class)
151 public void checkIfExceptionIsThrown_whenHTTPMethodIsNotSupported() throws Exception {
153 transaction.setExecutionRPC(HttpMethod.HEAD);
155 outputMessage = restExecutor.execute(transaction, null);
158 assertNotSupportedHTTPMethod();
162 private void assertResponseOK() {
163 assertFalse("Output Message is empty", outputMessage.isEmpty());
164 assertTrue("Output Message does not contain " + REST_RESPONSE, outputMessage.containsKey(REST_RESPONSE));
165 assertTrue("restResponse is not " + OK.getReasonPhrase(),
166 (OK.getReasonPhrase()).equals(outputMessage.get(REST_RESPONSE)));
167 assertTrue("HTTP_Response in NOK", transaction.getResponses().stream()
168 .anyMatch(response -> Integer.toString(OK.getStatusCode()).equals(response.getResponseCode())));
171 private void assertResponseNOK(Exception e) {
172 assertTrue("Output Message is not empty as it should", outputMessage.isEmpty());
173 assertTrue("Expected HTTP error code: " + FORBIDDEN.getStatusCode() + " is not present",
174 e.getCause().getMessage().contains(Integer.toString(FORBIDDEN.getStatusCode())));
177 private void assertNotSupportedHTTPMethod() {
178 assertTrue("Output Message is not empty as it should", outputMessage.isEmpty());
179 assertTrue("HTTP Method: " + transaction.getExecutionRPC() + " is supported but was not handled",
180 Stream.of(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE)
181 .noneMatch(httpMethod -> httpMethod.equals(transaction.getExecutionRPC())));