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.asdc.parser;
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25 import org.apache.commons.lang3.tuple.Pair;
26 import org.jetbrains.annotations.NotNull;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.MockitoAnnotations;
31 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
32 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
33 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
34 import org.onap.sdc.toscaparser.api.NodeTemplate;
35 import org.onap.sdc.toscaparser.api.Property;
36 import org.onap.sdc.toscaparser.api.elements.Metadata;
37 import org.onap.vid.model.*;
38 import org.onap.vid.properties.Features;
39 import org.testng.annotations.AfterMethod;
40 import org.testng.annotations.BeforeClass;
41 import org.testng.annotations.DataProvider;
42 import org.testng.annotations.Test;
43 import org.togglz.core.manager.FeatureManager;
45 import java.util.LinkedHashMap;
46 import java.util.UUID;
47 import java.util.function.BiConsumer;
49 import static org.hamcrest.MatcherAssert.assertThat;
50 import static org.hamcrest.Matchers.hasProperty;
51 import static org.hamcrest.Matchers.is;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.when;
54 import static org.testng.Assert.assertEquals;
56 public class VidNotionsBuilderTest {
59 VidNotionsBuilder vidNotionsBuilder;
62 private FeatureManager featureManagerMock;
65 public void initMocks() {
66 MockitoAnnotations.initMocks(this);
71 Mockito.reset(featureManagerMock);
75 public void VLNetworkWithPropertyNetworkTechnologyOVS_UIHintIsPositive() {
76 ISdcCsarHelper csarHelper = mockForNonLegacyInstantiationUI();
78 assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.NETWORK_WITH_PROPERTY_NETWORK_TECHNOLOGY_EQUALS_STANDARD_SRIOV_OR_OVS));
79 assertThat(vidNotionsBuilder.suggestModelCategory(csarHelper) , is(VidNotions.ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL));
83 protected ISdcCsarHelper mockForNonLegacyInstantiationUI() {
84 ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
86 NodeTemplate nodeTemplate = mock(NodeTemplate.class);
88 when(nodeTemplate.getProperties()).thenReturn(new LinkedHashMap<>(ImmutableMap.of(
89 "dummy_val", mock(Property.class),
90 "network_technology", new Property(Pair.of("network_technology","ovs"))
93 when(csarHelper.getServiceVlList()).thenReturn(ImmutableList.of(nodeTemplate));
94 when(featureManagerMock.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)).thenReturn(true);
99 public static Object[][] anyAlacarteDataProvider() {
100 return new Object[][] {
101 {"A-La-Carte", VidNotions.InstantiationUI.ANY_ALACARTE_NEW_UI},
102 {"Macro", VidNotions.InstantiationUI.LEGACY},
106 @Test(dataProvider = "anyAlacarteDataProvider")
107 public void FLAG_EXP_ANY_ALACARTE_NEW_INSTANTIATION_UI_is_active_UIHintIsPositive(String instantiationType, VidNotions.InstantiationUI expectedInstantiationUI) {
108 when(featureManagerMock.isActive(Features.FLAG_EXP_ANY_ALACARTE_NEW_INSTANTIATION_UI)).thenReturn(true);
109 ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
110 when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(ImmutableMap.of(
111 "instantiationType", instantiationType
113 NodeTemplate nodeTemplate = mock(NodeTemplate.class);
115 when(nodeTemplate.getProperties()).thenReturn(new LinkedHashMap<>(ImmutableMap.of(
116 "dummy_val", mock(Property.class),
117 "network_technology", new Property(Pair.of("network_technology","ovs"))
120 when(csarHelper.getServiceVlList()).thenReturn(ImmutableList.of(nodeTemplate));
122 assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(expectedInstantiationUI));
126 public void VLNetworkWithPropertyNetworkTechnologyNot5G_UIHintIsNegative() {
127 ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
129 NodeTemplate nodeTemplate = mock(NodeTemplate.class);
131 when(nodeTemplate.getProperties()).thenReturn(new LinkedHashMap<>(ImmutableMap.of(
132 "dummy_val", mock(Property.class),
133 "network_technology", new Property(Pair.of("network_technology","old_value"))
136 when(csarHelper.getServiceVlList()).thenReturn(ImmutableList.of(nodeTemplate));
138 assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.LEGACY));
139 assertThat(vidNotionsBuilder.suggestModelCategory(csarHelper) , is(VidNotions.ModelCategory.OTHER));
143 public void withoutMocks_givenZippedToscaFile_hasAnyNetworkWithPropertyEqualsToAnyOfYieldsTrue() throws SdcToscaParserException {
144 SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
145 ISdcCsarHelper csarHelper = factory.getSdcCsarHelper(getClass().getClassLoader().getResource("service-vl-csar.zip").getPath(),false);
147 assertThat(vidNotionsBuilder.isALaCarte(csarHelper), is(false));
148 assertThat(vidNotionsBuilder.hasAnyNetworkWithPropertyEqualsToAnyOf(csarHelper, "unexpected_property_name"), is(false));
149 assertThat(vidNotionsBuilder.hasAnyNetworkWithPropertyEqualsToAnyOf(csarHelper, "network_technology","Standard-SR-IOV"), is(true));
150 assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.LEGACY));
154 //public void withoutMocks_givenZippedToscaFile_hasFabricConfigurationYieldsTrue() throws SdcToscaParserException {
155 // SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
156 // ISdcCsarHelper csarHelper = factory.getSdcCsarHelper(getClass().getClassLoader().getResource("service-fabric-configuration.zip").getPath(),false);
158 // assertThat(vidNotionsBuilder.isALaCarte(csarHelper), is(false));
159 // assertThat(vidNotionsBuilder.hasFabricConfiguration(csarHelper), is(true));
160 // assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.LEGACY));
165 public void uuidIsExactly1ffce89fEtc_UIHintIsPositive() {
166 ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
168 when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(ImmutableMap.of(
169 "UUID", "95eb2c44-bff2-4e8b-ad5d-8266870b7717"
171 when(featureManagerMock.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)).thenReturn(true);
172 assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.SERVICE_UUID_IS_1ffce89f_ef3f_4cbb_8b37_82134590c5de));
177 public static Object[][] trueAndFalse() {
178 return new Object[][] {{true}, {false}};
181 @Test(dataProvider = "trueAndFalse")
182 public void buildVidNotions_nullByFlag(boolean flagValue) {
183 ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
185 when(featureManagerMock.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)).thenReturn(flagValue);
186 assertThat(vidNotionsBuilder.buildVidNotions(csarHelper, null), hasProperty("instantiationUI", is(VidNotions.InstantiationUI.LEGACY)));
190 public static Object[][] ServiceRoleTypesDataProvider() {
191 return new Object[][] {
192 {"gROUPING", VidNotions.InstantiationUI.SERVICE_WITH_VNF_GROUPING},
193 {"", VidNotions.InstantiationUI.LEGACY},
197 @Test(dataProvider = "ServiceRoleTypesDataProvider")
198 public void testGetViewEditUITypeForResourceGroup(String serviceRole, VidNotions.InstantiationUI expectedViewEditUI) {
199 when(featureManagerMock.isActive(Features.FLAG_ASYNC_INSTANTIATION)).thenReturn(true);
200 when(featureManagerMock.isActive(Features.FLAG_1902_VNF_GROUPING)).thenReturn(true);
201 ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
202 when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(ImmutableMap.of(
203 "serviceRole", serviceRole
206 assertThat(vidNotionsBuilder.suggestViewEditUI(csarHelper, null), is(expectedViewEditUI));
210 public static Object[][] macroToViewEditDataProvider() {
211 return new Object[][] {
212 {"macro service + not excluded + needed flags are open", true, false, true, true, VidNotions.InstantiationUI.MACRO_SERVICE},
213 {"not macro service", false, false, true, true, VidNotions.InstantiationUI.LEGACY},
214 {"macro that shall be excluded because it has pnf", true, true, true, true, VidNotions.InstantiationUI.LEGACY},
215 {"macro service + FLAG_ASYNC_INSTANTIATION off", true, false, false, true, VidNotions.InstantiationUI.LEGACY},
216 {"macro service + FLAG_1902_NEW_VIEW_EDIT off", true, false, true, false, VidNotions.InstantiationUI.LEGACY},
220 @Test(dataProvider="macroToViewEditDataProvider")
221 public void whenServiceIsMacro_viewEditIsRight(
222 String testDescription,
225 boolean isFlagAsyncInstantiationActive,
226 boolean isFlag1902NewViewEdit,
227 VidNotions.InstantiationUI expectedViewEditUi) {
229 ISdcCsarHelper csarHelper = mock(ISdcCsarHelper.class);
230 ServiceModel serviceModel = mock(ServiceModel.class);
233 String instantiationType = isMacro ? ToscaParserImpl2.Constants.MACRO : ToscaParserImpl2.Constants.A_LA_CARTE;
234 Service service = mock(Service.class);
235 when(serviceModel.getService()).thenReturn(service);
236 when(service.getInstantiationType()).thenReturn(instantiationType);
237 when(featureManagerMock.isActive(Features.FLAG_ASYNC_INSTANTIATION)).thenReturn(isFlagAsyncInstantiationActive);
238 when(featureManagerMock.isActive(Features.FLAG_1902_NEW_VIEW_EDIT)).thenReturn(isFlag1902NewViewEdit);
240 //mock for isExcluded
242 when(serviceModel.getPnfs()).thenReturn(ImmutableMap.of("a", mock(Node.class)));
245 VidNotions.InstantiationUI result = vidNotionsBuilder.suggestViewEditUI(csarHelper, serviceModel);
246 assertEquals(expectedViewEditUi, result);
250 public static Object[][] instantiationUIToViewEditDataProvider() {
251 return new Object[][] {
252 {"network cloud(5G) service + needed flags are open", true, true, true, VidNotions.InstantiationUI.NETWORK_WITH_PROPERTY_NETWORK_TECHNOLOGY_EQUALS_STANDARD_SRIOV_OR_OVS},
253 {"mocked service + needed flags are open", false, true, true, VidNotions.InstantiationUI.LEGACY},
254 {"network cloud(5G) service + FLAG_ASYNC_INSTANTIATION is off", true, false, true, VidNotions.InstantiationUI.LEGACY},
255 {"network cloud(5G) service + FLAG_1902_NEW_VIEW_EDIT is off", true, true, false, VidNotions.InstantiationUI.LEGACY},
260 @Test(dataProvider="instantiationUIToViewEditDataProvider")
261 public void whenInstantiationUIIsNotLegacy_viewEditIsRight(
262 String testDescription,
263 boolean isInstantiationUINotLegacy,
264 boolean isFlagAsyncInstantiationActive,
265 boolean isFlag1902NewViewEdit,
266 VidNotions.InstantiationUI expectedViewEditUi) {
268 ISdcCsarHelper csarHelper = isInstantiationUINotLegacy ? mockForNonLegacyInstantiationUI() : mock(ISdcCsarHelper.class);
269 when(featureManagerMock.isActive(Features.FLAG_ASYNC_INSTANTIATION)).thenReturn(isFlagAsyncInstantiationActive);
270 when(featureManagerMock.isActive(Features.FLAG_1902_NEW_VIEW_EDIT)).thenReturn(isFlag1902NewViewEdit);
272 ServiceModel serviceModel = mock(ServiceModel.class);
273 Service service = mock(Service.class);
274 when(serviceModel.getService()).thenReturn(service);
275 when(service.getInstantiationType()).thenReturn(ToscaParserImpl2.Constants.A_LA_CARTE);
277 VidNotions.InstantiationUI result = vidNotionsBuilder.suggestViewEditUI(csarHelper, serviceModel);
278 assertEquals(expectedViewEditUi, result);
282 public static Object[][] mockerForMacroExcluded() {
283 return new Object[][] {
284 {"service with pnfs", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->when(serviceModel.getPnfs()).thenReturn(ImmutableMap.of("a", mock(Node.class))), true},
285 {"service with collection resource", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->when(serviceModel.getCollectionResource()).thenReturn(ImmutableMap.of("a", mock(CR.class))), true},
286 {"service with network + FLAG_NETWORK_TO_ASYNC_INSTANTIATION false ", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->{
287 when(serviceModel.getNetworks()).thenReturn(ImmutableMap.of("a", mock(Network.class)));
288 when(fm.isActive(Features.FLAG_NETWORK_TO_ASYNC_INSTANTIATION)).thenReturn(false);}
290 {"service with network + FLAG_NETWORK_TO_ASYNC_INSTANTIATION true", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->{
291 when(serviceModel.getNetworks()).thenReturn(ImmutableMap.of("a", mock(Network.class)));
292 when(fm.isActive(Features.FLAG_NETWORK_TO_ASYNC_INSTANTIATION)).thenReturn(true);}
294 {"empty service + FLAG_NETWORK_TO_ASYNC_INSTANTIATION false", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->when(fm.isActive(Features.FLAG_NETWORK_TO_ASYNC_INSTANTIATION)).thenReturn(false), false},
298 @Test(dataProvider="mockerForMacroExcluded")
299 public void testIsMacroExcludedFromAsyncFlow(String testDescription, BiConsumer<ServiceModel, FeatureManager> mocker, boolean shallBeExcluded) {
300 ServiceModel serviceModel = mock(ServiceModel.class);
301 mocker.accept(serviceModel, featureManagerMock);
302 assertEquals(shallBeExcluded, vidNotionsBuilder.isMacroExcludedFromAsyncFlow(serviceModel));