Merge "Enable DeleteChildService functionality" into recursive-orch
[so.git] / common / src / test / java / org / onap / so / client / HttpClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client;
22
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;
32 import java.net.URL;
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;
37
38 public class HttpClientTest {
39
40
41     private final HttpClientFactory httpClientFactory = new HttpClientFactory();
42     @Rule
43     public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort().dynamicHttpsPort());
44
45     @Test
46     public void testPost_success() throws MalformedURLException {
47
48         wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
49                 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("")));
50
51         URL url = new URL("http://localhost:" + wireMockRule.port() + "/services/sdnc/post");
52         HttpClient client = httpClientFactory.newJsonClient(url, ONAPComponents.BPMN);
53
54         client.addBasicAuthHeader(
55                 "97FF88AB352DA16E00DDD81E3876431DEF8744465DACA489EB3B3BE1F10F63EDA1715E626D0A4827A3E19CD88421BF",
56                 "123");
57         client.addAdditionalHeader("Accept", "application/json");
58
59         client.post("{}");
60
61         verify(exactly(1), postRequestedFor(urlEqualTo("/services/sdnc/post")));
62     }
63
64     @Test
65     public void testPost_nullHeader() throws MalformedURLException {
66
67         wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
68                 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("")));
69
70         URL url = new URL("http://localhost:" + wireMockRule.port() + "/services/sdnc/post");
71         HttpClient client = httpClientFactory.newJsonClient(url, ONAPComponents.BPMN);
72
73         client.addAdditionalHeader("Accept", "application/json");
74         client.addAdditionalHeader("id", null);
75
76         client.post("{}");
77
78         verify(exactly(1),
79                 postRequestedFor(urlEqualTo("/services/sdnc/post")).withHeader("Accept", equalTo("application/json")));
80     }
81
82     @Test
83     public void testPost_nullBasicAuth() throws MalformedURLException {
84
85         wireMockRule.stubFor(post(urlEqualTo("/services/sdnc/post"))
86                 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody("")));
87
88         URL url = new URL("http://localhost:" + wireMockRule.port() + "/services/sdnc/post");
89         HttpClient client = httpClientFactory.newJsonClient(url, ONAPComponents.BPMN);
90
91         client.addBasicAuthHeader("", "12345");
92         client.addAdditionalHeader("Accept", "application/json");
93
94         client.post("{}");
95
96         verify(exactly(1),
97                 postRequestedFor(urlEqualTo("/services/sdnc/post")).withHeader("Accept", equalTo("application/json")));
98     }
99
100 }