b4c96d5d518687bbe0ecdec114cdddf1c6da7dcb
[appc.git] /
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  * 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
16  * 
17  *      http://www.apache.org/licenses/LICENSE-2.0
18  * 
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.
24  * 
25  * ============LICENSE_END=========================================================
26  */
27
28 package org.onap.appc.adapter.rest.impl;
29
30 import static org.junit.Assert.assertEquals;
31
32 import java.io.IOException;
33 import java.util.HashMap;
34 import java.util.Map;
35
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;
45
46 import org.onap.appc.exceptions.APPCException;
47 import com.att.cdp.exceptions.ZoneException;
48 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
49
50
51 /**
52  * Test the ProviderAdapter implementation.
53  */
54 public class TestRestAdapterImpl {
55     private RestAdapterImpl adapter;
56
57
58     @SuppressWarnings("nls")
59     @BeforeClass
60     public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
61
62     }
63
64     @Before
65     public void setup() throws IllegalArgumentException, IllegalAccessException {
66
67         adapter = new RestAdapterImpl();
68     }
69     
70     @Test
71     public void testCreateHttpRequestGet() throws IOException, IllegalStateException, IllegalArgumentException,
72         ZoneException, APPCException {
73
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");
77
78         HttpGet httpGet = ((HttpGet) givenParams(params, "GET"));
79
80         assertEquals("GET", httpGet.getMethod());
81         assertEquals("http://example.com:8080/about/health", httpGet.getURI().toURL().toString());
82     }
83
84     @Test
85     public void testCreateRequestNoParamGet() throws IOException, IllegalStateException, IllegalArgumentException,
86             ZoneException, APPCException {
87
88         SvcLogicContext ctx = new SvcLogicContext();
89         Map<String, String> params = new HashMap<>();
90
91         adapter.commonGet(params, ctx);
92
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"));
97     }
98
99     @Test
100     public void testCreateRequestInvalidParamGet() throws IOException, IllegalStateException, IllegalArgumentException,
101             ZoneException, APPCException {
102
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}");
108
109         adapter.commonGet(params, ctx);
110
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"));
115     }
116     
117     @Test
118     public void testCreateHttpRequestPost() throws IOException, IllegalStateException, IllegalArgumentException,
119         ZoneException, APPCException {    
120
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}");
125
126         HttpPost httpPost = ((HttpPost) givenParams(params, "POST"));
127
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()));
131     }
132
133     @Test
134     public void testCreateRequestNoParamPost() throws IOException, IllegalStateException, IllegalArgumentException,
135             ZoneException, APPCException {
136
137         SvcLogicContext ctx = new SvcLogicContext();
138         Map<String, String> params = new HashMap<>();
139
140         adapter.commonPost(params, ctx);
141
142         assertEquals("failure", ctx.getStatus());
143         assertEquals("500", ctx.getAttribute("org.openecomp.rest.result.code"));
144         assertEquals("java.lang.IllegalArgumentException: HTTP request may not be null",
145                      ctx.getAttribute("org.openecomp.rest.result.message"));
146     }
147
148     @Test
149     public void testCreateRequestInvalidParamPost() throws IOException, IllegalStateException, IllegalArgumentException,
150             ZoneException, APPCException {
151
152         SvcLogicContext ctx = new SvcLogicContext();
153         Map<String, String> params = new HashMap<>();
154         params.put("org.onap.appc.instance.URI", "boo");
155         params.put("org.onap.appc.instance.haveHeader","false");
156         params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}");
157
158         adapter.commonPost(params, ctx);
159
160         assertEquals("failure", ctx.getStatus());
161         assertEquals("500", ctx.getAttribute("org.openecomp.rest.result.code"));
162         assertEquals("org.apache.http.client.ClientProtocolException",
163                      ctx.getAttribute("org.openecomp.rest.result.message"));
164     }
165     
166     @Test
167     public void testCreateHttpRequestPut() throws IOException, IllegalStateException, IllegalArgumentException,
168         ZoneException, APPCException {    
169
170         Map<String, String> params = new HashMap<>();
171         params.put("org.onap.appc.instance.URI", "http://example.com:8081/puttest");
172         params.put("org.onap.appc.instance.haveHeader","false");
173         params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}");
174
175         HttpPut httpPut = ((HttpPut) givenParams(params, "PUT"));
176         //Header headers[] = httpPut.getAllHeaders();
177
178         assertEquals("PUT", httpPut.getMethod());
179         assertEquals("http://example.com:8081/puttest", httpPut.getURI().toURL().toString());
180         assertEquals("{\"name\":\"MyNode2\", \"width\":300, \"height\":300}", EntityUtils.toString(httpPut.getEntity()));
181     }
182
183     @Test
184     public void testCreateRequestNoParamPut() throws IOException, IllegalStateException, IllegalArgumentException,
185             ZoneException, APPCException {
186
187         SvcLogicContext ctx = new SvcLogicContext();
188         Map<String, String> params = new HashMap<>();
189
190         adapter.commonPut(params, ctx);
191
192         assertEquals("failure", ctx.getStatus());
193         assertEquals("500", ctx.getAttribute("org.openecomp.rest.result.code"));
194         assertEquals("java.lang.IllegalArgumentException: HTTP request may not be null",
195                      ctx.getAttribute("org.openecomp.rest.result.message"));
196     }
197
198     @Test
199     public void testCreateRequestInvalidParamPut() throws IOException, IllegalStateException, IllegalArgumentException,
200             ZoneException, APPCException {
201
202         SvcLogicContext ctx = new SvcLogicContext();
203         Map<String, String> params = new HashMap<>();
204         params.put("org.onap.appc.instance.URI", "boo");
205         params.put("org.onap.appc.instance.haveHeader","false");
206         params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}");
207
208         adapter.commonPut(params, ctx);
209
210         assertEquals("failure", ctx.getStatus());
211         assertEquals("500", ctx.getAttribute("org.openecomp.rest.result.code"));
212         assertEquals("org.apache.http.client.ClientProtocolException",
213                      ctx.getAttribute("org.openecomp.rest.result.message"));
214     }
215
216     @Test
217     public void testCreateHttpRequestDelete() throws IOException, IllegalStateException, IllegalArgumentException,
218         ZoneException, APPCException {    
219
220         Map<String, String> params = new HashMap<>();
221         params.put("org.onap.appc.instance.URI", "http://example.com:8081/deletetest");
222         params.put("org.onap.appc.instance.haveHeader","false");
223
224         HttpDelete httpDelete = ((HttpDelete) givenParams(params, "DELETE"));
225
226         assertEquals("DELETE", httpDelete.getMethod());
227         assertEquals("http://example.com:8081/deletetest", httpDelete.getURI().toURL().toString());
228     }
229
230     @Test
231     public void testCreateRequestNoParamDelete() throws IOException, IllegalStateException, IllegalArgumentException,
232             ZoneException, APPCException {
233
234         SvcLogicContext ctx = new SvcLogicContext();
235         Map<String, String> params = new HashMap<>();
236
237         adapter.commonDelete(params, ctx);
238
239         assertEquals("failure", ctx.getStatus());
240         assertEquals("500", ctx.getAttribute("org.openecomp.rest.result.code"));
241         assertEquals("java.lang.IllegalArgumentException: HTTP request may not be null",
242                      ctx.getAttribute("org.openecomp.rest.result.message"));
243     }
244
245     @Test
246     public void testCreateRequestInvalidParamDelete() throws IOException, IllegalStateException, IllegalArgumentException,
247             ZoneException, APPCException {
248
249         SvcLogicContext ctx = new SvcLogicContext();
250         Map<String, String> params = new HashMap<>();
251         params.put("org.onap.appc.instance.URI", "boo");
252         params.put("org.onap.appc.instance.haveHeader","false");
253         params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}");
254
255         adapter.commonDelete(params, ctx);
256
257         assertEquals("failure", ctx.getStatus());
258         assertEquals("500", ctx.getAttribute("org.openecomp.rest.result.code"));
259         assertEquals("org.apache.http.client.ClientProtocolException",
260                      ctx.getAttribute("org.openecomp.rest.result.message"));
261     }
262     
263     private HttpRequestBase givenParams(Map<String, String> params, String method){
264         SvcLogicContext ctx = new SvcLogicContext();
265         RequestContext rc = new RequestContext(ctx);
266         rc.isAlive();
267
268         adapter = new RestAdapterImpl();
269         return adapter.createHttpRequest(method, params, rc);
270     }
271
272
273 }