Skip to content

Commit

Permalink
Code refactored for Discovery API Integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HasiniSama committed Jun 26, 2024
1 parent bec4c80 commit a85c40c
Show file tree
Hide file tree
Showing 13 changed files with 463 additions and 310 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/*
* Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2019-2024, WSO2 LLC. (http://www.wso2.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* WSO2 LLC. licenses this file to you 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.
* 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.
*/

package org.wso2.identity.integration.test.rest.api.common;
Expand Down Expand Up @@ -321,6 +323,21 @@ protected Response getResponseOfGetWithQueryParams(String endpointUri, Map<Strin
.get(endpointUri);
}

/**
* Invoke given endpointURL for GET with OAuth2 authentication, using the provided token.
*
* @param endpointURL Endpoint to be invoked.
* @param accessToken OAuth2 access token.
* @return Response.
*/
protected Response getResponseOfGetWithOAuth2(String endpointURL, String accessToken) {

return given().auth().preemptive().oauth2(accessToken)
.contentType(ContentType.JSON)
.when()
.get(endpointURL);
}

/**
* Invoke given endpointUri for GET without authentication.
*
Expand Down Expand Up @@ -388,6 +405,24 @@ protected Response getResponseOfPost(String endpointUri, String body, String con
.post(endpointUri);
}

/**
* Invoke the given endpointURL for POST with the provided body and OAuth2 authentication, using the provided
* access token.
*
* @param endpointURL Endpoint to be invoked.
* @param body Payload.
* @param accessToken OAuth2 access token.
* @return Response.
*/
protected Response getResponseOfPostWithOAuth2(String endpointURL, String body, String accessToken) {

return given().auth().preemptive().oauth2(accessToken)
.contentType(ContentType.JSON)
.body(body)
.when()
.post(endpointURL);
}

/**
* Uploads a file to a given endpointUri with a POST request.
*
Expand Down Expand Up @@ -643,6 +678,24 @@ protected Response getResponseOfPut(String endpointUri, String body, String cont
.put(endpointUri);
}

/**
* Invoke the given endpointURL for PUT with the provided body and OAuth2 authentication, using the provided
* access token.
*
* @param endpointURL Endpoint to be invoked.
* @param body Payload.
* @param accessToken OAuth2 access token.
* @return Response.
*/
protected Response getResponseOfPutWithOAuth2(String endpointURL, String body, String accessToken) {

return given().auth().preemptive().oauth2(accessToken)
.contentType(ContentType.JSON)
.body(body)
.when()
.put(endpointURL);
}

/**
* Invoke given endpointUri for PATCH request with given body, headers and Basic authentication, authentication
* credential being the authenticatingUserName and authenticatingCredential.
Expand Down Expand Up @@ -749,13 +802,28 @@ protected Response getResponseOfDelete(String endpointURI, Map<String, String> h
.delete(endpointURI);
}

/**
* Invoke the given endpointURL for DELETE with OAuth2 authentication, using the provided access token.
*
* @param endpointURL Endpoint to be invoked.
* @param accessToken OAuth2 access token.
* @return Response.
*/
protected Response getResponseOfDeleteWithOAuth2(String endpointURL, String accessToken) {

return given().auth().preemptive().oauth2(accessToken)
.contentType(ContentType.JSON)
.when()
.delete(endpointURL);
}

/**
* Validate the response to be in the following desired format of Error Response
* {
* "code": "some_error_code",
* "message": "Some Error Message",
* "description": "Some Error Description",
* "traceId"" : "corelation-id",
* "traceId"" : "correlation-id",
* }
*
* @param response API error response
Expand All @@ -774,6 +842,29 @@ protected void validateErrorResponse(Response response, int httpStatusCode, Stri
validateErrorDescription(response, errorCode, errorDescriptionArgs);
}

/**
* Validate the response to be in the following desired format of Error Response.
* {
* "code": "some_error_code",
* "message": "Some Error Message",
* "description": "Some Error Description"
* }
*
* @param response API error response.
* @param httpStatusCode HTTP status code.
* @param errorCode Error code.
* @param errorDescriptionArgs Placeholder values if error description in RESTAPIErrors.properties contains
* dynamic values.
*/
protected void validateErrorResponseWithoutTraceId(Response response, int httpStatusCode, String errorCode,
String... errorDescriptionArgs) {

validateHttpStatusCode(response, httpStatusCode);
validateResponseElement(response, "code", is(errorCode));
validateErrorMessage(response, errorCode);
validateErrorDescription(response, errorCode, errorDescriptionArgs);
}

/**
* Validate http status code of the response
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.identity.integration.test.rest.api.server.organization.management.v1;

import io.restassured.RestAssured;
Expand Down
Loading

0 comments on commit a85c40c

Please sign in to comment.