Skip to content

Commit

Permalink
* Fix - replace Assert type of response actions by enum.
Browse files Browse the repository at this point in the history
* Fix - replace Log type of response actions by enum.
* Fix - replace ExtractJson type of response actions by enum.
  • Loading branch information
miroslavpojer committed Sep 21, 2023
1 parent c203ab6 commit 2e251ae
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,15 @@ case class Action private(methodName: String, url: String, body: Option[String]
}

/**
* Case class that represents ResponseAction.
* This class is used to hold test response action.
* It implements the `ReferenceResolver` trait to support resolution of reference constants.
* Represents a `ResponseAction` case class.
*
* <p>This class encapsulates the details of a test response action and provides
* functionality to resolve reference constants through the `ReferenceResolver` trait.</p>
*
* @constructor create a new ResponseAction with a name and value.
* @param method the "group and name" of the response action.
* @param params the map containing the parameters of the response action. Each key-value pair in the map
* represents a parameter name and its corresponding value.
* @param group The type of the response action group.
* @param name The name that identifies the response action.
* @param params A map containing the parameters of the response action. Each entry in the map
* corresponds to a parameter name and its associated value.
*/
case class ResponseAction private(group: ResponseActionGroupType,
name: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2023 ABSA Group Limited
*
* 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.
*/

package africa.absa.testing.scapi.rest.response

import africa.absa.testing.scapi.rest.response

import scala.language.implicitConversions

object AssertResponseActionType extends Enumeration {
type AssertResponseActionType = Value

// response-time-...
val RESPONSE_TIME_IS_BELOW: response.AssertResponseActionType.Value = Value("response-time-is-below")
val RESPONSE_TIME_IS_ABOVE: response.AssertResponseActionType.Value = Value("response-time-is-above")

// status-code-...
val STATUS_CODE_EQUALS: response.AssertResponseActionType.Value = Value("status-code-equals")
val STATUS_CODE_IS_SUCCESS: response.AssertResponseActionType.Value = Value("status-code-is-success")
val STATUS_CODE_IS_CLIENT_ERROR: response.AssertResponseActionType.Value = Value("status-code-is-client-error")
val STATUS_CODE_IS_SERVER_ERROR: response.AssertResponseActionType.Value = Value("status-code-is-server-error")

// header-...
val HEADER_EXISTS: response.AssertResponseActionType.Value = Value("header-exists")
val HEADER_VALUE_EQUALS: response.AssertResponseActionType.Value = Value("header-value-equals")

// content-type-...
val CONTENT_TYPE_IS_JSON: response.AssertResponseActionType.Value = Value("content-type-is-json")
val CONTENT_TYPE_IS_XML: response.AssertResponseActionType.Value = Value("content-type-is-xml")
val CONTENT_TYPE_IS_HTML: response.AssertResponseActionType.Value = Value("content-type-is-html")

// cookies-...
val COOKIE_EXISTS: response.AssertResponseActionType.Value = Value("cookie-exists")
val COOKIE_VALUE_EQUALS: response.AssertResponseActionType.Value = Value("cookie-value-equals")
val COOKIE_IS_SECURED: response.AssertResponseActionType.Value = Value("cookie-is-secured")
val COOKIE_IS_NOT_SECURED: response.AssertResponseActionType.Value = Value("cookie-is-not-secured")

// body-...
val BODY_CONTAINS_TEXT: response.AssertResponseActionType.Value = Value("body-contains-text")

private val stringToValueMap = values.map(v => v.toString -> v).toMap

def fromString(s: String): Option[AssertResponseActionType] = stringToValueMap.get(s)

// Implicit conversion from AssertResponseActionType to String
implicit def enumValueToString(value: AssertResponseActionType): String = value.toString
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import africa.absa.testing.scapi.json.ResponseAction
import africa.absa.testing.scapi.logging.Logger
import africa.absa.testing.scapi.utils.validation.ContentValidator
import spray.json._
import africa.absa.testing.scapi.rest.response.AssertResponseActionType._

import scala.util.{Failure, Success, Try}
import scala.xml.XML
Expand All @@ -31,42 +32,15 @@ import scala.xml.XML
*/
object AssertionResponseAction extends ResponsePerformer {

// response-time-...
val RESPONSE_TIME_IS_BELOW = "response-time-is-below"
val RESPONSE_TIME_IS_ABOVE = "response-time-is-above"

// status-code-...
val STATUS_CODE_EQUALS = "status-code-equals"
val STATUS_CODE_IS_SUCCESS = "status-code-is-success"
val STATUS_CODE_IS_CLIENT_ERROR = "status-code-is-client-error"
val STATUS_CODE_IS_SERVER_ERROR = "status-code-is-server-error"

// header-...
val HEADER_EXISTS = "header-exists"
val HEADER_VALUE_EQUALS = "header-value-equals"

// content-type-...
val CONTENT_TYPE_IS_JSON = "content-type-is-json"
val CONTENT_TYPE_IS_XML = "content-type-is-xml"
val CONTENT_TYPE_IS_HTML = "content-type-is-html"

// cookies-...
val COOKIE_EXISTS = "cookie-exists"
val COOKIE_VALUE_EQUALS = "cookie-value-equals"
val COOKIE_IS_SECURED = "cookie-is-secured"
val COOKIE_IS_NOT_SECURED = "cookie-is-not-secured"

// body-...
val BODY_CONTAINS_TEXT = "body-contains-text"

/**
* Validates the content of an assertion response action object depending on its type.
*
* @param responseAction The response action object to be validated.
* @throws UndefinedResponseActionTypeException If the response action type is not recognized.
*/
def validateContent(responseAction: ResponseAction): Unit = {
responseAction.name.toLowerCase match {
val action = fromString(responseAction.name.toLowerCase).getOrElse(None)
action match {

// response-time-...
case RESPONSE_TIME_IS_BELOW | RESPONSE_TIME_IS_ABOVE =>
Expand All @@ -89,7 +63,7 @@ object AssertionResponseAction extends ResponsePerformer {
case headerName: String => ContentValidator.validateNonEmptyString(headerName, s"ResponseAssertion.${responseAction.name}.headerName")
case None => throw new IllegalArgumentException(s"Missing required 'headerName' parameter for assertion ${responseAction.name} logic.")
}
responseAction.name.toLowerCase match {
action match {
case HEADER_VALUE_EQUALS =>
responseAction.params.getOrElse("expectedValue", None) match {
case expectedValue: String => ContentValidator.validateNonEmptyString(expectedValue, s"ResponseAssertion.$HEADER_VALUE_EQUALS.expectedValue")
Expand All @@ -107,7 +81,7 @@ object AssertionResponseAction extends ResponsePerformer {
case cookieName: String => ContentValidator.validateNonEmptyString(cookieName, s"ResponseAssertion.${responseAction.name}.cookieName")
case None => throw new IllegalArgumentException(s"Missing required 'cookieName' parameter for assertion ${responseAction.name} logic.")
}
responseAction.name.toLowerCase match {
action match {
case COOKIE_VALUE_EQUALS =>
responseAction.params.getOrElse("expectedValue", None) match {
case expectedValue: String => ContentValidator.validateNonEmptyString(expectedValue, s"ResponseAssertion.$COOKIE_VALUE_EQUALS.expectedValue")
Expand Down Expand Up @@ -135,12 +109,13 @@ object AssertionResponseAction extends ResponsePerformer {
* @throws IllegalArgumentException If the assertion type is not supported.
*/
def performResponseAction(response: Response, responseAction: ResponseAction): Try[Unit] = {
responseAction.name match {
val action = fromString(responseAction.name.toLowerCase).getOrElse(None)
action match {

// response-time-...
case RESPONSE_TIME_IS_BELOW | RESPONSE_TIME_IS_ABOVE =>
val limit = responseAction.params("limit")
responseAction.name match {
action match {
case RESPONSE_TIME_IS_BELOW => assertResponseTimeIsBelow(response, limit)
case RESPONSE_TIME_IS_ABOVE => assertResponseTimeIsAbove(response, limit)
}
Expand All @@ -156,7 +131,7 @@ object AssertionResponseAction extends ResponsePerformer {
// header-...
case HEADER_EXISTS | HEADER_VALUE_EQUALS =>
val headerName = responseAction.params("headerName")
responseAction.name match {
action match {
case HEADER_EXISTS => assertHeaderExists(response, headerName)
case HEADER_VALUE_EQUALS =>
val expectedValue = responseAction.params("expectedValue")
Expand All @@ -171,7 +146,7 @@ object AssertionResponseAction extends ResponsePerformer {
// cookies-...
case COOKIE_EXISTS | COOKIE_VALUE_EQUALS | COOKIE_IS_SECURED | COOKIE_IS_NOT_SECURED =>
val cookieName = responseAction.params("cookieName")
responseAction.name match {
action match {
case COOKIE_EXISTS => assertCookieExists(response, cookieName)
case COOKIE_VALUE_EQUALS =>
val expectedValue = responseAction.params("expectedValue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import africa.absa.testing.scapi.logging.Logger
import africa.absa.testing.scapi.utils.cache.RuntimeCache
import africa.absa.testing.scapi.utils.validation.ContentValidator
import spray.json._
import africa.absa.testing.scapi.rest.response.ExtractJsonResponseActionType._

import scala.util.{Failure, Success, Try}

Expand All @@ -31,16 +32,15 @@ import scala.util.{Failure, Success, Try}
*/
object ExtractJsonResponseAction extends ResponsePerformer {

val STRING_FROM_LIST = "string-from-list"

/**
* Validates the content of an extract response action object depending on its type.
*
* @param responseAction The ResponseAction instance to be validated.
* @throws UndefinedResponseActionTypeException if an unsupported assertion type is encountered.
*/
def validateContent(responseAction: ResponseAction): Unit = {
responseAction.name.toLowerCase match {
val action = fromString(responseAction.name.toLowerCase).getOrElse(None)
action match {
case STRING_FROM_LIST => validateStringFromList(responseAction)
case _ => throw UndefinedResponseActionTypeException(responseAction.name)
}
Expand All @@ -54,7 +54,8 @@ object ExtractJsonResponseAction extends ResponsePerformer {
* @throws IllegalArgumentException if an unsupported response action name is encountered.
*/
def performResponseAction(response: Response, responseAction: ResponseAction): Try[Unit] = {
responseAction.name match {
val action = fromString(responseAction.name.toLowerCase).getOrElse(None)
action match {
case STRING_FROM_LIST =>
val cacheKey = responseAction.params("cacheKey")
val listIndex = responseAction.params("listIndex").toInt
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2023 ABSA Group Limited
*
* 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.
*/

package africa.absa.testing.scapi.rest.response

import africa.absa.testing.scapi.rest.response

import scala.language.implicitConversions

object ExtractJsonResponseActionType extends Enumeration {
type ExtractJsonResponseActionType = Value

val STRING_FROM_LIST: response.ExtractJsonResponseActionType.Value = Value("string-from-list")

private val stringToValueMap = values.map(v => v.toString -> v).toMap

def fromString(s: String): Option[ExtractJsonResponseActionType] = stringToValueMap.get(s)

// Implicit conversion from AssertResponseActionType to String
implicit def enumValueToString(value: ExtractJsonResponseActionType): String = value.toString
}



Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package africa.absa.testing.scapi.rest.response
import africa.absa.testing.scapi.UndefinedResponseActionTypeException
import africa.absa.testing.scapi.json.ResponseAction
import africa.absa.testing.scapi.logging.Logger
import africa.absa.testing.scapi.rest.response.LogResponseActionType._
import africa.absa.testing.scapi.utils.validation.ContentValidator

import scala.util.{Failure, Success, Try}
Expand All @@ -29,25 +30,21 @@ import scala.util.{Failure, Success, Try}
*/
object LogResponseAction extends ResponsePerformer {

val ERROR= "error"
val WARN = "warn"
val INFO = "info"
val DEBUG = "debug"

/**
* Validates the content of an log response action object depending on its type.
*
* @param responseAction The response action to be validated.
* @throws UndefinedResponseActionTypeException if the response action's name is not recognized.
*/
def validateContent(responseAction: ResponseAction): Unit = {
responseAction.name.toLowerCase match {
val action = fromString(responseAction.name.toLowerCase).getOrElse(None)
action match {
case ERROR | WARN | INFO | DEBUG =>
responseAction.params.get("message") match {
case Some(message) => ContentValidator.validateNonEmptyString(message, s"ResponseLog.${responseAction.name}.message")
case None => throw new IllegalArgumentException(s"Missing required 'message' for assertion ${responseAction.name} logic.")
}
case _ => throw UndefinedResponseActionTypeException(responseAction.name)
case _ => throw UndefinedResponseActionTypeException(responseAction.name.toString)
}
}

Expand All @@ -60,8 +57,8 @@ object LogResponseAction extends ResponsePerformer {
*/
def performResponseAction(response: Response, responseAction: ResponseAction): Try[Unit] = {
val message = responseAction.params.getOrElse("message", return Failure(new IllegalArgumentException("Missing 'message' parameter")))

responseAction.name match {
val action = fromString(responseAction.name.toLowerCase).getOrElse(None)
action match {
case ERROR => logError(message)
case WARN => logWarn(message)
case INFO => logInfo(message)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2023 ABSA Group Limited
*
* 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.
*/

package africa.absa.testing.scapi.rest.response

import africa.absa.testing.scapi.rest.response

import scala.language.implicitConversions

object LogResponseActionType extends Enumeration {
type LogResponseActionType = Value

val ERROR: response.LogResponseActionType.Value = Value("error")
val WARN: response.LogResponseActionType.Value = Value("warn")
val INFO: response.LogResponseActionType.Value = Value("info")
val DEBUG: response.LogResponseActionType.Value = Value("debug")

private val stringToValueMap = values.map(v => v.toString -> v).toMap

def fromString(s: String): Option[LogResponseActionType] = stringToValueMap.get(s)

// Implicit conversion from AssertResponseActionType to String
implicit def enumValueToString(value: LogResponseActionType): String = value.toString
}
Loading

0 comments on commit 2e251ae

Please sign in to comment.