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