aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/services/VidServiceImplTest.java
blob: 7dbd622afdbaba7b6d50bbf4888951bd91d3f438 (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
package org.onap.vid.services;

import com.google.common.collect.ImmutableMap;
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.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.*;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.*;

public class VidServiceImplTest {

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

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

    @Mock
    FeatureManager featureManager;

    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());
    }

}