2a8db965045664ba862c5a5aa14589cce3510fa9
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / AaiServiceInstanceStandardQueryControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.vid.controller;
22
23 import org.mockito.Answers;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.onap.vid.aai.util.ServiceInstanceStandardQuery;
28 import org.onap.vid.asdc.AsdcCatalogException;
29 import org.onap.vid.model.Service;
30 import org.onap.vid.model.ServiceModel;
31 import org.onap.vid.model.VidNotions;
32 import org.onap.vid.model.VidNotions.ModelCategory;
33 import org.onap.vid.properties.Features;
34 import org.onap.vid.services.VidService;
35 import org.springframework.mock.web.MockHttpServletRequest;
36 import org.testng.annotations.AfterMethod;
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.Test;
39 import org.togglz.core.manager.FeatureManager;
40
41 import java.util.UUID;
42
43 import static org.hamcrest.Matchers.hasSize;
44 import static org.junit.Assert.assertThat;
45 import static org.junit.Assert.assertTrue;
46 import static org.mockito.Mockito.*;
47
48 public class AaiServiceInstanceStandardQueryControllerTest {
49
50     @InjectMocks
51     private AaiServiceInstanceStandardQueryController aaiServiceInstanceStandardQueryController;
52
53     @Mock
54     private FeatureManager featureManager;
55
56     @Mock
57     private VidService sdcService;
58
59     @Mock(answer = Answers.RETURNS_MOCKS)
60     private ServiceInstanceStandardQuery serviceInstanceStandardQuery;
61
62     //Don't use initMocks with @BeforeMethod
63     //because AaiServiceInstanceStandardQueryController contains final members that can not be injected twice
64     //See https://stackoverflow.com/questions/20046210/mockito-injectmocks-strange-behaviour-with-final-fields?answertab=active#tab-top
65     @BeforeClass
66     public void initMocks() {
67         MockitoAnnotations.initMocks(this);
68     }
69
70     @AfterMethod
71     public void resetMocks() {
72         reset(sdcService, featureManager, serviceInstanceStandardQuery);
73     }
74
75     @Test
76     public void getNetworksToVlansByServiceInstance_given5G_PROVIDER_NETWORK_aaiIsAccessed() throws AsdcCatalogException {
77         //  - turn on FLAG_PRESENT_PROVIDER_NETWORKS
78         //  - mock an model with 5G_PROVIDER_NETWORK
79         //  - request it's AAI network->vlan mapping
80         //  - assert that AAI was accessed
81
82         when(featureManager.isActive(Features.FLAG_PRESENT_PROVIDER_NETWORKS_ASSOCIATIONS)).thenReturn(true);
83
84         final UUID randomModelUuid = UUID.randomUUID();
85         mockServiceModel(ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL, randomModelUuid);
86
87         doGetNetworksToVlansByServiceInstance(randomModelUuid);
88
89         verify(serviceInstanceStandardQuery).fetchServiceInstance(any(), any(), any());
90     }
91
92     @Test
93     public void getNetworksToVlansByServiceInstance_givenNon5G_PROVIDER_NETWORK_aaiIsNotAccessed() throws AsdcCatalogException {
94         //  - turn on FLAG_PRESENT_PROVIDER_NETWORKS
95         //  - mock an model without 5G_PROVIDER_NETWORK (i.e. OTHER)
96         //  - request it's AAI network->vlan mapping
97         //  - assert that AAI was not accessed
98         //  - empty result was responded
99
100         when(featureManager.isActive(Features.FLAG_PRESENT_PROVIDER_NETWORKS_ASSOCIATIONS)).thenReturn(true);
101
102         final UUID randomModelUuid = UUID.randomUUID();
103         mockServiceModel(ModelCategory.OTHER, randomModelUuid);
104
105         assertThat(doGetNetworksToVlansByServiceInstance(randomModelUuid).serviceNetworks, hasSize(0));
106         verifyZeroInteractions(serviceInstanceStandardQuery);
107     }
108
109     @Test
110     public void isModelOf5g_givenServiceWithFabricConfiguration_returnTrue() throws AsdcCatalogException {
111         final UUID randomModelUuid = UUID.randomUUID();
112         mockServiceModel(ModelCategory.IS_5G_FABRIC_CONFIGURATION_MODEL, randomModelUuid, VidNotions.InstantiationUI.SERVICE_WITH_FABRIC_CONFIGURATION);
113
114         assertTrue(aaiServiceInstanceStandardQueryController.isModelOf5g(randomModelUuid));
115     }
116
117     private void mockServiceModel(ModelCategory modelCategory, UUID randomModelUuid) throws AsdcCatalogException {
118         mockServiceModel(modelCategory, randomModelUuid, VidNotions.InstantiationUI.LEGACY);
119     }
120
121     private void mockServiceModel(ModelCategory modelCategory, UUID randomModelUuid, VidNotions.InstantiationUI instantiationUI) throws AsdcCatalogException {
122         ServiceModel mockedModel = mock(ServiceModel.class);
123         Service mockedService = mock(Service.class);
124         when(mockedModel.getService()).thenReturn(mockedService);
125         when(mockedService.getVidNotions()).thenReturn(
126                 new VidNotions(instantiationUI, modelCategory, VidNotions.InstantiationUI.LEGACY)
127         );
128
129         when(sdcService.getService(randomModelUuid.toString())).thenReturn(mockedModel);
130     }
131
132     private AaiServiceInstanceStandardQueryController.VlansByNetworksHierarchy doGetNetworksToVlansByServiceInstance(UUID randomModelUuid) throws AsdcCatalogException {
133         return aaiServiceInstanceStandardQueryController.getNetworksToVlansByServiceInstance(
134                 new MockHttpServletRequest(),
135                 randomModelUuid,
136                 "my global customer id",
137                 "my service type",
138                 "my instance id");
139     }
140 }