import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
-import org.oransc.policyagent.clients.AsyncRestClient.AsyncRestClientException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
+import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import reactor.util.Loggers;
-public class AsyncRestClientTest {
+class AsyncRestClientTest {
private static final String BASE_URL = "BaseUrl";
private static final String REQUEST_URL = "/test";
private static final String USERNAME = "username";
private static AsyncRestClient clientUnderTest;
@BeforeAll
- public static void init() {
+ static void init() {
// skip a lot of unnecessary logs from MockWebServer
InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);
Loggers.useJdkLoggers();
}
@AfterAll
- public static void tearDown() throws IOException {
+ static void tearDown() throws IOException {
mockWebServer.shutdown();
}
@Test
- public void testGetNoError() {
+ void testGetNoError() {
mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
.setBody(TEST_JSON));
}
@Test
- public void testGetError() {
+ void testGetError() {
mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
- StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof AsyncRestClientException)
- .verify();
+ StepVerifier.create(returnedMono)
+ .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
}
@Test
- public void testPutNoError() {
+ void testPutNoError() {
mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
.setBody(TEST_JSON));
}
@Test
- public void testPutError() {
+ void testPutError() {
mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
- StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof AsyncRestClientException)
- .verify();
+ StepVerifier.create(returnedMono)
+ .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
}
@Test
- public void testDeleteNoError() {
+ void testDeleteNoError() {
mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE));
Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
- StepVerifier.create(returnedMono).expectComplete().verify();
+ StepVerifier.create(returnedMono).expectNext("").expectComplete().verify();
}
@Test
- public void testDeleteError() {
+ void testDeleteError() {
mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
- StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof AsyncRestClientException)
- .verify();
+ StepVerifier.create(returnedMono)
+ .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
}
@Test
- public void testPostNoError() {
+ void testPostNoError() {
mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
.setBody(TEST_JSON));
}
@Test
- public void testPostError() {
+ void testPostError() {
mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
Mono<String> returnedMono = clientUnderTest.post(REQUEST_URL, TEST_JSON);
- StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof AsyncRestClientException)
- .verify();
+ StepVerifier.create(returnedMono)
+ .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
}
@Test
- public void testPostWithAuthHeaderNoError() {
+ void testPostWithAuthHeaderNoError() {
mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
.setBody(TEST_JSON));
}
@Test
- public void testPostWithAuthHeaderError() {
+ void testPostWithAuthHeaderError() {
mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
Mono<String> returnedMono = clientUnderTest.postWithAuthHeader(REQUEST_URL, TEST_JSON, USERNAME, PASSWORD);
- StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof AsyncRestClientException)
- .verify();
+ StepVerifier.create(returnedMono)
+ .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
}
}