Changes for checkstyle 8.32
[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 import javax.net.ssl.HttpsURLConnection;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35
36 public class WebClientTest {
37
38     HttpsURLConnection mockedHttpsUrlConnection;
39     String sampleString = "Response Code :200";
40
41     /**
42      * Set up the mocked REST manager.
43      *
44      * @throws IOException on I/O errors
45      */
46     @Before
47     public void setupMockedRest() throws IOException {
48         mockedHttpsUrlConnection = mock(HttpsURLConnection.class);
49         InputStream inputStream = new ByteArrayInputStream(sampleString.getBytes());
50         when(mockedHttpsUrlConnection.getInputStream()).thenReturn(inputStream);
51         Mockito.doNothing().when(mockedHttpsUrlConnection).connect();
52     }
53
54     @Test
55     public void testHttpsRequest() {
56         WebClient cl = new WebClient();
57         String result = cl
58             .httpRequest("https://some.random.url/data", "POST", null, "admin", "admin", "application/json");
59         assertNotNull(result);
60     }
61
62     @Test
63     public void testHttpRequest() {
64         WebClient cl = new WebClient();
65         String result = cl
66             .httpRequest("http://some.random.url/data", "GET", null, "admin", "admin", "application/json");
67         assertNotNull(result);
68     }
69
70     @Test
71     public void testToPrettyString() {
72         String xmlSample = "<input xmlns=\"org:onap:sdnc:northbound:generic-resource\">"
73             + "<sdnc-request-header> <svc-action>update</svc-action> </sdnc-request-header></input>";
74         WebClient cl = new WebClient();
75         assertNotNull(cl.toPrettyString(xmlSample, 4));
76     }
77 }