diff options
author | Eylon Malin <eylon.malin@intl.att.com> | 2019-08-29 17:19:33 +0300 |
---|---|---|
committer | Eylon Malin <eylon.malin@intl.att.com> | 2019-08-29 17:19:33 +0300 |
commit | 1206f28338523e1b1b6b89be824b21e615a7fa8e (patch) | |
tree | e28db270fa6a73b6aa580c95cd23947af888d62e | |
parent | 955722b6ddf8ddeab3d1904f5514a5be9226a59f (diff) |
add ut getServiceToscaModel rethrow Exception
Issue-ID: VID-378
Signed-off-by: Eylon Malin <eylon.malin@intl.att.com>
Change-Id: Ia8a649a281cbc687bbec4f25e6bd67e1681276bc
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java | 87 |
1 files changed, 67 insertions, 20 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java b/vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java index a20571785..5eaaf9335 100644 --- a/vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java @@ -3,6 +3,7 @@ * VID * ================================================================================ * Copyright (C) 2018 - 2019 Nokia Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,33 +21,39 @@ package org.onap.vid.asdc.rest; -import io.joshworks.restclient.http.HttpResponse; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import org.onap.vid.asdc.AsdcCatalogException; -import org.onap.vid.asdc.AsdcClient; -import org.onap.vid.asdc.beans.Service; -import org.onap.vid.client.SyncRestClient; - -import java.io.InputStream; -import java.nio.file.Path; -import java.util.Collections; -import java.util.UUID; - +import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyMap; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.contains; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.matches; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.testng.AssertJUnit.fail; + +import io.joshworks.restclient.http.HttpResponse; +import java.io.InputStream; +import java.nio.file.Path; +import java.util.Collections; +import java.util.UUID; +import java.util.function.Consumer; +import javax.ws.rs.NotFoundException; +import javax.ws.rs.ProcessingException; +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.vid.asdc.AsdcCatalogException; +import org.onap.vid.asdc.AsdcClient; +import org.onap.vid.asdc.beans.Service; +import org.onap.vid.client.SyncRestClient; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; -@RunWith(MockitoJUnitRunner.class) public class SdcRestClientTest { private static final String SAMPLE_SERVICE_NAME = "sampleService"; @@ -78,8 +85,9 @@ public class SdcRestClientTest { private SdcRestClient restClient; - @Before + @BeforeClass public void setUp() { + MockitoAnnotations.initMocks(this); randomId = UUID.randomUUID(); sampleService = createTestService(); restClient = new SdcRestClient(SAMPLE_BASE_URL, SAMPLE_AUTH, mockedSyncRestClient); @@ -98,7 +106,7 @@ public class SdcRestClientTest { assertThat(service, is(sampleService)); } - @Test(expected = AsdcCatalogException.class) + @Test( expectedExceptions = AsdcCatalogException.class) public void shouldRaiseAsdcExceptionWhenClientFails() throws AsdcCatalogException { String url = String.format(METADATA_URL_REGEX, randomId); when(mockedSyncRestClient.get(matches(url), anyMap(), anyMap(), any())).thenThrow(new RuntimeException()); @@ -123,7 +131,7 @@ public class SdcRestClientTest { serviceToscaModel.toFile().deleteOnExit(); } - @Test(expected = AsdcCatalogException.class) + @Test(expectedExceptions = AsdcCatalogException.class) public void shouldRaiseAsdcExceptionWhenDownloadFails() throws AsdcCatalogException { String url = String.format(MODEL_URL_REGEX, randomId); when(mockedSyncRestClient.getStream(matches(url), anyMap(), anyMap())).thenThrow(new RuntimeException()); @@ -151,4 +159,43 @@ public class SdcRestClientTest { return service; } + @DataProvider + public static Object[][] javaxExceptions() { + + return new Object[][] { + {NotFoundException.class, (Consumer<SyncRestClient>) restClient -> + when(restClient.getStream(anyString(), anyMap(), anyMap())).thenThrow( + new NotFoundException("HTTP 404 Not Found")) + }, + {ProcessingException.class, (Consumer<SyncRestClient>) restClient -> + when(restClient.getStream(anyString(), anyMap(), anyMap())).thenThrow( + new ProcessingException("java.net.ConnectException: Connection refused: connect"))}, + }; + } + + + @Test(dataProvider = "javaxExceptions") + public void whenJavaxClientThrowException_then_getServiceToscaModelRethrowException(Class<? extends Throwable> expectedType, Consumer<SyncRestClient> setupMocks) throws Exception { + /* + Call chain is like: + this test -> RestfulAsdcClient -> javax's Client + + 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. + */ + + /// TEST: + SyncRestClient syncRestClient = mock(SyncRestClient.class); + setupMocks.accept(syncRestClient); + + try { + new SdcRestClient(SAMPLE_BASE_URL, SAMPLE_AUTH, syncRestClient).getServiceToscaModel(UUID.randomUUID()); + } catch (Exception e) { + assertThat("root cause incorrect for " + ExceptionUtils.getStackTrace(e), ExceptionUtils.getRootCause(e), instanceOf(expectedType)); + return; //OK + } + + fail("exception shall rethrown by getServiceToscaModel once javax client throw exception "); + } + } |