use of org.sakuli.exceptions.SakuliForwarderCheckedException in project sakuli by ConSol.
the class GearmanResultServiceImpl method tearDown.
@Override
public void tearDown(Optional<AbstractTestDataEntity> dataEntity, boolean asyncCall) {
dataEntity.ifPresent(data -> {
logger.info("======= SEND RESULTS TO GEARMAN SERVER ======");
GearmanClient gearmanClient = getGearmanClient();
GearmanJobServerConnection connection = getGearmanConnection(properties.getServerHost(), properties.getServerPort());
List<NagiosCheckResult> 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));
}
}
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(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()) {
try {
cacheService.cacheResults(results);
} catch (SakuliForwarderCheckedException e) {
handleTeardownException(e, asyncCall, data);
}
}
gearmanClient.shutdown();
logger.info("======= FINISHED: SEND RESULTS TO GEARMAN SERVER ======");
});
}
use of org.sakuli.exceptions.SakuliForwarderCheckedException in project sakuli by ConSol.
the class GearmanCacheService method cacheResults.
/**
* Writes results to Gearman cache file.
*
* @param results
*/
public void cacheResults(List<NagiosCheckResult> results) throws SakuliForwarderCheckedException {
Path cacheFile = testSuiteProperties.getTestSuiteFolder().resolve(CACHE_FILE);
File output = new File(cacheFile.toUri());
if (!output.getParentFile().exists()) {
output.getParentFile().mkdirs();
}
try (FileOutputStream fos = new FileOutputStream(output)) {
for (NagiosCheckResult result : results) {
fos.write((CACHE_SEPARATOR + " " + result.getQueueName() + ":" + result.getUuid() + LINE_SEPARATOR).getBytes(CHARSET_NAME));
fos.write((result.getPayload().trim() + LINE_SEPARATOR).getBytes(CHARSET_NAME));
fos.write((CACHE_SEPARATOR + LINE_SEPARATOR).getBytes(CHARSET_NAME));
}
if (results.isEmpty()) {
fos.write(LINE_SEPARATOR.getBytes(CHARSET_NAME));
}
fos.flush();
} catch (IOException e) {
throw new SakuliForwarderCheckedException(e, "Failed to write Gearman cache file");
}
}
use of org.sakuli.exceptions.SakuliForwarderCheckedException in project sakuli by ConSol.
the class ScreenshotDivConverterTest method testThrowException.
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
@Test
public void testThrowException() throws Exception {
Path screenshotPath = Paths.get("computerNOTVALID.png");
ArgumentCaptor<Exception> 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(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);
}
use of org.sakuli.exceptions.SakuliForwarderCheckedException in project sakuli by ConSol.
the class AbstractTemplateOutputBuilder method createOutput.
/**
* 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(abstractTestDataEntity);
EnvironmentConfiguration configuration = EnvironmentConfigurationBuilder.configuration().extensions().add(new SpacelessExtension(new SpacelessConfiguration(new LeadingWhitespaceRemover()))).and().functions().add(new IsBlankFunction()).add(new GetOutputStateFunction()).add(new GetOutputDurationFunction()).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 (Exception e) {
throw new SakuliForwarderCheckedException(e, "Exception during rendering of Twig template occurred!");
}
}
use of org.sakuli.exceptions.SakuliForwarderCheckedException in project sakuli by ConSol.
the class ScreenshotDivConverter method extractScreenshotAsBase64.
protected String extractScreenshotAsBase64(Exception 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 SakuliForwarderCheckedException(e, String.format("error during the BASE64 encoding of the screenshot '%s'", screenshotPath.toString())));
}
}
}
return null;
}
Aggregations