Changed to unmaintained
[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  * 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
18  * 
19  *      http://www.apache.org/licenses/LICENSE-2.0
20  * 
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.
26  * 
27  * ============LICENSE_END=========================================================
28  */
29
30 package org.onap.appc.adapter.rest.impl;
31
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;
37 import java.util.Map;
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;
62
63
64 /**
65  * Test the ProviderAdapter implementation.
66  */
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;
75
76     @SuppressWarnings("nls")
77     @BeforeClass
78     public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
79
80     }
81
82     @Before
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();
95     }
96
97     @Test
98     public void testCreateHttpRequestGet() throws IOException, IllegalStateException, IllegalArgumentException,
99         ZoneException, APPCException {
100
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");
104
105         HttpGet httpGet = ((HttpGet) givenParams(params, "GET"));
106
107         assertEquals("GET", httpGet.getMethod());
108         assertEquals("http://example.com:8080/about/health", httpGet.getURI().toURL().toString());
109     }
110
111     @Test
112     public void testCreateHttpRequestPost() throws IOException, IllegalStateException, IllegalArgumentException,
113         ZoneException, APPCException {
114
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}");
119
120         HttpPost httpPost = ((HttpPost) givenParams(params, "POST"));
121
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()));
125     }
126
127     @Test
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}");
136
137         adapter.commonPost(params, ctx);
138
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"));
143     }
144
145     @Test
146     public void testCreateHttpRequestPut() throws IOException, IllegalStateException, IllegalArgumentException,
147         ZoneException, APPCException {    
148
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}");
153
154         HttpPut httpPut = ((HttpPut) givenParams(params, "PUT"));
155
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()));
159     }
160
161     @Test
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<>();
167
168         adapter.commonPut(params, ctx);
169
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"));
174     }
175
176     @Test
177     public void testCreateHttpRequestDelete() throws IOException, IllegalStateException, IllegalArgumentException,
178         ZoneException, APPCException {    
179
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");
183
184         HttpDelete httpDelete = ((HttpDelete) givenParams(params, "DELETE"));
185
186         assertEquals("DELETE", httpDelete.getMethod());
187         assertEquals("http://example.com:8081/deletetest", httpDelete.getURI().toURL().toString());
188     }
189
190     @Test
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<>();
196
197         adapter.commonDelete(params, ctx);
198
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"));
203     }
204
205     @Test
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);
211     }
212
213     @Test
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");
219
220         HttpGet httpGet = ((HttpGet) givenParams(params, "GET"));
221
222         assertEquals("GET", httpGet.getMethod());
223         assertNotNull(httpGet.getHeaders("header1"));
224     }
225
226     @Test
227     public void testExecuteHttpRequest() {
228         SvcLogicContext ctx = new SvcLogicContext();
229         Map<String, String> params = new HashMap<>();
230         adapter.commonGet(params, ctx);
231         assertNotNull(params);
232     }
233
234     @Test
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<>();
240
241         adapter.commonDelete(params, ctx);
242
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"));
247     }
248
249     private HttpRequestBase givenParams(Map<String, String> params, String method){
250         SvcLogicContext ctx = new SvcLogicContext();
251         RequestContext rc = new RequestContext(ctx);
252         rc.isAlive();
253
254         adapter = new RestAdapterImpl();
255         return adapter.createHttpRequest(method, params, rc);
256     }
257 }