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