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