c32dd2141f60b3e63bb195dff512ce2a0f9b26c4
[vid.git] / vid-app-common / src / test / java / org / onap / vid / asdc / parser / VidNotionsBuilderTest.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.asdc.parser;
22
23 import static java.util.Collections.emptyList;
24 import static java.util.Collections.emptyMap;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.equalTo;
27 import static org.hamcrest.Matchers.hasProperty;
28 import static org.hamcrest.Matchers.is;
29 import static org.mockito.ArgumentMatchers.any;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
32 import static org.onap.vid.model.VidNotions.InstantiationType;
33 import static org.onap.vid.model.VidNotions.InstantiationUI;
34 import static org.onap.vid.model.VidNotions.ModelCategory;
35 import static org.testng.AssertJUnit.assertEquals;
36
37 import com.google.common.collect.ImmutableList;
38 import com.google.common.collect.ImmutableMap;
39 import java.io.File;
40 import java.io.IOException;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
43 import java.util.Collections;
44 import java.util.LinkedHashMap;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.UUID;
48 import java.util.function.BiConsumer;
49 import org.apache.commons.lang3.tuple.Pair;
50 import org.jetbrains.annotations.NotNull;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
56 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
57 import org.onap.sdc.toscaparser.api.NodeTemplate;
58 import org.onap.sdc.toscaparser.api.Property;
59 import org.onap.sdc.toscaparser.api.elements.Metadata;
60 import org.onap.vid.asdc.parser.ToscaParserImpl2.Constants;
61 import org.onap.vid.model.CR;
62 import org.onap.vid.model.Network;
63 import org.onap.vid.model.Node;
64 import org.onap.vid.model.Service;
65 import org.onap.vid.model.ServiceModel;
66 import org.onap.vid.model.VidNotions;
67 import org.onap.vid.properties.Features;
68 import org.onap.vid.testUtils.TestUtils;
69 import org.testng.annotations.AfterMethod;
70 import org.testng.annotations.BeforeClass;
71 import org.testng.annotations.DataProvider;
72 import org.testng.annotations.Test;
73 import org.togglz.core.manager.FeatureManager;
74
75 public class VidNotionsBuilderTest {
76
77     @InjectMocks
78     VidNotionsBuilder vidNotionsBuilder;
79
80     @Mock
81     private FeatureManager featureManagerMock;
82
83     private ServiceModel serviceModel;
84
85     private ISdcCsarHelper csarHelper;
86
87     @BeforeClass
88     public void initMocks() {
89         MockitoAnnotations.initMocks(this);
90     }
91
92     @AfterMethod
93     public void reset() {
94         Mockito.reset(featureManagerMock);
95     }
96
97     @Test
98     public void VLNetworkWithPropertyNetworkTechnologyOVS_UIHintIsPositive() {
99         ISdcCsarHelper csarHelper = mockForNonLegacyInstantiationUI();
100
101         assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper, serviceModel, ModelCategory.OTHER), is(InstantiationUI.NETWORK_WITH_PROPERTY_NETWORK_TECHNOLOGY_EQUALS_STANDARD_SRIOV_OR_OVS));
102         assertThat(vidNotionsBuilder.suggestModelCategory(csarHelper, serviceModel) , is(ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL));
103     }
104
105     @NotNull
106     private ISdcCsarHelper mockForNonLegacyInstantiationUI() {
107         ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
108
109         NodeTemplate nodeTemplate = mock(NodeTemplate.class);
110
111         when(nodeTemplate.getProperties()).thenReturn(new LinkedHashMap<>(ImmutableMap.of(
112                 "dummy_val", mock(Property.class),
113                 "network_technology", new Property(Pair.of("network_technology","ovs"))
114         )));
115
116         when(csarHelper.getServiceVlList()).thenReturn(ImmutableList.of(nodeTemplate));
117         when(featureManagerMock.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)).thenReturn(true);
118         return csarHelper;
119     }
120
121     @DataProvider
122     public static Object[][] anyAlacarteDataProvider() {
123         return new Object[][] {
124                 {"A-La-Carte", InstantiationUI.ANY_ALACARTE_NEW_UI},
125                 {"Macro", InstantiationUI.LEGACY},
126         };
127     }
128
129     @Test(dataProvider = "anyAlacarteDataProvider")
130     public void FLAG_EXP_ANY_ALACARTE_NEW_INSTANTIATION_UI_is_active_UIHintIsPositive(String instantiationType, InstantiationUI expectedInstantiationUI) {
131         initServiceModelAndscarHelperWithMocks();
132
133         when(featureManagerMock.isActive(Features.FLAG_EXP_ANY_ALACARTE_NEW_INSTANTIATION_UI)).thenReturn(true);
134         when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(ImmutableMap.of(
135                 "instantiationType", instantiationType
136         )));
137         NodeTemplate nodeTemplate = mock(NodeTemplate.class);
138
139         when(nodeTemplate.getProperties()).thenReturn(new LinkedHashMap<>(ImmutableMap.of(
140                 "dummy_val", mock(Property.class),
141                 "network_technology", new Property(Pair.of("network_technology","ovs"))
142         )));
143
144         when(csarHelper.getServiceVlList()).thenReturn(ImmutableList.of(nodeTemplate));
145
146         assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper, serviceModel, ModelCategory.OTHER), is(expectedInstantiationUI));
147     }
148
149     @Test
150     public void VLNetworkWithPropertyNetworkTechnologyNot5G_UIHintIsNegative() {
151         initServiceModelAndscarHelperWithMocks();
152
153         NodeTemplate nodeTemplate = mock(NodeTemplate.class);
154
155         when(nodeTemplate.getProperties()).thenReturn(new LinkedHashMap<>(ImmutableMap.of(
156                 "dummy_val", mock(Property.class),
157                 "network_technology", new Property(Pair.of("network_technology","old_value"))
158         )));
159
160         when(csarHelper.getServiceVlList()).thenReturn(ImmutableList.of(nodeTemplate));
161
162         assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper, serviceModel, ModelCategory.OTHER), is(InstantiationUI.LEGACY));
163         assertThat(vidNotionsBuilder.suggestModelCategory(csarHelper, serviceModel) , is(ModelCategory.OTHER));
164     }
165
166     @Test
167     public void withoutMocks_givenZippedToscaFile_hasAnyNetworkWithPropertyEqualsToAnyOfYieldsTrue() throws SdcToscaParserException, IOException {
168         initServiceModelAndscarHelperWithRealCsar("/csars/service-vl-csar.zip");
169         assertThat(vidNotionsBuilder.isALaCarte(csarHelper), is(false));
170         assertThat(vidNotionsBuilder.hasAnyNetworkWithPropertyEqualsToAnyOf(csarHelper, "unexpected_property_name"), is(false));
171         assertThat(vidNotionsBuilder.hasAnyNetworkWithPropertyEqualsToAnyOf(csarHelper, "network_technology","Standard-SR-IOV"), is(true));
172         assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper, serviceModel, ModelCategory.OTHER), is(InstantiationUI.LEGACY));
173     }
174
175     @Test
176     public void withoutMocks_givenZippedToscaFile_hasFabricConfigurationYieldsTrue() throws SdcToscaParserException, IOException {
177         initServiceModelAndscarHelperWithRealCsar("/csars/service-fabric-configuration.zip");
178         assertThat(vidNotionsBuilder.isALaCarte(csarHelper), is(false));
179         assertThat(vidNotionsBuilder.hasFabricConfiguration(csarHelper), is(true));
180         assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper, serviceModel, ModelCategory.OTHER), is(InstantiationUI.LEGACY));
181     }
182
183     @Test(dataProvider = "trueAndFalse", dataProviderClass = TestUtils.class)
184     public void withoutMocks_givenZippedToscaFileOfTransportService_InstantiationUIAndCategoryAreRight(boolean flagValue) throws SdcToscaParserException, IOException {
185         initServiceModelAndscarHelperWithRealCsar("/csars/csarTransportWithPnfs.zip");
186
187         when(featureManagerMock.isActive(Features.FLAG_1908_TRANSPORT_SERVICE_NEW_INSTANTIATION_UI)).thenReturn(flagValue);
188
189         assertThat(vidNotionsBuilder.isALaCarte(csarHelper), is(false));
190         assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper, serviceModel, ModelCategory.OTHER), is(flagValue ? InstantiationUI.TRANSPORT_SERVICE : InstantiationUI.LEGACY));
191         assertThat(vidNotionsBuilder.suggestModelCategory(csarHelper, serviceModel), is(ModelCategory.Transport));
192     }
193
194     @Test(dataProvider = "trueAndFalse", dataProviderClass = TestUtils.class)
195     public void withoutMocks_givenZippedToscaFileOfInfraStructureVpn_InstantiationUIIsRight(boolean flagValue) throws SdcToscaParserException, IOException {
196         initServiceModelAndscarHelperWithRealCsar("/csars/service-Infravpn-csar.zip");
197         when(featureManagerMock.isActive(Features.FLAG_1908_INFRASTRUCTURE_VPN)).thenReturn(flagValue);
198         assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper, serviceModel, ModelCategory.OTHER), is(flagValue ? InstantiationUI.INFRASTRUCTURE_VPN : InstantiationUI.LEGACY));
199         assertThat(vidNotionsBuilder.suggestModelCategory(csarHelper, serviceModel), is(ModelCategory.INFRASTRUCTURE_VPN));
200     }
201
202     @Test()
203     public void withoutMocks_givenToscaOfPortMirroring_InstantiationUIIsLegacyAndCategoryIsPortMirroring() throws SdcToscaParserException, IOException {
204         initServiceModelAndscarHelperWithRealCsar("/csars/portMirroringService.zip");
205         when(featureManagerMock.isActive(Features.FLAG_2002_ANY_ALACARTE_BESIDES_EXCLUDED_NEW_INSTANTIATION_UI)).thenReturn(true);
206         assertThat(vidNotionsBuilder.buildVidNotions(csarHelper, serviceModel),
207             equalTo(new VidNotions(InstantiationUI.LEGACY, ModelCategory.PORT_MIRRORING, InstantiationUI.LEGACY, InstantiationType.ClientConfig)));
208
209     }
210
211     @Test()
212     public void withoutMocks_givenToscaOfVLanTagging_InstantiationUIIsLegacyAndCategoryIsVlanTagging() throws SdcToscaParserException, IOException {
213         initServiceModelAndscarHelperWithRealCsar("/csars/service-VdorotheaSrv-csar.zip");
214         when(featureManagerMock.isActive(Features.FLAG_2002_ANY_ALACARTE_BESIDES_EXCLUDED_NEW_INSTANTIATION_UI)).thenReturn(true);
215         assertThat(vidNotionsBuilder.buildVidNotions(csarHelper, serviceModel),
216             equalTo(new VidNotions(InstantiationUI.LEGACY, ModelCategory.VLAN_TAGGING, InstantiationUI.LEGACY, InstantiationType.ALaCarte)));
217     }
218
219     @Test
220     public void withoutMocks_givenToscaWithoutTypeAndFlagOn_InstantiationUIisAlacarte()
221         throws SdcToscaParserException, IOException {
222         initServiceModelAndscarHelperWithRealCsar("/csars/service-Vocg1804Svc.zip");
223         when(featureManagerMock.isActive(Features.FLAG_2002_ANY_ALACARTE_BESIDES_EXCLUDED_NEW_INSTANTIATION_UI)).thenReturn(true);
224         assertThat(vidNotionsBuilder.buildVidNotions(csarHelper, serviceModel),
225             equalTo(new VidNotions(
226                 InstantiationUI.ANY_ALACARTE_WHICH_NOT_EXCLUDED,
227                 ModelCategory.OTHER,
228                 InstantiationUI.LEGACY,
229                 InstantiationType.ClientConfig)));
230     }
231
232     @DataProvider
233     public static Object[][] anyAlaCarteDataProvider() {
234         return new Object[][] {
235             {true, Constants.A_LA_CARTE, InstantiationUI.ANY_ALACARTE_WHICH_NOT_EXCLUDED},
236             {false, Constants.A_LA_CARTE, InstantiationUI.LEGACY},
237             {true, Constants.MACRO, InstantiationUI.LEGACY},
238             {true, Constants.CLIENT_CONFIG, InstantiationUI.ANY_ALACARTE_WHICH_NOT_EXCLUDED},
239             {true, null, InstantiationUI.ANY_ALACARTE_WHICH_NOT_EXCLUDED},
240             {true, "", InstantiationUI.ANY_ALACARTE_WHICH_NOT_EXCLUDED}
241         };
242     }
243
244     @Test(dataProvider = "anyAlaCarteDataProvider")
245     public void testAnyAlaCarteNewUI_byInstantiationTypeAndFeatureFlag(boolean flag, String instantiationType, InstantiationUI expected) {
246         initServiceModelAndscarHelperWithMocks();
247         mockInstantiationType(serviceModel, instantiationType);
248         when(featureManagerMock.isActive(Features.FLAG_2002_ANY_ALACARTE_BESIDES_EXCLUDED_NEW_INSTANTIATION_UI)).thenReturn(flag);
249         assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper, serviceModel, ModelCategory.OTHER), is(expected));
250     }
251
252     @Test
253     public void uuidIsExactly1ffce89fEtc_UIHintIsPositive() {
254         initServiceModelAndscarHelperWithMocks();
255
256         when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(ImmutableMap.of(
257                 "UUID", "95eb2c44-bff2-4e8b-ad5d-8266870b7717"
258         )));
259         when(featureManagerMock.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)).thenReturn(true);
260         assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper, serviceModel, ModelCategory.OTHER), is(InstantiationUI.SERVICE_UUID_IS_1ffce89f_ef3f_4cbb_8b37_82134590c5de));
261     }
262
263     @Test(dataProvider = "trueAndFalse", dataProviderClass = TestUtils.class)
264     public void buildVidNotions_nullByFlag(boolean flagValue) {
265         initServiceModelAndscarHelperWithMocks();
266
267         when(featureManagerMock.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)).thenReturn(flagValue);
268         assertThat(vidNotionsBuilder.buildVidNotions(csarHelper, serviceModel), hasProperty("instantiationUI", is(InstantiationUI.LEGACY)));
269     }
270
271     private void mockInstantiationType(ServiceModel serviceModel, String instantiationType) {
272         Service mockService = mock(Service.class);
273         when(serviceModel.getService()).thenReturn(mockService);
274         when(mockService.getInstantiationType()).thenReturn(instantiationType);
275     }
276
277     @DataProvider
278     public static Object[][] ServiceRoleTypesDataProvider() {
279         return new Object[][] {
280                 {"gROUPING", InstantiationUI.SERVICE_WITH_VNF_GROUPING},
281                 {"", InstantiationUI.LEGACY},
282         };
283     }
284
285     @Test(dataProvider = "ServiceRoleTypesDataProvider")
286     public void testGetViewEditUITypeForResourceGroup(String serviceRole, InstantiationUI expectedViewEditUI) {
287         initServiceModelAndscarHelperWithMocks();
288         when(featureManagerMock.isActive(Features.FLAG_1902_VNF_GROUPING)).thenReturn(true);
289         when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(ImmutableMap.of(
290                 "serviceRole", serviceRole
291         )));
292
293         assertThat(vidNotionsBuilder.suggestViewEditUI(csarHelper, serviceModel, ModelCategory.OTHER), is(expectedViewEditUI));
294     }
295
296     @DataProvider
297     public static Object[][] instantiationUIToViewEditDataProvider() {
298         return new Object[][] {
299                 {"network cloud(5G) service + needed flags are open", true, true, InstantiationUI.NETWORK_WITH_PROPERTY_NETWORK_TECHNOLOGY_EQUALS_STANDARD_SRIOV_OR_OVS},
300                 {"mocked service + needed flags are open", false, true, InstantiationUI.LEGACY},
301                 {"network cloud(5G) service + FLAG_1902_NEW_VIEW_EDIT is off", true, false, InstantiationUI.LEGACY},
302         };
303     }
304
305
306     @Test(dataProvider="instantiationUIToViewEditDataProvider")
307     public void whenInstantiationUIIsNotLegacy_viewEditIsRight(
308             String testDescription,
309             boolean isInstantiationUINotLegacy,
310             boolean isFlag1902NewViewEdit,
311             InstantiationUI expectedViewEditUi) {
312
313         ISdcCsarHelper csarHelper = isInstantiationUINotLegacy ?  mockForNonLegacyInstantiationUI() : mock(ISdcCsarHelper.class);
314         when(featureManagerMock.isActive(Features.FLAG_1902_NEW_VIEW_EDIT)).thenReturn(isFlag1902NewViewEdit);
315
316         ServiceModel serviceModel = mock(ServiceModel.class);
317         mockInstantiationType(serviceModel, Constants.A_LA_CARTE);
318
319         InstantiationUI result = vidNotionsBuilder.suggestViewEditUI(csarHelper, serviceModel, ModelCategory.OTHER);
320         assertEquals(expectedViewEditUi, result);
321     }
322
323     @DataProvider
324     public static Object[][] mockerForMacroExcluded() {
325         return new Object[][] {
326                 {"service with pnfs", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->when(serviceModel.getPnfs()).thenReturn(ImmutableMap.of("a", mock(Node.class))), true},
327                 {"service with collection resource", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm) -> when(serviceModel.getCollectionResources()).thenReturn(ImmutableMap.of("a", mock(CR.class))), true},
328                 {"service with network + FLAG_NETWORK_TO_ASYNC_INSTANTIATION false ", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->{
329                     when(serviceModel.getNetworks()).thenReturn(ImmutableMap.of("a", mock(Network.class)));
330                     when(fm.isActive(Features.FLAG_NETWORK_TO_ASYNC_INSTANTIATION)).thenReturn(false);}
331                         , true},
332                 {"service with network + FLAG_NETWORK_TO_ASYNC_INSTANTIATION true", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->{
333                     when(serviceModel.getNetworks()).thenReturn(ImmutableMap.of("a", mock(Network.class)));
334                     when(fm.isActive(Features.FLAG_NETWORK_TO_ASYNC_INSTANTIATION)).thenReturn(true);}
335                         , false},
336                 {"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},
337         };
338     }
339
340     @Test(dataProvider="mockerForMacroExcluded")
341     public void testIsMacroExcludedFromAsyncFlow(String testDescription, BiConsumer<ServiceModel, FeatureManager> mocker, boolean shallBeExcluded) {
342         ServiceModel serviceModel = mock(ServiceModel.class);
343         mocker.accept(serviceModel, featureManagerMock);
344         assertEquals(shallBeExcluded, vidNotionsBuilder.isMacroExcludedFromAsyncFlow(serviceModel));
345     }
346
347     @DataProvider
348     public static Object[][] toscaParserInstantiationTypeToVidNotion() {
349         return new Object[][] {
350                 {ToscaParserImpl2.Constants.MACRO, InstantiationType.Macro},
351                 {ToscaParserImpl2.Constants.A_LA_CARTE, InstantiationType.ALaCarte},
352                 {ToscaParserImpl2.Constants.CLIENT_CONFIG, InstantiationType.ClientConfig},
353                 {"I dont know", InstantiationType.ClientConfig},
354                 {"", InstantiationType.ClientConfig}
355         };
356     }
357
358     @Test(dataProvider="toscaParserInstantiationTypeToVidNotion")
359     public void testSuggestInstantiationTypeWhenInstantiationUiLegacy(String toscaParserInstantiationType, InstantiationType expectedInstantiationType) {
360         ServiceModel serviceModel = mock(ServiceModel.class);
361         mockInstantiationType(serviceModel, toscaParserInstantiationType);
362         assertEquals(expectedInstantiationType, vidNotionsBuilder.suggestInstantiationType(serviceModel, ModelCategory.OTHER));
363     }
364
365     @DataProvider
366     public static Object[][] instantiationUIAndFeatureFlagsForInstantiationType() {
367         return new Object[][] {
368                 {ModelCategory.Transport, Features.FLAG_1908_TRANSPORT_SERVICE_NEW_INSTANTIATION_UI, true, InstantiationType.Macro},
369                 {ModelCategory.Transport, Features.FLAG_1908_TRANSPORT_SERVICE_NEW_INSTANTIATION_UI, false, InstantiationType.ALaCarte},
370                 {ModelCategory.INFRASTRUCTURE_VPN, Features.FLAG_1908_INFRASTRUCTURE_VPN, true, InstantiationType.Macro},
371                 {ModelCategory.INFRASTRUCTURE_VPN, Features.FLAG_1908_INFRASTRUCTURE_VPN, false, InstantiationType.ALaCarte},
372                 {ModelCategory.OTHER, Features.FLAG_1908_INFRASTRUCTURE_VPN, true, InstantiationType.ALaCarte}, //not mapped InstantiationUI
373         };
374     }
375
376     @Test(dataProvider="instantiationUIAndFeatureFlagsForInstantiationType")
377     public void testSuggestInstantiationTypeByModelCategoryAndFeatureFlags(
378             ModelCategory instantiationUI,
379             Features featureFlag,
380             boolean isFeatureOn,
381             InstantiationType expectedInstantiationType) {
382         ServiceModel serviceModel = mock(ServiceModel.class);
383         mockInstantiationType(serviceModel, Constants.A_LA_CARTE);
384         when(featureManagerMock.isActive(featureFlag)).thenReturn(isFeatureOn);
385         assertEquals(expectedInstantiationType, vidNotionsBuilder.suggestInstantiationType(serviceModel, instantiationUI));
386     }
387
388     @DataProvider
389     public static Object[][] FLAG_1908_COLLECTION_RESOURCE_NEW_INSTANTIATION_UIValueAndCollectionResourceForVidNotions() {
390         return new Object[][] {
391                 {true, ImmutableMap.of("Some string", mock(CR.class)), InstantiationUI.SERVICE_WITH_COLLECTION_RESOURCE, ModelCategory.SERVICE_WITH_COLLECTION_RESOURCE},
392                 {true, Collections.EMPTY_MAP, InstantiationUI.LEGACY, ModelCategory.OTHER},
393                 {true, null, InstantiationUI.LEGACY, ModelCategory.OTHER},
394                 {false, ImmutableMap.of("Some string", mock(CR.class)), InstantiationUI.LEGACY, ModelCategory.SERVICE_WITH_COLLECTION_RESOURCE},
395                 {false, Collections.EMPTY_MAP, InstantiationUI.LEGACY, ModelCategory.OTHER},
396                 {false, null, InstantiationUI.LEGACY, ModelCategory.OTHER}
397         };
398     }
399
400     @Test(dataProvider="FLAG_1908_COLLECTION_RESOURCE_NEW_INSTANTIATION_UIValueAndCollectionResourceForVidNotions")
401     public void testSuggestInstantiationUiAndModelCategoryByCollectionResourceAndFeatureFlag_FLAG_1908_COLLECTION_RESOURCE_NEW_INSTANTIATION_UI(
402             boolean featureFlagValue,
403             Map<String, CR> collectionResource,
404             VidNotions.InstantiationUI expectedInstantiationUi,
405             VidNotions.ModelCategory expectedModelCategory) {
406         initServiceModelAndscarHelperWithMocks();
407
408         Service service = mock(Service.class);
409         when(service.getInstantiationType()).thenReturn(ToscaParserImpl2.Constants.MACRO);
410         when(serviceModel.getService()).thenReturn(service);
411         when(serviceModel.getCollectionResources()).thenReturn(collectionResource);
412         when(featureManagerMock.isActive(Features.FLAG_1908_COLLECTION_RESOURCE_NEW_INSTANTIATION_UI)).thenReturn(featureFlagValue);
413         VidNotions vidNotions = vidNotionsBuilder.buildVidNotions(csarHelper, serviceModel);
414         assertEquals(expectedInstantiationUi, vidNotions.getInstantiationUI());
415         assertEquals(expectedModelCategory, vidNotions.getModelCategory());
416         assertEquals(InstantiationUI.LEGACY, vidNotions.getViewEditUI());
417         assertEquals(InstantiationType.Macro, vidNotions.getInstantiationType());
418     }
419
420     @Test
421     public void whenServiceModelIsNull_thenInstantiationTypeIsClientConfig() {
422         assertEquals( InstantiationType.ClientConfig, vidNotionsBuilder.suggestInstantiationType(null, ModelCategory.OTHER));
423     }
424
425     @Test
426     public void whenServiceInServiceModelIsNull_thenInstantiationTypeIsClientConfig() {
427         assertEquals( InstantiationType.ClientConfig, vidNotionsBuilder.suggestInstantiationType(mock(ServiceModel.class), ModelCategory.OTHER));
428     }
429
430     @Test
431     public void whenInstantiationTypeInServiceModelIsNull_thenInstantiationTypeIsClientConfig() {
432         initServiceModelAndscarHelperWithMocks();
433         mockInstantiationType(serviceModel, null);
434         assertEquals( InstantiationType.ClientConfig, vidNotionsBuilder.suggestInstantiationType(serviceModel, ModelCategory.OTHER));
435     }
436
437     private void initServiceModelAndscarHelperWithRealCsar(String path) throws SdcToscaParserException, IOException {
438         Path csarPath = Paths.get(new File(getClass().getResource(path).getPath()).getCanonicalPath());
439         ToscaParserImpl2 toscaParser = new ToscaParserImpl2(vidNotionsBuilder);
440         org.onap.vid.asdc.beans.Service asdcServiceMetadata = mock(org.onap.vid.asdc.beans.Service.class);
441         when(asdcServiceMetadata.getVersion()).thenReturn("versions");
442         serviceModel = toscaParser.makeServiceModel(csarPath, asdcServiceMetadata);
443         csarHelper = toscaParser.getSdcCsarHelper(csarPath);
444     }
445
446     private void initServiceModelAndscarHelperWithMocks() {
447         csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
448         serviceModel = mock(ServiceModel.class);
449     }
450
451     @DataProvider
452     public static Object[][] VnfNcIndicationDataProvider() {
453         return new Object[][] {
454                 {true, "VNF",  InstantiationUI.A_LA_CARTE_VNF_SERVICE_ROLE},
455                 {false, "VNF", InstantiationUI.LEGACY},
456                 {false, "notVNF", InstantiationUI.LEGACY},
457                 {true, null, InstantiationUI.LEGACY},
458                 {true, "notVNF", InstantiationUI.LEGACY},
459                 {true, "vnf", InstantiationUI.A_LA_CARTE_VNF_SERVICE_ROLE},
460         };
461     }
462
463     @Test (dataProvider = "VnfNcIndicationDataProvider")
464     public void whenServiceRoleVnf_thenInstantiationTypeNewUI(boolean flagOn, String serviceRole, InstantiationUI expectedViewEditUi){
465         initServiceModelAndscarHelperWithMocks();
466
467         when(featureManagerMock.isActive(Features.FLAG_1908_A_LA_CARTE_VNF_NEW_INSTANTIATION_UI)).thenReturn(flagOn);
468
469         when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(serviceRole == null ?
470                 emptyMap() : ImmutableMap.of(ToscaParserImpl2.Constants.SERVICE_ROLE, serviceRole)
471         ));
472
473         assertEquals(expectedViewEditUi, vidNotionsBuilder.suggestInstantiationUI(csarHelper, serviceModel, ModelCategory.OTHER));
474     }
475
476     private static NodeTemplate mockNodeTemplateChild(boolean withFabricConfiguration) {
477         NodeTemplate child = mock(NodeTemplate.class);
478         when(child.getType()).thenReturn(withFabricConfiguration ? ToscaParserImpl2.Constants.FABRIC_CONFIGURATION_TYPE : "nothing");
479         return child;
480     }
481
482     private static ISdcCsarHelper mockServiceNodeTemplates(ISdcCsarHelper csarHelper, ImmutableList<NodeTemplate> children) {
483         when(csarHelper.getNodeTemplateChildren(any())).thenReturn(children);
484
485         NodeTemplate parent = mock(NodeTemplate.class);
486         List<NodeTemplate> nodeTemplates = ImmutableList.of(parent);
487
488         when(csarHelper.getServiceNodeTemplates()).thenReturn(nodeTemplates);
489         return csarHelper;
490     }
491
492     @DataProvider
493     public static Object[][] csarHelpersForFabricConfiguration() {
494         ISdcCsarHelper csarHelperWithNoNodes = mock(ISdcCsarHelper.class);
495         when(csarHelperWithNoNodes.getServiceNodeTemplates()).thenReturn(emptyList());
496
497         return new Object[][] {
498                 { "zero nodes", false, csarHelperWithNoNodes },
499                 { "single node with no child", false, mockServiceNodeTemplates(mock(ISdcCsarHelper.class), ImmutableList.of()) },
500                 { "single node with single fabric child", true, mockServiceNodeTemplates(mock(ISdcCsarHelper.class), ImmutableList.of(mockNodeTemplateChild(true))) },
501                 { "single node with single fabric child and single non-fabric", true, mockServiceNodeTemplates(mock(ISdcCsarHelper.class), ImmutableList.of(
502                         mockNodeTemplateChild(true), mockNodeTemplateChild(true))) },
503         };
504     }
505
506     @Test (dataProvider = "csarHelpersForFabricConfiguration")
507     public void hasFabricConfiguration(String desc, boolean shouldHaveFabricConfiguration, ISdcCsarHelper csarHelper) {
508         assertThat(desc, vidNotionsBuilder.hasFabricConfiguration(csarHelper), is(shouldHaveFabricConfiguration));
509     }
510
511     @DataProvider
512     public static Object[][] macroTransportDataProvider() {
513         return new Object[][]{
514             {"transport service flag is open", true, true, true, InstantiationUI.LEGACY},
515             {"macro service flag is open", true, false, true, InstantiationUI.MACRO_SERVICE},
516             {"macro service flag is closed", false, true, true, InstantiationUI.LEGACY},
517             {"transport service flag is closed", false, false, true, InstantiationUI.LEGACY},
518             {"not a macro service", true, false, false, InstantiationUI.LEGACY}
519         };
520     }
521
522     @Test (dataProvider = "macroTransportDataProvider")
523     public void viewEditMacroService_transportOrNotTransport(String desc, boolean flagActive, boolean isTransport, boolean isMacro, InstantiationUI expectedViewEditUi) {
524         initServiceModelAndscarHelperWithMocks();
525         Service service = mock(Service.class);
526
527         String instantiationType = isMacro ? ToscaParserImpl2.Constants.MACRO : ToscaParserImpl2.Constants.A_LA_CARTE;
528         when(serviceModel.getService()).thenReturn(service);
529         when(service.getInstantiationType()).thenReturn(instantiationType);
530         when(featureManagerMock.isActive(Features.FLAG_1908_MACRO_NOT_TRANSPORT_NEW_VIEW_EDIT)).thenReturn(flagActive);
531         when(featureManagerMock.isActive(Features.FLAG_1902_NEW_VIEW_EDIT)).thenReturn(false);
532         when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(isTransport ? ImmutableMap.of(ToscaParserImpl2.Constants.SERVICE_TYPE, "TRANSPORT") : emptyMap()
533         ));
534
535         assertEquals(expectedViewEditUi, vidNotionsBuilder.suggestViewEditUI(csarHelper, serviceModel, ModelCategory.OTHER));
536     }
537
538     @DataProvider
539     public static Object[][] invariantUuidToMacroDataProvider() {
540         return new Object[][]{
541             {"117f5f1a-1b47-4ae1-ae04-489c9a7ada28", true},
542             {"117F5f1a-1b47-4AE1-ae04-489C9A7ada28", true},
543             {"2efab359-cdd4-4da2-9b79-61df990796c2", true},
544             {"67e09a1f-9e42-4b63-8dee-bc60bae50de1", false},
545             {"67e09A1F-9E42-4b63-8Dee-bc60bae50de1", false},
546             {"5d854f6b-759c-4aa6-b472-7e4bb1c003d4", false},
547             {"I'm not a uuid", false},
548             {null, false},
549         };
550     }
551
552     @Test(dataProvider = "invariantUuidToMacroDataProvider")
553     public void testIsMacroByInvariantUuid(String uuid, boolean expectedIsMacro) {
554         assertEquals(expectedIsMacro, vidNotionsBuilder.isMacroByInvariantUuid(uuid));
555     }
556 }