a743521beae783366949aee3b7f324e9404f0a5a
[sdc.git] /
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.onap.sdc.backend.ci.tests.data.providers;
21
22 import static org.testng.Assert.fail;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Optional;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.onap.sdc.backend.ci.tests.datatypes.enums.XnfTypeEnum;
31 import org.onap.sdc.backend.ci.tests.utils.general.FileHandling;
32 import org.onap.sdc.backend.ci.tests.utils.general.OnboardingUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.testng.annotations.DataProvider;
36
37 public final class OnboardingDataProviders {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(OnboardingDataProviders.class);
40     private static final String VNF_FILE_PATH = FileHandling.getXnfRepositoryPath(XnfTypeEnum.VNF);
41
42     private OnboardingDataProviders() {
43
44     }
45
46     @DataProvider(name = "randomVNF_List")
47     private static Object[][] randomVnfList() {
48         final int randomElementNumber = 3; //how many VNFs to onboard randomly
49         final List<String> fileNamesFromFolder = OnboardingUtils.getVnfNamesFileListExcludeToscaParserFailure();
50         final List<String> newRandomFileNamesFromFolder = getRandomElements(randomElementNumber, fileNamesFromFolder);
51         if (CollectionUtils.isEmpty(newRandomFileNamesFromFolder)) {
52             fail("Required number of VNF files not exists under " + VNF_FILE_PATH);
53             return new Object[0][];
54         }
55         LOGGER.debug(String.format("There are %s zip file(s) to test", newRandomFileNamesFromFolder.size()));
56         return provideData(newRandomFileNamesFromFolder, VNF_FILE_PATH);
57     }
58
59     @DataProvider(name = "VNF_List", parallel = true)
60     private static Object[][] vnfList() {
61         final List<String> fileNamesFromFolder = OnboardingUtils.getXnfNamesFileList(XnfTypeEnum.VNF);
62         LOGGER.debug(String.format("There are %s package file(s) to test", fileNamesFromFolder.size()));
63         return provideData(fileNamesFromFolder, VNF_FILE_PATH);
64     }
65
66     @DataProvider(name = "PNF_List", parallel = true)
67     private static Object[][] pnfList() {
68         return provideData(OnboardingUtils.getXnfNamesFileList(XnfTypeEnum.PNF),
69             FileHandling.getXnfRepositoryPath(XnfTypeEnum.PNF));
70     }
71
72     @DataProvider(name = "CNF_List", parallel = true)
73     private static Object[][] cnfList() {
74         final List<String> fileNamesFromFolder = OnboardingUtils.getXnfNamesFileList(XnfTypeEnum.CNF);
75         LOGGER.debug(String.format("There are %s package file(s) to test", fileNamesFromFolder.size()));
76         return provideData(fileNamesFromFolder, FileHandling.getXnfRepositoryPath(XnfTypeEnum.CNF));
77     }
78
79     @DataProvider(name = "Single_VNF", parallel = true)
80     private static Object[][] singleVNF() {
81         final List<String> fileNamesFromFolder = OnboardingUtils.getXnfNamesFileList(XnfTypeEnum.VNF);
82         final List<String> newList = new ArrayList<>();
83         newList.add(fileNamesFromFolder.get(0));
84         LOGGER.debug(String.format("There are %s zip file(s) to test", fileNamesFromFolder.size()));
85         return provideData(newList, VNF_FILE_PATH);
86     }
87
88     @DataProvider(name = "softwareInformationPnf", parallel = true)
89     private static Object[][] softwareInformationPnf() {
90         final List<String> pnfPackageFileNameList = OnboardingUtils.getXnfNamesFileList(XnfTypeEnum.PNF);
91         if (CollectionUtils.isEmpty(pnfPackageFileNameList)) {
92             fail("Could not create softwareInformationPnf datasource");
93         }
94         final String pnfPackage = "sample-pnf-1.0.1-SNAPSHOT.csar";
95         final Optional<String> softwareInformationPnfPackage = pnfPackageFileNameList.stream()
96             .filter(pnfPackage::equals).findFirst();
97         if (!softwareInformationPnfPackage.isPresent()) {
98             fail(String.format("Could not create softwareInformationPnf datasource, the package '%s' was not found",
99                 pnfPackage));
100         }
101
102         final String folderPath = FileHandling.getXnfRepositoryPath(XnfTypeEnum.PNF);
103         final Object[][] parametersArray = new Object[1][];
104         parametersArray[0] = new Object[]{folderPath, softwareInformationPnfPackage.get(),
105             Arrays.asList("5gDUv18.05.201", "5gDUv18.06.205")};
106         return parametersArray;
107     }
108
109     private static Object[][] provideData(final List<String> fileNamesFromFolder, final String folderPath) {
110         final Object[][] parametersArray = new Object[fileNamesFromFolder.size()][];
111         int index = 0;
112         for (final Object obj : fileNamesFromFolder) {
113             parametersArray[index++] = new Object[]{folderPath, obj};
114         }
115         return parametersArray;
116     }
117
118     public static List<String> getRandomElements(final int randomElementNumber,
119                                                  final List<String> fileNamesFromFolder) {
120         if (fileNamesFromFolder.isEmpty() || fileNamesFromFolder.size() < randomElementNumber) {
121             return Collections.emptyList();
122         } else {
123             final List<Integer> indexList = new ArrayList<>();
124             final List<String> newRandomFileNamesFromFolder = new ArrayList<>();
125             for (int i = 0; i < fileNamesFromFolder.size(); i++) {
126                 indexList.add(i);
127             }
128             Collections.shuffle(indexList);
129             final Integer[] randomArray = indexList.subList(0, randomElementNumber)
130                 .toArray(new Integer[randomElementNumber]);
131             for (final Integer randomNumber : randomArray) {
132                 newRandomFileNamesFromFolder.add(fileNamesFromFolder.get(randomNumber));
133             }
134             return newRandomFileNamesFromFolder;
135         }
136     }
137
138
139 }