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;
32 import javax.net.ssl.HttpsURLConnection;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mockito;
38 public class WebClientTest {
40 HttpsURLConnection mockedHttpsUrlConnection;
41 String sampleString = "Response Code :200";
44 * Set up the mocked REST manager.
46 * @throws IOException on I/O errors
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();
57 public void testHttpsRequest() {
58 WebClient cl = new WebClient();
60 .httpRequest("https://some.random.url/data", "POST", null, "admin", "admin", "application/json");
61 assertNotNull(result);
65 public void testHttpRequest() {
66 WebClient cl = new WebClient();
68 .httpRequest("http://some.random.url/data", "GET", null, "admin", "admin", "application/json");
69 assertNotNull(result);
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));