ca5a89874653254ad0c994dc4495ba15d224f0af
[appc.git] / appc-adapters / appc-rest-adapter / appc-rest-adapter-bundle / src / test / java / org / onap / appc / adapter / rest / impl / TestRestAdapterImpl.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  * 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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.adapter.rest.impl;
27
28 import static org.junit.Assert.assertEquals;
29
30 import java.io.IOException;
31 import java.util.HashMap;
32 import java.util.Map;
33
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;
43
44 import org.onap.appc.exceptions.APPCException;
45 import com.att.cdp.exceptions.ZoneException;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
47
48
49 /**
50  * Test the ProviderAdapter implementation.
51  */
52 public class TestRestAdapterImpl {
53     private RestAdapterImpl adapter;
54
55
56     @SuppressWarnings("nls")
57     @BeforeClass
58     public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
59
60     }
61
62     @Before
63     public void setup() throws IllegalArgumentException, IllegalAccessException {
64
65         adapter = new RestAdapterImpl();
66     }
67     
68     @Test
69     public void testCreateHttpRequestGet() throws IOException, IllegalStateException, IllegalArgumentException,
70         ZoneException, APPCException {
71
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");
75
76         HttpGet httpGet = ((HttpGet) givenParams(params, "GET"));
77
78         assertEquals("GET", httpGet.getMethod());
79         assertEquals("http://example.com:8080/about/health", httpGet.getURI().toURL().toString());
80     }
81     
82     @Test
83     public void testCreateHttpRequestPost() throws IOException, IllegalStateException, IllegalArgumentException,
84         ZoneException, APPCException {    
85
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}");
90
91         HttpPost httpPost = ((HttpPost) givenParams(params, "POST"));
92
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()));
96     }
97     
98     @Test
99     public void testCreateHttpRequestPut() throws IOException, IllegalStateException, IllegalArgumentException,
100         ZoneException, APPCException {    
101
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}");
106
107         HttpPut httpPut = ((HttpPut) givenParams(params, "PUT"));
108         //Header headers[] = httpPut.getAllHeaders();
109
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()));
113     }
114     
115     @Test
116     public void testCreateHttpRequestDelete() throws IOException, IllegalStateException, IllegalArgumentException,
117         ZoneException, APPCException {    
118
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");
122
123         HttpDelete httpDelete = ((HttpDelete) givenParams(params, "DELETE"));
124
125         assertEquals("DELETE", httpDelete.getMethod());
126         assertEquals("http://example.com:8081/deletetest", httpDelete.getURI().toURL().toString());
127     }
128
129     private HttpRequestBase givenParams(Map<String, String> params, String method){
130         SvcLogicContext ctx = new SvcLogicContext();
131         RequestContext rc = new RequestContext(ctx);
132         rc.isAlive();
133
134         adapter = new RestAdapterImpl();
135         return adapter.createHttpRequest(method, params, rc);
136     }
137
138
139 }