aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java
diff options
context:
space:
mode:
authorWojciech Sliwka <wojciech.sliwka@nokia.com>2018-08-20 15:45:03 +0200
committerWojciech Sliwka <wojciech.sliwka@nokia.com>2018-08-24 12:26:03 +0200
commit17e8b54e83547d8dc37c335c5df1b989f8b5ff3c (patch)
tree87201581f26048e7c88a1d9339392f7c11d05b8e /vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java
parent5adc0b0ee92d1e39ddaacd27e67a327dd5988f11 (diff)
Replace sdc client
Replace old SDC client to use genericRestClient Change-Id: I9b3b567871ac3f319742fca9457e42b6d4db0a8a Issue-ID: VID-268 Signed-off-by: Wojciech Sliwka <wojciech.sliwka@nokia.com>
Diffstat (limited to 'vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java')
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java137
1 files changed, 137 insertions, 0 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
new file mode 100644
index 000000000..c1d6ab78a
--- /dev/null
+++ b/vid-app-common/src/test/java/org/onap/vid/asdc/rest/SdcRestClientTest.java
@@ -0,0 +1,137 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * VID
+ * ================================================================================
+ * Copyright (C) 2018 Nokia 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+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.runners.MockitoJUnitRunner;
+import org.onap.vid.asdc.AsdcCatalogException;
+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.UUID;
+
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyMapOf;
+import static org.mockito.Matchers.matches;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class SdcRestClientTest {
+
+ private static final String SAMPLE_SERVICE_NAME = "sampleService";
+ private static final String SAMPLE_BASE_URL = "baseUrl";
+ private static final String SAMPLE_AUTH = "sampleAuth";
+ private static final String METADATA_URL_REGEX = ".*sdc/v1/catalog/services/%s/metadata";
+ private static final String MODEL_URL_REGEX = ".*sdc/v1/catalog/services/%s/toscaModel";
+
+
+ @Mock
+ private SyncRestClient mockedSyncRestClient;
+
+ @Mock
+ private HttpResponse<Object> httpResponse;
+
+ @Mock
+ private HttpResponse<InputStream> httpStreamResponse;
+
+ @Mock
+ private InputStream inputStream;
+
+ private UUID randomId;
+
+ private Service sampleService;
+
+ private SdcRestClient restClient;
+
+
+ @Before
+ public void setUp() {
+ randomId = UUID.randomUUID();
+ sampleService = createTestService();
+ restClient = new SdcRestClient(SAMPLE_BASE_URL, SAMPLE_AUTH, mockedSyncRestClient);
+ }
+
+
+ @Test
+ public void shouldReturnServiceForGivenUUID() throws AsdcCatalogException {
+ String url = String.format(METADATA_URL_REGEX, randomId);
+ when(mockedSyncRestClient.get(matches(url), anyMapOf(String.class, String.class), anyMapOf(String.class, String.class), any())).thenReturn(httpResponse);
+ when(httpResponse.getBody()).thenReturn(sampleService);
+
+ Service service = restClient.getService(randomId);
+
+
+ assertThat(service, is(sampleService));
+ }
+
+ @Test(expected = AsdcCatalogException.class)
+ public void shouldRaiseAsdcExceptionWhenClientFails() throws AsdcCatalogException {
+ String url = String.format(METADATA_URL_REGEX, randomId);
+ when(mockedSyncRestClient.get(matches(url), anyMapOf(String.class, String.class), anyMapOf(String.class, String.class), any())).thenThrow(new RuntimeException());
+
+ restClient.getService(randomId);
+ }
+
+
+ @Test
+ public void shouldProperlyDownloadAndCopyToscaArtifact() throws AsdcCatalogException {
+ String url = String.format(MODEL_URL_REGEX, randomId);
+ when(mockedSyncRestClient.getStream(matches(url), any(), any())).thenReturn(httpStreamResponse);
+ when(httpStreamResponse.getBody()).thenReturn(inputStream);
+
+
+ Path serviceToscaModel = restClient.getServiceToscaModel(randomId);
+
+
+ assertThat(serviceToscaModel, notNullValue());
+ assertThat(serviceToscaModel.toFile().isFile(), is(true));
+
+ serviceToscaModel.toFile().deleteOnExit();
+ }
+
+ @Test(expected = AsdcCatalogException.class)
+ public void shouldRaiseAsdcExceptionWhenDownloadFails() throws AsdcCatalogException {
+ String url = String.format(MODEL_URL_REGEX, randomId);
+ when(mockedSyncRestClient.getStream(matches(url), anyMapOf(String.class, String.class), anyMapOf(String.class, String.class))).thenThrow(new RuntimeException());
+
+
+ restClient.getServiceToscaModel(randomId);
+ }
+
+
+ private Service createTestService() {
+ Service service = new Service();
+ service.setInvariantUUID(randomId.toString());
+ service.setUuid(randomId.toString());
+ service.setName(SAMPLE_SERVICE_NAME);
+ return service;
+ }
+
+} \ No newline at end of file