aboutsummaryrefslogtreecommitdiffstats
path: root/ui-ci/src/main/java/org/openecomp/sdc/ci/tests/utilities/RestCDUtils.java
blob: d5441b54761af23def300a139c1355e6623cc0f6 (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
/*-
 * ============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.utilities;

import com.aventstack.extentreports.Status;
import org.apache.http.HttpStatus;
import org.codehaus.jettison.json.JSONObject;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.category.CategoryDefinition;
import org.openecomp.sdc.ci.tests.config.Config;
import org.openecomp.sdc.ci.tests.datatypes.ComponentReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.ci.tests.execute.setup.DriverFactory;
import org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions;
import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
import org.openecomp.sdc.ci.tests.utils.rest.CatalogRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.CategoryRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.UserRestUtils;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class RestCDUtils {

    private static final int SLEEP_DURATION = 1000;

    private static void setResourceUniqueIdAndUUID(ComponentReqDetails element, RestResponse getResourceResponse) {
        element.setUniqueId(ResponseParser.getUniqueIdFromResponse(getResourceResponse));
        element.setUUID(ResponseParser.getUuidFromResponse(getResourceResponse));
    }

    public static RestResponse getResource(ResourceReqDetails resource, User user) {
        final String getResourceMsg = "Trying to get resource named " + resource.getName() + " with version " + resource.getVersion();
        final String succeedGetResourceMsg = "Succeeded to get resource named " + resource.getName() + " with version " + resource.getVersion();
        final String failedGetResourceMsg = "Failed to get resource named " + resource.getName() + " with version " + resource.getVersion();
        try {
            ExtentTestActions.log(Status.INFO, getResourceMsg);
            System.out.println(getResourceMsg);
            GeneralUIUtils.sleep(SLEEP_DURATION);
            RestResponse getResourceResponse = null;
            String resourceUniqueId = resource.getUniqueId();
            if (resourceUniqueId != null) {
                getResourceResponse = ResourceRestUtils.getResource(resourceUniqueId);
                if (getResourceResponse.getErrorCode().intValue() == HttpStatus.SC_OK) {
                    ExtentTestActions.log(Status.INFO, succeedGetResourceMsg);
                    System.out.println(succeedGetResourceMsg);
                }
                return getResourceResponse;
            }
            JSONObject getResourceJSONObject = null;
            getResourceResponse = ResourceRestUtils.getResourceByNameAndVersion(user.getUserId(), resource.getName(), resource.getVersion());
            if (getResourceResponse.getErrorCode().intValue() == HttpStatus.SC_OK) {
                setResourceUniqueIdAndUUID(resource, getResourceResponse);
                ExtentTestActions.log(Status.INFO, succeedGetResourceMsg);
                System.out.println(succeedGetResourceMsg);
                return getResourceResponse;
            }
            ExtentTestActions.log(Status.INFO, failedGetResourceMsg);
            return getResourceResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static RestResponse getService(ServiceReqDetails service, User user) {
        final int threadSleepTime = 3500;
        try {
            Thread.sleep(threadSleepTime);
            RestResponse getServiceResponse = ServiceRestUtils.getServiceByNameAndVersion(user, service.getName(),
                    service.getVersion());
            if (getServiceResponse.getErrorCode().intValue() == HttpStatus.SC_OK) {
                setResourceUniqueIdAndUUID(service, getServiceResponse);
            }
            return getServiceResponse;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public static String getExecutionHostAddress() {

        String computerName = null;
        try {
            computerName = InetAddress.getLocalHost().getHostAddress().replaceAll("\\.", "·");
            System.out.println(computerName);
            if (computerName.contains(".")) {
                computerName = computerName.substring(0, computerName.indexOf(".")).toUpperCase();
            }
        } catch (UnknownHostException e) {
            System.out.println("Uknown hostAddress");
        }
        return computerName != null ? computerName : "Uknown hostAddress";
    }

    public static Map<String, List<Component>> getCatalogAsMap() throws IOException {
        User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
        RestResponse catalog = CatalogRestUtils.getCatalog(defaultAdminUser.getUserId());
        return ResponseParser.convertCatalogResponseToJavaObject(catalog.getResponse());
    }

    public static Map<String, List<CategoryDefinition>> getCategories() throws Exception {

        User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);

        Map<String, List<CategoryDefinition>> map = new HashMap<>();


        RestResponse allResourceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.RESOURCE_PARAM_NAME);
        RestResponse allServiceCategories = CategoryRestUtils.getAllCategories(defaultAdminUser, ComponentTypeEnum.SERVICE_PARAM_NAME);

        List<CategoryDefinition> parsedResourceCategories = ResponseParser.parseCategories(allResourceCategories);
        List<CategoryDefinition> parsedServiceCategories = ResponseParser.parseCategories(allServiceCategories);

        map.put(ComponentTypeEnum.RESOURCE_PARAM_NAME, parsedResourceCategories);
        map.put(ComponentTypeEnum.SERVICE_PARAM_NAME, parsedServiceCategories);

        return map;
    }


    public static void deleteCreatedComponents(Map<String, List<Component>> map) throws IOException {

        System.out.println("going to delete all created components...");

        User defaultAdminUser = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
        final String userId = defaultAdminUser.getUserId();


        List<Component> resourcesArrayList = map.get("resources");
        List<String> collect = resourcesArrayList.stream().filter(s -> s.getName().startsWith(ElementFactory.getResourcePrefix())).
                map(e -> e.getUniqueId()).
                collect(Collectors.toList());
        for (String uId : collect) {
            ResourceRestUtils.markResourceToDelete(uId, userId);

        }
        ResourceRestUtils.deleteMarkedResources(userId);

        resourcesArrayList = map.get("services");
        collect = resourcesArrayList.stream().
                filter(e -> e != null).
                filter(e -> e.getName() != null).
                filter(s -> s.getName().startsWith(ElementFactory.getServicePrefix())).
                filter(e -> e.getUniqueId() != null).
                map(e -> e.getUniqueId()).
                collect(Collectors.toList());
        for (String uId : collect) {
            ServiceRestUtils.markServiceToDelete(uId, userId);
        }
        ServiceRestUtils.deleteMarkedServices(userId);

    }

    public static String getUserRole(User reqUser, User user) {
        try {
            RestResponse getUserRoleResp = UserRestUtils.getUserRole(reqUser, user);
            JSONObject jObject = new JSONObject(getUserRoleResp.getResponse());
            return jObject.getString("role");
        } catch (Exception e) {
            return null;
        }
    }

    public static RestResponse getUser(User reqUser, User user) {
        try {
            return UserRestUtils.getUser(reqUser, user);
        } catch (Exception e) {
            return null;
        }
    }

    /*************************************/

    public static void deleteOnDemand() throws IOException {
        Config config = DriverFactory.getConfig();
        if (!config.getSystemUnderDebug()) {
            deleteCreatedComponents(getCatalogAsMap());
        } else {
            System.out.println("According to configuration components will not be deleted, in case to unable option to delete, please change systemUnderDebug parameter value to false ...");
        }
    }

}