2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Copyright (C) 2017 Intel Corp.
10 * =============================================================================
11 * Modifications Copyright (C) 2018 Samsung
12 * ================================================================================
13 * Modifications Copyright (C) 2019 Ericsson
14 * ================================================================================
15 * Licensed under the Apache License, Version 2.0 (the "License");
16 * you may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
19 * http://www.apache.org/licenses/LICENSE-2.0
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
27 * ============LICENSE_END=========================================================
30 package org.onap.appc.adapter.rest.impl;
32 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.assertNotNull;
34 import java.io.IOException;
35 import java.net.MalformedURLException;
36 import java.util.HashMap;
38 import org.apache.http.HttpEntity;
39 import org.apache.http.StatusLine;
40 import org.apache.http.client.ClientProtocolException;
41 import org.apache.http.client.methods.CloseableHttpResponse;
42 import org.apache.http.client.methods.HttpDelete;
43 import org.apache.http.client.methods.HttpGet;
44 import org.apache.http.client.methods.HttpPost;
45 import org.apache.http.client.methods.HttpPut;
46 import org.apache.http.client.methods.HttpRequestBase;
47 import org.apache.http.impl.client.CloseableHttpClient;
48 import org.apache.http.impl.client.HttpClients;
49 import org.apache.http.ssl.SSLContexts;
50 import org.apache.http.util.EntityUtils;
51 import org.junit.Before;
52 import org.junit.BeforeClass;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55 import org.mockito.Mockito;
56 import org.onap.appc.exceptions.APPCException;
57 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
58 import org.powermock.api.mockito.PowerMockito;
59 import org.powermock.core.classloader.annotations.PrepareForTest;
60 import org.powermock.modules.junit4.PowerMockRunner;
61 import com.att.cdp.exceptions.ZoneException;
65 * Test the ProviderAdapter implementation.
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest({HttpClients.class, SSLContexts.class})
69 public class TestRestAdapterImpl {
70 private RestAdapterImpl adapter;
71 private CloseableHttpClient client;
72 private CloseableHttpResponse httpResponse;
73 private HttpEntity httpEntity;
74 private StatusLine statusLine;
76 @SuppressWarnings("nls")
78 public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
83 public void setup() throws IllegalArgumentException, IllegalAccessException, ClientProtocolException, IOException {
84 client = Mockito.mock(CloseableHttpClient.class);
85 PowerMockito.mockStatic(HttpClients.class);
86 PowerMockito.when(HttpClients.createDefault()).thenReturn(client);
87 httpResponse = Mockito.mock(CloseableHttpResponse.class);
88 statusLine = Mockito.mock(StatusLine.class);
89 Mockito.when(statusLine.getStatusCode()).thenReturn(200);
90 Mockito.when(httpResponse.getStatusLine()).thenReturn(statusLine);
91 httpEntity = Mockito.mock(HttpEntity.class);
92 Mockito.when(httpResponse.getEntity()).thenReturn(httpEntity);
93 Mockito.when(client.execute(Mockito.any())).thenReturn(httpResponse);
94 adapter = new RestAdapterImpl();
98 public void testCreateHttpRequestGet() throws IOException, IllegalStateException, IllegalArgumentException,
99 ZoneException, APPCException {
101 Map<String, String> params = new HashMap<>();
102 params.put("org.onap.appc.instance.URI", "http://example.com:8080/about/health");
103 params.put("org.onap.appc.instance.haveHeader","false");
105 HttpGet httpGet = ((HttpGet) givenParams(params, "GET"));
107 assertEquals("GET", httpGet.getMethod());
108 assertEquals("http://example.com:8080/about/health", httpGet.getURI().toURL().toString());
112 public void testCreateHttpRequestPost() throws IOException, IllegalStateException, IllegalArgumentException,
113 ZoneException, APPCException {
115 Map<String, String> params = new HashMap<>();
116 params.put("org.onap.appc.instance.URI", "http://example.com:8081/posttest");
117 params.put("org.onap.appc.instance.haveHeader","false");
118 params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode\", \"width\":200, \"height\":100}");
120 HttpPost httpPost = ((HttpPost) givenParams(params, "POST"));
122 assertEquals("POST", httpPost.getMethod());
123 assertEquals("http://example.com:8081/posttest", httpPost.getURI().toURL().toString());
124 assertEquals("{\"name\":\"MyNode\", \"width\":200, \"height\":100}", EntityUtils.toString(httpPost.getEntity()));
128 public void testCreateRequestInvalidParamPost() throws IOException, IllegalStateException, IllegalArgumentException,
129 ZoneException, APPCException {
130 Mockito.when(statusLine.getStatusCode()).thenReturn(500);
131 SvcLogicContext ctx = new SvcLogicContext();
132 Map<String, String> params = new HashMap<>();
133 params.put("org.onap.appc.instance.URI", "boo");
134 params.put("org.onap.appc.instance.haveHeader","false");
135 params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}");
137 adapter.commonPost(params, ctx);
139 assertEquals("failure", ctx.getStatus());
140 assertEquals("500", ctx.getAttribute("org.onap.rest.result.code"));
141 assertEquals("Internal Server Error",
142 ctx.getAttribute("org.onap.rest.result.message"));
146 public void testCreateHttpRequestPut() throws IOException, IllegalStateException, IllegalArgumentException,
147 ZoneException, APPCException {
149 Map<String, String> params = new HashMap<>();
150 params.put("org.onap.appc.instance.URI", "http://example.com:8081/puttest");
151 params.put("org.onap.appc.instance.haveHeader","false");
152 params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}");
154 HttpPut httpPut = ((HttpPut) givenParams(params, "PUT"));
156 assertEquals("PUT", httpPut.getMethod());
157 assertEquals("http://example.com:8081/puttest", httpPut.getURI().toURL().toString());
158 assertEquals("{\"name\":\"MyNode2\", \"width\":300, \"height\":300}", EntityUtils.toString(httpPut.getEntity()));
162 public void testCreateRequestNoParamPut() throws IOException, IllegalStateException, IllegalArgumentException,
163 ZoneException, APPCException {
164 Mockito.when(statusLine.getStatusCode()).thenReturn(200);
165 SvcLogicContext ctx = new SvcLogicContext();
166 Map<String, String> params = new HashMap<>();
168 adapter.commonPut(params, ctx);
170 assertEquals("success", ctx.getStatus());
171 assertEquals("200", ctx.getAttribute("org.onap.rest.result.code"));
172 assertEquals("java.lang.NullPointerException",
173 ctx.getAttribute("org.onap.rest.result.message"));
177 public void testCreateHttpRequestDelete() throws IOException, IllegalStateException, IllegalArgumentException,
178 ZoneException, APPCException {
180 Map<String, String> params = new HashMap<>();
181 params.put("org.onap.appc.instance.URI", "http://example.com:8081/deletetest");
182 params.put("org.onap.appc.instance.haveHeader","false");
184 HttpDelete httpDelete = ((HttpDelete) givenParams(params, "DELETE"));
186 assertEquals("DELETE", httpDelete.getMethod());
187 assertEquals("http://example.com:8081/deletetest", httpDelete.getURI().toURL().toString());
191 public void testCreateRequestNoParamDelete() throws IOException, IllegalStateException, IllegalArgumentException,
192 ZoneException, APPCException {
193 Mockito.when(statusLine.getStatusCode()).thenReturn(400);
194 SvcLogicContext ctx = new SvcLogicContext();
195 Map<String, String> params = new HashMap<>();
197 adapter.commonDelete(params, ctx);
199 assertEquals("failure", ctx.getStatus());
200 assertEquals("400", ctx.getAttribute("org.onap.rest.result.code"));
201 assertEquals("Bad Request",
202 ctx.getAttribute("org.onap.rest.result.message"));
206 public void testDoFailureMultiLineErrorMessage() {
207 Map<String, String> mockParams = Mockito.mock(Map.class);
208 Mockito.when(mockParams.get("org.onap.appc.instance.URI")).thenThrow(new RuntimeException("\n\n"));
209 adapter.createHttpRequest("test_method", mockParams, new RequestContext(new SvcLogicContext()));
210 assertNotNull(mockParams);
214 public void testCreateHttpRequestWithHeader() throws MalformedURLException {
215 Map<String, String> params = new HashMap<>();
216 params.put("org.onap.appc.instance.URI", "http://example.com:8080/about/health");
217 params.put("org.onap.appc.instance.headers", "{\"header1\":\"header1-value\"}");
218 params.put("org.onap.appc.instance.haveHeader","true");
220 HttpGet httpGet = ((HttpGet) givenParams(params, "GET"));
222 assertEquals("GET", httpGet.getMethod());
223 assertNotNull(httpGet.getHeaders("header1"));
227 public void testExecuteHttpRequest() {
228 SvcLogicContext ctx = new SvcLogicContext();
229 Map<String, String> params = new HashMap<>();
230 adapter.commonGet(params, ctx);
231 assertNotNull(params);
235 public void testExecuteRequestException() throws IOException, IllegalStateException, IllegalArgumentException,
236 ZoneException, APPCException {
237 Mockito.when(client.execute(Mockito.any())).thenThrow(new IOException());
238 SvcLogicContext ctx = new SvcLogicContext();
239 Map<String, String> params = new HashMap<>();
241 adapter.commonDelete(params, ctx);
243 assertEquals("failure", ctx.getStatus());
244 assertEquals("500", ctx.getAttribute("org.onap.rest.result.code"));
245 assertEquals("java.io.IOException",
246 ctx.getAttribute("org.onap.rest.result.message"));
249 private HttpRequestBase givenParams(Map<String, String> params, String method){
250 SvcLogicContext ctx = new SvcLogicContext();
251 RequestContext rc = new RequestContext(ctx);
254 adapter = new RestAdapterImpl();
255 return adapter.createHttpRequest(method, params, rc);