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