3f5f78d44746674088117a4b3b0c78836b0785ff
[nonrtric.git] / information-coordinator-service / src / test / java / org / oransc / ics / clients / AsyncRestClientTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.ics.clients;
22
23 import io.netty.util.internal.logging.InternalLoggerFactory;
24 import io.netty.util.internal.logging.JdkLoggerFactory;
25
26 import java.io.IOException;
27
28 import okhttp3.mockwebserver.MockResponse;
29 import okhttp3.mockwebserver.MockWebServer;
30
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;
37
38 import reactor.core.publisher.Mono;
39 import reactor.test.StepVerifier;
40 import reactor.util.Loggers;
41
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;
50
51     private static MockWebServer mockWebServer;
52
53     private static AsyncRestClient clientUnderTest;
54
55     private static final SecurityContext securityContext = new SecurityContext("");
56
57     @BeforeAll
58     static void init() {
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);
64     }
65
66     @AfterAll
67     static void tearDown() throws IOException {
68         mockWebServer.shutdown();
69     }
70
71     @Test
72     void testGetNoError() {
73         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
74             .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
75             .setBody(TEST_JSON));
76
77         Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
78         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
79     }
80
81     @Test
82     void testGetError() {
83         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
84
85         Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
86         StepVerifier.create(returnedMono)
87             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
88     }
89
90     @Test
91     void testPutNoError() {
92         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
93             .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
94             .setBody(TEST_JSON));
95
96         Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
97         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
98     }
99
100     @Test
101     void testPutError() {
102         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
103
104         Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
105         StepVerifier.create(returnedMono)
106             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
107     }
108
109     @Test
110     void testDeleteNoError() {
111         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE));
112
113         Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
114         StepVerifier.create(returnedMono).expectNext("").expectComplete().verify();
115     }
116
117     @Test
118     void testDeleteError() {
119         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
120
121         Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
122         StepVerifier.create(returnedMono)
123             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
124     }
125
126     @Test
127     void testPostNoError() {
128         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
129             .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
130             .setBody(TEST_JSON));
131
132         Mono<String> returnedMono = clientUnderTest.post(REQUEST_URL, TEST_JSON);
133         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
134     }
135
136     @Test
137     void testPostError() {
138         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
139
140         Mono<String> returnedMono = clientUnderTest.post(REQUEST_URL, TEST_JSON);
141         StepVerifier.create(returnedMono)
142             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
143     }
144
145     @Test
146     void testPostWithAuthHeaderNoError() {
147         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
148             .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
149             .setBody(TEST_JSON));
150
151         Mono<String> returnedMono = clientUnderTest.postWithAuthHeader(REQUEST_URL, TEST_JSON, USERNAME, PASSWORD);
152         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
153     }
154
155     @Test
156     void testPostWithAuthHeaderError() {
157         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
158
159         Mono<String> returnedMono = clientUnderTest.postWithAuthHeader(REQUEST_URL, TEST_JSON, USERNAME, PASSWORD);
160         StepVerifier.create(returnedMono)
161             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
162     }
163 }