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