Onboard PNF software version Ui test case
[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.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.openecomp.sdc.ci.tests.datatypes.enums.XnfTypeEnum;
31 import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
32 import org.openecomp.sdc.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 = "Single_VNF", parallel = true)
73     private static Object[][] singleVNF() {
74         final List<String> fileNamesFromFolder = OnboardingUtils.getXnfNamesFileList(XnfTypeEnum.VNF);
75         final List<String> newList = new ArrayList<>();
76         newList.add(fileNamesFromFolder.get(0));
77         LOGGER.debug(String.format("There are %s zip file(s) to test", fileNamesFromFolder.size()));
78         return provideData(newList, VNF_FILE_PATH);
79     }
80
81     @DataProvider(name = "softwareInformationPnf", parallel = true)
82     private static Object[][] softwareInformationPnf() {
83         final List<String> pnfPackageFileNameList = OnboardingUtils.getXnfNamesFileList(XnfTypeEnum.PNF);
84         if (CollectionUtils.isEmpty(pnfPackageFileNameList)) {
85             fail("Could not create softwareInformationPnf datasource");
86         }
87         final String pnfPackage = "sample-pnf-1.0.1-SNAPSHOT.csar";
88         final Optional<String> softwareInformationPnfPackage = pnfPackageFileNameList.stream()
89             .filter(pnfPackage::equals).findFirst();
90         if (!softwareInformationPnfPackage.isPresent()) {
91             fail(String.format("Could not create softwareInformationPnf datasource, the package '%s' was not found",
92                 pnfPackage));
93         }
94
95         final String folderPath = FileHandling.getXnfRepositoryPath(XnfTypeEnum.PNF);
96         final Object[][] parametersArray = new Object[1][];
97         parametersArray[0] = new Object[]{folderPath, softwareInformationPnfPackage.get(),
98             Arrays.asList("5gDUv18.05.201", "5gDUv18.06.205")};
99         return parametersArray;
100     }
101
102     private static Object[][] provideData(final List<String> fileNamesFromFolder, final String folderPath) {
103         final Object[][] parametersArray = new Object[fileNamesFromFolder.size()][];
104         int index = 0;
105         for (final Object obj : fileNamesFromFolder) {
106             parametersArray[index++] = new Object[]{folderPath, obj};
107         }
108         return parametersArray;
109     }
110
111     public static List<String> getRandomElements(final int randomElementNumber,
112                                                  final List<String> fileNamesFromFolder) {
113         if (fileNamesFromFolder.isEmpty() || fileNamesFromFolder.size() < randomElementNumber) {
114             return Collections.emptyList();
115         } else {
116             final List<Integer> indexList = new ArrayList<>();
117             final List<String> newRandomFileNamesFromFolder = new ArrayList<>();
118             for (int i = 0; i < fileNamesFromFolder.size(); i++) {
119                 indexList.add(i);
120             }
121             Collections.shuffle(indexList);
122             final Integer[] randomArray = indexList.subList(0, randomElementNumber)
123                 .toArray(new Integer[randomElementNumber]);
124             for (final Integer randomNumber : randomArray) {
125                 newRandomFileNamesFromFolder.add(fileNamesFromFolder.get(randomNumber));
126             }
127             return newRandomFileNamesFromFolder;
128         }
129     }
130
131
132 }