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.policyagent.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 public 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;
56 public static void init() {
57 // skip a lot of unnecessary logs from MockWebServer
58 InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);
59 Loggers.useJdkLoggers();
60 mockWebServer = new MockWebServer();
61 clientUnderTest = new AsyncRestClient(mockWebServer.url(BASE_URL).toString());
65 public static void tearDown() throws IOException {
66 mockWebServer.shutdown();
70 public void testGetNoError() {
71 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
72 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
75 Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
76 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
80 public void testGetError() {
81 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
83 Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
84 StepVerifier.create(returnedMono)
85 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
89 public void testPutNoError() {
90 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
91 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
94 Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
95 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
99 public void testPutError() {
100 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
102 Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
103 StepVerifier.create(returnedMono)
104 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
108 public void testDeleteNoError() {
109 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE));
111 Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
112 StepVerifier.create(returnedMono).expectNext("").expectComplete().verify();
116 public void testDeleteError() {
117 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
119 Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
120 StepVerifier.create(returnedMono)
121 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
125 public void testPostNoError() {
126 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
127 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
128 .setBody(TEST_JSON));
130 Mono<String> returnedMono = clientUnderTest.post(REQUEST_URL, TEST_JSON);
131 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
135 public void testPostError() {
136 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
138 Mono<String> returnedMono = clientUnderTest.post(REQUEST_URL, TEST_JSON);
139 StepVerifier.create(returnedMono)
140 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
144 public void testPostWithAuthHeaderNoError() {
145 mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
146 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
147 .setBody(TEST_JSON));
149 Mono<String> returnedMono = clientUnderTest.postWithAuthHeader(REQUEST_URL, TEST_JSON, USERNAME, PASSWORD);
150 StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
154 public void testPostWithAuthHeaderError() {
155 mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
157 Mono<String> returnedMono = clientUnderTest.postWithAuthHeader(REQUEST_URL, TEST_JSON, USERNAME, PASSWORD);
158 StepVerifier.create(returnedMono)
159 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();