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