2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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.runners.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.Matchers.any;
41 import static org.mockito.Matchers.anyMapOf;
42 import static org.mockito.Matchers.matches;
43 import static org.mockito.Mockito.when;
45 @RunWith(MockitoJUnitRunner.class)
46 public class SdcRestClientTest {
48 private static final String SAMPLE_SERVICE_NAME = "sampleService";
49 private static final String SAMPLE_BASE_URL = "baseUrl";
50 private static final String SAMPLE_AUTH = "sampleAuth";
51 private static final String METADATA_URL_REGEX = ".*sdc/v1/catalog/services/%s/metadata";
52 private static final String MODEL_URL_REGEX = ".*sdc/v1/catalog/services/%s/toscaModel";
56 private SyncRestClient mockedSyncRestClient;
59 private HttpResponse<Object> httpResponse;
62 private HttpResponse<InputStream> httpStreamResponse;
65 private InputStream inputStream;
67 private UUID randomId;
69 private Service sampleService;
71 private SdcRestClient restClient;
76 randomId = UUID.randomUUID();
77 sampleService = createTestService();
78 restClient = new SdcRestClient(SAMPLE_BASE_URL, SAMPLE_AUTH, mockedSyncRestClient);
83 public void shouldReturnServiceForGivenUUID() throws AsdcCatalogException {
84 String url = String.format(METADATA_URL_REGEX, randomId);
85 when(mockedSyncRestClient.get(matches(url), anyMapOf(String.class, String.class), anyMapOf(String.class, String.class), any())).thenReturn(httpResponse);
86 when(httpResponse.getBody()).thenReturn(sampleService);
88 Service service = restClient.getService(randomId);
91 assertThat(service, is(sampleService));
94 @Test(expected = AsdcCatalogException.class)
95 public void shouldRaiseAsdcExceptionWhenClientFails() throws AsdcCatalogException {
96 String url = String.format(METADATA_URL_REGEX, randomId);
97 when(mockedSyncRestClient.get(matches(url), anyMapOf(String.class, String.class), anyMapOf(String.class, String.class), any())).thenThrow(new RuntimeException());
99 restClient.getService(randomId);
104 public void shouldProperlyDownloadAndCopyToscaArtifact() throws AsdcCatalogException {
105 String url = String.format(MODEL_URL_REGEX, randomId);
106 when(mockedSyncRestClient.getStream(matches(url), any(), any())).thenReturn(httpStreamResponse);
107 when(httpStreamResponse.getBody()).thenReturn(inputStream);
110 Path serviceToscaModel = restClient.getServiceToscaModel(randomId);
113 assertThat(serviceToscaModel, notNullValue());
114 assertThat(serviceToscaModel.toFile().isFile(), is(true));
116 serviceToscaModel.toFile().deleteOnExit();
119 @Test(expected = AsdcCatalogException.class)
120 public void shouldRaiseAsdcExceptionWhenDownloadFails() throws AsdcCatalogException {
121 String url = String.format(MODEL_URL_REGEX, randomId);
122 when(mockedSyncRestClient.getStream(matches(url), anyMapOf(String.class, String.class), anyMapOf(String.class, String.class))).thenThrow(new RuntimeException());
125 restClient.getServiceToscaModel(randomId);
129 private Service createTestService() {
130 Service service = new Service();
131 service.setInvariantUUID(randomId.toString());
132 service.setUuid(randomId.toString());
133 service.setName(SAMPLE_SERVICE_NAME);