1 package org.onap.vid.asdc.rest;
3 import org.apache.commons.lang3.exception.ExceptionUtils;
4 import org.onap.vid.testUtils.TestUtils;
5 import org.testng.annotations.DataProvider;
6 import org.testng.annotations.Test;
8 import javax.ws.rs.NotFoundException;
9 import javax.ws.rs.ProcessingException;
10 import javax.ws.rs.client.Client;
12 import java.util.UUID;
13 import java.util.function.Consumer;
15 import static org.hamcrest.CoreMatchers.instanceOf;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.mockito.Matchers.any;
18 import static org.mockito.Mockito.when;
19 import static org.testng.AssertJUnit.fail;
21 public class RestfulAsdcClientTest {
24 public static Object[][] javaxExceptions() {
26 return new Object[][] {
27 {NotFoundException.class, (Consumer<Client>) javaxClientMock ->
28 when(javaxClientMock.target(any(URI.class))).thenThrow(
29 new NotFoundException("HTTP 404 Not Found"))},
30 {ProcessingException.class, (Consumer<Client>) javaxClientMock ->
31 when(javaxClientMock.target(any(URI.class))).thenThrow(
32 new ProcessingException("java.net.ConnectException: Connection refused: connect"))},
37 @Test(dataProvider = "javaxExceptions")
38 public void whenJavaxClientThrowException_thenExceptionRethrown(Class<? extends Throwable> expectedType, Consumer<Client> setupMocks) throws Exception {
41 this test -> RestfulAsdcClient -> javax's Client
43 In this test, *RestfulAsdcClient* is under test (actual implementation is used), while javax's Client is
44 mocked to return pseudo-responses or - better - throw exceptions.
48 TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks();
49 Client javaxClientMock = mocks.getFakeClient();
51 // prepare real RestfulAsdcClient (Under test)
52 RestfulAsdcClient restfulAsdcClient = new RestfulAsdcClient.Builder(javaxClientMock, new URI(""))
57 setupMocks.accept(javaxClientMock);
60 restfulAsdcClient.getServiceToscaModel(UUID.randomUUID());
61 } catch (Exception e) {
62 assertThat("root cause incorrect for " + ExceptionUtils.getStackTrace(e), ExceptionUtils.getRootCause(e), instanceOf(expectedType));
66 fail("exception shall rethrown by getServiceToscaModel once javax client throw exception ");