aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/asdc/parser/VidNotionsBuilderTest.java
blob: 26fb2cf17c9e6635946641b7f465179440330925 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package org.onap.vid.asdc.parser;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
import org.onap.sdc.toscaparser.api.NodeTemplate;
import org.onap.sdc.toscaparser.api.Property;
import org.onap.sdc.toscaparser.api.elements.Metadata;
import org.onap.vid.model.*;
import org.onap.vid.properties.Features;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.togglz.core.manager.FeatureManager;

import java.util.LinkedHashMap;
import java.util.UUID;
import java.util.function.BiConsumer;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

public class VidNotionsBuilderTest {

    @InjectMocks
    VidNotionsBuilder vidNotionsBuilder;

    @Mock
    private FeatureManager featureManagerMock;

    @BeforeClass
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }

    @AfterMethod
    public void reset() {
        Mockito.reset(featureManagerMock);
    }

    @Test
    public void VLNetworkWithPropertyNetworkTechnologyOVS_UIHintIsPositive() {
        ISdcCsarHelper csarHelper = mockForNonLegacyInstantiationUI();

        assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.NETWORK_WITH_PROPERTY_NETWORK_TECHNOLOGY_EQUALS_STANDARD_SRIOV_OR_OVS));
        assertThat(vidNotionsBuilder.suggestModelCategory(csarHelper) , is(VidNotions.ModelCategory.IS_5G_PROVIDER_NETWORK_MODEL));
    }

    @NotNull
    protected ISdcCsarHelper mockForNonLegacyInstantiationUI() {
        ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());

        NodeTemplate nodeTemplate = mock(NodeTemplate.class);

        when(nodeTemplate.getProperties()).thenReturn(new LinkedHashMap<>(ImmutableMap.of(
                "dummy_val", mock(Property.class),
                "network_technology", new Property(Pair.of("network_technology","ovs"))
        )));

        when(csarHelper.getServiceVlList()).thenReturn(ImmutableList.of(nodeTemplate));
        when(featureManagerMock.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)).thenReturn(true);
        return csarHelper;
    }

    @DataProvider
    public static Object[][] anyAlacarteDataProvider() {
        return new Object[][] {
                {"A-La-Carte", VidNotions.InstantiationUI.ANY_ALACARTE_NEW_UI},
                {"Macro", VidNotions.InstantiationUI.LEGACY},
        };
    }

    @Test(dataProvider = "anyAlacarteDataProvider")
    public void FLAG_EXP_ANY_ALACARTE_NEW_INSTANTIATION_UI_is_active_UIHintIsPositive(String instantiationType, VidNotions.InstantiationUI expectedInstantiationUI) {
        when(featureManagerMock.isActive(Features.FLAG_EXP_ANY_ALACARTE_NEW_INSTANTIATION_UI)).thenReturn(true);
        ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
        when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(ImmutableMap.of(
                "instantiationType", instantiationType
        )));
        NodeTemplate nodeTemplate = mock(NodeTemplate.class);

        when(nodeTemplate.getProperties()).thenReturn(new LinkedHashMap<>(ImmutableMap.of(
                "dummy_val", mock(Property.class),
                "network_technology", new Property(Pair.of("network_technology","ovs"))
        )));

        when(csarHelper.getServiceVlList()).thenReturn(ImmutableList.of(nodeTemplate));

        assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(expectedInstantiationUI));
    }

    @Test
    public void VLNetworkWithPropertyNetworkTechnologyNot5G_UIHintIsNegative() {
        ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());

        NodeTemplate nodeTemplate = mock(NodeTemplate.class);

        when(nodeTemplate.getProperties()).thenReturn(new LinkedHashMap<>(ImmutableMap.of(
                "dummy_val", mock(Property.class),
                "network_technology", new Property(Pair.of("network_technology","old_value"))
        )));

        when(csarHelper.getServiceVlList()).thenReturn(ImmutableList.of(nodeTemplate));

        assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.LEGACY));
        assertThat(vidNotionsBuilder.suggestModelCategory(csarHelper) , is(VidNotions.ModelCategory.OTHER));
    }

    @Test
    public void withoutMocks_givenZippedToscaFile_hasAnyNetworkWithPropertyEqualsToAnyOfYieldsTrue() throws SdcToscaParserException {
        SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
        ISdcCsarHelper csarHelper = factory.getSdcCsarHelper(getClass().getClassLoader().getResource("service-vl-csar.zip").getPath(),false);

        assertThat(vidNotionsBuilder.isALaCarte(csarHelper), is(false));
        assertThat(vidNotionsBuilder.hasAnyNetworkWithPropertyEqualsToAnyOf(csarHelper, "unexpected_property_name"), is(false));
        assertThat(vidNotionsBuilder.hasAnyNetworkWithPropertyEqualsToAnyOf(csarHelper, "network_technology","Standard-SR-IOV"), is(true));
        assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.LEGACY));
    }

    //@Test
    //public void withoutMocks_givenZippedToscaFile_hasFabricConfigurationYieldsTrue() throws SdcToscaParserException {
    //    SdcToscaParserFactory factory = SdcToscaParserFactory.getInstance();
    //    ISdcCsarHelper csarHelper = factory.getSdcCsarHelper(getClass().getClassLoader().getResource("service-fabric-configuration.zip").getPath(),false);
    //
    //    assertThat(vidNotionsBuilder.isALaCarte(csarHelper), is(false));
    //    assertThat(vidNotionsBuilder.hasFabricConfiguration(csarHelper), is(true));
    //    assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.LEGACY));
    //}


    @Test
    public void uuidIsExactly1ffce89fEtc_UIHintIsPositive() {
        ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());

        when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(ImmutableMap.of(
                "UUID", "95eb2c44-bff2-4e8b-ad5d-8266870b7717"
        )));
        when(featureManagerMock.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)).thenReturn(true);
        assertThat(vidNotionsBuilder.suggestInstantiationUI(csarHelper), is(VidNotions.InstantiationUI.SERVICE_UUID_IS_1ffce89f_ef3f_4cbb_8b37_82134590c5de));
    }


    @DataProvider
    public static Object[][] trueAndFalse() {
        return new Object[][] {{true}, {false}};
    }

    @Test(dataProvider = "trueAndFalse")
    public void buildVidNotions_nullByFlag(boolean flagValue) {
        ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());

        when(featureManagerMock.isActive(Features.FLAG_5G_IN_NEW_INSTANTIATION_UI)).thenReturn(flagValue);
        assertThat(vidNotionsBuilder.buildVidNotions(csarHelper, null), hasProperty("instantiationUI", is(VidNotions.InstantiationUI.LEGACY)));
    }

    @DataProvider
    public static Object[][] ServiceRoleTypesDataProvider() {
        return new Object[][] {
                {"gROUPING", VidNotions.InstantiationUI.SERVICE_WITH_VNF_GROUPING},
                {"", VidNotions.InstantiationUI.LEGACY},
        };
    }

    @Test(dataProvider = "ServiceRoleTypesDataProvider")
    public void testGetViewEditUITypeForResourceGroup(String serviceRole, VidNotions.InstantiationUI expectedViewEditUI) {
        when(featureManagerMock.isActive(Features.FLAG_ASYNC_INSTANTIATION)).thenReturn(true);
        when(featureManagerMock.isActive(Features.FLAG_1902_VNF_GROUPING)).thenReturn(true);
        ISdcCsarHelper csarHelper = ToscaParserImpl2Test.getMockedSdcCsarHelper(UUID.randomUUID().toString());
        when(csarHelper.getServiceMetadata()).thenReturn(new Metadata(ImmutableMap.of(
                "serviceRole", serviceRole
        )));

        assertThat(vidNotionsBuilder.suggestViewEditUI(csarHelper, null), is(expectedViewEditUI));
    }

    @DataProvider
    public static Object[][] macroToViewEditDataProvider() {
        return new Object[][] {
                {"macro service + not excluded + needed flags are open", true, false, true, true, VidNotions.InstantiationUI.MACRO_SERVICE},
                {"not macro service", false, false, true, true, VidNotions.InstantiationUI.LEGACY},
                {"macro that shall be excluded because it has pnf", true, true, true, true, VidNotions.InstantiationUI.LEGACY},
                {"macro service + FLAG_ASYNC_INSTANTIATION off", true, false, false, true, VidNotions.InstantiationUI.LEGACY},
                {"macro service + FLAG_1902_NEW_VIEW_EDIT off", true, false, true, false, VidNotions.InstantiationUI.LEGACY},
        };
    }

    @Test(dataProvider="macroToViewEditDataProvider")
    public void whenServiceIsMacro_viewEditIsRight(
            String testDescription,
            boolean isMacro,
            boolean isExcluded,
            boolean isFlagAsyncInstantiationActive,
            boolean isFlag1902NewViewEdit,
            VidNotions.InstantiationUI expectedViewEditUi) {

        ISdcCsarHelper csarHelper = mock(ISdcCsarHelper.class);
        ServiceModel serviceModel = mock(ServiceModel.class);

        //mock for is Macro
        String instantiationType = isMacro ? ToscaParserImpl2.Constants.MACRO : ToscaParserImpl2.Constants.A_LA_CARTE;
        Service service = mock(Service.class);
        when(serviceModel.getService()).thenReturn(service);
        when(service.getInstantiationType()).thenReturn(instantiationType);
        when(featureManagerMock.isActive(Features.FLAG_ASYNC_INSTANTIATION)).thenReturn(isFlagAsyncInstantiationActive);
        when(featureManagerMock.isActive(Features.FLAG_1902_NEW_VIEW_EDIT)).thenReturn(isFlag1902NewViewEdit);

        //mock for isExcluded
        if (isExcluded) {
            when(serviceModel.getPnfs()).thenReturn(ImmutableMap.of("a", mock(Node.class)));
        }

        VidNotions.InstantiationUI result = vidNotionsBuilder.suggestViewEditUI(csarHelper, serviceModel);
        assertEquals(expectedViewEditUi, result);
    }

    @DataProvider
    public static Object[][] instantiationUIToViewEditDataProvider() {
        return new Object[][] {
                {"network cloud(5G) service + needed flags are open", true, true, true, VidNotions.InstantiationUI.NETWORK_WITH_PROPERTY_NETWORK_TECHNOLOGY_EQUALS_STANDARD_SRIOV_OR_OVS},
                {"mocked service + needed flags are open", false, true, true, VidNotions.InstantiationUI.LEGACY},
                {"network cloud(5G) service + FLAG_ASYNC_INSTANTIATION is off", true, false, true, VidNotions.InstantiationUI.LEGACY},
                {"network cloud(5G) service + FLAG_1902_NEW_VIEW_EDIT is off", true, true, false, VidNotions.InstantiationUI.LEGACY},
        };
    }


    @Test(dataProvider="instantiationUIToViewEditDataProvider")
    public void whenInstantiationUIIsNotLegacy_viewEditIsRight(
            String testDescription,
            boolean isInstantiationUINotLegacy,
            boolean isFlagAsyncInstantiationActive,
            boolean isFlag1902NewViewEdit,
            VidNotions.InstantiationUI expectedViewEditUi) {

        ISdcCsarHelper csarHelper = isInstantiationUINotLegacy ?  mockForNonLegacyInstantiationUI() : mock(ISdcCsarHelper.class);
        when(featureManagerMock.isActive(Features.FLAG_ASYNC_INSTANTIATION)).thenReturn(isFlagAsyncInstantiationActive);
        when(featureManagerMock.isActive(Features.FLAG_1902_NEW_VIEW_EDIT)).thenReturn(isFlag1902NewViewEdit);

        ServiceModel serviceModel = mock(ServiceModel.class);
        Service service = mock(Service.class);
        when(serviceModel.getService()).thenReturn(service);
        when(service.getInstantiationType()).thenReturn(ToscaParserImpl2.Constants.A_LA_CARTE);

        VidNotions.InstantiationUI result = vidNotionsBuilder.suggestViewEditUI(csarHelper, serviceModel);
        assertEquals(expectedViewEditUi, result);
    }

    @DataProvider
    public static Object[][] mockerForMacroExcluded() {
        return new Object[][] {
                {"service with pnfs", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->when(serviceModel.getPnfs()).thenReturn(ImmutableMap.of("a", mock(Node.class))), true},
                {"service with collection resource", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->when(serviceModel.getCollectionResource()).thenReturn(ImmutableMap.of("a", mock(CR.class))), true},
                {"service with network + FLAG_NETWORK_TO_ASYNC_INSTANTIATION false ", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->{
                    when(serviceModel.getNetworks()).thenReturn(ImmutableMap.of("a", mock(Network.class)));
                    when(fm.isActive(Features.FLAG_NETWORK_TO_ASYNC_INSTANTIATION)).thenReturn(false);}
                        , true},
                {"service with network + FLAG_NETWORK_TO_ASYNC_INSTANTIATION true", (BiConsumer<ServiceModel, FeatureManager>) (serviceModel, fm)->{
                    when(serviceModel.getNetworks()).thenReturn(ImmutableMap.of("a", mock(Network.class)));
                    when(fm.isActive(Features.FLAG_NETWORK_TO_ASYNC_INSTANTIATION)).thenReturn(true);}
                    , false},
                {"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},
        };
    }

    @Test(dataProvider="mockerForMacroExcluded")
    public void testIsMacroExcludedFromAsyncFlow(String testDescription, BiConsumer<ServiceModel, FeatureManager> mocker, boolean shallBeExcluded) {
        ServiceModel serviceModel = mock(ServiceModel.class);
        mocker.accept(serviceModel, featureManagerMock);
        assertEquals(shallBeExcluded, vidNotionsBuilder.isMacroExcludedFromAsyncFlow(serviceModel));
    }






}