788f7808453fa6b8ca662cd16f1890ab71ec806c
[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 com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25 import org.apache.commons.lang3.tuple.Pair;
26 import org.jetbrains.annotations.NotNull;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.MockitoAnnotations;
31 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
32 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
33 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
34 import org.onap.sdc.toscaparser.api.NodeTemplate;
35 import org.onap.sdc.toscaparser.api.Property;
36 import org.onap.sdc.toscaparser.api.elements.Metadata;
37 import org.onap.vid.model.*;
38 import org.onap.vid.properties.Features;
39 import org.testng.annotations.AfterMethod;
40 import org.testng.annotations.BeforeClass;
41 import org.testng.annotations.DataProvider;
42 import org.testng.annotations.Test;
43 import org.togglz.core.manager.FeatureManager;
44
45 import java.util.LinkedHashMap;
46 import java.util.UUID;
47 import java.util.function.BiConsumer;
48
49 import static org.hamcrest.MatcherAssert.assertThat;
50 import static org.hamcrest.Matchers.hasProperty;
51 import static org.hamcrest.Matchers.is;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.when;
54 import static org.testng.Assert.assertEquals;
55
56 public class VidNotionsBuilderTest {
57
58     @InjectMocks
59     VidNotionsBuilder vidNotionsBuilder;
60
61     @Mock
62     private FeatureManager featureManagerMock;
63
64     @BeforeClass
65     public void initMocks() {
66         MockitoAnnotations.initMocks(this);
67     }
68
69     @AfterMethod
70     public void reset() {
71         Mockito.reset(featureManagerMock);
72     }
73
74     @Test
75     public void VLNetworkWithPropertyNetworkTechnologyOVS_UIHintIsPositive() {
76         ISdcCsarHelper csarHelper = mockForNonLegacyInstantiationUI();
77
78         assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.NETWORK_WITH_PROPERTY_NETWORK_TECHNOLOGY_EQUALS_STANDARD_SRIOV_OR_OVS));
79         assertThat(vidNotionsBuilder.suggestModelCategory(csarHelper) , is(VidNotions.ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL));
80     }
81
82     @NotNull
83     protected ISdcCsarHelper mockForNonLegacyInstantiationUI() {
84         ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
85
86         NodeTemplate nodeTemplate = mock(NodeTemplate.class);
87
88         when(nodeTemplate.getProperties()).thenReturn(new LinkedHashMap<>(ImmutableMap.of(
89                 "dummy_val", mock(Property.class),
90                 "network_technology", new Property(Pair.of("network_technology","ovs"))
91         )));
92
93         when(csarHelper.getServiceVlList()).thenReturn(ImmutableList.of(nodeTemplate));
94         when(featureManagerMock.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)).thenReturn(true);
95         return csarHelper;
96     }
97
98     @DataProvider
99     public static Object[][] anyAlacarteDataProvider() {
100         return new Object[][] {
101                 {"A-La-Carte", VidNotions.InstantiationUI.ANY_ALACARTE_NEW_UI},
102                 {"Macro", VidNotions.InstantiationUI.LEGACY},
103         };
104     }
105
106     @Test(dataProvider = "anyAlacarteDataProvider")
107     public void FLAG_EXP_ANY_ALACARTE_NEW_INSTANTIATION_UI_is_active_UIHintIsPositive(String instantiationType, VidNotions.InstantiationUI expectedInstantiationUI) {
108         when(featureManagerMock.isActive(Features.FLAG_EXP_ANY_ALACARTE_NEW_INSTANTIATION_UI)).thenReturn(true);
109         ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
110         when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(ImmutableMap.of(
111                 "instantiationType", instantiationType
112         )));
113         NodeTemplate nodeTemplate = mock(NodeTemplate.class);
114
115         when(nodeTemplate.getProperties()).thenReturn(new LinkedHashMap<>(ImmutableMap.of(
116                 "dummy_val", mock(Property.class),
117                 "network_technology", new Property(Pair.of("network_technology","ovs"))
118         )));
119
120         when(csarHelper.getServiceVlList()).thenReturn(ImmutableList.of(nodeTemplate));
121
122         assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(expectedInstantiationUI));
123     }
124
125     @Test
126     public void VLNetworkWithPropertyNetworkTechnologyNot5G_UIHintIsNegative() {
127         ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
128
129         NodeTemplate nodeTemplate = mock(NodeTemplate.class);
130
131         when(nodeTemplate.getProperties()).thenReturn(new LinkedHashMap<>(ImmutableMap.of(
132                 "dummy_val", mock(Property.class),
133                 "network_technology", new Property(Pair.of("network_technology","old_value"))
134         )));
135
136         when(csarHelper.getServiceVlList()).thenReturn(ImmutableList.of(nodeTemplate));
137
138         assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.LEGACY));
139         assertThat(vidNotionsBuilder.suggestModelCategory(csarHelper) , is(VidNotions.ModelCategory.OTHER));
140     }
141
142     @Test
143     public void withoutMocks_givenZippedToscaFile_hasAnyNetworkWithPropertyEqualsToAnyOfYieldsTrue() throws SdcToscaParserException {
144         SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
145         ISdcCsarHelper csarHelper = factory.getSdcCsarHelper(getClass().getClassLoader().getResource("service-vl-csar.zip").getPath(),false);
146
147         assertThat(vidNotionsBuilder.isALaCarte(csarHelper), is(false));
148         assertThat(vidNotionsBuilder.hasAnyNetworkWithPropertyEqualsToAnyOf(csarHelper, "unexpected_property_name"), is(false));
149         assertThat(vidNotionsBuilder.hasAnyNetworkWithPropertyEqualsToAnyOf(csarHelper, "network_technology","Standard-SR-IOV"), is(true));
150         assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.LEGACY));
151     }
152
153     //@Test
154     //public void withoutMocks_givenZippedToscaFile_hasFabricConfigurationYieldsTrue() throws SdcToscaParserException {
155     //    SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
156     //    ISdcCsarHelper csarHelper = factory.getSdcCsarHelper(getClass().getClassLoader().getResource("service-fabric-configuration.zip").getPath(),false);
157     //
158     //    assertThat(vidNotionsBuilder.isALaCarte(csarHelper), is(false));
159     //    assertThat(vidNotionsBuilder.hasFabricConfiguration(csarHelper), is(true));
160     //    assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.LEGACY));
161     //}
162
163
164     @Test
165     public void uuidIsExactly1ffce89fEtc_UIHintIsPositive() {
166         ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
167
168         when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(ImmutableMap.of(
169                 "UUID", "95eb2c44-bff2-4e8b-ad5d-8266870b7717"
170         )));
171         when(featureManagerMock.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)).thenReturn(true);
172         assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.SERVICE_UUID_IS_1ffce89f_ef3f_4cbb_8b37_82134590c5de));
173     }
174
175
176     @DataProvider
177     public static Object[][] trueAndFalse() {
178         return new Object[][] {{true}, {false}};
179     }
180
181     @Test(dataProvider = "trueAndFalse")
182     public void buildVidNotions_nullByFlag(boolean flagValue) {
183         ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
184
185         when(featureManagerMock.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)).thenReturn(flagValue);
186         assertThat(vidNotionsBuilder.buildVidNotions(csarHelper, null), hasProperty("instantiationUI", is(VidNotions.InstantiationUI.LEGACY)));
187     }
188
189     @DataProvider
190     public static Object[][] ServiceRoleTypesDataProvider() {
191         return new Object[][] {
192                 {"gROUPING", VidNotions.InstantiationUI.SERVICE_WITH_VNF_GROUPING},
193                 {"", VidNotions.InstantiationUI.LEGACY},
194         };
195     }
196
197     @Test(dataProvider = "ServiceRoleTypesDataProvider")
198     public void testGetViewEditUITypeForResourceGroup(String serviceRole, VidNotions.InstantiationUI expectedViewEditUI) {
199         when(featureManagerMock.isActive(Features.FLAG_ASYNC_INSTANTIATION)).thenReturn(true);
200         when(featureManagerMock.isActive(Features.FLAG_1902_VNF_GROUPING)).thenReturn(true);
201         ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
202         when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(ImmutableMap.of(
203                 "serviceRole", serviceRole
204         )));
205
206         assertThat(vidNotionsBuilder.suggestViewEditUI(csarHelper, null), is(expectedViewEditUI));
207     }
208
209     @DataProvider
210     public static Object[][] macroToViewEditDataProvider() {
211         return new Object[][] {
212                 {"macro service + not excluded + needed flags are open", true, false, true, true, VidNotions.InstantiationUI.MACRO_SERVICE},
213                 {"not macro service", false, false, true, true, VidNotions.InstantiationUI.LEGACY},
214                 {"macro that shall be excluded because it has pnf", true, true, true, true, VidNotions.InstantiationUI.LEGACY},
215                 {"macro service + FLAG_ASYNC_INSTANTIATION off", true, false, false, true, VidNotions.InstantiationUI.LEGACY},
216                 {"macro service + FLAG_1902_NEW_VIEW_EDIT off", true, false, true, false, VidNotions.InstantiationUI.LEGACY},
217         };
218     }
219
220     @Test(dataProvider="macroToViewEditDataProvider")
221     public void whenServiceIsMacro_viewEditIsRight(
222             String testDescription,
223             boolean isMacro,
224             boolean isExcluded,
225             boolean isFlagAsyncInstantiationActive,
226             boolean isFlag1902NewViewEdit,
227             VidNotions.InstantiationUI expectedViewEditUi) {
228
229         ISdcCsarHelper csarHelper = mock(ISdcCsarHelper.class);
230         ServiceModel serviceModel = mock(ServiceModel.class);
231
232         //mock for is Macro
233         String instantiationType = isMacro ? ToscaParserImpl2.Constants.MACRO : ToscaParserImpl2.Constants.A_LA_CARTE;
234         Service service = mock(Service.class);
235         when(serviceModel.getService()).thenReturn(service);
236         when(service.getInstantiationType()).thenReturn(instantiationType);
237         when(featureManagerMock.isActive(Features.FLAG_ASYNC_INSTANTIATION)).thenReturn(isFlagAsyncInstantiationActive);
238         when(featureManagerMock.isActive(Features.FLAG_1902_NEW_VIEW_EDIT)).thenReturn(isFlag1902NewViewEdit);
239
240         //mock for isExcluded
241         if (isExcluded) {
242             when(serviceModel.getPnfs()).thenReturn(ImmutableMap.of("a", mock(Node.class)));
243         }
244
245         VidNotions.InstantiationUI result = vidNotionsBuilder.suggestViewEditUI(csarHelper, serviceModel);
246         assertEquals(expectedViewEditUi, result);
247     }
248
249     @DataProvider
250     public static Object[][] instantiationUIToViewEditDataProvider() {
251         return new Object[][] {
252                 {"network cloud(5G) service + needed flags are open", true, true, true, VidNotions.InstantiationUI.NETWORK_WITH_PROPERTY_NETWORK_TECHNOLOGY_EQUALS_STANDARD_SRIOV_OR_OVS},
253                 {"mocked service + needed flags are open", false, true, true, VidNotions.InstantiationUI.LEGACY},
254                 {"network cloud(5G) service + FLAG_ASYNC_INSTANTIATION is off", true, false, true, VidNotions.InstantiationUI.LEGACY},
255                 {"network cloud(5G) service + FLAG_1902_NEW_VIEW_EDIT is off", true, true, false, VidNotions.InstantiationUI.LEGACY},
256         };
257     }
258
259
260     @Test(dataProvider="instantiationUIToViewEditDataProvider")
261     public void whenInstantiationUIIsNotLegacy_viewEditIsRight(
262             String testDescription,
263             boolean isInstantiationUINotLegacy,
264             boolean isFlagAsyncInstantiationActive,
265             boolean isFlag1902NewViewEdit,
266             VidNotions.InstantiationUI expectedViewEditUi) {
267
268         ISdcCsarHelper csarHelper = isInstantiationUINotLegacy ?  mockForNonLegacyInstantiationUI() : mock(ISdcCsarHelper.class);
269         when(featureManagerMock.isActive(Features.FLAG_ASYNC_INSTANTIATION)).thenReturn(isFlagAsyncInstantiationActive);
270         when(featureManagerMock.isActive(Features.FLAG_1902_NEW_VIEW_EDIT)).thenReturn(isFlag1902NewViewEdit);
271
272         ServiceModel serviceModel = mock(ServiceModel.class);
273         Service service = mock(Service.class);
274         when(serviceModel.getService()).thenReturn(service);
275         when(service.getInstantiationType()).thenReturn(ToscaParserImpl2.Constants.A_LA_CARTE);
276
277         VidNotions.InstantiationUI result = vidNotionsBuilder.suggestViewEditUI(csarHelper, serviceModel);
278         assertEquals(expectedViewEditUi, result);
279     }
280
281     @DataProvider
282     public static Object[][] mockerForMacroExcluded() {
283         return new Object[][] {
284                 {"service with pnfs", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->when(serviceModel.getPnfs()).thenReturn(ImmutableMap.of("a", mock(Node.class))), true},
285                 {"service with collection resource", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->when(serviceModel.getCollectionResource()).thenReturn(ImmutableMap.of("a", mock(CR.class))), true},
286                 {"service with network + FLAG_NETWORK_TO_ASYNC_INSTANTIATION false ", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->{
287                     when(serviceModel.getNetworks()).thenReturn(ImmutableMap.of("a", mock(Network.class)));
288                     when(fm.isActive(Features.FLAG_NETWORK_TO_ASYNC_INSTANTIATION)).thenReturn(false);}
289                         , true},
290                 {"service with network + FLAG_NETWORK_TO_ASYNC_INSTANTIATION true", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->{
291                     when(serviceModel.getNetworks()).thenReturn(ImmutableMap.of("a", mock(Network.class)));
292                     when(fm.isActive(Features.FLAG_NETWORK_TO_ASYNC_INSTANTIATION)).thenReturn(true);}
293                     , false},
294                 {"empty service + FLAG_NETWORK_TO_ASYNC_INSTANTIATION false", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->when(fm.isActive(Features.FLAG_NETWORK_TO_ASYNC_INSTANTIATION)).thenReturn(false), false},
295         };
296     }
297
298     @Test(dataProvider="mockerForMacroExcluded")
299     public void testIsMacroExcludedFromAsyncFlow(String testDescription, BiConsumer<ServiceModel, FeatureManager> mocker, boolean shallBeExcluded) {
300         ServiceModel serviceModel = mock(ServiceModel.class);
301         mocker.accept(serviceModel, featureManagerMock);
302         assertEquals(shallBeExcluded, vidNotionsBuilder.isMacroExcludedFromAsyncFlow(serviceModel));
303     }
304
305
306
307
308
309
310 }