75ce5f322f65229419dfbc9c3fe61bec1537abfe
[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.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;
32
33 import com.github.tomakehurst.wiremock.junit.WireMockRule;
34 import java.net.MalformedURLException;
35 import java.net.URL;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.onap.so.utils.TargetEntity;
39
40 public class HttpClientTest{
41
42
43         private final HttpClientFactory httpClientFactory = new HttpClientFactory();
44         @Rule
45         public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicHttpsPort());
46
47         @Test
48         public void testPost_success() throws MalformedURLException{
49
50         stubFor(post(urlEqualTo("/services/sdnc/post"))
51                         .willReturn(aResponse().withStatus(200)
52                 .withHeader("Content-Type", "application/json")
53                 .withBody("")));
54
55       URL url = new URL("http://localhost:" + wireMockConfig().portNumber() + "/services/sdnc/post");
56       HttpClient client = httpClientFactory.newJsonClient(url, TargetEntity.BPMN);
57
58           client.addBasicAuthHeader("97FF88AB352DA16E00DDD81E3876431DEF8744465DACA489EB3B3BE1F10F63EDA1715E626D0A4827A3E19CD88421BF", "123");
59           client.addAdditionalHeader("Accept", "application/json");
60
61           client.post("{}");
62
63           verify(exactly(1), postRequestedFor(urlEqualTo("/services/sdnc/post")));
64         }
65
66     @Test
67         public void testPost_nullHeader() throws MalformedURLException{
68
69         stubFor(post(urlEqualTo("/services/sdnc/post"))
70                         .willReturn(aResponse().withStatus(200)
71                 .withHeader("Content-Type", "application/json")
72                 .withBody("")));
73
74       URL url = new URL("http://localhost:" + wireMockConfig().portNumber() + "/services/sdnc/post");
75       HttpClient client = httpClientFactory.newJsonClient(url, TargetEntity.BPMN);
76
77           client.addAdditionalHeader("Accept", "application/json");
78           client.addAdditionalHeader("id", null);
79
80           client.post("{}");
81
82           verify(exactly(1), postRequestedFor(urlEqualTo("/services/sdnc/post"))
83                           .withHeader("Accept", equalTo("application/json")));
84         }
85
86     @Test
87         public void testPost_nullBasicAuth() throws MalformedURLException{
88
89         stubFor(post(urlEqualTo("/services/sdnc/post"))
90                         .willReturn(aResponse().withStatus(200)
91                 .withHeader("Content-Type", "application/json")
92                 .withBody("")));
93
94       URL url = new URL("http://localhost:" + wireMockConfig().portNumber() + "/services/sdnc/post");
95       HttpClient client = httpClientFactory.newJsonClient(url, TargetEntity.BPMN);
96
97       client.addBasicAuthHeader("", "12345");
98           client.addAdditionalHeader("Accept", "application/json");
99
100           client.post("{}");
101
102           verify(exactly(1), postRequestedFor(urlEqualTo("/services/sdnc/post"))
103                           .withHeader("Accept", equalTo("application/json")));
104         }
105
106 }