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;
32 import java.net.MalformedURLException;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.onap.so.utils.TargetEntity;
39 import com.github.tomakehurst.wiremock.junit.WireMockRule;
41 public class HttpClientTest{
44 private final HttpClientFactory httpClientFactory = new HttpClientFactory();
46 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicHttpsPort());
49 public void testPost_success() throws MalformedURLException{
51 wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
52 .willReturn(aResponse().withStatus(200)
53 .withHeader("Content-Type", "application/json")
56 URL url = new URL("http://localhost:" + wireMockConfig().portNumber() + "/services/sdnc/post");
57 HttpClient client = httpClientFactory.newJsonClient(url, TargetEntity.BPMN);
59 client.addBasicAuthHeader("97FF88AB352DA16E00DDD81E3876431DEF8744465DACA489EB3B3BE1F10F63EDA1715E626D0A4827A3E19CD88421BF", "123");
60 client.addAdditionalHeader("Accept", "application/json");
64 verify(exactly(1), postRequestedFor(urlEqualTo("/services/sdnc/post")));
68 public void testPost_nullHeader() throws MalformedURLException{
70 wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
71 .willReturn(aResponse().withStatus(200)
72 .withHeader("Content-Type", "application/json")
75 URL url = new URL("http://localhost:" + wireMockConfig().portNumber() + "/services/sdnc/post");
76 HttpClient client = httpClientFactory.newJsonClient(url, TargetEntity.BPMN);
78 client.addAdditionalHeader("Accept", "application/json");
79 client.addAdditionalHeader("id", null);
83 verify(exactly(1), postRequestedFor(urlEqualTo("/services/sdnc/post"))
84 .withHeader("Accept", equalTo("application/json")));
88 public void testPost_nullBasicAuth() throws MalformedURLException{
90 wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
91 .willReturn(aResponse().withStatus(200)
92 .withHeader("Content-Type", "application/json")
95 URL url = new URL("http://localhost:" + wireMockConfig().portNumber() + "/services/sdnc/post");
96 HttpClient client = httpClientFactory.newJsonClient(url, TargetEntity.BPMN);
98 client.addBasicAuthHeader("", "12345");
99 client.addAdditionalHeader("Accept", "application/json");
103 verify(exactly(1), postRequestedFor(urlEqualTo("/services/sdnc/post"))
104 .withHeader("Accept", equalTo("application/json")));