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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.examples.bbs;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
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;
36 public class WebClientTest {
38 HttpsURLConnection mockedHttpsUrlConnection;
39 String sampleString = "Response Code :200";
42 * Set up the mocked REST manager.
44 * @throws IOException on I/O errors
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();
55 public void testHttpsRequest() {
56 WebClient cl = new WebClient();
58 .httpRequest("https://some.random.url/data", "POST", null, "admin", "admin", "application/json");
59 assertNotNull(result);
63 public void testHttpRequest() {
64 WebClient cl = new WebClient();
66 .httpRequest("http://some.random.url/data", "GET", null, "admin", "admin", "application/json");
67 assertNotNull(result);
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));