diff options
Diffstat (limited to 'vid-app-common/src/test/java/org/onap/vid/asdc/rest/RestfulAsdcClientTest.java')
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/asdc/rest/RestfulAsdcClientTest.java | 248 |
1 files changed, 46 insertions, 202 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/asdc/rest/RestfulAsdcClientTest.java b/vid-app-common/src/test/java/org/onap/vid/asdc/rest/RestfulAsdcClientTest.java index c1833bb7..75b84b2d 100644 --- a/vid-app-common/src/test/java/org/onap/vid/asdc/rest/RestfulAsdcClientTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/asdc/rest/RestfulAsdcClientTest.java @@ -1,225 +1,69 @@ package org.onap.vid.asdc.rest; -import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.Collection; -import java.util.Map; -import java.util.Properties; -import java.util.UUID; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.onap.vid.testUtils.TestUtils; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.SSLSession; +import javax.ws.rs.NotFoundException; +import javax.ws.rs.ProcessingException; import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; - -import org.junit.Before; -import org.junit.Test; -import org.onap.vid.asdc.AsdcCatalogException; -import org.onap.vid.asdc.beans.Artifact; -import org.onap.vid.asdc.beans.Resource; -import org.onap.vid.asdc.beans.Service; +import java.net.URI; +import java.util.UUID; +import java.util.function.Consumer; -import nu.xom.Builder; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.when; +import static org.testng.AssertJUnit.fail; public class RestfulAsdcClientTest { - private RestfulAsdcClient createTestSubject() { - return new RestfulAsdcClient.Builder(restClient, uri).auth(auth) - .build(); + @DataProvider + public static Object[][] javaxExceptions() { + + return new Object[][] { + {NotFoundException.class, (Consumer<Client>) javaxClientMock -> + when(javaxClientMock.target(any(URI.class))).thenThrow( + new NotFoundException("HTTP 404 Not Found"))}, + {ProcessingException.class, (Consumer<Client>) javaxClientMock -> + when(javaxClientMock.target(any(URI.class))).thenThrow( + new ProcessingException("java.net.ConnectException: Connection refused: connect"))}, + }; } - /** The rest client. */ - private Client restClient; - - /** The uri. */ - private URI uri; - - /** The properties. */ - private Properties properties; - - /** The auth. */ - private String auth; - - /** - * Sets the up. - * - * @throws URISyntaxException - * the URI syntax exception - * @throws IOException - * Signals that an I/O exception has occurred. - */ - @Before - public void setUp() throws URISyntaxException, IOException { - final InputStream propertiesFile = getClass().getClassLoader() - .getResourceAsStream("asdc.properties"); - properties = new Properties(); - properties.load(propertiesFile); + @Test(dataProvider = "javaxExceptions") + public void whenJavaxClientThrowException_thenExceptionRethrown(Class<? extends Throwable> expectedType, Consumer<Client> setupMocks) throws Exception { + /* + Call chain is like: + this test -> RestfulAsdcClient -> javax's Client - final String protocol = properties.getProperty("protocol", "http"); + In this test, *RestfulAsdcClient* is under test (actual implementation is used), while javax's Client is + mocked to return pseudo-responses or - better - throw exceptions. + */ - restClient = ClientBuilder.newBuilder() - .hostnameVerifier(new HostnameVerifier() { + // prepare mocks + TestUtils.JavaxRsClientMocks mocks = new TestUtils.JavaxRsClientMocks(); + Client javaxClientMock = mocks.getFakeClient(); - @Override - public boolean verify(String arg0, SSLSession arg1) { - return true; - } - }) + // prepare real RestfulAsdcClient (Under test) + RestfulAsdcClient restfulAsdcClient = new RestfulAsdcClient.Builder(javaxClientMock, new URI("")) + .auth("") .build(); - uri = new URI(protocol + "://" + properties.getProperty("host", "localhost") + ":" - + properties.getProperty("port", "80") + "/"); - auth = properties.getProperty("auth"); - } - - @Test - public void testGetResource() throws Exception { - RestfulAsdcClient testSubject; - UUID uuid = UUID.fromString("123e4567-e89b-12d3-a456-556642440000"); - Resource result; - - // default test - testSubject = createTestSubject(); - try { - result = testSubject.getResource(uuid); - } catch (Exception e) { - - } - } - - @Test - public void testGetResourceArtifact() throws Exception { - RestfulAsdcClient testSubject; - UUID resourceUuid = UUID.fromString("123e4567-e89b-12d3-a456-556642440000"); - UUID artifactUuid = UUID.fromString("123e4567-e89b-12d3-a456-556642440000"); - Artifact result; - - // default test - testSubject = createTestSubject(); - try { - result = testSubject.getResourceArtifact(resourceUuid, artifactUuid); - } catch (Exception e) { - - } - } - - @Test - public void testGetResources() throws Exception { - RestfulAsdcClient testSubject; - Collection<Resource> result; - - // default test - testSubject = createTestSubject(); - try { - result = testSubject.getResources(); - } catch (Exception e) { - - } - } - - @Test - public void testGetResources_1() throws Exception { - RestfulAsdcClient testSubject; - Map<String, String[]> filter = null; - Collection<Resource> result; - - // default test - testSubject = createTestSubject(); - try { - result = testSubject.getResources(filter); - } catch (Exception e) { - - } - } - - @Test - public void testGetResourceToscaModel() throws Exception { - RestfulAsdcClient testSubject; - UUID resourceUuid = UUID.fromString("123e4567-e89b-12d3-a456-556642440000"); - - // default test - testSubject = createTestSubject(); - try { - testSubject.getResourceToscaModel(resourceUuid); - } catch (Exception e) { - } - } + /// TEST: + setupMocks.accept(javaxClientMock); - @Test - public void testGetService() throws Exception { - RestfulAsdcClient testSubject; - UUID uuid = UUID.fromString("123e4567-e89b-12d3-a456-556642440000"); - Service result; - - // default test - testSubject = createTestSubject(); try { - result = testSubject.getService(uuid); + restfulAsdcClient.getServiceToscaModel(UUID.randomUUID()); } catch (Exception e) { - + assertThat("root cause incorrect for " + ExceptionUtils.getStackTrace(e), ExceptionUtils.getRootCause(e), instanceOf(expectedType)); + return; //OK } - } - - @Test - public void testGetServiceArtifact() throws Exception { - RestfulAsdcClient testSubject; - UUID serviceUuid = UUID.fromString("123e4567-e89b-12d3-a456-556642440000"); - UUID artifactUuid = UUID.fromString("123e4567-e89b-12d3-a456-556642440000"); - Artifact result; - - // default test - testSubject = createTestSubject(); - try { - result = testSubject.getServiceArtifact(serviceUuid, artifactUuid); - } catch (Exception e) { - - } - } - - @Test - public void testGetServices() throws Exception { - RestfulAsdcClient testSubject; - Collection<Service> result; - // default test - testSubject = createTestSubject(); - try { - result = testSubject.getServices(); - } catch (Exception e) { - - } - } - - @Test - public void testGetServices_1() throws Exception { - RestfulAsdcClient testSubject; - Map<String, String[]> filter = null; - Collection<Service> result; - - // default test - testSubject = createTestSubject(); - try { - result = testSubject.getServices(filter); - } catch (Exception e) { - - } - } - - @Test - public void testGetServiceToscaModel() throws Exception { - RestfulAsdcClient testSubject; - UUID serviceUuid = UUID.fromString("123e4567-e89b-12d3-a456-556642440000"); - - // default test - testSubject = createTestSubject(); - try { - testSubject.getServiceToscaModel(serviceUuid); - } catch (Exception e) { - - } + fail("exception shall rethrown by getServiceToscaModel once javax client throw exception "); } -}
\ No newline at end of file +} |