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.AsdcClient;
31 import org.onap.vid.asdc.beans.Service;
32 import org.onap.vid.client.SyncRestClient;
34 import java.io.InputStream;
35 import java.nio.file.Path;
36 import java.util.Collections;
37 import java.util.UUID;
39 import static org.hamcrest.CoreMatchers.notNullValue;
40 import static org.hamcrest.MatcherAssert.assertThat;
41 import static org.hamcrest.core.Is.is;
42 import static org.mockito.ArgumentMatchers.any;
43 import static org.mockito.ArgumentMatchers.anyMap;
44 import static org.mockito.ArgumentMatchers.contains;
45 import static org.mockito.ArgumentMatchers.eq;
46 import static org.mockito.ArgumentMatchers.matches;
47 import static org.mockito.Mockito.when;
49 @RunWith(MockitoJUnitRunner.class)
50 public class SdcRestClientTest {
52 private static final String SAMPLE_SERVICE_NAME = "sampleService";
53 private static final String SAMPLE_BASE_URL = "baseUrl";
54 private static final String SAMPLE_AUTH = "sampleAuth";
55 private static final String METADATA_URL_REGEX = ".*sdc/v1/catalog/services/%s/metadata";
56 private static final String MODEL_URL_REGEX = ".*sdc/v1/catalog/services/%s/toscaModel";
60 private SyncRestClient mockedSyncRestClient;
63 private HttpResponse<Object> httpResponse;
66 private HttpResponse<InputStream> httpStreamResponse;
69 private HttpResponse<String> httpStringResponse;
72 private InputStream inputStream;
74 private UUID randomId;
76 private Service sampleService;
78 private SdcRestClient restClient;
83 randomId = UUID.randomUUID();
84 sampleService = createTestService();
85 restClient = new SdcRestClient(SAMPLE_BASE_URL, SAMPLE_AUTH, mockedSyncRestClient);
90 public void shouldReturnServiceForGivenUUID() throws AsdcCatalogException {
91 String url = String.format(METADATA_URL_REGEX, randomId);
92 when(mockedSyncRestClient.get(matches(url), anyMap(), anyMap(), any())).thenReturn(httpResponse);
93 when(httpResponse.getBody()).thenReturn(sampleService);
95 Service service = restClient.getService(randomId);
98 assertThat(service, is(sampleService));
101 @Test(expected = AsdcCatalogException.class)
102 public void shouldRaiseAsdcExceptionWhenClientFails() throws AsdcCatalogException {
103 String url = String.format(METADATA_URL_REGEX, randomId);
104 when(mockedSyncRestClient.get(matches(url), anyMap(), anyMap(), any())).thenThrow(new RuntimeException());
106 restClient.getService(randomId);
111 public void shouldProperlyDownloadAndCopyToscaArtifact() throws AsdcCatalogException {
112 String url = String.format(MODEL_URL_REGEX, randomId);
113 when(mockedSyncRestClient.getStream(matches(url), any(), any())).thenReturn(httpStreamResponse);
114 when(httpStreamResponse.getBody()).thenReturn(inputStream);
117 Path serviceToscaModel = restClient.getServiceToscaModel(randomId);
120 assertThat(serviceToscaModel, notNullValue());
121 assertThat(serviceToscaModel.toFile().isFile(), is(true));
123 serviceToscaModel.toFile().deleteOnExit();
126 @Test(expected = AsdcCatalogException.class)
127 public void shouldRaiseAsdcExceptionWhenDownloadFails() throws AsdcCatalogException {
128 String url = String.format(MODEL_URL_REGEX, randomId);
129 when(mockedSyncRestClient.getStream(matches(url), anyMap(), anyMap())).thenThrow(new RuntimeException());
132 restClient.getServiceToscaModel(randomId);
136 public void shouldCallSDCHealthCheck() {
137 when(mockedSyncRestClient.get(contains(AsdcClient.URIS.HEALTH_CHECK_ENDPOINT), anyMap(),
138 eq(Collections.emptyMap()), eq(String.class))).thenReturn(httpStringResponse);
141 HttpResponse<String> stringHttpResponse = restClient.checkSDCConnectivity();
143 assertThat(httpStringResponse, is(stringHttpResponse));
146 private Service createTestService() {
147 Service service = new Service();
148 service.setInvariantUUID(randomId.toString());
149 service.setUuid(randomId.toString());
150 service.setName(SAMPLE_SERVICE_NAME);