2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 - 2019 Nokia Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.asdc.rest;
23 import io.joshworks.restclient.http.HttpResponse;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.onap.vid.asdc.AsdcCatalogException;
30 import org.onap.vid.asdc.beans.Service;
31 import org.onap.vid.client.SyncRestClient;
33 import java.io.InputStream;
34 import java.nio.file.Path;
35 import java.util.UUID;
37 import static org.hamcrest.CoreMatchers.notNullValue;
38 import static org.hamcrest.MatcherAssert.assertThat;
39 import static org.hamcrest.core.Is.is;
40 import static org.mockito.ArgumentMatchers.*;
41 import static org.mockito.Mockito.when;
43 @RunWith(MockitoJUnitRunner.class)
44 public class SdcRestClientTest {
46 private static final String SAMPLE_SERVICE_NAME = "sampleService";
47 private static final String SAMPLE_BASE_URL = "baseUrl";
48 private static final String SAMPLE_AUTH = "sampleAuth";
49 private static final String METADATA_URL_REGEX = ".*sdc/v1/catalog/services/%s/metadata";
50 private static final String MODEL_URL_REGEX = ".*sdc/v1/catalog/services/%s/toscaModel";
54 private SyncRestClient mockedSyncRestClient;
57 private HttpResponse<Object> httpResponse;
60 private HttpResponse<InputStream> httpStreamResponse;
63 private InputStream inputStream;
65 private UUID randomId;
67 private Service sampleService;
69 private SdcRestClient restClient;
74 randomId = UUID.randomUUID();
75 sampleService = createTestService();
76 restClient = new SdcRestClient(SAMPLE_BASE_URL, SAMPLE_AUTH, mockedSyncRestClient);
81 public void shouldReturnServiceForGivenUUID() throws AsdcCatalogException {
82 String url = String.format(METADATA_URL_REGEX, randomId);
83 when(mockedSyncRestClient.get(matches(url), anyMap(), anyMap(), any())).thenReturn(httpResponse);
84 when(httpResponse.getBody()).thenReturn(sampleService);
86 Service service = restClient.getService(randomId);
89 assertThat(service, is(sampleService));
92 @Test(expected = AsdcCatalogException.class)
93 public void shouldRaiseAsdcExceptionWhenClientFails() throws AsdcCatalogException {
94 String url = String.format(METADATA_URL_REGEX, randomId);
95 when(mockedSyncRestClient.get(matches(url), anyMap(), anyMap(), any())).thenThrow(new RuntimeException());
97 restClient.getService(randomId);
102 public void shouldProperlyDownloadAndCopyToscaArtifact() throws AsdcCatalogException {
103 String url = String.format(MODEL_URL_REGEX, randomId);
104 when(mockedSyncRestClient.getStream(matches(url), any(), any())).thenReturn(httpStreamResponse);
105 when(httpStreamResponse.getBody()).thenReturn(inputStream);
108 Path serviceToscaModel = restClient.getServiceToscaModel(randomId);
111 assertThat(serviceToscaModel, notNullValue());
112 assertThat(serviceToscaModel.toFile().isFile(), is(true));
114 serviceToscaModel.toFile().deleteOnExit();
117 @Test(expected = AsdcCatalogException.class)
118 public void shouldRaiseAsdcExceptionWhenDownloadFails() throws AsdcCatalogException {
119 String url = String.format(MODEL_URL_REGEX, randomId);
120 when(mockedSyncRestClient.getStream(matches(url), anyMap(), anyMap())).thenThrow(new RuntimeException());
123 restClient.getServiceToscaModel(randomId);
127 private Service createTestService() {
128 Service service = new Service();
129 service.setInvariantUUID(randomId.toString());
130 service.setUuid(randomId.toString());
131 service.setName(SAMPLE_SERVICE_NAME);