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