Merge "Removed dependency from lombok"
[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 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.runners.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.Matchers.any;
41 import static org.mockito.Matchers.anyMapOf;
42 import static org.mockito.Matchers.matches;
43 import static org.mockito.Mockito.when;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class SdcRestClientTest {
47
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";
53
54
55     @Mock
56     private SyncRestClient mockedSyncRestClient;
57
58     @Mock
59     private HttpResponse<Object> httpResponse;
60
61     @Mock
62     private HttpResponse<InputStream> httpStreamResponse;
63
64     @Mock
65     private InputStream inputStream;
66
67     private UUID randomId;
68
69     private Service sampleService;
70
71     private SdcRestClient restClient;
72
73
74     @Before
75     public void setUp() {
76         randomId = UUID.randomUUID();
77         sampleService = createTestService();
78         restClient = new SdcRestClient(SAMPLE_BASE_URL, SAMPLE_AUTH, mockedSyncRestClient);
79     }
80
81
82     @Test
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);
87
88         Service service = restClient.getService(randomId);
89
90
91         assertThat(service, is(sampleService));
92     }
93
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());
98
99         restClient.getService(randomId);
100     }
101
102
103     @Test
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);
108
109
110         Path serviceToscaModel = restClient.getServiceToscaModel(randomId);
111
112
113         assertThat(serviceToscaModel, notNullValue());
114         assertThat(serviceToscaModel.toFile().isFile(), is(true));
115
116         serviceToscaModel.toFile().deleteOnExit();
117     }
118
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());
123
124
125         restClient.getServiceToscaModel(randomId);
126     }
127
128
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);
134         return service;
135     }
136
137 }