Fix/Add unit tests for appc-rest-adapter
[appc.git] / appc-adapters / appc-rest-adapter / appc-rest-adapter-bundle / src / test / java / org / openecomp / appc / adapter / rest / impl / TestRestAdapterImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24  * ============LICENSE_END=========================================================
25  */
26
27 package org.openecomp.appc.adapter.rest.impl;
28
29 import static org.junit.Assert.assertEquals;
30
31 import java.io.IOException;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import org.apache.http.client.methods.HttpDelete;
36 import org.apache.http.client.methods.HttpGet;
37 import org.apache.http.client.methods.HttpPost;
38 import org.apache.http.client.methods.HttpPut;
39 import org.apache.http.client.methods.HttpRequestBase;
40 import org.apache.http.util.EntityUtils;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44
45 import org.openecomp.appc.exceptions.APPCException;
46 import com.att.cdp.exceptions.ZoneException;
47 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
48
49
50 /**
51  * Test the ProviderAdapter implementation.
52  */
53 public class TestRestAdapterImpl {
54     private RestAdapterImpl adapter;
55
56
57     @SuppressWarnings("nls")
58     @BeforeClass
59     public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
60
61     }
62
63     @Before
64     public void setup() throws IllegalArgumentException, IllegalAccessException {
65
66         adapter = new RestAdapterImpl();
67     }
68     
69     @Test
70     public void testCreateHttpRequestGet() throws IOException, IllegalStateException, IllegalArgumentException,
71         ZoneException, APPCException {
72
73         Map<String, String> params = new HashMap<>();
74         params.put("org.openecomp.appc.instance.URI", "http://example.com:8080/about/health");
75         params.put("org.openecomp.appc.instance.haveHeader","false");
76
77         HttpGet httpGet = ((HttpGet) givenParams(params, "GET"));
78
79         assertEquals("GET", httpGet.getMethod());
80         assertEquals("http://example.com:8080/about/health", httpGet.getURI().toURL().toString());
81     }
82     
83     @Test
84     public void testCreateHttpRequestPost() throws IOException, IllegalStateException, IllegalArgumentException,
85         ZoneException, APPCException {    
86
87         Map<String, String> params = new HashMap<>();
88         params.put("org.openecomp.appc.instance.URI", "http://example.com:8081/posttest");
89         params.put("org.openecomp.appc.instance.haveHeader","false");
90         params.put("org.openecomp.appc.instance.requestBody", "{\"name\":\"MyNode\", \"width\":200, \"height\":100}");
91
92         HttpPost httpPost = ((HttpPost) givenParams(params, "POST"));
93
94         assertEquals("POST", httpPost.getMethod());
95         assertEquals("http://example.com:8081/posttest", httpPost.getURI().toURL().toString());
96         assertEquals("{\"name\":\"MyNode\", \"width\":200, \"height\":100}", EntityUtils.toString(httpPost.getEntity()));
97     }
98     
99     @Test
100     public void testCreateHttpRequestPut() throws IOException, IllegalStateException, IllegalArgumentException,
101         ZoneException, APPCException {    
102
103         Map<String, String> params = new HashMap<>();
104         params.put("org.openecomp.appc.instance.URI", "http://example.com:8081/puttest");
105         params.put("org.openecomp.appc.instance.haveHeader","false");
106         params.put("org.openecomp.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}");
107
108         HttpPut httpPut = ((HttpPut) givenParams(params, "PUT"));
109         //Header headers[] = httpPut.getAllHeaders();
110
111         assertEquals("PUT", httpPut.getMethod());
112         assertEquals("http://example.com:8081/puttest", httpPut.getURI().toURL().toString());
113         assertEquals("{\"name\":\"MyNode2\", \"width\":300, \"height\":300}", EntityUtils.toString(httpPut.getEntity()));
114     }
115     
116     @Test
117     public void testCreateHttpRequestDelete() throws IOException, IllegalStateException, IllegalArgumentException,
118         ZoneException, APPCException {    
119
120         Map<String, String> params = new HashMap<>();
121         params.put("org.openecomp.appc.instance.URI", "http://example.com:8081/deletetest");
122         params.put("org.openecomp.appc.instance.haveHeader","false");
123
124         HttpDelete httpDelete = ((HttpDelete) givenParams(params, "DELETE"));
125
126         assertEquals("DELETE", httpDelete.getMethod());
127         assertEquals("http://example.com:8081/deletetest", httpDelete.getURI().toURL().toString());
128     }
129
130     private HttpRequestBase givenParams(Map<String, String> params, String method){
131         SvcLogicContext ctx = new SvcLogicContext();
132         RequestContext rc = new RequestContext(ctx);
133         rc.isAlive();
134
135         adapter = new RestAdapterImpl();
136         return adapter.createHttpRequest(method, params, rc);
137     }
138
139
140 }