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.urlEqualTo;
29 import static com.github.tomakehurst.wiremock.client.WireMock.verify;
30 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
31 import java.net.MalformedURLException;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.onap.logging.filter.base.ONAPComponents;
36 import com.github.tomakehurst.wiremock.junit.WireMockRule;
38 public class HttpClientTest {
41 private final HttpClientFactory httpClientFactory = new HttpClientFactory();
43 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort().dynamicHttpsPort());
46 public void testPost_success() throws MalformedURLException {
48 wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
49 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("")));
51 URL url = new URL("http://localhost:" + wireMockRule.port() + "/services/sdnc/post");
52 HttpClient client = httpClientFactory.newJsonClient(url, ONAPComponents.BPMN);
54 client.addBasicAuthHeader(
55 "97FF88AB352DA16E00DDD81E3876431DEF8744465DACA489EB3B3BE1F10F63EDA1715E626D0A4827A3E19CD88421BF",
57 client.addAdditionalHeader("Accept", "application/json");
61 verify(exactly(1), postRequestedFor(urlEqualTo("/services/sdnc/post")));
65 public void testPost_nullHeader() throws MalformedURLException {
67 wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
68 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("")));
70 URL url = new URL("http://localhost:" + wireMockRule.port() + "/services/sdnc/post");
71 HttpClient client = httpClientFactory.newJsonClient(url, ONAPComponents.BPMN);
73 client.addAdditionalHeader("Accept", "application/json");
74 client.addAdditionalHeader("id", null);
79 postRequestedFor(urlEqualTo("/services/sdnc/post")).withHeader("Accept", equalTo("application/json")));
83 public void testPost_nullBasicAuth() throws MalformedURLException {
85 wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
86 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("")));
88 URL url = new URL("http://localhost:" + wireMockRule.port() + "/services/sdnc/post");
89 HttpClient client = httpClientFactory.newJsonClient(url, ONAPComponents.BPMN);
91 client.addBasicAuthHeader("", "12345");
92 client.addAdditionalHeader("Accept", "application/json");
97 postRequestedFor(urlEqualTo("/services/sdnc/post")).withHeader("Accept", equalTo("application/json")));
101 public void testPostUsingXmlClient_success() throws MalformedURLException {
103 wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
104 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/xml").withBody("")));
106 URL url = new URL("http://localhost:" + wireMockRule.port() + "/services/sdnc/post");
107 HttpClient client = httpClientFactory.newXmlClient(url, ONAPComponents.BPMN);
109 client.addBasicAuthHeader(
110 "97FF88AB352DA16E00DDD81E3876431DEF8744465DACA489EB3B3BE1F10F63EDA1715E626D0A4827A3E19CD88421BF",
112 client.addAdditionalHeader("Accept", "application/xml");
116 verify(exactly(1), postRequestedFor(urlEqualTo("/services/sdnc/post")));
120 public void testPostUsingXmlClient_nullHeader() throws MalformedURLException {
122 wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
123 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/xml").withBody("")));
125 URL url = new URL("http://localhost:" + wireMockRule.port() + "/services/sdnc/post");
126 HttpClient client = httpClientFactory.newXmlClient(url, ONAPComponents.BPMN);
128 client.accept = "application/xml";
129 client.addAdditionalHeader("id", null);
134 postRequestedFor(urlEqualTo("/services/sdnc/post")).withHeader("Accept", equalTo("application/xml")));
138 public void testPostUsingXmlClient_nullBasicAuth() throws MalformedURLException {
140 wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
141 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/xml").withBody("")));
143 URL url = new URL("http://localhost:" + wireMockRule.port() + "/services/sdnc/post");
144 HttpClient client = httpClientFactory.newXmlClient(url, ONAPComponents.BPMN);
146 client.accept = "application/xml";
147 client.addBasicAuthHeader("", "12345");
152 postRequestedFor(urlEqualTo("/services/sdnc/post")).withHeader("Accept", equalTo("application/xml")));
156 public void testPostUsingTextXmlClient_success() throws MalformedURLException {
158 wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
159 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/xml").withBody("")));
161 URL url = new URL("http://localhost:" + wireMockRule.port() + "/services/sdnc/post");
162 HttpClient client = httpClientFactory.newTextXmlClient(url, ONAPComponents.BPMN);
164 client.addBasicAuthHeader(
165 "97FF88AB352DA16E00DDD81E3876431DEF8744465DACA489EB3B3BE1F10F63EDA1715E626D0A4827A3E19CD88421BF",
167 client.addAdditionalHeader("Accept", "text/xml");
171 verify(exactly(1), postRequestedFor(urlEqualTo("/services/sdnc/post")));
175 public void testPostUsingTextXmlClient_nullHeader() throws MalformedURLException {
177 wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
178 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/xml").withBody("")));
180 URL url = new URL("http://localhost:" + wireMockRule.port() + "/services/sdnc/post");
181 HttpClient client = httpClientFactory.newTextXmlClient(url, ONAPComponents.BPMN);
183 client.addAdditionalHeader("Accept", "text/xml");
184 client.addAdditionalHeader("id", null);
189 postRequestedFor(urlEqualTo("/services/sdnc/post")).withHeader("Accept", equalTo("text/xml")));
193 public void testPostUsingTextXmlClient_nullBasicAuth() throws MalformedURLException {
195 wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
196 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/xml").withBody("")));
198 URL url = new URL("http://localhost:" + wireMockRule.port() + "/services/sdnc/post");
199 HttpClient client = httpClientFactory.newTextXmlClient(url, ONAPComponents.BPMN);
201 client.addBasicAuthHeader("", "12345");
202 client.addAdditionalHeader("Accept", "text/xml");
207 postRequestedFor(urlEqualTo("/services/sdnc/post")).withHeader("Accept", equalTo("text/xml")));