2  * ============LICENSE_START=======================================================
 
   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
 
  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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  24  * ============LICENSE_END=========================================================
 
  27 package org.onap.appc.adapter.rest.impl;
 
  29 import static org.junit.Assert.assertEquals;
 
  31 import java.io.IOException;
 
  32 import java.util.HashMap;
 
  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;
 
  45 import org.onap.appc.exceptions.APPCException;
 
  46 import com.att.cdp.exceptions.ZoneException;
 
  47 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  51  * Test the ProviderAdapter implementation.
 
  53 public class TestRestAdapterImpl {
 
  54     private RestAdapterImpl adapter;
 
  57     @SuppressWarnings("nls")
 
  59     public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
 
  64     public void setup() throws IllegalArgumentException, IllegalAccessException {
 
  66         adapter = new RestAdapterImpl();
 
  70     public void testCreateHttpRequestGet() throws IOException, IllegalStateException, IllegalArgumentException,
 
  71         ZoneException, APPCException {
 
  73         Map<String, String> params = new HashMap<>();
 
  74         params.put("org.onap.appc.instance.URI", "http://example.com:8080/about/health");
 
  75         params.put("org.onap.appc.instance.haveHeader","false");
 
  77         HttpGet httpGet = ((HttpGet) givenParams(params, "GET"));
 
  79         assertEquals("GET", httpGet.getMethod());
 
  80         assertEquals("http://example.com:8080/about/health", httpGet.getURI().toURL().toString());
 
  84     public void testCreateHttpRequestPost() throws IOException, IllegalStateException, IllegalArgumentException,
 
  85         ZoneException, APPCException {    
 
  87         Map<String, String> params = new HashMap<>();
 
  88         params.put("org.onap.appc.instance.URI", "http://example.com:8081/posttest");
 
  89         params.put("org.onap.appc.instance.haveHeader","false");
 
  90         params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode\", \"width\":200, \"height\":100}");
 
  92         HttpPost httpPost = ((HttpPost) givenParams(params, "POST"));
 
  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()));
 
 100     public void testCreateHttpRequestPut() throws IOException, IllegalStateException, IllegalArgumentException,
 
 101         ZoneException, APPCException {    
 
 103         Map<String, String> params = new HashMap<>();
 
 104         params.put("org.onap.appc.instance.URI", "http://example.com:8081/puttest");
 
 105         params.put("org.onap.appc.instance.haveHeader","false");
 
 106         params.put("org.onap.appc.instance.requestBody", "{\"name\":\"MyNode2\", \"width\":300, \"height\":300}");
 
 108         HttpPut httpPut = ((HttpPut) givenParams(params, "PUT"));
 
 109         //Header headers[] = httpPut.getAllHeaders();
 
 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()));
 
 117     public void testCreateHttpRequestDelete() throws IOException, IllegalStateException, IllegalArgumentException,
 
 118         ZoneException, APPCException {    
 
 120         Map<String, String> params = new HashMap<>();
 
 121         params.put("org.onap.appc.instance.URI", "http://example.com:8081/deletetest");
 
 122         params.put("org.onap.appc.instance.haveHeader","false");
 
 124         HttpDelete httpDelete = ((HttpDelete) givenParams(params, "DELETE"));
 
 126         assertEquals("DELETE", httpDelete.getMethod());
 
 127         assertEquals("http://example.com:8081/deletetest", httpDelete.getURI().toURL().toString());
 
 130     private HttpRequestBase givenParams(Map<String, String> params, String method){
 
 131         SvcLogicContext ctx = new SvcLogicContext();
 
 132         RequestContext rc = new RequestContext(ctx);
 
 135         adapter = new RestAdapterImpl();
 
 136         return adapter.createHttpRequest(method, params, rc);