Merge "Fix bug in A1 clients"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / 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.policyagent.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     @BeforeAll
56     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());
62     }
63
64     @AfterAll
65     static void tearDown() throws IOException {
66         mockWebServer.shutdown();
67     }
68
69     @Test
70     void testGetNoError() {
71         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
72             .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
73             .setBody(TEST_JSON));
74
75         Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
76         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
77     }
78
79     @Test
80     void testGetError() {
81         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
82
83         Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
84         StepVerifier.create(returnedMono)
85             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
86     }
87
88     @Test
89     void testPutNoError() {
90         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
91             .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
92             .setBody(TEST_JSON));
93
94         Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
95         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
96     }
97
98     @Test
99     void testPutError() {
100         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
101
102         Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
103         StepVerifier.create(returnedMono)
104             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
105     }
106
107     @Test
108     void testDeleteNoError() {
109         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE));
110
111         Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
112         StepVerifier.create(returnedMono).expectNext("").expectComplete().verify();
113     }
114
115     @Test
116     void testDeleteError() {
117         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
118
119         Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
120         StepVerifier.create(returnedMono)
121             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
122     }
123
124     @Test
125     void testPostNoError() {
126         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
127             .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
128             .setBody(TEST_JSON));
129
130         Mono<String> returnedMono = clientUnderTest.post(REQUEST_URL, TEST_JSON);
131         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
132     }
133
134     @Test
135     void testPostError() {
136         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
137
138         Mono<String> returnedMono = clientUnderTest.post(REQUEST_URL, TEST_JSON);
139         StepVerifier.create(returnedMono)
140             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
141     }
142
143     @Test
144     void testPostWithAuthHeaderNoError() {
145         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
146             .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
147             .setBody(TEST_JSON));
148
149         Mono<String> returnedMono = clientUnderTest.postWithAuthHeader(REQUEST_URL, TEST_JSON, USERNAME, PASSWORD);
150         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
151     }
152
153     @Test
154     void testPostWithAuthHeaderError() {
155         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
156
157         Mono<String> returnedMono = clientUnderTest.postWithAuthHeader(REQUEST_URL, TEST_JSON, USERNAME, PASSWORD);
158         StepVerifier.create(returnedMono)
159             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
160     }
161 }