Extend probe mechanism
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / VidServiceImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T 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.services;
22
23 import com.google.common.collect.ImmutableMap;
24 import io.joshworks.restclient.http.HttpResponse;
25 import org.apache.commons.lang3.reflect.FieldUtils;
26 import org.mockito.Answers;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
30 import org.onap.vid.asdc.AsdcCatalogException;
31 import org.onap.vid.asdc.AsdcClient;
32 import org.onap.vid.asdc.beans.Service;
33 import org.onap.vid.asdc.parser.ToscaParserImpl2;
34 import org.onap.vid.model.ServiceModel;
35 import org.onap.vid.model.probes.ExternalComponentStatus;
36 import org.onap.vid.model.probes.HttpRequestMetadata;
37 import org.onap.vid.properties.Features;
38 import org.testng.annotations.BeforeMethod;
39 import org.testng.annotations.Test;
40 import org.togglz.core.manager.FeatureManager;
41
42 import java.util.Map;
43 import java.util.UUID;
44
45 import static java.util.stream.Collectors.toMap;
46 import static org.hamcrest.CoreMatchers.containsString;
47 import static org.hamcrest.CoreMatchers.is;
48 import static org.hamcrest.Matchers.not;
49 import static org.hamcrest.Matchers.nullValue;
50 import static org.hamcrest.core.IsSame.sameInstance;
51 import static org.junit.Assert.assertThat;
52 import static org.mockito.Mockito.any;
53 import static org.mockito.Mockito.mock;
54 import static org.mockito.Mockito.times;
55 import static org.mockito.Mockito.verify;
56 import static org.mockito.Mockito.when;
57
58 public class VidServiceImplTest {
59
60     @Mock(answer = Answers.RETURNS_MOCKS)
61     private AsdcClient asdcClientMock;
62
63     @Mock(answer = Answers.RETURNS_MOCKS)
64     private ToscaParserImpl2 toscaParserMock;
65
66     @Mock
67     private FeatureManager featureManager;
68
69     @Mock
70     private HttpResponse<String> httpResponse;
71
72     private final UUID uuid1 = UUID.randomUUID();
73     private final UUID uuid2 = UUID.randomUUID();
74     private final UUID uuid3 = UUID.randomUUID();
75     private final Map<UUID, Service> pseudoServiceByUuid = ImmutableMap.of(
76             uuid1, mock(Service.class),
77             uuid2, mock(Service.class),
78             uuid3, mock(Service.class)
79     );
80
81     private final Map<Service, ServiceModel> pseudoModelByService =
82             pseudoServiceByUuid.values().stream()
83                     .collect(toMap(service -> service, service -> mock(ServiceModel.class)));
84     private VidServiceImpl vidService;
85
86     private ServiceModel expectedServiceModelForUuid(UUID uuid) {
87         final ServiceModel serviceModel = pseudoModelByService.get(pseudoServiceByUuid.get(uuid));
88         assertThat(serviceModel, is(not(nullValue())));
89         return serviceModel;
90     }
91
92     @BeforeMethod
93     public void initMocks() throws AsdcCatalogException, SdcToscaParserException, IllegalAccessException {
94         MockitoAnnotations.initMocks(this);
95
96         vidService = new VidServiceImpl(asdcClientMock, featureManager);
97         FieldUtils.writeField(vidService, "toscaParser", toscaParserMock, true);
98
99         when(featureManager.isActive(Features.FLAG_SERVICE_MODEL_CACHE)).thenReturn(true);
100
101         when(asdcClientMock.getService(any())).thenAnswer(invocation -> pseudoServiceByUuid.get(invocation.getArguments()[0]));
102         when(toscaParserMock.makeServiceModel(any(), any())).thenAnswer(invocation -> pseudoModelByService.get(invocation.getArguments()[1]));
103     }
104
105     @Test
106     public void whenGetServiceMultipleTimes_asdcIsCalledOnlyOnce() throws AsdcCatalogException {
107         vidService.getService(uuid1.toString());
108         vidService.getService(uuid1.toString());
109         vidService.getService(uuid1.toString());
110
111         verify(asdcClientMock, times(1)).getService(uuid1);
112     }
113
114     @Test
115     public void whenGetServiceTwiceWithResetBetween_asdcIsCalledTwice() throws AsdcCatalogException {
116         vidService.getService(uuid1.toString());
117         vidService.invalidateServiceCache();
118         vidService.getService(uuid1.toString());
119
120         verify(asdcClientMock, times(2)).getService(uuid1);
121     }
122
123     @Test
124     public void cache_saves_service_model_correctly() throws AsdcCatalogException {
125         ServiceModel service1 = vidService.getService(uuid1.toString());
126         ServiceModel service2 = vidService.getService(uuid1.toString());
127         ServiceModel service3 = vidService.getService(uuid1.toString());
128
129         assertThat(service1, sameInstance(expectedServiceModelForUuid(uuid1)));
130         assertThat(service1, sameInstance(service2));
131         assertThat(service1, sameInstance(service3));
132     }
133
134     @Test
135     public void cache_provide_correct_serviceModel() throws AsdcCatalogException {
136         ServiceModel service2 = vidService.getService(uuid2.toString());
137         assertThat(service2, sameInstance(expectedServiceModelForUuid(uuid2)));
138
139         ServiceModel service3 = vidService.getService(uuid3.toString());
140         assertThat(service3, sameInstance(expectedServiceModelForUuid(uuid3)));
141
142         UUID nonExisting = UUID.randomUUID();
143         ServiceModel service4 = vidService.getService(nonExisting.toString());
144         assertThat(service4, is(nullValue()));
145     }
146
147     @Test(expectedExceptions = AsdcCatalogException.class, expectedExceptionsMessageRegExp = "someMessage")
148     public void whenAsdcClientThrowAsdcCatalogException_thenGetServiceAlsoThrowIt() throws AsdcCatalogException {
149         when(asdcClientMock.getServiceToscaModel(any())).thenThrow(new AsdcCatalogException("someMessage"));
150         vidService.getService(uuid1.toString());
151     }
152
153     @Test
154     public void shouldCheckConnectionToSdc() {
155         when(asdcClientMock.checkSDCConnectivity()).thenReturn(httpResponse);
156         when(httpResponse.isSuccessful()).thenReturn(true);
157         when(httpResponse.getBody()).thenReturn("sampleBody");
158
159
160         ExternalComponentStatus externalComponentStatus = vidService.probeSDCConnection();
161
162         assertThat(externalComponentStatus.isAvailable(), is(true));
163         assertThat(externalComponentStatus.getComponent(), is(ExternalComponentStatus.Component.SDC));
164         HttpRequestMetadata metadata = (HttpRequestMetadata) externalComponentStatus.getMetadata();
165         assertThat(metadata.getRawData(), is("sampleBody"));
166     }
167
168     @Test
169     public void shouldProperlyHandleNotWorkingSDCConnection(){
170         when(asdcClientMock.checkSDCConnectivity()).thenThrow(new RuntimeException("not working"));
171
172         ExternalComponentStatus externalComponentStatus = vidService.probeSDCConnection();
173
174         assertThat(externalComponentStatus.isAvailable(), is(false));
175         assertThat(externalComponentStatus.getMetadata().getDescription(),containsString("not working"));
176     }
177 }
178