Extend probe mechanism
[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.AsdcClient;
31 import org.onap.vid.asdc.beans.Service;
32 import org.onap.vid.client.SyncRestClient;
33
34 import java.io.InputStream;
35 import java.nio.file.Path;
36 import java.util.Collections;
37 import java.util.UUID;
38
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;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class SdcRestClientTest {
51
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";
57
58
59     @Mock
60     private SyncRestClient mockedSyncRestClient;
61
62     @Mock
63     private HttpResponse<Object> httpResponse;
64
65     @Mock
66     private HttpResponse<InputStream> httpStreamResponse;
67
68     @Mock
69     private HttpResponse<String> httpStringResponse;
70
71     @Mock
72     private InputStream inputStream;
73
74     private UUID randomId;
75
76     private Service sampleService;
77
78     private SdcRestClient restClient;
79
80
81     @Before
82     public void setUp() {
83         randomId = UUID.randomUUID();
84         sampleService = createTestService();
85         restClient = new SdcRestClient(SAMPLE_BASE_URL, SAMPLE_AUTH, mockedSyncRestClient);
86     }
87
88
89     @Test
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);
94
95         Service service = restClient.getService(randomId);
96
97
98         assertThat(service, is(sampleService));
99     }
100
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());
105
106         restClient.getService(randomId);
107     }
108
109
110     @Test
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);
115
116
117         Path serviceToscaModel = restClient.getServiceToscaModel(randomId);
118
119
120         assertThat(serviceToscaModel, notNullValue());
121         assertThat(serviceToscaModel.toFile().isFile(), is(true));
122
123         serviceToscaModel.toFile().deleteOnExit();
124     }
125
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());
130
131
132         restClient.getServiceToscaModel(randomId);
133     }
134
135     @Test
136     public void shouldCallSDCHealthCheck() {
137         when(mockedSyncRestClient.get(contains(AsdcClient.URIS.HEALTH_CHECK_ENDPOINT), anyMap(),
138                 eq(Collections.emptyMap()), eq(String.class))).thenReturn(httpStringResponse);
139
140
141         HttpResponse<String> stringHttpResponse = restClient.checkSDCConnectivity();
142
143         assertThat(httpStringResponse, is(stringHttpResponse));
144     }
145
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);
151         return service;
152     }
153
154 }