aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/services/VidServiceImplTest.java
blob: 3cd3aa8a674c71cf02239d92fff8acc2a67e2455 (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
/*-
 * ============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 com.google.common.collect.ImmutableMap;
import io.joshworks.restclient.http.HttpResponse;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
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.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.togglz.core.manager.FeatureManager;

import java.util.Map;
import java.util.UUID;

import static java.util.stream.Collectors.toMap;
import static org.hamcrest.CoreMatchers.containsString;
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.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;

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, 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() {
        when(asdcClientMock.checkSDCConnectivity()).thenReturn(httpResponse);
        when(httpResponse.isSuccessful()).thenReturn(true);
        when(httpResponse.getBody()).thenReturn("sampleBody");


        ExternalComponentStatus externalComponentStatus = vidService.probeSDCConnection();

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

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

        ExternalComponentStatus externalComponentStatus = vidService.probeSDCConnection();

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