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 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
17 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
25 * ============LICENSE_END=========================================================
28 package org.onap.appc.adapter.rest.impl;
30 import static org.junit.Assert.assertEquals;
32 import java.io.IOException;
33 import java.util.HashMap;
36 import org.apache.http.client.methods.HttpDelete;
37 import org.apache.http.client.methods.HttpGet;
38 import org.apache.http.client.methods.HttpPost;
39 import org.apache.http.client.methods.HttpPut;
40 import org.apache.http.client.methods.HttpRequestBase;
41 import org.apache.http.util.EntityUtils;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
46 import org.onap.appc.exceptions.APPCException;
47 import com.att.cdp.exceptions.ZoneException;
48 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
52 * Test the ProviderAdapter implementation.
54 public class TestRestAdapterImpl {
55 private RestAdapterImpl adapter;
58 @SuppressWarnings("nls")
60 public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
65 public void setup() throws IllegalArgumentException, IllegalAccessException {
67 adapter = new RestAdapterImpl();
71 public void testCreateHttpRequestGet() throws IOException, IllegalStateException, IllegalArgumentException,
72 ZoneException, APPCException {
74 Map<String, String> params = new HashMap<>();
75 params.put("org.onap.appc.instance.URI", "http://example.com:8080/about/health");
76 params.put("org.onap.appc.instance.haveHeader","false");
78 HttpGet httpGet = ((HttpGet) givenParams(params, "GET"));
80 assertEquals("GET", httpGet.getMethod());
81 assertEquals("http://example.com:8080/about/health", httpGet.getURI().toURL().toString());
85 public void testCreateRequestNoParamGet() throws IOException, IllegalStateException, IllegalArgumentException,
86 ZoneException, APPCException {
88 SvcLogicContext ctx = new SvcLogicContext();
89 Map<String, String> params = new HashMap<>();
91 adapter.commonGet(params, ctx);
93 assertEquals("failure", ctx.getStatus());
94 assertEquals("500", ctx.getAttribute("org.openecomp.rest.result.code"));
95 assertEquals("java.lang.IllegalArgumentException: HTTP request may not be null",
96 ctx.getAttribute("org.openecomp.rest.result.message"));
100 public void testCreateRequestInvalidParamGet() throws IOException, IllegalStateException, IllegalArgumentException,
101 ZoneException, APPCException {
103 SvcLogicContext ctx = new SvcLogicContext();
104 Map<String, String> params = new HashMap<>();
105 params.put("org.onap.appc.instance.URI", "boo");
106 params.put("org.onap.appc.instance.haveHeader","false");
107 params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}");
109 adapter.commonGet(params, ctx);
111 assertEquals("failure", ctx.getStatus());
112 assertEquals("500", ctx.getAttribute("org.openecomp.rest.result.code"));
113 assertEquals("org.apache.http.client.ClientProtocolException",
114 ctx.getAttribute("org.openecomp.rest.result.message"));
118 public void testCreateHttpRequestPost() throws IOException, IllegalStateException, IllegalArgumentException,
119 ZoneException, APPCException {
121 Map<String, String> params = new HashMap<>();
122 params.put("org.onap.appc.instance.URI", "http://example.com:8081/posttest");
123 params.put("org.onap.appc.instance.haveHeader","false");
124 params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode\", \"width\":200, \"height\":100}");
126 HttpPost httpPost = ((HttpPost) givenParams(params, "POST"));
128 assertEquals("POST", httpPost.getMethod());
129 assertEquals("http://example.com:8081/posttest", httpPost.getURI().toURL().toString());
130 assertEquals("{\"name\":\"MyNode\", \"width\":200, \"height\":100}", EntityUtils.toString(httpPost.getEntity()));
134 public void testCreateHttpRequestPut() throws IOException, IllegalStateException, IllegalArgumentException,
135 ZoneException, APPCException {
137 Map<String, String> params = new HashMap<>();
138 params.put("org.onap.appc.instance.URI", "http://example.com:8081/puttest");
139 params.put("org.onap.appc.instance.haveHeader","false");
140 params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}");
142 HttpPut httpPut = ((HttpPut) givenParams(params, "PUT"));
143 //Header headers[] = httpPut.getAllHeaders();
145 assertEquals("PUT", httpPut.getMethod());
146 assertEquals("http://example.com:8081/puttest", httpPut.getURI().toURL().toString());
147 assertEquals("{\"name\":\"MyNode2\", \"width\":300, \"height\":300}", EntityUtils.toString(httpPut.getEntity()));
151 public void testCreateRequestNoParamPut() throws IOException, IllegalStateException, IllegalArgumentException,
152 ZoneException, APPCException {
154 SvcLogicContext ctx = new SvcLogicContext();
155 Map<String, String> params = new HashMap<>();
157 adapter.commonPut(params, ctx);
159 assertEquals("failure", ctx.getStatus());
160 assertEquals("500", ctx.getAttribute("org.openecomp.rest.result.code"));
161 assertEquals("java.lang.IllegalArgumentException: HTTP request may not be null",
162 ctx.getAttribute("org.openecomp.rest.result.message"));
166 public void testCreateRequestInvalidParamPut() throws IOException, IllegalStateException, IllegalArgumentException,
167 ZoneException, APPCException {
169 SvcLogicContext ctx = new SvcLogicContext();
170 Map<String, String> params = new HashMap<>();
171 params.put("org.onap.appc.instance.URI", "boo");
172 params.put("org.onap.appc.instance.haveHeader","false");
173 params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}");
175 adapter.commonPut(params, ctx);
177 assertEquals("failure", ctx.getStatus());
178 assertEquals("500", ctx.getAttribute("org.openecomp.rest.result.code"));
179 assertEquals("org.apache.http.client.ClientProtocolException",
180 ctx.getAttribute("org.openecomp.rest.result.message"));
184 public void testCreateHttpRequestDelete() throws IOException, IllegalStateException, IllegalArgumentException,
185 ZoneException, APPCException {
187 Map<String, String> params = new HashMap<>();
188 params.put("org.onap.appc.instance.URI", "http://example.com:8081/deletetest");
189 params.put("org.onap.appc.instance.haveHeader","false");
191 HttpDelete httpDelete = ((HttpDelete) givenParams(params, "DELETE"));
193 assertEquals("DELETE", httpDelete.getMethod());
194 assertEquals("http://example.com:8081/deletetest", httpDelete.getURI().toURL().toString());
198 public void testCreateRequestNoParamDelete() throws IOException, IllegalStateException, IllegalArgumentException,
199 ZoneException, APPCException {
201 SvcLogicContext ctx = new SvcLogicContext();
202 Map<String, String> params = new HashMap<>();
204 adapter.commonDelete(params, ctx);
206 assertEquals("failure", ctx.getStatus());
207 assertEquals("500", ctx.getAttribute("org.openecomp.rest.result.code"));
208 assertEquals("java.lang.IllegalArgumentException: HTTP request may not be null",
209 ctx.getAttribute("org.openecomp.rest.result.message"));
213 public void testCreateRequestInvalidParamDelete() throws IOException, IllegalStateException, IllegalArgumentException,
214 ZoneException, APPCException {
216 SvcLogicContext ctx = new SvcLogicContext();
217 Map<String, String> params = new HashMap<>();
218 params.put("org.onap.appc.instance.URI", "boo");
219 params.put("org.onap.appc.instance.haveHeader","false");
220 params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}");
222 adapter.commonDelete(params, ctx);
224 assertEquals("failure", ctx.getStatus());
225 assertEquals("500", ctx.getAttribute("org.openecomp.rest.result.code"));
226 assertEquals("org.apache.http.client.ClientProtocolException",
227 ctx.getAttribute("org.openecomp.rest.result.message"));
230 private HttpRequestBase givenParams(Map<String, String> params, String method){
231 SvcLogicContext ctx = new SvcLogicContext();
232 RequestContext rc = new RequestContext(ctx);
235 adapter = new RestAdapterImpl();
236 return adapter.createHttpRequest(method, params, rc);