2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2020 Nordix Foundation. All rights reserved.
6 * ======================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.onap.ccsdk.oran.a1policymanagementservice.clients;
23 import io.netty.util.internal.logging.InternalLoggerFactory;
24 import io.netty.util.internal.logging.JdkLoggerFactory;
26 import java.io.IOException;
28 import okhttp3.mockwebserver.MockResponse;
29 import okhttp3.mockwebserver.MockWebServer;
31 import org.junit.jupiter.api.AfterAll;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.Test;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.MediaType;
36 import org.springframework.web.reactive.function.client.WebClientResponseException;
38 import reactor.core.publisher.Mono;
39 import reactor.test.StepVerifier;
40 import reactor.util.Loggers;
42 class AsyncRestClientTest {
43 private static final String BASE_URL = "BaseUrl";
44 private static final String REQUEST_URL = "/test";
45 private static final String USERNAME = "username";
46 private static final String PASSWORD = "password";
47 private static final String TEST_JSON = "{\"type\":\"type1\"}";
48 private static final int SUCCESS_CODE = 200;
49 private static final int ERROR_CODE = 500;
51 private static MockWebServer mockWebServer;
53 private static AsyncRestClient clientUnderTest;
57 // skip a lot of unnecessary logs from MockWebServer
58 InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);
59 Loggers.useJdkLoggers();
60 mockWebServer = new MockWebServer();
62 new AsyncRestClient(mockWebServer.url(BASE_URL).toString(), null, null, new SecurityContext(""));
66 static void tearDown() throws IOException {
67 mockWebServer.shutdown();
71 void testGetNoError() {
72 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
73 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
76 Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
77 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
82 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
84 Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
85 StepVerifier.create(returnedMono)
86 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
90 void testPutNoError() {
91 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
92 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
95 Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
96 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
100 void testPutError() {
101 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
103 Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
104 StepVerifier.create(returnedMono)
105 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
109 void testDeleteNoError() {
110 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE));
112 Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
113 StepVerifier.create(returnedMono).expectNext("").expectComplete().verify();
117 void testDeleteError() {
118 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
120 Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
121 StepVerifier.create(returnedMono)
122 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
126 void testPostNoError() {
127 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
128 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
129 .setBody(TEST_JSON));
131 Mono<String> returnedMono = clientUnderTest.post(REQUEST_URL, TEST_JSON);
132 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
136 void testPostError() {
137 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
139 Mono<String> returnedMono = clientUnderTest.post(REQUEST_URL, TEST_JSON);
140 StepVerifier.create(returnedMono)
141 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
145 void testPostWithAuthHeaderNoError() {
146 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
147 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
148 .setBody(TEST_JSON));
150 Mono<String> returnedMono = clientUnderTest.postWithAuthHeader(REQUEST_URL, TEST_JSON, USERNAME, PASSWORD);
151 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
155 void testPostWithAuthHeaderError() {
156 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
158 Mono<String> returnedMono = clientUnderTest.postWithAuthHeader(REQUEST_URL, TEST_JSON, USERNAME, PASSWORD);
159 StepVerifier.create(returnedMono)
160 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();