Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

# 37 - Add debug logs calls into action response logic to provide use… #38

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ import scala.util.{Failure, Success}
* Object `ScAPIRunner` serves as the main entry point for the ScAPI runner.
*/
object ScAPIRunner {


/**
* The main method that is being invoked to run the ScAPI runner.
*
* @param args Command-line arguments.
*/
def main(args: Array[String]): Unit = {
def run(args: Array[String]): String = {
val cmd = ScAPIRunnerConfig.getCmdLineArguments(args) match {
case Success(value) => value
case Failure(exception) => throw exception
Expand All @@ -58,10 +60,16 @@ object ScAPIRunner {
// run tests and result reporting - use categories for test filtering
if (cmd.validateOnly) {
Logger.info("Validate only => end run.")
""
} else {
Logger.info("Running tests")
val suiteResults: List[SuiteResult] = SuiteRunner.runSuites(suiteBundles, environment, () => new RestClient(ScAPIRequestSender))
StdOutReporter.printReport(suiteResults)
}
}

def main(args: Array[String]): Unit = {
val output = run(args)
Logger.info(s"\n$output")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ object StdOutReporter {
*
* @param testResults The set of test suite results to be reported.
*/
def printReport(testResults: List[SuiteResult]): Unit = {
def printReport(testResults: List[SuiteResult]): String = {
val report = new StringBuilder()

def addToReport(text: String): Unit = report.append(text + "\n")
def createFormattedLine(line: Option[String] = None, maxChars: Int = 80, repeatChar: Char = '*'): String =
line match {
case Some(text) => s"${repeatChar.toString * ((maxChars - text.length - 2) / 2)} $text ${repeatChar.toString * ((maxChars - text.length - 2) / 2)}"
Expand All @@ -42,18 +45,18 @@ object StdOutReporter {
else math.max(testResults.flatMap(_.categories.flatMap(c => Option(c).map(_.split(",").length))).maxOption.getOrElse(0) + 3, 10)
val maxChars = 33 + maxSuiteLength + maxTestLength + maxTestCategoriesLength

def printTableRowSplitter(): Unit = println(s"| ${"-" * maxSuiteLength} | ${"-" * maxTestLength} | ${"-" * 13} | ${"-" * 7} | ${"-" * maxTestCategoriesLength} |")
def printFormattedLineHeader(): Unit = println(createFormattedLine(maxChars = maxChars))
def printFormattedLineNoHeader(): Unit = println(createFormattedLine(repeatChar = '-', maxChars = maxChars))
def printTableRowSplitter(): Unit = addToReport(s"| ${"-" * maxSuiteLength} | ${"-" * maxTestLength} | ${"-" * 13} | ${"-" * 7} | ${"-" * maxTestCategoriesLength} |")
def printFormattedLineHeader(): Unit = addToReport(createFormattedLine(maxChars = maxChars))
def printFormattedLineNoHeader(): Unit = addToReport(createFormattedLine(repeatChar = '-', maxChars = maxChars))
def printHeader(title: String): Unit = {
printFormattedLineHeader()
println(createFormattedLine(Some(title), maxChars = maxChars))
addToReport(createFormattedLine(Some(title), maxChars = maxChars))
printFormattedLineHeader()
}
def printInnerHeader(title: String): Unit = {
println()
addToReport("")
printFormattedLineNoHeader()
println(s"$title:")
addToReport(s"$title:")
printFormattedLineNoHeader()
}

Expand All @@ -62,9 +65,9 @@ object StdOutReporter {
val successCount = testResults.count(r => r.isSuccess && r.resultType == SuiteResultType.TestSet)
val failureCount = testResults.count(r => !r.isSuccess && r.resultType == SuiteResultType.TestSet)

println(s"Number of tests run: ${successCount + failureCount}")
println(s"Number of successful tests: $successCount")
println(s"Number of failed tests: $failureCount")
addToReport(s"Number of tests run: ${successCount + failureCount}")
addToReport(s"Number of successful tests: $successCount")
addToReport(s"Number of failed tests: $failureCount")

if (testResults.nonEmpty) {
val suiteSummary = testResults
Expand All @@ -77,17 +80,17 @@ object StdOutReporter {
printInnerHeader("Suites Summary")
suiteSummary.foreach {
case (suiteName, total, successCount) =>
println(s"Suite: $suiteName, Total tests: $total, Successful: $successCount, Failed: ${total - successCount}")
addToReport(s"Suite: $suiteName, Total tests: $total, Successful: $successCount, Failed: ${total - successCount}")
}

printInnerHeader("Summary of all tests")
printTableRowSplitter()
println(s"| %-${maxSuiteLength}s | %-${maxTestLength}s | %-13s | %-7s | %-${maxTestCategoriesLength}s | ".format("Suite Name", "Test Name", "Duration (ms)", "Status", "Categories"))
addToReport(s"| %-${maxSuiteLength}s | %-${maxTestLength}s | %-13s | %-7s | %-${maxTestCategoriesLength}s | ".format("Suite Name", "Test Name", "Duration (ms)", "Status", "Categories"))
printTableRowSplitter()
val resultsList = testResults.filter(_.resultType == SuiteResultType.TestSet)
resultsList.zipWithIndex.foreach { case (result, index) =>
val duration = result.duration.map(_.toString).getOrElse("NA")
println(s"| %-${maxSuiteLength}s | %-${maxTestLength}s | %13s | %-7s | %-${maxTestCategoriesLength}s | ".format(
addToReport(s"| %-${maxSuiteLength}s | %-${maxTestLength}s | %13s | %-7s | %-${maxTestCategoriesLength}s | ".format(
result.suiteName,
result.name,
duration,
Expand All @@ -101,20 +104,21 @@ object StdOutReporter {
if (failureCount > 0) {
printInnerHeader("Details of failed tests")
testResults.filter(!_.isSuccess).foreach { result =>
println(s"Suite: ${result.suiteName}")
addToReport(s"Suite: ${result.suiteName}")
result.resultType match {
case SuiteResultType.BeforeTestSet => println(s"Before: ${result.name}")
case SuiteResultType.TestSet => println(s"Test: ${result.name}")
case SuiteResultType.AfterTestSet => println(s"After: ${result.name}")
case SuiteResultType.BeforeTestSet => addToReport(s"Before: ${result.name}")
case SuiteResultType.TestSet => addToReport(s"Test: ${result.name}")
case SuiteResultType.AfterTestSet => addToReport(s"After: ${result.name}")
}
println(s"Error: ${result.errorMsg.getOrElse("No details available")}")
println(s"Duration: ${result.duration.getOrElse("NA")} ms")
println(s"Category: ${result.categories}")
println()
addToReport(s"Error: ${result.errorMsg.getOrElse("No details available")}")
addToReport(s"Duration: ${result.duration.getOrElse("NA")} ms")
addToReport(s"Category: ${result.categories}")
addToReport("")
}
}
}

printHeader("End Report")
report.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ object AssertionResponseAction extends ResponseActions {
* @throws UndefinedResponseActionTypeException If the response action type is not recognized.
*/
def validateContent(responseAction: ResponseAction): Unit = {
Logger.debug(s"Validating content for response action. \nResponseAction: $responseAction")

val action = fromString(responseAction.name.toLowerCase).getOrElse(None)
action match {

Expand Down Expand Up @@ -153,6 +155,8 @@ object AssertionResponseAction extends ResponseActions {
* @return A Try[Unit] indicating the success of the assertion operation.
*/
def performResponseAction(response: Response, responseAction: ResponseAction): Try[Unit] = {
Logger.debug(s"Performing response action: \nResponse: $response, \nResponseAction: $responseAction")

val action = fromString(responseAction.name.toLowerCase).getOrElse(None)
action match {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ object ExtractJsonResponseAction extends ResponseActions {
* @throws UndefinedResponseActionTypeException if an unsupported assertion type is encountered.
*/
def validateContent(responseAction: ResponseAction): Unit = {
Logger.debug(s"Validating content for response action. \nResponseAction: $responseAction")

val action = fromString(responseAction.name.toLowerCase).getOrElse(None)
action match {
case StringFromList => validateStringFromList(responseAction)
Expand All @@ -55,6 +57,8 @@ object ExtractJsonResponseAction extends ResponseActions {
* @throws UndefinedResponseActionTypeException if an unsupported response action name is encountered.
*/
def performResponseAction(response: Response, responseAction: ResponseAction): Try[Unit] = {
Logger.debug(s"Performing response action: \nResponse: $response, \nResponseAction: $responseAction")

val action = fromString(responseAction.name.toLowerCase).getOrElse(None)
action match {
case StringFromList =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ object LogResponseAction extends ResponseActions {
* @throws UndefinedResponseActionTypeException if the response action's name is not recognized.
*/
def validateContent(responseAction: ResponseAction): Unit = {
Logger.debug(s"Validating content for response action. \nResponseAction: $responseAction")

val action = fromString(responseAction.name.toLowerCase).getOrElse(None)
action match {
case Error | Warn | Info | Debug =>
Expand All @@ -58,6 +60,8 @@ object LogResponseAction extends ResponseActions {
* @throws PropertyNotFoundException if the required 'message' parameter is missing.
*/
def performResponseAction(response: Response, responseAction: ResponseAction): Try[Unit] = {
Logger.debug(s"Performing response action. \nResponse: $response, \nResponseAction: $responseAction")

val message = responseAction.params.getOrElse("message", return Failure(PropertyNotFoundException("Missing 'message' parameter")))
val action = fromString(responseAction.name.toLowerCase).getOrElse(None)
Try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"constants": {
"server": "localhost",
"port": "9967",
"basicToken": "SecretToken",
"ownerCellPhoneNumber": "6085551749"
},
"properties": {
"url": "http://{{ server}}:{{ port }}/petclinic"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@
{
"method": "log.info",
"message": "{{ cache.ownerId }}"
},
{
"method": "log.info",
"message": "{{ cache.ownerName }}"
}
]
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ class ScAPIRunnerTest extends FunSuite {
}
}

test("call main with minimum params - report of failures") {
val args: Array[String] = Array(
"--env", getClass.getResource("/test_project/localhostBadPort.env.json").getPath,
"--test-root-path", getClass.getResource("/test_project").getPath)
val report = ScAPIRunner.run(args)

assert(report.contains("* Simple Text Report *"))
assert(report.contains("| getOwners Demo Suite | SKIPPED | 0 | Failure | SKIPPED |"))
assert(report.contains("Before: getOwners Demo Before"))
assert(report.contains("Error: Connection refused"))
assert(report.contains("Test: SKIPPED"))
assert(report.contains("Error: Problems during running before suite logic. Details: Suite-Before for Suite: getOwners Demo Suite has failed methods. Not executing main tests and Suite-After."))
}

test("call main with minimum params - validate only") {
val args: Array[String] = Array(
"--env", getClass.getResource("/test_project/localhost.env.json").getPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import africa.absa.testing.scapi.model.suite.SuiteResult
import africa.absa.testing.scapi.model.suite.types.SuiteResultType
import munit.FunSuite

import java.io.ByteArrayOutputStream
import scala.util.{Failure, Success}

class StdOutReporterTest extends FunSuite {
Expand Down Expand Up @@ -73,14 +72,7 @@ class StdOutReporterTest extends FunSuite {
printReport
*/
test("empty results") {
val baos = new ByteArrayOutputStream()

Console.withOut(baos) {
StdOutReporter.printReport(List.empty)
}

// Get the output as a string
val output = baos.toString
val output = StdOutReporter.printReport(List.empty)

// Assertions
assert(clue(output.contains("Simple Text Report")))
Expand All @@ -99,14 +91,7 @@ class StdOutReporterTest extends FunSuite {
min 1 suites with min 2 tests
*/

val baos = new ByteArrayOutputStream()

Console.withOut(baos) {
StdOutReporter.printReport(mixedSuccessTestResults)
}

// Get the output as a string
val output = baos.toString
val output = StdOutReporter.printReport(mixedSuccessTestResults)

// Assertions
// report header & tail
Expand All @@ -131,14 +116,7 @@ class StdOutReporterTest extends FunSuite {
}

test("results all success") {
val baos = new ByteArrayOutputStream()

Console.withOut(baos) {
StdOutReporter.printReport(successTestResults)
}

// Get the output as a string
val output = baos.toString
val output = StdOutReporter.printReport(successTestResults)

// Assertions
// report header & tail
Expand Down
Loading