aboutsummaryrefslogtreecommitdiffstats
path: root/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/validation/BaseValidationUtils.java
blob: b29fab4c57dec657edf5bcd794b9aa6d0a39b6ce (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
/*-
 * ============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.utils.validation;

import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.ci.tests.datatypes.enums.ErrorInfo;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
import org.openecomp.sdc.exception.ResponseFormat;
import org.testng.Assert;

import java.io.FileNotFoundException;
import java.util.Arrays;

import static org.testng.AssertJUnit.assertEquals;

public class BaseValidationUtils {

	public static final int STATUS_CODE_SUCCESS = 200;
	public static final int STATUS_CODE_CREATED = 201;
	public static final int STATUS_CODE_DELETE = 204;
	public static final int STATUS_CODE_NOT_FOUND = 404;
	public static final int STATUS_CODE_SUCCESS_NO_CONTENT = 204;
	public static final int STATUS_CODE_SUCCESS_DELETE = 204;
	public static final int STATUS_CODE_INVALID_CONTENT = 400;
	public static final int STATUS_CODE_MISSING_DATA = 400;
	public static final int STATUS_CODE_MISSING_INFORMATION = 403;
	public static final int STATUS_CODE_RESTRICTED_ACCESS = 403;
	public static final int STATUS_CODE_RESTRICTED_OPERATION = 409;
	public static final int STATUS_CODE_ALREADY_EXISTS = 409;

	// ------
	protected static Boolean checkErrorCode(RestResponse deleteResponse) {
		if (deleteResponse.getErrorCode() == STATUS_CODE_SUCCESS
				|| deleteResponse.getErrorCode() == STATUS_CODE_DELETE) {
			return true;
		}
		return false;
	}

	// *** STATUS CODE VALIDATION UTIITIES ****
	public static void checkStatusCode(RestResponse response, String assertMessage, boolean AND, int... statuses) {
		int statusCode = response.getErrorCode();
		for (int status : statuses) {
			if (AND && statusCode != status) {
				Assert.fail(assertMessage + " status: " + statusCode);
			} else if (statusCode == status) {
				return;
			}
		}
		if (!AND) {
			Assert.fail(assertMessage + " status: " + statusCode);
		}
	}

	public static void checkDeleteResponse(RestResponse response) {
		checkStatusCode(response, "delete request failed", false, STATUS_CODE_DELETE, STATUS_CODE_NOT_FOUND,
				STATUS_CODE_SUCCESS); // STATUS_CODE_SUCCESS for deActivate user
	}

	public static void checkCreateResponse(RestResponse response) {
		checkStatusCode(response, "create request failed", false, STATUS_CODE_CREATED);
	}

	public static void checkSuccess(RestResponse response) {
		checkStatusCode(response, "request failed", false, STATUS_CODE_SUCCESS);
	}

	public static void checkErrorResponse(RestResponse errorResponse, ActionStatus actionStatus,
			String... expectedVariables) throws FileNotFoundException {
		// Expected error
		ErrorInfo expectedError = ErrorValidationUtils.parseErrorConfigYaml(actionStatus.name());
		String expectedMessage = expectedError.getMessage();

		// Actual error
		ResponseFormat responseFormat = ResponseParser.parseToObjectUsingMapper(errorResponse.getResponse(),
				ResponseFormat.class);
		String actualMessage = responseFormat.getText();
		String[] actualVariables = responseFormat.getVariables();

		assertEquals("Unexpected error message", expectedMessage, actualMessage);
		assertEquals("Unexpected error variables", Arrays.asList(expectedVariables), Arrays.asList(actualVariables));
	}

	public static void checkErrorMessageResponse(RestResponse errorResponse, ActionStatus actionStatus)
			throws FileNotFoundException {
		// Expected error
		ErrorInfo expectedError = ErrorValidationUtils.parseErrorConfigYaml(actionStatus.name());
		String expectedMessage = expectedError.getMessage();

		// Actual error
		ResponseFormat responseFormat = ResponseParser.parseToObjectUsingMapper(errorResponse.getResponse(),
				ResponseFormat.class);
		String actualMessage = responseFormat.getText();

		assertEquals("Unexpected error message", expectedMessage, actualMessage);
	}
}