aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/services/VidServiceImplTest.java
blob: 65712b423295fbbb10718d92d12adda47391be65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.vid.services;

import static java.util.stream.Collectors.toMap;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.IsSame.sameInstance;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.AssertJUnit.assertTrue;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.joshworks.restclient.http.HttpResponse;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Map;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.ws.rs.ProcessingException;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.commons.lang3.tuple.Triple;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
import org.onap.vid.aai.ExceptionWithRequestInfo;
import org.onap.vid.aai.HttpResponseWithRequestInfo;
import org.onap.vid.asdc.AsdcCatalogException;
import org.onap.vid.asdc.AsdcClient;
import org.onap.vid.asdc.beans.Service;
import org.onap.vid.asdc.parser.ToscaParserImpl2;
import org.onap.vid.model.ServiceModel;
import org.onap.vid.model.probes.ExternalComponentStatus;
import org.onap.vid.model.probes.HttpRequestMetadata;
import org.onap.vid.properties.Features;
import org.onap.vid.testUtils.TestUtils;
import org.springframework.http.HttpMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.togglz.core.manager.FeatureManager;

public class VidServiceImplTest {

    @Mock(answer = Answers.RETURNS_MOCKS)
    private AsdcClient asdcClientMock;

    @Mock(answer = Answers.RETURNS_MOCKS)
    private ToscaParserImpl2 toscaParserMock;

    @Mock
    private FeatureManager featureManager;

    @Mock
    private HttpResponse<String> httpResponse;

    private final UUID uuid1 = UUID.randomUUID();
    private final UUID uuid2 = UUID.randomUUID();
    private final UUID uuid3 = UUID.randomUUID();
    private final Map<UUID, Service> pseudoServiceByUuid = ImmutableMap.of(
            uuid1, mock(Service.class),
            uuid2, mock(Service.class),
            uuid3, mock(Service.class)
    );

    private final Map<Service, ServiceModel> pseudoModelByService =
            pseudoServiceByUuid.values().stream()
                    .collect(toMap(service -> service, service -> mock(ServiceModel.class)));
    private VidServiceImpl vidService;

    private ServiceModel expectedServiceModelForUuid(UUID uuid) {
        final ServiceModel serviceModel = pseudoModelByService.get(pseudoServiceByUuid.get(uuid));
        assertThat(serviceModel, is(not(nullValue())));
        return serviceModel;
    }

    @BeforeMethod
    public void initMocks() throws AsdcCatalogException, SdcToscaParserException, IllegalAccessException {
        MockitoAnnotations.initMocks(this);

        vidService = new VidServiceImpl(asdcClientMock, toscaParserMock, featureManager);
        FieldUtils.writeField(vidService, "toscaParser", toscaParserMock, true);

        when(featureManager.isActive(Features.FLAG_SERVICE_MODEL_CACHE)).thenReturn(true);

        when(asdcClientMock.getService(any())).thenAnswer(invocation -> pseudoServiceByUuid.get(invocation.getArguments()[0]));
        when(toscaParserMock.makeServiceModel(any(), any())).thenAnswer(invocation -> pseudoModelByService.get(invocation.getArguments()[1]));
    }

    @Test
    public void whenGetServiceMultipleTimes_asdcIsCalledOnlyOnce() throws AsdcCatalogException {
        vidService.getService(uuid1.toString());
        vidService.getService(uuid1.toString());
        vidService.getService(uuid1.toString());

        verify(asdcClientMock, times(1)).getService(uuid1);
    }

    @Test
    public void whenGetServiceTwiceWithResetBetween_asdcIsCalledTwice() throws AsdcCatalogException {
        vidService.getService(uuid1.toString());
        vidService.invalidateServiceCache();
        vidService.getService(uuid1.toString());

        verify(asdcClientMock, times(2)).getService(uuid1);
    }

    @Test
    public void cache_saves_service_model_correctly() throws AsdcCatalogException {
        ServiceModel service1 = vidService.getService(uuid1.toString());
        ServiceModel service2 = vidService.getService(uuid1.toString());
        ServiceModel service3 = vidService.getService(uuid1.toString());

        assertThat(service1, sameInstance(expectedServiceModelForUuid(uuid1)));
        assertThat(service1, sameInstance(service2));
        assertThat(service1, sameInstance(service3));
    }

    @Test
    public void cache_provide_correct_serviceModel() throws AsdcCatalogException {
        ServiceModel service2 = vidService.getService(uuid2.toString());
        assertThat(service2, sameInstance(expectedServiceModelForUuid(uuid2)));

        ServiceModel service3 = vidService.getService(uuid3.toString());
        assertThat(service3, sameInstance(expectedServiceModelForUuid(uuid3)));

        UUID nonExisting = UUID.randomUUID();
        ServiceModel service4 = vidService.getService(nonExisting.toString());
        assertThat(service4, is(nullValue()));
    }

    @Test(expectedExceptions = AsdcCatalogException.class, expectedExceptionsMessageRegExp = "someMessage")
    public void whenAsdcClientThrowAsdcCatalogException_thenGetServiceAlsoThrowIt() throws AsdcCatalogException {
        when(asdcClientMock.getServiceToscaModel(any())).thenThrow(new AsdcCatalogException("someMessage"));
        vidService.getService(uuid1.toString());
    }

    @Test
    public void shouldCheckConnectionToSdc() {
        mockGoodSdcConnectivityResponse();

        ExternalComponentStatus externalComponentStatus = vidService.probeComponent();

        assertThat(externalComponentStatus.isAvailable(), is(true));
        assertThat(externalComponentStatus.getComponent(), is(ExternalComponentStatus.Component.SDC));
        HttpRequestMetadata metadata = (HttpRequestMetadata) externalComponentStatus.getMetadata();
        assertThat(metadata.getRawData(), is("sampleBody"));
    }

    private void mockGoodSdcConnectivityResponse() {
        when(asdcClientMock.checkSDCConnectivity()).thenReturn(httpResponse);
        when(httpResponse.isSuccessful()).thenReturn(true);
        when(httpResponse.getBody()).thenReturn("sampleBody");
    }

    @Test
    public void shouldProperlyHandleNotWorkingSDCConnection(){
        when(asdcClientMock.checkSDCConnectivity()).thenThrow(new RuntimeException("not working"));

        ExternalComponentStatus externalComponentStatus = vidService.probeComponent();

        assertThat(externalComponentStatus.isAvailable(), is(false));
        assertThat(externalComponentStatus.getMetadata().getDescription(),containsString("not working"));
    }

    @Test
    public void shouldNotProbeBySdcConnectionIfProbeUuidConfigured() throws Exception {
        TestUtils.testWithSystemProperty(
            "probe.sdc.model.uuid",
            UUID.randomUUID().toString(),
            ()->{
                mockGoodSdcConnectivityResponse(); //no mocking for probeSdcByGettingModel
                ExternalComponentStatus externalComponentStatus = vidService.probeComponent();
                assertThat(externalComponentStatus.isAvailable(), is(false));
            }
        );
    }


    @Test
    public void givenProbeUUID_whenAsdcClientReturnNormal_thenProbeComponentReturnAvailableAnswer() throws Exception {

        final UUID uuidForProbe = UUID.randomUUID();
        final HttpResponse<InputStream> mockResponse = mock(HttpResponse.class);
        responseSetupper(mockResponse, 200, new ByteArrayInputStream(RandomUtils.nextBytes(66000)));
        when(asdcClientMock.getServiceInputStream(eq(uuidForProbe), eq(true))).thenReturn(
            new HttpResponseWithRequestInfo(mockResponse, "my pretty url", HttpMethod.GET)
        );

        TestUtils.testWithSystemProperty(
            "probe.sdc.model.uuid",
            uuidForProbe.toString(),
            ()-> {

                ExternalComponentStatus sdcComponentStatus = vidService.probeComponent();
                assertTrue(sdcComponentStatus.isAvailable());
                assertThat(sdcComponentStatus.getComponent(), is(ExternalComponentStatus.Component.SDC));
                assertThat(sdcComponentStatus.getMetadata().getDescription(), equalTo("OK"));
                assertThat(sdcComponentStatus.getMetadata(), instanceOf(HttpRequestMetadata.class));

                HttpRequestMetadata componentStatusMetadata = ((HttpRequestMetadata) sdcComponentStatus.getMetadata());
                assertThat(componentStatusMetadata.getHttpMethod(), equalTo(HttpMethod.GET));
                assertThat(componentStatusMetadata.getHttpCode(), equalTo(200));
                assertThat(componentStatusMetadata.getUrl(), equalTo("my pretty url"));
            }
        );
    }

    private static void responseSetupper(HttpResponse<InputStream> r, Integer httpCode, InputStream body) {
        when(r.getStatus()).thenReturn(httpCode);
        when(r.getRawBody()).thenReturn(body);
    }

    @DataProvider
    public static Object[][] executionResults() {
        final BiConsumer<AsdcClient, HttpResponse<InputStream>> defaultClientSetup = (c, r) ->
            when(c.getServiceInputStream(any(), eq(true))).thenReturn(new HttpResponseWithRequestInfo(r, "foo url", HttpMethod.GET));

        final ByteArrayInputStream defaultResponseBody = new ByteArrayInputStream(RandomUtils.nextBytes(200));

        return Stream.<Triple<HttpRequestMetadata, BiConsumer<AsdcClient, HttpResponse<InputStream>>, Consumer<HttpResponse<InputStream>>>>of(

            Triple.of(
                new HttpRequestMetadata(null, 200, null, null, "IllegalStateException", 0),
                defaultClientSetup,
                r -> {
                    when(r.getStatus()).thenReturn(200);
                    when(r.getRawBody()).thenThrow(new IllegalStateException("good news for people who love bad news"));
                }
            ),

            Triple.of(
                new HttpRequestMetadata(null, 200, null, null, "error reading model", 0),
                defaultClientSetup,
                r -> responseSetupper(r, 200, new ByteArrayInputStream(new byte[0]))
            ),
//
            Triple.of(
                new HttpRequestMetadata(null, 200, null, null, "NullPointerException", 0),
                defaultClientSetup,
                r -> responseSetupper(r, 200, null)
            ),
//
            Triple.of(
                new HttpRequestMetadata(null, 0, "bar url", null, "java.net.ConnectException: Connection refused", 0),
                (c, r) ->
                    when(c.getServiceInputStream(any(), eq(true))).thenThrow(new ExceptionWithRequestInfo(HttpMethod.GET, "bar url",
                        new ProcessingException("java.net.ConnectException: Connection refused: connect"))),
                r -> responseSetupper(r, 200, defaultResponseBody)
            ),

            Triple.of(
                new HttpRequestMetadata(null, 500, null, null, "error while retrieving model", 0),
                defaultClientSetup,
                r -> responseSetupper(r, 500, defaultResponseBody)
            ),

            Triple.of(
                new HttpRequestMetadata(null, 404, null, null, "updating vid probe configuration", 0),
                defaultClientSetup,
                r -> responseSetupper(r, 404, defaultResponseBody)
            )

        ).map(t -> ImmutableList.of(t.getLeft(), t.getMiddle(), t.getRight()).toArray()).collect(Collectors.toList()).toArray(new Object[][]{});
    }

    @Test(dataProvider = "executionResults")
    public void whenClientReturnWithError_thenProbeSdcByGettingModelDescribes(HttpRequestMetadata expectedMetadata,
        BiConsumer<AsdcClient, HttpResponse<InputStream>> clientSetup,
        Consumer<HttpResponse<InputStream>> responseSetup) {

        final HttpResponse<InputStream> mockResponse = mock(HttpResponse.class);
        clientSetup.accept(asdcClientMock, mockResponse);
        responseSetup.accept(mockResponse);

        ExternalComponentStatus sdcComponentStatus = vidService.probeSdcByGettingModel(UUID.randomUUID());
        assertThat(sdcComponentStatus.getComponent(), is(ExternalComponentStatus.Component.SDC));
        assertThat(sdcComponentStatus.getMetadata(), instanceOf(HttpRequestMetadata.class));

        HttpRequestMetadata componentStatusMetadata = ((HttpRequestMetadata) sdcComponentStatus.getMetadata());
        assertThat(componentStatusMetadata.getDescription(), containsString(defaultIfNull(expectedMetadata.getDescription(), "OK")));
        assertThat(componentStatusMetadata.getHttpMethod(), equalTo(defaultIfNull(expectedMetadata.getHttpMethod(), HttpMethod.GET)));
        assertThat(componentStatusMetadata.getHttpCode(), equalTo(expectedMetadata.getHttpCode()));
        assertThat(componentStatusMetadata.getUrl(), equalTo(defaultIfNull(expectedMetadata.getUrl(), "foo url")));

        assertThat(sdcComponentStatus.isAvailable(), is(false));
    }

}