Sonar fixes in RestServiceNode
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.flow.controller.executorImpl;
26
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;
35
36 import com.sun.jersey.api.client.Client;
37 import com.sun.jersey.api.client.ClientResponse;
38 import com.sun.jersey.api.client.WebResource;
39 import java.net.URI;
40 import java.util.HashMap;
41 import java.util.Map;
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;
50
51 public class RestExecutorTest {
52
53     private static final String ANY = "notNullString";
54     private static final String REST_RESPONSE = "restResponse";
55
56     private Transaction transaction;
57     private Map<String, String> outputMessage = new HashMap<>();
58
59     @Spy
60     private RestExecutor restExecutor = new RestExecutor();
61     @Mock
62     private Client client;
63     @Mock
64     private WebResource webResource;
65     @Mock
66     private WebResource.Builder webResourceBuilder;
67     @Mock
68     private ClientResponse clientResponse;
69
70
71     @Before
72     public void setUp() {
73         transaction = new Transaction();
74         transaction.setuId(ANY);
75         transaction.setPswd(ANY);
76         transaction.setExecutionEndPoint(ANY);
77         transaction.setPayload(ANY);
78
79         MockitoAnnotations.initMocks(this);
80         doReturn(client).when(restExecutor).createClient(any());
81         when(client.resource(any(URI.class))).thenReturn(webResource);
82
83         when(webResource.accept(anyString())).thenReturn(webResourceBuilder);
84         when(webResource.type(anyString())).thenReturn(webResourceBuilder);
85
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);
90
91         when(clientResponse.getStatus()).thenReturn(OK.getStatusCode());
92         when(clientResponse.getEntity(String.class)).thenReturn(OK.getReasonPhrase());
93     }
94
95     @Test
96     public void checkClientResponse_whenHTTPMethodIsGET() throws Exception {
97
98         transaction.setExecutionRPC(HttpMethod.GET);
99
100         outputMessage = restExecutor.execute(transaction, null);
101
102         assertResponseOK();
103     }
104
105     @Test
106     public void checkClientResponse_whenHTTPMethodIsPOST() throws Exception {
107
108         transaction.setExecutionRPC(HttpMethod.POST);
109
110         outputMessage = restExecutor.execute(transaction, null);
111
112         assertResponseOK();
113     }
114
115     @Test
116     public void checkClientResponse_whenHTTPMethodIsPUT() throws Exception {
117
118         transaction.setExecutionRPC(HttpMethod.PUT);
119
120         outputMessage = restExecutor.execute(transaction, null);
121
122         assertResponseOK();
123     }
124
125     @Test
126     public void checkClientResponse_whenHTTPMethodIsDELETE() throws Exception {
127
128         transaction.setExecutionRPC(HttpMethod.DELETE);
129
130         outputMessage = restExecutor.execute(transaction, null);
131
132         assertResponseOK();
133     }
134
135     @Test(expected=Exception.class)
136     public void checkClienResponse_whenStatusNOK() throws Exception {
137         try {
138             when(clientResponse.getStatus()).thenReturn(FORBIDDEN.getStatusCode());
139             when(clientResponse.getEntity(String.class)).thenReturn(FORBIDDEN.getReasonPhrase());
140             transaction.setExecutionRPC(HttpMethod.GET);
141
142             outputMessage = restExecutor.execute(transaction, null);
143
144         } catch(Exception e) {
145             assertResponseNOK(e);
146             throw e;
147         }
148     }
149
150     @Test(expected=Exception.class)
151     public void checkIfExceptionIsThrown_whenHTTPMethodIsNotSupported() throws Exception {
152         try {
153             transaction.setExecutionRPC(HttpMethod.HEAD);
154
155             outputMessage = restExecutor.execute(transaction, null);
156
157         } finally {
158             assertNotSupportedHTTPMethod();
159         }
160     }
161
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())));
169     }
170
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())));
175     }
176
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())));
182     }
183 }