use of org.sakuli.exceptions.SakuliExceptionWithScreenshot in project sakuli by ConSol.
the class ScreenshotDivConverter method extractScreenshotAsBase64.
protected String extractScreenshotAsBase64(Throwable exception) {
if (exception instanceof SakuliExceptionWithScreenshot) {
Path screenshotPath = ((SakuliExceptionWithScreenshot) exception).getScreenshot();
if (screenshotPath != null) {
try {
byte[] binaryScreenshot = Files.readAllBytes(screenshotPath);
String base64String = new BASE64Encoder().encode(binaryScreenshot);
for (String newLine : Arrays.asList("\n", "\r")) {
base64String = StringUtils.remove(base64String, newLine);
}
return base64String;
} catch (IOException e) {
exceptionHandler.handleException(new SakuliForwarderException(e, String.format("error during the BASE64 encoding of the screenshot '%s'", screenshotPath.toString())));
}
}
}
return null;
}
use of org.sakuli.exceptions.SakuliExceptionWithScreenshot in project sakuli by ConSol.
the class DaoTestCaseImplTest method testGetScreenshotAsSqlLobValue.
@Test
public void testGetScreenshotAsSqlLobValue() throws Exception {
Path screenshotPath = Paths.get(this.getClass().getResource("computer.png").toURI());
TestCase testCase = new TestCaseExampleBuilder().withException(new SakuliExceptionWithScreenshot("test-exception", screenshotPath)).buildExample();
SqlLobValue result = testling.getScreenshotAsSqlLobValue(testCase);
assertNotNull(result);
}
use of org.sakuli.exceptions.SakuliExceptionWithScreenshot in project sakuli by ConSol.
the class NagiosOutputBuilderTest method testFormatTestCaseTableStateMessageWithScreenshot.
@Test
public void testFormatTestCaseTableStateMessageWithScreenshot() throws Exception {
Path screenshotPath = Paths.get(NagiosOutputBuilder.class.getResource("computer.png").toURI());
String htmlTemplate = "<tr valign=\"top\"><td class=\"%s\">%s<\\/td><\\/tr>";
GearmanProperties properties = MonitoringPropertiesTestHelper.initMock(mock(GearmanProperties.class));
TestCase testCase = new TestCaseExampleBuilder().withState(TestCaseState.ERRORS).withId("case-error").withException(new SakuliExceptionWithScreenshot("EXCEPTION-MESSAGE", screenshotPath)).buildExample();
String regex = String.format(htmlTemplate, "serviceCRITICAL", "\\[CRIT\\] case \"case-error\" EXCEPTION: EXCEPTION-MESSAGE" + "<div.* src=\"data:image\\/png;base64,.*><\\/div>");
BaseTest.assertRegExMatch(testling.formatTestCaseTableStateMessage(testCase, properties.lookUpTemplate(testCase.getState())), regex);
}
use of org.sakuli.exceptions.SakuliExceptionWithScreenshot in project sakuli by ConSol.
the class NagiosOutputBuilderTest method testFormatTestSuiteTableExceptionWithScreenshot.
@Test
public void testFormatTestSuiteTableExceptionWithScreenshot() throws Exception {
Path screenshotPath = Paths.get(NagiosOutputBuilder.class.getResource("computer.png").toURI());
Date startDate = new Date();
TestSuite testSuiteExample = new TestSuiteExampleBuilder().withId("sakuli-123").withState(TestSuiteState.ERRORS).withStartDate(startDate).withException(new SakuliExceptionWithScreenshot("TEST-ERROR", screenshotPath)).withStopDate(DateUtils.addSeconds(startDate, 120)).buildExample();
String result = testling.formatTestSuiteTableStateMessage(testSuiteExample, gearmanProperties.getTemplateSuiteTable());
String lastRun = AbstractOutputBuilder.dateFormat.format(testSuiteExample.getStopDate());
final String separator = "<div";
assertEquals(result.substring(0, result.indexOf(separator)), "<tr valign=\"top\"><td class=\"serviceCRITICAL\">[CRIT] Sakuli suite \"sakuli-123\"" + " (120.00s) EXCEPTION: 'TEST-ERROR'. (Last suite run: " + lastRun + ")");
String screenshotHash = result.substring(result.indexOf(ScreenshotDiv.DEFAULT_SAKULI_SCREENSHOT_DIV_ID) + ScreenshotDiv.DEFAULT_SAKULI_SCREENSHOT_DIV_ID.length());
screenshotHash = screenshotHash.substring(0, screenshotHash.indexOf("\">"));
String start_1 = "<div id=\"sakuli_screenshot" + screenshotHash + "\">" + "<div id=\"openModal_sakuli_screenshot" + screenshotHash + "\" class=\"modalDialog\">" + "<a href=\"#close\" title=\"Close\" class=\"close\">Close X</a>" + "<a href=\"#openModal_sakuli_screenshot" + screenshotHash + "\"><img class=\"screenshot\" src=\"";
String start = start_1 + "data:image/png;base64,";
String end = "</a></div></div></td></tr>";
String substring = result.substring(result.indexOf(separator));
assertEquals(substring.substring(0, start.length()), start);
assertEquals(substring.substring(substring.length() - end.length()), end);
//now check the remove function
String resultWithOutBase64Data = ScreenshotDivConverter.removeBase64ImageDataString(substring);
assertEquals(resultWithOutBase64Data, start_1 + "\" >" + end);
}
Aggregations