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 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.adapter.rest.impl;
28 import static org.junit.Assert.assertEquals;
30 import java.io.IOException;
31 import java.util.HashMap;
34 import org.apache.http.client.methods.HttpDelete;
35 import org.apache.http.client.methods.HttpGet;
36 import org.apache.http.client.methods.HttpPost;
37 import org.apache.http.client.methods.HttpPut;
38 import org.apache.http.client.methods.HttpRequestBase;
39 import org.apache.http.util.EntityUtils;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
44 import org.onap.appc.exceptions.APPCException;
45 import com.att.cdp.exceptions.ZoneException;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
50 * Test the ProviderAdapter implementation.
52 public class TestRestAdapterImpl {
53 private RestAdapterImpl adapter;
56 @SuppressWarnings("nls")
58 public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
63 public void setup() throws IllegalArgumentException, IllegalAccessException {
65 adapter = new RestAdapterImpl();
69 public void testCreateHttpRequestGet() throws IOException, IllegalStateException, IllegalArgumentException,
70 ZoneException, APPCException {
72 Map<String, String> params = new HashMap<>();
73 params.put("org.onap.appc.instance.URI", "http://example.com:8080/about/health");
74 params.put("org.onap.appc.instance.haveHeader","false");
76 HttpGet httpGet = ((HttpGet) givenParams(params, "GET"));
78 assertEquals("GET", httpGet.getMethod());
79 assertEquals("http://example.com:8080/about/health", httpGet.getURI().toURL().toString());
83 public void testCreateHttpRequestPost() throws IOException, IllegalStateException, IllegalArgumentException,
84 ZoneException, APPCException {
86 Map<String, String> params = new HashMap<>();
87 params.put("org.onap.appc.instance.URI", "http://example.com:8081/posttest");
88 params.put("org.onap.appc.instance.haveHeader","false");
89 params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode\", \"width\":200, \"height\":100}");
91 HttpPost httpPost = ((HttpPost) givenParams(params, "POST"));
93 assertEquals("POST", httpPost.getMethod());
94 assertEquals("http://example.com:8081/posttest", httpPost.getURI().toURL().toString());
95 assertEquals("{\"name\":\"MyNode\", \"width\":200, \"height\":100}", EntityUtils.toString(httpPost.getEntity()));
99 public void testCreateHttpRequestPut() throws IOException, IllegalStateException, IllegalArgumentException,
100 ZoneException, APPCException {
102 Map<String, String> params = new HashMap<>();
103 params.put("org.onap.appc.instance.URI", "http://example.com:8081/puttest");
104 params.put("org.onap.appc.instance.haveHeader","false");
105 params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}");
107 HttpPut httpPut = ((HttpPut) givenParams(params, "PUT"));
108 //Header headers[] = httpPut.getAllHeaders();
110 assertEquals("PUT", httpPut.getMethod());
111 assertEquals("http://example.com:8081/puttest", httpPut.getURI().toURL().toString());
112 assertEquals("{\"name\":\"MyNode2\", \"width\":300, \"height\":300}", EntityUtils.toString(httpPut.getEntity()));
116 public void testCreateHttpRequestDelete() throws IOException, IllegalStateException, IllegalArgumentException,
117 ZoneException, APPCException {
119 Map<String, String> params = new HashMap<>();
120 params.put("org.onap.appc.instance.URI", "http://example.com:8081/deletetest");
121 params.put("org.onap.appc.instance.haveHeader","false");
123 HttpDelete httpDelete = ((HttpDelete) givenParams(params, "DELETE"));
125 assertEquals("DELETE", httpDelete.getMethod());
126 assertEquals("http://example.com:8081/deletetest", httpDelete.getURI().toURL().toString());
129 private HttpRequestBase givenParams(Map<String, String> params, String method){
130 SvcLogicContext ctx = new SvcLogicContext();
131 RequestContext rc = new RequestContext(ctx);
134 adapter = new RestAdapterImpl();
135 return adapter.createHttpRequest(method, params, rc);