aboutsummaryrefslogtreecommitdiffstats
path: root/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/api/ComponentBaseTest.java
blob: beef13afc604f9c5b6a74ceaa01c75a40f449e29 (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
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.ci.tests.api;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import org.janusgraph.core.JanusGraph;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.rules.TestName;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
import org.openecomp.sdc.be.model.*;
import org.openecomp.sdc.ci.tests.config.Config;
import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.ci.tests.utils.Utils;
import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
import org.openecomp.sdc.ci.tests.utils.rest.*;
import org.slf4j.LoggerFactory;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNotNull;

public abstract class ComponentBaseTest {

    protected static Logger logger = LogManager.getLogger(ComponentBaseTest.class);

    protected static final String REPORT_FOLDER = "." + File.separator + "ExtentReport" + File.separator;
    private static final String VERSIONS_INFO_FILE_NAME = "versions.info";
    private static final String REPORT_FILE_NAME = "SDC_CI_Extent_Report.html";
    protected static JanusGraph janusGraph;
    public static Config config;
    protected static ITestContext myContext;


    /**************** METHODS ****************/
    public static ExtentTest getExtendTest() {
        SomeInterface testManager = new ExtentTestManager();
        return testManager.getTest();
    }

    public static enum ComponentOperationEnum {
        CREATE_COMPONENT, UPDATE_COMPONENT, GET_COMPONENT, DELETE_COMPONENT, CHANGE_STATE_CHECKIN, CHANGE_STATE_CHECKOUT, CHANGE_STATE_UNDO_CHECKOUT
    }

    public ComponentBaseTest(TestName name, String name2) {
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        lc.getLogger("com.thinkaurelius").setLevel(Level.INFO);
        lc.getLogger("com.datastax").setLevel(Level.INFO);
        lc.getLogger("io.netty").setLevel(Level.INFO);
        lc.getLogger("c.d").setLevel(Level.INFO);
    }

    public static String getReportFolder() {
        return REPORT_FOLDER;
    }

    @BeforeSuite(alwaysRun = true)
    public void setupBeforeSuite(ITestContext context) throws Exception {
        config = Utils.getConfig();
        myContext = context;
        ExtentManager.initReporter(getReportFolder(), REPORT_FILE_NAME, context);
        AtomicOperationUtils.createDefaultConsumer(true);
        performClean();
    }

    @BeforeMethod(alwaysRun = true)
    public void setupBeforeTest(java.lang.reflect.Method method, ITestContext context) throws Exception {

        System.out.println(" method.getName() " + method.getName());
        if (!"onboardVNFShotFlow".equals(method.getName()) &&
            !"onboardPNFFlow".equals(method.getName()) ) {
            logger.info("ExtentReport instance started from BeforeMethod...");
            ExtentTestManager.startTest(method.getName());
            ExtentTestManager.assignCategory(this.getClass());

        } else {
            logger.debug("ExtentReport instance started from Test...");
        }

    }

    @AfterMethod(alwaysRun = true)
    public void quitAfterTest(ITestResult result, ITestContext context) throws Exception {

        String testName = result.getName();
        Throwable throwable = result.getThrowable();
        int status = result.getStatus();

        switch (status) {
            case ITestResult.SUCCESS:
                getExtendTest().log(Status.PASS, "Test Result : <span class='label success'>Success</span>");
                break;

            case ITestResult.FAILURE:
                getExtendTest().log(Status.ERROR, "ERROR - The following exepction occured");
                getExtendTest().log(Status.ERROR, result.getThrowable());
                getExtendTest().log(Status.FAIL, "<span class='label failure'>Failure</span>");
                break;

            case ITestResult.SKIP:
                getExtendTest().log(Status.SKIP, "SKIP - The following exepction occured");
                break;
            default:
                break;
        }


        ExtentTestManager.endTest();


    }

    @AfterSuite(alwaysRun = true)
    public static void shutdownJanusGraph() throws Exception {
        performClean();
    }

    public void setLog(String fromDataProvider) {

        String suiteName = ExtentManager.getSuiteName(myContext);
        ExtentTestManager.startTest(Thread.currentThread().getStackTrace()[2].getMethodName() + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + fromDataProvider);
        ExtentTestManager.assignCategory(this.getClass());

    }

    protected static void performClean() throws Exception, FileNotFoundException {
        if (!config.getSystemUnderDebug()) {
            deleteCreatedComponents(getCatalogAsMap());
            FileHandling.overWriteExistindDir("outputCsar");
        } else {
            System.out.println("Accordindig to configuration components will not be deleted, in case to unable option to delete, please change systemUnderDebug parameter value to false ...");
        }
    }

    public void verifyErrorCode(RestResponse response, String action, int expectedCode) {
        assertNotNull("check response object is not null after " + action, response);
        assertNotNull("check error code exists in response after " + action, response.getErrorCode());
        assertEquals("Check response code after  + action" + action, expectedCode, response.getErrorCode().intValue());
    }

    private static void deleteCreatedComponents(Map<String, List<Component>> convertCatalogResponseToJavaObject) throws IOException {
        final String userId = UserRoleEnum.DESIGNER.getUserId();

    /*    List<Component> resourcesArrayList = convertCatalogResponseToJavaObject.get(ComponentTypeEnum.PRODUCT_PARAM_NAME);
        if (resourcesArrayList.size() > 0) {
            List<String> collect = buildCollectionUniqueId(resourcesArrayList);
            for (String uId : collect) {
                ProductRestUtils.deleteProduct(uId, userId);
            }
        }*/

        List<Component> resourcesArrayList = convertCatalogResponseToJavaObject.get(ComponentTypeEnum.RESOURCE_PARAM_NAME);
        if (!CollectionUtils.isEmpty(resourcesArrayList)) {
            List<String> collect = buildCollectionUniqueId(resourcesArrayList);
            for (String uId : collect) {
                ResourceRestUtils.markResourceToDelete(uId, userId);
            }
            ResourceRestUtils.deleteMarkedResources(userId);
        }

        resourcesArrayList = convertCatalogResponseToJavaObject.get(ComponentTypeEnum.SERVICE_PARAM_NAME);
        if (resourcesArrayList.size() > 0) {
            List<String> collect = buildCollectionUniqueId(resourcesArrayList);
            for (String uId : collect) {
                ServiceRestUtils.markServiceToDelete(uId, userId);
            }
            ServiceRestUtils.deleteMarkedServices(userId);
        }
    }

    protected static List<String> buildCollectionUniqueId(List<Component> resourcesArrayList) {


        List<String> genericCollection = new ArrayList<>();
        if(resourcesArrayList.get(0) != null) {
            ComponentTypeEnum componentTypeEnum = resourcesArrayList.get(0).getComponentType();
            resourcesArrayList.stream().filter(a -> a != null).
                    filter(s -> s.getName().toLowerCase().startsWith("ci") && !s.getName().toLowerCase().equals("cindervolume")).
                    filter(f -> f.getUniqueId() != null).
                    map(e -> e.getUniqueId()).
                    collect(Collectors.toList()).
                    forEach((i) -> {
                        buildCollectionBaseOnComponentType(componentTypeEnum, genericCollection, i);
                    });
        }
        return genericCollection;
    }

    public static void buildCollectionBaseOnComponentType(ComponentTypeEnum componentTypeEnum,
                                                          List<String> genericCollection, String i) {
        try {
            switch (componentTypeEnum) {
                case RESOURCE:
                    RestResponse resource = ResourceRestUtils.getResource(i);
                    Resource convertResourceResponseToJavaObject = ResponseParser.convertResourceResponseToJavaObject(resource.getResponse());
                    Map<String, String> allVersions = convertResourceResponseToJavaObject.getAllVersions();
                    Collection<String> values = allVersions.values();
                    genericCollection.addAll(values);

                    break;
                case SERVICE:
                    RestResponse service = ServiceRestUtils.getService(i);
                    Service convertServiceResponseToJavaObject = ResponseParser.convertServiceResponseToJavaObject(service.getResponse());
                    allVersions = convertServiceResponseToJavaObject.getAllVersions();
                    values = allVersions.values();
                    genericCollection.addAll(values);

                    break;


                case PRODUCT:
                    RestResponse product = ProductRestUtils.getProduct(i);
                    Product convertProductResponseToJavaObject = ResponseParser.convertProductResponseToJavaObject(product.getResponse());
                    allVersions = convertProductResponseToJavaObject.getAllVersions();
                    values = allVersions.values();
                    genericCollection.addAll(values);

                    break;
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    protected static Map<String, List<Component>> getCatalogAsMap() throws Exception {
        RestResponse catalog = CatalogRestUtils.getCatalog(UserRoleEnum.DESIGNER.getUserId());
        Map<String, List<Component>> convertCatalogResponseToJavaObject = ResponseParser.convertCatalogResponseToJavaObject(catalog.getResponse());
        return convertCatalogResponseToJavaObject;
    }

    protected Resource createVfFromCSAR(User sdncModifierDetails, String csarId) throws Exception {
        // create new resource from Csar
        ResourceReqDetails resourceDetails = ElementFactory.getDefaultResource();

        resourceDetails.setCsarUUID(csarId);
        resourceDetails.setResourceType(ResourceTypeEnum.VF.name());
        RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
        BaseRestUtils.checkCreateResponse(createResource);
        Resource createdResource = ResponseParser.convertResourceResponseToJavaObject(createResource.getResponse());
        return createdResource;
    }


}