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 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;
43 import java.util.UUID;
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.*;
50 public class AaiServiceInstanceStandardQueryControllerTest {
53 private AaiServiceInstanceStandardQueryController aaiServiceInstanceStandardQueryController;
56 private FeatureManager featureManager;
59 private VidService sdcService;
62 private AAIServiceTree aaiServiceTree;
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
69 public void initMocks() {
70 MockitoAnnotations.initMocks(this);
74 public void resetMocks() {
75 reset(sdcService, featureManager);
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
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)));
88 final UUID randomModelUuid = UUID.randomUUID();
89 mockServiceModel(ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL, randomModelUuid);
91 doGetNetworksToVlansByServiceInstance(randomModelUuid);
93 verify(aaiServiceTree).buildAAITree(any(), any(), any(HttpMethod.class), any(), anyBoolean());
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
104 when(featureManager.isActive(Features.FLAG_PRESENT_PROVIDER_NETWORKS_ASSOCIATIONS)).thenReturn(true);
106 final UUID randomModelUuid = UUID.randomUUID();
107 mockServiceModel(ModelCategory.OTHER, randomModelUuid);
109 assertThat(doGetNetworksToVlansByServiceInstance(randomModelUuid).serviceNetworks, hasSize(0));
110 verifyZeroInteractions(aaiServiceTree);
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);
118 assertTrue(aaiServiceInstanceStandardQueryController.isModelOf5g(randomModelUuid));
121 private void mockServiceModel(ModelCategory modelCategory, UUID randomModelUuid) throws AsdcCatalogException {
122 mockServiceModel(modelCategory, randomModelUuid, VidNotions.InstantiationUI.LEGACY);
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)
133 when(sdcService.getService(randomModelUuid.toString())).thenReturn(mockedModel);
136 private AaiServiceInstanceStandardQueryController.VlansByNetworksHierarchy doGetNetworksToVlansByServiceInstance(UUID randomModelUuid) throws AsdcCatalogException {
137 return aaiServiceInstanceStandardQueryController.getNetworksToVlansByServiceInstance(
138 new MockHttpServletRequest(),
140 "my global customer id",