2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client;
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
25 import static com.github.tomakehurst.wiremock.client.WireMock.exactly;
26 import static com.github.tomakehurst.wiremock.client.WireMock.post;
27 import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
28 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
29 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
30 import static com.github.tomakehurst.wiremock.client.WireMock.verify;
31 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
33 import com.github.tomakehurst.wiremock.junit.WireMockRule;
34 import java.net.MalformedURLException;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.onap.so.utils.TargetEntity;
40 public class HttpClientTest{
43 private final HttpClientFactory httpClientFactory = new HttpClientFactory();
45 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicHttpsPort());
48 public void testPost_success() throws MalformedURLException{
50 stubFor(post(urlEqualTo("/services/sdnc/post"))
51 .willReturn(aResponse().withStatus(200)
52 .withHeader("Content-Type", "application/json")
55 URL url = new URL("http://localhost:" + wireMockConfig().portNumber() + "/services/sdnc/post");
56 HttpClient client = httpClientFactory.newJsonClient(url, TargetEntity.BPMN);
58 client.addBasicAuthHeader("97FF88AB352DA16E00DDD81E3876431DEF8744465DACA489EB3B3BE1F10F63EDA1715E626D0A4827A3E19CD88421BF", "123");
59 client.addAdditionalHeader("Accept", "application/json");
63 verify(exactly(1), postRequestedFor(urlEqualTo("/services/sdnc/post")));
67 public void testPost_nullHeader() throws MalformedURLException{
69 stubFor(post(urlEqualTo("/services/sdnc/post"))
70 .willReturn(aResponse().withStatus(200)
71 .withHeader("Content-Type", "application/json")
74 URL url = new URL("http://localhost:" + wireMockConfig().portNumber() + "/services/sdnc/post");
75 HttpClient client = httpClientFactory.newJsonClient(url, TargetEntity.BPMN);
77 client.addAdditionalHeader("Accept", "application/json");
78 client.addAdditionalHeader("id", null);
82 verify(exactly(1), postRequestedFor(urlEqualTo("/services/sdnc/post"))
83 .withHeader("Accept", equalTo("application/json")));
87 public void testPost_nullBasicAuth() throws MalformedURLException{
89 stubFor(post(urlEqualTo("/services/sdnc/post"))
90 .willReturn(aResponse().withStatus(200)
91 .withHeader("Content-Type", "application/json")
94 URL url = new URL("http://localhost:" + wireMockConfig().portNumber() + "/services/sdnc/post");
95 HttpClient client = httpClientFactory.newJsonClient(url, TargetEntity.BPMN);
97 client.addBasicAuthHeader("", "12345");
98 client.addAdditionalHeader("Accept", "application/json");
102 verify(exactly(1), postRequestedFor(urlEqualTo("/services/sdnc/post"))
103 .withHeader("Accept", equalTo("application/json")));