Updated jersey from com.sun.jersey to org.glassfish.jersey
[appc.git] / appc-config / appc-flow-controller / provider / src / test / java / org / onap / appc / flow / controller / executorImpl / RestExecutorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.flow.controller.executorImpl;
25
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;
36
37 import java.net.URI;
38 import java.util.HashMap;
39 import java.util.Map;
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;
48
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;
55
56 public class RestExecutorTest {
57
58     private static final String ANY = "notNullString";
59     private static final String REST_RESPONSE = "restResponse";
60
61     private Transaction transaction;
62     private Map<String, String> outputMessage = new HashMap<>();
63
64     @Spy
65     private RestExecutor restExecutor = new RestExecutor();
66     @Mock
67     private Client client;
68     @Mock
69     private WebTarget webResource;
70     @Mock
71     private Invocation.Builder webResourceBuilder;
72     @Mock
73     final private Response clientResponse = mock(Response.class);
74
75
76     @Before
77     public void setUp() {
78         transaction = new Transaction();
79         transaction.setuId(ANY);
80         transaction.setPswd(ANY);
81         transaction.setExecutionEndPoint(ANY);
82         transaction.setPayload(ANY);
83
84         MockitoAnnotations.initMocks(this);
85         doReturn(client).when(restExecutor).createClient(any());
86         when(client.target(any(URI.class))).thenReturn(webResource);
87
88         when(webResource.request(eq("Content-Type"),anyString())).thenReturn(webResourceBuilder);
89         when(webResource.request(anyString())).thenReturn(webResourceBuilder);
90
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);
96
97         when(clientResponse.getStatus()).thenReturn(OK.getStatusCode());
98         when(clientResponse.readEntity(String.class)).thenReturn(OK.getReasonPhrase());
99     }
100
101     @Test
102     public void checkClientResponse_whenHTTPMethodIsGET() throws Exception {
103
104         transaction.setExecutionRPC(HttpMethod.GET);
105
106         outputMessage = restExecutor.execute(transaction, null);
107
108         assertResponseOK();
109     }
110
111     @Test
112     public void checkClientResponse_whenHTTPMethodIsPOST() throws Exception {
113
114         transaction.setExecutionRPC(HttpMethod.POST);
115
116         outputMessage = restExecutor.execute(transaction, null);
117
118         assertResponseOK();
119     }
120
121     @Test
122     public void checkClientResponse_whenHTTPMethodIsPUT() throws Exception {
123
124         transaction.setExecutionRPC(HttpMethod.PUT);
125
126         outputMessage = restExecutor.execute(transaction, null);
127
128         assertResponseOK();
129     }
130
131     @Test
132     public void checkClientResponse_whenHTTPMethodIsDELETE() throws Exception {
133
134         transaction.setExecutionRPC(HttpMethod.DELETE);
135
136         outputMessage = restExecutor.execute(transaction, null);
137
138         assertResponseOK();
139     }
140
141     @Test(expected=Exception.class)
142     public void checkClienResponse_whenStatusNOK() throws Exception {
143         try {
144             when(clientResponse.getStatus()).thenReturn(FORBIDDEN.getStatusCode());
145             when(clientResponse.readEntity(String.class)).thenReturn(FORBIDDEN.getReasonPhrase());
146              transaction.setExecutionRPC(HttpMethod.GET);
147
148             outputMessage = restExecutor.execute(transaction, null);
149
150         } catch(Exception e) {
151             assertResponseNOK(e);
152             throw e;
153         }
154     }
155
156     @Test(expected=Exception.class)
157     public void checkIfExceptionIsThrown_whenHTTPMethodIsNotSupported() throws Exception {
158         try {
159             transaction.setExecutionRPC(HttpMethod.HEAD);
160
161             outputMessage = restExecutor.execute(transaction, null);
162
163         } finally {
164             assertNotSupportedHTTPMethod();
165         }
166     }
167
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())));
175     }
176
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())));
181     }
182
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())));
188     }
189 }