cc33e48530786f6991f7a575782b6ba5eb3bcce6
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / data / providers / OnboardingDataProviders.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.ci.tests.data.providers;
21
22 import static org.testng.Assert.fail;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import org.apache.commons.collections4.CollectionUtils;
28 import org.openecomp.sdc.ci.tests.datatypes.enums.XnfTypeEnum;
29 import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
30 import org.openecomp.sdc.ci.tests.utils.general.OnboardingUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.testng.annotations.DataProvider;
34
35 public class OnboardingDataProviders {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(OnboardingDataProviders.class);
38     private static final String VNF_FILE_PATH = FileHandling.getXnfRepositoryPath(XnfTypeEnum.VNF);
39
40     private OnboardingDataProviders() {
41
42     }
43
44     @DataProvider(name = "randomVNF_List")
45     private static Object[][] randomVnfList() {
46         final int randomElementNumber = 3; //how many VNFs to onboard randomly
47         final List<String> fileNamesFromFolder = OnboardingUtils.getVnfNamesFileListExcludeToscaParserFailure();
48         final List<String> newRandomFileNamesFromFolder = getRandomElements(randomElementNumber, fileNamesFromFolder);
49         if (CollectionUtils.isEmpty(newRandomFileNamesFromFolder)) {
50             fail("Required number of VNF files not exists under " + VNF_FILE_PATH);
51             return new Object[0][];
52         }
53         LOGGER.debug(String.format("There are %s zip file(s) to test", newRandomFileNamesFromFolder.size()));
54         return provideData(newRandomFileNamesFromFolder, VNF_FILE_PATH);
55     }
56
57     @DataProvider(name = "VNF_List", parallel = true)
58     private static Object[][] vnfList() {
59         final List<String> fileNamesFromFolder = OnboardingUtils.getXnfNamesFileList(XnfTypeEnum.VNF);
60         LOGGER.debug(String.format("There are %s package file(s) to test", fileNamesFromFolder.size()));
61         return provideData(fileNamesFromFolder, VNF_FILE_PATH);
62     }
63
64     @DataProvider(name = "PNF_List", parallel = true)
65     private static Object[][] pnfList() {
66         return provideData(OnboardingUtils.getXnfNamesFileList(XnfTypeEnum.PNF),
67             FileHandling.getXnfRepositoryPath(XnfTypeEnum.PNF));
68     }
69
70     @DataProvider(name = "Single_VNF", parallel = true)
71     private static Object[][] singleVNF() {
72         final List<String> fileNamesFromFolder = OnboardingUtils.getXnfNamesFileList(XnfTypeEnum.VNF);
73         final List<String> newList = new ArrayList<>();
74         newList.add(fileNamesFromFolder.get(0));
75         LOGGER.debug(String.format("There are %s zip file(s) to test", fileNamesFromFolder.size()));
76         return provideData(newList, VNF_FILE_PATH);
77     }
78
79     private static Object[][] provideData(final List<String> fileNamesFromFolder, final String folderPath) {
80         final Object[][] parametersArray = new Object[fileNamesFromFolder.size()][];
81         int index = 0;
82         for (final Object obj : fileNamesFromFolder) {
83             parametersArray[index++] = new Object[]{folderPath, obj};
84         }
85         return parametersArray;
86     }
87
88     public static List<String> getRandomElements(final int randomElementNumber,
89                                                  final List<String> fileNamesFromFolder) {
90         if (fileNamesFromFolder.isEmpty() || fileNamesFromFolder.size() < randomElementNumber) {
91             return Collections.emptyList();
92         } else {
93             final List<Integer> indexList = new ArrayList<>();
94             final List<String> newRandomFileNamesFromFolder = new ArrayList<>();
95             for (int i = 0; i < fileNamesFromFolder.size(); i++) {
96                 indexList.add(i);
97             }
98             Collections.shuffle(indexList);
99             final Integer[] randomArray = indexList.subList(0, randomElementNumber)
100                 .toArray(new Integer[randomElementNumber]);
101             for (final Integer randomNumber : randomArray) {
102                 newRandomFileNamesFromFolder.add(fileNamesFromFolder.get(randomNumber));
103             }
104             return newRandomFileNamesFromFolder;
105         }
106     }
107
108
109 }