aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/controller/AaiServiceInstanceStandardQueryControllerTest.java
blob: 9a5ee3ecb6dfe5f2728ecf4e5cce6e61d3a79a1b (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
package org.onap.vid.controller;

import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.onap.vid.aai.util.ServiceInstanceStandardQuery;
import org.onap.vid.asdc.AsdcCatalogException;
import org.onap.vid.model.Service;
import org.onap.vid.model.ServiceModel;
import org.onap.vid.model.VidNotions;
import org.onap.vid.model.VidNotions.ModelCategory;
import org.onap.vid.properties.Features;
import org.onap.vid.services.VidService;
import org.springframework.mock.web.MockHttpServletRequest;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.togglz.core.manager.FeatureManager;

import java.util.UUID;

import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

public class AaiServiceInstanceStandardQueryControllerTest {

    @InjectMocks
    private AaiServiceInstanceStandardQueryController aaiServiceInstanceStandardQueryController;

    @Mock
    private FeatureManager featureManager;

    @Mock
    private VidService sdcService;

    @Mock(answer = Answers.RETURNS_MOCKS)
    private ServiceInstanceStandardQuery serviceInstanceStandardQuery;

    //Don't use initMocks with @BeforeMethod
    //because AaiServiceInstanceStandardQueryController contains final members that can not be injected twice
    //See https://stackoverflow.com/questions/20046210/mockito-injectmocks-strange-behaviour-with-final-fields?answertab=active#tab-top
    @BeforeClass
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }

    @AfterMethod
    public void resetMocks() {
        reset(sdcService, featureManager, serviceInstanceStandardQuery);
    }

    @Test
    public void getNetworksToVlansByServiceInstance_given5G_PROVIDER_NETWORK_aaiIsAccessed() throws AsdcCatalogException {
        //  - turn on FLAG_PRESENT_PROVIDER_NETWORKS
        //  - mock an model with 5G_PROVIDER_NETWORK
        //  - request it's AAI network->vlan mapping
        //  - assert that AAI was accessed

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

        final UUID randomModelUuid = UUID.randomUUID();
        mockServiceModel(ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL, randomModelUuid);

        doGetNetworksToVlansByServiceInstance(randomModelUuid);

        verify(serviceInstanceStandardQuery).fetchServiceInstance(any(), any(), any());
    }

    @Test
    public void getNetworksToVlansByServiceInstance_givenNon5G_PROVIDER_NETWORK_aaiIsNotAccessed() throws AsdcCatalogException {
        //  - turn on FLAG_PRESENT_PROVIDER_NETWORKS
        //  - mock an model without 5G_PROVIDER_NETWORK (i.e. OTHER)
        //  - request it's AAI network->vlan mapping
        //  - assert that AAI was not accessed
        //  - empty result was responded

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

        final UUID randomModelUuid = UUID.randomUUID();
        mockServiceModel(ModelCategory.OTHER, randomModelUuid);

        assertThat(doGetNetworksToVlansByServiceInstance(randomModelUuid).serviceNetworks, hasSize(0));
        verifyZeroInteractions(serviceInstanceStandardQuery);
    }

    @Test
    public void isModelOf5g_givenServiceWithFabricConfiguration_returnTrue() throws AsdcCatalogException {
        final UUID randomModelUuid = UUID.randomUUID();
        mockServiceModel(ModelCategory.IS_5G_FABRIC_CONFIGURATION_MODEL, randomModelUuid, VidNotions.InstantiationUI.SERVICE_WITH_FABRIC_CONFIGURATION);

        assertTrue(aaiServiceInstanceStandardQueryController.isModelOf5g(randomModelUuid));
    }

    private void mockServiceModel(ModelCategory modelCategory, UUID randomModelUuid) throws AsdcCatalogException {
        mockServiceModel(modelCategory, randomModelUuid, VidNotions.InstantiationUI.LEGACY);
    }

    private void mockServiceModel(ModelCategory modelCategory, UUID randomModelUuid, VidNotions.InstantiationUI instantiationUI) throws AsdcCatalogException {
        ServiceModel mockedModel = mock(ServiceModel.class);
        Service mockedService = mock(Service.class);
        when(mockedModel.getService()).thenReturn(mockedService);
        when(mockedService.getVidNotions()).thenReturn(
                new VidNotions(instantiationUI, modelCategory, VidNotions.InstantiationUI.LEGACY)
        );

        when(sdcService.getService(randomModelUuid.toString())).thenReturn(mockedModel);
    }

    private AaiServiceInstanceStandardQueryController.VlansByNetworksHierarchy doGetNetworksToVlansByServiceInstance(UUID randomModelUuid) throws AsdcCatalogException {
        return aaiServiceInstanceStandardQueryController.getNetworksToVlansByServiceInstance(
                new MockHttpServletRequest(),
                randomModelUuid,
                "my global customer id",
                "my service type",
                "my instance id");
    }
}