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.web.reactive.function.client.WebClientResponseException;
38 import reactor.core.publisher.Mono;
39 import reactor.test.StepVerifier;
40 import reactor.util.Loggers;
42 public class AsyncRestClientTest {
43 private static final String BASE_URL = "BaseUrl";
44 private static final String REQUEST_URL = "/test";
45 private static final String TEST_JSON = "{\"type\":\"type1\"}";
46 private static final int SUCCESS_CODE = 200;
47 private static final int ERROR_CODE = 500;
49 private static MockWebServer mockWebServer;
51 private static AsyncRestClient clientUnderTest;
54 public static void init() {
55 // skip a lot of unnecessary logs from MockWebServer
56 InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);
57 Loggers.useJdkLoggers();
58 mockWebServer = new MockWebServer();
59 clientUnderTest = new AsyncRestClient(mockWebServer.url(BASE_URL).toString());
63 public static void tearDown() throws IOException {
64 mockWebServer.shutdown();
68 public void testGetNoError() {
69 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
70 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
73 Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
74 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
78 public void testGetError() {
79 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
81 Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
82 StepVerifier.create(returnedMono)
83 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
87 public void testPutNoError() {
88 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
89 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
92 Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
93 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
97 public void testPutError() {
98 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
100 Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
101 StepVerifier.create(returnedMono)
102 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
106 public void testDeleteNoError() {
107 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE));
109 Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
110 StepVerifier.create(returnedMono).expectNext("").expectComplete().verify();
114 public void testDeleteError() {
115 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
117 Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
118 StepVerifier.create(returnedMono)
119 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();