2 * ============LICENSE_START=======================================================
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
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.services;
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;
43 import java.util.UUID;
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;
58 public class VidServiceImplTest {
60 @Mock(answer = Answers.RETURNS_MOCKS)
61 private AsdcClient asdcClientMock;
63 @Mock(answer = Answers.RETURNS_MOCKS)
64 private ToscaParserImpl2 toscaParserMock;
67 private FeatureManager featureManager;
70 private HttpResponse<String> httpResponse;
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)
81 private final Map<Service, ServiceModel> pseudoModelByService =
82 pseudoServiceByUuid.values().stream()
83 .collect(toMap(service -> service, service -> mock(ServiceModel.class)));
84 private VidServiceImpl vidService;
86 private ServiceModel expectedServiceModelForUuid(UUID uuid) {
87 final ServiceModel serviceModel = pseudoModelByService.get(pseudoServiceByUuid.get(uuid));
88 assertThat(serviceModel, is(not(nullValue())));
93 public void initMocks() throws AsdcCatalogException, SdcToscaParserException, IllegalAccessException {
94 MockitoAnnotations.initMocks(this);
96 vidService = new VidServiceImpl(asdcClientMock, featureManager);
97 FieldUtils.writeField(vidService, "toscaParser", toscaParserMock, true);
99 when(featureManager.isActive(Features.FLAG_SERVICE_MODEL_CACHE)).thenReturn(true);
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]));
106 public void whenGetServiceMultipleTimes_asdcIsCalledOnlyOnce() throws AsdcCatalogException {
107 vidService.getService(uuid1.toString());
108 vidService.getService(uuid1.toString());
109 vidService.getService(uuid1.toString());
111 verify(asdcClientMock, times(1)).getService(uuid1);
115 public void whenGetServiceTwiceWithResetBetween_asdcIsCalledTwice() throws AsdcCatalogException {
116 vidService.getService(uuid1.toString());
117 vidService.invalidateServiceCache();
118 vidService.getService(uuid1.toString());
120 verify(asdcClientMock, times(2)).getService(uuid1);
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());
129 assertThat(service1, sameInstance(expectedServiceModelForUuid(uuid1)));
130 assertThat(service1, sameInstance(service2));
131 assertThat(service1, sameInstance(service3));
135 public void cache_provide_correct_serviceModel() throws AsdcCatalogException {
136 ServiceModel service2 = vidService.getService(uuid2.toString());
137 assertThat(service2, sameInstance(expectedServiceModelForUuid(uuid2)));
139 ServiceModel service3 = vidService.getService(uuid3.toString());
140 assertThat(service3, sameInstance(expectedServiceModelForUuid(uuid3)));
142 UUID nonExisting = UUID.randomUUID();
143 ServiceModel service4 = vidService.getService(nonExisting.toString());
144 assertThat(service4, is(nullValue()));
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());
154 public void shouldCheckConnectionToSdc() {
155 when(asdcClientMock.checkSDCConnectivity()).thenReturn(httpResponse);
156 when(httpResponse.isSuccessful()).thenReturn(true);
157 when(httpResponse.getBody()).thenReturn("sampleBody");
160 ExternalComponentStatus externalComponentStatus = vidService.probeSDCConnection();
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"));
169 public void shouldProperlyHandleNotWorkingSDCConnection(){
170 when(asdcClientMock.checkSDCConnectivity()).thenThrow(new RuntimeException("not working"));
172 ExternalComponentStatus externalComponentStatus = vidService.probeSDCConnection();
174 assertThat(externalComponentStatus.isAvailable(), is(false));
175 assertThat(externalComponentStatus.getMetadata().getDescription(),containsString("not working"));