2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.controller;
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;
41 import java.util.UUID;
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.*;
48 public class AaiServiceInstanceStandardQueryControllerTest {
51 private AaiServiceInstanceStandardQueryController aaiServiceInstanceStandardQueryController;
54 private FeatureManager featureManager;
57 private VidService sdcService;
59 @Mock(answer = Answers.RETURNS_MOCKS)
60 private ServiceInstanceStandardQuery serviceInstanceStandardQuery;
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
66 public void initMocks() {
67 MockitoAnnotations.initMocks(this);
71 public void resetMocks() {
72 reset(sdcService, featureManager, serviceInstanceStandardQuery);
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
82 when(featureManager.isActive(Features.FLAG_PRESENT_PROVIDER_NETWORKS_ASSOCIATIONS)).thenReturn(true);
84 final UUID randomModelUuid = UUID.randomUUID();
85 mockServiceModel(ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL, randomModelUuid);
87 doGetNetworksToVlansByServiceInstance(randomModelUuid);
89 verify(serviceInstanceStandardQuery).fetchServiceInstance(any(), any(), any());
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
100 when(featureManager.isActive(Features.FLAG_PRESENT_PROVIDER_NETWORKS_ASSOCIATIONS)).thenReturn(true);
102 final UUID randomModelUuid = UUID.randomUUID();
103 mockServiceModel(ModelCategory.OTHER, randomModelUuid);
105 assertThat(doGetNetworksToVlansByServiceInstance(randomModelUuid).serviceNetworks, hasSize(0));
106 verifyZeroInteractions(serviceInstanceStandardQuery);
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);
114 assertTrue(aaiServiceInstanceStandardQueryController.isModelOf5g(randomModelUuid));
117 private void mockServiceModel(ModelCategory modelCategory, UUID randomModelUuid) throws AsdcCatalogException {
118 mockServiceModel(modelCategory, randomModelUuid, VidNotions.InstantiationUI.LEGACY);
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)
129 when(sdcService.getService(randomModelUuid.toString())).thenReturn(mockedModel);
132 private AaiServiceInstanceStandardQueryController.VlansByNetworksHierarchy doGetNetworksToVlansByServiceInstance(UUID randomModelUuid) throws AsdcCatalogException {
133 return aaiServiceInstanceStandardQueryController.getNetworksToVlansByServiceInstance(
134 new MockHttpServletRequest(),
136 "my global customer id",