Search in sources :

Example 1 with SakuliForwarderException

use of org.sakuli.exceptions.SakuliForwarderException in project sakuli by ConSol.

the class DatabaseResultServiceImpl method saveAllResults.

@Override
public void saveAllResults() {
    LOGGER.info("======= SAVE RESULTS TO DATABASE ======");
    ;
    try {
        daoTestSuite.saveTestSuiteResult();
        daoTestSuite.saveTestSuiteToSahiJobs();
        if (!CollectionUtils.isEmpty(testSuite.getTestCases())) {
            for (TestCase tc : testSuite.getTestCasesAsSortedSet()) {
                //write testcase and steps to DB
                daoTestCase.saveTestCaseResult(tc);
                LOGGER.info("... try to save all STEPS for test case '" + tc.getId() + "'!");
                SortedSet<TestCaseStep> steps = tc.getStepsAsSortedSet();
                if (!steps.isEmpty()) {
                    daoTestCaseStep.saveTestCaseSteps(steps, tc.getDbPrimaryKey());
                    LOGGER.info("all STEPS for '" + tc.getId() + "' saved!");
                } else {
                    LOGGER.info("no STEPS for '\" + tc.getId() +\"'found => no STEPS saved in DB!");
                }
            }
        }
        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);
    }
}
Also used : DaoTestCase(org.sakuli.services.forwarder.database.dao.DaoTestCase) TestCase(org.sakuli.datamodel.TestCase) DaoTestCaseStep(org.sakuli.services.forwarder.database.dao.DaoTestCaseStep) TestCaseStep(org.sakuli.datamodel.TestCaseStep) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException)

Example 2 with SakuliForwarderException

use of org.sakuli.exceptions.SakuliForwarderException in project sakuli by ConSol.

the class Icinga2ResultServiceImpl method saveAllResults.

@Override
public void saveAllResults() {
    LOGGER.info("======= SEND RESULTS TO ICINGA SERVER ======");
    LOGGER.info("POST Sakuli results to '{}'", icinga2RestCient.getTargetCheckResult().getUri().toString());
    Entity<Icinga2Request> payload = Entity.json(icinga2CheckResultBuilder.build());
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("ICINGA Payload: {}", convertToJSON(payload));
    }
    Response response = icinga2RestCient.getTargetCheckResult().request(MediaType.APPLICATION_JSON_TYPE).post(payload);
    Icinga2Result result = response.readEntity(Icinga2Result.class);
    if (result.isSuccess()) {
        LOGGER.info("ICINGA Response: {}", result.getFirstElementAsString());
        LOGGER.info("======= FINISHED: SEND RESULTS TO ICINGA SERVER ======");
    } else {
        exceptionHandler.handleException(new SakuliForwarderException(String.format("Unexpected result of REST-POST to Incinga monitoring server (%s): %s", icinga2RestCient.getTargetCheckResult().getUri(), result.getFirstElementAsString())));
    }
}
Also used : Response(javax.ws.rs.core.Response) Icinga2Result(org.sakuli.services.forwarder.icinga2.model.Icinga2Result) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException) Icinga2Request(org.sakuli.services.forwarder.icinga2.model.Icinga2Request)

Example 3 with SakuliForwarderException

use of org.sakuli.exceptions.SakuliForwarderException in project sakuli by ConSol.

the class AbstractTemplateOutputBuilder method createOutput.

/**
     * Converts the current test suite to a string based on the template for the concrete converter.
     *
     * @return
     */
public String createOutput() throws SakuliForwarderException {
    try {
        JtwigModel model = createModel();
        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()).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!");
    }
}
Also used : SpacelessExtension(org.jtwig.spaceless.SpacelessExtension) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException) JtwigTemplate(org.jtwig.JtwigTemplate) SpacelessConfiguration(org.jtwig.spaceless.configuration.SpacelessConfiguration) JtwigModel(org.jtwig.JtwigModel) EnvironmentConfiguration(org.jtwig.environment.EnvironmentConfiguration)

Example 4 with SakuliForwarderException

use of org.sakuli.exceptions.SakuliForwarderException 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;
}
Also used : Path(java.nio.file.Path) BASE64Encoder(sun.misc.BASE64Encoder) SakuliExceptionWithScreenshot(org.sakuli.exceptions.SakuliExceptionWithScreenshot) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException) IOException(java.io.IOException)

Example 5 with SakuliForwarderException

use of org.sakuli.exceptions.SakuliForwarderException 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 ======");
    });
}
Also used : GearmanJobServerConnection(org.gearman.common.GearmanJobServerConnection) SakuliForwarderRuntimeException(org.sakuli.exceptions.SakuliForwarderRuntimeException) ArrayList(java.util.ArrayList) NagiosCheckResult(org.sakuli.services.forwarder.gearman.model.NagiosCheckResult) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException) SakuliForwarderCheckedException(org.sakuli.exceptions.SakuliForwarderCheckedException) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException) SakuliForwarderRuntimeException(org.sakuli.exceptions.SakuliForwarderRuntimeException) SakuliForwarderCheckedException(org.sakuli.exceptions.SakuliForwarderCheckedException)

Aggregations

SakuliForwarderException (org.sakuli.exceptions.SakuliForwarderException)6 ArrayList (java.util.ArrayList)2 GearmanJobServerConnection (org.gearman.common.GearmanJobServerConnection)2 NagiosCheckResult (org.sakuli.services.forwarder.gearman.model.NagiosCheckResult)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Response (javax.ws.rs.core.Response)1 JtwigModel (org.jtwig.JtwigModel)1 JtwigTemplate (org.jtwig.JtwigTemplate)1 EnvironmentConfiguration (org.jtwig.environment.EnvironmentConfiguration)1 SpacelessExtension (org.jtwig.spaceless.SpacelessExtension)1 SpacelessConfiguration (org.jtwig.spaceless.configuration.SpacelessConfiguration)1 TestCase (org.sakuli.datamodel.TestCase)1 TestCaseStep (org.sakuli.datamodel.TestCaseStep)1 SakuliExceptionWithScreenshot (org.sakuli.exceptions.SakuliExceptionWithScreenshot)1 SakuliForwarderCheckedException (org.sakuli.exceptions.SakuliForwarderCheckedException)1 SakuliForwarderRuntimeException (org.sakuli.exceptions.SakuliForwarderRuntimeException)1 DaoTestCase (org.sakuli.services.forwarder.database.dao.DaoTestCase)1 DaoTestCaseStep (org.sakuli.services.forwarder.database.dao.DaoTestCaseStep)1 Icinga2Request (org.sakuli.services.forwarder.icinga2.model.Icinga2Request)1