2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.ci.tests.utils.validation;
23 import static org.testng.AssertJUnit.assertEquals;
25 import java.io.FileNotFoundException;
26 import java.util.Arrays;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.ci.tests.datatypes.enums.ErrorInfo;
30 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
31 import org.openecomp.sdc.ci.tests.utils.Utils;
32 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
33 import org.openecomp.sdc.exception.ResponseFormat;
34 import org.testng.Assert;
36 public class BaseValidationUtils {
38 public static final int STATUS_CODE_SUCCESS = 200;
39 public static final int STATUS_CODE_CREATED = 201;
40 public static final int STATUS_CODE_DELETE = 204;
41 public static final int STATUS_CODE_NOT_FOUND = 404;
42 public static final int STATUS_CODE_SUCCESS_NO_CONTENT = 204;
43 public static final int STATUS_CODE_SUCCESS_DELETE = 204;
44 public static final int STATUS_CODE_INVALID_CONTENT = 400;
45 public static final int STATUS_CODE_MISSING_DATA = 400;
46 public static final int STATUS_CODE_MISSING_INFORMATION = 403;
47 public static final int STATUS_CODE_RESTRICTED_ACCESS = 403;
48 public static final int STATUS_CODE_RESTRICTED_OPERATION = 409;
49 public static final int STATUS_CODE_ALREADY_EXISTS = 409;
52 protected static Boolean checkErrorCode(RestResponse deleteResponse) {
53 if (deleteResponse.getErrorCode() == STATUS_CODE_SUCCESS
54 || deleteResponse.getErrorCode() == STATUS_CODE_DELETE) {
60 // *** STATUS CODE VALIDATION UTIITIES ****
61 public static void checkStatusCode(RestResponse response, String assertMessage, boolean AND, int... statuses) {
62 int statusCode = response.getErrorCode();
63 for (int status : statuses) {
64 if (AND && statusCode != status) {
65 Assert.fail(assertMessage + " status: " + statusCode);
66 } else if (statusCode == status) {
71 Assert.fail(assertMessage + " status: " + statusCode);
75 public static void checkDeleteResponse(RestResponse response) {
76 checkStatusCode(response, "delete request failed", false, STATUS_CODE_DELETE, STATUS_CODE_NOT_FOUND,
77 STATUS_CODE_SUCCESS); // STATUS_CODE_SUCCESS for deActivate user
80 public static void checkCreateResponse(RestResponse response) {
81 checkStatusCode(response, "create request failed", false, STATUS_CODE_CREATED);
84 public static void checkSuccess(RestResponse response) {
85 checkStatusCode(response, "request failed", false, STATUS_CODE_SUCCESS);
88 public static void checkErrorResponse(RestResponse errorResponse, ActionStatus actionStatus,
89 String... expectedVariables) throws FileNotFoundException {
91 ErrorInfo expectedError = ErrorValidationUtils.parseErrorConfigYaml(actionStatus.name());
92 String expectedMessage = expectedError.getMessage();
95 ResponseFormat responseFormat = ResponseParser.parseToObjectUsingMapper(errorResponse.getResponse(),
96 ResponseFormat.class);
97 String actualMessage = responseFormat.getText();
98 String[] actualVariables = responseFormat.getVariables();
100 assertEquals("Unexpected error message", expectedMessage, actualMessage);
101 assertEquals("Unexpected error variables", Arrays.asList(expectedVariables), Arrays.asList(actualVariables));
104 public static void checkErrorMessageResponse(RestResponse errorResponse, ActionStatus actionStatus)
105 throws FileNotFoundException {
107 ErrorInfo expectedError = ErrorValidationUtils.parseErrorConfigYaml(actionStatus.name());
108 String expectedMessage = expectedError.getMessage();
111 ResponseFormat responseFormat = ResponseParser.parseToObjectUsingMapper(errorResponse.getResponse(),
112 ResponseFormat.class);
113 String actualMessage = responseFormat.getText();
115 assertEquals("Unexpected error message", expectedMessage, actualMessage);