diff --git a/docs/manual/testdefinition/sakuli-api-application.adoc b/docs/manual/testdefinition/sakuli-api-application.adoc index f4c7053c..e362ec28 100644 --- a/docs/manual/testdefinition/sakuli-api-application.adoc +++ b/docs/manual/testdefinition/sakuli-api-application.adoc @@ -12,7 +12,7 @@ Application Class - Represents an application. *Params* -* applicationNameOrPath `String` - Path to the application file. *Example:* `C:\Windows\system32\notepad.exe` +* applicationNameOrPath `String` - Path to the application file. Example: `C:\Windows\system32\notepad.exe` * optResumeOnException `Boolean` - Determines whether to ignore exceptions from this class. If this parameter is undefined, it will be false. diff --git a/docs/manual/testdefinition/sakuli-api-environment.adoc b/docs/manual/testdefinition/sakuli-api-environment.adoc index 8107478c..85835981 100644 --- a/docs/manual/testdefinition/sakuli-api-environment.adoc +++ b/docs/manual/testdefinition/sakuli-api-environment.adoc @@ -107,7 +107,7 @@ Takes a screenshot of the current screen and add the current timestamp in the fi * optFormat `string` - optional format, for the screenshot (currently supported: jpg and png) If null or empty use property `sakuli.screenshot.format` -*Returns*: `String` - file path to the created screenshot OR null on errors +*Returns*: `Path` - to the created screenshot OR null on errors *Example* [source,js] ---- diff --git a/docs/manual/testdefinition/sakuli-api-testcase.adoc b/docs/manual/testdefinition/sakuli-api-testcase.adoc index 3d57fddd..f0d6f076 100644 --- a/docs/manual/testdefinition/sakuli-api-testcase.adoc +++ b/docs/manual/testdefinition/sakuli-api-testcase.adoc @@ -12,9 +12,10 @@ TestCase - initializes the Sakuli object and sets the warning and critical time *Params* -* warningTime `number` - threshold in seconds. If the threshold is set to 0, +* optCaseId `String` - optional ID to set the testcase ID to some specifc value. Default is the folder name. +* optWarningTime `number` - threshold in seconds. If the threshold is not set or is set to 0, the execution time will never exceed, so the state will be always OK! -* criticalTime `number` - threshold in seconds. If the threshold is set to 0, +* optCriticalTime `number` - threshold in seconds. If the threshold is not set or is set to 0, the execution time will never exceed, so the state will be always OK! * optImagePathArray `Array.<String>` - (optional) Path or Array of Paths to the folder containing the image patterns for these test cases. @@ -23,16 +24,20 @@ TestCase - initializes the Sakuli object and sets the warning and critical time *Example* [source,js] ---- -var testCase = new TestCase(20,30, "path-to/image-folder-name"); +//new syntex since v1.2.0 +var testCase = new TestCase(["own-case-id", 20, 30, "path-to/image-folder-name"]); + +//old syntax < v1.2.0 +var testCase = new TestCase(20, 30, ["path-to/image-folder-name"]); ---- *Members* * link:#TestCase[TestCase] * link:#TestCase.addImagePaths[TestCase.addImagePaths(imagePaths)] -* link:#TestCase.endOfStep[TestCase.endOfStep(stepName, optWarningTime)] +* link:#TestCase.endOfStep[TestCase.endOfStep(stepName, optWarningTime, optCriticalTime, optForward)] * link:#TestCase.handleException[TestCase.handleException(e)] -* link:#TestCase.saveResult[TestCase.saveResult()] +* link:#TestCase.saveResult[TestCase.saveResult(optForward)] * link:#TestCase.getID[TestCase.getID()] * link:#TestCase.getLastURL[TestCase.getLastURL()] * link:#TestCase.getTestCaseFolderPath[TestCase.getTestCaseFolderPath()] @@ -52,7 +57,7 @@ If a relative path is assigned, the current testcase folder will be used as curr [[TestCase.endOfStep]] -.TestCase.endOfStep(stepName, optWarningTime) +.TestCase.endOfStep(stepName, optWarningTime, optCriticalTime, optForward) A step allows to sub-divide a case to measure logical units, such as "login", "load report" etc. in its particular runtime. When a case starts, Sakuli starts a "step" timer. It gets read out, stored with the @@ -65,6 +70,11 @@ If the step runtime exceeds the step threshold (second parameter, optional), the * stepName `String` * optWarningTime `number` - (optional) threshold in seconds, default = 0. If the threshold is set to 0, the execution time will never exceed, so the state will be always OK! +* optCriticalTime `number` - (optional) threshold in seconds, default = 0. If the threshold is set to 0, + the execution time will never exceed, so the state will be always OK! +* optForward `boolean` - (optional) indicate whether the result of the test step shall be immediately + processed by the enabled forwarders. This means before the test suite has been executed to + the end. If not specified in another way, this option is disabled! Default: `false` [[TestCase.handleException]] @@ -97,6 +107,10 @@ Saves the results of the current test case for further processing. Should be called in finally-block of the test case: +*Params* + +* optForward `boolean` - indicate whether the result of the test case shall be immediately processed by the enabled forwarders. This means before the test suite has been executed to the end. If not specified in another way, this option is disabled! Default: `false` + *Example* [source,js] ---- diff --git a/docs/manual/testdefinition/sakuli-api.adoc b/docs/manual/testdefinition/sakuli-api.adoc index 8d7dda7d..3d29b864 100644 --- a/docs/manual/testdefinition/sakuli-api.adoc +++ b/docs/manual/testdefinition/sakuli-api.adoc @@ -26,9 +26,9 @@ IMPORTANT: All classes and methods are implemented identical for Java and JavaSc * link:#Sahi-API[Sahi-API] * link:#TestCase[TestCase] ** link:#TestCase.addImagePaths[TestCase.addImagePaths(imagePaths)] -** link:#TestCase.endOfStep[TestCase.endOfStep(stepName, optWarningTime)] +** link:#TestCase.endOfStep[TestCase.endOfStep(stepName, optWarningTime, optCriticalTime, optForward)] ** link:#TestCase.handleException[TestCase.handleException(e)] -** link:#TestCase.saveResult[TestCase.saveResult()] +** link:#TestCase.saveResult[TestCase.saveResult(optForward)] ** link:#TestCase.getID[TestCase.getID()] ** link:#TestCase.getLastURL[TestCase.getLastURL()] ** link:#TestCase.getTestCaseFolderPath[TestCase.getTestCaseFolderPath()] diff --git a/docs/manual/testdefinition/sakuli-api.md b/docs/manual/testdefinition/sakuli-api.md index 0a8b262d..03e6a005 100644 --- a/docs/manual/testdefinition/sakuli-api.md +++ b/docs/manual/testdefinition/sakuli-api.md @@ -5,9 +5,9 @@ * [Sahi-API](#Sahi-API) * [TestCase](#TestCase) * [TestCase.addImagePaths(imagePaths)](#TestCase.addImagePaths) - * [TestCase.endOfStep(stepName, optWarningTime)](#TestCase.endOfStep) + * [TestCase.endOfStep(stepName, optWarningTime, optCriticalTime, optForward)](#TestCase.endOfStep) * [TestCase.handleException(e)](#TestCase.handleException) - * [TestCase.saveResult()](#TestCase.saveResult) + * [TestCase.saveResult(optForward)](#TestCase.saveResult) * [TestCase.getID()](#TestCase.getID) * [TestCase.getLastURL()](#TestCase.getLastURL) * [TestCase.getTestCaseFolderPath()](#TestCase.getTestCaseFolderPath) @@ -123,9 +123,10 @@ TestCase - initializes the Sakuli object and sets the warning and critical time **Params** -- warningTime `number` - threshold in seconds. If the threshold is set to 0, +- optCaseId `String` - optional ID to set the testcase ID to some specifc value. Default is the folder name. +- optWarningTime `number` - threshold in seconds. If the threshold is not set or is set to 0, the execution time will never exceed, so the state will be always OK! -- criticalTime `number` - threshold in seconds. If the threshold is set to 0, +- optCriticalTime `number` - threshold in seconds. If the threshold is not set or is set to 0, the execution time will never exceed, so the state will be always OK! - optImagePathArray `Array.` - (optional) Path or Array of Paths to the folder containing the image patterns for these test cases. @@ -133,16 +134,20 @@ TestCase - initializes the Sakuli object and sets the warning and critical time **Returns**: - an initialized Sakuli object. **Example** ``` -var testCase = new TestCase(20,30, "path-to/image-folder-name"); +//new syntex since v1.2.0 +var testCase = new TestCase(["own-case-id", 20, 30, "path-to/image-folder-name"]); + +//old syntax < v1.2.0 +var testCase = new TestCase(20, 30, ["path-to/image-folder-name"]); ``` **Members** * [TestCase](#TestCase) * [TestCase.addImagePaths(imagePaths)](#TestCase.addImagePaths) - * [TestCase.endOfStep(stepName, optWarningTime)](#TestCase.endOfStep) + * [TestCase.endOfStep(stepName, optWarningTime, optCriticalTime, optForward)](#TestCase.endOfStep) * [TestCase.handleException(e)](#TestCase.handleException) - * [TestCase.saveResult()](#TestCase.saveResult) + * [TestCase.saveResult(optForward)](#TestCase.saveResult) * [TestCase.getID()](#TestCase.getID) * [TestCase.getLastURL()](#TestCase.getLastURL) * [TestCase.getTestCaseFolderPath()](#TestCase.getTestCaseFolderPath) @@ -159,11 +164,11 @@ If a relative path is assigned, the current testcase folder will be used as curr - imagePaths `string` - one or more path strings -##TestCase.endOfStep(stepName, optWarningTime) +##TestCase.endOfStep(stepName, optWarningTime, optCriticalTime, optForward) A step allows to sub-divide a case to measure logical units, such as "login", "load report" etc. in its particular runtime. When a case starts, Sakuli starts a "step" timer. It gets read out, stored with the step name, and the timer will set to `0` each time endOfStep() is called. -If the step runtime exceeds the step threshold (second parameter, optional), the step is saved with state +If the step runtime exceeds the step threshold (second parameter, optional), the step is saved with state "WARNING" (there is no CRITICAL state). **Params** @@ -171,6 +176,11 @@ If the step runtime exceeds the step threshold (second parameter, optional), the - stepName `String` - optWarningTime `number` - (optional) threshold in seconds, default = 0. If the threshold is set to 0, the execution time will never exceed, so the state will be always OK! +- optCriticalTime `number` - (optional) threshold in seconds, default = 0. If the threshold is set to 0, + the execution time will never exceed, so the state will be always OK! +- optForward `boolean` - (optional) indicate whether the result of the test step shall be immediately + processed by the enabled forwarders. This means before the test suite has been executed to + the end. If not specified in another way, this option is disabled! Default: `false` ##TestCase.handleException(e) @@ -193,11 +203,15 @@ try { ``` -##TestCase.saveResult() +##TestCase.saveResult(optForward) Saves the results of the current test case for further processing. Should be called in finally-block of the test case: +**Params** + +- optForward `boolean` - indicate whether the result of the test case shall be immediately processed by the enabled forwarders. This means before the test suite has been executed to the end. If not specified in another way, this option is disabled! Default: `false` + **Example** ``` try { diff --git a/pom.xml b/pom.xml index c26815bb..7db3fad2 100644 --- a/pom.xml +++ b/pom.xml @@ -76,6 +76,7 @@ unit ui sakuli-test + 2.5.0 @@ -403,6 +404,16 @@ + + + org.checkerframework + checker-qual + + + org.checkerframework + jdk8 + + org.springframework @@ -415,7 +426,7 @@ org.mockito - mockito-all + mockito-core @@ -630,8 +641,8 @@ org.mockito - mockito-all - 1.9.5 + mockito-core + 2.18.0 test @@ -649,6 +660,17 @@ gson 2.8.1 + + + org.checkerframework + checker-qual + ${dependencies.checker.version} + + + org.checkerframework + jdk8 + ${dependencies.checker.version} + diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/check_mk/main.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/check_mk/main.twig index 967bbe4c..7a202536 100644 --- a/src/common/src/main/resources/org/sakuli/common/config/templates/check_mk/main.twig +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/check_mk/main.twig @@ -1,8 +1,8 @@ {% spaceless %} {% import 'error_message_creator.twig' as errorMessageCreator %} <<>>$newline$ - {% include 'performance_data.twig' %} - {% include 'short_summary.twig' %} - {% include 'detailed_summary.twig' %} + {% include 'performance_data.twig' with { testsuite: testDataEntity}%} + {% include 'short_summary.twig' with { testsuite: testDataEntity}%} + {% include 'detailed_summary.twig' with { testsuite: testDataEntity}%} $newline$ {% endspaceless %} diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/case_information.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/case_information.twig index 4ee47195..7bf73e33 100644 --- a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/case_information.twig +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/case_information.twig @@ -1,14 +1,10 @@ -{% for testCaseId, testCase in testsuite.testCasesAsSortedSet %} - {% if (testCase.state.warning || testCase.state.critical || testCase.state.error) %} - , case "{{ testCase.name }}"$whitespace$ - {% if (testCase.state.error) %} - EXCEPTION: {{ errorMessageCreator.exceptionMessageTestCase(testCase) }} - {% else %} - {% if (testCase.state.critical) %} - over runtime ( {{ testCase.duration | number_format(2, '.') }}s/crit at {{ testCase.criticalTime }}s) - {% else %} - over runtime ( {{ testCase.duration | number_format(2, '.') }}s/warn at {{ testCase.warningTime }}s) - {% endif %} - {% endif %} +{% if (testCase.state.warning || testCase.state.critical || testCase.state.error) %} + , case "{{ testCase.id }}"$whitespace$ + {% if (testCase.state.error) %} + EXCEPTION: {{ errorMessageCreator.exceptionMessageTestCase(testCase) }} + {% elseif (testCase.state.critical) %} + over runtime ( {{ testCase.duration | number_format(2, '.') }}s/crit at {{ testCase.criticalTime }}s) + {% else %} + over runtime ( {{ testCase.duration | number_format(2, '.') }}s/warn at {{ testCase.warningTime }}s) {% endif %} -{% endfor %} +{% endif %} diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/detailed_summary.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/detailed_summary.twig index 16ce20ff..d1373209 100644 --- a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/detailed_summary.twig +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/detailed_summary.twig @@ -1,78 +1,8 @@ -{# This template renders the detailed summary for the current test suite. #} -{% include 'detailed_summary_css_styles.twig' %} - - - - - {% for testCase in testsuite.testCasesAsSortedSet %} - - - - {% endfor %} -
- {{ getOutputState(testsuite.state).shortState }} Sakuli suite "{{ testsuite.id }}"$whitespace$ - {% if (testsuite.state.error) %} - {% else %} - {{ testsuite.state.nagiosStateDescription }}$whitespace$ - {% endif %} - {% if (testsuite.state.ok) %} - ({{ testsuite.duration | number_format(2, '.') }}s)$whitespace$ - {% elseif (testsuite.state.name == 'WARNING_IN_SUITE') %} - ({{ testsuite.duration | number_format(2, '.') }}s/warn at {{ testsuite.warningTime }}s)$whitespace$ - {% elseif (testsuite.state.name == 'CRITICAL_IN_SUITE') %} - ({{ testsuite.duration | number_format(2, '.') }}s/crit at {{ testsuite.criticalTime }}s)$whitespace$ - {% elseif (testsuite.state.name == 'WARNING_IN_STEP') %} - {% for testCaseId, testCase in testsuite.testCasesAsSortedSet %} - {% if (testCase.state.warningInStep) %} - {% for testStep in testCase.steps %} - {% if (testStep.state.warning) %} - , step "{{ testStep.id }}" over runtime ({{ testStep.duration | number_format(2, '.') }}s/warn at {{ testStep.warningTime }}s) - {% endif %} - {% endfor %} - {% endif %} - {% endfor %} - {% elseif (testsuite.state.name == 'WARNING_IN_CASE') %} - {% for testCaseId, testCase in testsuite.testCasesAsSortedSet %} - {% if (testCase.state.warning) %} - , case "{{ testCase.id }}" over runtime ({{ testCase.duration | number_format(2, '.') }}s/warn at {{ testCase.warningTime }}s) - {% endif %} - {% endfor %} - {% elseif (testsuite.state.name == 'CRITICAL_IN_CASE') %} - {% for testCaseId, testCase in testsuite.testCasesAsSortedSet %} - {% if (testCase.state.critical) %} - , case "{{ testCase.id }}" over runtime ({{ testCase.duration | number_format(2, '.') }}s/crit at {{ testCase.criticalTime }}s) - {% endif %} - {% endfor %} - {% endif %} - {% if (testsuite.state.error) %} - ({{ testsuite.duration | number_format(2, '.') }}s)$whitespace$ - {{ testsuite.state.nagiosStateDescription }}:$whitespace$ - {{ errorMessageCreator.exceptionMessageTestSuite(testsuite) }}.$whitespace$ - {% endif %} - . (Last suite run:$whitespace$ - {% if (empty(testsuite.stopDate)) %} - xx - {% else %} - {{ testsuite.stopDate|date("dd.MM.YY HH:mm:ss") }} - {% endif %}) - {% include 'exception_screenshot.twig' with { testDataEntity: testsuite } %} -
- {{ getOutputState(testCase.state).shortState }} case "{{ testCase.id }}"$whitespace$ - {% if (testCase.state.ok) %} - ran in {{ testCase.duration | number_format(2, '.') }}s - {{ testCase.state.nagiosStateDescription }} - {% elseif (testCase.state.warning) %} - over runtime ({{ testCase.duration | number_format(2, '.') }}s/warn at {{ testCase.warningTime }}s) - {% include 'step_information.twig' with { testCase: testCase } %}$whitespace$ - {% elseif (testCase.state.critical) %} - {% set errorMessage = errorMessageCreator.exceptionMessageTestCase(testCase) %} - {% if (empty(errorMessage)) %} - over runtime ({{ testCase.duration | number_format(2, '.') }}s/crit at {{ testCase.criticalTime }}s) - {% else %} - EXCEPTION: {{ errorMessageCreator.exceptionMessageTestCase(testCase) }} - {% include 'exception_screenshot.twig' with { testDataEntity: testCase } %} - {% for testStep in testCase.stepsAsSortedSet %} - {% include 'exception_screenshot.twig' with { testDataEntity: testStep } %} - {% endfor %} - {% endif %} - {% endif %} -
+{% set testDataEntityType = getTestDataEntityType(testDataEntity) %} +{% if ('TestSuite' == testDataEntityType) %} + {% include 'detailed_summary_suite.twig' with { testsuite: testDataEntity} %} +{% elseif ('TestCase' == testDataEntityType) %} + {% include 'detailed_summary_case.twig' with { testCase: testDataEntity } %} +{% elseif ('TestCaseStep' == testDataEntityType) %} + {% include 'detailed_summary_step.twig' with { testStep: testDataEntity } %} +{% endif %} diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/detailed_summary_case.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/detailed_summary_case.twig new file mode 100644 index 00000000..44d308b2 --- /dev/null +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/detailed_summary_case.twig @@ -0,0 +1,65 @@ +{# This template renders the detailed summary for the current test case. #} +{% include 'detailed_summary_css_styles.twig' %} + + + + + {% for testStep in testDataEntity.stepsAsSortedSet %} + + + + {% endfor %} +
+ {{ getOutputState(testDataEntity.state).shortState }} Case "{{testDataEntity.id}}" of Sakuli suite "{{testSuiteId}}"$whitespace$ + {% if (testDataEntity.state.error) %} + {% else %} + {{ testDataEntity.state.nagiosStateDescription }}$whitespace$ + {% endif %} + {% if (testDataEntity.state.ok) %} + ({{ testDataEntity.duration | number_format(2, '.') }}s)$whitespace$ + {% elseif (testDataEntity.state.name == 'WARNING') %} + ({{ testDataEntity.duration | number_format(2, '.') }}s/warn at {{ testDataEntity.warningTime }}s)$whitespace$ + {% elseif (testDataEntity.state.name == 'CRITICAL') %} + ({{ testDataEntity.duration | number_format(2, '.') }}s/crit at {{ testDataEntity.criticalTime }}s)$whitespace$ + {% elseif (testDataEntity.state.name == 'WARNING_IN_STEP') %} + {% for testStep in testDataEntity.steps %} + {% if (testStep.state.warning) %} + , step "{{testStep.id}}" over runtime ({{ testStep.duration | number_format(2, '.') }}s/warn at {{ testStep.warningTime }}s) + {% endif %} + {% endfor %} + {% elseif (testDataEntity.state.name == 'CRITICAL_IN_STEP') %} + {% for testStep in testDataEntity.steps %} + {% if (testStep.state.warning) %} + , step "{{testStep.id}}" over runtime ({{ testStep.duration | number_format(2, '.') }}s/crit at {{ testStep.criticalTime }}s) + {% endif %} + {% endfor %} + {% endif %} + {% if (testDataEntity.state.error) %} + ({{ testDataEntity.duration | number_format(2, '.') }}s)$whitespace$ + {{ testDataEntity.state.nagiosStateDescription }}:$whitespace$ + {{ errorMessageCreator.exceptionMessagetestDataEntity(testDataEntity) }}.$whitespace$ + {% endif %} + . (Last suite run:$whitespace$ + {% if (empty(testDataEntity.stopDate)) %} + xx + {% else %} + {{ testDataEntity.stopDate|date("dd.MM.YY HH:mm:ss") }} + {% endif %}) + {% include 'exception_screenshot.twig' with { testDataEntity: testDataEntity } %} +
+ {{ getOutputState(testStep.state).shortState }} step "{{ testStep.id }}"$whitespace$ + {% if (testStep.state.ok) %} + ran in {{ testStep.duration | number_format(2, '.') }}s - {{ testStep.state.nagiosStateDescription }} + {% elseif (testStep.state.warning) %} + over runtime ({{ testStep.duration | number_format(2, '.') }}s/warn at {{ testStep.warningTime }}s) + $whitespace$ + {% elseif (testStep.state.critical) %} + {% set errorMessage = errorMessageCreator.exceptionMessageTestCase(testStep) %} + {% if (empty(errorMessage)) %} + over runtime ({{ testStep.duration | number_format(2, '.') }}s/crit at {{ testStep.criticalTime }}s) + {% else %} + EXCEPTION: {{ errorMessageCreator.exceptionMessageTestCase(testStep) }} + {% include 'exception_screenshot.twig' with { testDataEntity: testStep } %} + {% endif %} + {% endif %} +
diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/detailed_summary_step.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/detailed_summary_step.twig new file mode 100644 index 00000000..4f917bd7 --- /dev/null +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/detailed_summary_step.twig @@ -0,0 +1,32 @@ +{# This template renders the detailed summary for the current test step. #} +{% include 'detailed_summary_css_styles.twig' %} + + + + +
+ {{ getOutputState(testDataEntity.state).shortState }} Step "{{testDataEntity.id}}" in case "{{testCaseId}}"$whitespace$ + {% if (testDataEntity.state.error) %} + {% else %} + {{ testDataEntity.state.nagiosStateDescription }}$whitespace$ + {% endif %} + {% if (testDataEntity.state.ok) %} + ({{ testDataEntity.duration | number_format(2, '.') }}s)$whitespace$ + {% elseif (testDataEntity.state.name == 'WARNING') %} + ({{ testDataEntity.duration | number_format(2, '.') }}s/warn at {{ testDataEntity.warningTime }}s)$whitespace$ + {% elseif (testDataEntity.state.name == 'CRITICAL') %} + ({{ testDataEntity.duration | number_format(2, '.') }}s/crit at {{ testDataEntity.criticalTime }}s)$whitespace$ + {% endif %} + {% if (testDataEntity.state.error) %} + ({{ testDataEntity.duration | number_format(2, '.') }}s)$whitespace$ + {{ testDataEntity.state.nagiosStateDescription }}:$whitespace$ + {{ errorMessageCreator.exceptionMessagetestDataEntity(testDataEntity) }}.$whitespace$ + {% endif %} + . (Last suite run:$whitespace$ + {% if (empty(testDataEntity.stopDate)) %} + xx + {% else %} + {{ testDataEntity.stopDate|date("dd.MM.YY HH:mm:ss") }} + {% endif %}) + {% include 'exception_screenshot.twig' with { testDataEntity: testDataEntity } %} +
diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/detailed_summary_suite.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/detailed_summary_suite.twig new file mode 100644 index 00000000..bee7bfbb --- /dev/null +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/detailed_summary_suite.twig @@ -0,0 +1,79 @@ +{# This template renders the detailed summary for the current test suite. #} +{% include 'detailed_summary_css_styles.twig' %} + + + + + {% for testCase in testsuite.testCasesAsSortedSet %} + + + + {% endfor %} +
+ {{ getOutputState(testsuite.state).shortState }} Sakuli suite "{{ testsuite.id }}"$whitespace$ + {% if (testsuite.state.error) %} + ({{ testsuite.duration | number_format(2, '.') }}s)$whitespace$ + {{ testsuite.state.nagiosStateDescription }}:$whitespace$ + {{ errorMessageCreator.exceptionMessageTestSuite(testsuite) }}.$whitespace$ + {% else %} + {{ testsuite.state.nagiosStateDescription }}$whitespace$ + {% if (testsuite.state.ok) %} + ({{ testsuite.duration | number_format(2, '.') }}s)$whitespace$ + {% elseif (testsuite.state.name == 'WARNING_IN_SUITE') %} + ({{ testsuite.duration | number_format(2, '.') }}s/warn at {{ testsuite.warningTime }}s)$whitespace$ + {% elseif (testsuite.state.name == 'CRITICAL_IN_SUITE') %} + ({{ testsuite.duration | number_format(2, '.') }}s/crit at {{ testsuite.criticalTime }}s)$whitespace$ + {% elseif (testsuite.state.name == 'WARNING_IN_STEP') %} + {% for testCase in testsuite.testCasesAsSortedSet %} + {% if (testCase.state.warningInStep) %} + {% for testStep in testCase.steps %} + {% if (testStep.state.warning) %} + , step "{{testStep.id}}" over runtime ({{ testStep.duration | number_format(2, '.') }}s/warn at {{ testStep.warningTime }}s) + {% endif %} + {% endfor %} + {% endif %} + {% endfor %} + {% elseif (testsuite.state.name == 'WARNING_IN_CASE') %} + {% for testCase in testsuite.testCasesAsSortedSet %} + {% if (testCase.state.warning) %} + , case "{{testCase.id}}" over runtime ({{ testCase.duration | number_format(2, '.') }}s/warn at {{ testCase.warningTime }}s) + {% endif %} + {% endfor %} + {% elseif (testsuite.state.name == 'CRITICAL_IN_CASE') %} + {% for testCase in testsuite.testCasesAsSortedSet %} + {% if (testCase.state.critical) %} + , case "{{testCase.id}}" over runtime ({{ testCase.duration | number_format(2, '.') }}s/crit at {{ testCase.criticalTime }}s) + {% endif %} + {% endfor %} + {% endif %} + {% endif %} + . (Last suite run:$whitespace$ + {% if (empty(testsuite.stopDate)) %} + xx + {% else %} + {{ testsuite.stopDate|date("dd.MM.YY HH:mm:ss") }} + {% endif %}) + {% include 'exception_screenshot.twig' with { testDataEntity: testsuite } %} +
+ {{ getOutputState(testCase.state).shortState }} case "{{ testCase.id }}"$whitespace$ + {% if (testCase.state.ok) %} + ran in {{ testCase.duration | number_format(2, '.') }}s - {{ testCase.state.nagiosStateDescription }} + {% elseif (testCase.state.warning) %} + over runtime ({{ testCase.duration | number_format(2, '.') }}s/warn at {{ testCase.warningTime }}s) + {% for testStep in testCase.stepsAsSortedSet %} + {% include 'step_information.twig' with { testStep: testStep } %} + {% endfor %} + $whitespace$ + {% elseif (testCase.state.critical) %} + {% set errorMessage = errorMessageCreator.exceptionMessageTestCase(testCase) %} + {% if (empty(errorMessage)) %} + over runtime ({{ testCase.duration | number_format(2, '.') }}s/crit at {{ testCase.criticalTime }}s) + {% else %} + EXCEPTION: {{ errorMessageCreator.exceptionMessageTestCase(testCase) }} + {% include 'exception_screenshot.twig' with { testDataEntity: testCase } %} + {% for testStep in testCase.stepsAsSortedSet %} + {% include 'exception_screenshot.twig' with { testDataEntity: testStep } %} + {% endfor %} + {% endif %} + {% endif %} +
diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/main.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/main.twig index d510a8c7..a96f3979 100644 --- a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/main.twig +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/main.twig @@ -1,19 +1,26 @@ {% spaceless %} -{% import 'error_message_creator.twig' as errorMessageCreator %} -type={{ gearman.serviceType }}$newline$ -host_name= + {% import 'error_message_creator.twig' as errorMessageCreator %} + type={{ gearman.serviceType }}$newline$ + host_name= {% if (not empty(gearman.nagiosHost)) %} {{ gearman.nagiosHost }} {% else %} {{ testsuite.host }} {% endif %}$newline$ -start_time={{ convertToUnixTimestamp(testsuite.startDate) }}$newline$ -finish_time={{ convertToUnixTimestamp(testsuite.stopDate) }}$newline$ -return_code={{ testsuite.state.nagiosErrorCode }}$newline$ -service_description={{ gearman.nagiosServiceDescription }}$newline$ -output= - {% include 'short_summary.twig' %}\\n + start_time={{ convertToUnixTimestamp(testDataEntity.startDate) }}$newline$ + finish_time={{ convertToUnixTimestamp(testDataEntity.stopDate) }}$newline$ + return_code={{ testDataEntity.state.nagiosErrorCode }}$newline$ + {% set testDataEntityType = getTestDataEntityType(testDataEntity) %} + {% if ('TestSuite' == testDataEntityType) %} + service_description={{ gearman.nagiosServiceDescription }}$newline$ + {% elseif ('TestCase' == testDataEntityType) %} + service_description={{ testSuiteId }}_{{ testDataEntity.id }}$newline$ + {% elseif ('TestCaseStep' == testDataEntityType) %} + service_description={{ testSuiteId }}_{{ testCaseId }}_{{ testDataEntity.id }}$newline$ + {% endif %} + output= + {% include 'short_summary.twig' %}\n {% include 'detailed_summary.twig' %} {% include 'performance_data.twig' %} -$whitespace$[{{ gearman.nagiosCheckCommand }}] + $whitespace$[{{ gearman.nagiosCheckCommand }}] {% endspaceless %} diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data.twig index a3ff1838..9153e092 100644 --- a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data.twig +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data.twig @@ -1,19 +1,16 @@ -{# This template renders the performance data for the current test suite. #} -|suite__state={{getOutputState(testsuite.state).errorCode}};;;; -$whitespace$suite__warning={{ testsuite.warningTime }}s;;;;$whitespace$suite__critical={{ testsuite.criticalTime }}s;;;;$whitespace$suite_{{ testsuite.id }}={{getOutputDuration(testsuite)}};{{ testsuite.warningTime }};{{ testsuite.criticalTime }};; -{% for testCaseId, testCase in testsuite.testCasesAsSortedSet %} - {% set testCaseIndex = format("%03d", loop.index) %} - $whitespace$c_{{ testCaseIndex }}__state={{getOutputState(testCase.state).errorCode}};;;; - $whitespace$c_{{ testCaseIndex }}__warning={{ testCase.warningTime }}s;;;; - $whitespace$c_{{ testCaseIndex }}__critical={{ testCase.criticalTime }}s;;;; - $whitespace$c_{{ testCaseIndex }}_{{ testCase.id }}={{getOutputDuration(testCase)}};{{ testCase.warningTime }};{{ testCase.criticalTime }};; +{% set testDataEntityType = getTestDataEntityType(testDataEntity) %} +|{% if ('TestSuite' == testDataEntityType) %} + {% include 'performance_data_suite.twig' with { testsuite: testDataEntity} %} +{% elseif ('TestCase' == testDataEntityType) %} + {% for testCaseId, testCase in testsuite.testCasesAsSortedSet %} + {% set testCaseIndex = format("%03d", loop.index) %} + $whitespace$ + {% include 'performance_data_case.twig' with { testCaseIndex: testCaseIndex, testCase: testDataEntity } %} + {% endfor %} +{% elseif ('TestCaseStep' == testDataEntityType) %} {% for testStep in testCase.steps %} {% set testStepIndex = format("%03d", loop.index) %} - $whitespace$s_{{ testCaseIndex }}_{{ testStepIndex }}_{{testStep.name}}={{getOutputDuration(testStep)}}; - {% set testStepWarningTime = testStep.warningTime %} - {% if (testStepWarningTime > 0) %} - {{testStepWarningTime}} - {% else %} - {% endif %};;; + $whitespace$ + {% include 'performance_data_step.twig' with { testCaseIndex: testCaseIndex, testStepIndex: testStepIndex, testStep: testDataEntity } %} {% endfor %} -{% endfor %} +{% endif %} diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data_case.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data_case.twig new file mode 100644 index 00000000..e2684495 --- /dev/null +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data_case.twig @@ -0,0 +1,14 @@ +{# This template renders the performance data for a specific test case. #} +{# only include metadata (suite_id, graph_type) if this is not a suite result #} +{% if not testsuite %} + {% include 'performance_data_meta.twig' with { graph_type: "case" } %}$whitespace$ +{% endif %} +c_{{ testCaseIndex }}__state={{getOutputState(testCase.state).errorCode}};;;; +$whitespace$c_{{ testCaseIndex }}__warning={{ testCase.warningTime }}s;;;; +$whitespace$c_{{ testCaseIndex }}__critical={{ testCase.criticalTime }}s;;;; +$whitespace$c_{{ testCaseIndex }}_{{ testCase.id }}={{getOutputDuration(testCase)}};{{ testCase.warningTime }};{{ testCase.criticalTime }};; +{% for testStep in testCase.steps %} + {% set testStepIndex = format("%03d", loop.index) %} + $whitespace$ + {% include 'performance_data_step.twig' with { testCaseIndex: testCaseIndex, testStepIndex: testStepIndex, testStep: testStep } %} +{% endfor %} diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data_meta.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data_meta.twig new file mode 100644 index 00000000..8c670a52 --- /dev/null +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data_meta.twig @@ -0,0 +1,4 @@ +{# This template prints the suiteID label for step/case results #} +{# and the graph type #} +suite_{{testSuiteId}}=0;;;; +$whitespace$graph_type_{{graph_type}}=0;;;; diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data_step.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data_step.twig new file mode 100644 index 00000000..58cdda31 --- /dev/null +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data_step.twig @@ -0,0 +1,15 @@ +{# This template renders the performance data for a specific test step. #} +{# only include metadata (suite_id, graph_type) if this is not a suite result #} +{% if not testCase %} + {% include 'performance_data_meta.twig' with { graph_type: "step" } %}$whitespace$ +{% endif %} +{% if (not empty(testCaseIndex)) %} + s_{{ testCaseIndex }}_{{ testStepIndex }}_{{testStep.id}}={{getOutputDuration(testStep)}}; +{% else %} + s_{{ testStepIndex }}_{{testStep.id}}={{getOutputDuration(testStep)}}; +{% endif %} +{% set testStepWarningTime = testStep.warningTime %} +{% if (testStepWarningTime > 0) %} + {{testStepWarningTime}} +{% else %} +{% endif %};;; diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data_suite.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data_suite.twig new file mode 100644 index 00000000..a55dfd11 --- /dev/null +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/performance_data_suite.twig @@ -0,0 +1,11 @@ +{# This template renders the performance data for the current test suite. #} +suite_{{ testsuite.id }}={{getOutputDuration(testsuite)}};{{ testsuite.warningTime }};{{ testsuite.criticalTime }};; +$whitespace$graph_type_suite=0;;;; +$whitespace$suite__state={{getOutputState(testsuite.state).errorCode}};;;; +$whitespace$suite__warning={{ testsuite.warningTime }}s;;;; +$whitespace$suite__critical={{ testsuite.criticalTime }}s;;;; +{% for testCaseId, testCase in testsuite.testCasesAsSortedSet %} + {% set testCaseIndex = format("%03d", loop.index) %} + $whitespace$ + {% include 'performance_data_case.twig' with { testCaseIndex: testCaseIndex, testCase: testCase } %} +{% endfor %} diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/short_summary.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/short_summary.twig index e14cc003..ffec86f7 100644 --- a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/short_summary.twig +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/short_summary.twig @@ -1,38 +1,8 @@ -{# This template renders the short summary for the current test suite. #} -{{ getOutputState(testsuite.state).shortState }} Sakuli suite "{{testsuite.id}}"$whitespace$ -{% if (testsuite.state.error) %} - {% set exceptionMessages = errorMessageCreator.exceptionMessageTestSuite(testsuite) %} - ({{ testsuite.duration | number_format(2, '.') }}s) {{ testsuite.state.nagiosStateDescription }}: {{ abbreviate(exceptionMessages, 200, true) }} -{% else %} - {{testsuite.state.nagiosStateDescription}} - {% if (testsuite.state.ok) %} - $whitespace$({{ testsuite.duration | number_format(2, '.') }}s) - {% elseif (testsuite.state.name == 'WARNING_IN_SUITE') %} - $whitespace$({{ testsuite.duration | number_format(2, '.') }}s/warn at {{ testsuite.warningTime }}s) - {% elseif (testsuite.state.name == 'CRITICAL_IN_SUITE') %} - $whitespace$({{ testsuite.duration | number_format(2, '.') }}s/crit at {{ testsuite.criticalTime }}s) - {% elseif (testsuite.state.name == 'WARNING_IN_STEP') %} - {% for testCaseId, testCase in testsuite.testCasesAsSortedSet %} - {% if (testCase.state.warningInStep) %} - {% for testStep in testCase.steps %} - {% if (testStep.state.warning) %} - , step "{{ testStep.id }}" over runtime ({{ testStep.duration | number_format(2, '.') }}s/warn at {{ testStep.warningTime }}s) - {% endif %} - {% endfor %} - {% endif %} - {% endfor %} - {% elseif (testsuite.state.name == 'WARNING_IN_CASE') %} - {% for testCaseId, testCase in testsuite.testCasesAsSortedSet %} - {% if (testCase.state.warning) %} - , case "{{ testCase.id }}" over runtime ({{ testCase.duration | number_format(2, '.') }}s/warn at {{ testCase.warningTime }}s) - {% endif %} - {% endfor %} - {% elseif (testsuite.state.name == 'CRITICAL_IN_CASE') %} - {% for testCaseId, testCase in testsuite.testCasesAsSortedSet %} - {% if (testCase.state.critical) %} - , case "{{ testCase.id }}" over runtime ({{ testCase.duration | number_format(2, '.') }}s/crit at {{ testCase.criticalTime }}s) - {% endif %} - {% endfor %} - {% endif %} - .$whitespace$(Last suite run: {% if (empty(testsuite.stopDate)) %}xx{% else %}{{ testsuite.stopDate|date("dd.MM.YY HH:mm:ss") }}{% endif %}) +{% set testDataEntityType = getTestDataEntityType(testDataEntity) %} +{% if ('TestSuite' == testDataEntityType) %} + {% include 'short_summary_suite.twig' with { testsuite: testDataEntity} %} +{% elseif ('TestCase' == testDataEntityType) %} + {% include 'short_summary_case.twig' with { testCase: testDataEntity } %} +{% elseif ('TestCaseStep' == testDataEntityType) %} + {% include 'short_summary_step.twig' with { testStep: testDataEntity } %} {% endif %} diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/short_summary_case.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/short_summary_case.twig new file mode 100644 index 00000000..d8e1e752 --- /dev/null +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/short_summary_case.twig @@ -0,0 +1,22 @@ +{# This template renders the short summary for the specified test case. #} +{{ getOutputState(testCase.state).shortState }} Case "{{testCase.id}}" of Sakuli suite "{{testSuiteId}}"$whitespace$ +{% if (testCase.state.error) %} + {% set exceptionMessages = errorMessageCreator.exceptionMessageTestCase(testCase) %} + ({{ testCase.duration | number_format(2, '.') }}s) {{testCase.state.nagiosStateDescription}}: {{abbreviate(exceptionMessages, 200, true)}} +{% else %} + {{testCase.state.nagiosStateDescription}} + {% if (testCase.state.ok) %} + $whitespace$({{ testCase.duration | number_format(2, '.') }}s) + {% elseif (testCase.state.name == 'WARNING') %} + $whitespace$ case "{{testCase.id}}" over runtime ({{ testCase.duration | number_format(2, '.') }}s/warn at {{ testCase.warningTime }}s) + {% elseif (testCase.state.name == 'CRITICAL') %} + $whitespace$ case "{{testCase.id}}" over runtime ({{ testCase.duration | number_format(2, '.') }}s/crit at {{ testCase.criticalTime }}s) + {% elseif (testCase.state.name == 'WARNING_IN_STEP') %} + {% for testStep in testCase.steps %} + {% if (testStep.state.warning) %} + , step "{{testStep.id}}" over runtime ({{ testStep.duration | number_format(2, '.') }}s/warn at {{ testStep.warningTime }}s) + {% endif %} + {% endfor %} + {% endif %} + .$whitespace$(Last suite run: {% if (empty(testCase.stopDate)) %}xx{% else %}{{ testCase.stopDate|date("dd.MM.YY HH:mm:ss") }}{% endif %}) +{% endif %} diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/short_summary_step.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/short_summary_step.twig new file mode 100644 index 00000000..8313a063 --- /dev/null +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/short_summary_step.twig @@ -0,0 +1,16 @@ +{# This template renders the short summary for the specified test step. #} +{{ getOutputState(testStep.state).shortState }} Step "{{testStep.id}}" in case "{{testCaseId}}" of Sakuli suite "{{testSuiteId}}"$whitespace$ +{% if (testStep.state.error) %} + {% set exceptionMessages = errorMessageCreator.exceptionMessage(testStep) %} + ({{ testStep.duration | number_format(2, '.') }}s) {{testStep.state.nagiosStateDescription}}: {{abbreviate(exceptionMessages, 200, true)}} +{% else %} + {{testStep.state.nagiosStateDescription}} + {% if (testStep.state.ok) %} + $whitespace$({{ testStep.duration | number_format(2, '.') }}s) + {% elseif (testStep.state.name == 'WARNING') %} + $whitespace$ step "{{testStep.id}}" over runtime ({{ testStep.duration | number_format(2, '.') }}s/warn at {{ testStep.warningTime }}s) + {% elseif (testStep.state.name == 'CRITICAL') %} + $whitespace$ step "{{testStep.id}}" over runtime ({{ testStep.duration | number_format(2, '.') }}s/crit at {{ testStep.criticalTime }}s) + {% endif %} + .$whitespace$(Last test step run: {% if (empty(testStep.stopDate)) %}xx{% else %}{{ testStep.stopDate|date("dd.MM.YY HH:mm:ss") }}{% endif %}) +{% endif %} diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/short_summary_suite.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/short_summary_suite.twig new file mode 100644 index 00000000..d53eac24 --- /dev/null +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/short_summary_suite.twig @@ -0,0 +1,34 @@ +{# This template renders the short summary for the current test suite. #} +{{ getOutputState(testsuite.state).shortState }} Sakuli suite "{{testsuite.id}}"$whitespace$ +{% if (testsuite.state.error) %} + {% set exceptionMessages = errorMessageCreator.exceptionMessageTestSuite(testsuite) %} + ({{ testsuite.duration | number_format(2, '.') }}s) {{testsuite.state.nagiosStateDescription}}: {{abbreviate(exceptionMessages, 200, true)}} +{% else %} + {{testsuite.state.nagiosStateDescription}} + {% if (testsuite.state.ok) %} + $whitespace$({{ testsuite.duration | number_format(2, '.') }}s) + {% elseif (testsuite.state.name == 'WARNING_IN_SUITE') %} + $whitespace$({{ testsuite.duration | number_format(2, '.') }}s/warn at {{ testsuite.warningTime }}s) + {% elseif (testsuite.state.name == 'CRITICAL_IN_SUITE') %} + $whitespace$({{ testsuite.duration | number_format(2, '.') }}s/crit at {{ testsuite.criticalTime }}s) + {% elseif (testsuite.state.name == 'WARNING_IN_STEP') %} + {% for testCaseId, testCase in testsuite.testCasesAsSortedSet %} + {% if (testCase.state.warningInStep) %} + {% for testStep in testCase.steps %} + {% if (testStep.state.warning) %} + , step "{{testStep.id}}" over runtime ({{ testStep.duration | number_format(2, '.') }}s/warn at {{ testStep.warningTime }}s) + {% endif %} + {% endfor %} + {% endif %} + {% endfor %} + {% elseif (testsuite.state.name == 'WARNING_IN_CASE' or testsuite.state.name == 'CRITICAL_IN_CASE') %} + {% for testCase in testsuite.testCasesAsSortedSet %} + {% if (testCase.state.warning) %} + , case "{{testCase.id}}" over runtime ({{ testCase.duration | number_format(2, '.') }}s/warn at {{ testCase.warningTime }}s) + {% elseif (testCase.state.critical) %} + , case "{{testCase.id}}" over runtime ({{ testCase.duration | number_format(2, '.') }}s/crit at {{ testCase.criticalTime }}s) + {% endif %} + {% endfor %} + {% endif %} + .$whitespace$(Last suite run: {% if (empty(testsuite.stopDate)) %}xx{% else %}{{ testsuite.stopDate|date("dd.MM.YY HH:mm:ss") }}{% endif %}) +{% endif %} diff --git a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/step_information.twig b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/step_information.twig index 864a298c..781d1a65 100644 --- a/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/step_information.twig +++ b/src/common/src/main/resources/org/sakuli/common/config/templates/gearman/step_information.twig @@ -1,9 +1,7 @@ -{% for testStep in testCase.stepsAsSortedSet %} - {% if (testStep.state.error) %} - , step "{{ testStep.name }}"$whitespace$ - EXCEPTION: {{ errorMessageCreator.exceptionMessage(testStep) }} - {% elseif (testStep.state.warning) %} - , step "{{ testStep.name }}"$whitespace$ - over runtime ({{ testStep.duration | number_format(2, '.') }}s/warn at {{ testStep.warningTime }}s) - {% endif %} -{% endfor %} +{% if (testStep.state.error) %} + , step "{{ testStep.id }}"$whitespace$ + EXCEPTION: {{ errorMessageCreator.exceptionMessage(testStep) }} +{% elseif (testStep.state.warning) %} + , step "{{ testStep.id }}"$whitespace$ + over runtime ({{ testStep.duration | number_format(2, '.') }}s/warn at {{ testStep.warningTime }}s) +{% endif %} diff --git a/src/common/src/main/resources/org/sakuli/common/libs/js/sakuli.js b/src/common/src/main/resources/org/sakuli/common/libs/js/sakuli.js index 3d44f572..b18adedf 100644 --- a/src/common/src/main/resources/org/sakuli/common/libs/js/sakuli.js +++ b/src/common/src/main/resources/org/sakuli/common/libs/js/sakuli.js @@ -44,12 +44,17 @@ _include("sakuli_Logger.js"); * TestCase - initializes the Sakuli object and sets the warning and critical time for this test case. * @example * ``` - * var testCase = new TestCase(20,30, "path-to/image-folder-name"); + * //new syntex since v1.2.0 + * var testCase = new TestCase(["own-case-id", 20, 30, "path-to/image-folder-name"]); + * + * //old syntax < v1.2.0 + * var testCase = new TestCase(20, 30, ["path-to/image-folder-name"]); * ``` + * @param {String} optCaseId optional ID to set the testcase ID to some specifc value. Default is the folder name. * - * @param {number} warningTime threshold in seconds. If the threshold is set to 0, + * @param {number} optWarningTime threshold in seconds. If the threshold is not set or is set to 0, * the execution time will never exceed, so the state will be always OK! - * @param {number} criticalTime threshold in seconds. If the threshold is set to 0, + * @param {number} optCriticalTime threshold in seconds. If the threshold is not set or is set to 0, * the execution time will never exceed, so the state will be always OK! * @param {String[]} optImagePathArray (optional) Path or Array of Paths to the folder containing the image patterns * for these test cases. @@ -57,14 +62,29 @@ _include("sakuli_Logger.js"); * @returns an initialized Sakuli object. * @namespace TestCase */ -function TestCase(warningTime, criticalTime, optImagePathArray) { +function TestCase(optCaseId, optWarningTime, optCriticalTime, optImagePathArray) { var that = {}; - var env; - that.javaObject = null; - that.tcID = null; - env = null; + var env = null; + var tcID = null; + //for legacy code check if first paramet is paramter + if (parseInt(optCaseId) > 0) { + optImagePathArray = optCriticalTime; + optCriticalTime = optWarningTime; + optWarningTime = optCaseId; + optCaseId = null; + } + if (optCaseId.length > 0) { + tcID = optCaseId; + } + + if (undefined == optWarningTime) { + optWarningTime = 0; + } + if (undefined == optCriticalTime) { + optCriticalTime = 0; + } if (undefined == optImagePathArray) { optImagePathArray = new Array(resolveDefaultImagePath()); } @@ -84,7 +104,7 @@ function TestCase(warningTime, criticalTime, optImagePathArray) { * @method addImagePaths */ that.addImagePaths = function (imagePaths) { - if (imagePaths instanceof String) { + if (imagePaths instanceof String) { that.javaObject.addImagePathsAsString(new Array(imagePaths)); } else { that.javaObject.addImagePathsAsString(imagePaths); @@ -95,24 +115,32 @@ function TestCase(warningTime, criticalTime, optImagePathArray) { * A step allows to sub-divide a case to measure logical units, such as "login", "load report" etc. in its * particular runtime. When a case starts, Sakuli starts a "step" timer. It gets read out, stored with the * step name, and the timer will set to `0` each time endOfStep() is called. - * If the step runtime exceeds the step threshold (second parameter, optional), the step is saved with state + * If the step runtime exceeds the step threshold (second parameter, optional), the step is saved with state * "WARNING" (there is no CRITICAL state). * @param {String} stepName * @param {number} optWarningTime (optional) threshold in seconds, default = 0. If the threshold is set to 0, * the execution time will never exceed, so the state will be always OK! + * @param {number} optCriticalTime (optional) threshold in seconds, default = 0. If the threshold is set to 0, + * the execution time will never exceed, so the state will be always OK! + * @param {boolean} optForward (optional) indicate whether the result of the test step shall be immediately + * processed by the enabled forwarders. This means before the test suite has been executed to + * the end. If not specified in another way, this option is disabled! Default: `false` * @memberOf TestCase * @method endOfStep */ - that.endOfStep = function (stepName, optWarningTime) { + that.endOfStep = function (stepName, optWarningTime, optCriticalTime, optForward) { if (undefined == optWarningTime) { optWarningTime = 0; } - if (undefined == warningTime) { - warningTime = 0; + if (undefined == optCriticalTime) { + optCriticalTime = 0; + } + if (undefined == optForward) { + optForward = false; } var currentTime = (new Date()).getTime(); //call the backend - that.javaObject.addTestCaseStep(stepName, that.stepStartTime, currentTime, optWarningTime); + that.javaObject.addTestCaseStep(stepName, that.stepStartTime, currentTime, optWarningTime, optCriticalTime, optForward); //set stepstart for the next step that.stepStartTime = currentTime; }; @@ -167,12 +195,16 @@ function TestCase(warningTime, criticalTime, optImagePathArray) { * sakuli.saveResult(); * } * ``` - * + * @param {boolean} optForward indicate whether the result of the test case shall be immediately processed by the enabled forwarders. This means before the test suite has been executed to the end. If not specified in another way, this option is disabled! Default: `false` * @memberOf TestCase * @method saveResult */ - that.saveResult = function () { - Logger.logInfo("=========== SAVE Test Case '" + that.tcID + "' =================="); + that.saveResult = function (optForward) { + if (undefined == optForward) { + optForward = false; + } + Logger.logInfo("=========== SAVE Test Case '" + tcID + "' =================="); + Logger.logInfo("Forward of test case results enabled: '" + optForward + "'"); //create the values var stopTime, lastURL = "", browser = ""; stopTime = (new Date()).getTime(); @@ -181,7 +213,7 @@ function TestCase(warningTime, criticalTime, optImagePathArray) { // Agent description can contain semicolon, replace globally browser = browser.replace(/;/g, ','); //call the backend - that.javaObject.saveResult(that.tcID, that.startTime, stopTime, lastURL, browser); + that.javaObject.saveResult(tcID, that.startTime, stopTime, lastURL, browser, optForward); }; /** @@ -191,7 +223,7 @@ function TestCase(warningTime, criticalTime, optImagePathArray) { * @method getID */ that.getID = function () { - return that.tcID; + return tcID; }; /** @@ -274,7 +306,6 @@ function TestCase(warningTime, criticalTime, optImagePathArray) { Logger.logDebug("get Backend - step 1 (load backend)"); that.javaObject = Packages.org.sakuli.loader.BeanLoader.loadTestCaseAction(); Logger.logDebug("get Backend - step 2 (get the test case id)"); - that.tcID = that.javaObject.getIdFromPath(_resolvePath()); /** * - set the start time for this cass and for the first step @@ -282,8 +313,16 @@ function TestCase(warningTime, criticalTime, optImagePathArray) { */ that.startTime = (new Date()).getTime(); that.stepStartTime = (new Date()).getTime(); - that.javaObject.init(that.tcID, warningTime, criticalTime, optImagePathArray); - Logger.logInfo("Now start to execute the test case '" + that.tcID + "' ! \n ....\n ...\n...."); + + //determine correct ID for testcase + var idFromPath = that.javaObject.getIdFromPath(_resolvePath()); + if (undefined == tcID) { + tcID = idFromPath; + that.javaObject.init(tcID, optWarningTime, optCriticalTime, optImagePathArray); + } else { + that.javaObject.initWithCaseID(idFromPath, tcID, optWarningTime, optCriticalTime, optImagePathArray); + } + Logger.logInfo("Now start to execute the test case '" + tcID + "' ! \n ....\n ...\n...."); } init(); diff --git a/src/core/src/main/java/org/sakuli/actions/TestCaseAction.java b/src/core/src/main/java/org/sakuli/actions/TestCaseAction.java index 58000de0..ac84cc40 100644 --- a/src/core/src/main/java/org/sakuli/actions/TestCaseAction.java +++ b/src/core/src/main/java/org/sakuli/actions/TestCaseAction.java @@ -20,20 +20,21 @@ import org.apache.commons.lang.StringUtils; import org.sakuli.actions.logging.LogToResult; +import org.sakuli.datamodel.AbstractTestDataEntity; import org.sakuli.datamodel.TestCase; import org.sakuli.datamodel.TestCaseStep; import org.sakuli.datamodel.TestSuite; import org.sakuli.datamodel.actions.ImageLib; import org.sakuli.datamodel.actions.LogLevel; import org.sakuli.datamodel.helper.TestCaseHelper; -import org.sakuli.datamodel.helper.TestCaseStepHelper; import org.sakuli.datamodel.helper.TestDataEntityHelper; import org.sakuli.exceptions.SakuliActionException; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.exceptions.SakuliExceptionHandler; import org.sakuli.exceptions.SakuliValidationException; import org.sakuli.loader.BaseActionLoader; import org.sakuli.loader.BaseActionLoaderImpl; +import org.sakuli.services.TeardownServiceHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -43,6 +44,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Date; +import java.util.concurrent.ExecutorService; /** * @author tschneck Date: 19.06.13 @@ -51,10 +53,12 @@ public class TestCaseAction { private final Logger logger = LoggerFactory.getLogger(this.getClass()); + @Autowired + private ExecutorService executorService; /** * Represents the current running TestCase. The object will be set at {@link #init(String, int, int, String...)} and - * releases at {@link #saveResult(String, String, String, String, String)} + * releases at {@link #saveResult(String, String, String, String, String, boolean)} */ @Autowired @Qualifier(BaseActionLoaderImpl.QUALIFIER) @@ -74,12 +78,19 @@ public class TestCaseAction { * the execution time will never exceed, so the state will be always OK! * @param imagePaths multiple paths to images */ - @LogToResult(message = "init a new test case") + @LogToResult(message = "init a new test case", logClassInstance = false) public void init(String testCaseID, int warningTime, int criticalTime, String... imagePaths) { loader.init(testCaseID, imagePaths); initWarningAndCritical(warningTime, criticalTime); } + @LogToResult(message = "init a new test case with caseID", logClassInstance = false) + public void initWithCaseID(String testCaseID, String newTestCaseID, int warningTime, int criticalTime, String... imagePaths) { + loader.init(testCaseID, imagePaths); + loader.getCurrentTestCase().setId(newTestCaseID); + initWarningAndCritical(warningTime, criticalTime); + } + /** * Set the warning and critical Time to the specific test case. * @@ -90,7 +101,7 @@ public void init(String testCaseID, int warningTime, int criticalTime, String... * the execution time will never exceed, so the state will be always OK! * @param imagePaths multiple paths to images */ - @LogToResult(message = "init a new test case") + @LogToResult(message = "init a new test case", logClassInstance = false) public void initWithPaths(String testCaseID, int warningTime, int criticalTime, Path... imagePaths) { loader.init(testCaseID, imagePaths); initWarningAndCritical(warningTime, criticalTime); @@ -101,9 +112,9 @@ public void initWithPaths(String testCaseID, int warningTime, int criticalTime, * If a relative path is assigned, the current testcase folder will be used as current directory. * * @param imagePaths one or more paths as {@link String} elements - * @throws SakuliException if an IO error occurs + * @throws SakuliCheckedException if an IO error occurs */ - public void addImagePathsAsString(String... imagePaths) throws SakuliException { + public void addImagePathsAsString(String... imagePaths) throws SakuliCheckedException { for (String path : imagePaths) { //check if absolute path if (!path.matches("(\\/\\S*|\\w:\\\\\\S*)")) { @@ -118,10 +129,10 @@ public void addImagePathsAsString(String... imagePaths) throws SakuliException { * Adds the additional paths to the current {@link ImageLib} object. * * @param imagePaths one or more {@link Path} elements - * @throws SakuliException if an IO error occurs + * @throws SakuliCheckedException if an IO error occurs */ @LogToResult - public void addImagePaths(Path... imagePaths) throws SakuliException { + public void addImagePaths(Path... imagePaths) throws SakuliCheckedException { loader.addImagePaths(imagePaths); } @@ -150,10 +161,10 @@ private void initWarningAndCritical(int warningTime, int criticalTime) { * @param stopTime end time in milliseconds * @param lastURL URL to the last visited page during this test case * @param browserInfo detail information about the used browser - * @throws SakuliException + * @throws SakuliCheckedException */ - @LogToResult(message = "save the result of the current test case") - public void saveResult(String testCaseId, String startTime, String stopTime, String lastURL, String browserInfo) throws SakuliException { + @LogToResult + public void saveResult(String testCaseId, String startTime, String stopTime, String lastURL, String browserInfo, boolean forward) throws SakuliCheckedException { if (!loader.getCurrentTestCase().getId().equals(testCaseId)) { handleException("testcaseID '" + testCaseId + "' to save the test case Result ist is not valid!"); } @@ -179,31 +190,35 @@ public void saveResult(String testCaseId, String startTime, String stopTime, Str //release current test case -> indicates that this case is finished loader.setCurrentTestCase(null); + if (forward) { + forwardTestDataEntity(tc); + } } /** - * Wrapper for {@link #addTestCaseStep(String, String, String, int)} with warningTime '0'. + * Wrapper for {@link #addTestCaseStep(String, String, String, int, int, boolean)} with warningTime '0'. */ - public void addTestCaseStep(String stepName, String startTime, String stopTime) throws SakuliException { - addTestCaseStep(stepName, startTime, stopTime, 0); + public void addTestCaseStep(String stepName, String startTime, String stopTime) throws SakuliCheckedException { + addTestCaseStep(stepName, startTime, stopTime, 0, 0, false); } /** - * Save a new step to a existing test case. Must be called before {@link #saveResult(String, String, String, String, - * String)} + * Save a new step to a existing test case. Must be called before {@link #saveResult(String, String, String, String, String, boolean)} * - * @param stepName name of this step - * @param startTime start time in milliseconds - * @param stopTime end time in milliseconds - * @param warningTime warning threshold in seconds. If the threshold is set to 0, the execution time will never exceed, so the state will be always OK! - * @throws SakuliException + * @param stepName name of this step + * @param startTime start time in milliseconds + * @param stopTime end time in milliseconds + * @param warningTime warning threshold in seconds. If the threshold is set to 0, the execution time will never exceed, so the state will be always OK! + * @param criticalTime critical threshold in seconds. If the threshold is set to 0, the execution time will never exceed, so the state will be always OK! + * @param forward boolean flag indicating whether the result of the test case shall be immediately processed by the enabled forwarders. This means before the test suite has been executed to the end. If not specified in another way, this option is disabled! + * @throws SakuliCheckedException */ - @LogToResult(message = "add a step to the current test case") - public void addTestCaseStep(String stepName, String startTime, String stopTime, int warningTime) throws SakuliException { + @LogToResult(message = "TestCase.endOfStep()", logClassInstance = false) + public void addTestCaseStep(String stepName, String startTime, String stopTime, int warningTime, int criticalTime, boolean forward) throws SakuliCheckedException { if (stepName == null || stepName.isEmpty() || stepName.equals("undefined")) { handleException("Please set a Name - all values of the test case step need to be set!"); } - String errormsg = TestCaseStepHelper.checkWarningTime(warningTime, stepName); + String errormsg = TestDataEntityHelper.checkWarningAndCriticalTime(warningTime, criticalTime, String.format("TestCaseStep [name = %s]", stepName)); if (errormsg != null) { handleException(errormsg); } @@ -213,6 +228,7 @@ public void addTestCaseStep(String stepName, String startTime, String stopTime, step.setStartDate(new Date(Long.parseLong(startTime))); step.setStopDate(new Date(Long.parseLong(stopTime))); step.setWarningTime(warningTime); + step.setCriticalTime(criticalTime); step.addActions(loader.getCurrentTestCase().getAndResetTestActions()); } catch (NullPointerException | NumberFormatException e) { @@ -227,6 +243,16 @@ public void addTestCaseStep(String stepName, String startTime, String stopTime, + "\" saved to test case \"" + loader.getCurrentTestCase().getId() + "\""); + if (forward) { + forwardTestDataEntity(step); + } + } + + private void forwardTestDataEntity(AbstractTestDataEntity abstractTestDataEntity) { + executorService.submit(() -> { + logger.info("======= TRIGGER ASYNC teardown of: {} =======", abstractTestDataEntity.toStringShort()); + TeardownServiceHelper.invokeTeardownServices(abstractTestDataEntity, true); + }); } protected TestCaseStep findStep(String stepName) { @@ -253,11 +279,11 @@ public void throwException(String message, boolean screenshot) { } /** - * calls the method {@link SakuliExceptionHandler#handleException(Throwable)} + * calls the method {@link SakuliExceptionHandler#handleException(Exception)} * * @param e the original exception */ - public void handleException(Throwable e) { + public void handleException(Exception e) { loader.getExceptionHandler().handleException(e, false); } @@ -283,16 +309,16 @@ public String toString() { * @param pathToTestCaseFile path to the test case file "_tc.js" * @return returns test the currentTestCase Name */ - @LogToResult(message = "convert the path of the test case file to a valid test case ID") + @LogToResult(message = "determine a valid test case ID from the test case file path", logClassInstance = false) public String getIdFromPath(String pathToTestCaseFile) { - logger.info("Return a test-case-id for \"" + pathToTestCaseFile + "\""); + logger.debug("determine a ID for testcase for file path '{}'", pathToTestCaseFile); String id = TestCaseHelper.convertTestCaseFileToID(pathToTestCaseFile); //check id if (loader.getTestSuite().checkTestCaseID(id)) { - logger.info("test-case-id = " + id); + logger.debug("determined testcase id: {}", id); return id; } else { - handleException("cannot identify testcase for pathToTestCaseFile=" + pathToTestCaseFile); + handleException("cannot determine testcase id for path of testcase file '" + pathToTestCaseFile + "'"); return null; } } @@ -300,7 +326,7 @@ public String getIdFromPath(String pathToTestCaseFile) { /** * @return String value of the last URL */ - @LogToResult(message = "return 'lastURL'") + @LogToResult public String getLastURL() { return loader.getCurrentTestCase().getLastURL(); } @@ -310,7 +336,7 @@ public String getLastURL() { * * @param lastURL String value of the last URL */ - @LogToResult(message = "set 'lastURL' to new value") + @LogToResult public void setLastURL(String lastURL) { loader.getCurrentTestCase().setLastURL(lastURL); } @@ -323,7 +349,7 @@ public String getTestCaseFolderPath() { try { return loader.getCurrentTestCase().getTcFile().getParent().toAbsolutePath().toString(); } catch (Exception e) { - handleException(new SakuliException(e, + handleException(new SakuliCheckedException(e, String.format("cannot resolve the folder path of the current testcase '%s'", loader.getCurrentTestCase()))); return null; @@ -338,7 +364,7 @@ public String getTestSuiteFolderPath() { try { return loader.getTestSuite().getTestSuiteFolder().toAbsolutePath().toString(); } catch (Exception e) { - handleException(new SakuliException(e, + handleException(new SakuliCheckedException(e, String.format("cannot resolve the folder path of the current testsuite '%s'", loader.getTestSuite()))); return null; diff --git a/src/core/src/main/java/org/sakuli/actions/environment/Application.java b/src/core/src/main/java/org/sakuli/actions/environment/Application.java index a92ea11c..ff495b0b 100644 --- a/src/core/src/main/java/org/sakuli/actions/environment/Application.java +++ b/src/core/src/main/java/org/sakuli/actions/environment/Application.java @@ -20,7 +20,7 @@ import org.sakuli.actions.logging.LogToResult; import org.sakuli.actions.screenbased.Region; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.loader.BeanLoader; import org.sakuli.loader.ScreenActionLoader; import org.sikuli.basics.Settings; @@ -147,7 +147,7 @@ public Application closeApp(boolean silent) { int retValue = -1; try { retValue = super.close(); - } catch (Throwable e) { + } catch (Exception e) { LOGGER.error("ERROR in closing Application", e); } if (!silent && retValue != 0) { @@ -179,7 +179,7 @@ public Application kill(boolean silent) { return killAppName(getName()); } return killAppPID(getPID()); - } catch (SakuliException e) { + } catch (SakuliCheckedException e) { if (!silent) { loader.getExceptionHandler().handleException(e); return null; @@ -189,26 +189,26 @@ public Application kill(boolean silent) { return this; } - private Application killAppName(String name) throws SakuliException { + private Application killAppName(String name) throws SakuliCheckedException { try { String cmd = String.format( Settings.isWindows() ? "Taskkill /IM \"%s\" /F" : "pkill \"%s\"" , name); CommandExecutorHelper.execute(cmd, 0); } catch (Exception e) { - throw new SakuliException(e, String.format("could not kill application with name '%s'.", name)); + throw new SakuliCheckedException(e, String.format("could not kill application with name '%s'.", name)); } return this; } - private Application killAppPID(Integer pid) throws SakuliException { + private Application killAppPID(Integer pid) throws SakuliCheckedException { try { String cmd = String.format( Settings.isWindows() ? "Taskkill /PID %d /F" : "kill -9 %d" , pid); CommandExecutorHelper.execute(cmd, 0); } catch (Exception e) { - throw new SakuliException(e, String.format("could not kill application with PID '%d'.", pid)); + throw new SakuliCheckedException(e, String.format("could not kill application with PID '%d'.", pid)); } return this; } diff --git a/src/core/src/main/java/org/sakuli/actions/environment/Environment.java b/src/core/src/main/java/org/sakuli/actions/environment/Environment.java index e02dcfd6..ebad6a78 100644 --- a/src/core/src/main/java/org/sakuli/actions/environment/Environment.java +++ b/src/core/src/main/java/org/sakuli/actions/environment/Environment.java @@ -26,7 +26,7 @@ import org.sakuli.actions.screenbased.RegionImpl; import org.sakuli.actions.screenbased.TypingUtil; import org.sakuli.datamodel.properties.ActionProperties; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.loader.BeanLoader; import org.sakuli.loader.ScreenActionLoader; import org.sakuli.utils.CommandLineUtil; @@ -80,16 +80,16 @@ public Environment(boolean resumeOnException) { * * @param command OS depended command as {@link CommandLineResult} * @return the result as {@link String} - * @throws SakuliException if the command won't exit with value 0 + * @throws SakuliCheckedException if the command won't exit with value 0 */ - public static CommandLineResult runCommand(String command) throws SakuliException { + public static CommandLineResult runCommand(String command) throws SakuliCheckedException { return runCommand(command, true); } /** * Equal to {@link #runCommand(String)}, but with option to avoid throwing an exception if the exit code != 0 */ - public static CommandLineResult runCommand(String command, boolean throwException) throws SakuliException { + public static CommandLineResult runCommand(String command, boolean throwException) throws SakuliCheckedException { return CommandLineUtil.runCommand(command, throwException); } diff --git a/src/core/src/main/java/org/sakuli/actions/screenbased/RegionImpl.java b/src/core/src/main/java/org/sakuli/actions/screenbased/RegionImpl.java index 3e478805..0c3299df 100644 --- a/src/core/src/main/java/org/sakuli/actions/screenbased/RegionImpl.java +++ b/src/core/src/main/java/org/sakuli/actions/screenbased/RegionImpl.java @@ -20,7 +20,7 @@ import org.sakuli.actions.Action; import org.sakuli.datamodel.actions.ImageLibObject; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.loader.ScreenActionLoader; import org.sikuli.basics.Settings; import org.sikuli.script.FindFailed; @@ -210,7 +210,7 @@ public RegionImpl mouseMoveMe() { public RegionImpl mouseDown(MouseButton mouseButton) { try { this.mouseDown(mouseButton.getValue()); - } catch (Throwable e) { + } catch (Exception e) { loader.getExceptionHandler().handleException("Could execute mouseDown action for " + this, this, resumeOnException); } return this; @@ -222,7 +222,7 @@ public RegionImpl mouseDown(MouseButton mouseButton) { public RegionImpl mouseUp(MouseButton mouseButton) { try { this.mouseUp(mouseButton.getValue()); - } catch (Throwable e) { + } catch (Exception e) { loader.getExceptionHandler().handleException("Could execute mouseUp action for " + this, this, resumeOnException); } return this; @@ -322,7 +322,7 @@ public String extractText() { protected ImageLibObject loadImage(String imageName) { try { return loader.getImageLib().getImage(imageName); - } catch (SakuliException e) { + } catch (SakuliCheckedException e) { loader.getExceptionHandler().handleException(e, resumeOnException); } return null; diff --git a/src/core/src/main/java/org/sakuli/actions/screenbased/ScreenshotActions.java b/src/core/src/main/java/org/sakuli/actions/screenbased/ScreenshotActions.java index 95ea98c1..1d6ebac8 100644 --- a/src/core/src/main/java/org/sakuli/actions/screenbased/ScreenshotActions.java +++ b/src/core/src/main/java/org/sakuli/actions/screenbased/ScreenshotActions.java @@ -23,7 +23,7 @@ import org.sakuli.datamodel.TestSuite; import org.sakuli.datamodel.actions.Screen; import org.sakuli.datamodel.properties.ActionProperties; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.loader.BaseActionLoader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -173,7 +173,7 @@ public Path takeScreenshot(Path picturePath, Rectangle rectangle) { } return createPictureFromBufferedImage(picturePath, format, createBufferedImage(rectangle)); } catch (Exception e) { - baseActionLoader.getExceptionHandler().handleException(new SakuliException(e, + baseActionLoader.getExceptionHandler().handleException(new SakuliCheckedException(e, "Can't create Screenshot for path '" + picturePath + "'")); return null; } @@ -210,7 +210,7 @@ public Path takeScreenshotWithTimestamp(String message, Path folderPath, String try { return takeScreenshotWithTimestampThrowIOException(message, folderPath, format, rectangle); } catch (IOException e) { - baseActionLoader.getExceptionHandler().handleException(new SakuliException(e, + baseActionLoader.getExceptionHandler().handleException(new SakuliCheckedException(e, "Can't execute 'takeScreenshotWithTimestamp()' for '" + message + ", " + folderPath + ", " + format + "'")); return null; } diff --git a/src/core/src/main/java/org/sakuli/aop/RhinoAspect.java b/src/core/src/main/java/org/sakuli/aop/RhinoAspect.java index 8f561e1b..d09cf708 100644 --- a/src/core/src/main/java/org/sakuli/aop/RhinoAspect.java +++ b/src/core/src/main/java/org/sakuli/aop/RhinoAspect.java @@ -74,7 +74,7 @@ public void getRhinoScriptRunner(JoinPoint joinPoint) { * Pointcut for the {@link org.sakuli.actions.TestCaseAction} class to do an {@link * #addActionLog(org.aspectj.lang.JoinPoint, org.sakuli.actions.logging.LogToResult)} */ - @Before("execution(* org.sakuli.actions.TestCaseAction.*(..)) &&" + + @Before("execution(* org.sakuli.actions.*.*(..)) &&" + "@annotation(logToResult)") public void doTestCaseActionLog(JoinPoint joinPoint, LogToResult logToResult) { addActionLog(joinPoint, logToResult); diff --git a/src/core/src/main/java/org/sakuli/aop/SahiCommandExecutionAspect.java b/src/core/src/main/java/org/sakuli/aop/SahiCommandExecutionAspect.java index a01add9b..52494cef 100644 --- a/src/core/src/main/java/org/sakuli/aop/SahiCommandExecutionAspect.java +++ b/src/core/src/main/java/org/sakuli/aop/SahiCommandExecutionAspect.java @@ -77,15 +77,15 @@ public String[] getCommandTokens(ProceedingJoinPoint joinPoint, String commandSt /** * Catch exceptions that would have been droped from {@link net.sf.sahi.util.Utils} of all 'execute*' methods and * forward it to the exception Handler. - * + *

* Exceptions which thrown by missing "keytool" command, will be ignored , due to the internal exception handling * strategie at the point {@link Configuration#getKeytoolPath()}. * * @param joinPoint the {@link JoinPoint} of the invoked method - * @param error any {@link Throwable} thrown of the Method + * @param error any {@link Exception} thrown of the Method */ @AfterThrowing(pointcut = "execution(* net.sf.sahi.util.Utils.execute*(..))", throwing = "error") - public void catchSahiCommandExcecutionErrors(JoinPoint joinPoint, Throwable error) { + public void catchSahiCommandExcecutionErrors(JoinPoint joinPoint, Exception error) { String argString = printArgs(joinPoint, true); if (!argString.contains("keytool")) { BeanLoader.loadBaseActionLoader().getExceptionHandler().handleException(new SakuliInitException(error, diff --git a/src/core/src/main/java/org/sakuli/datamodel/AbstractTestDataEntity.java b/src/core/src/main/java/org/sakuli/datamodel/AbstractTestDataEntity.java index b1e6d7fc..db63a151 100644 --- a/src/core/src/main/java/org/sakuli/datamodel/AbstractTestDataEntity.java +++ b/src/core/src/main/java/org/sakuli/datamodel/AbstractTestDataEntity.java @@ -34,16 +34,16 @@ /** * @author tschneck - * Date: 12.07.13 + * Date: 12.07.13 */ -public abstract class AbstractTestDataEntity implements Comparable { +public abstract class AbstractTestDataEntity implements Comparable { public final static DateFormat GUID_DATE_FORMATE = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SS"); public final static DateFormat PRINT_DATE_FORMATE = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); protected transient final Logger logger = LoggerFactory.getLogger(this.getClass()); protected Date startDate; protected Date stopDate; - protected E exception; + protected Exception exception; protected S state; protected String name; /** @@ -52,7 +52,7 @@ public abstract class AbstractTestDataEntity { +public class TestCase extends AbstractTestDataEntity { /** * {@link #id} and {@link # startUrl} will be set with the method {@link org.sakuli.starter.SahiConnector#init()} @@ -39,7 +38,7 @@ public class TestCase extends AbstractTestDataEntity steps; diff --git a/src/core/src/main/java/org/sakuli/datamodel/TestCaseStep.java b/src/core/src/main/java/org/sakuli/datamodel/TestCaseStep.java index fd938f92..ce2ba050 100644 --- a/src/core/src/main/java/org/sakuli/datamodel/TestCaseStep.java +++ b/src/core/src/main/java/org/sakuli/datamodel/TestCaseStep.java @@ -20,7 +20,6 @@ import org.apache.commons.lang.StringUtils; import org.sakuli.datamodel.state.TestCaseStepState; -import org.sakuli.exceptions.SakuliException; import java.util.ArrayList; import java.util.List; @@ -31,7 +30,7 @@ * * @author tschneck Date: 18.06.13 */ -public class TestCaseStep extends AbstractTestDataEntity { +public class TestCaseStep extends AbstractTestDataEntity { private List testActions = new ArrayList<>(); @@ -47,7 +46,9 @@ public void refreshState() { } //if a step exceed the runtime set WARNING TestCaseStepState newState; - if (warningTime > 0 && getDuration() > warningTime) { + if (criticalTime > 0 && getDuration() > criticalTime) { + newState = TestCaseStepState.CRITICAL; + } else if (warningTime > 0 && getDuration() > warningTime) { newState = TestCaseStepState.WARNING; } else if (startDate != null && stopDate != null) { newState = TestCaseStepState.OK; diff --git a/src/core/src/main/java/org/sakuli/datamodel/TestSuite.java b/src/core/src/main/java/org/sakuli/datamodel/TestSuite.java index aadacd15..be4efb7b 100644 --- a/src/core/src/main/java/org/sakuli/datamodel/TestSuite.java +++ b/src/core/src/main/java/org/sakuli/datamodel/TestSuite.java @@ -18,17 +18,19 @@ package org.sakuli.datamodel; -import org.sakuli.datamodel.properties.SakuliProperties; import org.sakuli.datamodel.properties.TestSuiteProperties; import org.sakuli.datamodel.state.TestCaseState; import org.sakuli.datamodel.state.TestSuiteState; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.nio.file.Path; -import java.util.*; +import java.util.HashMap; +import java.util.Map; +import java.util.SortedSet; +import java.util.TreeSet; import static org.apache.commons.lang.StringUtils.*; @@ -36,7 +38,7 @@ * @author tschneck Date: 10.06.13 */ @Component -public class TestSuite extends AbstractTestDataEntity { +public class TestSuite extends AbstractTestDataEntity { //browser name where to start the test execution private String browserName; @@ -82,7 +84,7 @@ public void refreshState() { //if errors are found suite state is always error! if (tc.getState() == null) { - tc.addException(new SakuliException("ERROR: NO RESULT STATE SET")); + tc.addException(new SakuliCheckedException("ERROR: NO RESULT STATE SET")); state = TestSuiteState.ERRORS; } else if (tc.getState().equals(TestCaseState.ERRORS)) { state = TestSuiteState.ERRORS; @@ -206,14 +208,6 @@ public void setBrowserInfo(String browserInfo) { this.browserInfo = browserInfo; } - /** - * @return a unique identifier for each execution of the test suite - */ - public String getGuid() { - Date guidDate = startDate != null ? startDate : new Date(); - return id + "__" + GUID_DATE_FORMATE.format(guidDate); - } - public void addTestCase(String testCaseId, TestCase testCase) { if (this.testCases == null) { this.testCases = new HashMap<>(); diff --git a/src/core/src/main/java/org/sakuli/datamodel/actions/ImageLib.java b/src/core/src/main/java/org/sakuli/datamodel/actions/ImageLib.java index 820dad70..90b3ef44 100644 --- a/src/core/src/main/java/org/sakuli/datamodel/actions/ImageLib.java +++ b/src/core/src/main/java/org/sakuli/datamodel/actions/ImageLib.java @@ -19,7 +19,7 @@ package org.sakuli.datamodel.actions; import org.sakuli.actions.settings.ScreenBasedSettings; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sikuli.script.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,7 +56,7 @@ public void addImagesFromFolder(Path... paths) throws IOException { if (imageLibObject.getId() != null) { put(imageLibObject.getId(), imageLibObject); } - } catch (SakuliException e) { + } catch (SakuliCheckedException e) { logger.error(e.getMessage()); } } @@ -66,17 +66,17 @@ public void addImagesFromFolder(Path... paths) throws IOException { /** * getter, which checks the format of the pic names. - * The function should prevent typos and throws a {@link SakuliException} + * The function should prevent typos and throws a {@link SakuliCheckedException} * if the pic is not loaded in the picLib * * @param imageName imageName with or without the ending ".png". * @return the corresponding {@link Pattern} object to the imageName. */ - public Pattern getPattern(String imageName) throws SakuliException { + public Pattern getPattern(String imageName) throws SakuliCheckedException { return getImage(imageName).getPattern(); } - public ImageLibObject getImage(String imageName) throws SakuliException { + public ImageLibObject getImage(String imageName) throws SakuliCheckedException { String key = imageName; if (ImageLibObject.isValidInputImageFileEnding(imageName)) { key = imageName.substring(0, imageName.lastIndexOf(".")); @@ -84,7 +84,7 @@ public ImageLibObject getImage(String imageName) throws SakuliException { //if the set imageName is not loaded in the piclib throw a exception and log it if (!this.containsKey(key)) { - throw new SakuliException("SIKULI-PIC \"" + imageName + "\" not found in the loaded picLib folders! ... available pictures are: " + this.values()); + throw new SakuliCheckedException("SIKULI-PIC \"" + imageName + "\" not found in the loaded picLib folders! ... available pictures are: " + this.values()); } ImageLibObject imageLibObject = get(key); imageLibObject.setMinSimilarity(ScreenBasedSettings.MinSimilarity); diff --git a/src/core/src/main/java/org/sakuli/datamodel/actions/ImageLibObject.java b/src/core/src/main/java/org/sakuli/datamodel/actions/ImageLibObject.java index 8c67dac0..a842a74e 100644 --- a/src/core/src/main/java/org/sakuli/datamodel/actions/ImageLibObject.java +++ b/src/core/src/main/java/org/sakuli/datamodel/actions/ImageLibObject.java @@ -18,7 +18,7 @@ package org.sakuli.datamodel.actions; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sikuli.script.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,7 +44,7 @@ public class ImageLibObject { private Pattern pattern; - public ImageLibObject(Path imageFile) throws SakuliException { + public ImageLibObject(Path imageFile) throws SakuliCheckedException { this.imageFile = imageFile; if (Files.exists(imageFile)) { @@ -66,7 +66,7 @@ else if (name.endsWith(".js")) { logger.info("internal image library: '" + imageFile.toFile().getAbsolutePath() + "' is no .png picture"); } } else { - throw new SakuliException("Image-File '" + imageFile.toFile().getAbsolutePath() + "' does not exists!"); + throw new SakuliCheckedException("Image-File '" + imageFile.toFile().getAbsolutePath() + "' does not exists!"); } } diff --git a/src/core/src/main/java/org/sakuli/datamodel/helper/TestCaseStepHelper.java b/src/core/src/main/java/org/sakuli/datamodel/helper/TestCaseStepHelper.java index 4d5a048e..e23b8990 100644 --- a/src/core/src/main/java/org/sakuli/datamodel/helper/TestCaseStepHelper.java +++ b/src/core/src/main/java/org/sakuli/datamodel/helper/TestCaseStepHelper.java @@ -100,8 +100,4 @@ private static String getStepName(List steps) { return sb.toString(); } - public static String checkWarningTime(int warningTime, String stepName) { - return TestDataEntityHelper.checkWarningAndCriticalTime(warningTime, 0, - String.format("TestCaseStep [name = %s]", stepName)); - } } diff --git a/src/core/src/main/java/org/sakuli/datamodel/state/TestCaseState.java b/src/core/src/main/java/org/sakuli/datamodel/state/TestCaseState.java index 6fc8e22e..c52c2e4d 100644 --- a/src/core/src/main/java/org/sakuli/datamodel/state/TestCaseState.java +++ b/src/core/src/main/java/org/sakuli/datamodel/state/TestCaseState.java @@ -39,6 +39,11 @@ public enum TestCaseState implements SakuliState { */ WARNING_IN_STEP(1, "warning in step"), + /** + * value = 5 + */ + CRITICAL_IN_STEP(5, "critical in step"), + /** * value = 2 */ @@ -74,11 +79,11 @@ public int getErrorCode() { @Override public int getNagiosErrorCode() { - if (getOkCodes().contains(this)) { + if (isOk()) { return 0; - } else if (getWarningCodes().contains(this)) { + } else if (isWarning()) { return 1; - } else if (getCriticalCodes().contains(this)) { + } else if (isCritical()) { return 2; } return 3; @@ -127,6 +132,6 @@ public List getOkCodes() { } public List getCriticalCodes() { - return Arrays.asList(CRITICAL, ERRORS); + return Arrays.asList(CRITICAL, CRITICAL_IN_STEP, ERRORS); } } \ No newline at end of file diff --git a/src/core/src/main/java/org/sakuli/datamodel/state/TestCaseStepState.java b/src/core/src/main/java/org/sakuli/datamodel/state/TestCaseStepState.java index dfb4d72a..1fa2f097 100644 --- a/src/core/src/main/java/org/sakuli/datamodel/state/TestCaseStepState.java +++ b/src/core/src/main/java/org/sakuli/datamodel/state/TestCaseStepState.java @@ -19,6 +19,7 @@ package org.sakuli.datamodel.state; import java.util.Arrays; +import java.util.List; /** * Enum which represents the Sahi-Case-Stati in file "sahi_return_codes" @@ -37,6 +38,11 @@ public enum TestCaseStepState implements SakuliState { */ WARNING(1, "warning"), + /** + * value = 2 + */ + CRITICAL(2, "critical"), + /** * value = 1 */ @@ -55,23 +61,22 @@ public enum TestCaseStepState implements SakuliState { this.stateDescription = stateDescription; } - public int getErrorCode() { - return errorCode; - } - @Override public int getNagiosErrorCode() { - switch (this) { - case OK: - return 0; - case WARNING: - return 1; - case ERRORS: - return 2; + if (isOk()) { + return 0; + } else if (isWarning()) { + return 1; + } else if (isCritical()) { + return 2; } return 3; } + public int getErrorCode() { + return errorCode; + } + @Override public String getNagiosStateDescription() { return stateDescription; @@ -79,17 +84,29 @@ public String getNagiosStateDescription() { @Override public boolean isOk() { - return this.equals(OK); + return getOkCodes().contains(this); } @Override public boolean isWarning() { - return this.equals(WARNING); + return getWarningCodes().contains(this); } @Override public boolean isCritical() { - return false; + return getCriticalCodes().contains(this); + } + + public List getOkCodes() { + return Arrays.asList(OK); + } + + public List getWarningCodes() { + return Arrays.asList(WARNING); + } + + public List getCriticalCodes() { + return Arrays.asList(CRITICAL, ERRORS); } @Override @@ -102,5 +119,4 @@ public boolean isFinishedWithoutErrors() { return !Arrays.asList(INIT, ERRORS).contains(this); } - -} \ No newline at end of file +} diff --git a/src/core/src/main/java/org/sakuli/datamodel/state/TestSuiteState.java b/src/core/src/main/java/org/sakuli/datamodel/state/TestSuiteState.java index c3e75a8e..478a4859 100644 --- a/src/core/src/main/java/org/sakuli/datamodel/state/TestSuiteState.java +++ b/src/core/src/main/java/org/sakuli/datamodel/state/TestSuiteState.java @@ -49,6 +49,11 @@ public enum TestSuiteState implements SakuliState { */ WARNING_IN_SUITE(3, "warning"), + /** + * value = 7 + */ + CRITICAL_IN_STEP(7, "critical in step"), + /** * value = 4 */ @@ -80,6 +85,7 @@ public int getErrorCode() { return errorCode; } + @Override public int getNagiosErrorCode() { if (isOk()) { return 0; @@ -132,7 +138,7 @@ public List getOkCodes() { } public List getCriticalCodes() { - return Arrays.asList(CRITICAL_IN_SUITE, CRITICAL_IN_CASE, ERRORS); + return Arrays.asList(CRITICAL_IN_SUITE, CRITICAL_IN_CASE, CRITICAL_IN_STEP, ERRORS); } } \ No newline at end of file diff --git a/src/core/src/main/java/org/sakuli/exceptions/SahiActionException.java b/src/core/src/main/java/org/sakuli/exceptions/SahiActionException.java index fa31f526..d4854703 100644 --- a/src/core/src/main/java/org/sakuli/exceptions/SahiActionException.java +++ b/src/core/src/main/java/org/sakuli/exceptions/SahiActionException.java @@ -23,7 +23,7 @@ /** * @author Tobias Schneck */ -public class SahiActionException extends SakuliException { +public class SahiActionException extends SakuliCheckedException { public SahiActionException(LogResult logResult) { super(logResult.toErrorMessage()); diff --git a/src/core/src/main/java/org/sakuli/exceptions/SakuliActionException.java b/src/core/src/main/java/org/sakuli/exceptions/SakuliActionException.java index b9c7dbda..56e162c4 100644 --- a/src/core/src/main/java/org/sakuli/exceptions/SakuliActionException.java +++ b/src/core/src/main/java/org/sakuli/exceptions/SakuliActionException.java @@ -21,12 +21,12 @@ import org.sakuli.actions.screenbased.RegionImpl; /** - * Wraper for Sakuli Exceptions from a {@link org.sakuli.actions.screenbased.Region}. + * Wrapper for Sakuli Exceptions from a {@link org.sakuli.actions.screenbased.Region}. * * @author tschneck - * Date: 01.09.13 + * Date: 01.09.13 */ -public class SakuliActionException extends SakuliException { +public class SakuliActionException extends SakuliCheckedException { protected RegionImpl lastRegion; @@ -39,7 +39,7 @@ public SakuliActionException(String s, RegionImpl lastRegion) { this.lastRegion = lastRegion; } - public SakuliActionException(Throwable e, RegionImpl lastRegion) { + public SakuliActionException(Exception e, RegionImpl lastRegion) { super(e); this.lastRegion = lastRegion; } diff --git a/src/core/src/main/java/org/sakuli/exceptions/SakuliCheckedException.java b/src/core/src/main/java/org/sakuli/exceptions/SakuliCheckedException.java new file mode 100644 index 00000000..3a5d10bf --- /dev/null +++ b/src/core/src/main/java/org/sakuli/exceptions/SakuliCheckedException.java @@ -0,0 +1,87 @@ +/* + * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. + * + * Copyright 2013 - 2015 the original author or authors. + * + * 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 org.sakuli.exceptions; + +import org.sakuli.datamodel.AbstractTestDataEntity; + +import java.util.Optional; + +/** + * @author tschneck + * Date: 20.06.13 + */ +public class SakuliCheckedException extends Exception implements SakuliException { + + @SuppressWarnings("OptionalUsedAsFieldOrParameterType") + private Optional asyncTestDataRef = Optional.empty(); + + /** + * creates a new {@link SakuliException} from type {@link Exception} + * + * @param message message of the exception + */ + public SakuliCheckedException(String message) { + super(message); + } + + /** + * wraps a {@link Exception} to a {@link SakuliException} + * + * @param e + */ + public SakuliCheckedException(Exception e) { + //use this constructor to avoid to get the classname as prefix in the exception message + super(e.getLocalizedMessage(), e); + } + + /** + * wraps a {@link Exception} to a {@link SakuliException} + * + * @param suppressedException + * @param message + */ + public SakuliCheckedException(Exception suppressedException, String message) { + super(message); + this.addSuppressed(suppressedException); + } + + @Override + public String toString() { + return getLocalizedMessage(); + } + + /** + * Provides the meta information on which execution step this exception is thrown, like e.g. {@link org.sakuli.services.TeardownService#handleTeardownException(Exception, boolean, AbstractTestDataEntity)} will use it. + * + * @return a reference on the {@link AbstractTestDataEntity} which was executed at the point of the exception is thrown. + */ + public Optional getAsyncTestDataRef() { + return asyncTestDataRef; + } + + /** + * Set additional meta information to provide on which execution step in async code like the {@link org.sakuli.services.TeardownService#handleTeardownException(Exception, boolean, AbstractTestDataEntity)} will use it. + * + * @param testDataRef extends {@link AbstractTestDataEntity} + */ + @Override + public void setAsyncTestDataRef(T testDataRef) { + this.asyncTestDataRef = Optional.ofNullable(testDataRef); + } +} diff --git a/src/core/src/main/java/org/sakuli/exceptions/SakuliCipherException.java b/src/core/src/main/java/org/sakuli/exceptions/SakuliCipherException.java index 57f024ff..d30834ed 100644 --- a/src/core/src/main/java/org/sakuli/exceptions/SakuliCipherException.java +++ b/src/core/src/main/java/org/sakuli/exceptions/SakuliCipherException.java @@ -20,9 +20,9 @@ /** * @author tschneck - * Date: 06.08.13 + * Date: 06.08.13 */ -public class SakuliCipherException extends SakuliException { +public class SakuliCipherException extends SakuliCheckedException { public String cipherLog; @@ -39,22 +39,22 @@ public SakuliCipherException(String message, String cipherLog) { } /** - * @param e any {@link Throwable} + * @param e any {@link Exception} */ - public SakuliCipherException(Throwable e, String cipherLog) { + public SakuliCipherException(Exception e, String cipherLog) { super(e); this.cipherLog = cipherLog; } /** - * creates a exception with the main message and adds the {@link Throwable} + * creates a exception with the main message and adds the {@link Exception} * * @param mainMessage - * @param suppressedThrowable + * @param suppressedException */ - public SakuliCipherException(String mainMessage, String cipherLog, Throwable suppressedThrowable) { + public SakuliCipherException(String mainMessage, String cipherLog, Exception suppressedException) { super(mainMessage); - this.addSuppressed(suppressedThrowable); + this.addSuppressed(suppressedException); this.cipherLog = cipherLog; } diff --git a/src/core/src/main/java/org/sakuli/exceptions/SakuliException.java b/src/core/src/main/java/org/sakuli/exceptions/SakuliException.java index dcd9f413..0840286a 100644 --- a/src/core/src/main/java/org/sakuli/exceptions/SakuliException.java +++ b/src/core/src/main/java/org/sakuli/exceptions/SakuliException.java @@ -1,7 +1,7 @@ /* * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. * - * Copyright 2013 - 2015 the original author or authors. + * Copyright 2013 - 2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,45 +18,32 @@ package org.sakuli.exceptions; +import org.sakuli.datamodel.AbstractTestDataEntity; + +import java.util.Optional; + /** - * @author tschneck - * Date: 20.06.13 + * Marker interface for all SakuliExceptions */ -public class SakuliException extends Exception { +public interface SakuliException { /** - * creates a new {@link SakuliException} from type {@link Throwable} + * Provides the meta information on which execution step this exception is thrown, like e.g. {@link org.sakuli.services.TeardownService#handleTeardownException(Exception, boolean, AbstractTestDataEntity)} will use it. * - * @param message message of the exception + * @return a reference on the {@link AbstractTestDataEntity} which was executed at the point of the exception is thrown. */ - public SakuliException(String message) { - super(message); - } + Optional getAsyncTestDataRef(); /** - * wraps a {@link Throwable} to a {@link SakuliException} + * Set additional meta information to provide on which execution step in async code like the {@link org.sakuli.services.TeardownService#handleTeardownException(Exception, boolean, AbstractTestDataEntity)} will use it. * - * @param e + * @param testDataRef extends {@link AbstractTestDataEntity} */ - public SakuliException(Throwable e) { - //use this constructor to avoid to get the classname as prefix in the exception message - super(e.getLocalizedMessage(), e); - } + void setAsyncTestDataRef(T testDataRef); - /** - * wraps a {@link Throwable} to a {@link SakuliException} - * - * @param suppressedException - * @param message - */ - public SakuliException(Throwable suppressedException, String message) { - super(message); - this.addSuppressed(suppressedException); - } + String getMessage(); - @Override - public String toString() { - return getLocalizedMessage(); + default Exception castTo() { + return SakuliExceptionHandler.castTo(this); } - } diff --git a/src/core/src/main/java/org/sakuli/exceptions/SakuliExceptionHandler.java b/src/core/src/main/java/org/sakuli/exceptions/SakuliExceptionHandler.java index 879fe25c..37e29ea6 100644 --- a/src/core/src/main/java/org/sakuli/exceptions/SakuliExceptionHandler.java +++ b/src/core/src/main/java/org/sakuli/exceptions/SakuliExceptionHandler.java @@ -40,7 +40,6 @@ /** * @author tschneck Date: 12.07.13 */ -@SuppressWarnings("ThrowableResultOfMethodCallIgnored") @Component public class SakuliExceptionHandler { @@ -48,14 +47,14 @@ public class SakuliExceptionHandler { @Autowired private ScreenActionLoader loader; - private List processedExceptions = new ArrayList<>(); - private List resumeExceptions = new ArrayList<>(); + private List processedExceptions = new ArrayList<>(); + private List resumeExceptions = new ArrayList<>(); /** * @return all exceptions from the test suite and there underlying test cases. */ - public static List getAllExceptions(TestSuite testSuite) { - List result = new ArrayList<>(); + public static List getAllExceptions(TestSuite testSuite) { + List result = new ArrayList<>(); if (testSuite.getException() != null) { result.add(testSuite.getException()); } @@ -77,12 +76,12 @@ public static List getAllExceptions(TestSuite testSuite) { } /** - * Checks if a {@link Throwable} is an instance of the {@link SakuliExceptionWithScreenshot}. + * Checks if a {@link Exception} is an instance of the {@link SakuliExceptionWithScreenshot}. * - * @param e any {@link Throwable} + * @param e any {@link Exception} * @return If true the method returns a valid {@link Path}. */ - public static Path getScreenshotFile(Throwable e) { + public static Path getScreenshotFile(Exception e) { if (e instanceof SakuliExceptionWithScreenshot) { if (((SakuliExceptionWithScreenshot) e).getScreenshot() != null) { return ((SakuliExceptionWithScreenshot) e).getScreenshot(); @@ -118,16 +117,23 @@ static boolean containsException(TestSuite testSuite) { return false; } + public static Exception castTo(SakuliException e) { + if (e instanceof Exception) { + return (Exception) e; + } + throw new ClassCastException("SakuliException should extend from Exception class!"); + } + /** - * handleException methode for Eception, where no testcase could be identified; The default value for non {@link + * handleException method for exception where no testcase could be identified; The default value for non {@link * SakuliException} is there that the Execution of the tescase will stop! * - * @param e any Throwable + * @param e any Exception */ - public void handleException(Throwable e) { + public void handleException(Exception e) { //avoid nullpointer for missing messages if (e.getMessage() == null) { - e = new SakuliException(e, e.getClass().getSimpleName()); + e = new SakuliCheckedException(e, e.getClass().getSimpleName()); } //e.g. Proxy Exception should only be handled if no other exceptions have been added @@ -146,16 +152,16 @@ public void handleException(Throwable e) { * Finally transfromes and saves the exception. This method should be called if the exception should really be * processed. * - * @param e any {@link Throwable} + * @param e any {@link Exception} */ - protected void processException(Throwable e) { + protected void processException(Exception e) { SakuliException transformedException = transformException(e); //Do different exception handling for different use cases: - if (!resumeToTestExcecution(e)) { + if (!resumeToTestExecution(e)) { //normal handling logger.error(transformedException.getMessage(), transformedException); - saveException(transformedException); + saveException(transformedException.castTo()); // a {@link SakuliForwarderException}, should only added to the report and not stop sahi, because // this error types only on already started the tear down of test suite. @@ -171,20 +177,20 @@ else if (!(e instanceof SahiActionException)) { else if (!loader.getSakuliProperties().isSuppressResumedExceptions()) { // if suppressResumedExceptions == false logger.error(e.getMessage(), transformedException); - saveException(transformedException); + saveException(transformedException.castTo()); addExceptionToSahiReport(transformedException); } else { //if suppressResumedExceptions == true logger.debug(transformedException.getMessage(), transformedException); } processedExceptions.add(e); - processedExceptions.add(transformedException); + processedExceptions.add(transformedException.castTo()); } /** * @return true if the exception have been already processed by Sakuli */ - public boolean isAlreadyProcessed(Throwable e) { + public boolean isAlreadyProcessed(Exception e) { String message = e.getMessage() != null ? e.getMessage() : e.toString(); return message.contains(RhinoAspect.ALREADY_PROCESSED) || message.contains(("Logging exception:")) || processedExceptions.contains(e); @@ -193,7 +199,7 @@ public boolean isAlreadyProcessed(Throwable e) { /** * @return true if, the exception should NOT stop the test case execution */ - public boolean resumeToTestExcecution(Throwable e) { + public boolean resumeToTestExecution(Exception e) { return resumeExceptions.contains(e); } @@ -201,9 +207,17 @@ public boolean resumeToTestExcecution(Throwable e) { * save the exception to the current testcase. If the current testcase is not reachale, then the exception will be * saved to the test suite. * - * @param e any {@link SakuliException} + * @param e any {@link Exception} */ - void saveException(SakuliException e) { + void saveException(Exception e) { + if (e instanceof SakuliException) { + if (((SakuliException) e).getAsyncTestDataRef().isPresent()) { + ((SakuliException) e).getAsyncTestDataRef().get().addException(e); + //skip test handling tasks + return; + } + } + if (loader.getCurrentTestCase() != null) { if (loader.getCurrentTestCaseStep() != null) { loader.getCurrentTestCaseStep().addException(e); @@ -227,7 +241,7 @@ private void addExceptionToSahiReport(SakuliException e) { loader.getSahiReport().addResult( e.getMessage(), ResultType.ERROR, - e.getStackTrace().toString(), + e.castTo().getStackTrace().toString(), e.getMessage() + RhinoAspect.ALREADY_PROCESSED); } } @@ -247,15 +261,15 @@ private void stopExecutionAndAddExceptionToSahiReport(SakuliException e) { } /** - * transforms any {@link Throwable} to SakuliException. If the property 'sakuli.screenshot.onError=true' is set, the - * methods add a Screenshot. + * transforms any {@link Exception} to SakuliException. If the property 'sakuli.screenshot.onError=true' + * is set, the method add a screenshot. * - * @param e a {@link Throwable} - * @return {@link SakuliException} or any child. + * @param e a {@link Exception} + * @return {@link SakuliException} or any child. */ - private SakuliException transformException(Throwable e) { - if (loader.getActionProperties().isTakeScreenshots() && - !(e instanceof NonScreenshotException)) { + SakuliException transformException(Exception e) { + if (!(e instanceof NonScreenshotException) + && loader.getActionProperties().isTakeScreenshots()) { //try to get a screenshot try { Path screenshot = loader.getScreenshotActions().takeScreenshotWithTimestampThrowIOException( @@ -263,22 +277,32 @@ private SakuliException transformException(Throwable e) { loader.getActionProperties().getScreenShotFolder(), null, null); - return addResumeOnException(new SakuliExceptionWithScreenshot(e, screenshot), resumeToTestExcecution(e)); + return addResumeOnException(new SakuliExceptionWithScreenshot(e, screenshot), resumeToTestExecution(e)); } catch (IOException e2) { logger.error("Screenshot could not be created", e2); e.addSuppressed(e2); } } - return addResumeOnException((e instanceof SakuliException) ? (SakuliException) e : new SakuliException(e), resumeToTestExcecution(e)); + if (SakuliCheckedException.class.isAssignableFrom(e.getClass())) { + return addResumeOnException(((SakuliCheckedException) e), resumeToTestExecution(e)); + } + if (e instanceof SakuliRuntimeException) { + return addResumeOnException(((SakuliRuntimeException) e), resumeToTestExecution(e)); + } + //not kind of SakuliException -> wrap it + if (e instanceof RuntimeException) { + return addResumeOnException(new SakuliRuntimeException(e), resumeToTestExecution(e)); + } + return addResumeOnException(new SakuliCheckedException(e), resumeToTestExecution(e)); } - public void handleException(Throwable e, boolean resumeOnException) { + public void handleException(Exception e, boolean resumeOnException) { handleException(addResumeOnException(e, resumeOnException)); } public void handleException(String exceptionMessage, boolean resumeOnException) { - handleException(new SakuliException(exceptionMessage), resumeOnException); + handleException(new SakuliCheckedException(exceptionMessage), resumeOnException); } public void handleException(String exceptionMessage, RegionImpl lastRegion, boolean resumeOnException) { @@ -288,7 +312,7 @@ public void handleException(String exceptionMessage, RegionImpl lastRegion, bool )); } - public void handleException(Throwable e, RegionImpl lastRegion, boolean resumeOnException) { + public void handleException(Exception e, RegionImpl lastRegion, boolean resumeOnException) { handleException(addResumeOnException( lastRegion != null ? new SakuliActionException(e, lastRegion) : e, resumeOnException @@ -299,22 +323,15 @@ public void handleException(LogResult logResult) { handleException(new SahiActionException(logResult)); } - private T addResumeOnException(T e, boolean resumeOnException) { - if (resumeOnException) { + private T addResumeOnException(T e, boolean resumeOnException) { + if (resumeOnException + //Forwarder error should never stop the test executions + || e instanceof SakuliForwarderException + //also exception within an async execution should not stop the execution + || (e instanceof SakuliException && ((SakuliException) e).getAsyncTestDataRef().isPresent()) + ) { resumeExceptions.add(e); } return e; } - - /** - * Throws a new {@link SakuliRuntimeException} for all collected resumed exceptions. A resumed exception have been - * created by{@link #handleException(Throwable, boolean)} with resumeOnException==true. - */ - public void throwCollectedResumedExceptions() { - if (resumeExceptions.size() > 0) { - SakuliRuntimeException e = new SakuliRuntimeException("test contains some suppressed resumed exceptions!"); - resumeExceptions.stream().forEach(t -> e.addSuppressed(t)); - throw e; - } - } } diff --git a/src/core/src/main/java/org/sakuli/exceptions/SakuliExceptionWithScreenshot.java b/src/core/src/main/java/org/sakuli/exceptions/SakuliExceptionWithScreenshot.java index aa14b656..2af8b26f 100644 --- a/src/core/src/main/java/org/sakuli/exceptions/SakuliExceptionWithScreenshot.java +++ b/src/core/src/main/java/org/sakuli/exceptions/SakuliExceptionWithScreenshot.java @@ -22,14 +22,14 @@ /** * @author tschneck - * Date: 20.06.13 + * Date: 20.06.13 */ -public class SakuliExceptionWithScreenshot extends SakuliException { +public class SakuliExceptionWithScreenshot extends SakuliCheckedException { private Path screenshot; /** - * creates a {@link SakuliException} from a {@link String} and stores the Path to screenshot + * creates a {@link SakuliCheckedException} from a {@link String} and stores the Path to screenshot * * @param message * @param screenshot @@ -41,12 +41,12 @@ public SakuliExceptionWithScreenshot(String message, Path screenshot) { /** - * creates a {@link SakuliException} from a {@link Throwable} and stores the Path to screenshot + * creates a {@link SakuliCheckedException} from a {@link Exception} and stores the Path to screenshot * * @param e * @param screenshot */ - public SakuliExceptionWithScreenshot(Throwable e, Path screenshot) { + public SakuliExceptionWithScreenshot(Exception e, Path screenshot) { super(e); this.screenshot = screenshot; } diff --git a/src/core/src/main/java/org/sakuli/services/common/AbstractResultService.java b/src/core/src/main/java/org/sakuli/exceptions/SakuliForwarderCheckedException.java similarity index 53% rename from src/core/src/main/java/org/sakuli/services/common/AbstractResultService.java rename to src/core/src/main/java/org/sakuli/exceptions/SakuliForwarderCheckedException.java index 514b2f76..704d5cc4 100644 --- a/src/core/src/main/java/org/sakuli/services/common/AbstractResultService.java +++ b/src/core/src/main/java/org/sakuli/exceptions/SakuliForwarderCheckedException.java @@ -16,31 +16,21 @@ * limitations under the License. */ -package org.sakuli.services.common; - -import org.sakuli.datamodel.TestSuite; -import org.sakuli.exceptions.SakuliExceptionHandler; -import org.sakuli.services.ResultService; -import org.springframework.beans.factory.annotation.Autowired; +package org.sakuli.exceptions; /** - * @author tschneck + * Checked Exception for {@link SakuliForwarderException} */ -public abstract class AbstractResultService implements ResultService { - - @Autowired - protected SakuliExceptionHandler exceptionHandler; - @Autowired - protected TestSuite testSuite; +public class SakuliForwarderCheckedException extends SakuliCheckedException implements SakuliForwarderException { + public SakuliForwarderCheckedException(String message) { + super(message); + } - @Override - public void refreshStates() { - testSuite.refreshState(); + public SakuliForwarderCheckedException(Exception e) { + super(e); } - @Override - public void triggerAction() { - this.refreshStates(); - this.saveAllResults(); + public SakuliForwarderCheckedException(Exception suppressedException, String message) { + super(suppressedException, message); } } diff --git a/src/core/src/main/java/org/sakuli/exceptions/SakuliForwarderException.java b/src/core/src/main/java/org/sakuli/exceptions/SakuliForwarderException.java index 60e21fe6..47ce2a1d 100644 --- a/src/core/src/main/java/org/sakuli/exceptions/SakuliForwarderException.java +++ b/src/core/src/main/java/org/sakuli/exceptions/SakuliForwarderException.java @@ -1,7 +1,7 @@ /* * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. * - * Copyright 2013 - 2015 the original author or authors. + * Copyright 2013 - 2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,19 +19,8 @@ package org.sakuli.exceptions; /** - * Wrapper for a {@link SakuliException} thrown by the sakuli {@link org.sakuli.services.forwarder} services. - * For this kind of exception, not error screenshot is necessary + * Marker for a {@link SakuliException} thrown by the sakuli {@link org.sakuli.services.forwarder} services. + * For this kind of exception, no error screenshot will created. */ -public class SakuliForwarderException extends SakuliException implements NonScreenshotException { - public SakuliForwarderException(String message) { - super(message); - } - - public SakuliForwarderException(Throwable e) { - super(e); - } - - public SakuliForwarderException(Throwable suppressedException, String message) { - super(suppressedException, message); - } +public interface SakuliForwarderException extends NonScreenshotException, SakuliException { } diff --git a/src/core/src/main/java/org/sakuli/exceptions/SakuliForwarderRuntimeException.java b/src/core/src/main/java/org/sakuli/exceptions/SakuliForwarderRuntimeException.java new file mode 100644 index 00000000..efbe046b --- /dev/null +++ b/src/core/src/main/java/org/sakuli/exceptions/SakuliForwarderRuntimeException.java @@ -0,0 +1,36 @@ +/* + * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. + * + * Copyright 2013 - 2015 the original author or authors. + * + * 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 org.sakuli.exceptions; + +/** + * {@link RuntimeException} for {@link SakuliForwarderException}s + */ +public class SakuliForwarderRuntimeException extends SakuliRuntimeException implements SakuliForwarderException { + public SakuliForwarderRuntimeException(String message) { + super(message); + } + + public SakuliForwarderRuntimeException(Exception e) { + super(e); + } + + public SakuliForwarderRuntimeException(String message, Exception suppressedException) { + super(message, suppressedException); + } +} diff --git a/src/core/src/main/java/org/sakuli/exceptions/SakuliInitException.java b/src/core/src/main/java/org/sakuli/exceptions/SakuliInitException.java index b0046ea1..a3fce1b3 100644 --- a/src/core/src/main/java/org/sakuli/exceptions/SakuliInitException.java +++ b/src/core/src/main/java/org/sakuli/exceptions/SakuliInitException.java @@ -19,18 +19,18 @@ package org.sakuli.exceptions; /** - * Wrapper for a {@link SakuliException} thrown when something went wrong during the initialisation. + * Wrapper for a {@link SakuliCheckedException} thrown when something went wrong during the initialisation. */ -public class SakuliInitException extends SakuliException implements NonScreenshotException { +public class SakuliInitException extends SakuliCheckedException implements NonScreenshotException { public SakuliInitException(String message) { super(message); } - public SakuliInitException(Throwable e) { + public SakuliInitException(Exception e) { super(e); } - public SakuliInitException(Throwable e, String message) { + public SakuliInitException(Exception e, String message) { super(e, message); } } diff --git a/src/core/src/main/java/org/sakuli/exceptions/SakuliRuntimeException.java b/src/core/src/main/java/org/sakuli/exceptions/SakuliRuntimeException.java index 8297c4dd..442221c5 100644 --- a/src/core/src/main/java/org/sakuli/exceptions/SakuliRuntimeException.java +++ b/src/core/src/main/java/org/sakuli/exceptions/SakuliRuntimeException.java @@ -18,12 +18,18 @@ package org.sakuli.exceptions; +import org.sakuli.datamodel.AbstractTestDataEntity; + +import java.util.Optional; + /** - * Wrapper for alle sakuli runtime exceptins + * Wrapper for all sakuli runtime exceptions * * @author Tobias Schneck */ -public class SakuliRuntimeException extends RuntimeException { +public class SakuliRuntimeException extends RuntimeException implements SakuliException { + @SuppressWarnings("OptionalUsedAsFieldOrParameterType") + private Optional asyncTestDataRef = Optional.empty(); public SakuliRuntimeException(String reason, Exception e) { super(reason, e); @@ -36,4 +42,28 @@ public SakuliRuntimeException(String reason) { public SakuliRuntimeException(Exception e) { super(e); } + + /** + * Provides the meta information on which execution step this exception is thrown, like e.g. {@link org.sakuli.services.TeardownService#handleTeardownException(Exception, boolean, AbstractTestDataEntity)} will use it. + * + * @return a reference on the {@link AbstractTestDataEntity} which was executed at the point of the exception is thrown. + */ + public Optional getAsyncTestDataRef() { + return asyncTestDataRef; + } + + /** + * Set additional meta information to provide on which execution step in async code like the {@link org.sakuli.services.TeardownService#handleTeardownException(Exception, boolean, AbstractTestDataEntity)} will use it. + * + * @param testDataRef extends {@link AbstractTestDataEntity} + */ + @Override + public void setAsyncTestDataRef(T testDataRef) { + this.asyncTestDataRef = Optional.ofNullable(testDataRef); + } + + @Override + public String toString() { + return getLocalizedMessage(); + } } diff --git a/src/core/src/main/java/org/sakuli/exceptions/SakuliValidationException.java b/src/core/src/main/java/org/sakuli/exceptions/SakuliValidationException.java index 58e25d23..972b699a 100644 --- a/src/core/src/main/java/org/sakuli/exceptions/SakuliValidationException.java +++ b/src/core/src/main/java/org/sakuli/exceptions/SakuliValidationException.java @@ -22,16 +22,16 @@ * Implementation for a {@link Exception}s which should be thrown if an validation during the testcase went wrong * and a screenshot should NOT be created. */ -public class SakuliValidationException extends SakuliException implements NonScreenshotException { +public class SakuliValidationException extends SakuliCheckedException implements NonScreenshotException { public SakuliValidationException(String message) { super(message); } - public SakuliValidationException(Throwable e) { + public SakuliValidationException(Exception e) { super(e); } - public SakuliValidationException(Throwable suppressedException, String message) { + public SakuliValidationException(Exception suppressedException, String message) { super(suppressedException, message); } } diff --git a/src/core/src/main/java/org/sakuli/loader/BaseActionLoader.java b/src/core/src/main/java/org/sakuli/loader/BaseActionLoader.java index b5aa6ed5..42013edc 100644 --- a/src/core/src/main/java/org/sakuli/loader/BaseActionLoader.java +++ b/src/core/src/main/java/org/sakuli/loader/BaseActionLoader.java @@ -28,7 +28,7 @@ import org.sakuli.datamodel.properties.SahiProxyProperties; import org.sakuli.datamodel.properties.SakuliProperties; import org.sakuli.datamodel.properties.TestSuiteProperties; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.exceptions.SakuliExceptionHandler; import org.sakuli.services.cipher.CipherService; @@ -87,9 +87,9 @@ public interface BaseActionLoader { * Adds the additional paths to the current {@link ImageLib} object. * * @param imagePaths one or more {@link Path} elements - * @throws SakuliException if an IO error occurs + * @throws SakuliCheckedException if an IO error occurs */ - void addImagePaths(Path... imagePaths) throws SakuliException; + void addImagePaths(Path... imagePaths) throws SakuliCheckedException; SakuliProperties getSakuliProperties(); diff --git a/src/core/src/main/java/org/sakuli/loader/BaseActionLoaderImpl.java b/src/core/src/main/java/org/sakuli/loader/BaseActionLoaderImpl.java index 76f85743..880e0972 100644 --- a/src/core/src/main/java/org/sakuli/loader/BaseActionLoaderImpl.java +++ b/src/core/src/main/java/org/sakuli/loader/BaseActionLoaderImpl.java @@ -28,7 +28,7 @@ import org.sakuli.datamodel.properties.SahiProxyProperties; import org.sakuli.datamodel.properties.SakuliProperties; import org.sakuli.datamodel.properties.TestSuiteProperties; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.exceptions.SakuliExceptionHandler; import org.sakuli.services.cipher.CipherService; import org.sakuli.utils.CleanUpHelper; @@ -97,7 +97,7 @@ public void init(String testCaseID, Path... imagePaths) { try { //set the current test case if (testSuite.getTestCase(testCaseID) == null) { - throw new SakuliException("Can't identify current test case in function init() in class SakuliBasedAction"); + throw new SakuliCheckedException("Can't identify current test case in function init() in class SakuliBasedAction"); } this.currentTestCase = testSuite.getTestCase(testCaseID); addImagePaths(imagePaths); @@ -106,7 +106,7 @@ public void init(String testCaseID, Path... imagePaths) { //add the "sakuli-delay-active" var to the script runner context if (rhinoScriptRunner == null || rhinoScriptRunner.getSession() == null) { //could be possible if the aspectj compiler won't worked correctly, see RhinoAspect#getRhinoScriptRunner - throw new SakuliException(String.format("cannot init rhino script runner with sakuli custom delay variable '%s'", + throw new SakuliCheckedException(String.format("cannot init rhino script runner with sakuli custom delay variable '%s'", SahiProxyProperties.SAHI_REQUEST_DELAY_ACTIVE_VAR)); } String isRequestDelayActive = String.valueOf(sahiProxyProperties.isRequestDelayActive()); @@ -114,7 +114,7 @@ public void init(String testCaseID, Path... imagePaths) { LOGGER.info("set isRequestDelayActive={}", isRequestDelayActive); } cleanUp(); - } catch (SakuliException e) { + } catch (SakuliCheckedException e) { exceptionHandler.handleException(e); } } @@ -124,13 +124,13 @@ protected void cleanUp() { } @Override - public void addImagePaths(Path... imagePaths) throws SakuliException { + public void addImagePaths(Path... imagePaths) throws SakuliCheckedException { //load the images for the screenbased actions if (imagePaths != null && imagePaths.length > 0) { try { imageLib.addImagesFromFolder(imagePaths); } catch (IOException e) { - throw new SakuliException(e); + throw new SakuliCheckedException(e); } } else { LOGGER.warn("No folder have been added to the test case image library!"); diff --git a/src/core/src/main/java/org/sakuli/loader/BeanLoader.java b/src/core/src/main/java/org/sakuli/loader/BeanLoader.java index 5f6f953a..e662ad93 100644 --- a/src/core/src/main/java/org/sakuli/loader/BeanLoader.java +++ b/src/core/src/main/java/org/sakuli/loader/BeanLoader.java @@ -62,8 +62,8 @@ public static Application loadApplication(String applicationNameOrPath, String r + applicationNameOrPath + "\""); try { return new Application(applicationNameOrPath, Boolean.valueOf(resumeOnException)); - } catch (Throwable throwable) { - loadBaseActionLoader().getExceptionHandler().handleException(new SakuliInitException(throwable, + } catch (Exception e) { + loadBaseActionLoader().getExceptionHandler().handleException(new SakuliInitException(e, "Unexpected error during creating an instance of class '" + Application.class.getName() + "'")); return null; } @@ -93,7 +93,7 @@ public static T loadBean(Class classDef) { try { logger.trace("load bean '{}' from application context", classDef.getSimpleName()); return getBeanFactory().getBean(classDef); - } catch (Throwable e) { + } catch (Exception e) { logger.error("error in BeanLoader", e); throw e; } diff --git a/src/core/src/main/java/org/sakuli/loader/ScreenActionLoaderImpl.java b/src/core/src/main/java/org/sakuli/loader/ScreenActionLoaderImpl.java index 085fc939..ad06cd4b 100644 --- a/src/core/src/main/java/org/sakuli/loader/ScreenActionLoaderImpl.java +++ b/src/core/src/main/java/org/sakuli/loader/ScreenActionLoaderImpl.java @@ -31,7 +31,7 @@ import org.sakuli.datamodel.properties.SahiProxyProperties; import org.sakuli.datamodel.properties.SakuliProperties; import org.sakuli.datamodel.properties.TestSuiteProperties; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.exceptions.SakuliExceptionHandler; import org.sakuli.services.cipher.CipherService; import org.springframework.beans.factory.annotation.Autowired; @@ -116,7 +116,7 @@ public void init(String testCaseID, Path... imagePaths) { } @Override - public void addImagePaths(Path... imagePaths) throws SakuliException { + public void addImagePaths(Path... imagePaths) throws SakuliCheckedException { baseLoader.addImagePaths(imagePaths); } diff --git a/src/core/src/main/java/org/sakuli/services/PrioritizedService.java b/src/core/src/main/java/org/sakuli/services/PrioritizedService.java index 5acec158..c659b195 100644 --- a/src/core/src/main/java/org/sakuli/services/PrioritizedService.java +++ b/src/core/src/main/java/org/sakuli/services/PrioritizedService.java @@ -24,7 +24,7 @@ public interface PrioritizedService { /** - * @return the Priority of the current {@link ResultService} implementation. Lower int value means lower priority. + * @return the Priority of the current {@link TeardownService} implementation. Lower int value means lower priority. * E.g. 100 is higher prioritized than 10. */ int getServicePriority(); diff --git a/src/core/src/main/java/org/sakuli/services/ResultService.java b/src/core/src/main/java/org/sakuli/services/ResultService.java index bf921c3a..669bd385 100644 --- a/src/core/src/main/java/org/sakuli/services/ResultService.java +++ b/src/core/src/main/java/org/sakuli/services/ResultService.java @@ -18,21 +18,12 @@ package org.sakuli.services; -import org.sakuli.datamodel.properties.SakuliProperties; - /** + * Marker interface which indicates all {@link TeardownService} which really save or forward a result. + * * @author tschneck Date: 23.05.14 */ public interface ResultService extends TeardownService { - /** - * refresh all states in the Sakuli test suite - */ - void refreshStates(); - - /** - * Triggers that all current results will be saved, in consideration of the {@link SakuliProperties}. - */ - void saveAllResults(); } diff --git a/src/core/src/main/java/org/sakuli/services/TeardownService.java b/src/core/src/main/java/org/sakuli/services/TeardownService.java index 80c511b7..f7071320 100644 --- a/src/core/src/main/java/org/sakuli/services/TeardownService.java +++ b/src/core/src/main/java/org/sakuli/services/TeardownService.java @@ -18,15 +18,88 @@ package org.sakuli.services; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.sakuli.datamodel.AbstractTestDataEntity; +import org.sakuli.datamodel.TestCase; +import org.sakuli.datamodel.TestCaseStep; +import org.sakuli.datamodel.TestSuite; +import org.sakuli.exceptions.SakuliRuntimeException; + +import java.util.Optional; + /** + * Service Interface which will be called on different teardown phases of the {@link AbstractTestDataEntity} objects + * * @author tschneck - * Date: 2/12/16 + * Date: 2/12/16 */ +@SuppressWarnings("OptionalUsedAsFieldOrParameterType") public interface TeardownService extends PrioritizedService { /** - * Triggers the different implementations of the {@link TeardownService}. + * see {@link #tearDown(Optional, boolean)}. + */ + default void tearDown(Optional dataEntity) { + tearDown(dataEntity, false); + } + + /** + * Triggers the default actions if some {@link AbstractTestDataEntity} are called by {@link org.sakuli.datamodel.helper.TestSuiteHelper}. + * Default caller method for: + * - {@link #teardownTestSuite(TestSuite)} + * - {@link #teardownTestCase(TestCase)} + * - {@link #teardownTestCaseStep(TestCaseStep)} + * + * @param dataEntity instace of {@link AbstractTestDataEntity} + * @param asyncCall indicates if a call is triggerd in an async process to the main process to use the correect exception handling, see {@link #handleTeardownException(Exception, boolean, AbstractTestDataEntity)}. + */ + default void tearDown(Optional dataEntity, boolean asyncCall) { + try { + dataEntity.filter(TestSuite.class::isInstance).map(TestSuite.class::cast) + .ifPresent(this::teardownTestSuite); + + dataEntity.filter(TestCase.class::isInstance).map(TestCase.class::cast) + .ifPresent(this::teardownTestCase); + + dataEntity.filter(TestCaseStep.class::isInstance).map(TestCaseStep.class::cast) + .ifPresent(this::teardownTestCaseStep); + } catch (Exception e) { + handleTeardownException(e, asyncCall, dataEntity.get()); + } + } + + /** + * Define exception handling when {@link #tearDown(Optional)} will throw an exception. + * Can't implement in an function context like here. + * + * @param e any {@link Exception} + * @param async defines if the caller is an asynchronous procedure to the main process of {@link org.sakuli.starter.SakuliStarter} + * @param testDataRef Provides the meta information on which execution step this exception is thrown. See {@link org.sakuli.exceptions.SakuliException#setAsyncTestDataRef(AbstractTestDataEntity)} + */ + void handleTeardownException(@NonNull Exception e, boolean async, @NonNull AbstractTestDataEntity testDataRef); + + /** + * Triggers the different implementations of the {@link TeardownService} for the {@link TestSuite} object. + * On Exception a {@link RuntimeException} should be thrown to be catched from {@link #tearDown(Optional, boolean)} + */ + default void teardownTestSuite(@NonNull TestSuite testSuite) throws RuntimeException { + throw new SakuliRuntimeException("Method 'teardownTestSuite' is not implemented for forwarder class " + getClass().getSimpleName()); + } + + /** + * Triggers the different implementations of the {@link TeardownService} for the {@link TestCase} object. + * On Exception a {@link RuntimeException} should be thrown to be catched from {@link #tearDown(Optional, boolean)} + */ + default void teardownTestCase(@NonNull TestCase testCase) throws RuntimeException { + throw new SakuliRuntimeException("Method 'teardownTestCase' is not implemented for forwarder class " + getClass().getSimpleName()); + } + + /** + * Triggers the different implementations of the {@link TeardownService} for the {@link TestCaseStep} object. + * On Exception a {@link RuntimeException} should be thrown to be catched from {@link #tearDown(Optional, boolean)} */ - void triggerAction(); + default void teardownTestCaseStep(@NonNull TestCaseStep testCaseStep) throws RuntimeException { + throw new SakuliRuntimeException("Method 'teardownTestCaseStep' is not implemented for forwarder class " + getClass().getSimpleName()); + } } diff --git a/src/core/src/main/java/org/sakuli/services/TeardownServiceHelper.java b/src/core/src/main/java/org/sakuli/services/TeardownServiceHelper.java index 45b15042..1066bae5 100644 --- a/src/core/src/main/java/org/sakuli/services/TeardownServiceHelper.java +++ b/src/core/src/main/java/org/sakuli/services/TeardownServiceHelper.java @@ -18,18 +18,35 @@ package org.sakuli.services; +import org.sakuli.datamodel.AbstractTestDataEntity; import org.sakuli.loader.BeanLoader; +import java.util.Optional; + /** * @author Tobias Schneck */ public class TeardownServiceHelper { + /** - * Invokes all {@link TeardownService}s, for example to save results. + * Default for {@link #invokeTeardownServices(AbstractTestDataEntity, boolean)} {@code abstractTestDataEntity, false} */ - public static void invokeTeardownServices() { - BeanLoader.loadMultipleBeans(TeardownService.class).values().stream() - .sorted(new PrioritizedServiceComparator<>()) - .forEach(TeardownService::triggerAction); + public static void invokeTeardownServices(AbstractTestDataEntity abstractTestDataEntity) { + invokeTeardownServices(abstractTestDataEntity, false); } + + /** + * Invokes all {@link TeardownService} callbacks, for example to save results. + * + * @param asyncCall indicates if a call is triggerd in an async process to the main process to use the correect exception handling, see {@link TeardownService#handleTeardownException(Exception, boolean, AbstractTestDataEntity)}. + */ + public static void invokeTeardownServices(AbstractTestDataEntity abstractTestDataEntity, boolean asyncCall) { + if (abstractTestDataEntity != null) { + abstractTestDataEntity.refreshState(); + BeanLoader.loadMultipleBeans(TeardownService.class).values().stream() + .sorted(new PrioritizedServiceComparator<>()) + .forEachOrdered(s -> s.tearDown(Optional.of(abstractTestDataEntity), asyncCall)); + } + } + } diff --git a/src/core/src/main/java/org/sakuli/services/common/CacheHandlingResultServiceImpl.java b/src/core/src/main/java/org/sakuli/services/common/CacheHandlingServiceImpl.java similarity index 73% rename from src/core/src/main/java/org/sakuli/services/common/CacheHandlingResultServiceImpl.java rename to src/core/src/main/java/org/sakuli/services/common/CacheHandlingServiceImpl.java index 161b8bbc..ada314ed 100644 --- a/src/core/src/main/java/org/sakuli/services/common/CacheHandlingResultServiceImpl.java +++ b/src/core/src/main/java/org/sakuli/services/common/CacheHandlingServiceImpl.java @@ -18,11 +18,15 @@ package org.sakuli.services.common; +import org.checkerframework.checker.nullness.qual.NonNull; import org.sakuli.datamodel.TestCase; import org.sakuli.datamodel.TestCaseStep; +import org.sakuli.datamodel.TestSuite; import org.sakuli.datamodel.helper.TestCaseStepHelper; import org.sakuli.datamodel.state.TestSuiteState; import org.sakuli.exceptions.SakuliRuntimeException; +import org.sakuli.services.TeardownService; +import org.sakuli.services.forwarder.AbstractTeardownService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -40,8 +44,9 @@ * @author tschneck */ @Component -public class CacheHandlingResultServiceImpl extends AbstractResultService { - private static Logger logger = LoggerFactory.getLogger(CacheHandlingResultServiceImpl.class); +public class CacheHandlingServiceImpl extends AbstractTeardownService implements TeardownService { + + private static Logger LOGGER = LoggerFactory.getLogger(CacheHandlingServiceImpl.class); @Override public int getServicePriority() { @@ -49,33 +54,32 @@ public int getServicePriority() { } @Override - public void saveAllResults() { + public void teardownTestSuite(@NonNull TestSuite testSuite) throws RuntimeException { if (testSuite.getState() != null && testSuite.getState().isFinishedWithoutErrors()) { - removeCachedInitSteps(); - writeCachedStepDefinitions(); + removeCachedInitSteps(testSuite); + writeCachedStepDefinitions(testSuite); } } - protected void removeCachedInitSteps() { + protected void removeCachedInitSteps(TestSuite testSuite) { for (TestCase tc : testSuite.getTestCases().values()) { List filteredSteps = new ArrayList<>(); for (TestCaseStep step : tc.getSteps()) { if (step.getState() != null && step.getState().isFinishedWithoutErrors()) { filteredSteps.add(step); } else { - logger.debug("remove cached and not called step '{}'", step.getId()); + LOGGER.debug("remove cached and not called step '{}'", step.getId()); } } tc.setSteps(filteredSteps); } } - protected void writeCachedStepDefinitions() { + protected void writeCachedStepDefinitions(TestSuite testSuite) { try { TestCaseStepHelper.writeCachedStepDefinitions(testSuite); } catch (IOException e) { - exceptionHandler.handleException( - new SakuliRuntimeException("Can't create cache file(s) for test case steps!", e), true); + throw new SakuliRuntimeException("Can't create cache file(s) for test case steps!", e); } } } diff --git a/src/core/src/main/java/org/sakuli/services/common/CommonCleanUpServiceImpl.java b/src/core/src/main/java/org/sakuli/services/common/CommonCleanUpServiceImpl.java new file mode 100644 index 00000000..251ebdd5 --- /dev/null +++ b/src/core/src/main/java/org/sakuli/services/common/CommonCleanUpServiceImpl.java @@ -0,0 +1,54 @@ +/* + * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. + * + * Copyright 2013 - 2015 the original author or authors. + * + * 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 org.sakuli.services.common; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.sakuli.datamodel.TestSuite; +import org.sakuli.services.ResultService; +import org.sakuli.services.forwarder.AbstractTeardownService; +import org.sakuli.utils.CleanUpHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +/** + * The common result service will be called every time after the test suite execution. + * Currently the result will be only logged. + * + * @author tschneck + */ +@Component +public class CommonCleanUpServiceImpl extends AbstractTeardownService implements ResultService { + private static Logger LOGGER = LoggerFactory.getLogger(CommonCleanUpServiceImpl.class); + + @Override + public int getServicePriority() { + return 5; + } + + @Override + public void teardownTestSuite(@NonNull TestSuite testSuite) throws RuntimeException { + try { + CleanUpHelper.cleanClipboard(); + CleanUpHelper.releaseAllModifiers(); + } catch (Exception e) { + LOGGER.warn("Some unexpected errors during the clean up procedure:", e); + } + } +} diff --git a/src/core/src/main/java/org/sakuli/services/common/CommonResultServiceImpl.java b/src/core/src/main/java/org/sakuli/services/common/CommonResultServiceImpl.java index 8cf9ede1..2239f88b 100644 --- a/src/core/src/main/java/org/sakuli/services/common/CommonResultServiceImpl.java +++ b/src/core/src/main/java/org/sakuli/services/common/CommonResultServiceImpl.java @@ -18,8 +18,11 @@ package org.sakuli.services.common; +import org.checkerframework.checker.nullness.qual.NonNull; import org.sakuli.datamodel.TestCase; -import org.sakuli.utils.CleanUpHelper; +import org.sakuli.datamodel.TestSuite; +import org.sakuli.services.ResultService; +import org.sakuli.services.forwarder.AbstractTeardownService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -33,7 +36,7 @@ * @author tschneck */ @Component -public class CommonResultServiceImpl extends AbstractResultService { +public class CommonResultServiceImpl extends AbstractTeardownService implements ResultService { private static Logger LOGGER = LoggerFactory.getLogger(CommonResultServiceImpl.class); @Override @@ -42,20 +45,19 @@ public int getServicePriority() { } @Override - public void saveAllResults() { - cleanUp(); + public void teardownTestSuite(@NonNull TestSuite testSuite) throws RuntimeException { LOGGER.info(testSuite.getResultString() + "\n=========== SAKULI Testsuite \"" + testSuite.getId() + "\" execution FINISHED - " + testSuite.getState() + " ======================\n"); switch (testSuite.getState()) { case WARNING_IN_CASE: - logTestCaseStateDetailInfo(tc -> tc.getState().isWarning()); + logTestCaseStateDetailInfo(testSuite, tc -> tc.getState().isWarning()); break; case WARNING_IN_STEP: - logTestCaseStateDetailInfo(tc -> tc.getState().isWarningInStep()); + logTestCaseStateDetailInfo(testSuite, tc -> tc.getState().isWarningInStep()); break; case CRITICAL_IN_CASE: - logTestCaseStateDetailInfo(tc -> tc.getState().isCritical()); + logTestCaseStateDetailInfo(testSuite, tc -> tc.getState().isCritical()); break; case ERRORS: String errorMsg = "ERROR:\n" + testSuite.getExceptionMessages(false); @@ -64,7 +66,7 @@ public void saveAllResults() { } } - private void logTestCaseStateDetailInfo(Predicate predicate) { + private void logTestCaseStateDetailInfo(TestSuite testSuite, Predicate predicate) { testSuite.getTestCases().values().stream() .filter(predicate) .forEach(tc -> { @@ -77,13 +79,4 @@ private void logTestCaseStateDetailInfo(Predicate predicate) { } }); } - - public void cleanUp() { - try { - CleanUpHelper.cleanClipboard(); - CleanUpHelper.releaseAllModifiers(); - } catch (Throwable e) { - LOGGER.warn("Some unexpected errors during the clean up procedure:", e); - } - } } diff --git a/src/core/src/main/java/org/sakuli/services/common/LogCleanUpResultServiceImpl.java b/src/core/src/main/java/org/sakuli/services/common/LogCleanUpServiceImpl.java similarity index 83% rename from src/core/src/main/java/org/sakuli/services/common/LogCleanUpResultServiceImpl.java rename to src/core/src/main/java/org/sakuli/services/common/LogCleanUpServiceImpl.java index 63ab234f..0b1f4f85 100644 --- a/src/core/src/main/java/org/sakuli/services/common/LogCleanUpResultServiceImpl.java +++ b/src/core/src/main/java/org/sakuli/services/common/LogCleanUpServiceImpl.java @@ -18,8 +18,11 @@ package org.sakuli.services.common; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.sakuli.datamodel.TestSuite; import org.sakuli.datamodel.properties.SakuliProperties; import org.sakuli.services.TeardownService; +import org.sakuli.services.forwarder.AbstractTeardownService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -33,12 +36,12 @@ /** * @author tschneck - * Date: 2/12/16 + * Date: 2/12/16 */ @Component -public class LogCleanUpResultServiceImpl implements TeardownService { +public class LogCleanUpServiceImpl extends AbstractTeardownService implements TeardownService { - private static final Logger LOGGER = LoggerFactory.getLogger(LogCleanUpResultServiceImpl.class); + private static final Logger LOGGER = LoggerFactory.getLogger(LogCleanUpServiceImpl.class); @Autowired private SakuliProperties sakuliProperties; @@ -49,7 +52,7 @@ public int getServicePriority() { } @Override - public void triggerAction() { + public void teardownTestSuite(@NonNull TestSuite testSuite) throws RuntimeException { if (Files.exists(sakuliProperties.getLogFolder())) { cleanUpDirectory(sakuliProperties.getLogFolder()); } @@ -74,12 +77,12 @@ void cleanUpDirectory(Path path) { Files.deleteIfExists(e); } } catch (IOException e1) { - LOGGER.error("can`t delete file", e1); + LOGGER.error("can't delete file", e1); } } }); } catch (IOException e) { - LOGGER.error("couldn`t access log file directory '" + path + "'", e); + LOGGER.error("couldn't access log file directory '" + path + "'", e); } } } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/AbstractOutputBuilder.java b/src/core/src/main/java/org/sakuli/services/forwarder/AbstractOutputBuilder.java index 04d95972..6cfbd997 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/AbstractOutputBuilder.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/AbstractOutputBuilder.java @@ -47,8 +47,6 @@ public abstract class AbstractOutputBuilder { @Autowired protected ScreenshotDivConverter screenshotDivConverter; - @Autowired - protected TestSuite testSuite; public static String replacePlaceHolder(String message, PlaceholderMap placeholderStringMap) { if (StringUtils.isBlank(message)) { diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/AbstractTeardownService.java b/src/core/src/main/java/org/sakuli/services/forwarder/AbstractTeardownService.java new file mode 100644 index 00000000..1289bf9d --- /dev/null +++ b/src/core/src/main/java/org/sakuli/services/forwarder/AbstractTeardownService.java @@ -0,0 +1,73 @@ +/* + * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. + * + * Copyright 2013 - 2018 the original author or authors. + * + * 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 org.sakuli.services.forwarder; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.sakuli.datamodel.AbstractTestDataEntity; +import org.sakuli.datamodel.TestCase; +import org.sakuli.datamodel.TestCaseStep; +import org.sakuli.exceptions.SakuliCheckedException; +import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliExceptionHandler; +import org.sakuli.services.TeardownService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * Abstract root class for {@link org.sakuli.services.TeardownService}s + * Contains implementations with states. + */ +public abstract class AbstractTeardownService implements TeardownService { + + private Logger logger = LoggerFactory.getLogger(this.getClass()); + @Autowired + private SakuliExceptionHandler exceptionHandler; + + @Override + public void handleTeardownException(@NonNull Exception e, boolean async, @NonNull AbstractTestDataEntity testDataRef) { + if (async) { + exceptionHandler.handleException(addTestRef(e, testDataRef)); + } else { + exceptionHandler.handleException(e); + } + } + + Exception addTestRef(@NonNull Exception e, @NonNull AbstractTestDataEntity testDataRef) { + if (!(e instanceof SakuliException)) { + e = new SakuliCheckedException(e); + } + ((SakuliException) e).setAsyncTestDataRef(testDataRef); + return e; + } + + @Override + public void teardownTestCase(@NonNull TestCase testCase) throws RuntimeException { + logTeardownNotNeeded(testCase); + } + + @Override + public void teardownTestCaseStep(@NonNull TestCaseStep testCaseStep) throws RuntimeException { + logTeardownNotNeeded(testCaseStep); + } + + private void logTeardownNotNeeded(AbstractTestDataEntity data) { + logger.debug("teardown of {} [{}] not needed.", this.getClass().getSimpleName(), data.toString()); + } +} diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/AbstractTemplateOutputBuilder.java b/src/core/src/main/java/org/sakuli/services/forwarder/AbstractTemplateOutputBuilder.java index 49504499..f1a184f7 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/AbstractTemplateOutputBuilder.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/AbstractTemplateOutputBuilder.java @@ -24,9 +24,10 @@ import org.jtwig.environment.EnvironmentConfigurationBuilder; import org.jtwig.spaceless.SpacelessExtension; import org.jtwig.spaceless.configuration.SpacelessConfiguration; +import org.sakuli.datamodel.AbstractTestDataEntity; import org.sakuli.datamodel.properties.SakuliProperties; import org.sakuli.exceptions.SakuliExceptionHandler; -import org.sakuli.exceptions.SakuliForwarderException; +import org.sakuli.exceptions.SakuliForwarderCheckedException; import org.sakuli.services.forwarder.configuration.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,6 +38,10 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Map; + +import static org.sakuli.services.forwarder.configuration.TemplateModelEntityName.SAKULI_PROPERTIES; +import static org.sakuli.services.forwarder.configuration.TemplateModelEntityName.TEST_DATA_ENTITY; /** * @author Georgi Todorov @@ -59,13 +64,21 @@ public abstract class AbstractTemplateOutputBuilder extends AbstractOutputBuilde public abstract String getConverterName(); /** - * Converts the current test suite to a string based on the template for the concrete converter. + * Returns a map of specific model objects based on the concrete template output builder. * - * @return + * @return map with additional model objects */ - public String createOutput() throws SakuliForwarderException { + public abstract Map getSpecificModelEntities(); + + /** + * Converts the current test data entity to a string based on the template for the concrete converter. + * + * @param abstractTestDataEntity Test data entity, which has to be converted + * @return A string representation of the provided test data entity + */ + public String createOutput(AbstractTestDataEntity abstractTestDataEntity) throws SakuliForwarderCheckedException { try { - JtwigModel model = createModel(); + JtwigModel model = createModel(abstractTestDataEntity); EnvironmentConfiguration configuration = EnvironmentConfigurationBuilder.configuration() .extensions() .add(new SpacelessExtension(new SpacelessConfiguration(new LeadingWhitespaceRemover()))) @@ -77,13 +90,14 @@ public String createOutput() throws SakuliForwarderException { .add(new ExtractScreenshotFunction(screenshotDivConverter)) .add(new AbbreviateFunction()) .add(new UnixTimestampConverterFunction()) + .add(new GetTestDataEntityTypeFunction()) .and() .build(); JtwigTemplate template = JtwigTemplate.fileTemplate(getTemplatePath().toFile(), configuration); logger.debug(String.format("Render model into JTwig template. Model: '%s'", model)); return template.render(model); - } catch (Throwable thr) { - throw new SakuliForwarderException(thr, "Exception during rendering of Twig template occurred!"); + } catch (Exception e) { + throw new SakuliForwarderCheckedException(e, "Exception during rendering of Twig template occurred!"); } } @@ -93,10 +107,16 @@ public String createOutput() throws SakuliForwarderException { * * @return */ - protected JtwigModel createModel() { - return JtwigModel.newModel() - .with("testsuite", testSuite) - .with("sakuli", sakuliProperties); + protected JtwigModel createModel(AbstractTestDataEntity abstractTestDataEntity) { + JtwigModel model = JtwigModel.newModel() + .with(TEST_DATA_ENTITY.getName(), abstractTestDataEntity) + .with(SAKULI_PROPERTIES.getName(), sakuliProperties); + if (getSpecificModelEntities() != null) { + getSpecificModelEntities().forEach((templateModelEntityName, object) -> { + model.with(templateModelEntityName.getName(), object); + }); + } + return model; } protected Path getTemplatePath() throws FileNotFoundException { diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/ScreenshotDivConverter.java b/src/core/src/main/java/org/sakuli/services/forwarder/ScreenshotDivConverter.java index d93ceb36..43d5457d 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/ScreenshotDivConverter.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/ScreenshotDivConverter.java @@ -21,7 +21,7 @@ import org.apache.commons.lang.StringUtils; import org.sakuli.exceptions.SakuliExceptionHandler; import org.sakuli.exceptions.SakuliExceptionWithScreenshot; -import org.sakuli.exceptions.SakuliForwarderException; +import org.sakuli.exceptions.SakuliForwarderCheckedException; import org.sakuli.services.forwarder.checkmk.ProfileCheckMK; import org.sakuli.services.forwarder.gearman.ProfileGearman; import org.sakuli.services.forwarder.icinga2.ProfileIcinga2; @@ -57,7 +57,7 @@ public static String removeBase64ImageDataString(String string) { return string; } - public ScreenshotDiv convert(Throwable e) { + public ScreenshotDiv convert(Exception e) { if (e != null) { String base64String = extractScreenshotAsBase64(e); String format = extractScreenshotFormat(e); @@ -72,7 +72,7 @@ public ScreenshotDiv convert(Throwable e) { return null; } - protected String extractScreenshotAsBase64(Throwable exception) { + protected String extractScreenshotAsBase64(Exception exception) { if (exception instanceof SakuliExceptionWithScreenshot) { Path screenshotPath = ((SakuliExceptionWithScreenshot) exception).getScreenshot(); if (screenshotPath != null) { @@ -84,7 +84,7 @@ protected String extractScreenshotAsBase64(Throwable exception) { } return base64String; } catch (IOException e) { - exceptionHandler.handleException(new SakuliForwarderException(e, + exceptionHandler.handleException(new SakuliForwarderCheckedException(e, String.format("error during the BASE64 encoding of the screenshot '%s'", screenshotPath.toString()))); } } @@ -92,7 +92,7 @@ protected String extractScreenshotAsBase64(Throwable exception) { return null; } - protected String extractScreenshotFormat(Throwable exception) { + protected String extractScreenshotFormat(Exception exception) { if (exception instanceof SakuliExceptionWithScreenshot) { Path screenshotPath = ((SakuliExceptionWithScreenshot) exception).getScreenshot(); if (screenshotPath != null) { diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/checkmk/CheckMKResultServiceImpl.java b/src/core/src/main/java/org/sakuli/services/forwarder/checkmk/CheckMKResultServiceImpl.java index 57d143fd..4c493065 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/checkmk/CheckMKResultServiceImpl.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/checkmk/CheckMKResultServiceImpl.java @@ -18,8 +18,10 @@ package org.sakuli.services.forwarder.checkmk; -import org.sakuli.exceptions.SakuliForwarderException; -import org.sakuli.services.common.AbstractResultService; +import org.sakuli.datamodel.AbstractTestDataEntity; +import org.sakuli.exceptions.SakuliForwarderCheckedException; +import org.sakuli.services.ResultService; +import org.sakuli.services.forwarder.AbstractTeardownService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -31,6 +33,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; +import java.util.Optional; import static org.apache.commons.lang3.StringUtils.isEmpty; @@ -39,13 +42,12 @@ */ @ProfileCheckMK @Component -public class CheckMKResultServiceImpl extends AbstractResultService { +public class CheckMKResultServiceImpl extends AbstractTeardownService implements ResultService { - private static final Logger logger = LoggerFactory.getLogger(CheckMKResultServiceImpl.class); + private static final Logger LOGGER = LoggerFactory.getLogger(CheckMKResultServiceImpl.class); @Autowired private CheckMKProperties checkMKProperties; - @Autowired private CheckMKTemplateOutputBuilder outputBuilder; @@ -55,38 +57,40 @@ public int getServicePriority() { } @Override - public void saveAllResults() { - try { - logger.info("======= WRITE FILE FOR CHECK_MK ======"); - String output = outputBuilder.createOutput(); - logger.debug(String.format("Output for check_mk:\n%s", output)); - writeToFile(createSpoolFilePath(), output); - logger.info("======= FINISHED: WRITE FILE FOR CHECK_MK ======"); - } catch (SakuliForwarderException e) { - exceptionHandler.handleException(e, false); - } + public void tearDown(Optional dataEntity, boolean asyncCall) { + dataEntity.ifPresent(data -> { + try { + LOGGER.info("======= WRITE FILE FOR CHECK_MK ======"); + String output = outputBuilder.createOutput(data); + LOGGER.debug(String.format("Output for check_mk:\n%s", output)); + writeToFile(createSpoolFilePath(data.getId()), output); + LOGGER.info("======= FINISHED: WRITE FILE FOR CHECK_MK ======"); + } catch (Exception e) { + handleTeardownException(e, asyncCall, data); + } + }); } - protected Path createSpoolFilePath() { + protected Path createSpoolFilePath(String dataId) { String spoolDir = checkMKProperties.getSpoolDir(); String fileName = new StringBuilder() .append(checkMKProperties.getFreshness()) .append(isEmpty(checkMKProperties.getSpoolFilePrefix()) - ? "" - : "_" + checkMKProperties.getSpoolFilePrefix() + ? "" + : "_" + checkMKProperties.getSpoolFilePrefix() ) .append("_") - .append(testSuite.getId()) + .append(dataId) .toString(); return Paths.get(spoolDir + File.separator + fileName); } - private void writeToFile(Path file, String output) throws SakuliForwarderException { + private void writeToFile(Path file, String output) throws SakuliForwarderCheckedException { try { - logger.info(String.format("Write file to '%s'", file)); + LOGGER.info(String.format("Write file to '%s'", file)); Files.write(file, output.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); } catch (IOException e) { - throw new SakuliForwarderException(e, + throw new SakuliForwarderCheckedException(e, String.format("Unexpected error by writing the output for check_mk to the following file '%s'", file)); } } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/checkmk/CheckMKTemplateOutputBuilder.java b/src/core/src/main/java/org/sakuli/services/forwarder/checkmk/CheckMKTemplateOutputBuilder.java index 194c880f..0eea39cf 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/checkmk/CheckMKTemplateOutputBuilder.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/checkmk/CheckMKTemplateOutputBuilder.java @@ -18,11 +18,15 @@ package org.sakuli.services.forwarder.checkmk; -import org.jtwig.JtwigModel; import org.sakuli.services.forwarder.AbstractTemplateOutputBuilder; +import org.sakuli.services.forwarder.configuration.TemplateModelEntityName; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + /** * @author Georgi Todorov */ @@ -39,9 +43,12 @@ public String getConverterName() { } @Override - public JtwigModel createModel() { - return super.createModel() - .with("checkmk", checkMKProperties); + public Map getSpecificModelEntities() { + return Collections.unmodifiableMap(new HashMap() { + { + put(TemplateModelEntityName.CHECK_MK_PROPERTIES, checkMKProperties); + } + }); } } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/configuration/GetTestDataEntityTypeFunction.java b/src/core/src/main/java/org/sakuli/services/forwarder/configuration/GetTestDataEntityTypeFunction.java new file mode 100644 index 00000000..bb746221 --- /dev/null +++ b/src/core/src/main/java/org/sakuli/services/forwarder/configuration/GetTestDataEntityTypeFunction.java @@ -0,0 +1,57 @@ +/* + * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. + * + * Copyright 2013 - 2018 the original author or authors. + * + * 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 org.sakuli.services.forwarder.configuration; + +import org.sakuli.datamodel.AbstractTestDataEntity; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +/** + * Custom JtwigFunction for retrieving the type of a provided AbstractTestDataEntity. + * + * @author Georgi Todorov + */ +public class GetTestDataEntityTypeFunction extends AbstractFunction { + + @Override + public String name() { + return "getTestDataEntityType"; + } + + @Override + protected int getExpectedNumberOfArguments() { + return 1; + } + + @Override + protected List getExpectedArgumentTypes() { + return Collections.singletonList(AbstractTestDataEntity.class); + } + + @Override + protected Object execute(List arguments) { + return Optional.ofNullable((AbstractTestDataEntity) arguments.get(0)) + .map(o -> o.getClass().getSimpleName()) + .orElse(null); + } + +} diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/configuration/TemplateModelEntityName.java b/src/core/src/main/java/org/sakuli/services/forwarder/configuration/TemplateModelEntityName.java new file mode 100644 index 00000000..cd701d94 --- /dev/null +++ b/src/core/src/main/java/org/sakuli/services/forwarder/configuration/TemplateModelEntityName.java @@ -0,0 +1,39 @@ +/* + * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. + * + * Copyright 2013 - 2018 the original author or authors. + * + * 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 org.sakuli.services.forwarder.configuration; + +public enum TemplateModelEntityName { + + TEST_DATA_ENTITY("testDataEntity"), + TEST_SUITE_ID("testSuiteId"), + TEST_CASE_ID("testCaseId"), + SAKULI_PROPERTIES("sakuli"), + CHECK_MK_PROPERTIES("checkmk"), + GEARMAN_PROPERTIES("gearman"); + + private String name; + + TemplateModelEntityName(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/database/DatabaseInitializingServiceImpl.java b/src/core/src/main/java/org/sakuli/services/forwarder/database/DatabaseInitializingServiceImpl.java index e681454d..dd56ea50 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/database/DatabaseInitializingServiceImpl.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/database/DatabaseInitializingServiceImpl.java @@ -20,7 +20,7 @@ import org.sakuli.datamodel.TestSuite; import org.sakuli.exceptions.SakuliExceptionHandler; -import org.sakuli.exceptions.SakuliForwarderException; +import org.sakuli.exceptions.SakuliForwarderCheckedException; import org.sakuli.services.InitializingService; import org.sakuli.services.common.CommonInitializingServiceImpl; import org.sakuli.services.forwarder.database.dao.DaoTestSuite; @@ -32,7 +32,7 @@ * The {@link CommonInitializingServiceImpl#initTestSuite()} will be also called. * * @author tschneck - * Date: 09.07.14 + * Date: 09.07.14 */ @ProfileJdbcDb @Component @@ -53,8 +53,8 @@ public int getServicePriority() { public void initTestSuite() { try { testSuite.setDbPrimaryKey(daoTestSuite.insertInitialTestSuiteData()); - } catch (Throwable e) { - exceptionHandler.handleException(new SakuliForwarderException(e, "error at database initialization!"), true); + } catch (Exception e) { + exceptionHandler.handleException(new SakuliForwarderCheckedException(e, "error at database initialization!"), true); } } } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/database/DatabaseResultServiceImpl.java b/src/core/src/main/java/org/sakuli/services/forwarder/database/DatabaseResultServiceImpl.java index 973e94e9..3b120c9c 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/database/DatabaseResultServiceImpl.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/database/DatabaseResultServiceImpl.java @@ -18,10 +18,14 @@ package org.sakuli.services.forwarder.database; +import org.checkerframework.checker.nullness.qual.NonNull; import org.sakuli.datamodel.TestCase; import org.sakuli.datamodel.TestCaseStep; -import org.sakuli.exceptions.SakuliForwarderException; -import org.sakuli.services.common.AbstractResultService; +import org.sakuli.datamodel.TestSuite; +import org.sakuli.exceptions.SakuliExceptionHandler; +import org.sakuli.exceptions.SakuliForwarderRuntimeException; +import org.sakuli.services.ResultService; +import org.sakuli.services.forwarder.AbstractTeardownService; import org.sakuli.services.forwarder.database.dao.DaoTestCase; import org.sakuli.services.forwarder.database.dao.DaoTestCaseStep; import org.sakuli.services.forwarder.database.dao.DaoTestSuite; @@ -35,13 +39,15 @@ /** * @author tschneck - * Date: 09.07.14 + * Date: 09.07.14 */ @ProfileJdbcDb @Component -public class DatabaseResultServiceImpl extends AbstractResultService { +public class DatabaseResultServiceImpl extends AbstractTeardownService implements ResultService { private static Logger LOGGER = LoggerFactory.getLogger(DatabaseResultServiceImpl.class); + @Autowired + private SakuliExceptionHandler exceptionHandler; @Autowired private DaoTestCase daoTestCase; @Autowired @@ -55,9 +61,8 @@ public int getServicePriority() { } @Override - public void saveAllResults() { + public void teardownTestSuite(@NonNull TestSuite testSuite) throws RuntimeException { LOGGER.info("======= SAVE RESULTS TO DATABASE ======"); - ; try { daoTestSuite.saveTestSuiteResult(); daoTestSuite.saveTestSuiteToSahiJobs(); @@ -78,11 +83,10 @@ public void saveAllResults() { } } LOGGER.info("======= FINISHED: SAVE RESULTS TO DATABASE ======"); - } catch (Throwable e) { - exceptionHandler.handleException( - new SakuliForwarderException(e, - String.format("error by saving the results to the database [%s]", testSuite.toString())), - true); + } catch (Exception e) { + throw new SakuliForwarderRuntimeException( + "error by saving the results to the database for: " + testSuite.toString(), e); } } + } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/Dao.java b/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/Dao.java index e4eb8077..87886afb 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/Dao.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/Dao.java @@ -19,7 +19,6 @@ package org.sakuli.services.forwarder.database.dao.impl; import org.sakuli.datamodel.TestSuite; -import org.sakuli.exceptions.SakuliException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -33,7 +32,7 @@ /** * @author tschneck - * Date: 12.07.13 + * Date: 12.07.13 */ public abstract class Dao extends NamedParameterJdbcDaoSupport { @@ -44,14 +43,14 @@ public abstract class Dao extends NamedParameterJdbcDaoSupport { @Autowired protected LobHandler lobHandler; - public Dao(DataSource dataSource) throws SakuliException { + public Dao(DataSource dataSource) { try { if (dataSource == null) { throw new ConnectException("Cannot get a connection to the Database!"); } setDataSource(dataSource); - } catch (Throwable e) { + } catch (Exception e) { LOGGER.debug("Suppressed Exception for missing DB connection: ", e); throw new RuntimeException("Database is not reachable, please check your 'db.properties' !!!"); } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/DaoTestCaseImpl.java b/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/DaoTestCaseImpl.java index 38a2b03e..467805a9 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/DaoTestCaseImpl.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/DaoTestCaseImpl.java @@ -20,7 +20,7 @@ import org.sakuli.datamodel.TestCase; import org.sakuli.datamodel.TestCaseStep; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.services.forwarder.database.ProfileJdbcDb; import org.sakuli.services.forwarder.database.dao.DaoTestCase; import org.springframework.beans.factory.annotation.Autowired; @@ -51,7 +51,7 @@ public class DaoTestCaseImpl extends Dao implements DaoTestCase { @Autowired - public DaoTestCaseImpl(DataSource dataSource) throws SakuliException { + public DaoTestCaseImpl(DataSource dataSource) throws SakuliCheckedException { super(dataSource); } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/DaoTestCaseStepImpl.java b/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/DaoTestCaseStepImpl.java index ca463858..be4b2607 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/DaoTestCaseStepImpl.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/DaoTestCaseStepImpl.java @@ -19,7 +19,7 @@ package org.sakuli.services.forwarder.database.dao.impl; import org.sakuli.datamodel.TestCaseStep; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.services.forwarder.database.ProfileJdbcDb; import org.sakuli.services.forwarder.database.dao.DaoTestCaseStep; import org.springframework.beans.factory.annotation.Autowired; @@ -39,7 +39,7 @@ public class DaoTestCaseStepImpl extends Dao implements DaoTestCaseStep { @Autowired - public DaoTestCaseStepImpl(DataSource dataSource) throws SakuliException { + public DaoTestCaseStepImpl(DataSource dataSource) throws SakuliCheckedException { super(dataSource); } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/DaoTestSuiteImpl.java b/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/DaoTestSuiteImpl.java index a839a970..22aae213 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/DaoTestSuiteImpl.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/database/dao/impl/DaoTestSuiteImpl.java @@ -18,7 +18,7 @@ package org.sakuli.services.forwarder.database.dao.impl; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.services.forwarder.database.ProfileJdbcDb; import org.sakuli.services.forwarder.database.dao.DaoTestSuite; import org.springframework.beans.factory.annotation.Autowired; @@ -42,7 +42,7 @@ public class DaoTestSuiteImpl extends Dao implements DaoTestSuite { @Autowired - public DaoTestSuiteImpl(DataSource dataSource) throws SakuliException { + public DaoTestSuiteImpl(DataSource dataSource) throws SakuliCheckedException { super(dataSource); } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/gearman/GearmanCacheService.java b/src/core/src/main/java/org/sakuli/services/forwarder/gearman/GearmanCacheService.java index 6927e230..615bb60a 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/gearman/GearmanCacheService.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/gearman/GearmanCacheService.java @@ -21,8 +21,7 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.sakuli.datamodel.properties.TestSuiteProperties; -import org.sakuli.exceptions.SakuliExceptionHandler; -import org.sakuli.exceptions.SakuliForwarderException; +import org.sakuli.exceptions.SakuliForwarderCheckedException; import org.sakuli.services.forwarder.gearman.model.NagiosCheckResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -47,17 +46,16 @@ public class GearmanCacheService { private static final String CACHE_SEPARATOR = "======="; private static final String CHARSET_NAME = "UTF-8"; private static final String LINE_SEPARATOR = "\n"; - @Autowired - private SakuliExceptionHandler exceptionHandler; @Autowired private TestSuiteProperties testSuiteProperties; /** * Reads cached results from Gearman cache file. + * * @return */ - public List getCachedResults() { + public List getCachedResults() throws SakuliForwarderCheckedException { List results = new ArrayList<>(); Path cacheFile = testSuiteProperties.getTestSuiteFolder().resolve(CACHE_FILE); @@ -79,7 +77,7 @@ public List getCachedResults() { } } } catch (IOException e) { - exceptionHandler.handleException(new SakuliForwarderException(e, String.format("Failed to read Gearman cache file '%s'", cacheFile)), true); + throw new SakuliForwarderCheckedException(e, String.format("Failed to read Gearman cache file '%s'", cacheFile)); } } @@ -88,9 +86,10 @@ public List getCachedResults() { /** * Writes results to Gearman cache file. + * * @param results */ - public void cacheResults(List results) { + public void cacheResults(List results) throws SakuliForwarderCheckedException { Path cacheFile = testSuiteProperties.getTestSuiteFolder().resolve(CACHE_FILE); File output = new File(cacheFile.toUri()); if (!output.getParentFile().exists()) { @@ -110,7 +109,7 @@ public void cacheResults(List results) { fos.flush(); } catch (IOException e) { - exceptionHandler.handleException(new SakuliForwarderException(e, "Failed to write Gearman cache file"), true); + throw new SakuliForwarderCheckedException(e, "Failed to write Gearman cache file"); } } } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/gearman/GearmanResultServiceImpl.java b/src/core/src/main/java/org/sakuli/services/forwarder/gearman/GearmanResultServiceImpl.java index 253b16c8..5edf1123 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/gearman/GearmanResultServiceImpl.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/gearman/GearmanResultServiceImpl.java @@ -22,8 +22,12 @@ import org.gearman.client.*; import org.gearman.common.GearmanJobServerConnection; import org.gearman.common.GearmanNIOJobServerConnection; +import org.sakuli.datamodel.AbstractTestDataEntity; +import org.sakuli.exceptions.SakuliForwarderCheckedException; import org.sakuli.exceptions.SakuliForwarderException; -import org.sakuli.services.common.AbstractResultService; +import org.sakuli.exceptions.SakuliForwarderRuntimeException; +import org.sakuli.services.ResultService; +import org.sakuli.services.forwarder.AbstractTeardownService; import org.sakuli.services.forwarder.ScreenshotDivConverter; import org.sakuli.services.forwarder.gearman.crypt.Aes; import org.sakuli.services.forwarder.gearman.model.NagiosCheckResult; @@ -37,6 +41,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.concurrent.Future; import java.util.stream.Collectors; @@ -44,11 +49,11 @@ /** * @author tschneck - * Date: 23.05.14 + * Date: 23.05.14 */ @ProfileGearman @Component -public class GearmanResultServiceImpl extends AbstractResultService { +public class GearmanResultServiceImpl extends AbstractTeardownService implements ResultService { private static final Logger logger = LoggerFactory.getLogger(GearmanResultServiceImpl.class); @Autowired private GearmanProperties properties; @@ -63,49 +68,59 @@ public int getServicePriority() { } @Override - public void saveAllResults() { - logger.info("======= SEND RESULTS TO GEARMAN SERVER ======"); - GearmanClient gearmanClient = getGearmanClient(); - GearmanJobServerConnection connection = getGearmanConnection(properties.getServerHost(), properties.getServerPort()); + public void tearDown(Optional dataEntity, boolean asyncCall) { + dataEntity.ifPresent(data -> { + logger.info("======= SEND RESULTS TO GEARMAN SERVER ======"); + GearmanClient gearmanClient = getGearmanClient(); + GearmanJobServerConnection connection = getGearmanConnection(properties.getServerHost(), properties.getServerPort()); + + List results = new ArrayList<>(); + try { + results.add(nagiosCheckResultBuilder.build(data)); + + if (properties.isCacheEnabled()) { + results.addAll(cacheService.getCachedResults()); + if (results.size() > 1) { + logger.info(String.format("Processing %s cached results first", results.size() - 1)); + } + } - List results = new ArrayList<>(); - try { - results.add(nagiosCheckResultBuilder.build()); + if (!gearmanClient.addJobServer(connection)) { + throw new SakuliForwarderCheckedException( + String.format("Failed to connect to Gearman server '%s:%s'", properties.getServerHost(), properties.getServerPort())); + } else { + //sending in reverse original happened order + Collections.reverse(results); + results = results.stream() + //filter all unsuccessful results + .filter(checkResult -> !sendResult(gearmanClient, checkResult, asyncCall, data)) + .collect(Collectors.toList()); + Collections.reverse(results); + } + } catch (Exception e) { + handleTeardownException((e instanceof SakuliForwarderException) + ? e + : new SakuliForwarderRuntimeException(String.format("Could not transfer Sakuli results to the Gearman server '%s:%s'", properties.getServerHost(), properties.getServerPort()), e), + asyncCall, + data); + } + //save all not send results if (properties.isCacheEnabled()) { - results.addAll(cacheService.getCachedResults()); - if (results.size() > 1) { - logger.info(String.format("Processing %s cached results first", results.size() - 1)); + try { + cacheService.cacheResults(results); + } catch (SakuliForwarderCheckedException e) { + handleTeardownException(e, asyncCall, data); } } - if (!gearmanClient.addJobServer(connection)) { - exceptionHandler.handleException(new SakuliForwarderException( - String.format("Failed to connect to Gearman server '%s:%s'", properties.getServerHost(), properties.getServerPort())), true); - } else { - //sending in reverse original happened order - Collections.reverse(results); - results = results.stream() - //filter all unsuccessful results - .filter(checkResult -> !sendResult(gearmanClient, checkResult)) - .collect(Collectors.toList()); - Collections.reverse(results); - } - } catch (Exception e) { - exceptionHandler.handleException(new SakuliForwarderException(e, - String.format("Could not transfer Sakuli results to the Gearman server '%s:%s'", properties.getServerHost(), properties.getServerPort())), true); - } - - //save all not send results - if (properties.isCacheEnabled()) { - cacheService.cacheResults(results); - } - - gearmanClient.shutdown(); - logger.info("======= FINISHED: SEND RESULTS TO GEARMAN SERVER ======"); + gearmanClient.shutdown(); + logger.info("======= FINISHED: SEND RESULTS TO GEARMAN SERVER ======"); + }); } - protected boolean sendResult(GearmanClient gearmanClient, NagiosCheckResult checkResult) { + + protected boolean sendResult(GearmanClient gearmanClient, NagiosCheckResult checkResult, boolean asyncCall, AbstractTestDataEntity data) { if (logger.isDebugEnabled()) { logger.debug(String.format("Sending result to Gearman server %s:%s", checkResult.getQueueName(), checkResult.getUuid())); } @@ -132,19 +147,13 @@ protected boolean sendResult(GearmanClient gearmanClient, NagiosCheckResult chec } return true; } - exceptionHandler.handleException( - NagiosExceptionBuilder.buildTransferException(properties.getServerHost(), properties.getServerPort(), result)); - - } catch (Throwable e) { - logger.error(e.getMessage()); - exceptionHandler.handleException( - NagiosExceptionBuilder.buildUnexpectedErrorException(e, properties.getServerHost(), properties.getServerPort()), - true); + handleTeardownException(NagiosExceptionBuilder.buildTransferException(properties.getServerHost(), properties.getServerPort(), result), asyncCall, data); + } catch (Exception e) { + handleTeardownException(NagiosExceptionBuilder.buildUnexpectedErrorException(e, properties.getServerHost(), properties.getServerPort()), asyncCall, data); } return false; } - /** * Logs the assigned Gearman message as follow: *
    @@ -177,7 +186,6 @@ protected GearmanJobServerConnection getGearmanConnection(String hostname, int p return new GearmanNIOJobServerConnection(hostname, port); } - protected GearmanClient getGearmanClient() { return new GearmanClientImpl(); } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/gearman/GearmanTemplateOutputBuilder.java b/src/core/src/main/java/org/sakuli/services/forwarder/gearman/GearmanTemplateOutputBuilder.java index 0b085cfc..b4e0896b 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/gearman/GearmanTemplateOutputBuilder.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/gearman/GearmanTemplateOutputBuilder.java @@ -18,11 +18,17 @@ package org.sakuli.services.forwarder.gearman; -import org.jtwig.JtwigModel; +import org.sakuli.datamodel.TestCase; +import org.sakuli.datamodel.TestSuite; +import org.sakuli.loader.BeanLoader; import org.sakuli.services.forwarder.AbstractTemplateOutputBuilder; +import org.sakuli.services.forwarder.configuration.TemplateModelEntityName; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.HashMap; +import java.util.Map; + /** * @author Georgi Todorov */ @@ -39,9 +45,26 @@ public String getConverterName() { } @Override - public JtwigModel createModel() { - return super.createModel() - .with("gearman", gearmanProperties); + public Map getSpecificModelEntities() { + Map modelEntitiesMap = new HashMap(); + modelEntitiesMap.put(TemplateModelEntityName.GEARMAN_PROPERTIES, gearmanProperties); + TestSuite testSuite = getCurrentTestSuite(); + if (testSuite != null) { + modelEntitiesMap.put(TemplateModelEntityName.TEST_SUITE_ID, testSuite.getId()); + } + TestCase currentTestCase = getCurrentTestCase(); + if (currentTestCase != null) { + modelEntitiesMap.put(TemplateModelEntityName.TEST_CASE_ID, currentTestCase.getId()); + } + return modelEntitiesMap; + } + + public TestSuite getCurrentTestSuite() { + return BeanLoader.loadBaseActionLoader().getTestSuite(); + } + + public TestCase getCurrentTestCase() { + return BeanLoader.loadBaseActionLoader().getCurrentTestCase(); } } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/gearman/model/builder/NagiosCheckResultBuilder.java b/src/core/src/main/java/org/sakuli/services/forwarder/gearman/model/builder/NagiosCheckResultBuilder.java index 978a99eb..d78cf0ae 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/gearman/model/builder/NagiosCheckResultBuilder.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/gearman/model/builder/NagiosCheckResultBuilder.java @@ -18,8 +18,8 @@ package org.sakuli.services.forwarder.gearman.model.builder; -import org.sakuli.datamodel.TestSuite; -import org.sakuli.exceptions.SakuliForwarderException; +import org.sakuli.datamodel.AbstractTestDataEntity; +import org.sakuli.exceptions.SakuliForwarderCheckedException; import org.sakuli.services.forwarder.gearman.GearmanProperties; import org.sakuli.services.forwarder.gearman.GearmanTemplateOutputBuilder; import org.sakuli.services.forwarder.gearman.ProfileGearman; @@ -39,18 +39,16 @@ public class NagiosCheckResultBuilder { private static final Logger logger = LoggerFactory.getLogger(NagiosCheckResultBuilder.class); - @Autowired - private TestSuite testSuite; @Autowired private GearmanProperties gearmanProperties; @Autowired private GearmanTemplateOutputBuilder outputBuilder; - public NagiosCheckResult build() throws SakuliForwarderException { + public NagiosCheckResult build(AbstractTestDataEntity abstractTestDataEntity) throws SakuliForwarderCheckedException { logger.info("======= CREATING OUTPUT FOR GEARMAN ======"); - String payload = outputBuilder.createOutput(); + String payload = outputBuilder.createOutput(abstractTestDataEntity); logger.info("======= FINISHED: CREATING OUTPUT FOR GEARMAN ======"); - return new NagiosCheckResult(gearmanProperties.getServerQueue(), testSuite.getGuid(), payload); + return new NagiosCheckResult(gearmanProperties.getServerQueue(), abstractTestDataEntity.getGuid(), payload); } } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/gearman/model/builder/NagiosExceptionBuilder.java b/src/core/src/main/java/org/sakuli/services/forwarder/gearman/model/builder/NagiosExceptionBuilder.java index 8e7f6e5e..6c15a4b7 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/gearman/model/builder/NagiosExceptionBuilder.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/gearman/model/builder/NagiosExceptionBuilder.java @@ -20,27 +20,27 @@ import org.apache.commons.lang.ArrayUtils; import org.gearman.client.GearmanJobResult; -import org.sakuli.exceptions.SakuliForwarderException; +import org.sakuli.exceptions.SakuliForwarderRuntimeException; import java.nio.charset.Charset; /** * @author tschneck - * Date: 11.07.14 + * Date: 11.07.14 */ public class NagiosExceptionBuilder { - public static SakuliForwarderException buildUnexpectedErrorException(Throwable e, String host, int port) { - return new SakuliForwarderException(e, + public static SakuliForwarderRuntimeException buildUnexpectedErrorException(Exception e, String host, int port) { + return new SakuliForwarderRuntimeException( String.format("unexpected error by sending the results to the gearman forwarder '%s:'%s'", host, port - )); + ), e); } - public static SakuliForwarderException buildTransferException(String host, int port, GearmanJobResult result) { - return new SakuliForwarderException( + public static SakuliForwarderRuntimeException buildTransferException(String host, int port, GearmanJobResult result) { + return new SakuliForwarderRuntimeException( String.format("something went wrong during the transfer of the results to the gearman forwarder '%s:'%s':\n%s", host, port, diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/icinga2/Icinga2ResultServiceImpl.java b/src/core/src/main/java/org/sakuli/services/forwarder/icinga2/Icinga2ResultServiceImpl.java index 8f53223d..af0b1aee 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/icinga2/Icinga2ResultServiceImpl.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/icinga2/Icinga2ResultServiceImpl.java @@ -20,8 +20,11 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import org.sakuli.exceptions.SakuliForwarderException; -import org.sakuli.services.common.AbstractResultService; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.sakuli.datamodel.TestSuite; +import org.sakuli.exceptions.SakuliForwarderRuntimeException; +import org.sakuli.services.ResultService; +import org.sakuli.services.forwarder.AbstractTeardownService; import org.sakuli.services.forwarder.icinga2.model.Icinga2Request; import org.sakuli.services.forwarder.icinga2.model.Icinga2Result; import org.sakuli.services.forwarder.icinga2.model.builder.Icinga2CheckResultBuilder; @@ -36,13 +39,14 @@ /** * @author tschneck - * Date: 2/22/16 + * Date: 2/22/16 */ @ProfileIcinga2 @Component -public class Icinga2ResultServiceImpl extends AbstractResultService { +public class Icinga2ResultServiceImpl extends AbstractTeardownService implements ResultService { private static final Logger LOGGER = LoggerFactory.getLogger(Icinga2ResultServiceImpl.class); + @Autowired private Icinga2RestCient icinga2RestCient; @Autowired @@ -62,7 +66,7 @@ public int getServicePriority() { } @Override - public void saveAllResults() { + public void teardownTestSuite(@NonNull TestSuite testSuite) throws RuntimeException { LOGGER.info("======= SEND RESULTS TO ICINGA SERVER ======"); LOGGER.info("POST Sakuli results to '{}'", icinga2RestCient.getTargetCheckResult().getUri().toString()); @@ -79,12 +83,11 @@ public void saveAllResults() { LOGGER.info("ICINGA Response: {}", result.getFirstElementAsString()); LOGGER.info("======= FINISHED: SEND RESULTS TO ICINGA SERVER ======"); } else { - exceptionHandler.handleException(new SakuliForwarderException(String.format( + throw new SakuliForwarderRuntimeException(String.format( "Unexpected result of REST-POST to Incinga monitoring server (%s): %s", icinga2RestCient.getTargetCheckResult().getUri(), - result.getFirstElementAsString()))); + result.getFirstElementAsString())); } - } } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/icinga2/model/builder/Icinga2OutputBuilder.java b/src/core/src/main/java/org/sakuli/services/forwarder/icinga2/model/builder/Icinga2OutputBuilder.java index 93b22de7..e23c7962 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/icinga2/model/builder/Icinga2OutputBuilder.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/icinga2/model/builder/Icinga2OutputBuilder.java @@ -20,6 +20,7 @@ import org.sakuli.datamodel.Builder; import org.sakuli.datamodel.TestCase; +import org.sakuli.datamodel.TestSuite; import org.sakuli.services.forwarder.AbstractOutputBuilder; import org.sakuli.services.forwarder.icinga2.Icinga2Properties; import org.sakuli.services.forwarder.icinga2.ProfileIcinga2; @@ -37,6 +38,8 @@ public class Icinga2OutputBuilder extends AbstractOutputBuilder implements Build public static final String ICINGA_SEPARATOR = "\n"; @Autowired private Icinga2Properties properties; + @Autowired + protected TestSuite testSuite; @Override protected int getSummaryMaxLength() { diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/json/GsonOutputBuilder.java b/src/core/src/main/java/org/sakuli/services/forwarder/json/GsonOutputBuilder.java index 6b2ca89a..ae6746fc 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/json/GsonOutputBuilder.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/json/GsonOutputBuilder.java @@ -21,7 +21,8 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import org.joda.time.DateTime; -import org.sakuli.exceptions.SakuliForwarderException; +import org.sakuli.datamodel.TestSuite; +import org.sakuli.exceptions.SakuliForwarderCheckedException; import org.sakuli.services.forwarder.AbstractOutputBuilder; import org.sakuli.services.forwarder.json.serializer.DateSerializer; import org.sakuli.services.forwarder.json.serializer.DateTimeSerializer; @@ -40,6 +41,8 @@ @Component public class GsonOutputBuilder extends AbstractOutputBuilder { + @Autowired + protected TestSuite testSuite; @Autowired private JsonProperties jsonProperties; @@ -48,7 +51,7 @@ public class GsonOutputBuilder extends AbstractOutputBuilder { * * @return */ - public String createOutput() throws SakuliForwarderException { + public String createOutput() throws SakuliForwarderCheckedException { try { Gson gsonBuilder = new GsonBuilder() .setExclusionStrategies(new GsonExclusionStrategy()) @@ -59,8 +62,8 @@ public String createOutput() throws SakuliForwarderException { .serializeNulls() .create(); return gsonBuilder.toJson(testSuite); - } catch (Throwable thr) { - throw new SakuliForwarderException(thr, "Exception during serializing testSuite into JSON!"); + } catch (Exception e) { + throw new SakuliForwarderCheckedException(e, "Exception during serializing testSuite into JSON!"); } } diff --git a/src/core/src/main/java/org/sakuli/services/forwarder/json/JsonResultServiceImpl.java b/src/core/src/main/java/org/sakuli/services/forwarder/json/JsonResultServiceImpl.java index e16d0d05..3d366301 100644 --- a/src/core/src/main/java/org/sakuli/services/forwarder/json/JsonResultServiceImpl.java +++ b/src/core/src/main/java/org/sakuli/services/forwarder/json/JsonResultServiceImpl.java @@ -18,8 +18,12 @@ package org.sakuli.services.forwarder.json; -import org.sakuli.exceptions.SakuliForwarderException; -import org.sakuli.services.common.AbstractResultService; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.sakuli.datamodel.TestSuite; +import org.sakuli.exceptions.SakuliForwarderCheckedException; +import org.sakuli.exceptions.SakuliForwarderRuntimeException; +import org.sakuli.services.ResultService; +import org.sakuli.services.forwarder.AbstractTeardownService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -37,10 +41,10 @@ */ @ProfileJson @Component -public class JsonResultServiceImpl extends AbstractResultService { +public class JsonResultServiceImpl extends AbstractTeardownService implements ResultService { - private static final Logger logger = LoggerFactory.getLogger(JsonResultServiceImpl.class); public static final SimpleDateFormat JSON_FILE_DATE_FORMAT = new SimpleDateFormat("yyyy.MM.dd-HH-mm-ss-SSS"); + private static final Logger logger = LoggerFactory.getLogger(JsonResultServiceImpl.class); @Autowired private GsonOutputBuilder outputBuilder; @@ -54,45 +58,45 @@ public int getServicePriority() { } @Override - public void saveAllResults() { + public void teardownTestSuite(@NonNull TestSuite testSuite) throws RuntimeException { try { logger.info("======= WRITE TEST OUTPUT AS JSON FILE ======"); String output = outputBuilder.createOutput(); logger.debug(String.format("JSON Output:\n%s", output)); - writeToFile(createJsonFilePath(), output); + writeToFile(createJsonFilePath(testSuite.getId()), output); logger.info("======= FINISHED: WRITE TEST OUTPUT AS JSON FILE ======"); - } catch (SakuliForwarderException e) { - exceptionHandler.handleException(e, false); + } catch (Exception e) { + throw new SakuliForwarderRuntimeException("Couldn't create the JSON result file for: " + testSuite); } } - protected Path createJsonFilePath() throws SakuliForwarderException { + protected Path createJsonFilePath(String dataId) throws SakuliForwarderCheckedException { Path outputDir = jsonProperties.getOutputJsonDir(); createDirectoryIfNotExists(outputDir); - String fileName = testSuite.getId() + + String fileName = dataId + "_" + JSON_FILE_DATE_FORMAT.format(new Date()) + ".json"; return outputDir.resolve(fileName); } - protected void createDirectoryIfNotExists(Path outputDir) throws SakuliForwarderException { + protected void createDirectoryIfNotExists(Path outputDir) throws SakuliForwarderCheckedException { if (!Files.exists(outputDir)) { try { Files.createDirectories(outputDir); } catch (IOException e) { - throw new SakuliForwarderException(e, + throw new SakuliForwarderCheckedException(e, String.format("Unexpected error during creating the json output directory '%s'", outputDir.toString())); } } } - private void writeToFile(Path file, String output) throws SakuliForwarderException { + private void writeToFile(Path file, String output) throws SakuliForwarderCheckedException { try { logger.info(String.format("Write file to '%s'", file)); Files.write(file, output.getBytes(), StandardOpenOption.CREATE); } catch (IOException e) { - throw new SakuliForwarderException(e, + throw new SakuliForwarderCheckedException(e, String.format("Unexpected error by writing the json output to the following file '%s'", file)); } } diff --git a/src/core/src/main/java/org/sakuli/starter/SahiConnector.java b/src/core/src/main/java/org/sakuli/starter/SahiConnector.java index b6c4e6cf..6f4a2f28 100644 --- a/src/core/src/main/java/org/sakuli/starter/SahiConnector.java +++ b/src/core/src/main/java/org/sakuli/starter/SahiConnector.java @@ -24,7 +24,7 @@ import org.sakuli.datamodel.TestSuite; import org.sakuli.datamodel.properties.SahiProxyProperties; import org.sakuli.datamodel.properties.SakuliProperties; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.exceptions.SakuliExceptionHandler; import org.sakuli.exceptions.SakuliInitException; import org.sakuli.services.InitializingServiceHelper; @@ -113,7 +113,7 @@ public void startSahiTestSuite() throws SakuliInitException { if (output.equals("FAILURE")) { if (isSahiScriptTimout(testSuite.getException())) { logger.warn("Sahi-Script-Runner timeout detected, start retry!"); - SakuliException causingError = new SakuliException(testSuite.getException()); + SakuliCheckedException causingError = new SakuliCheckedException(testSuite.getException()); //reset all values InitializingServiceHelper.invokeInitializingServcies(); this.reconnect(causingError); @@ -125,7 +125,7 @@ public void startSahiTestSuite() throws SakuliInitException { //Reconnect - wait for Thread "sahiRunner" this.reconnect(e); } - } catch (Throwable e) { + } catch (Exception e) { sakuliExceptionHandler.handleException(e); } finally { logger.info("test suite finished"); @@ -198,7 +198,7 @@ protected String getTestSuiteFolderJsPath() { * @param e the thrown ConnectException or IllegalMonitorStateException * @throws InterruptedException */ - protected void reconnect(Exception e) throws InterruptedException, SakuliException { + protected void reconnect(Exception e) throws InterruptedException, SakuliCheckedException { logger.warn("Cannot connect to sahi proxy - start Proxy.main()"); if (countConnections <= sahiProxyProperties.getMaxConnectTries()) { logger.info( diff --git a/src/core/src/main/java/org/sakuli/starter/SakuliStarter.java b/src/core/src/main/java/org/sakuli/starter/SakuliStarter.java index 3ed0d6ec..3fabd37a 100644 --- a/src/core/src/main/java/org/sakuli/starter/SakuliStarter.java +++ b/src/core/src/main/java/org/sakuli/starter/SakuliStarter.java @@ -261,7 +261,7 @@ public static TestSuite runTestSuite(String testSuiteFolderPath, String sakuliHo System.exit(99); } finally { LOGGER.info("========== TEAR-DOWN SAKULI TEST SUITE '{}' ==========", result.getId()); - TeardownServiceHelper.invokeTeardownServices(); + TeardownServiceHelper.invokeTeardownServices(result, false); //finally shutdown context and return the result result = BeanLoader.loadBean(TestSuite.class); diff --git a/src/core/src/main/java/org/sakuli/starter/helper/ConnectionTester.java b/src/core/src/main/java/org/sakuli/starter/helper/ConnectionTester.java index 7fca5015..68ef88be 100644 --- a/src/core/src/main/java/org/sakuli/starter/helper/ConnectionTester.java +++ b/src/core/src/main/java/org/sakuli/starter/helper/ConnectionTester.java @@ -20,7 +20,7 @@ import org.sakuli.datamodel.TestCase; import org.sakuli.datamodel.TestSuite; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,18 +35,18 @@ public class ConnectionTester { static Logger logger = LoggerFactory.getLogger(ConnectionTester.class); - public static void checkTestCaseInitURL(TestSuite testSuite) throws SakuliException { + public static void checkTestCaseInitURL(TestSuite testSuite) throws SakuliCheckedException { if (testSuite != null) { if (testSuite.getTestCases() != null) { for (TestCase tc : testSuite.getTestCases().values()) { pingURL(tc.getStartUrl()); } } - throw new SakuliException(String.format("no test cases for test suite '%s' have been loaded! Check the configuration in the file '%s'", + throw new SakuliCheckedException(String.format("no test cases for test suite '%s' have been loaded! Check the configuration in the file '%s'", testSuite.getId(), testSuite.getAbsolutePathOfTestSuiteFile())); } - throw new SakuliException("The test suite has not be configured correctly, please check your settings!"); + throw new SakuliCheckedException("The test suite has not be configured correctly, please check your settings!"); } //TODO TS finalize diff --git a/src/core/src/main/java/org/sakuli/starter/helper/SahiProxy.java b/src/core/src/main/java/org/sakuli/starter/helper/SahiProxy.java index 85538082..22a3e78b 100644 --- a/src/core/src/main/java/org/sakuli/starter/helper/SahiProxy.java +++ b/src/core/src/main/java/org/sakuli/starter/helper/SahiProxy.java @@ -90,7 +90,7 @@ public void startProxy(boolean asyncron) throws SakuliInitException, FileNotFoun } catch (RuntimeException e) { logger.error("RUNTIME EXCEPTION"); throw new SakuliInitException(e); - } catch (Throwable e) { + } catch (Exception e) { logger.error("THROWABLE EXCEPTION"); throw new SakuliInitException(e.getMessage()); } diff --git a/src/core/src/main/java/org/sakuli/utils/CommandLineUtil.java b/src/core/src/main/java/org/sakuli/utils/CommandLineUtil.java index c3f081ec..55e9ed96 100644 --- a/src/core/src/main/java/org/sakuli/utils/CommandLineUtil.java +++ b/src/core/src/main/java/org/sakuli/utils/CommandLineUtil.java @@ -23,7 +23,7 @@ import org.apache.commons.exec.PumpStreamHandler; import org.apache.commons.lang.StringUtils; import org.sakuli.actions.environment.Environment; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import java.io.ByteArrayOutputStream; @@ -35,7 +35,7 @@ */ public class CommandLineUtil { - static public CommandLineResult runCommand(String command, boolean throwException) throws SakuliException { + static public CommandLineResult runCommand(String command, boolean throwException) throws SakuliCheckedException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream error = new ByteArrayOutputStream(); CommandLineResult result = new CommandLineResult(); @@ -47,7 +47,7 @@ static public CommandLineResult runCommand(String command, boolean throwExceptio result.setOutput(error.toString() + outputStream.toString()); } catch (Exception e) { if (throwException) { - throw new SakuliException(e, String.format("Error during execution of command '%s': %s", command, error.toString())); + throw new SakuliCheckedException(e, String.format("Error during execution of command '%s': %s", command, error.toString())); } result.setExitCode(resolveExitCode(e.getMessage())); result.setOutput(e.getMessage()); diff --git a/src/core/src/test/java/org/sakuli/services/common/CommonResultServiceMockImpl.java b/src/core/src/main/java/org/sakuli/utils/ConfigBean.java similarity index 57% rename from src/core/src/test/java/org/sakuli/services/common/CommonResultServiceMockImpl.java rename to src/core/src/main/java/org/sakuli/utils/ConfigBean.java index 6a22fcc0..0fca2503 100644 --- a/src/core/src/test/java/org/sakuli/services/common/CommonResultServiceMockImpl.java +++ b/src/core/src/main/java/org/sakuli/utils/ConfigBean.java @@ -1,7 +1,7 @@ /* * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. * - * Copyright 2013 - 2016 the original author or authors. + * Copyright 2013 - 2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,17 +16,22 @@ * limitations under the License. */ -package org.sakuli.services.common; +package org.sakuli.utils; -import org.sakuli.datamodel.properties.SakuliProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; -/** - * @author tschneck - * Date: 09.04.15 - */ -public class CommonResultServiceMockImpl extends CommonResultServiceImpl { - @Override - public void cleanUp() { - // do nothing +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +@Configuration +public class ConfigBean { + /** + * @return ExecutorService + */ + @Bean + public ExecutorService createdExecutorService() { + return Executors.newCachedThreadPool(); } + } diff --git a/src/core/src/test/java/org/sakuli/BaseTest.java b/src/core/src/test/java/org/sakuli/BaseTest.java index dbac30fa..19112e97 100644 --- a/src/core/src/test/java/org/sakuli/BaseTest.java +++ b/src/core/src/test/java/org/sakuli/BaseTest.java @@ -85,7 +85,7 @@ public void setContextProperties() { protected void initBaseActionLoader() { loaderMock = BeanLoader.loadBean(BaseActionLoaderImpl.class); - if (new MockUtil().isMock(loaderMock)) { + if (MockUtil.isMock(loaderMock)) { reset(loaderMock); when(loaderMock.getSahiReport()).thenReturn(mock(Report.class)); when(loaderMock.getSakuliProperties()).thenReturn(mock(SakuliProperties.class)); diff --git a/src/core/src/test/java/org/sakuli/actions/TestCaseActionTest.java b/src/core/src/test/java/org/sakuli/actions/TestCaseActionTest.java index 484e7b37..b2bf88f3 100644 --- a/src/core/src/test/java/org/sakuli/actions/TestCaseActionTest.java +++ b/src/core/src/test/java/org/sakuli/actions/TestCaseActionTest.java @@ -33,7 +33,6 @@ import org.sakuli.exceptions.SakuliActionException; import org.sakuli.exceptions.SakuliExceptionHandler; import org.sakuli.exceptions.SakuliValidationException; -import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -43,7 +42,7 @@ import java.nio.file.Paths; import java.util.*; -import static org.mockito.Matchers.anyString; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; import static org.testng.Assert.*; @@ -63,7 +62,7 @@ public class TestCaseActionTest extends BaseTest { private Map testCases; @BeforeMethod - public void initMocks() throws Exception { + public void initMocks() { MockitoAnnotations.initMocks(this); sample = new TestCase("testling", "1234_"); testCases = new HashMap<>(); @@ -83,7 +82,7 @@ public void initMocks() throws Exception { @DataProvider public Object[][] testGetTestCaseIdDP() { - return new Object[][] { + return new Object[][]{ {"testSuiteFolder" + File.separator + "1234_" @@ -95,12 +94,12 @@ public Object[][] testGetTestCaseIdDP() { @Test(dataProvider = "testGetTestCaseIdDP") public void testGetTestCaseId(String pathToTestCaseFile, String expectedTestCaseId) throws Exception { - Assert.assertEquals(testling.getIdFromPath(pathToTestCaseFile), expectedTestCaseId); + assertEquals(testling.getIdFromPath(pathToTestCaseFile), expectedTestCaseId); verifyTestCaseHasActions(loaderMock.getCurrentTestCase(), new Object[][]{{"TestCaseAction", "getIdFromPath"}}); } @Test - public void testSaveTestCaseResult() throws Throwable { + public void testSaveTestCaseResult() throws Exception { ArgumentCaptor argument = ArgumentCaptor.forClass(String.class); Date startDate = new Date(new Date().getTime() - 8000); @@ -114,7 +113,8 @@ public void testSaveTestCaseResult() throws Throwable { "" + startDate.getTime(), "" + stopDate.getTime(), url, - browser + browser, + false ); assertEquals(startDate, testSuiteMock.getTestCases().get(sample.getId()).getStartDate()); @@ -125,7 +125,7 @@ public void testSaveTestCaseResult() throws Throwable { } @Test - public void testAddTestCaseStep() throws Throwable { + public void testAddTestCaseStep() throws Exception { sample.setWarningTime(0); sample.setCriticalTime(0); @@ -134,7 +134,9 @@ public void testAddTestCaseStep() throws Throwable { "step for JUnit", "" + (now - 3000), "" + now, - 2 //warning + 2, + 5, + false ); TestCaseStep step = testSuiteMock.getTestCases().get(sample.getId()).getSteps().get(0); assertNotNull(step); @@ -147,7 +149,9 @@ public void testAddTestCaseStep() throws Throwable { "step2 for JUnit", "" + (now + 300), "" + (now + 4300), - 5 //no warning + 5, + 5, + false ); TestCaseStep step2 = testSuiteMock.getTestCases().get(sample.getId()).getSteps().get(1); assertNotNull(step2); @@ -158,7 +162,7 @@ public void testAddTestCaseStep() throws Throwable { } @Test - public void testAddTestCaseStepWithAlreadyInitializedStep() throws Throwable { + public void testAddTestCaseStepWithAlreadyInitializedStep() throws Exception { sample.setWarningTime(0); sample.setCriticalTime(0); TestCaseStep predefinedStep = new TestCaseStep(); @@ -177,7 +181,9 @@ public void testAddTestCaseStepWithAlreadyInitializedStep() throws Throwable { "step for JUnit", "" + (currentTime - 10000), "" + currentTime, - 9 //warning + 9, + 10, + false ); List testCaseSteps = sample.getSteps(); @@ -196,21 +202,36 @@ public void testInitTestCase() throws Exception { testling.init(tcID, 4, 5, "."); verify(loaderMock).init(anyString(), anyString()); - Assert.assertEquals(testSuiteMock.getTestCase(tcID).getWarningTime(), 4); - Assert.assertEquals(testSuiteMock.getTestCase(tcID).getCriticalTime(), 5); + assertEquals(testSuiteMock.getTestCase(tcID).getWarningTime(), 4); + assertEquals(testSuiteMock.getTestCase(tcID).getCriticalTime(), 5); verifyTestCaseHasActions(loaderMock.getCurrentTestCase(), new Object[][]{{"TestCaseAction", "init"}}); } + @Test + public void testInitTestCaseWithCustomID() throws Exception { + + String tcID = sample.getId(); + String newId = "new-id"; + testling.initWithCaseID(tcID, newId, 4, 5, "."); + + verify(loaderMock).init(eq(tcID), anyString()); + assertEquals(sample.getId(), newId); + assertEquals(sample.getWarningTime(), 4); + assertEquals(sample.getCriticalTime(), 5); + verifyTestCaseHasActions(loaderMock.getCurrentTestCase(), new Object[][]{{"TestCaseAction", "initWithCaseID"}}); + } + @DataProvider public Object[][] testInitTestCaseExceptionHandlingDP() { - return new Object[][] { - { -4, -5, true }, //negativ times - { 5, 4, true }, //warning time bigger than critical time - { 0, 0, false }, //zero times - { 4, 5, false }, //zero times - { 0, -8, true }, //negativ critical time + return new Object[][]{ + {-4, -5, true}, //negativ times + {5, 4, true}, //warning time bigger than critical time + {0, 0, false}, //zero times + {4, 5, false}, //zero times + {0, -8, true}, //negativ critical time }; } + @Test(dataProvider = "testInitTestCaseExceptionHandlingDP") public void testInitTestCaseExceptionHandling(int warningTime, int criticalTime, boolean expectedExceptionHandling) throws Exception { String tcID = sample.getId(); @@ -249,7 +270,7 @@ public void testGetTestSuiteFolderPath() throws Exception { public void testThrowExceptionNoScreenshot() throws Exception { String exMessage = "TEST"; testling.throwException(exMessage, false); - ArgumentCaptor ac = ArgumentCaptor.forClass(Throwable.class); + ArgumentCaptor ac = ArgumentCaptor.forClass(Exception.class); verify(exceptionHandlerMock).handleException(ac.capture()); assertEquals(ac.getValue().getMessage(), exMessage); assertTrue(ac.getValue() instanceof SakuliValidationException); @@ -263,7 +284,7 @@ public void testThrowExceptionNoScreenshot() throws Exception { public void testThrowExceptionWithScreenshot() throws Exception { String exMessage = "TEST"; testling.throwException(exMessage, true); - ArgumentCaptor ac = ArgumentCaptor.forClass(Throwable.class); + ArgumentCaptor ac = ArgumentCaptor.forClass(Exception.class); verify(exceptionHandlerMock).handleException(ac.capture()); assertEquals(ac.getValue().getMessage(), exMessage); assertFalse(ac.getValue() instanceof SakuliValidationException); diff --git a/src/core/src/test/java/org/sakuli/actions/environment/EnvironmentTest.java b/src/core/src/test/java/org/sakuli/actions/environment/EnvironmentTest.java index 35a166bd..2f321377 100644 --- a/src/core/src/test/java/org/sakuli/actions/environment/EnvironmentTest.java +++ b/src/core/src/test/java/org/sakuli/actions/environment/EnvironmentTest.java @@ -20,7 +20,7 @@ import org.apache.commons.lang.StringUtils; import org.sakuli.BaseTest; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.utils.CommandLineUtil; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -71,7 +71,7 @@ public void testRunCmdNoError() throws Exception { assertEquals(result.getExitCode(), 2); } - @Test(expectedExceptions = SakuliException.class) + @Test(expectedExceptions = SakuliCheckedException.class) public void testRunCmdException() throws Exception { Environment.runCommand("host?name"); } diff --git a/src/core/src/test/java/org/sakuli/actions/screenbased/ScreenBasedSettingsSikuliLogSystemOutTest.java b/src/core/src/test/java/org/sakuli/actions/screenbased/ScreenBasedSettingsSikuliLogSystemOutTest.java index 88d7cd47..2e1224f5 100644 --- a/src/core/src/test/java/org/sakuli/actions/screenbased/ScreenBasedSettingsSikuliLogSystemOutTest.java +++ b/src/core/src/test/java/org/sakuli/actions/screenbased/ScreenBasedSettingsSikuliLogSystemOutTest.java @@ -29,7 +29,7 @@ public class ScreenBasedSettingsSikuliLogSystemOutTest extends AopBaseTest { @Test - public void testDoHandleSikuliLog() throws Throwable { + public void testDoHandleSikuliLog() throws Exception { ScreenBasedSettings testling = BeanLoader.loadBean(ScreenBasedSettings.class); testling.setDefaults(); BeanLoader.loadBean(LoggerInitializer.class).initLoggerContext(); diff --git a/src/core/src/test/java/org/sakuli/actions/screenbased/ScreenshotActionsTest.java b/src/core/src/test/java/org/sakuli/actions/screenbased/ScreenshotActionsTest.java index 6b6af1b8..94bd17b7 100644 --- a/src/core/src/test/java/org/sakuli/actions/screenbased/ScreenshotActionsTest.java +++ b/src/core/src/test/java/org/sakuli/actions/screenbased/ScreenshotActionsTest.java @@ -42,7 +42,7 @@ import java.util.Arrays; import java.util.Date; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import static org.sakuli.AbstractLogAwareTest.getResource; import static org.sakuli.BaseTest.assertRegExMatch; diff --git a/src/core/src/test/java/org/sakuli/aop/ModifySahiTimerAspectTest.java b/src/core/src/test/java/org/sakuli/aop/ModifySahiTimerAspectTest.java index c08b23b6..596bd47d 100644 --- a/src/core/src/test/java/org/sakuli/aop/ModifySahiTimerAspectTest.java +++ b/src/core/src/test/java/org/sakuli/aop/ModifySahiTimerAspectTest.java @@ -38,7 +38,7 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import static org.mockito.Matchers.anyString; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; import static org.testng.Assert.assertEquals; @@ -96,13 +96,13 @@ public void testModifySahiTimer() throws Exception { //test modifcation of timer to 1000ms testling.modifySahiTimer(mock(JoinPoint.class), true); verify(session).setVariable(SahiProxyProperties.SAHI_REQUEST_DELAY_TIME_VAR, "1000"); - verify(baseActionLoader.getExceptionHandler(), never()).handleException(any(Throwable.class)); + verify(baseActionLoader.getExceptionHandler(), never()).handleException(any(Exception.class)); assertLastLine(logFile, "sahi-proxy-timer", LogLevel.INFO, "sahi-proxy-timer modified to 1000 ms"); //test reset timer testling.modifySahiTimer(mock(JoinPoint.class), false); verify(session).setVariable(SahiProxyProperties.SAHI_REQUEST_DELAY_TIME_VAR, null); - verify(baseActionLoader.getExceptionHandler(), never()).handleException(any(Throwable.class)); + verify(baseActionLoader.getExceptionHandler(), never()).handleException(any(Exception.class)); assertLastLine(logFile, "sahi-proxy-timer", LogLevel.INFO, "reset sahi-proxy-timer"); } @@ -122,7 +122,7 @@ public void testDisabledModifySahiTimer() throws Exception { testling.modifySahiTimer(mock(JoinPoint.class), true); testling.modifySahiTimer(mock(JoinPoint.class), false); verify(session, never()).setVariable(anyString(), anyString()); - verify(baseActionLoader.getExceptionHandler(), never()).handleException(any(Throwable.class)); + verify(baseActionLoader.getExceptionHandler(), never()).handleException(any(Exception.class)); //test no session available when(baseActionLoader.getSahiProxyProperties().isRequestDelayActive()).thenReturn(true); @@ -130,7 +130,7 @@ public void testDisabledModifySahiTimer() throws Exception { testling.modifySahiTimer(mock(JoinPoint.class), true); testling.modifySahiTimer(mock(JoinPoint.class), false); verify(session, never()).setVariable(anyString(), anyString()); - verify(baseActionLoader.getExceptionHandler(), never()).handleException(any(Throwable.class)); + verify(baseActionLoader.getExceptionHandler(), never()).handleException(any(Exception.class)); } diff --git a/src/core/src/test/java/org/sakuli/aop/RhinoAspectTest.java b/src/core/src/test/java/org/sakuli/aop/RhinoAspectTest.java index d9313a1e..eca7a0d0 100644 --- a/src/core/src/test/java/org/sakuli/aop/RhinoAspectTest.java +++ b/src/core/src/test/java/org/sakuli/aop/RhinoAspectTest.java @@ -39,7 +39,7 @@ import org.testng.annotations.DataProvider; import org.testng.annotations.Test; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; @@ -232,7 +232,7 @@ public void testDoTestCaseActionLog() throws Exception { testAction.init("testID", 3, 4, "imagefolder1", "imagefolder2"); assertLastLine(logFile, testAction.getClass().getSimpleName(), LogLevel.INFO, - "\"test case [" + sampleTc.getActionValueString() + "]\" TestCaseAction.init() - init a new test case with arg(s) [testID, 3, 4, [imagefolder1, imagefolder2]]"); + "TestCaseAction.init() - init a new test case with arg(s) [testID, 3, 4, [imagefolder1, imagefolder2]]"); } @Test diff --git a/src/core/src/test/java/org/sakuli/aop/SahiCommandExecutionAspectTest.java b/src/core/src/test/java/org/sakuli/aop/SahiCommandExecutionAspectTest.java index b6c7300b..52cb9435 100644 --- a/src/core/src/test/java/org/sakuli/aop/SahiCommandExecutionAspectTest.java +++ b/src/core/src/test/java/org/sakuli/aop/SahiCommandExecutionAspectTest.java @@ -32,7 +32,7 @@ import java.util.Arrays; import java.util.Iterator; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; diff --git a/src/core/src/test/java/org/sakuli/builder/TestCaseExampleBuilder.java b/src/core/src/test/java/org/sakuli/builder/TestCaseExampleBuilder.java index 7b6281f7..3516ff34 100644 --- a/src/core/src/test/java/org/sakuli/builder/TestCaseExampleBuilder.java +++ b/src/core/src/test/java/org/sakuli/builder/TestCaseExampleBuilder.java @@ -23,7 +23,7 @@ import org.sakuli.datamodel.TestCase; import org.sakuli.datamodel.TestCaseStep; import org.sakuli.datamodel.state.TestCaseState; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import java.nio.file.Path; import java.util.Collections; @@ -45,7 +45,7 @@ public class TestCaseExampleBuilder implements ExampleBuilder { private Date stopDate; private int warningTime; private int criticalTime; - private SakuliException exception; + private SakuliCheckedException exception; private Path testCaseFile; private String lastURL; private String startURL; @@ -125,7 +125,7 @@ public TestCaseExampleBuilder withCriticalTime(int criticalTime) { return this; } - public TestCaseExampleBuilder withException(SakuliException exception) { + public TestCaseExampleBuilder withException(SakuliCheckedException exception) { this.exception = exception; return this; } diff --git a/src/core/src/test/java/org/sakuli/builder/TestCaseStepExampleBuilder.java b/src/core/src/test/java/org/sakuli/builder/TestCaseStepExampleBuilder.java index 2b4f90f4..6bc2f6b1 100644 --- a/src/core/src/test/java/org/sakuli/builder/TestCaseStepExampleBuilder.java +++ b/src/core/src/test/java/org/sakuli/builder/TestCaseStepExampleBuilder.java @@ -23,7 +23,7 @@ import org.sakuli.datamodel.TestCaseStep; import org.sakuli.datamodel.builder.TestCaseStepBuilder; import org.sakuli.datamodel.state.TestCaseStepState; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import java.util.Date; @@ -35,15 +35,17 @@ public class TestCaseStepExampleBuilder implements ExampleBuilder private TestCaseStepState state; private String name; private int warningTime; + private int criticalTime; private Date stopDate; private Date startDate; - private SakuliException exception; + private SakuliCheckedException exception; private DateTime creationDate; public TestCaseStepExampleBuilder() { this.state = TestCaseStepState.OK; this.name = "step for unit test"; this.warningTime = 4; + this.criticalTime = 8; this.startDate = new Date(); this.stopDate = DateUtils.addSeconds(startDate, 3); this.creationDate = new DateTime(); @@ -55,6 +57,7 @@ public TestCaseStep buildExample() { step.setStartDate(startDate); step.setStopDate(stopDate); step.setWarningTime(warningTime); + step.setCriticalTime(criticalTime); step.setName(name); step.setState(state); step.addException(exception); @@ -82,12 +85,17 @@ public TestCaseStepExampleBuilder withWarningTime(int warningTime) { return this; } + public TestCaseStepExampleBuilder withCriticalTime(int criticalTime) { + this.criticalTime = criticalTime; + return this; + } + public TestCaseStepExampleBuilder withName(String name) { this.name = name; return this; } - public TestCaseStepExampleBuilder withException(SakuliException e) { + public TestCaseStepExampleBuilder withException(SakuliCheckedException e) { this.exception = e; return this; } diff --git a/src/core/src/test/java/org/sakuli/builder/TestSuiteExampleBuilder.java b/src/core/src/test/java/org/sakuli/builder/TestSuiteExampleBuilder.java index ade3152f..951097f3 100644 --- a/src/core/src/test/java/org/sakuli/builder/TestSuiteExampleBuilder.java +++ b/src/core/src/test/java/org/sakuli/builder/TestSuiteExampleBuilder.java @@ -22,7 +22,7 @@ import org.sakuli.datamodel.TestCase; import org.sakuli.datamodel.TestSuite; import org.sakuli.datamodel.state.TestSuiteState; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import java.nio.file.Path; import java.util.Arrays; @@ -41,7 +41,7 @@ public class TestSuiteExampleBuilder implements ExampleBuilder { private String id; private Date stopDate; private Date startDate; - private SakuliException exception; + private SakuliCheckedException exception; private List testCases; private int warningTime; private int criticalTime; @@ -107,7 +107,7 @@ public TestSuiteExampleBuilder withStartDate(Date startDate) { return this; } - public TestSuiteExampleBuilder withException(SakuliException exception) { + public TestSuiteExampleBuilder withException(SakuliCheckedException exception) { this.exception = exception; return this; } diff --git a/src/core/src/test/java/org/sakuli/datamodel/TestCaseStepTest.java b/src/core/src/test/java/org/sakuli/datamodel/TestCaseStepTest.java index 94395366..d57c6b0a 100644 --- a/src/core/src/test/java/org/sakuli/datamodel/TestCaseStepTest.java +++ b/src/core/src/test/java/org/sakuli/datamodel/TestCaseStepTest.java @@ -19,10 +19,12 @@ package org.sakuli.datamodel; +import org.joda.time.DateTime; import org.sakuli.builder.TestCaseExampleBuilder; import org.sakuli.builder.TestCaseStepExampleBuilder; import org.sakuli.datamodel.state.TestCaseStepState; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.util.Collections; @@ -36,22 +38,32 @@ * Date: 19.07.13 */ public class TestCaseStepTest { - @Test - public void testRefreshState() throws Exception { - TestCaseStep testling = new TestCaseStep(); - testling.refreshState(); - assertEquals(TestCaseStepState.INIT, testling.getState()); - Date currentDate = new Date(); - testling.setStartDate(new Date(currentDate.getTime() - TimeUnit.SECONDS.toMillis(5))); - testling.stopDate = currentDate; - - testling.refreshState(); - assertEquals(TestCaseStepState.OK, testling.getState()); + @DataProvider + public Object[][] refreshStateDP() { + DateTime currentDate = new DateTime(); + return new Object[][] { + { null, currentDate.plusSeconds(5), -1, -1, TestCaseStepState.INIT}, + { currentDate, null, -1, -1, TestCaseStepState.INIT}, + { currentDate, currentDate.plusSeconds(5), -1, -1, TestCaseStepState.OK}, + { currentDate, currentDate.plusSeconds(5), 5, -1, TestCaseStepState.OK}, + { currentDate, currentDate.plusSeconds(5), 5, 5, TestCaseStepState.OK}, + { currentDate, currentDate.plusSeconds(5), 3, 4, TestCaseStepState.CRITICAL}, + { currentDate, currentDate.plusSeconds(5), 1, 5, TestCaseStepState.WARNING}, + }; + } - testling.setWarningTime(4); + @Test(dataProvider = "refreshStateDP") + public void refreshState(DateTime startDate, DateTime stopDate, int warningTime, int criticalTime,TestCaseStepState expectedState) { + TestCaseStep testling = new TestCaseStepExampleBuilder() + .withState(null) + .withStartDate(startDate != null ? startDate.toDate() : null) + .withStopDate(stopDate != null ? stopDate.toDate() : null) + .withWarningTime(warningTime) + .withCriticalTime(criticalTime) + .buildExample(); testling.refreshState(); - assertEquals(TestCaseStepState.WARNING, testling.getState()); + assertEquals(testling.getState(), expectedState); } @Test @@ -87,13 +99,13 @@ public void testSetId() throws Exception { @Test public void testGetExceptionMessage() throws Exception { - TestCase testCase = new TestCaseExampleBuilder().withException(new SakuliException("CASE-EXCEPTION")).buildExample(); + TestCase testCase = new TestCaseExampleBuilder().withException(new SakuliCheckedException("CASE-EXCEPTION")).buildExample(); assertEquals(testCase.getExceptionMessages(true), "CASE-EXCEPTION"); assertEquals(testCase.getExceptionMessages(false), "CASE-EXCEPTION"); testCase.setSteps(Collections.singletonList( new TestCaseStepExampleBuilder() - .withException(new SakuliException("STEP-EXCEPTION")) + .withException(new SakuliCheckedException("STEP-EXCEPTION")) .buildExample())); assertEquals(testCase.getExceptionMessages(true), "CASE-EXCEPTION - STEP \"step_for_unit_test\": STEP-EXCEPTION"); assertEquals(testCase.getExceptionMessages(false), "CASE-EXCEPTION\n\tSTEP \"step_for_unit_test\": STEP-EXCEPTION"); diff --git a/src/core/src/test/java/org/sakuli/datamodel/TestSuiteTest.java b/src/core/src/test/java/org/sakuli/datamodel/TestSuiteTest.java index 202dbaf1..ccd2bf85 100644 --- a/src/core/src/test/java/org/sakuli/datamodel/TestSuiteTest.java +++ b/src/core/src/test/java/org/sakuli/datamodel/TestSuiteTest.java @@ -22,11 +22,14 @@ import org.sakuli.datamodel.builder.TestCaseStepBuilder; import org.sakuli.datamodel.state.TestCaseState; import org.sakuli.datamodel.state.TestSuiteState; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.springframework.test.util.ReflectionTestUtils; import org.testng.annotations.Test; -import java.util.*; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.TimeUnit; import static org.testng.Assert.*; @@ -154,13 +157,13 @@ public void testGetGuid() throws Exception { public void testGetExceptionMessage() { TestSuite testSuite = new TestSuite(); String message = "suite-exception"; - testSuite.addException(new SakuliException(message)); + testSuite.addException(new SakuliCheckedException(message)); assertEquals(testSuite.getExceptionMessages(false), message); assertEquals(testSuite.getExceptionMessages(true), message); TestCase tc1 = new TestCase("case1", "case1"); String messageCase = "case-exception"; - tc1.addException(new SakuliException(messageCase)); + tc1.addException(new SakuliCheckedException(messageCase)); testSuite.addTestCase(tc1.getId(), tc1); assertEquals(testSuite.getExceptionMessages(false), message + "\n" + "CASE \"" + tc1.getId() + "\": " + messageCase); assertEquals(testSuite.getExceptionMessages(true), message + " -- CASE \"" + tc1.getId() + "\": " + messageCase); @@ -170,18 +173,18 @@ public void testGetExceptionMessage() { public void testGetExceptionMessageWithErrorInStep() { TestSuite testSuite = new TestSuite(); String message = "suite-exception"; - testSuite.addException(new SakuliException(message)); + testSuite.addException(new SakuliCheckedException(message)); assertEquals(testSuite.getExceptionMessages(false), message); assertEquals(testSuite.getExceptionMessages(true), message); TestCase tc1 = new TestCase("case1", "case1"); String messageCase = "case-exception"; - tc1.addException(new SakuliException(messageCase)); + tc1.addException(new SakuliCheckedException(messageCase)); testSuite.addTestCase(tc1.getId(), tc1); TestCaseStep step1 = new TestCaseStepBuilder("step1").build(); String messageStep = "step-exception"; - step1.addException(new SakuliException(messageStep)); + step1.addException(new SakuliCheckedException(messageStep)); tc1.addStep(step1); assertEquals(testSuite.getExceptionMessages(false), diff --git a/src/core/src/test/java/org/sakuli/datamodel/actions/ImageLibObjectTest.java b/src/core/src/test/java/org/sakuli/datamodel/actions/ImageLibObjectTest.java index 04b7d1b0..4ad47415 100644 --- a/src/core/src/test/java/org/sakuli/datamodel/actions/ImageLibObjectTest.java +++ b/src/core/src/test/java/org/sakuli/datamodel/actions/ImageLibObjectTest.java @@ -1,7 +1,25 @@ +/* + * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. + * + * Copyright 2013 - 2018 the original author or authors. + * + * 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 org.sakuli.datamodel.actions; import org.sakuli.BaseTest; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.testng.annotations.Test; import java.nio.file.Path; @@ -42,7 +60,7 @@ public void testCreationPNG() throws Exception { assertNotNull(testling.getPattern()); } - @Test(expectedExceptions = SakuliException.class, expectedExceptionsMessageRegExp = "Image-File '.*no-existing-path' does not exists!") + @Test(expectedExceptions = SakuliCheckedException.class, expectedExceptionsMessageRegExp = "Image-File '.*no-existing-path' does not exists!") public void testException() throws Exception { new ImageLibObject(Paths.get("no-existing-path")); } diff --git a/src/core/src/test/java/org/sakuli/datamodel/helper/TestCaseStepHelperTest.java b/src/core/src/test/java/org/sakuli/datamodel/helper/TestCaseStepHelperTest.java index 5ac37cba..f1adbda9 100644 --- a/src/core/src/test/java/org/sakuli/datamodel/helper/TestCaseStepHelperTest.java +++ b/src/core/src/test/java/org/sakuli/datamodel/helper/TestCaseStepHelperTest.java @@ -19,7 +19,6 @@ package org.sakuli.datamodel.helper; import org.apache.commons.io.FileUtils; -import org.sakuli.BaseTest; import org.sakuli.datamodel.TestCase; import org.sakuli.datamodel.TestCaseStep; import org.sakuli.datamodel.TestSuite; @@ -45,7 +44,7 @@ /** * @author tschneck - * Date: 02.07.15 + * Date: 02.07.15 */ public class TestCaseStepHelperTest { @@ -59,15 +58,7 @@ public void setUp() throws Exception { } @Test - public void testCheckWarningTime() throws Exception { - assertNull(TestCaseStepHelper.checkWarningTime(0, "test")); - assertNull(TestCaseStepHelper.checkWarningTime(1, "test")); - String regex = "TestCaseStep \\[name = test\\] - the warning threshold.*"; - BaseTest.assertRegExMatch(TestCaseStepHelper.checkWarningTime(-1, "test"), regex); - } - - @Test - public void testParseSteps() throws Throwable { + public void testParseSteps() throws Exception { Path tcFile = getResource("stephelper/tc.js"); FileUtils.writeStringToFile( tcFile.getParent().resolve(TestCaseStepHelper.STEPS_CACHE_FILE).toFile(), @@ -91,14 +82,14 @@ protected void assertInitStep(TestCaseStep step, String stepId) { } @Test - public void testNoCacheFileFound() throws Throwable { + public void testNoCacheFileFound() throws Exception { assertNull(this.getClass().getResource(CACHEFILE_NAME)); List steps = TestCaseStepHelper.readCachedStepDefinitions(getResource("stephelper/tc.js")); assertEquals(steps.size(), 0); } @Test - public void testWriteCachedStepsError() throws Throwable { + public void testWriteCachedStepsError() throws Exception { Path tcFile = getResource("stephelper/tc.js"); TestSuite testSuite = mock(TestSuite.class); TestCase tc = mock(TestCase.class); diff --git a/src/core/src/test/java/org/sakuli/datamodel/helper/TestSuiteHelperTest.java b/src/core/src/test/java/org/sakuli/datamodel/helper/TestSuiteHelperTest.java index 70f35154..27b80d05 100644 --- a/src/core/src/test/java/org/sakuli/datamodel/helper/TestSuiteHelperTest.java +++ b/src/core/src/test/java/org/sakuli/datamodel/helper/TestSuiteHelperTest.java @@ -41,7 +41,7 @@ public class TestSuiteHelperTest extends TestSuitePropertiesTestUtils { @Test - public void testInit() throws Throwable { + public void testInit() throws Exception { TestSuiteProperties testProps = TestSuitePropertiesTestUtils.getTestProps(this.getClass(), "valid", "suite_id_001"); HashMap result = TestSuiteHelper.loadTestCases(testProps); @@ -55,7 +55,7 @@ public void testInit() throws Throwable { } @Test(expectedExceptions = FileNotFoundException.class, expectedExceptionsMessageRegExp = "test case path \".*unValidTestCase.*\" doesn't exists - check your \"testsuite.suite\" file") - public void testInitExceptionForTestCase() throws Throwable { + public void testInitExceptionForTestCase() throws Exception { TestSuiteProperties testProps = TestSuitePropertiesTestUtils.getTestProps(this.getClass(), "unvalid", "unvalid_id"); TestSuiteHelper.loadTestCases(testProps); } diff --git a/src/core/src/test/java/org/sakuli/exceptions/SakuliExceptionHandlerTest.java b/src/core/src/test/java/org/sakuli/exceptions/SakuliExceptionHandlerTest.java index 4c95c64c..ea15ab3d 100644 --- a/src/core/src/test/java/org/sakuli/exceptions/SakuliExceptionHandlerTest.java +++ b/src/core/src/test/java/org/sakuli/exceptions/SakuliExceptionHandlerTest.java @@ -39,13 +39,14 @@ import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import java.awt.*; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.List; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; import static org.testng.Assert.*; @@ -71,6 +72,11 @@ public class SakuliExceptionHandlerTest extends BaseTest { @InjectMocks private SakuliExceptionHandler testling; + @SuppressWarnings("unchecked") + private static List getResumeExceptions(SakuliExceptionHandler testling) { + return (List) ReflectionTestUtils.getField(testling, "resumeExceptions"); + } + @BeforeMethod public void init() { MockitoAnnotations.initMocks(this); @@ -101,7 +107,7 @@ public void testHandleException() throws Exception { setUp(); when(loader.getCurrentTestCase()).thenReturn(null); testling.handleException(new Exception(testExcMessage)); - assertTrue(testSuite.getException() instanceof SakuliException); + assertTrue(testSuite.getException() instanceof SakuliCheckedException); assertEquals(testSuite.getException().getMessage(), testExcMessage); assertEquals(testSuite.getScreenShotPath(), expectedScreenshotPath); @@ -118,7 +124,7 @@ public void testHandleActionException() throws Exception { setUp(); when(loader.getCurrentTestCase()).thenReturn(null); testling.handleException(new SakuliActionException(testExcMessage)); - assertTrue(testSuite.getException() instanceof SakuliException); + assertTrue(testSuite.getException() instanceof SakuliCheckedException); assertEquals(testSuite.getException().getMessage(), testExcMessage); assertEquals(testSuite.getScreenShotPath(), expectedScreenshotPath); } @@ -146,37 +152,17 @@ public void testHandleExceptionForTestCases() throws Exception { assertTrue(testling.isAlreadyProcessed(testExc)); } - @Test - public void testGetSuppressedExceptions() throws Exception { - setUp(); - Exception testExc = new Exception(testExcMessage); - testling.handleException(testExc, true); - assertTrue(testCase.getException() instanceof SakuliExceptionWithScreenshot); - assertEquals(testCase.getException().getMessage(), testExcMessage); - assertEquals(testCase.getScreenShotPath(), expectedScreenshotPath); - - SakuliRuntimeException resumedExceptions = null; - try { - testling.throwCollectedResumedExceptions(); - } catch (SakuliRuntimeException e) { - resumedExceptions = e; - } - assertNotNull(resumedExceptions, "exception is expected!"); - assertEquals(resumedExceptions.getMessage(), "test contains some suppressed resumed exceptions!"); - assertEquals(resumedExceptions.getSuppressed()[0].getMessage(), testExcMessage); - } - @Test public void testSakuliForwarderException() throws Exception { setUp(); when(loader.getCurrentTestCase()).thenReturn(null); - SakuliForwarderException forwarderException = new SakuliForwarderException("FORWARDER_EXCEPTION"); + SakuliForwarderCheckedException forwarderException = new SakuliForwarderCheckedException("FORWARDER_EXCEPTION"); testling.handleException(forwarderException, true); verify(screenshotActionsMock, never()).takeScreenshotWithTimestampThrowIOException(anyString(), any(Path.class), anyString(), any()); verify(sahiReport).addResult(anyString(), any(ResultType.class), anyString(), anyString()); assertEquals(testSuite.getException(), forwarderException); - assertTrue(testling.resumeToTestExcecution(testSuite.getException())); + assertTrue(testling.resumeToTestExecution(testSuite.getException())); assertTrue(testling.isAlreadyProcessed(testSuite.getException())); } @@ -184,7 +170,7 @@ public void testSakuliForwarderException() throws Exception { public void testSakuliForwarderException2() throws Exception { setUp(); when(loader.getCurrentTestCase()).thenReturn(null); - SakuliForwarderException forwarderException = new SakuliForwarderException("FORWARDER_EXCEPTION"); + SakuliForwarderCheckedException forwarderException = new SakuliForwarderCheckedException("FORWARDER_EXCEPTION"); testling.handleException(forwarderException); verify(screenshotActionsMock, never()).takeScreenshotWithTimestampThrowIOException(anyString(), any(Path.class), anyString(), any()); @@ -200,11 +186,11 @@ public void testSakuliActionExceptionTakeUnsuccessfulScreenshotFromRegion() thro SakuliActionException sakuliActionException = new SakuliActionException("ACTION_EXCEPTION"); testling.handleException(sakuliActionException, mock(RegionImpl.class), true); - verify(screenshotActionsMock).takeScreenshotWithTimestampThrowIOException(anyString(), any(Path.class), anyString(), any()); + verify(screenshotActionsMock).takeScreenshotWithTimestampThrowIOException(anyString(), nullable(Path.class), nullable(String.class), nullable(Rectangle.class)); verify(sahiReport).addResult(anyString(), any(ResultType.class), anyString(), anyString()); assertTrue(testSuite.getException() instanceof SakuliExceptionWithScreenshot); assertEquals(((SakuliExceptionWithScreenshot) testSuite.getException()).getScreenshot(), expectedScreenshotPath); - assertTrue(testling.resumeToTestExcecution(testSuite.getException())); + assertTrue(testling.resumeToTestExecution(testSuite.getException())); assertTrue(testling.isAlreadyProcessed(testSuite.getException())); } @@ -216,11 +202,11 @@ public void testSakuliActionExceptionTakeSuccessfulScreenshotFromRegion() throws SakuliActionException sakuliActionException = new SakuliActionException("ACTION_EXCEPTION"); testling.handleException(sakuliActionException, mock(RegionImpl.class), true); - verify(screenshotActionsMock).takeScreenshotWithTimestampThrowIOException(anyString(), any(Path.class), anyString(), any()); + verify(screenshotActionsMock).takeScreenshotWithTimestampThrowIOException(anyString(), nullable(Path.class), nullable(String.class), nullable(Rectangle.class)); verify(sahiReport).addResult(anyString(), any(ResultType.class), anyString(), anyString()); assertTrue(testSuite.getException() instanceof SakuliExceptionWithScreenshot); assertEquals(((SakuliExceptionWithScreenshot) testSuite.getException()).getScreenshot(), expectedScreenshotPath); - assertTrue(testling.resumeToTestExcecution(testSuite.getException())); + assertTrue(testling.resumeToTestExecution(testSuite.getException())); assertTrue(testling.isAlreadyProcessed(testSuite.getException())); } @@ -259,15 +245,15 @@ public void testGetScreenshotFile() throws Exception { @Test public void testGetAllExceptions() throws Exception { TestSuite ts = new TestSuite(); - ts.addException(new SakuliException("bla")); + ts.addException(new SakuliCheckedException("bla")); TestCase tc = new TestCase(null, null); - tc.addException(new SakuliException("bla2")); + tc.addException(new SakuliCheckedException("bla2")); ts.addTestCase(tc); TestCaseStep step = new TestCaseStep(); - step.addException(new SakuliException("bla3")); + step.addException(new SakuliCheckedException("bla3")); tc.addStep(step); - List allExceptions = SakuliExceptionHandler.getAllExceptions(ts); + List allExceptions = SakuliExceptionHandler.getAllExceptions(ts); assertEquals(allExceptions.size(), 3); } @@ -277,7 +263,7 @@ public void testContainsExceptionsStep() throws Exception { TestCase tc = new TestCase(null, null); ts.addTestCase(tc); TestCaseStep step = new TestCaseStep(); - step.addException(new SakuliException("bla3")); + step.addException(new SakuliCheckedException("bla3")); tc.addStep(step); assertTrue(SakuliExceptionHandler.containsException(ts)); } @@ -286,7 +272,7 @@ public void testContainsExceptionsStep() throws Exception { public void testContainsExceptionsCase() throws Exception { TestSuite ts = new TestSuite(); TestCase tc = new TestCase(null, null); - tc.addException(new SakuliException("bla2")); + tc.addException(new SakuliCheckedException("bla2")); ts.addTestCase(tc); TestCaseStep step = new TestCaseStep(); @@ -297,7 +283,7 @@ public void testContainsExceptionsCase() throws Exception { @Test public void testContainsExceptionsSuite() throws Exception { TestSuite ts = new TestSuite(); - ts.addException(new SakuliException("bla")); + ts.addException(new SakuliCheckedException("bla")); TestCase tc = new TestCase(null, null); ts.addTestCase(tc); TestCaseStep step = new TestCaseStep(); @@ -305,11 +291,21 @@ public void testContainsExceptionsSuite() throws Exception { assertTrue(SakuliExceptionHandler.containsException(ts)); } + @Test + public void testSaveExceptionToAsyncRef() throws Exception { + TestSuite ts = new TestSuite(); + final SakuliCheckedException e = new SakuliCheckedException("test"); + e.setAsyncTestDataRef(ts); + testling.saveException(e); + assertEquals(ts.getException().getMessage(), "test"); + verify(loader, never()).getTestSuite(); + } + @Test public void testSaveExceptionsInSuite() throws Exception { TestSuite ts = new TestSuite(); when(loader.getTestSuite()).thenReturn(ts); - testling.saveException(new SakuliException("test")); + testling.saveException(new SakuliCheckedException("test")); assertEquals(ts.getException().getMessage(), "test"); } @@ -319,7 +315,7 @@ public void testSaveExceptionsInCase() throws Exception { when(loader.getTestSuite()).thenReturn(ts); TestCase tc = new TestCase(null, null); when(loader.getCurrentTestCase()).thenReturn(tc); - testling.saveException(new SakuliException("test")); + testling.saveException(new SakuliCheckedException("test")); assertNull(ts.getException()); assertEquals(tc.getException().getMessage(), "test"); } @@ -332,9 +328,63 @@ public void testSaveExceptionsInStep() throws Exception { when(loader.getCurrentTestCase()).thenReturn(tc); TestCaseStep step = new TestCaseStep(); when(loader.getCurrentTestCaseStep()).thenReturn(step); - testling.saveException(new SakuliException("test")); + testling.saveException(new SakuliCheckedException("test")); assertNull(ts.getException()); assertNull(tc.getException()); assertEquals(step.getException().getMessage(), "test"); } + + @Test + public void testTransformExceptionSakuliCheckedException() { + Exception e = new SakuliForwarderCheckedException("test"); + assertEquals(testling.transformException(e), e); + List resumeExceptions = getResumeExceptions(testling); + assertTrue(resumeExceptions.contains(e)); + } + + @Test + public void testTransformExceptionSakuliRuntimeException() { + Exception e = new SakuliForwarderRuntimeException("test"); + assertEquals(testling.transformException(e), e); + List resumeExceptions = getResumeExceptions(testling); + assertTrue(resumeExceptions.contains(e)); + } + + @Test + public void testTransformExceptionAnyRuntimeException() throws Exception { + setUp(); + when(actionProperties.isTakeScreenshots()).thenReturn(false); + Exception e = new RuntimeException("test"); + final SakuliException result = testling.transformException(e); + assertEquals(result.getClass(), SakuliRuntimeException.class); + assertEquals(result.castTo().getCause(), e); + List resumeExceptions = getResumeExceptions(testling); + assertFalse(resumeExceptions.contains(e)); + assertFalse(resumeExceptions.contains(result)); + } + + @Test + public void testTransformExceptionAnyCheckedException() throws Exception { + setUp(); + when(actionProperties.isTakeScreenshots()).thenReturn(false); + Exception e = new Exception("test"); + final SakuliException result = testling.transformException(e); + assertEquals(result.getClass(), SakuliCheckedException.class); + assertEquals(result.castTo().getCause(), e); + List resumeExceptions = getResumeExceptions(testling); + assertFalse(resumeExceptions.contains(e)); + assertFalse(resumeExceptions.contains(result)); + } + + @Test + public void testTransformExceptionAsyncException() throws Exception { + setUp(); + SakuliException e = new SakuliRuntimeException(""); + e.setAsyncTestDataRef(testSuite); + when(actionProperties.isTakeScreenshots()).thenReturn(false); + final SakuliException result = testling.transformException(e.castTo()); + assertEquals(result, e); + List resumeExceptions = getResumeExceptions(testling); + assertTrue(resumeExceptions.contains(e)); + } } diff --git a/src/core/src/test/java/org/sakuli/loader/BaseActionLoaderTest.java b/src/core/src/test/java/org/sakuli/loader/BaseActionLoaderTest.java index 49dc0dd8..16537fac 100644 --- a/src/core/src/test/java/org/sakuli/loader/BaseActionLoaderTest.java +++ b/src/core/src/test/java/org/sakuli/loader/BaseActionLoaderTest.java @@ -29,7 +29,7 @@ import org.sakuli.datamodel.properties.SakuliProperties; import org.sakuli.datamodel.properties.TestSuiteProperties; import org.sakuli.datamodel.state.TestCaseStepState; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.exceptions.SakuliExceptionHandler; import org.springframework.test.util.ReflectionTestUtils; import org.testng.annotations.BeforeMethod; @@ -37,7 +37,7 @@ import java.io.IOException; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import static org.testng.Assert.*; @@ -101,7 +101,7 @@ public void testInitNoSahiSession() throws Exception { when(rhinoScriptRunner.getSession()).thenReturn(null); when(sakuliProperties.isLoadJavaScriptEngine()).thenReturn(true); - ArgumentCaptor ac = ArgumentCaptor.forClass(SakuliException.class); + ArgumentCaptor ac = ArgumentCaptor.forClass(SakuliCheckedException.class); testling.init(testCaseId, "."); verify(exceptionHandler).handleException(ac.capture()); assertEquals(ac.getValue().getMessage(), "cannot init rhino script runner with sakuli custom delay variable 'sakuli-delay-active'"); @@ -113,7 +113,7 @@ public void testInitTCNull() throws Exception { String testCaseId = "xyz"; when(testSuite.getTestCase(testCaseId)).thenReturn(null); testling.init(testCaseId, "."); - verify(exceptionHandler, times(1)).handleException(any(SakuliException.class)); + verify(exceptionHandler, times(1)).handleException(any(SakuliCheckedException.class)); } @Test diff --git a/src/core/src/test/java/org/sakuli/services/TeardownServiceHelperTest.java b/src/core/src/test/java/org/sakuli/services/TeardownServiceHelperTest.java index b4278c09..4385ce56 100644 --- a/src/core/src/test/java/org/sakuli/services/TeardownServiceHelperTest.java +++ b/src/core/src/test/java/org/sakuli/services/TeardownServiceHelperTest.java @@ -21,12 +21,15 @@ import org.sakuli.datamodel.TestSuite; import org.sakuli.datamodel.state.TestSuiteState; import org.sakuli.loader.BeanLoader; -import org.sakuli.services.common.CacheHandlingResultServiceImpl; -import org.sakuli.services.common.LogCleanUpResultServiceImpl; +import org.sakuli.services.common.CacheHandlingServiceImpl; +import org.sakuli.services.common.LogCleanUpServiceImpl; import org.sakuli.services.forwarder.database.DatabaseResultServiceImpl; import org.sakuli.services.forwarder.gearman.GearmanResultServiceImpl; import org.testng.annotations.Test; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.verify; import static org.testng.Assert.assertEquals; @@ -34,56 +37,50 @@ /** * @author tschneck - * Date: 09.04.15 + * Date: 09.04.15 */ public class TeardownServiceHelperTest extends AbstractServiceBaseTest { @Test - public void testInvokeAllTeardwonServices() throws Exception { + public void testInvokeAllTeardwonServices() { assertEquals(BeanLoader.loadMultipleBeans(TeardownService.class).size(), 5); DatabaseResultServiceImpl databaseResultService = mockDatabaseResultService(); GearmanResultServiceImpl gearmanResultService = mockGearmanResultService(); - CacheHandlingResultServiceImpl cacheHandlingResultService = mockCacheHandlingResultService(); - LogCleanUpResultServiceImpl logCleanUpResultService = mockLogCleanUpResultService(); + CacheHandlingServiceImpl cacheHandlingResultService = mockCacheHandlingResultService(); + LogCleanUpServiceImpl logCleanUpResultService = mockLogCleanUpResultService(); TestSuite testSuite = BeanLoader.loadBean(TestSuite.class); testSuite.setState(TestSuiteState.RUNNING); - TeardownServiceHelper.invokeTeardownServices(); + TeardownServiceHelper.invokeTeardownServices(testSuite, false); assertEquals(testSuite.getState(), TestSuiteState.OK); assertTrue(testSuite.getStopDate().after(testSuite.getStartDate())); - verify(databaseResultService).saveAllResults(); - verify(databaseResultService).refreshStates(); - verify(gearmanResultService).saveAllResults(); - verify(gearmanResultService).refreshStates(); - verify(cacheHandlingResultService).saveAllResults(); - verify(cacheHandlingResultService).refreshStates(); - verify(logCleanUpResultService).triggerAction(); + verify(databaseResultService).teardownTestSuite(eq(testSuite)); + verify(gearmanResultService).tearDown(eq(Optional.of(testSuite)), anyBoolean()); + verify(cacheHandlingResultService).teardownTestSuite(eq(testSuite)); + verify(logCleanUpResultService).teardownTestSuite(eq(testSuite)); } - private LogCleanUpResultServiceImpl mockLogCleanUpResultService() { - LogCleanUpResultServiceImpl logCleanUpResultService = BeanLoader.loadBean(LogCleanUpResultServiceImpl.class); - doNothing().when(logCleanUpResultService).triggerAction(); + private LogCleanUpServiceImpl mockLogCleanUpResultService() { + LogCleanUpServiceImpl logCleanUpResultService = BeanLoader.loadBean(LogCleanUpServiceImpl.class); + doNothing().when(logCleanUpResultService).teardownTestSuite(any()); return logCleanUpResultService; } private GearmanResultServiceImpl mockGearmanResultService() { GearmanResultServiceImpl gearmanResultService = BeanLoader.loadBean(GearmanResultServiceImpl.class); - doNothing().when(gearmanResultService).refreshStates(); - doNothing().when(gearmanResultService).saveAllResults(); + doNothing().when(gearmanResultService).tearDown(any(), anyBoolean()); return gearmanResultService; } private DatabaseResultServiceImpl mockDatabaseResultService() { DatabaseResultServiceImpl databaseResultService = BeanLoader.loadBean(DatabaseResultServiceImpl.class); - doNothing().when(databaseResultService).refreshStates(); - doNothing().when(databaseResultService).saveAllResults(); + doNothing().when(databaseResultService).teardownTestSuite(any()); return databaseResultService; } - private CacheHandlingResultServiceImpl mockCacheHandlingResultService() { - CacheHandlingResultServiceImpl cacheHandlingResultService = BeanLoader.loadBean(CacheHandlingResultServiceImpl.class); - doNothing().when(cacheHandlingResultService).refreshStates(); - doNothing().when(cacheHandlingResultService).saveAllResults(); + private CacheHandlingServiceImpl mockCacheHandlingResultService() { + CacheHandlingServiceImpl cacheHandlingResultService = BeanLoader.loadBean(CacheHandlingServiceImpl.class); + doNothing().when(cacheHandlingResultService).teardownTestSuite(any()); return cacheHandlingResultService; } } \ No newline at end of file diff --git a/src/core/src/test/java/org/sakuli/services/TeardownServiceTest.java b/src/core/src/test/java/org/sakuli/services/TeardownServiceTest.java new file mode 100644 index 00000000..125eace6 --- /dev/null +++ b/src/core/src/test/java/org/sakuli/services/TeardownServiceTest.java @@ -0,0 +1,74 @@ +/* + * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. + * + * Copyright 2013 - 2018 the original author or authors. + * + * 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 org.sakuli.services; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.sakuli.datamodel.AbstractTestDataEntity; +import org.sakuli.datamodel.TestCase; +import org.sakuli.datamodel.TestCaseStep; +import org.sakuli.datamodel.TestSuite; +import org.sakuli.exceptions.SakuliRuntimeException; +import org.testng.annotations.Test; + +import java.util.Optional; + +public class TeardownServiceTest { + + private TeardownService testling = new TeardownService() { + @Override + public void handleTeardownException(@NonNull Exception e, boolean async, @NonNull AbstractTestDataEntity testDataRef) { + throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e); + } + + @Override + public int getServicePriority() { + return 0; + } + }; + + @Test + public void testTearDownNoCalls() { + //if some method would be called the default exception will be thrown + testling.tearDown(Optional.empty()); + testling.tearDown(Optional.of(new AbstractTestDataEntity() { + @Override + public void refreshState() { + //stub + } + })); + } + + @Test(expectedExceptions = SakuliRuntimeException.class, + expectedExceptionsMessageRegExp = "Method 'teardownTestSuite' is not implemented.*") + public void testTeardownTestSuite() { + testling.tearDown(Optional.of(new TestSuite())); + } + + @Test(expectedExceptions = SakuliRuntimeException.class, + expectedExceptionsMessageRegExp = "Method 'teardownTestCase' is not implemented.*") + public void testTeardownTestCase() { + testling.tearDown(Optional.of(new TestCase("test", "test"))); + } + + @Test(expectedExceptions = SakuliRuntimeException.class, + expectedExceptionsMessageRegExp = "Method 'teardownTestCaseStep' is not implemented.*") + public void testTeardownTestCaseStep() { + testling.tearDown(Optional.of(new TestCaseStep())); + } +} \ No newline at end of file diff --git a/src/core/src/test/java/org/sakuli/services/cipher/EnvironmentCipherTest.java b/src/core/src/test/java/org/sakuli/services/cipher/EnvironmentCipherTest.java index 9c27ee9e..666511f9 100644 --- a/src/core/src/test/java/org/sakuli/services/cipher/EnvironmentCipherTest.java +++ b/src/core/src/test/java/org/sakuli/services/cipher/EnvironmentCipherTest.java @@ -33,7 +33,7 @@ /** * @author tschneck - * Date: 6/28/17 + * Date: 6/28/17 */ public class EnvironmentCipherTest { private EnvironmentCipher testling; @@ -41,7 +41,7 @@ public class EnvironmentCipherTest { private CipherProperties props; @BeforeMethod - public void setUp() throws Throwable { + public void setUp() throws Exception { MockitoAnnotations.initMocks(this); testling = new EnvironmentCipher(props); when(props.getEncryptionKey()).thenReturn(AesKeyHelper.createRandomBase64Key()); @@ -75,7 +75,7 @@ public void testEncryptRandomKey() throws Exception { } @Test(expectedExceptions = SakuliCipherException.class) - public void testNotEncryptedException() throws Throwable { + public void testNotEncryptedException() throws Exception { try { testling.decrypt("nonEncrypted"); } catch (SakuliCipherException e) { @@ -87,7 +87,7 @@ public void testNotEncryptedException() throws Throwable { } @Test(expectedExceptions = SakuliCipherException.class) - public void testEmptyTestException() throws Throwable { + public void testEmptyTestException() throws Exception { try { testling.decrypt(""); } catch (SakuliCipherException e) { @@ -99,7 +99,7 @@ public void testEmptyTestException() throws Throwable { } @Test(expectedExceptions = SakuliCipherException.class) - public void testNullKey() throws Throwable { + public void testNullKey() throws Exception { when(props.getEncryptionKey()).thenReturn(null); try { testling.decrypt("irrelevant"); @@ -112,7 +112,7 @@ public void testNullKey() throws Throwable { } @Test(expectedExceptions = SakuliCipherException.class) - public void testEmptyKey() throws Throwable { + public void testEmptyKey() throws Exception { when(props.getEncryptionKey()).thenReturn(""); try { testling.decrypt("irrelevant"); diff --git a/src/core/src/test/java/org/sakuli/services/cipher/NetworkInterfaceCipherUtilsTest.java b/src/core/src/test/java/org/sakuli/services/cipher/NetworkInterfaceCipherUtilsTest.java index 5155f8a9..8b9f1643 100644 --- a/src/core/src/test/java/org/sakuli/services/cipher/NetworkInterfaceCipherUtilsTest.java +++ b/src/core/src/test/java/org/sakuli/services/cipher/NetworkInterfaceCipherUtilsTest.java @@ -30,7 +30,7 @@ /** * @author tschneck - * Date: 06.08.13 + * Date: 06.08.13 */ public class NetworkInterfaceCipherUtilsTest { @@ -48,7 +48,7 @@ public static Object[][] secrects() { } @BeforeMethod - public void setUp() throws Throwable { + public void setUp() throws Exception { MockitoAnnotations.initMocks(this); props = new CipherProperties(); props.setEncryptionInterfaceAutodetect(true); @@ -57,13 +57,13 @@ public void setUp() throws Throwable { } @Test(dataProvider = "secrects") - public void testEncrypt(String testSecrect) throws Throwable { + public void testEncrypt(String testSecrect) throws Exception { //check if MAC-Adrress is reachable Assert.assertNotNull(testling.encrypt(testSecrect)); } @Test(dataProvider = "secrects") - public void testEncryptAndDecrypt(String testSecrect) throws Throwable { + public void testEncryptAndDecrypt(String testSecrect) throws Exception { String encrypted = testling.encrypt(testSecrect); final NetworkInterfaceCipher cipher2 = new NetworkInterfaceCipher(props); cipher2.scanNetworkInterfaces(); @@ -75,7 +75,7 @@ public void testEncryptAndDecrypt(String testSecrect) throws Throwable { } @Test(expectedExceptions = SakuliCipherException.class) - public void testNotEncryptedException() throws Throwable { + public void testNotEncryptedException() throws Exception { try { testling.decrypt("nonEncrypted"); } catch (SakuliCipherException e) { @@ -87,7 +87,7 @@ public void testNotEncryptedException() throws Throwable { } @Test(expectedExceptions = SakuliCipherException.class) - public void testEmptyTestException() throws Throwable { + public void testEmptyTestException() throws Exception { try { testling.decrypt(""); } catch (SakuliCipherException e) { @@ -99,7 +99,7 @@ public void testEmptyTestException() throws Throwable { } @Test - public void testChipherException() throws Throwable { + public void testChipherException() throws Exception { try { CipherProperties props = new CipherProperties(); props.setEncryptionInterfaceAutodetect(false); diff --git a/src/core/src/test/java/org/sakuli/services/common/AbstractResultServiceTest.java b/src/core/src/test/java/org/sakuli/services/common/AbstractResultServiceTest.java deleted file mode 100644 index e03719e1..00000000 --- a/src/core/src/test/java/org/sakuli/services/common/AbstractResultServiceTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. - * - * Copyright 2013 - 2015 the original author or authors. - * - * 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 org.sakuli.services.common; - -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.sakuli.datamodel.TestSuite; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import static org.mockito.Mockito.verify; - -public class AbstractResultServiceTest { - - @Mock - private TestSuite testSuite; - @InjectMocks - private AbstractResultService testling = new AbstractResultService() { - @Override - public int getServicePriority() { - return 0; - } - - @Override - public void saveAllResults() { - } - }; - - @BeforeMethod - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - } - - @Test - public void testRefreshStates() throws Exception { - testling.refreshStates(); - verify(testSuite).refreshState(); - } - -} \ No newline at end of file diff --git a/src/core/src/test/java/org/sakuli/services/common/CacheHandlingResultServiceImplTest.java b/src/core/src/test/java/org/sakuli/services/common/CacheHandlingServiceImplTest.java similarity index 83% rename from src/core/src/test/java/org/sakuli/services/common/CacheHandlingResultServiceImplTest.java rename to src/core/src/test/java/org/sakuli/services/common/CacheHandlingServiceImplTest.java index 45996c56..edd0a746 100644 --- a/src/core/src/test/java/org/sakuli/services/common/CacheHandlingResultServiceImplTest.java +++ b/src/core/src/test/java/org/sakuli/services/common/CacheHandlingServiceImplTest.java @@ -19,7 +19,9 @@ package org.sakuli.services.common; import org.apache.commons.io.FileUtils; -import org.mockito.Mockito; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import org.sakuli.builder.TestSuiteExampleBuilder; import org.sakuli.datamodel.TestCase; import org.sakuli.datamodel.TestSuite; @@ -27,9 +29,8 @@ import org.sakuli.datamodel.helper.TestCaseStepHelper; import org.sakuli.datamodel.state.TestCaseStepState; import org.sakuli.datamodel.state.TestSuiteState; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.exceptions.SakuliExceptionHandler; -import org.springframework.test.util.ReflectionTestUtils; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -41,27 +42,26 @@ import java.nio.file.Paths; import java.util.Arrays; import java.util.Collections; +import java.util.Optional; -import static org.mockito.Mockito.spy; import static org.testng.Assert.*; /** * @author tschneck - * Date: 8/25/15 + * Date: 8/25/15 */ -public class CacheHandlingResultServiceImplTest { - private CacheHandlingResultServiceImpl testling = spy(new CacheHandlingResultServiceImpl()); +public class CacheHandlingServiceImplTest { private TestSuite testSuite; + @Mock private SakuliExceptionHandler exceptionHandler; + @InjectMocks + private CacheHandlingServiceImpl testling; @BeforeMethod public void init() { testSuite = new TestSuiteExampleBuilder() - .withId("LOG_TEST_SUITE").withState(TestSuiteState.ERRORS).withException(new SakuliException("TEST")).buildExample(); - - exceptionHandler = Mockito.mock(SakuliExceptionHandler.class); - ReflectionTestUtils.setField(testling, "testSuite", testSuite); - ReflectionTestUtils.setField(testling, "exceptionHandler", exceptionHandler); + .withId("LOG_TEST_SUITE").withState(TestSuiteState.ERRORS).withException(new SakuliCheckedException("TEST")).buildExample(); + MockitoAnnotations.initMocks(this); } @Test @@ -83,12 +83,12 @@ public void testWriteCachedStepDefinitions() throws Exception { testSuite.setTestCases(Collections.singletonMap("1", tc)); //on error no cache file should be written - testling.saveAllResults(); + testling.tearDown(Optional.of(testSuite)); assertNull(getResource(cacheFilePath, false)); //on != error cache file should be written testSuite.setState(TestSuiteState.CRITICAL_IN_SUITE); - testling.saveAllResults(); + testling.tearDown(Optional.of(testSuite)); Path cacheFile = getResource(cacheFilePath, true); assertTrue(Files.exists(cacheFile)); assertEquals(FileUtils.readFileToString(cacheFile.toFile(), Charset.forName("UTF-8")), diff --git a/src/core/src/test/java/org/sakuli/services/common/CommonInitializingServiceImplTest.java b/src/core/src/test/java/org/sakuli/services/common/CommonInitializingServiceImplTest.java index ba7cf1b2..59a213f3 100644 --- a/src/core/src/test/java/org/sakuli/services/common/CommonInitializingServiceImplTest.java +++ b/src/core/src/test/java/org/sakuli/services/common/CommonInitializingServiceImplTest.java @@ -38,7 +38,7 @@ /** * @author tschneck - * Date: 22.05.14 + * Date: 22.05.14 */ public class CommonInitializingServiceImplTest { @@ -52,7 +52,7 @@ public class CommonInitializingServiceImplTest { private CommonInitializingServiceImpl testling; @Test - public void testInit() throws Throwable { + public void testInit() throws Exception { testSuiteProperties = spy(TestSuitePropertiesTestUtils.getTestProps(this.getClass(), "valid", "suite_id")); ts = spy(new TestSuite(testSuiteProperties)); MockitoAnnotations.initMocks(this); @@ -68,7 +68,7 @@ public void testInit() throws Throwable { } @Test - public void testInitWithOutTestCases() throws Throwable { + public void testInitWithOutTestCases() throws Exception { TestSuiteProperties props = new TestSuiteProperties(); props.setTestSuiteId("suite_id"); props.setLoadTestCasesAutomatic(false); @@ -88,7 +88,7 @@ public void testInitWithOutTestCases() throws Throwable { @Test(expectedExceptions = SakuliInitException.class, expectedExceptionsMessageRegExp = "Cannot read testsuite.suite.*") - public void testInitExceptionForTestCase() throws Throwable { + public void testInitExceptionForTestCase() throws Exception { testSuiteProperties = spy(TestSuitePropertiesTestUtils.getTestProps(this.getClass(), "unvalid", "")); testSuiteProperties.setTestSuiteId("testid"); ts = spy(new TestSuite(testSuiteProperties)); diff --git a/src/core/src/test/java/org/sakuli/services/common/CommonResultServiceImplTest.java b/src/core/src/test/java/org/sakuli/services/common/CommonResultServiceImplTest.java index 679bd039..3dab80d2 100644 --- a/src/core/src/test/java/org/sakuli/services/common/CommonResultServiceImplTest.java +++ b/src/core/src/test/java/org/sakuli/services/common/CommonResultServiceImplTest.java @@ -19,7 +19,6 @@ package org.sakuli.services.common; import org.mockito.InjectMocks; -import org.mockito.Spy; import org.sakuli.LoggerTest; import org.sakuli.builder.TestCaseExampleBuilder; import org.sakuli.builder.TestCaseStepExampleBuilder; @@ -28,11 +27,9 @@ import org.sakuli.datamodel.state.TestCaseState; import org.sakuli.datamodel.state.TestCaseStepState; import org.sakuli.datamodel.state.TestSuiteState; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.test.util.ReflectionTestUtils; -import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -40,11 +37,8 @@ import java.nio.file.Paths; import java.util.*; -import static org.mockito.Mockito.doNothing; - public class CommonResultServiceImplTest extends LoggerTest { - @Spy @InjectMocks private CommonResultServiceImpl testling; private Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -59,28 +53,21 @@ public static Object[][] states() { }; } - @Override - @BeforeMethod - public void init() { - super.init(); - doNothing().when(testling).cleanUp(); - } - @Test(dataProvider = "states") public void testSaveAllResults(TestSuiteState testSuiteState, TestCaseState testCaseState, String stateOutputRegex) throws Exception { TestCaseStepState stepState = TestCaseStepState.WARNING; TestSuite testSuite = new TestSuiteExampleBuilder() .withId("LOG_TEST_SUITE").withState(testSuiteState) - .withException(testSuiteState.isError() ? new SakuliException("TEST") : null) + .withException(testSuiteState.isError() ? new SakuliCheckedException("TEST") : null) .withTestCases(Collections.singletonList(new TestCaseExampleBuilder() - .withTestCaseSteps(Collections.singletonList(new TestCaseStepExampleBuilder().withState(stepState).buildExample())) - .withState(testCaseState) - .buildExample() + .withTestCaseSteps(Collections.singletonList(new TestCaseStepExampleBuilder() + .withState(stepState).buildExample())) + .withState(testCaseState) + .buildExample() )) .buildExample(); - ReflectionTestUtils.setField(testling, "testSuite", testSuite); Path logfile = Paths.get(properties.getLogFile()); - testling.saveAllResults(); + testling.tearDown(Optional.of(testSuite)); String lastLineOfLogFile = getLastLineOfLogFile(logfile, testSuiteState.isError() ? 42 : 39); List regExes = getValidationExpressions(testSuiteState, testCaseState, stepState, stateOutputRegex, "TEST"); @@ -102,7 +89,6 @@ private void verifyOutputLines(List lines, Iterator regExIterato private List getValidationExpressions(TestSuiteState testSuiteState, TestCaseState testCaseState, TestCaseStepState testCaseStepState, String stateOutputRegex, String errorMessage) { return Arrays.asList( - "INFO.*", "=========== RESULT of SAKULI Testsuite \"LOG_TEST_SUITE\" - " + testSuiteState + " =================", "test suite id: LOG_TEST_SUITE", "guid: LOG_TEST_SUITE.*", @@ -138,6 +124,7 @@ private List getValidationExpressions(TestSuiteState testSuiteState, Tes "\t\tdb primary key: -1*", "\t\tduration: 3.0 sec.", "\t\twarning time: 4 sec.", + "\t\tcritical time: 8 sec.", "\t\tstart time: .*", "\t\tend time: .*", "=========== SAKULI Testsuite \"LOG_TEST_SUITE\" execution FINISHED - " + testSuiteState + " ======================", diff --git a/src/core/src/test/java/org/sakuli/services/common/LogCleanUpResultServiceImplTest.java b/src/core/src/test/java/org/sakuli/services/common/LogCleanUpServiceImplTest.java similarity index 93% rename from src/core/src/test/java/org/sakuli/services/common/LogCleanUpResultServiceImplTest.java rename to src/core/src/test/java/org/sakuli/services/common/LogCleanUpServiceImplTest.java index 04239430..5566c941 100644 --- a/src/core/src/test/java/org/sakuli/services/common/LogCleanUpResultServiceImplTest.java +++ b/src/core/src/test/java/org/sakuli/services/common/LogCleanUpServiceImplTest.java @@ -23,6 +23,7 @@ import org.mockito.MockitoAnnotations; import org.mockito.Spy; import org.sakuli.BaseTest; +import org.sakuli.datamodel.TestSuite; import org.sakuli.datamodel.properties.SakuliProperties; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -36,23 +37,23 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; /** * @author tschneck - * Date: 2/12/16 + * Date: 2/12/16 */ -public class LogCleanUpResultServiceImplTest { +public class LogCleanUpServiceImplTest { @Mock private SakuliProperties sakuliProperties; @InjectMocks @Spy - private LogCleanUpResultServiceImpl testling; + private LogCleanUpServiceImpl testling; private Path tempLog; @BeforeMethod @@ -82,7 +83,7 @@ private void deletePath(Path path) throws IOException { @Test public void testNoLogFolder() throws Exception { when(sakuliProperties.getLogFolder()).thenReturn(Paths.get("NOT_EXIST")); - testling.triggerAction(); + testling.teardownTestSuite(new TestSuite()); verify(testling, never()).cleanUpDirectory(any()); } @@ -99,7 +100,7 @@ public void testTriggerAction() throws Exception { Path notToOld2 = createFileWithDate(tempLog.resolve("today-13.log"), todayMinus13); when(sakuliProperties.getLogFolder()).thenReturn(tempLog); - testling.triggerAction(); + testling.teardownTestSuite(new TestSuite()); verify(testling, times(2)).cleanUpDirectory(any()); assertTrue(Files.exists(folder1)); assertTrue(Files.exists(today)); diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/AbstractOutputBuilderTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/AbstractOutputBuilderTest.java index 9174fb50..d300f951 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/AbstractOutputBuilderTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/AbstractOutputBuilderTest.java @@ -30,11 +30,10 @@ import org.sakuli.datamodel.TestCase; import org.sakuli.datamodel.TestCaseStep; import org.sakuli.datamodel.TestSuite; -import org.sakuli.datamodel.properties.SakuliProperties; import org.sakuli.datamodel.state.TestCaseState; import org.sakuli.datamodel.state.TestCaseStepState; import org.sakuli.datamodel.state.TestSuiteState; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.exceptions.SakuliRuntimeException; import org.sakuli.services.forwarder.gearman.GearmanProperties; import org.springframework.test.util.ReflectionTestUtils; @@ -44,9 +43,11 @@ import java.io.File; import java.nio.file.Paths; -import java.util.*; +import java.util.Arrays; +import java.util.Date; +import java.util.SortedSet; +import java.util.TreeSet; -import static org.mockito.Mockito.when; import static org.sakuli.services.forwarder.gearman.TextPlaceholder.*; import static org.testng.Assert.assertEquals; @@ -155,7 +156,7 @@ public void testFormatTestSuiteStateMessageException() throws Exception { .withId("sakuli-123") .withState(TestSuiteState.ERRORS) .withStartDate(startDate) - .withException(new SakuliException("TEST-ERROR")) + .withException(new SakuliCheckedException("TEST-ERROR")) .withStopDate(DateUtils.addSeconds(startDate, 120)) .buildExample(); diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/AbstractTeardownServiceTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/AbstractTeardownServiceTest.java new file mode 100644 index 00000000..01c9c44d --- /dev/null +++ b/src/core/src/test/java/org/sakuli/services/forwarder/AbstractTeardownServiceTest.java @@ -0,0 +1,129 @@ +/* + * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. + * + * Copyright 2013 - 2018 the original author or authors. + * + * 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 org.sakuli.services.forwarder; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.sakuli.builder.TestSuiteExampleBuilder; +import org.sakuli.datamodel.TestCase; +import org.sakuli.datamodel.TestSuite; +import org.sakuli.datamodel.properties.ActionProperties; +import org.sakuli.datamodel.properties.SakuliProperties; +import org.sakuli.exceptions.*; +import org.sakuli.loader.ScreenActionLoader; +import org.springframework.test.util.ReflectionTestUtils; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.List; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; +import static org.sakuli.BaseTest.assertRegExMatch; +import static org.testng.Assert.*; + +@SuppressWarnings({"unchecked", "ThrowableNotThrown"}) +public class AbstractTeardownServiceTest { + + @Spy + @InjectMocks + private AbstractTeardownService testling = new AbstractTeardownService() { + @Override + public int getServicePriority() { + return 0; + } + + @Override + public void teardownTestCase(@NonNull TestCase testCase) throws RuntimeException { + throw new SakuliRuntimeException("teardown test case"); + } + }; + private SakuliExceptionHandler exceptionHandler; + @Mock + private ScreenActionLoader loader; + @Mock + private ActionProperties props; + @Mock + private SakuliProperties sakuliProps; + private TestSuite testsuite; + + @BeforeMethod + public void setUp() { + exceptionHandler = spy(SakuliExceptionHandler.class); + MockitoAnnotations.initMocks(this); + ReflectionTestUtils.setField(exceptionHandler, "loader", loader); + testsuite = new TestSuiteExampleBuilder().buildExample(); + when(props.isTakeScreenshots()).thenReturn(false); + when(sakuliProps.isSuppressResumedExceptions()).thenReturn(false); + when(loader.getActionProperties()).thenReturn(props); + when(loader.getSakuliProperties()).thenReturn(sakuliProps); + when(loader.getTestSuite()).thenReturn(testsuite); + } + + @Test + public void testHandleTeardownExceptionSequential() { + testling.tearDown(Optional.of(testsuite)); + verify(exceptionHandler).handleException(any(SakuliRuntimeException.class)); + verify(testling, never()).addTestRef(any(), any()); + + List resumeExceptions = (List) ReflectionTestUtils.getField(exceptionHandler, "resumeExceptions"); + //ensures that no stop execution action wil be triggered + assertTrue(resumeExceptions.stream().noneMatch(e -> e.getClass().isAssignableFrom(SakuliRuntimeException.class))); + + assertEquals(testsuite.getException().getClass(), SakuliRuntimeException.class); + assertRegExMatch(testsuite.getException().toString(), "Method 'teardownTestSuite' is not implemented for forwarder class.*"); + } + + @Test + public void testHandleTeardownExceptionAsync() { + final TestCase testCase = testsuite.getTestCases().values().stream().findFirst().get(); + testling.tearDown(Optional.of(testCase), true); + verify(exceptionHandler).handleException(any(SakuliRuntimeException.class)); + verify(testling).addTestRef(any(SakuliRuntimeException.class), eq(testCase)); + + List resumeExceptions = (List) ReflectionTestUtils.getField(exceptionHandler, "resumeExceptions"); + //ensures that no stop execution action wil be triggered + assertTrue(resumeExceptions.stream().allMatch(e -> e.getClass().isAssignableFrom(SakuliRuntimeException.class))); + + assertNull(testsuite.getException()); + assertEquals(testCase.getException().getClass(), SakuliRuntimeException.class); + assertEquals(testCase.getException().toString(), "teardown test case"); + } + + @Test + public void testAddTestRefSakuliException() { + @NonNull Exception e = new SakuliForwarderRuntimeException(""); + final Exception result = testling.addTestRef(e, testsuite); + assertEquals(result.getClass(), SakuliForwarderRuntimeException.class); + assertEquals(((SakuliException) result).getAsyncTestDataRef().get(), testsuite); + } + + @Test + public void testAddTestRefNoSakuliException() { + @NonNull Exception e = new RuntimeException(""); + final Exception result = testling.addTestRef(e, testsuite); + assertEquals(result.getClass(), SakuliCheckedException.class); + assertEquals(result.getCause(), e); + assertEquals(((SakuliException) result).getAsyncTestDataRef().get(), testsuite); + } +} \ No newline at end of file diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/AbstractTemplateOutputBuilderTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/AbstractTemplateOutputBuilderTest.java index dd1e49ae..b061d033 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/AbstractTemplateOutputBuilderTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/AbstractTemplateOutputBuilderTest.java @@ -41,6 +41,7 @@ import java.nio.file.Paths; import static org.mockito.Mockito.doReturn; +import static org.sakuli.services.forwarder.configuration.TemplateModelEntityName.*; import static org.testng.Assert.assertEquals; /** @@ -72,10 +73,10 @@ public void clean() throws Exception { @Test public void createModel() { - JtwigModel model = testling.createModel(); - assertEquals(model.get("testsuite").get().getValue(), testSuite); - assertEquals(model.get("sakuli").get().getValue(), sakuliProperties); - assertEquals(model.get("checkmk").get().getValue(), checkMKProperties); + JtwigModel model = testling.createModel(testSuite); + assertEquals(model.get(TEST_DATA_ENTITY.getName()).get().getValue(), testSuite); + assertEquals(model.get(SAKULI_PROPERTIES.getName()).get().getValue(), sakuliProperties); + assertEquals(model.get(CHECK_MK_PROPERTIES.getName()).get().getValue(), checkMKProperties); } @DataProvider diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/ScreenshotDivConverterTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/ScreenshotDivConverterTest.java index ceab60d2..cf55c197 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/ScreenshotDivConverterTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/ScreenshotDivConverterTest.java @@ -21,7 +21,7 @@ import org.mockito.*; import org.sakuli.exceptions.SakuliExceptionHandler; import org.sakuli.exceptions.SakuliExceptionWithScreenshot; -import org.sakuli.exceptions.SakuliForwarderException; +import org.sakuli.exceptions.SakuliForwarderCheckedException; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -30,7 +30,7 @@ import java.nio.file.Path; import java.nio.file.Paths; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import static org.testng.Assert.*; @@ -104,9 +104,7 @@ public void testExtractScreenshotName() throws Exception { testling.extractScreenshotFormat(new SakuliExceptionWithScreenshot("test", Paths.get("bsald_w.jpg"))), "jpg"); - assertEquals( - testling.extractScreenshotFormat(new SakuliExceptionWithScreenshot("test", null)), - null); + assertNull(testling.extractScreenshotFormat(new SakuliExceptionWithScreenshot("test", null))); } @SuppressWarnings("ThrowableResultOfMethodCallIgnored") @@ -114,14 +112,14 @@ public void testExtractScreenshotName() throws Exception { public void testThrowException() throws Exception { Path screenshotPath = Paths.get("computerNOTVALID.png"); - ArgumentCaptor excpCaptor = ArgumentCaptor.forClass(Throwable.class); + ArgumentCaptor excpCaptor = ArgumentCaptor.forClass(Exception.class); doNothing().when(sakuliExceptionHandler).handleException(excpCaptor.capture()); ScreenshotDiv result = testling.convert(new SakuliExceptionWithScreenshot("test", screenshotPath)); assertNull(result); - verify(sakuliExceptionHandler).handleException(any(SakuliForwarderException.class)); - Throwable excp = excpCaptor.getValue(); - assertTrue(excp instanceof SakuliForwarderException); + verify(sakuliExceptionHandler).handleException(any(SakuliForwarderCheckedException.class)); + Exception excp = excpCaptor.getValue(); + assertTrue(excp instanceof SakuliForwarderCheckedException); assertEquals(excp.getMessage(), "error during the BASE64 encoding of the screenshot 'computerNOTVALID.png'"); assertTrue(excp.getSuppressed()[0] instanceof NoSuchFileException); } @@ -136,10 +134,10 @@ public void testRemoveBase64Data() throws Exception { String result = ScreenshotDivConverter.removeBase64ImageDataString(testling.getPayloadString()); assertEquals(result, "
    " + - "
    " + - "Close X" + - "" + - "
    " + + "
    " + + "Close X" + + "" + + "
    " + "
    "); String srcString2 = "blas\nblakdfakdfjie"; diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/checkmk/CheckMKResultServiceImplTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/checkmk/CheckMKResultServiceImplTest.java index 8ce2f383..a6b50b1b 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/checkmk/CheckMKResultServiceImplTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/checkmk/CheckMKResultServiceImplTest.java @@ -63,7 +63,7 @@ public void createSpoolFilePath(String spoolDir, String freshness, String spoolF doReturn(spoolDir).when(checkMKProperties).getSpoolDir(); doReturn(freshness).when(checkMKProperties).getFreshness(); doReturn(spoolFileNamePrefix).when(checkMKProperties).getSpoolFilePrefix(); - assertEquals(testling.createSpoolFilePath().toString(), expectedFilePath); + assertEquals(testling.createSpoolFilePath(testSuite.getId()).toString(), expectedFilePath); } } diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/configuration/ExtractScreenshotFunctionTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/configuration/ExtractScreenshotFunctionTest.java index ab67c090..2448da25 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/configuration/ExtractScreenshotFunctionTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/configuration/ExtractScreenshotFunctionTest.java @@ -27,7 +27,7 @@ import java.util.Collections; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import static org.testng.Assert.assertNull; diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/configuration/GetTestDataEntityTypeFunctionTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/configuration/GetTestDataEntityTypeFunctionTest.java new file mode 100644 index 00000000..72ddb38b --- /dev/null +++ b/src/core/src/test/java/org/sakuli/services/forwarder/configuration/GetTestDataEntityTypeFunctionTest.java @@ -0,0 +1,49 @@ +/* + * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. + * + * Copyright 2013 - 2018 the original author or authors. + * + * 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 org.sakuli.services.forwarder.configuration; + +import org.sakuli.datamodel.AbstractTestDataEntity; +import org.sakuli.datamodel.TestCase; +import org.sakuli.datamodel.TestCaseStep; +import org.sakuli.datamodel.TestSuite; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.util.Arrays; + +import static org.testng.Assert.assertEquals; + +public class GetTestDataEntityTypeFunctionTest { + + @DataProvider + public Object[][] getTestDataEntityTypeDP() { + return new Object[][] { + { new TestSuite(), "TestSuite"}, + { new TestCase("test", "1"), "TestCase"}, + { new TestCaseStep(), "TestCaseStep"}, + }; + } + + @Test(dataProvider = "getTestDataEntityTypeDP") + public void getTestDataEntityType(AbstractTestDataEntity testDataEntity, String expectedType) { + GetTestDataEntityTypeFunction getTestDataEntityTypeFunction = new GetTestDataEntityTypeFunction(); + assertEquals(getTestDataEntityTypeFunction.execute(Arrays.asList(testDataEntity)), expectedType); + } + +} diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/database/DatabaseInitializingServiceImplTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/database/DatabaseInitializingServiceImplTest.java index 110b3f49..4cf0220d 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/database/DatabaseInitializingServiceImplTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/database/DatabaseInitializingServiceImplTest.java @@ -23,15 +23,15 @@ import org.mockito.MockitoAnnotations; import org.sakuli.datamodel.TestSuite; import org.sakuli.exceptions.SakuliExceptionHandler; -import org.sakuli.exceptions.SakuliForwarderException; +import org.sakuli.exceptions.SakuliForwarderCheckedException; import org.sakuli.services.forwarder.database.dao.DaoTestSuite; -import org.springframework.dao.DataAccessException; +import org.springframework.dao.DataAccessResourceFailureException; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyBoolean; -import static org.mockito.Matchers.eq; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; public class DatabaseInitializingServiceImplTest { @@ -60,8 +60,8 @@ public void testInitTestSuiteInDatabase() throws Exception { @Test public void testSaveResultsInDatabaseHandleException() throws Exception { - doThrow(DataAccessException.class).when(daoTestSuite).insertInitialTestSuiteData(); + doThrow(DataAccessResourceFailureException.class).when(daoTestSuite).insertInitialTestSuiteData(); testling.initTestSuite(); - verify(exceptionHandler).handleException(any(SakuliForwarderException.class), anyBoolean()); + verify(exceptionHandler).handleException(any(SakuliForwarderCheckedException.class), anyBoolean()); } } \ No newline at end of file diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/database/DatabaseResultServiceImplTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/database/DatabaseResultServiceImplTest.java index a1ab0e0d..8a592895 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/database/DatabaseResultServiceImplTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/database/DatabaseResultServiceImplTest.java @@ -30,15 +30,14 @@ import org.sakuli.services.forwarder.database.dao.DaoTestCase; import org.sakuli.services.forwarder.database.dao.DaoTestCaseStep; import org.sakuli.services.forwarder.database.dao.DaoTestSuite; -import org.springframework.dao.DataAccessException; -import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.dao.DataAccessResourceFailureException; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.util.*; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyInt; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.*; public class DatabaseResultServiceImplTest { @@ -63,24 +62,24 @@ public void setUp() throws Exception { } @Test - public void testSaveResultsInDatabase() throws Exception { - when(testSuite.getTestCases()).thenReturn(null); - testling.saveAllResults(); + public void testSaveResultsInDatabase() { + when(testSuite.getTestCases()).thenAnswer(a -> null); + testling.teardownTestSuite(testSuite); verify(daoTestSuite).saveTestSuiteResult(); verify(daoTestSuite).saveTestSuiteToSahiJobs(); verify(daoTestCase, never()).saveTestCaseResult(any(TestCase.class)); verify(daoTestCaseStep, never()).saveTestCaseSteps(any(SortedSet.class), anyInt()); } - @Test - public void testSaveResultsInDatabaseHandleException() throws Exception { - doThrow(DataAccessException.class).when(daoTestSuite).saveTestSuiteResult(); - testling.saveAllResults(); - verify(exceptionHandler).handleException(any(SakuliForwarderException.class), anyBoolean()); + @Test(expectedExceptions = SakuliForwarderException.class, + expectedExceptionsMessageRegExp = "error by saving the results to the database.*") + public void testSaveResultsInDatabaseHandleException() { + doThrow(DataAccessResourceFailureException.class).when(daoTestSuite).saveTestSuiteResult(); + testling.teardownTestSuite(testSuite); } @Test - public void testSaveResultsInDatabaseWithTestcases() throws Exception { + public void testSaveResultsInDatabaseWithTestcases() { Integer tcPrimaryKey = 22; TestCase tc1 = mock(TestCase.class); @@ -98,9 +97,8 @@ public void testSaveResultsInDatabaseWithTestcases() throws Exception { testSuite = new TestSuite(); testSuite.setTestCases(testCaseMap); - ReflectionTestUtils.setField(testling, "testSuite", testSuite); - testling.saveAllResults(); + testling.teardownTestSuite(testSuite); verify(daoTestSuite).saveTestSuiteResult(); verify(daoTestSuite).saveTestSuiteToSahiJobs(); diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/database/dao/DaoTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/database/dao/DaoTest.java index 8e25d03e..36177ef9 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/database/dao/DaoTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/database/dao/DaoTest.java @@ -20,7 +20,7 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.services.forwarder.database.dao.impl.Dao; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.testng.Assert; @@ -31,7 +31,7 @@ /** * @author tschneck - * Date: 22.07.13 + * Date: 22.07.13 */ public class DaoTest { @@ -41,13 +41,13 @@ public class DaoTest { private DataSource dataSource; @BeforeMethod - public void init() throws SakuliException { + public void init() throws SakuliCheckedException { MockitoAnnotations.initMocks(this); } @Test - public void testCreateSqlSetStringForNamedParameter() throws Throwable { + public void testCreateSqlSetStringForNamedParameter() throws Exception { testling = new Dao(dataSource) { }; MapSqlParameterSource source = new MapSqlParameterSource().addValue("testling2", "value").addValue("testling", "value"); diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/gearman/GearmanCacheServiceTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/gearman/GearmanCacheServiceTest.java index 0cd7b892..46121e0f 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/gearman/GearmanCacheServiceTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/gearman/GearmanCacheServiceTest.java @@ -28,7 +28,6 @@ import org.sakuli.services.forwarder.MonitoringPropertiesTestHelper; import org.sakuli.services.forwarder.gearman.model.NagiosCheckResult; import org.sakuli.services.forwarder.gearman.model.builder.NagiosCheckResultBuilder; -import org.springframework.test.util.ReflectionTestUtils; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -51,19 +50,17 @@ public class GearmanCacheServiceTest extends BaseTest { private GearmanProperties gearmanProperties; @Mock private TestSuiteProperties testSuiteProperties; - - @Mock - private TestSuite testSuite; @Mock private GearmanTemplateOutputBuilder outputBuilder; @InjectMocks private NagiosCheckResultBuilder checkResultBuilder; + private TestSuite testSuite; @BeforeMethod public void setUp() throws Exception { MockitoAnnotations.initMocks(this); - ReflectionTestUtils.setField(checkResultBuilder, "testSuite", new TestSuiteExampleBuilder().buildExample()); - when(outputBuilder.createOutput()).thenReturn("test_payload"); + testSuite = new TestSuiteExampleBuilder().buildExample(); + when(outputBuilder.createOutput(testSuite)).thenReturn("test_payload"); MonitoringPropertiesTestHelper.initMock(gearmanProperties); } @@ -76,7 +73,7 @@ public void testCacheResultsEmptyAndNew() throws Exception { testling.cacheResults(results); Assert.assertEquals(testling.getCachedResults().size(), 0L); - NagiosCheckResult newResult = checkResultBuilder.build(); + NagiosCheckResult newResult = checkResultBuilder.build(testSuite); results.add(newResult); testling.cacheResults(results); @@ -86,7 +83,7 @@ public void testCacheResultsEmptyAndNew() throws Exception { Assert.assertEquals(results.get(0).getUuid(), newResult.getUuid()); Assert.assertEquals(results.get(0).getPayload().trim(), newResult.getPayload().trim()); - NagiosCheckResult newResult2 = checkResultBuilder.build(); + NagiosCheckResult newResult2 = checkResultBuilder.build(testSuite); results.add(newResult2); testling.cacheResults(results); @@ -125,7 +122,7 @@ public void testCacheResultsRoundtrip() throws Exception { Assert.assertEquals(results.get(1).getUuid(), "example_xfce__2016_03_23_15_00_00_000"); Assert.assertTrue(results.get(1).getPayload().contains("[OK] case \"case2\"")); - NagiosCheckResult newResult = checkResultBuilder.build(); + NagiosCheckResult newResult = checkResultBuilder.build(testSuite); results.add(0, newResult); testling.cacheResults(results); diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/gearman/GearmanResultServiceImplTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/gearman/GearmanResultServiceImplTest.java index 447c19a4..c9e785b9 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/gearman/GearmanResultServiceImplTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/gearman/GearmanResultServiceImplTest.java @@ -27,12 +27,11 @@ import org.sakuli.builder.TestSuiteExampleBuilder; import org.sakuli.datamodel.TestSuite; import org.sakuli.exceptions.SakuliExceptionHandler; -import org.sakuli.exceptions.SakuliForwarderException; +import org.sakuli.exceptions.SakuliForwarderRuntimeException; import org.sakuli.exceptions.SakuliRuntimeException; import org.sakuli.services.forwarder.gearman.crypt.Aes; import org.sakuli.services.forwarder.gearman.model.NagiosCheckResult; import org.sakuli.services.forwarder.gearman.model.builder.NagiosCheckResultBuilder; -import org.springframework.test.util.ReflectionTestUtils; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; @@ -48,6 +47,9 @@ public class GearmanResultServiceImplTest extends BaseTest { + private final String host = "99.99.99.20"; + private final int port = 4730; + private final String queueName = "check_results"; @Spy @InjectMocks private GearmanResultServiceImpl testling; @@ -61,10 +63,6 @@ public class GearmanResultServiceImplTest extends BaseTest { private NagiosCheckResultBuilder checkResultBuilder; private GearmanClient gearmanClient; private GearmanJobServerConnection connection; - - private final String host = "99.99.99.20"; - private final int port = 4730; - private final String queueName = "check_results"; private String testResult = "blub"; @BeforeMethod @@ -97,7 +95,6 @@ public void restoreKeyLength() { @Test public void testSaveAllResults() throws Exception { - when(checkResultBuilder.build()).thenReturn(new NagiosCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult)); GearmanJob job = mock(GearmanJob.class); doReturn(job).when(testling).creatJob(any(NagiosCheckResult.class)); @@ -118,16 +115,15 @@ public void testSaveAllResults() throws Exception { .withStopDate(stopDate) .withStartDate(DateUtils.addSeconds(stopDate, -60)) .buildExample(); + when(checkResultBuilder.build(eq(testSuite))).thenReturn(new NagiosCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult)); - ReflectionTestUtils.setField(testling, "testSuite", testSuite); - - testling.saveAllResults(); + testling.tearDown(Optional.of(testSuite)); //checks verify(gearmanCacheService, never()).cacheResults(anyList()); verify(gearmanCacheService, never()).getCachedResults(); - verify(exceptionHandler, never()).handleException(any(Throwable.class)); - verify(exceptionHandler, never()).handleException(any(Throwable.class), anyBoolean()); + verify(exceptionHandler, never()).handleException(any(Exception.class)); + verify(exceptionHandler, never()).handleException(any(Exception.class), anyBoolean()); verify(testling).getGearmanClient(); verify(testling).getGearmanConnection(host, port); verify(gearmanClient).addJobServer(connection); @@ -161,14 +157,16 @@ public void testCreateJobEncryptionEnabled() throws Exception { public void testSaveAllResultsConnectionFailed() throws Exception { when(gearmanClient.addJobServer(connection)).thenReturn(false); - when(checkResultBuilder.build()).thenReturn(new NagiosCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult)); + TestSuite testSuite = new TestSuiteExampleBuilder() + .buildExample(); + when(checkResultBuilder.build(testSuite)).thenReturn(new NagiosCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult)); - testling.saveAllResults(); + testling.tearDown(Optional.of(testSuite)); //checks verify(gearmanCacheService, never()).cacheResults(anyList()); verify(gearmanCacheService, never()).getCachedResults(); - verify(exceptionHandler).handleException(any(Throwable.class), eq(true)); + verify(exceptionHandler).handleException(any(Exception.class)); verify(testling).getGearmanClient(); verify(testling).getGearmanConnection(host, port); verify(gearmanClient).addJobServer(connection); @@ -179,7 +177,9 @@ public void testSaveAllResultsConnectionFailed() throws Exception { public void testSaveAllResultsConnectionFailedCacheResults() throws Exception { when(properties.isCacheEnabled()).thenReturn(true); - when(checkResultBuilder.build()).thenReturn(new NagiosCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult)); + TestSuite testSuite = new TestSuiteExampleBuilder() + .buildExample(); + when(checkResultBuilder.build(testSuite)).thenReturn(new NagiosCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult)); when(gearmanClient.addJobServer(connection)).thenReturn(false); @@ -190,12 +190,12 @@ public void testSaveAllResultsConnectionFailedCacheResults() throws Exception { return null; }).when(gearmanCacheService).cacheResults(anyList()); - testling.saveAllResults(); + testling.tearDown(Optional.of(testSuite)); //checks verify(gearmanCacheService).cacheResults(anyList()); verify(gearmanCacheService).getCachedResults(); - verify(exceptionHandler).handleException(any(Throwable.class), eq(true)); + verify(exceptionHandler).handleException(any(Exception.class)); verify(testling).getGearmanClient(); verify(testling).getGearmanConnection(host, port); verify(gearmanClient).addJobServer(connection); @@ -215,7 +215,9 @@ public void testSaveAllResultsJobSubmitFailedAddCacheResults() throws Exception when(gearmanCacheService.getCachedResults()).thenReturn(Arrays.asList(mockedResult1, mockedResult2)); NagiosCheckResult newResult = new NagiosCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult); - when(checkResultBuilder.build()).thenReturn(newResult); + TestSuite testSuite = new TestSuiteExampleBuilder() + .buildExample(); + when(checkResultBuilder.build(testSuite)).thenReturn(newResult); doAnswer(invocationOnMock -> { List results = ((List) invocationOnMock.getArguments()[0]); @@ -233,15 +235,15 @@ public void testSaveAllResultsJobSubmitFailedAddCacheResults() throws Exception sendOrder.append(result.hashCode()); return invocationOnMock.callRealMethod(); - }).when(testling).sendResult(any(), any()); + }).when(testling).sendResult(any(), any(), anyBoolean(), any()); - testling.saveAllResults(); + testling.tearDown(Optional.of(testSuite)); //checks assertEquals(sendOrder.toString(), "" + mockedResult2.hashCode() + mockedResult1.hashCode() + newResult.hashCode()); verify(gearmanCacheService).cacheResults(anyList()); verify(gearmanCacheService).getCachedResults(); - verify(exceptionHandler, times(3)).handleException(any(Throwable.class), eq(true)); + verify(exceptionHandler, times(3)).handleException(any(Exception.class)); verify(testling).getGearmanClient(); verify(testling).getGearmanConnection(host, port); verify(gearmanClient).addJobServer(connection); @@ -260,7 +262,9 @@ public void testSaveAllResultsConnectionFailedAddCacheResults() throws Exception when(gearmanCacheService.getCachedResults()).thenReturn(Collections.singletonList(mockedResult1)); NagiosCheckResult newResult = new NagiosCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult); - when(checkResultBuilder.build()).thenReturn(newResult); + TestSuite testSuite = new TestSuiteExampleBuilder() + .buildExample(); + when(checkResultBuilder.build(testSuite)).thenReturn(newResult); doAnswer(invocationOnMock -> { List results = ((List) invocationOnMock.getArguments()[0]); @@ -271,12 +275,12 @@ public void testSaveAllResultsConnectionFailedAddCacheResults() throws Exception return null; }).when(gearmanCacheService).cacheResults(anyList()); - testling.saveAllResults(); + testling.tearDown(Optional.of(testSuite)); //checks verify(gearmanCacheService).cacheResults(anyList()); verify(gearmanCacheService).getCachedResults(); - verify(exceptionHandler).handleException(any(Throwable.class), eq(true)); + verify(exceptionHandler).handleException(any(Exception.class)); verify(testling).getGearmanClient(); verify(testling).getGearmanConnection(host, port); verify(gearmanClient).addJobServer(connection); @@ -289,7 +293,9 @@ public void testSaveAllResultsWrongConnectionCacheResults() throws Exception { when(properties.getServerHost()).thenReturn(host); when(properties.isCacheEnabled()).thenReturn(true); - when(checkResultBuilder.build()).thenReturn(new NagiosCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult)); + TestSuite testSuite = new TestSuiteExampleBuilder() + .buildExample(); + when(checkResultBuilder.build(testSuite)).thenReturn(new NagiosCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult)); doReturn(connection).when(testling).getGearmanConnection(host, port); when(gearmanClient.addJobServer(connection)).thenThrow(new UnresolvedAddressException()); @@ -307,14 +313,14 @@ public void testSaveAllResultsWrongConnectionCacheResults() throws Exception { "Could not transfer Sakuli results to the Gearman server.*"); assertEquals(exception.getSuppressed()[0].getClass(), UnresolvedAddressException.class); return null; - }).when(exceptionHandler).handleException(any(Throwable.class), anyBoolean()); + }).when(exceptionHandler).handleException(any(Exception.class), anyBoolean()); - testling.saveAllResults(); + testling.tearDown(Optional.of(testSuite)); //checks verify(gearmanCacheService).cacheResults(anyList()); verify(gearmanCacheService).getCachedResults(); - verify(exceptionHandler).handleException(any(SakuliForwarderException.class), anyBoolean()); + verify(exceptionHandler).handleException(any(SakuliForwarderRuntimeException.class)); verify(testling).getGearmanClient(); verify(testling).getGearmanConnection(host, port); verify(gearmanClient).addJobServer(connection); diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/gearman/model/NagiosCheckResultTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/gearman/model/NagiosCheckResultTest.java index e01067bc..25ae474d 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/gearman/model/NagiosCheckResultTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/gearman/model/NagiosCheckResultTest.java @@ -56,9 +56,9 @@ public void setUp() throws Exception { @Test public void testGetPayload() throws Exception { String gearmanPayload="test_payload"; - when(outputBuilder.createOutput()).thenReturn(gearmanPayload); + when(outputBuilder.createOutput(testSuite)).thenReturn(gearmanPayload); - NagiosCheckResult checkResult = testling.build(); + NagiosCheckResult checkResult = testling.build(testSuite); Assert.assertEquals(checkResult.getPayload(), gearmanPayload); Assert.assertNotNull(checkResult.getQueueName()); Assert.assertEquals(checkResult.getQueueName(), gearmanProperties.getServerQueue()); diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/gearman/model/builder/NagiosExceptionBuilderTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/gearman/model/builder/NagiosExceptionBuilderTest.java index 9998ae5e..1ebefc1f 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/gearman/model/builder/NagiosExceptionBuilderTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/gearman/model/builder/NagiosExceptionBuilderTest.java @@ -19,7 +19,7 @@ package org.sakuli.services.forwarder.gearman.model.builder; import org.gearman.client.GearmanJobResult; -import org.sakuli.exceptions.SakuliForwarderException; +import org.sakuli.exceptions.SakuliForwarderRuntimeException; import org.testng.annotations.Test; import static org.mockito.Mockito.mock; @@ -30,7 +30,7 @@ public class NagiosExceptionBuilderTest { @Test public void testBuildUnexpectedErrorException() throws Exception { - SakuliForwarderException sakuliForwarderException = NagiosExceptionBuilder.buildUnexpectedErrorException(new Exception("TEST"), "localhost", 4370); + SakuliForwarderRuntimeException sakuliForwarderException = NagiosExceptionBuilder.buildUnexpectedErrorException(new Exception("TEST"), "localhost", 4370); assertEquals(sakuliForwarderException.getMessage(), "unexpected error by sending the results to the gearman forwarder 'localhost:'4370'"); } @@ -40,7 +40,7 @@ public void testBuildTransferException() throws Exception { GearmanJobResult result = mock(GearmanJobResult.class); when(result.getExceptions()).thenReturn("exceptions".getBytes()); when(result.getWarnings()).thenReturn("warnings".getBytes()); - SakuliForwarderException sakuliForwarderException = NagiosExceptionBuilder.buildTransferException("localhost", 4370, result); + SakuliForwarderRuntimeException sakuliForwarderException = NagiosExceptionBuilder.buildTransferException("localhost", 4370, result); assertEquals(sakuliForwarderException.getMessage(), "something went wrong during the transfer of the results to the gearman forwarder 'localhost:'4370':\n" + "EXCEPTIONS:\n" + diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/Icinga2ResultServiceImplTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/Icinga2ResultServiceImplTest.java index a015630b..55693f12 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/Icinga2ResultServiceImplTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/Icinga2ResultServiceImplTest.java @@ -21,7 +21,9 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.sakuli.datamodel.TestSuite; import org.sakuli.exceptions.SakuliExceptionHandler; +import org.sakuli.exceptions.SakuliForwarderException; import org.sakuli.services.forwarder.icinga2.model.Icinga2Request; import org.sakuli.services.forwarder.icinga2.model.Icinga2Result; import org.sakuli.services.forwarder.icinga2.model.builder.Icinga2CheckResultBuilder; @@ -40,12 +42,12 @@ import java.util.HashMap; import java.util.Map; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; /** * @author tschneck - * Date: 2/23/16 + * Date: 2/23/16 */ public class Icinga2ResultServiceImplTest { @Mock @@ -79,12 +81,13 @@ public void testSaveAllResults() throws Exception { result.setResults(Collections.singletonList(map)); mockAndReturn(result); when(icinga2CheckResultBuilder.build()).thenReturn(getRequestExample()); - testling.saveAllResults(); + testling.teardownTestSuite(new TestSuite()); verify(exceptionHandler, never()).handleException(any(Exception.class)); verify(icinga2CheckResultBuilder).build(); } - @Test + @Test(expectedExceptions = SakuliForwarderException.class, + expectedExceptionsMessageRegExp = "Unexpected result of REST-POST.*") public void testSaveAllResultsWrongCode() throws Exception { Icinga2Result result = new Icinga2Result(); Map map = new HashMap<>(); @@ -93,12 +96,11 @@ public void testSaveAllResultsWrongCode() throws Exception { result.setResults(Collections.singletonList(map)); mockAndReturn(result); when(icinga2CheckResultBuilder.build()).thenReturn(getRequestExample()); - testling.saveAllResults(); - verify(exceptionHandler).handleException(any(Exception.class)); - verify(icinga2CheckResultBuilder).build(); + testling.teardownTestSuite(new TestSuite()); } - @Test + @Test(expectedExceptions = SakuliForwarderException.class, + expectedExceptionsMessageRegExp = "Unexpected result of REST-POST.*") public void testSaveAllResultsWrongStatus() throws Exception { Icinga2Result result = new Icinga2Result(); Map map = new HashMap<>(); @@ -107,9 +109,7 @@ public void testSaveAllResultsWrongStatus() throws Exception { result.setResults(Collections.singletonList(map)); when(icinga2CheckResultBuilder.build()).thenReturn(getRequestExample()); mockAndReturn(result); - testling.saveAllResults(); - verify(exceptionHandler).handleException(any(Exception.class)); - verify(icinga2CheckResultBuilder).build(); + testling.teardownTestSuite(new TestSuite()); } @Test diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/Icinga2ResultServiceTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/Icinga2ResultServiceTest.java index 9ce37ba5..5acdf6e8 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/Icinga2ResultServiceTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/Icinga2ResultServiceTest.java @@ -18,6 +18,7 @@ package org.sakuli.services.forwarder.icinga2; +import org.sakuli.datamodel.TestSuite; import org.sakuli.loader.BeanLoader; import org.testng.annotations.Test; @@ -25,7 +26,7 @@ /** * @author tschneck - * Date: 2/23/16 + * Date: 2/23/16 */ public class Icinga2ResultServiceTest extends AbstractIcinga2ForwarderBaseTest { @@ -33,6 +34,6 @@ public class Icinga2ResultServiceTest extends AbstractIcinga2ForwarderBaseTest { expectedExceptionsMessageRegExp = "java.net.UnknownHostException: my-icinga-host") public void testServerNotExist() throws Exception { Icinga2ResultServiceImpl testling = BeanLoader.loadBean(Icinga2ResultServiceImpl.class); - testling.saveAllResults(); + testling.teardownTestSuite(new TestSuite()); } } \ No newline at end of file diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/ManualIcinga2ResultTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/ManualIcinga2ResultTest.java index 89551f08..9038262f 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/ManualIcinga2ResultTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/ManualIcinga2ResultTest.java @@ -21,6 +21,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.sakuli.datamodel.TestSuite; import org.sakuli.services.forwarder.icinga2.model.Icinga2Request; import org.sakuli.services.forwarder.icinga2.model.builder.Icinga2CheckResultBuilder; import org.sakuli.services.forwarder.icinga2.model.builder.Icinga2RequestBuilder; @@ -32,7 +33,7 @@ /** * @author tschneck - * Date: 2/22/16 + * Date: 2/22/16 */ public class ManualIcinga2ResultTest { @@ -57,7 +58,7 @@ public void testSaveAllResults() throws Exception { when(properties.getApiPassword()).thenReturn("rootroot"); when(properties.getApiURL()).thenReturn("https://localhost:5665/v1/actions/process-check-result?service=sakuliclient01!sakuli_demo"); ReflectionTestUtils.setField(testling, "icinga2RestCient", icinga2RestCient); - testling.saveAllResults(); + testling.teardownTestSuite(new TestSuite()); } protected Icinga2Request getRequestExample() { diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/model/builder/Icinga2OutputBuilderTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/model/builder/Icinga2OutputBuilderTest.java index 499ebaa8..94b6353c 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/model/builder/Icinga2OutputBuilderTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/icinga2/model/builder/Icinga2OutputBuilderTest.java @@ -29,7 +29,7 @@ import org.sakuli.datamodel.state.TestCaseState; import org.sakuli.datamodel.state.TestCaseStepState; import org.sakuli.datamodel.state.TestSuiteState; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.services.forwarder.MonitoringPropertiesTestHelper; import org.sakuli.services.forwarder.ScreenshotDivConverter; import org.sakuli.services.forwarder.icinga2.Icinga2Properties; @@ -84,7 +84,7 @@ public void testBuildErrorInSuite() throws Exception { .withId("TEST-CASE-ID") .withState(TestCaseState.OK) .buildExample())) - .withException(new SakuliException("MY-TEST-ERROR-SUITE")) + .withException(new SakuliCheckedException("MY-TEST-ERROR-SUITE")) .buildExample(); ReflectionTestUtils.setField(testling, "testSuite", testSuite); Assert.assertEquals(testling.build(), @@ -100,7 +100,7 @@ public void testBuildErrorInCase() throws Exception { .withTestCases(Collections.singletonList(new TestCaseExampleBuilder() .withId("TEST-CASE-ID") .withState(TestCaseState.ERRORS) - .withException(new SakuliException("MY-TEST-ERROR-CASE")) + .withException(new SakuliCheckedException("MY-TEST-ERROR-CASE")) .buildExample())) .buildExample(); ReflectionTestUtils.setField(testling, "testSuite", testSuite); @@ -117,7 +117,7 @@ public void testBuildErrorInCaseMaxLength() throws Exception { .withTestCases(Collections.singletonList(new TestCaseExampleBuilder() .withId("TEST-CASE-ID") .withState(TestCaseState.ERRORS) - .withException(new SakuliException("MY-TEST-ERROR-CASE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + + .withException(new SakuliCheckedException("MY-TEST-ERROR-CASE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")) .buildExample())) .buildExample(); @@ -140,7 +140,7 @@ public void testBuildErrorInStep() throws Exception { .withTestCaseSteps(Collections.singletonList( new TestCaseStepExampleBuilder() .withState(TestCaseStepState.ERRORS) - .withException(new SakuliException("MY-TEST-ERROR-IN-STEP")) + .withException(new SakuliCheckedException("MY-TEST-ERROR-IN-STEP")) .buildExample() )) .buildExample())) diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/json/JsonResultServiceImplTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/json/JsonResultServiceImplTest.java index a4d6d42c..ad40dcd3 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/json/JsonResultServiceImplTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/json/JsonResultServiceImplTest.java @@ -23,7 +23,7 @@ import org.mockito.MockitoAnnotations; import org.mockito.Spy; import org.sakuli.datamodel.TestSuite; -import org.sakuli.exceptions.SakuliForwarderException; +import org.sakuli.exceptions.SakuliForwarderCheckedException; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -61,10 +61,10 @@ public Object[][] createJsonFilePathDP() { } @Test(dataProvider = "createJsonFilePathDP") - public void createJsonFilePath(String outputJsonDir, String testSuiteId, String expectedFilePathRegEx) throws SakuliForwarderException { + public void createJsonFilePath(String outputJsonDir, String testSuiteId, String expectedFilePathRegEx) throws SakuliForwarderCheckedException { doReturn(testSuiteId).when(testSuite).getId(); doReturn(Paths.get(outputJsonDir)).when(jsonProperties).getOutputJsonDir(); - assertTrue(testling.createJsonFilePath().toString().matches(expectedFilePathRegEx)); + assertTrue(testling.createJsonFilePath(testSuite.getId()).toString().matches(expectedFilePathRegEx)); } } diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/json/serializer/DateSerializerTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/json/serializer/DateSerializerTest.java index c8ec1e37..2b4435b7 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/json/serializer/DateSerializerTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/json/serializer/DateSerializerTest.java @@ -28,8 +28,8 @@ import java.util.Date; import java.util.GregorianCalendar; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; import static org.testng.Assert.assertEquals; diff --git a/src/core/src/test/java/org/sakuli/services/forwarder/json/serializer/ThrowableSerializerTest.java b/src/core/src/test/java/org/sakuli/services/forwarder/json/serializer/ThrowableSerializerTest.java index 563eb2e2..98802951 100644 --- a/src/core/src/test/java/org/sakuli/services/forwarder/json/serializer/ThrowableSerializerTest.java +++ b/src/core/src/test/java/org/sakuli/services/forwarder/json/serializer/ThrowableSerializerTest.java @@ -28,8 +28,8 @@ import java.nio.file.Path; import java.nio.file.Paths; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; import static org.testng.Assert.assertTrue; diff --git a/src/core/src/test/java/org/sakuli/starter/SahiConnectorTest.java b/src/core/src/test/java/org/sakuli/starter/SahiConnectorTest.java index ce5ca4ea..f6f91b49 100644 --- a/src/core/src/test/java/org/sakuli/starter/SahiConnectorTest.java +++ b/src/core/src/test/java/org/sakuli/starter/SahiConnectorTest.java @@ -44,7 +44,7 @@ import java.util.Date; import java.util.stream.Stream; -import static org.mockito.Matchers.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; @@ -82,7 +82,7 @@ public void testGetTestRunner() throws Exception { } @Test - public void testStartSahiTestSuiteFAILURE() throws Throwable { + public void testStartSahiTestSuiteFAILURE() throws Exception { TestRunner testRunnerMock = mock(TestRunner.class); doReturn(testRunnerMock).when(testling).getTestRunner(); doReturn(SAKULI_HOME_FOLDER_PATH).when(testling).getIncludeFolderJsPath(); @@ -98,7 +98,7 @@ public void testStartSahiTestSuiteFAILURE() throws Throwable { } @Test - public void testStartSahiTestSuiteOK() throws Throwable { + public void testStartSahiTestSuiteOK() throws Exception { TestRunner testRunnerMock = mock(TestRunner.class); doReturn(testRunnerMock).when(testling).getTestRunner(); doReturn(SAKULI_HOME_FOLDER_PATH).when(testling).getIncludeFolderJsPath(); @@ -114,7 +114,7 @@ public void testStartSahiTestSuiteOK() throws Throwable { } @Test - public void testStartSahiTestSuiteReconnect() throws Throwable { + public void testStartSahiTestSuiteReconnect() throws Exception { TestRunner testRunnerMock = mock(TestRunner.class); doReturn(testRunnerMock).when(testling).getTestRunner(); when(testRunnerMock.execute()).thenThrow(new ConnectException("TEST")); @@ -147,7 +147,7 @@ public void testGetIncludFolderJsPath() throws Exception { } @Test - public void testReconnectOK() throws Throwable { + public void testReconnectOK() throws Exception { Path pathMock = mock(Path.class); when(sakuliProperties.getJsLibFolder()).thenReturn(pathMock); when(pathMock.toAbsolutePath()).thenReturn(pathMock); @@ -160,7 +160,7 @@ public void testReconnectOK() throws Throwable { } @Test(expectedExceptions = InterruptedException.class) - public void testReconnectFAILURE() throws Throwable { + public void testReconnectFAILURE() throws Exception { Path pathMock = mock(Path.class); when(sakuliProperties.getJsLibFolder()).thenReturn(pathMock); when(pathMock.toAbsolutePath()).thenReturn(pathMock); @@ -185,10 +185,10 @@ public void testIsSahiTimoutException() throws Exception { Pair.of("some other error message", false)).forEach(p -> { String sahiError = p.getLeft(); - Throwable testException = new SakuliExceptionWithScreenshot(new SahiActionException + Exception testException = new SakuliExceptionWithScreenshot(new SahiActionException (new LogResult("error", ResultType.FAILURE, "debug info", sahiError)), null); - Stream.of(testException, testException.getCause(), new SakuliException(sahiError)) + Stream.of(testException, testException.getCause(), new SakuliCheckedException(sahiError)) .forEach(e -> Assert.assertEquals(SahiConnector.isSahiScriptTimout(e), (boolean) p.getRight())); diff --git a/src/core/src/test/java/org/sakuli/utils/LoggerInitializerTest.java b/src/core/src/test/java/org/sakuli/utils/LoggerInitializerTest.java index 5fb1f1c9..5239d85b 100644 --- a/src/core/src/test/java/org/sakuli/utils/LoggerInitializerTest.java +++ b/src/core/src/test/java/org/sakuli/utils/LoggerInitializerTest.java @@ -61,7 +61,7 @@ public void setUp() throws Exception { } @AfterClass - public void cleanUp() throws Throwable { + public void cleanUp() throws Exception { //revert to default config BeanLoader.loadBean(LoggerInitializer.class).initLoggerContext(); deleteFile(Paths.get(logFolder + File.separator + "_sakuli.log")); @@ -69,7 +69,7 @@ public void cleanUp() throws Throwable { } @Test - public void testInitLoggerContextFromIncludeFolder() throws Throwable { + public void testInitLoggerContextFromIncludeFolder() throws Exception { LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger root = context.getLogger("root"); Logger sakuliLogger = context.getLogger("org.sakuli"); @@ -100,7 +100,7 @@ public void testInitLoggerContextFromIncludeFolder() throws Throwable { } @Test(expectedExceptions = LogConfigurationException.class) - public void testInitLoggerContextException() throws Throwable { + public void testInitLoggerContextException() throws Exception { doReturn(null).when(testling).getConfigFileFromClasspath(); doReturn(null).when(testling).getConfigFile(); @@ -114,7 +114,7 @@ public void testGetConfigFileFromClasspath() throws Exception { } @Test - public void testGetConfigFileFromIncludeFolder() throws Throwable { + public void testGetConfigFileFromIncludeFolder() throws Exception { when(sakuliProperties.getConfigFolder()).thenReturn(Paths.get(BaseTest.SAKULI_HOME_FOLDER_PATH + SakuliProperties.CONFIG_FOLDER_APPEDER)); String configFileFromConfigFolder = testling.getConfigFile(); Assert.assertNotNull(configFileFromConfigFolder); diff --git a/src/core/src/test/resources/org/sakuli/services/serviceTest-applicationContext.xml b/src/core/src/test/resources/org/sakuli/services/serviceTest-applicationContext.xml index ecf61531..be1404e6 100644 --- a/src/core/src/test/resources/org/sakuli/services/serviceTest-applicationContext.xml +++ b/src/core/src/test/resources/org/sakuli/services/serviceTest-applicationContext.xml @@ -40,9 +40,7 @@ - - + @@ -62,10 +60,10 @@ - + - + diff --git a/src/integration-test/pom.xml b/src/integration-test/pom.xml index 9793e8ff..7f8ae181 100644 --- a/src/integration-test/pom.xml +++ b/src/integration-test/pom.xml @@ -82,6 +82,7 @@ verify + ${project.build.sourceEncoding} false **/*.class diff --git a/src/integration-test/src/test/java/org/sakuli/integration/dao/DaoIntegrationTest.java b/src/integration-test/src/test/java/org/sakuli/integration/dao/DaoIntegrationTest.java index 17a74907..cb7db402 100644 --- a/src/integration-test/src/test/java/org/sakuli/integration/dao/DaoIntegrationTest.java +++ b/src/integration-test/src/test/java/org/sakuli/integration/dao/DaoIntegrationTest.java @@ -25,7 +25,7 @@ import org.sakuli.datamodel.TestSuite; import org.sakuli.datamodel.actions.LogResult; import org.sakuli.datamodel.properties.SakuliProperties; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.exceptions.SakuliExceptionHandler; import org.sakuli.integration.IntegrationTest; import org.sakuli.integration.builder.TestSuiteBuilder; @@ -41,9 +41,9 @@ import java.net.URL; import java.nio.file.Paths; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyBoolean; -import static org.mockito.Matchers.anyString; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; import static org.sakuli.integration.IntegrationTest.GROUP; @@ -65,7 +65,7 @@ public abstract class DaoIntegrationTest implements IntegrationTe @Mock private SakuliProperties sakuliProperties; - protected abstract D createTestling() throws SakuliException; + protected abstract D createTestling() throws SakuliCheckedException; @BeforeClass public void initTestFolder() { @@ -86,7 +86,7 @@ private String getTestSuiteFolder() { } @BeforeMethod - public void init() throws SakuliException { + public void init() throws SakuliCheckedException { testling = createTestling(); MockitoAnnotations.initMocks(this); testling.setDataSource(dataSource); @@ -95,13 +95,13 @@ public void init() throws SakuliException { protected void initExceptionHandlerMock(SakuliExceptionHandler sakuliExceptionHandlerMock) { doAnswer(invocation -> { - throw (Throwable) invocation.getArguments()[0]; - }).when(sakuliExceptionHandlerMock).handleException(any(Throwable.class)); - doCallRealMethod().when(sakuliExceptionHandlerMock).handleException(any(Throwable.class), anyBoolean()); + throw (Exception) invocation.getArguments()[0]; + }).when(sakuliExceptionHandlerMock).handleException(any(Exception.class)); + doCallRealMethod().when(sakuliExceptionHandlerMock).handleException(any(Exception.class), anyBoolean()); doCallRealMethod().when(sakuliExceptionHandlerMock).handleException(any(LogResult.class)); doCallRealMethod().when(sakuliExceptionHandlerMock).handleException(anyString(), anyBoolean()); doCallRealMethod().when(sakuliExceptionHandlerMock).handleException(anyString(), any(RegionImpl.class), anyBoolean()); - doCallRealMethod().when(sakuliExceptionHandlerMock).handleException(any(Throwable.class), any(RegionImpl.class), anyBoolean()); + doCallRealMethod().when(sakuliExceptionHandlerMock).handleException(any(Exception.class), any(RegionImpl.class), anyBoolean()); } protected void initDeafultTestSuiteMock() { diff --git a/src/integration-test/src/test/java/org/sakuli/integration/dao/DaoTestCaseImplIntegrationTest.java b/src/integration-test/src/test/java/org/sakuli/integration/dao/DaoTestCaseImplIntegrationTest.java index 44e659d0..fd7bd1f9 100644 --- a/src/integration-test/src/test/java/org/sakuli/integration/dao/DaoTestCaseImplIntegrationTest.java +++ b/src/integration-test/src/test/java/org/sakuli/integration/dao/DaoTestCaseImplIntegrationTest.java @@ -18,7 +18,7 @@ package org.sakuli.integration.dao; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.integration.builder.TestCaseBuilder; import org.sakuli.services.forwarder.database.dao.impl.DaoTestCaseImpl; import org.testng.Assert; @@ -28,19 +28,19 @@ /** * @author tschneck - * Date: 25.07.13 + * Date: 25.07.13 */ @Test(groups = GROUP) public class DaoTestCaseImplIntegrationTest extends DaoIntegrationTest { @Override - protected DaoTestCaseImpl createTestling() throws SakuliException { + protected DaoTestCaseImpl createTestling() throws SakuliCheckedException { return new DaoTestCaseImpl(dataSource); } @Test - public void testGetCountOfSahiCases() throws Throwable { + public void testGetCountOfSahiCases() throws Exception { int countOfSahiCases = testling.getCountOfSahiCases(); Assert.assertTrue(countOfSahiCases >= 0); diff --git a/src/integration-test/src/test/java/org/sakuli/integration/dao/DaoTestSuiteImplIntegrationTest.java b/src/integration-test/src/test/java/org/sakuli/integration/dao/DaoTestSuiteImplIntegrationTest.java index c36583c8..b87b8a09 100644 --- a/src/integration-test/src/test/java/org/sakuli/integration/dao/DaoTestSuiteImplIntegrationTest.java +++ b/src/integration-test/src/test/java/org/sakuli/integration/dao/DaoTestSuiteImplIntegrationTest.java @@ -19,7 +19,7 @@ package org.sakuli.integration.dao; import org.sakuli.datamodel.TestSuite; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.integration.builder.TestSuiteBuilder; import org.sakuli.services.forwarder.database.dao.impl.DaoTestSuiteImpl; import org.testng.Assert; @@ -32,13 +32,13 @@ /** * @author tschneck - * Date: 25.07.13 + * Date: 25.07.13 */ @Test(groups = GROUP) public class DaoTestSuiteImplIntegrationTest extends DaoIntegrationTest { @Override - protected DaoTestSuiteImpl createTestling() throws SakuliException { + protected DaoTestSuiteImpl createTestling() throws SakuliCheckedException { return new DaoTestSuiteImpl(dataSource); } @@ -61,7 +61,7 @@ public void testUpdateTestSuiteResult() throws Exception { } @Test - public void testSaveTestSuiteToSahiJobs() throws Throwable { + public void testSaveTestSuiteToSahiJobs() throws Exception { String guid = "JUNIT_123" + TestSuite.GUID_DATE_FORMATE.format(new Date()); when(testSuiteMock.getGuid()).thenReturn(guid); Assert.assertTrue(testSuiteMock.getGuid().contains("JUNIT_123")); @@ -72,8 +72,8 @@ public void testSaveTestSuiteToSahiJobs() throws Throwable { } @Test - public void testGetCountOfSahiJobs() throws Throwable { - Assert.assertNotNull(testling.getCountOfSahiJobs()); + public void testGetCountOfSahiJobs() throws Exception { + Assert.assertTrue(testling.getCountOfSahiJobs() == 0); } } diff --git a/src/integration-test/src/test/java/org/sakuli/integration/services/forwarder/checkmk/CheckMKTemplateOutputBuilderIntegrationTest.java b/src/integration-test/src/test/java/org/sakuli/integration/services/forwarder/checkmk/CheckMKTemplateOutputBuilderIntegrationTest.java index 6ba100a4..cfca426d 100644 --- a/src/integration-test/src/test/java/org/sakuli/integration/services/forwarder/checkmk/CheckMKTemplateOutputBuilderIntegrationTest.java +++ b/src/integration-test/src/test/java/org/sakuli/integration/services/forwarder/checkmk/CheckMKTemplateOutputBuilderIntegrationTest.java @@ -32,12 +32,12 @@ import org.sakuli.datamodel.state.TestCaseState; import org.sakuli.datamodel.state.TestCaseStepState; import org.sakuli.datamodel.state.TestSuiteState; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.integration.IntegrationTest; +import org.sakuli.services.forwarder.ScreenshotDiv; import org.sakuli.services.forwarder.ScreenshotDivConverter; import org.sakuli.services.forwarder.checkmk.CheckMKProperties; import org.sakuli.services.forwarder.checkmk.CheckMKTemplateOutputBuilder; -import org.sakuli.services.forwarder.ScreenshotDiv; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -49,8 +49,8 @@ import java.util.SortedSet; import java.util.TreeSet; -import static org.mockito.Matchers.anyBoolean; -import static org.mockito.Matchers.notNull; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.notNull; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.when; @@ -170,7 +170,7 @@ public void testOK() throws Exception { .buildExample() )); doReturn(testCaseAsSortedSet).when(testSuite).getTestCasesAsSortedSet(); - String output = testling.createOutput(); + String output = testling.createOutput(testSuite); Assert.assertEquals(output, loadExpectedOutput(TestCaseState.OK.name())); } @@ -252,7 +252,7 @@ public void testWarnInStep() throws Exception { .buildExample() )); doReturn(testCaseAsSortedSet).when(testSuite).getTestCasesAsSortedSet(); - String output = testling.createOutput(); + String output = testling.createOutput(testSuite); Assert.assertEquals(output, loadExpectedOutput(TestSuiteState.WARNING_IN_STEP.name())); } @@ -334,7 +334,7 @@ public void testWarnInCase() throws Exception { .buildExample() )); doReturn(testCaseAsSortedSet).when(testSuite).getTestCasesAsSortedSet(); - String output = testling.createOutput(); + String output = testling.createOutput(testSuite); Assert.assertEquals(output, loadExpectedOutput(TestSuiteState.WARNING_IN_CASE.name())); } @@ -416,7 +416,7 @@ public void testCritInCase() throws Exception { .buildExample() )); doReturn(testCaseAsSortedSet).when(testSuite).getTestCasesAsSortedSet(); - String output = testling.createOutput(); + String output = testling.createOutput(testSuite); String expected = loadExpectedOutput(TestSuiteState.CRITICAL_IN_CASE.name()); Assert.assertEquals(output.getBytes(), expected.getBytes()); Assert.assertEquals(output, expected); @@ -500,7 +500,7 @@ public void testWarnInSuite() throws Exception { .buildExample() )); doReturn(testCaseAsSortedSet).when(testSuite).getTestCasesAsSortedSet(); - String output = testling.createOutput(); + String output = testling.createOutput(testSuite); Assert.assertEquals(output, loadExpectedOutput(TestSuiteState.WARNING_IN_SUITE.name())); } @@ -582,7 +582,7 @@ public void testCritInSuite() throws Exception { .buildExample() )); doReturn(testCaseAsSortedSet).when(testSuite).getTestCasesAsSortedSet(); - String output = testling.createOutput(); + String output = testling.createOutput(testSuite); Assert.assertEquals(output, loadExpectedOutput(TestSuiteState.CRITICAL_IN_SUITE.name())); } @@ -646,7 +646,7 @@ public void testException() throws Exception { .withWarningTime(5) .withStartDate(new DateTime(1970, 1, 1, 10, 36, 0).toDate()) .withStopDate(new DateTime(1970, 1, 1, 10, 36, 1, 50).toDate()) - .withException(new SakuliException("_highlight(_link(\"xSL Manager\")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://sahi.example.com/_s_/spr/concat.js line 3607 > eval:1:1 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/concat.js:3607:9 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/sakuli/inject.js:46:12 @http://sahi.example.com/_s_/spr/concat.js:3373:5 Click for browser script")) + .withException(new SakuliCheckedException("_highlight(_link(\"xSL Manager\")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://sahi.example.com/_s_/spr/concat.js line 3607 > eval:1:1 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/concat.js:3607:9 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/sakuli/inject.js:46:12 @http://sahi.example.com/_s_/spr/concat.js:3373:5 Click for browser script")) .buildExample(), new TestCaseStepExampleBuilder() .withState(TestCaseStepState.OK) @@ -671,8 +671,8 @@ public void testException() throws Exception { screenshotDiv.setId("sakuli_screenshot243575009"); screenshotDiv.setFormat("jpg"); screenshotDiv.setBase64screenshot("/9j/4AAQSkZJRgABAgAAAQABAAD9k="); - doReturn(screenshotDiv).when(screenshotDivConverter).convert(notNull(Throwable.class)); - String output = testling.createOutput(); + doReturn(screenshotDiv).when(screenshotDivConverter).convert(notNull(Exception.class)); + String output = testling.createOutput(testSuite); Assert.assertEquals(output, loadExpectedOutput(TestSuiteState.ERRORS.name())); } diff --git a/src/integration-test/src/test/java/org/sakuli/integration/services/forwarder/gearman/GearmanTemplateOutputBuilderIntegrationTest.java b/src/integration-test/src/test/java/org/sakuli/integration/services/forwarder/gearman/GearmanTemplateOutputBuilderIntegrationTest.java index 06f82d65..a65be727 100644 --- a/src/integration-test/src/test/java/org/sakuli/integration/services/forwarder/gearman/GearmanTemplateOutputBuilderIntegrationTest.java +++ b/src/integration-test/src/test/java/org/sakuli/integration/services/forwarder/gearman/GearmanTemplateOutputBuilderIntegrationTest.java @@ -32,7 +32,7 @@ import org.sakuli.datamodel.state.TestCaseState; import org.sakuli.datamodel.state.TestCaseStepState; import org.sakuli.datamodel.state.TestSuiteState; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.integration.IntegrationTest; import org.sakuli.services.forwarder.ScreenshotDiv; import org.sakuli.services.forwarder.ScreenshotDivConverter; @@ -47,7 +47,7 @@ import java.nio.file.Paths; import java.util.Arrays; -import static org.mockito.Matchers.notNull; +import static org.mockito.ArgumentMatchers.notNull; import static org.mockito.Mockito.doReturn; /** @@ -56,19 +56,19 @@ @Test(groups = IntegrationTest.GROUP) public class GearmanTemplateOutputBuilderIntegrationTest extends BaseTest { - private static final String DEFAULT_SERVICE_DESCRIPTION = "service_description"; + private static final String TESTSUITE_ID = "example_xfce"; private static final String DEFAULT_SERVICE_TYPE = "passive"; private static final String DEFAULT_NAGIOS_HOST = "my.nagios.host"; private static final String DEFAULT_NAGIOS_CHECK_COMMMAND = "check_sakuli"; @InjectMocks + @Spy private GearmanTemplateOutputBuilder testling; @Mock private ScreenshotDivConverter screenshotDivConverter; @Mock private GearmanProperties gearmanProperties; - @Spy - private TestSuite testSuite = new TestSuite(); + private TestSuite testSuite; @Mock private SakuliProperties sakuliProperties; @@ -76,13 +76,15 @@ public class GearmanTemplateOutputBuilderIntegrationTest extends BaseTest { public void setUp() throws Exception { MockitoAnnotations.initMocks(this); doReturn(getTemplatePath()).when(sakuliProperties).getForwarderTemplateFolder(); - doReturn(DEFAULT_SERVICE_DESCRIPTION).when(gearmanProperties).getNagiosServiceDescription(); + doReturn(TESTSUITE_ID).when(gearmanProperties).getNagiosServiceDescription(); doReturn(DEFAULT_SERVICE_TYPE).when(gearmanProperties).getServiceType(); doReturn(DEFAULT_NAGIOS_HOST).when(gearmanProperties).getNagiosHost(); doReturn(DEFAULT_NAGIOS_CHECK_COMMMAND).when(gearmanProperties).getNagiosCheckCommand(); - - testSuite.setId("example_xfce"); + testSuite = new TestSuite(); + testSuite.setId(TESTSUITE_ID); testSuite.setTestCases(null); + doReturn(testSuite).when(testling).getCurrentTestSuite(); + doReturn(null).when(testling).getCurrentTestCase(); } private String getTemplatePath() { @@ -95,7 +97,7 @@ private String getOutputPath() { } private String loadExpectedOutput(String testCaseName) throws IOException { - return FileUtils.readFileToString(Paths.get(getOutputPath() + File.separator + "TestCase_" + testCaseName + ".txt").toFile()); + return FileUtils.readFileToString(Paths.get(getOutputPath() + File.separator + "TestSuite_" + testCaseName + ".txt").toFile()); } @Test @@ -174,7 +176,7 @@ public void testOK() throws Exception { ) .buildExample() ); - String output = testling.createOutput(); + String output = testling.createOutput(testSuite); Assert.assertEquals(output, loadExpectedOutput(TestCaseState.OK.name())); } @@ -254,7 +256,7 @@ public void testWarnInStep() throws Exception { ) .buildExample() ); - String output = testling.createOutput(); + String output = testling.createOutput(testSuite); Assert.assertEquals(output, loadExpectedOutput(TestSuiteState.WARNING_IN_STEP.name())); } @@ -335,7 +337,7 @@ public void testWarnInCase() throws Exception { ) .buildExample() ); - String output = testling.createOutput(); + String output = testling.createOutput(testSuite); Assert.assertEquals(output, loadExpectedOutput(TestSuiteState.WARNING_IN_CASE.name())); } @@ -416,7 +418,7 @@ public void testCritInCase() throws Exception { ) .buildExample() ); - String output = testling.createOutput(); + String output = testling.createOutput(testSuite); String expected = loadExpectedOutput(TestSuiteState.CRITICAL_IN_CASE.name()); Assert.assertEquals(output, expected); Assert.assertEquals(output.getBytes(), expected.getBytes()); @@ -499,7 +501,7 @@ public void testWarnInSuite() throws Exception { ) .buildExample() ); - String output = testling.createOutput(); + String output = testling.createOutput(testSuite); Assert.assertEquals(output, loadExpectedOutput(TestSuiteState.WARNING_IN_SUITE.name())); } @@ -580,7 +582,7 @@ public void testCritInSuite() throws Exception { ) .buildExample() ); - String output = testling.createOutput(); + String output = testling.createOutput(testSuite); Assert.assertEquals(output, loadExpectedOutput(TestSuiteState.CRITICAL_IN_SUITE.name())); } @@ -642,7 +644,7 @@ public void testException() throws Exception { .withWarningTime(5) .withStartDate(new DateTime(1970, 1, 1, 10, 36, 0).toDate()) .withStopDate(new DateTime(1970, 1, 1, 10, 36, 1, 50).toDate()) - .withException(new SakuliException("_highlight(_link(\"xSL Manager\")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://sahi.example.com/_s_/spr/concat.js line 3607 > eval:1:1 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/concat.js:3607:9 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/sakuli/inject.js:46:12 @http://sahi.example.com/_s_/spr/concat.js:3373:5 Click for browser script")) + .withException(new SakuliCheckedException("_highlight(_link(\"xSL Manager\")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://sahi.example.com/_s_/spr/concat.js line 3607 > eval:1:1 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/concat.js:3607:9 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/sakuli/inject.js:46:12 @http://sahi.example.com/_s_/spr/concat.js:3373:5 Click for browser script")) .buildExample(), new TestCaseStepExampleBuilder() .withState(TestCaseStepState.OK) @@ -666,8 +668,8 @@ public void testException() throws Exception { screenshotDiv.setId("sakuli_screenshot243575009"); screenshotDiv.setFormat("jpg"); screenshotDiv.setBase64screenshot("/9j/4AAQSkZJRgABAgAAAQABAAD9k="); - doReturn(screenshotDiv).when(screenshotDivConverter).convert(notNull(Throwable.class)); - String output = testling.createOutput(); + doReturn(screenshotDiv).when(screenshotDivConverter).convert(notNull(Exception.class)); + String output = testling.createOutput(testSuite); Assert.assertEquals(output, loadExpectedOutput(TestSuiteState.ERRORS.name())); } } diff --git a/src/integration-test/src/test/java/org/sakuli/integration/services/forwarder/gearman/GearmanTestCaseTemplateOutputBuilderIntegrationTest.java b/src/integration-test/src/test/java/org/sakuli/integration/services/forwarder/gearman/GearmanTestCaseTemplateOutputBuilderIntegrationTest.java new file mode 100644 index 00000000..6122de9c --- /dev/null +++ b/src/integration-test/src/test/java/org/sakuli/integration/services/forwarder/gearman/GearmanTestCaseTemplateOutputBuilderIntegrationTest.java @@ -0,0 +1,347 @@ +/* + * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. + * + * Copyright 2013 - 2018 the original author or authors. + * + * 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 org.sakuli.integration.services.forwarder.gearman; + +import org.apache.commons.io.FileUtils; +import org.joda.time.DateTime; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.sakuli.BaseTest; +import org.sakuli.builder.TestCaseExampleBuilder; +import org.sakuli.builder.TestCaseStepExampleBuilder; +import org.sakuli.datamodel.TestCase; +import org.sakuli.datamodel.TestSuite; +import org.sakuli.datamodel.properties.SakuliProperties; +import org.sakuli.datamodel.state.TestCaseState; +import org.sakuli.datamodel.state.TestCaseStepState; +import org.sakuli.exceptions.SakuliCheckedException; +import org.sakuli.integration.IntegrationTest; +import org.sakuli.services.forwarder.ScreenshotDiv; +import org.sakuli.services.forwarder.ScreenshotDivConverter; +import org.sakuli.services.forwarder.gearman.GearmanProperties; +import org.sakuli.services.forwarder.gearman.GearmanTemplateOutputBuilder; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.Arrays; + +import static org.mockito.ArgumentMatchers.notNull; +import static org.mockito.Mockito.doReturn; + +/** + * @author Georgi Todorov + */ +@Test(groups = IntegrationTest.GROUP) +public class GearmanTestCaseTemplateOutputBuilderIntegrationTest extends BaseTest { + + private static final String TESTSUITE_ID = "example_xfce"; + private static final String DEFAULT_SERVICE_TYPE = "passive"; + private static final String DEFAULT_NAGIOS_HOST = "my.nagios.host"; + private static final String DEFAULT_NAGIOS_CHECK_COMMMAND = "check_sakuli"; + + @InjectMocks + @Spy + private GearmanTemplateOutputBuilder testling; + @Mock + private ScreenshotDivConverter screenshotDivConverter; + @Mock + private GearmanProperties gearmanProperties; + @Mock + private SakuliProperties sakuliProperties; + + @BeforeMethod + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + doReturn(getTemplatePath()).when(sakuliProperties).getForwarderTemplateFolder(); + doReturn(TESTSUITE_ID).when(gearmanProperties).getNagiosServiceDescription(); + doReturn(DEFAULT_SERVICE_TYPE).when(gearmanProperties).getServiceType(); + doReturn(DEFAULT_NAGIOS_HOST).when(gearmanProperties).getNagiosHost(); + doReturn(DEFAULT_NAGIOS_CHECK_COMMMAND).when(gearmanProperties).getNagiosCheckCommand(); + + TestSuite testSuite = new TestSuite(); + testSuite.setId(TESTSUITE_ID); + doReturn(testSuite).when(testling).getCurrentTestSuite(); + doReturn(null).when(testling).getCurrentTestCase(); + } + + private String getTemplatePath() { + // If not available execute test via `mvn test-compile` to extract the file dependencies + return getResource("common/config/templates"); + } + + private String getOutputPath() { + return getResource("output", this.getClass()); + } + + private String loadExpectedOutput(String testCaseName) throws IOException { + return FileUtils.readFileToString(Paths.get(getOutputPath() + File.separator + "TestCase_" + testCaseName + ".txt").toFile()); + } + + @Test + public void testOK() throws Exception { + TestCase testCase = new TestCaseExampleBuilder() + .withState(TestCaseState.OK) + .withWarningTime(20) + .withCriticalTime(30) + .withStartDate(new DateTime(1970, 1, 1, 10, 30, 0).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 30, 14, 20).toDate()) + .withId("case1") + .withTestCaseSteps( + Arrays.asList( + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Test_Sahi_landing_page") + .withWarningTime(5) + .withStartDate(new DateTime(1970, 1, 1, 10, 30, 0).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 30, 1, 160).toDate()) + .buildExample(), + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Calculation") + .withWarningTime(10) + .withStartDate(new DateTime(1970, 1, 1, 10, 30, 0, 10).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 30, 7, 290).toDate()) + .buildExample(), + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Editor") + .withWarningTime(10) + .withStartDate(new DateTime(1970, 1, 1, 10, 30, 0, 20).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 30, 1, 500).toDate()) + .buildExample() + ) + ) + .buildExample(); + String output = testling.createOutput(testCase); + Assert.assertEquals(output, loadExpectedOutput(TestCaseState.OK.name())); + } + + @Test + public void testWarnInStep() throws Exception { + TestCase testCase = new TestCaseExampleBuilder() + .withState(TestCaseState.WARNING_IN_STEP) + .withWarningTime(20) + .withCriticalTime(30) + .withStartDate(new DateTime(1970, 1, 1, 10, 31, 20).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 31, 33, 430).toDate()) + .withId("case2") + .withTestCaseSteps( + Arrays.asList( + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Test_Sahi_landing_page_(case2)") + .withWarningTime(5) + .withStartDate(new DateTime(1970, 1, 1, 10, 31, 0, 10).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 31, 0, 930).toDate()) + .buildExample(), + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.WARNING) + .withName("Calculation_(case2)") + .withWarningTime(1) + .withStartDate(new DateTime(1970, 1, 1, 10, 31, 0, 20).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 31, 7, 20).toDate()) + .buildExample(), + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Editor_(case2)") + .withWarningTime(10) + .withStartDate(new DateTime(1970, 1, 1, 10, 31, 0, 30).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 31, 1, 420).toDate()) + .buildExample() + ) + ) + .buildExample(); + String output = testling.createOutput(testCase); + Assert.assertEquals(output, loadExpectedOutput(TestCaseState.WARNING_IN_STEP.name())); + } + + @Test + public void testCritInStep() throws Exception { + TestCase testCase = new TestCaseExampleBuilder() + .withState(TestCaseState.CRITICAL_IN_STEP) + .withWarningTime(20) + .withCriticalTime(30) + .withStartDate(new DateTime(1970, 1, 1, 10, 31, 20).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 31, 33, 430).toDate()) + .withId("case2") + .withTestCaseSteps( + Arrays.asList( + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Test_Sahi_landing_page_(case2)") + .withWarningTime(5) + .withCriticalTime(10) + .withStartDate(new DateTime(1970, 1, 1, 10, 31, 0, 10).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 31, 0, 930).toDate()) + .buildExample(), + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.WARNING) + .withName("Calculation_(case2)") + .withWarningTime(1) + .withCriticalTime(2) + .withStartDate(new DateTime(1970, 1, 1, 10, 31, 0, 20).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 31, 7, 20).toDate()) + .buildExample(), + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Editor_(case2)") + .withWarningTime(10) + .withCriticalTime(20) + .withStartDate(new DateTime(1970, 1, 1, 10, 31, 0, 30).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 31, 1, 420).toDate()) + .buildExample() + ) + ) + .buildExample(); + String output = testling.createOutput(testCase); + Assert.assertEquals(output, loadExpectedOutput(TestCaseState.CRITICAL_IN_STEP.name())); + } + + @Test + public void testWarnInCase() throws Exception { + TestCase testCase = new TestCaseExampleBuilder() + .withState(TestCaseState.WARNING) + .withWarningTime(2) + .withCriticalTime(30) + .withStartDate(new DateTime(1970, 1, 1, 10, 34, 20).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 34, 33, 540).toDate()) + .withId("case2") + .withTestCaseSteps( + Arrays.asList( + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Test_Sahi_landing_page_(case2)") + .withWarningTime(5) + .withStartDate(new DateTime(1970, 1, 1, 10, 34, 0, 10).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 34, 0, 940).toDate()) + .buildExample(), + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Calculation_(case2)") + .withWarningTime(10) + .withStartDate(new DateTime(1970, 1, 1, 10, 34, 0, 20).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 34, 7, 140).toDate()) + .buildExample(), + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Editor_(case2)") + .withWarningTime(10) + .withStartDate(new DateTime(1970, 1, 1, 10, 34, 0, 30).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 34, 1, 390).toDate()) + .buildExample() + ) + ) + .buildExample(); + String output = testling.createOutput(testCase); + Assert.assertEquals(output, loadExpectedOutput(TestCaseState.WARNING.name())); + } + + @Test + public void testCritInCase() throws Exception { + TestCase testCase = new TestCaseExampleBuilder() + .withState(TestCaseState.CRITICAL) + .withWarningTime(2) + .withCriticalTime(3) + .withStartDate(new DateTime(1970, 1, 1, 10, 35, 20).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 35, 33, 700).toDate()) + .withId("case2") + .withTestCaseSteps( + Arrays.asList( + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Test_Sahi_landing_page_(case2)") + .withWarningTime(5) + .withStartDate(new DateTime(1970, 1, 1, 10, 35, 0).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 35, 1, 80).toDate()) + .buildExample(), + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Calculation_(case2)") + .withWarningTime(10) + .withStartDate(new DateTime(1970, 1, 1, 10, 35, 0, 10).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 35, 7, 120).toDate()) + .buildExample(), + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Editor_(case2)") + .withWarningTime(10) + .withStartDate(new DateTime(1970, 1, 1, 10, 35, 0, 20).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 35, 1, 440).toDate()) + .buildExample() + ) + ) + .buildExample(); + String output = testling.createOutput(testCase); + String expected = loadExpectedOutput(TestCaseState.CRITICAL.name()); + Assert.assertEquals(output, expected); + Assert.assertEquals(output.getBytes(), expected.getBytes()); + } + + @Test + public void testException() throws Exception { + TestCase testCase = new TestCaseExampleBuilder() + .withState(TestCaseState.ERRORS) + .withWarningTime(20) + .withCriticalTime(30) + .withStartDate(new DateTime(1970, 1, 1, 10, 36, 10).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 36, 23, 550).toDate()) + .withId("case2") + .withTestCaseSteps( + Arrays.asList( + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.ERRORS) + .withName("Test_Sahi_landing_page_(case2)") + .withWarningTime(5) + .withStartDate(new DateTime(1970, 1, 1, 10, 36, 0).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 36, 1, 50).toDate()) + .withException(new SakuliCheckedException("_highlight(_link(\"xSL Manager\")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://sahi.example.com/_s_/spr/concat.js line 3607 > eval:1:1 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/concat.js:3607:9 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/sakuli/inject.js:46:12 @http://sahi.example.com/_s_/spr/concat.js:3373:5 Click for browser script")) + .buildExample(), + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Calculation_(case2)") + .withWarningTime(10) + .withStartDate(new DateTime(1970, 1, 1, 10, 36, 10).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 36, 17, 30).toDate()) + .buildExample(), + new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Editor_(case2)") + .withWarningTime(10) + .withStartDate(new DateTime(1970, 1, 1, 10, 36, 20).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 36, 21, 390).toDate()) + .buildExample() + ) + ) + .buildExample(); + ScreenshotDiv screenshotDiv = new ScreenshotDiv(); + screenshotDiv.setId("sakuli_screenshot243575009"); + screenshotDiv.setFormat("jpg"); + screenshotDiv.setBase64screenshot("/9j/4AAQSkZJRgABAgAAAQABAAD9k="); + doReturn(screenshotDiv).when(screenshotDivConverter).convert(notNull(Exception.class)); + String output = testling.createOutput(testCase); + Assert.assertEquals(output, loadExpectedOutput(TestCaseState.ERRORS.name())); + } + +} diff --git a/src/integration-test/src/test/java/org/sakuli/integration/services/forwarder/gearman/GearmanTestStepTemplateOutputBuilderIntegrationTest.java b/src/integration-test/src/test/java/org/sakuli/integration/services/forwarder/gearman/GearmanTestStepTemplateOutputBuilderIntegrationTest.java new file mode 100644 index 00000000..8df3feb4 --- /dev/null +++ b/src/integration-test/src/test/java/org/sakuli/integration/services/forwarder/gearman/GearmanTestStepTemplateOutputBuilderIntegrationTest.java @@ -0,0 +1,162 @@ +/* + * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. + * + * Copyright 2013 - 2018 the original author or authors. + * + * 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 org.sakuli.integration.services.forwarder.gearman; + +import org.apache.commons.io.FileUtils; +import org.joda.time.DateTime; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; +import org.sakuli.BaseTest; +import org.sakuli.builder.TestCaseExampleBuilder; +import org.sakuli.builder.TestCaseStepExampleBuilder; +import org.sakuli.datamodel.TestCaseStep; +import org.sakuli.datamodel.TestSuite; +import org.sakuli.datamodel.properties.SakuliProperties; +import org.sakuli.datamodel.state.TestCaseStepState; +import org.sakuli.exceptions.SakuliCheckedException; +import org.sakuli.integration.IntegrationTest; +import org.sakuli.services.forwarder.ScreenshotDiv; +import org.sakuli.services.forwarder.ScreenshotDivConverter; +import org.sakuli.services.forwarder.gearman.GearmanProperties; +import org.sakuli.services.forwarder.gearman.GearmanTemplateOutputBuilder; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; + +import static org.mockito.ArgumentMatchers.notNull; +import static org.mockito.Mockito.doReturn; + +/** + * @author Georgi Todorov + */ +@Test(groups = IntegrationTest.GROUP) +public class GearmanTestStepTemplateOutputBuilderIntegrationTest extends BaseTest { + + private static final String TESTSUITE_ID = "example_xfce"; + private static final String DEFAULT_SERVICE_TYPE = "passive"; + private static final String DEFAULT_NAGIOS_HOST = "my.nagios.host"; + private static final String DEFAULT_NAGIOS_CHECK_COMMMAND = "check_sakuli"; + + @InjectMocks + @Spy + private GearmanTemplateOutputBuilder testling; + @Mock + private ScreenshotDivConverter screenshotDivConverter; + @Mock + private GearmanProperties gearmanProperties; + @Mock + private SakuliProperties sakuliProperties; + + @BeforeMethod + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + doReturn(getTemplatePath()).when(sakuliProperties).getForwarderTemplateFolder(); + doReturn(TESTSUITE_ID).when(gearmanProperties).getNagiosServiceDescription(); + doReturn(DEFAULT_SERVICE_TYPE).when(gearmanProperties).getServiceType(); + doReturn(DEFAULT_NAGIOS_HOST).when(gearmanProperties).getNagiosHost(); + doReturn(DEFAULT_NAGIOS_CHECK_COMMMAND).when(gearmanProperties).getNagiosCheckCommand(); + + TestSuite testSuite = new TestSuite(); + testSuite.setId(TESTSUITE_ID); + doReturn(testSuite).when(testling).getCurrentTestSuite(); + doReturn(new TestCaseExampleBuilder().withId("case2").buildExample()) + .when(testling).getCurrentTestCase(); + } + + private String getTemplatePath() { + // If not available execute test via `mvn test-compile` to extract the file dependencies + return getResource("common/config/templates"); + } + + private String getOutputPath() { + return getResource("output", this.getClass()); + } + + private String loadExpectedOutput(String testCaseName) throws IOException { + return FileUtils.readFileToString(Paths.get(getOutputPath() + File.separator + "TestStep_" + testCaseName + ".txt").toFile()); + } + + @Test + public void testOK() throws Exception { + TestCaseStep testStep = new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.OK) + .withName("Calculation") + .withWarningTime(10) + .withWarningTime(20) + .withStartDate(new DateTime(1970, 1, 1, 10, 30, 0, 10).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 30, 7, 290).toDate()) + .buildExample(); + String output = testling.createOutput(testStep); + Assert.assertEquals(output, loadExpectedOutput(TestCaseStepState.OK.name())); + } + + @Test + public void testWarnInStep() throws Exception { + TestCaseStep testStep = new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.WARNING) + .withName("Calculation") + .withWarningTime(1) + .withCriticalTime(10) + .withStartDate(new DateTime(1970, 1, 1, 10, 31, 0, 20).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 31, 7, 20).toDate()) + .buildExample(); + String output = testling.createOutput(testStep); + Assert.assertEquals(output, loadExpectedOutput(TestCaseStepState.WARNING.name())); + } + + @Test + public void testCritInStep() throws Exception { + TestCaseStep testStep = new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.WARNING) + .withName("Calculation") + .withWarningTime(1) + .withCriticalTime(2) + .withStartDate(new DateTime(1970, 1, 1, 10, 31, 0, 20).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 31, 7, 20).toDate()) + .buildExample(); + String output = testling.createOutput(testStep); + Assert.assertEquals(output, loadExpectedOutput(TestCaseStepState.CRITICAL.name())); + } + + @Test + public void testException() throws Exception { + TestCaseStep testStep = new TestCaseStepExampleBuilder() + .withState(TestCaseStepState.ERRORS) + .withName("Test_Sahi_landing_page") + .withWarningTime(5) + .withWarningTime(10) + .withStartDate(new DateTime(1970, 1, 1, 10, 36, 0).toDate()) + .withStopDate(new DateTime(1970, 1, 1, 10, 36, 1, 50).toDate()) + .withException(new SakuliCheckedException("_highlight(_link(\"xSL Manager\")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://sahi.example.com/_s_/spr/concat.js line 3607 > eval:1:1 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/concat.js:3607:9 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/sakuli/inject.js:46:12 @http://sahi.example.com/_s_/spr/concat.js:3373:5 Click for browser script")) + .buildExample(); + ScreenshotDiv screenshotDiv = new ScreenshotDiv(); + screenshotDiv.setId("sakuli_screenshot243575009"); + screenshotDiv.setFormat("jpg"); + screenshotDiv.setBase64screenshot("/9j/4AAQSkZJRgABAgAAAQABAAD9k="); + doReturn(screenshotDiv).when(screenshotDivConverter).convert(notNull(Exception.class)); + String output = testling.createOutput(testStep); + Assert.assertEquals(output, loadExpectedOutput(TestCaseStepState.ERRORS.name())); + } +} diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_CRITICAL.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_CRITICAL.txt new file mode 100644 index 00000000..55d0bd93 --- /dev/null +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_CRITICAL.txt @@ -0,0 +1,7 @@ +type=passive +host_name=my.nagios.host +start_time=34520.000 +finish_time=34533.700 +return_code=2 +service_description=example_xfce_case2 +output=[CRIT] Case "case2" of Sakuli suite "example_xfce" critical case "case2" over runtime (13.70s/crit at 3s). (Last suite run: 01.01.70 10:35:33)\n
    [CRIT] Case "case2" of Sakuli suite "example_xfce" critical (13.70s/crit at 3s) . (Last suite run: 01.01.70 10:35:33)
    [OK] step "Test_Sahi_landing_page_(case2)" ran in 1.08s - ok
    [OK] step "Calculation_(case2)" ran in 7.11s - ok
    [OK] step "Editor_(case2)" ran in 1.42s - ok
    | suite_example_xfce=0;;;; graph_type_case=0;;;; c_001__state=2;;;; c_001__warning=2s;;;; c_001__critical=3s;;;; c_001_case2=13.70s;2;3;; s_001_001_Test_Sahi_landing_page_(case2)=1.08s;5;;; s_001_002_Calculation_(case2)=7.11s;10;;; s_001_003_Editor_(case2)=1.42s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_CRITICAL_IN_CASE.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_CRITICAL_IN_CASE.txt deleted file mode 100644 index d40ef311..00000000 --- a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_CRITICAL_IN_CASE.txt +++ /dev/null @@ -1,7 +0,0 @@ -type=passive -host_name=my.nagios.host -start_time=34500.000 -finish_time=34546.960 -return_code=2 -service_description=service_description -output=[CRIT] Sakuli suite "example_xfce" critical in case, case "case2" over runtime (13.70s/crit at 3s). (Last suite run: 01.01.70 10:35:46)\\n
    [CRIT] Sakuli suite "example_xfce" critical in case , case "case2" over runtime (13.70s/crit at 3s). (Last suite run: 01.01.70 10:35:46)
    [OK] case "case1" ran in 14.13s - ok
    [CRIT] case "case2" EXCEPTION:
    |suite__state=2;;;; suite__warning=300s;;;; suite__critical=400s;;;; suite_example_xfce=46.96s;300;400;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=14.13s;20;30;; s_001_001_Test_Sahi_landing_page=1.28s;5;;; s_001_002_Calculation=7.30s;10;;; s_001_003_Editor=1.45s;10;;; c_002__state=2;;;; c_002__warning=2s;;;; c_002__critical=3s;;;; c_002_case2=13.70s;2;3;; s_002_001_Test_Sahi_landing_page_(case2)=1.08s;5;;; s_002_002_Calculation_(case2)=7.11s;10;;; s_002_003_Editor_(case2)=1.42s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_CRITICAL_IN_STEP.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_CRITICAL_IN_STEP.txt new file mode 100644 index 00000000..7df67ef4 --- /dev/null +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_CRITICAL_IN_STEP.txt @@ -0,0 +1,7 @@ +type=passive +host_name=my.nagios.host +start_time=34280.000 +finish_time=34293.430 +return_code=2 +service_description=example_xfce_case2 +output=[CRIT] Case "case2" of Sakuli suite "example_xfce" critical in step. (Last suite run: 01.01.70 10:31:33)\n
    [CRIT] Case "case2" of Sakuli suite "example_xfce" critical in step , step "Calculation_(case2)" over runtime (7.00s/crit at 2s). (Last suite run: 01.01.70 10:31:33)
    [OK] step "Test_Sahi_landing_page_(case2)" ran in 0.92s - ok
    [WARN] step "Calculation_(case2)" over runtime (7.00s/warn at 1s)
    [OK] step "Editor_(case2)" ran in 1.39s - ok
    | suite_example_xfce=0;;;; graph_type_case=0;;;; c_001__state=2;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case2=13.43s;20;30;; s_001_001_Test_Sahi_landing_page_(case2)=0.92s;5;;; s_001_002_Calculation_(case2)=7.00s;1;;; s_001_003_Editor_(case2)=1.39s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_CRITICAL_IN_SUITE.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_CRITICAL_IN_SUITE.txt deleted file mode 100644 index c86ab421..00000000 --- a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_CRITICAL_IN_SUITE.txt +++ /dev/null @@ -1,7 +0,0 @@ -type=passive -host_name=my.nagios.host -start_time=34380.000 -finish_time=34424.810 -return_code=2 -service_description=service_description -output=[CRIT] Sakuli suite "example_xfce" critical (44.81s/crit at 40s). (Last suite run: 01.01.70 10:33:44)\\n
    [CRIT] Sakuli suite "example_xfce" critical (44.81s/crit at 40s) . (Last suite run: 01.01.70 10:33:44)
    [OK] case "case1" ran in 13.91s - ok
    [OK] case "case2" ran in 13.55s - ok
    |suite__state=2;;;; suite__warning=30s;;;; suite__critical=40s;;;; suite_example_xfce=44.81s;30;40;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=13.91s;20;30;; s_001_001_Test_Sahi_landing_page=1.16s;5;;; s_001_002_Calculation=7.24s;10;;; s_001_003_Editor=1.43s;10;;; c_002__state=0;;;; c_002__warning=20s;;;; c_002__critical=30s;;;; c_002_case2=13.55s;20;30;; s_002_001_Test_Sahi_landing_page_(case2)=1.05s;5;;; s_002_002_Calculation_(case2)=7.03s;10;;; s_002_003_Editor_(case2)=1.39s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_ERRORS.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_ERRORS.txt index cf83d6e2..6c393644 100644 --- a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_ERRORS.txt +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_ERRORS.txt @@ -1,7 +1,7 @@ type=passive host_name=my.nagios.host -start_time=34560.000 -finish_time=34604.800 +start_time=34570.000 +finish_time=34583.550 return_code=2 -service_description=service_description -output=[CRIT] Sakuli suite "example_xfce" (44.80s) EXCEPTION: CASE "case2": STEP "Test_Sahi_landing_page_(case2)": _highlight(_link("xSL Manager")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://...\\n
    [CRIT] Sakuli suite "example_xfce" (44.80s) EXCEPTION: CASE "case2": STEP "Test_Sahi_landing_page_(case2)": _highlight(_link("xSL Manager")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://sahi.example.com/_s_/spr/concat.js line 3607 > eval:1:1 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/concat.js:3607:9 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/sakuli/inject.js:46:12 @http://sahi.example.com/_s_/spr/concat.js:3373:5 Click for browser script. . (Last suite run: 01.01.70 10:36:44)
    [OK] case "case1" ran in 14.20s - ok
    [CRIT] case "case2" EXCEPTION: STEP "Test_Sahi_landing_page_(case2)": _highlight(_link("xSL Manager")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://sahi.example.com/_s_/spr/concat.js line 3607 > eval:1:1 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/concat.js:3607:9 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/sakuli/inject.js:46:12 @http://sahi.example.com/_s_/spr/concat.js:3373:5 Click for browser script
    |suite__state=2;;;; suite__warning=300s;;;; suite__critical=400s;;;; suite_example_xfce=U;300;400;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=14.20s;20;30;; s_001_001_Test_Sahi_landing_page=1.14s;5;;; s_001_002_Calculation=7.53s;10;;; s_001_003_Editor=1.45s;10;;; c_002__state=2;;;; c_002__warning=20s;;;; c_002__critical=30s;;;; c_002_case2=U;20;30;; s_002_001_Test_Sahi_landing_page_(case2)=U;5;;; s_002_002_Calculation_(case2)=7.03s;10;;; s_002_003_Editor_(case2)=1.39s;10;;; [check_sakuli] +service_description=example_xfce_case2 +output=[CRIT] Case "case2" of Sakuli suite "example_xfce" (13.55s) EXCEPTION: STEP "Test_Sahi_landing_page_(case2)": _highlight(_link("xSL Manager")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://sahi.example.c...\n
    [CRIT] Case "case2" of Sakuli suite "example_xfce" (13.55s) EXCEPTION: . . (Last suite run: 01.01.70 10:36:23)
    [CRIT] step "Test_Sahi_landing_page_(case2)" EXCEPTION: _highlight(_link("xSL Manager")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://sahi.example.com/_s_/spr/concat.js line 3607 > eval:1:1 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/concat.js:3607:9 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/sakuli/inject.js:46:12 @http://sahi.example.com/_s_/spr/concat.js:3373:5 Click for browser script
    [OK] step "Calculation_(case2)" ran in 7.03s - ok
    [OK] step "Editor_(case2)" ran in 1.39s - ok
    | suite_example_xfce=0;;;; graph_type_case=0;;;; c_001__state=2;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case2=U;20;30;; s_001_001_Test_Sahi_landing_page_(case2)=U;5;;; s_001_002_Calculation_(case2)=7.03s;10;;; s_001_003_Editor_(case2)=1.39s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_OK.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_OK.txt index d274b8eb..765a1c50 100644 --- a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_OK.txt +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_OK.txt @@ -1,7 +1,7 @@ type=passive host_name=my.nagios.host start_time=34200.000 -finish_time=34244.990 +finish_time=34214.020 return_code=0 -service_description=service_description -output=[OK] Sakuli suite "example_xfce" ok (44.99s). (Last suite run: 01.01.70 10:30:44)\\n
    [OK] Sakuli suite "example_xfce" ok (44.99s) . (Last suite run: 01.01.70 10:30:44)
    [OK] case "case1" ran in 14.02s - ok
    [OK] case "case2" ran in 13.58s - ok
    |suite__state=0;;;; suite__warning=300s;;;; suite__critical=400s;;;; suite_example_xfce=44.99s;300;400;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=14.02s;20;30;; s_001_001_Test_Sahi_landing_page=1.16s;5;;; s_001_002_Calculation=7.28s;10;;; s_001_003_Editor=1.48s;10;;; c_002__state=0;;;; c_002__warning=20s;;;; c_002__critical=30s;;;; c_002_case2=13.58s;20;30;; s_002_001_Test_Sahi_landing_page_(case2)=1.03s;5;;; s_002_002_Calculation_(case2)=7.07s;10;;; s_002_003_Editor_(case2)=1.37s;10;;; [check_sakuli] +service_description=example_xfce_case1 +output=[OK] Case "case1" of Sakuli suite "example_xfce" ok (14.02s). (Last suite run: 01.01.70 10:30:14)\n
    [OK] Case "case1" of Sakuli suite "example_xfce" ok (14.02s) . (Last suite run: 01.01.70 10:30:14)
    [OK] step "Test_Sahi_landing_page" ran in 1.16s - ok
    [OK] step "Calculation" ran in 7.28s - ok
    [OK] step "Editor" ran in 1.48s - ok
    | suite_example_xfce=0;;;; graph_type_case=0;;;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=14.02s;20;30;; s_001_001_Test_Sahi_landing_page=1.16s;5;;; s_001_002_Calculation=7.28s;10;;; s_001_003_Editor=1.48s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_WARNING.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_WARNING.txt new file mode 100644 index 00000000..7a38060e --- /dev/null +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_WARNING.txt @@ -0,0 +1,7 @@ +type=passive +host_name=my.nagios.host +start_time=34460.000 +finish_time=34473.540 +return_code=1 +service_description=example_xfce_case2 +output=[WARN] Case "case2" of Sakuli suite "example_xfce" warning case "case2" over runtime (13.54s/warn at 2s). (Last suite run: 01.01.70 10:34:33)\n
    [WARN] Case "case2" of Sakuli suite "example_xfce" warning (13.54s/warn at 2s) . (Last suite run: 01.01.70 10:34:33)
    [OK] step "Test_Sahi_landing_page_(case2)" ran in 0.93s - ok
    [OK] step "Calculation_(case2)" ran in 7.12s - ok
    [OK] step "Editor_(case2)" ran in 1.36s - ok
    | suite_example_xfce=0;;;; graph_type_case=0;;;; c_001__state=1;;;; c_001__warning=2s;;;; c_001__critical=30s;;;; c_001_case2=13.54s;2;30;; s_001_001_Test_Sahi_landing_page_(case2)=0.93s;5;;; s_001_002_Calculation_(case2)=7.12s;10;;; s_001_003_Editor_(case2)=1.36s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_WARNING_IN_CASE.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_WARNING_IN_CASE.txt deleted file mode 100644 index d150baec..00000000 --- a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_WARNING_IN_CASE.txt +++ /dev/null @@ -1,7 +0,0 @@ -type=passive -host_name=my.nagios.host -start_time=34440.000 -finish_time=34482.840 -return_code=1 -service_description=service_description -output=[WARN] Sakuli suite "example_xfce" warning in case, case "case2" over runtime (13.54s/warn at 2s). (Last suite run: 01.01.70 10:34:42)\\n
    [WARN] Sakuli suite "example_xfce" warning in case , case "case2" over runtime (13.54s/warn at 2s). (Last suite run: 01.01.70 10:34:42)
    [OK] case "case1" ran in 14.03s - ok
    [WARN] case "case2" over runtime (13.54s/warn at 2s)
    |suite__state=1;;;; suite__warning=300s;;;; suite__critical=400s;;;; suite_example_xfce=42.84s;300;400;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=14.03s;20;30;; s_001_001_Test_Sahi_landing_page=1.15s;5;;; s_001_002_Calculation=7.32s;10;;; s_001_003_Editor=1.43s;10;;; c_002__state=1;;;; c_002__warning=2s;;;; c_002__critical=30s;;;; c_002_case2=13.54s;2;30;; s_002_001_Test_Sahi_landing_page_(case2)=0.93s;5;;; s_002_002_Calculation_(case2)=7.12s;10;;; s_002_003_Editor_(case2)=1.36s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_WARNING_IN_STEP.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_WARNING_IN_STEP.txt index d5888221..77b33897 100644 --- a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_WARNING_IN_STEP.txt +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_WARNING_IN_STEP.txt @@ -1,7 +1,7 @@ type=passive host_name=my.nagios.host -start_time=34260.000 -finish_time=34304.750 +start_time=34280.000 +finish_time=34293.430 return_code=1 -service_description=service_description -output=[WARN] Sakuli suite "example_xfce" warning in step, step "Calculation_(case2)" over runtime (7.00s/warn at 1s). (Last suite run: 01.01.70 10:31:44)\\n
    [WARN] Sakuli suite "example_xfce" warning in step , step "Calculation_(case2)" over runtime (7.00s/warn at 1s). (Last suite run: 01.01.70 10:31:44)
    [OK] case "case1" ran in 13.83s - ok
    [WARN] case "case2" over runtime (13.43s/warn at 20s), step "Calculation_(case2)" over runtime (7.00s/warn at 1s)
    |suite__state=1;;;; suite__warning=300s;;;; suite__critical=400s;;;; suite_example_xfce=44.75s;300;400;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=13.83s;20;30;; s_001_001_Test_Sahi_landing_page=1.08s;5;;; s_001_002_Calculation=7.13s;10;;; s_001_003_Editor=1.53s;10;;; c_002__state=1;;;; c_002__warning=20s;;;; c_002__critical=30s;;;; c_002_case2=13.43s;20;30;; s_002_001_Test_Sahi_landing_page_(case2)=0.92s;5;;; s_002_002_Calculation_(case2)=7.00s;1;;; s_002_003_Editor_(case2)=1.39s;10;;; [check_sakuli] +service_description=example_xfce_case2 +output=[WARN] Case "case2" of Sakuli suite "example_xfce" warning in step, step "Calculation_(case2)" over runtime (7.00s/warn at 1s). (Last suite run: 01.01.70 10:31:33)\n
    [WARN] Case "case2" of Sakuli suite "example_xfce" warning in step , step "Calculation_(case2)" over runtime (7.00s/warn at 1s). (Last suite run: 01.01.70 10:31:33)
    [OK] step "Test_Sahi_landing_page_(case2)" ran in 0.92s - ok
    [WARN] step "Calculation_(case2)" over runtime (7.00s/warn at 1s)
    [OK] step "Editor_(case2)" ran in 1.39s - ok
    | suite_example_xfce=0;;;; graph_type_case=0;;;; c_001__state=1;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case2=13.43s;20;30;; s_001_001_Test_Sahi_landing_page_(case2)=0.92s;5;;; s_001_002_Calculation_(case2)=7.00s;1;;; s_001_003_Editor_(case2)=1.39s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_WARNING_IN_SUITE.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_WARNING_IN_SUITE.txt deleted file mode 100644 index f253b2a5..00000000 --- a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestCase_WARNING_IN_SUITE.txt +++ /dev/null @@ -1,7 +0,0 @@ -type=passive -host_name=my.nagios.host -start_time=34320.000 -finish_time=34366.940 -return_code=1 -service_description=service_description -output=[WARN] Sakuli suite "example_xfce" warning (46.94s/warn at 3s). (Last suite run: 01.01.70 10:32:46)\\n
    [WARN] Sakuli suite "example_xfce" warning (46.94s/warn at 3s) . (Last suite run: 01.01.70 10:32:46)
    [OK] case "case1" ran in 14.13s - ok
    [OK] case "case2" ran in 13.58s - ok
    |suite__state=1;;;; suite__warning=3s;;;; suite__critical=400s;;;; suite_example_xfce=46.94s;3;400;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=14.13s;20;30;; s_001_001_Test_Sahi_landing_page=1.21s;5;;; s_001_002_Calculation=7.40s;10;;; s_001_003_Editor=1.41s;10;;; c_002__state=0;;;; c_002__warning=20s;;;; c_002__critical=30s;;;; c_002_case2=13.58s;20;30;; s_002_001_Test_Sahi_landing_page_(case2)=1.06s;5;;; s_002_002_Calculation_(case2)=7.07s;10;;; s_002_003_Editor_(case2)=1.34s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestStep_CRITICAL.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestStep_CRITICAL.txt new file mode 100644 index 00000000..f2a87b78 --- /dev/null +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestStep_CRITICAL.txt @@ -0,0 +1,7 @@ +type=passive +host_name=my.nagios.host +start_time=34260.020 +finish_time=34267.020 +return_code=1 +service_description=example_xfce_case2_Calculation +output=[WARN] Step "Calculation" in case "case2" of Sakuli suite "example_xfce" warning step "Calculation" over runtime (7.00s/warn at 1s). (Last test step run: 01.01.70 10:31:07)\n
    [WARN] Step "Calculation" in case "case2" warning (7.00s/warn at 1s) . (Last suite run: 01.01.70 10:31:07)
    | suite_example_xfce=0;;;; graph_type_step=0;;;; s_001_Calculation=7.00s;1;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestStep_ERRORS.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestStep_ERRORS.txt new file mode 100644 index 00000000..a1d813f5 --- /dev/null +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestStep_ERRORS.txt @@ -0,0 +1,7 @@ +type=passive +host_name=my.nagios.host +start_time=34560.000 +finish_time=34561.050 +return_code=2 +service_description=example_xfce_case2_Test_Sahi_landing_page +output=[CRIT] Step "Test_Sahi_landing_page" in case "case2" of Sakuli suite "example_xfce" (1.05s) EXCEPTION: _highlight(_link("xSL Manager")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://sahi.example.com/_s_/spr/concat.js line 3607 > eval:1...\n
    [CRIT] Step "Test_Sahi_landing_page" in case "case2" (1.05s) EXCEPTION: . . (Last suite run: 01.01.70 10:36:01)
    | suite_example_xfce=0;;;; graph_type_step=0;;;; s_001_Test_Sahi_landing_page=U;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestStep_OK.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestStep_OK.txt new file mode 100644 index 00000000..7d11b0bf --- /dev/null +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestStep_OK.txt @@ -0,0 +1,7 @@ +type=passive +host_name=my.nagios.host +start_time=34200.010 +finish_time=34207.290 +return_code=0 +service_description=example_xfce_case2_Calculation +output=[OK] Step "Calculation" in case "case2" of Sakuli suite "example_xfce" ok (7.28s). (Last test step run: 01.01.70 10:30:07)\n
    [OK] Step "Calculation" in case "case2" ok (7.28s) . (Last suite run: 01.01.70 10:30:07)
    | suite_example_xfce=0;;;; graph_type_step=0;;;; s_001_Calculation=7.28s;20;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestStep_WARNING.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestStep_WARNING.txt new file mode 100644 index 00000000..f2a87b78 --- /dev/null +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestStep_WARNING.txt @@ -0,0 +1,7 @@ +type=passive +host_name=my.nagios.host +start_time=34260.020 +finish_time=34267.020 +return_code=1 +service_description=example_xfce_case2_Calculation +output=[WARN] Step "Calculation" in case "case2" of Sakuli suite "example_xfce" warning step "Calculation" over runtime (7.00s/warn at 1s). (Last test step run: 01.01.70 10:31:07)\n
    [WARN] Step "Calculation" in case "case2" warning (7.00s/warn at 1s) . (Last suite run: 01.01.70 10:31:07)
    | suite_example_xfce=0;;;; graph_type_step=0;;;; s_001_Calculation=7.00s;1;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_CRITICAL_IN_CASE.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_CRITICAL_IN_CASE.txt new file mode 100644 index 00000000..bad02cc9 --- /dev/null +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_CRITICAL_IN_CASE.txt @@ -0,0 +1,7 @@ +type=passive +host_name=my.nagios.host +start_time=34500.000 +finish_time=34546.960 +return_code=2 +service_description=example_xfce +output=[CRIT] Sakuli suite "example_xfce" critical in case, case "case2" over runtime (13.70s/crit at 3s). (Last suite run: 01.01.70 10:35:46)\n
    [CRIT] Sakuli suite "example_xfce" critical in case , case "case2" over runtime (13.70s/crit at 3s). (Last suite run: 01.01.70 10:35:46)
    [OK] case "case1" ran in 14.13s - ok
    [CRIT] case "case2" EXCEPTION:
    |suite_example_xfce=46.96s;300;400;; graph_type_suite=0;;;; suite__state=2;;;; suite__warning=300s;;;; suite__critical=400s;;;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=14.13s;20;30;; s_001_001_Test_Sahi_landing_page=1.28s;5;;; s_001_002_Calculation=7.30s;10;;; s_001_003_Editor=1.45s;10;;; c_002__state=2;;;; c_002__warning=2s;;;; c_002__critical=3s;;;; c_002_case2=13.70s;2;3;; s_002_001_Test_Sahi_landing_page_(case2)=1.08s;5;;; s_002_002_Calculation_(case2)=7.11s;10;;; s_002_003_Editor_(case2)=1.42s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_CRITICAL_IN_SUITE.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_CRITICAL_IN_SUITE.txt new file mode 100644 index 00000000..826f36db --- /dev/null +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_CRITICAL_IN_SUITE.txt @@ -0,0 +1,7 @@ +type=passive +host_name=my.nagios.host +start_time=34380.000 +finish_time=34424.810 +return_code=2 +service_description=example_xfce +output=[CRIT] Sakuli suite "example_xfce" critical (44.81s/crit at 40s). (Last suite run: 01.01.70 10:33:44)\n
    [CRIT] Sakuli suite "example_xfce" critical (44.81s/crit at 40s) . (Last suite run: 01.01.70 10:33:44)
    [OK] case "case1" ran in 13.91s - ok
    [OK] case "case2" ran in 13.55s - ok
    |suite_example_xfce=44.81s;30;40;; graph_type_suite=0;;;; suite__state=2;;;; suite__warning=30s;;;; suite__critical=40s;;;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=13.91s;20;30;; s_001_001_Test_Sahi_landing_page=1.16s;5;;; s_001_002_Calculation=7.24s;10;;; s_001_003_Editor=1.43s;10;;; c_002__state=0;;;; c_002__warning=20s;;;; c_002__critical=30s;;;; c_002_case2=13.55s;20;30;; s_002_001_Test_Sahi_landing_page_(case2)=1.05s;5;;; s_002_002_Calculation_(case2)=7.03s;10;;; s_002_003_Editor_(case2)=1.39s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_ERRORS.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_ERRORS.txt new file mode 100644 index 00000000..5c905d59 --- /dev/null +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_ERRORS.txt @@ -0,0 +1,7 @@ +type=passive +host_name=my.nagios.host +start_time=34560.000 +finish_time=34604.800 +return_code=2 +service_description=example_xfce +output=[CRIT] Sakuli suite "example_xfce" (44.80s) EXCEPTION: CASE "case2": STEP "Test_Sahi_landing_page_(case2)": _highlight(_link("xSL Manager")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://...\n
    [CRIT] Sakuli suite "example_xfce" (44.80s) EXCEPTION: CASE "case2": STEP "Test_Sahi_landing_page_(case2)": _highlight(_link("xSL Manager")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://sahi.example.com/_s_/spr/concat.js line 3607 > eval:1:1 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/concat.js:3607:9 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/sakuli/inject.js:46:12 @http://sahi.example.com/_s_/spr/concat.js:3373:5 Click for browser script. . (Last suite run: 01.01.70 10:36:44)
    [OK] case "case1" ran in 14.20s - ok
    [CRIT] case "case2" EXCEPTION: STEP "Test_Sahi_landing_page_(case2)": _highlight(_link("xSL Manager")); TypeError: el is undefined Sahi.prototype._highlight@http://sahi.example.com/_s_/spr/concat.js:1210:9 @http://sahi.example.com/_s_/spr/concat.js line 3607 > eval:1:1 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/concat.js:3607:9 Sahi.prototype.ex@http://sahi.example.com/_s_/spr/sakuli/inject.js:46:12 @http://sahi.example.com/_s_/spr/concat.js:3373:5 Click for browser script
    |suite_example_xfce=U;300;400;; graph_type_suite=0;;;; suite__state=2;;;; suite__warning=300s;;;; suite__critical=400s;;;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=14.20s;20;30;; s_001_001_Test_Sahi_landing_page=1.14s;5;;; s_001_002_Calculation=7.53s;10;;; s_001_003_Editor=1.45s;10;;; c_002__state=2;;;; c_002__warning=20s;;;; c_002__critical=30s;;;; c_002_case2=U;20;30;; s_002_001_Test_Sahi_landing_page_(case2)=U;5;;; s_002_002_Calculation_(case2)=7.03s;10;;; s_002_003_Editor_(case2)=1.39s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_OK.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_OK.txt new file mode 100644 index 00000000..7214f17a --- /dev/null +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_OK.txt @@ -0,0 +1,7 @@ +type=passive +host_name=my.nagios.host +start_time=34200.000 +finish_time=34244.990 +return_code=0 +service_description=example_xfce +output=[OK] Sakuli suite "example_xfce" ok (44.99s). (Last suite run: 01.01.70 10:30:44)\n
    [OK] Sakuli suite "example_xfce" ok (44.99s) . (Last suite run: 01.01.70 10:30:44)
    [OK] case "case1" ran in 14.02s - ok
    [OK] case "case2" ran in 13.58s - ok
    |suite_example_xfce=44.99s;300;400;; graph_type_suite=0;;;; suite__state=0;;;; suite__warning=300s;;;; suite__critical=400s;;;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=14.02s;20;30;; s_001_001_Test_Sahi_landing_page=1.16s;5;;; s_001_002_Calculation=7.28s;10;;; s_001_003_Editor=1.48s;10;;; c_002__state=0;;;; c_002__warning=20s;;;; c_002__critical=30s;;;; c_002_case2=13.58s;20;30;; s_002_001_Test_Sahi_landing_page_(case2)=1.03s;5;;; s_002_002_Calculation_(case2)=7.07s;10;;; s_002_003_Editor_(case2)=1.37s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_WARNING_IN_CASE.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_WARNING_IN_CASE.txt new file mode 100644 index 00000000..4916a541 --- /dev/null +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_WARNING_IN_CASE.txt @@ -0,0 +1,7 @@ +type=passive +host_name=my.nagios.host +start_time=34440.000 +finish_time=34482.840 +return_code=1 +service_description=example_xfce +output=[WARN] Sakuli suite "example_xfce" warning in case, case "case2" over runtime (13.54s/warn at 2s). (Last suite run: 01.01.70 10:34:42)\n
    [WARN] Sakuli suite "example_xfce" warning in case , case "case2" over runtime (13.54s/warn at 2s). (Last suite run: 01.01.70 10:34:42)
    [OK] case "case1" ran in 14.03s - ok
    [WARN] case "case2" over runtime (13.54s/warn at 2s)
    |suite_example_xfce=42.84s;300;400;; graph_type_suite=0;;;; suite__state=1;;;; suite__warning=300s;;;; suite__critical=400s;;;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=14.03s;20;30;; s_001_001_Test_Sahi_landing_page=1.15s;5;;; s_001_002_Calculation=7.32s;10;;; s_001_003_Editor=1.43s;10;;; c_002__state=1;;;; c_002__warning=2s;;;; c_002__critical=30s;;;; c_002_case2=13.54s;2;30;; s_002_001_Test_Sahi_landing_page_(case2)=0.93s;5;;; s_002_002_Calculation_(case2)=7.12s;10;;; s_002_003_Editor_(case2)=1.36s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_WARNING_IN_STEP.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_WARNING_IN_STEP.txt new file mode 100644 index 00000000..1dc438d4 --- /dev/null +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_WARNING_IN_STEP.txt @@ -0,0 +1,7 @@ +type=passive +host_name=my.nagios.host +start_time=34260.000 +finish_time=34304.750 +return_code=1 +service_description=example_xfce +output=[WARN] Sakuli suite "example_xfce" warning in step, step "Calculation_(case2)" over runtime (7.00s/warn at 1s). (Last suite run: 01.01.70 10:31:44)\n
    [WARN] Sakuli suite "example_xfce" warning in step , step "Calculation_(case2)" over runtime (7.00s/warn at 1s). (Last suite run: 01.01.70 10:31:44)
    [OK] case "case1" ran in 13.83s - ok
    [WARN] case "case2" over runtime (13.43s/warn at 20s), step "Calculation_(case2)" over runtime (7.00s/warn at 1s)
    |suite_example_xfce=44.75s;300;400;; graph_type_suite=0;;;; suite__state=1;;;; suite__warning=300s;;;; suite__critical=400s;;;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=13.83s;20;30;; s_001_001_Test_Sahi_landing_page=1.08s;5;;; s_001_002_Calculation=7.13s;10;;; s_001_003_Editor=1.53s;10;;; c_002__state=1;;;; c_002__warning=20s;;;; c_002__critical=30s;;;; c_002_case2=13.43s;20;30;; s_002_001_Test_Sahi_landing_page_(case2)=0.92s;5;;; s_002_002_Calculation_(case2)=7.00s;1;;; s_002_003_Editor_(case2)=1.39s;10;;; [check_sakuli] diff --git a/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_WARNING_IN_SUITE.txt b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_WARNING_IN_SUITE.txt new file mode 100644 index 00000000..836b86a4 --- /dev/null +++ b/src/integration-test/src/test/resources/org/sakuli/integration/services/forwarder/gearman/output/TestSuite_WARNING_IN_SUITE.txt @@ -0,0 +1,7 @@ +type=passive +host_name=my.nagios.host +start_time=34320.000 +finish_time=34366.940 +return_code=1 +service_description=example_xfce +output=[WARN] Sakuli suite "example_xfce" warning (46.94s/warn at 3s). (Last suite run: 01.01.70 10:32:46)\n
    [WARN] Sakuli suite "example_xfce" warning (46.94s/warn at 3s) . (Last suite run: 01.01.70 10:32:46)
    [OK] case "case1" ran in 14.13s - ok
    [OK] case "case2" ran in 13.58s - ok
    |suite_example_xfce=46.94s;3;400;; graph_type_suite=0;;;; suite__state=1;;;; suite__warning=3s;;;; suite__critical=400s;;;; c_001__state=0;;;; c_001__warning=20s;;;; c_001__critical=30s;;;; c_001_case1=14.13s;20;30;; s_001_001_Test_Sahi_landing_page=1.21s;5;;; s_001_002_Calculation=7.40s;10;;; s_001_003_Editor=1.41s;10;;; c_002__state=0;;;; c_002__warning=20s;;;; c_002__critical=30s;;;; c_002_case2=13.58s;20;30;; s_002_001_Test_Sahi_landing_page_(case2)=1.06s;5;;; s_002_002_Calculation_(case2)=7.07s;10;;; s_002_003_Editor_(case2)=1.34s;10;;; [check_sakuli] diff --git a/src/java-dsl/src/main/java/org/sakuli/javaDSL/TestCaseInitParameter.java b/src/java-dsl/src/main/java/org/sakuli/javaDSL/TestCaseInitParameter.java index 76467f22..e52dd5d1 100644 --- a/src/java-dsl/src/main/java/org/sakuli/javaDSL/TestCaseInitParameter.java +++ b/src/java-dsl/src/main/java/org/sakuli/javaDSL/TestCaseInitParameter.java @@ -19,7 +19,7 @@ package org.sakuli.javaDSL; import org.sakuli.datamodel.TestCase; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import java.nio.file.Files; import java.nio.file.Path; @@ -63,12 +63,12 @@ public TestCaseInitParameter(String testCaseId, String testCaseFolderName) { imagePaths = new ArrayList<>(); } - public static Path checkFolder(String path) throws SakuliException { + public static Path checkFolder(String path) throws SakuliCheckedException { Path folder = Paths.get(path); if (Files.exists(folder)) { return folder; } - throw new SakuliException(String.format("The required folder '%s' does not exist!", folder.toAbsolutePath().toString())); + throw new SakuliCheckedException(String.format("The required folder '%s' does not exist!", folder.toAbsolutePath().toString())); } public TestCaseInitParameter withWarningTime(int warningTime) { @@ -81,7 +81,7 @@ public TestCaseInitParameter withCriticalTime(int criticalTime) { return this; } - public TestCaseInitParameter addImagePath(String... imagePaths) throws SakuliException { + public TestCaseInitParameter addImagePath(String... imagePaths) throws SakuliCheckedException { if (imagePaths != null) { for (String imagePath : imagePaths) { this.imagePaths.add(checkFolder(imagePath)); diff --git a/src/java-dsl/src/main/java/org/sakuli/javaDSL/aop/ExceptionHandlerAspect.java b/src/java-dsl/src/main/java/org/sakuli/javaDSL/aop/ExceptionHandlerAspect.java index e47b45b6..d41ae8c6 100644 --- a/src/java-dsl/src/main/java/org/sakuli/javaDSL/aop/ExceptionHandlerAspect.java +++ b/src/java-dsl/src/main/java/org/sakuli/javaDSL/aop/ExceptionHandlerAspect.java @@ -37,19 +37,19 @@ public class ExceptionHandlerAspect extends BaseSakuliAspect { /** - * Throw the handled Exception after {@link SakuliExceptionHandler#processException(Throwable)} to stop the current + * Throw the handled Exception after {@link SakuliExceptionHandler#processException(Exception)} to stop the current * test case execution of an JAVA test. */ @After("execution(* org.sakuli.exceptions.SakuliExceptionHandler.processException(..)) ") - public void processJavaException(JoinPoint joinPoint) throws Throwable { + public void processJavaException(JoinPoint joinPoint) throws Exception { Object[] args = joinPoint.getArgs(); if (args != null && args.length == 1) { Object e = args[0]; SakuliExceptionHandler exceptionHandler = BeanLoader.loadBaseActionLoader().getExceptionHandler(); - if (e instanceof SakuliException && exceptionHandler.resumeToTestExcecution((SakuliException) e)) { + if (e instanceof SakuliException && exceptionHandler.resumeToTestExecution(((SakuliException) e).castTo())) { return; } - throw (Throwable) e; + throw (Exception) e; } } diff --git a/src/java-dsl/src/test/java/org/sakuli/javaDSL/AbstractSakuliTest.java b/src/java-dsl/src/test/java/org/sakuli/javaDSL/AbstractSakuliTest.java index 69b0f949..d0e72a12 100644 --- a/src/java-dsl/src/test/java/org/sakuli/javaDSL/AbstractSakuliTest.java +++ b/src/java-dsl/src/test/java/org/sakuli/javaDSL/AbstractSakuliTest.java @@ -25,7 +25,7 @@ import org.sakuli.actions.TestCaseAction; import org.sakuli.datamodel.TestSuite; import org.sakuli.datamodel.builder.TestCaseBuilder; -import org.sakuli.exceptions.SakuliException; +import org.sakuli.exceptions.SakuliCheckedException; import org.sakuli.exceptions.SakuliRuntimeException; import org.sakuli.javaDSL.service.SahiInitializingService; import org.sakuli.javaDSL.utils.SakuliJavaPropertyPlaceholderConfigurer; @@ -126,7 +126,7 @@ public void initTC() throws Exception { String testCaseName = this.getClass().getSimpleName(); initParameter = getTestCaseInitParameter(); if (initParameter == null) { - throw new SakuliException("init parameter have to be set!"); + throw new SakuliCheckedException("init parameter have to be set!"); } testSuite = BeanLoader.loadBean(TestSuite.class); @@ -173,11 +173,13 @@ public void initTcStep() throws Exception { } @AfterMethod(alwaysRun = true) - public void saveTcStep() throws Throwable { + public void saveTcStep() throws Exception { testCaseAction.addTestCaseStep("step " + counter, String.valueOf(startTime.getMillis()), String.valueOf(DateTime.now().getMillis()), - 0 + 0, + 0, + false ); } @@ -192,7 +194,8 @@ public void stopTC() throws Exception { String.valueOf(startTimeCase.getMillis()), String.valueOf(DateTime.now().getMillis()), null, - null + null, + false ); if (browser != null) { browser.close(); @@ -238,7 +241,7 @@ public void tearDown() throws Exception { if (testSuite != null) { LOGGER.info("========== TEAR-DOWN SAKULI TEST SUITE '{}' ==========", testSuite.getId()); testSuite.setStopDate(DateTime.now().toDate()); - TeardownServiceHelper.invokeTeardownServices(); + TeardownServiceHelper.invokeTeardownServices(testSuite); } if (executorService != null) { executorService.shutdownNow(); @@ -249,4 +252,5 @@ public void tearDown() throws Exception { ProcessHelper.killAll(browserProcessName); } } + } diff --git a/src/java-dsl/src/test/java/org/sakuli/javaDSL/SakuliExceptionListener.java b/src/java-dsl/src/test/java/org/sakuli/javaDSL/SakuliExceptionListener.java index 47089e16..157b046f 100644 --- a/src/java-dsl/src/test/java/org/sakuli/javaDSL/SakuliExceptionListener.java +++ b/src/java-dsl/src/test/java/org/sakuli/javaDSL/SakuliExceptionListener.java @@ -1,3 +1,21 @@ +/* + * Sakuli - Testing and Monitoring-Tool for Websites and common UIs. + * + * Copyright 2013 - 2018 the original author or authors. + * + * 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 org.sakuli.javaDSL;/* * Copyright (c) 2015 - M-net Telekommunikations GmbH * All rights reserved. @@ -15,11 +33,15 @@ */ public class SakuliExceptionListener extends TestListenerAdapter { + private static Exception castTo(Throwable throwable) { + return (throwable instanceof Exception) ? (Exception) throwable : new Exception(throwable); + } + @Override public void onTestFailure(ITestResult tr) { SakuliExceptionHandler exceptionHandler = BeanLoader.loadBaseActionLoader().getExceptionHandler(); - if (!exceptionHandler.isAlreadyProcessed(tr.getThrowable())) { - exceptionHandler.handleException(tr.getThrowable()); + if (exceptionHandler != null && !exceptionHandler.isAlreadyProcessed(castTo(tr.getThrowable()))) { + exceptionHandler.handleException(castTo(tr.getThrowable())); } super.onTestFailure(tr); }