2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 Nordix Foundation
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.oransc.ics.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;
55 private static final SecurityContext securityContext = new SecurityContext("");
59 // skip a lot of unnecessary logs from MockWebServer
60 InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);
61 Loggers.useJdkLoggers();
62 mockWebServer = new MockWebServer();
63 clientUnderTest = new AsyncRestClient(mockWebServer.url(BASE_URL).toString(), null, null, securityContext);
67 static void tearDown() throws IOException {
68 mockWebServer.shutdown();
72 void testGetNoError() {
73 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
74 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
77 Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
78 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
83 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
85 Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
86 StepVerifier.create(returnedMono)
87 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
91 void testPutNoError() {
92 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
93 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
96 Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
97 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
101 void testPutError() {
102 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
104 Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
105 StepVerifier.create(returnedMono)
106 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
110 void testDeleteNoError() {
111 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE));
113 Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
114 StepVerifier.create(returnedMono).expectNext("").expectComplete().verify();
118 void testDeleteError() {
119 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
121 Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
122 StepVerifier.create(returnedMono)
123 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
127 void testPostNoError() {
128 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
129 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
130 .setBody(TEST_JSON));
132 Mono<String> returnedMono = clientUnderTest.post(REQUEST_URL, TEST_JSON);
133 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
137 void testPostError() {
138 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
140 Mono<String> returnedMono = clientUnderTest.post(REQUEST_URL, TEST_JSON);
141 StepVerifier.create(returnedMono)
142 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
146 void testPostWithAuthHeaderNoError() {
147 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
148 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
149 .setBody(TEST_JSON));
151 Mono<String> returnedMono = clientUnderTest.postWithAuthHeader(REQUEST_URL, TEST_JSON, USERNAME, PASSWORD);
152 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
156 void testPostWithAuthHeaderError() {
157 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
159 Mono<String> returnedMono = clientUnderTest.postWithAuthHeader(REQUEST_URL, TEST_JSON, USERNAME, PASSWORD);
160 StepVerifier.create(returnedMono)
161 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();