f89895690bdd95b01e38eb9c78911374654041c7
[portal/nonrtric-controlpanel.git] / webapp-backend / src / test / java / org / oransc / portal / nonrtric / controlpanel / util / 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.portal.nonrtric.controlpanel.util;
22
23 import java.io.IOException;
24 import org.junit.jupiter.api.AfterAll;
25 import org.junit.jupiter.api.BeforeAll;
26 import org.junit.jupiter.api.Test;
27 import org.springframework.http.HttpHeaders;
28 import org.springframework.http.MediaType;
29 import org.springframework.http.ResponseEntity;
30 import org.springframework.web.reactive.function.client.WebClientResponseException;
31 import io.netty.util.internal.logging.InternalLoggerFactory;
32 import io.netty.util.internal.logging.JdkLoggerFactory;
33 import okhttp3.mockwebserver.MockResponse;
34 import okhttp3.mockwebserver.MockWebServer;
35 import reactor.core.publisher.Mono;
36 import reactor.test.StepVerifier;
37 import reactor.util.Loggers;
38
39 class AsyncRestClientTest {
40     private static final String BASE_URL = "BaseUrl";
41     private static final String REQUEST_URL = "/test";
42     private static final String TEST_JSON = "{\"type\":\"type1\"}";
43     private static final String USERNAME = "user";
44     private static final String PASSWORD = "pass";
45     private static final int SUCCESS_CODE = 200;
46     private static final int ERROR_CODE = 500;
47
48     private static MockWebServer mockWebServer;
49
50     private static AsyncRestClient clientUnderTest;
51
52     @BeforeAll
53     public static void init() {
54         // skip a lot of unnecessary logs from MockWebServer
55         InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);
56         Loggers.useJdkLoggers();
57         mockWebServer = new MockWebServer();
58         clientUnderTest = new AsyncRestClient(mockWebServer.url(BASE_URL).toString());
59     }
60
61     @AfterAll
62     public static void tearDown() throws IOException {
63         mockWebServer.shutdown();
64     }
65
66     @Test
67     void testGetNoError() {
68         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
69             .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
70             .setBody(TEST_JSON));
71
72         Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
73         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
74     }
75
76     @Test
77     void testGetError() {
78         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
79
80         Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
81         StepVerifier.create(returnedMono)
82             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
83     }
84
85     @Test
86     void testPutNoError() {
87         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
88             .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
89             .setBody(TEST_JSON));
90
91         Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
92         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
93     }
94
95     @Test
96     void testPutWithoutBodyNoError() {
97         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
98             .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
99
100         Mono<ResponseEntity<String>> returnedMono = clientUnderTest.putForEntity(REQUEST_URL);
101         StepVerifier.create(returnedMono).expectNextCount(1).expectComplete().verify();
102     }
103
104     @Test
105     void testPutError() {
106         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
107
108         Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
109         StepVerifier.create(returnedMono)
110             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
111     }
112
113     @Test
114     void testDeleteNoError() {
115         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE));
116
117         Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
118         StepVerifier.create(returnedMono).expectNext("").expectComplete().verify();
119     }
120
121     @Test
122     void testDeleteError() {
123         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
124
125         Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
126         StepVerifier.create(returnedMono)
127             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
128     }
129
130 }