ba1481c0b5d2678098d97cee0682ce72d3014a24
[policy/apex-pdp.git] / examples / examples-onap-bbs / src / test / java / org / onap / policy / apex / examples / bbs / WebClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 huawei. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.examples.bbs;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31
32 import javax.net.ssl.HttpsURLConnection;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37
38 public class WebClientTest {
39
40     HttpsURLConnection mockedHttpsUrlConnection;
41     String sampleString = "Response Code :200";
42
43     /**
44      * Set up the mocked REST manager.
45      *
46      * @throws IOException on I/O errors
47      */
48     @Before
49     public void setupMockedRest() throws IOException {
50         mockedHttpsUrlConnection = mock(HttpsURLConnection.class);
51         InputStream inputStream = new ByteArrayInputStream(sampleString.getBytes());
52         when(mockedHttpsUrlConnection.getInputStream()).thenReturn(inputStream);
53         Mockito.doNothing().when(mockedHttpsUrlConnection).connect();
54     }
55
56     @Test
57     public void testHttpsRequest() {
58         WebClient cl = new WebClient();
59         String result = cl
60             .httpRequest("https://some.random.url/data", "POST", null, "admin", "admin", "application/json");
61         assertNotNull(result);
62     }
63
64     @Test
65     public void testHttpRequest() {
66         WebClient cl = new WebClient();
67         String result = cl
68             .httpRequest("http://some.random.url/data", "GET", null, "admin", "admin", "application/json");
69         assertNotNull(result);
70     }
71
72     @Test
73     public void testToPrettyString() {
74         String xmlSample = "<input xmlns=\"org:onap:sdnc:northbound:generic-resource\">"
75             + "<sdnc-request-header> <svc-action>update</svc-action> </sdnc-request-header></input>";
76         WebClient cl = new WebClient();
77         assertNotNull(cl.toPrettyString(xmlSample, 4));
78     }
79 }