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.portal.nonrtric.controlpanel.util;
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.http.ResponseEntity;
37 import org.springframework.web.reactive.function.client.WebClientResponseException;
39 import reactor.core.publisher.Mono;
40 import reactor.test.StepVerifier;
41 import reactor.util.Loggers;
43 class AsyncRestClientTest {
44 private static final String BASE_URL = "BaseUrl";
45 private static final String REQUEST_URL = "/test";
46 private static final String TEST_JSON = "{\"type\":\"type1\"}";
47 private static final int SUCCESS_CODE = 200;
48 private static final int ERROR_CODE = 500;
50 private static MockWebServer mockWebServer;
52 private static AsyncRestClient clientUnderTest;
55 public static void init() {
56 // skip a lot of unnecessary logs from MockWebServer
57 InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);
58 Loggers.useJdkLoggers();
59 mockWebServer = new MockWebServer();
60 clientUnderTest = new AsyncRestClient(mockWebServer.url(BASE_URL).toString());
64 public static void tearDown() throws IOException {
65 mockWebServer.shutdown();
69 void testGetNoError() {
70 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
71 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
74 Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
75 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
80 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
82 Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
83 StepVerifier.create(returnedMono)
84 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
88 void testPutNoError() {
89 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
90 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
93 Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
94 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
98 void testPutWithoutBodyNoError() {
99 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
100 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
102 Mono<ResponseEntity<String>> returnedMono = clientUnderTest.putForEntity(REQUEST_URL);
103 StepVerifier.create(returnedMono).expectNextCount(1).expectComplete().verify();
107 void testPutError() {
108 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
110 Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
111 StepVerifier.create(returnedMono)
112 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
116 void testDeleteNoError() {
117 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE));
119 Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
120 StepVerifier.create(returnedMono).expectNext("").expectComplete().verify();
124 void testDeleteError() {
125 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
127 Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
128 StepVerifier.create(returnedMono)
129 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();