Un-SNAPSHOT all modules while build
[vid.git] / vid-app-common / src / test / java / org / onap / vid / asdc / rest / SdcRestClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.vid.asdc.rest;
22
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;
32
33 import java.io.InputStream;
34 import java.nio.file.Path;
35 import java.util.UUID;
36
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;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class SdcRestClientTest {
45
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";
51
52
53     @Mock
54     private SyncRestClient mockedSyncRestClient;
55
56     @Mock
57     private HttpResponse<Object> httpResponse;
58
59     @Mock
60     private HttpResponse<InputStream> httpStreamResponse;
61
62     @Mock
63     private InputStream inputStream;
64
65     private UUID randomId;
66
67     private Service sampleService;
68
69     private SdcRestClient restClient;
70
71
72     @Before
73     public void setUp() {
74         randomId = UUID.randomUUID();
75         sampleService = createTestService();
76         restClient = new SdcRestClient(SAMPLE_BASE_URL, SAMPLE_AUTH, mockedSyncRestClient);
77     }
78
79
80     @Test
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);
85
86         Service service = restClient.getService(randomId);
87
88
89         assertThat(service, is(sampleService));
90     }
91
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());
96
97         restClient.getService(randomId);
98     }
99
100
101     @Test
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);
106
107
108         Path serviceToscaModel = restClient.getServiceToscaModel(randomId);
109
110
111         assertThat(serviceToscaModel, notNullValue());
112         assertThat(serviceToscaModel.toFile().isFile(), is(true));
113
114         serviceToscaModel.toFile().deleteOnExit();
115     }
116
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());
121
122
123         restClient.getServiceToscaModel(randomId);
124     }
125
126
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);
132         return service;
133     }
134
135 }