All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.chutneytesting.execution.infra.storage.DatabaseExecutionHistoryRepository Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package com.chutneytesting.execution.infra.storage;

import com.chutneytesting.execution.domain.history.ExecutionHistory.DetachedExecution;
import com.chutneytesting.execution.domain.history.ExecutionHistory.Execution;
import com.chutneytesting.execution.domain.history.ExecutionHistory.ExecutionSummary;
import com.chutneytesting.execution.domain.history.ExecutionHistoryRepository;
import com.chutneytesting.execution.domain.history.ImmutableExecutionHistory;
import com.chutneytesting.execution.domain.history.ReportNotFoundException;
import com.chutneytesting.execution.domain.report.ServerReportStatus;
import com.google.common.collect.ImmutableMap;
import java.time.ZoneId;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

@Component
class DatabaseExecutionHistoryRepository implements ExecutionHistoryRepository {

    private static final int LIMIT_BLOC_SIZE = 20;
    private final ExecutionRowMapper executionRowMapper = new ExecutionRowMapper();
    private final ExecutionSummaryRowMapper executionSummaryRowMapper = new ExecutionSummaryRowMapper();
    private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    DatabaseExecutionHistoryRepository(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
    }

    @Override
    public List getExecutions(String scenarioId) {
        return namedParameterJdbcTemplate.query(
            "SELECT ID, EXECUTION_TIME, DURATION, STATUS, INFORMATION, ERROR, TEST_CASE_TITLE, ENVIRONMENT, DATASET_ID, DATASET_VERSION, USER_ID FROM SCENARIO_EXECUTION_HISTORY WHERE SCENARIO_ID = :scenarioId ORDER BY ID DESC LIMIT " + LIMIT_BLOC_SIZE,
            ImmutableMap.builder().put("scenarioId", scenarioId).build(),
            executionSummaryRowMapper);
    }

    @Override
    public Execution store(String scenarioId, DetachedExecution detachedExecution) throws IllegalStateException {
        long nextId = namedParameterJdbcTemplate.queryForObject("SELECT nextval('SCENARIO_EXECUTION_HISTORY_SEQ')", Collections.emptyMap(), long.class);
        Execution execution = detachedExecution.attach(nextId);
        Map executionParameters = executionParameters(execution);
        executionParameters.put("scenarioId", scenarioId);
        executionParameters.put("id", nextId);
        namedParameterJdbcTemplate.update("INSERT INTO SCENARIO_EXECUTION_HISTORY"
                + "(ID, SCENARIO_ID, EXECUTION_TIME, DURATION, STATUS, INFORMATION, ERROR, REPORT, TEST_CASE_TITLE, ENVIRONMENT, DATASET_ID, DATASET_VERSION, USER_ID) VALUES "
                + "(:id, :scenarioId, :executionTime, :duration, :status, :information, :error, :report, :title, :environment, :datasetId, :datasetVersion, :user)",
            executionParameters);

        return ImmutableExecutionHistory.Execution.builder()
            .from(execution)
            .executionId(nextId)
            .build();
    }

    @Override
    public Execution getExecution(String scenarioId, Long reportId) throws ReportNotFoundException {
        try {
            return namedParameterJdbcTemplate.queryForObject(
                "SELECT ID, EXECUTION_TIME, DURATION, STATUS, INFORMATION, ERROR, REPORT, TEST_CASE_TITLE, ENVIRONMENT, DATASET_ID, DATASET_VERSION, USER_ID FROM SCENARIO_EXECUTION_HISTORY WHERE ID = :reportId AND SCENARIO_ID = :scenarioId",
                ImmutableMap.builder()
                    .put("reportId", reportId)
                    .put("scenarioId", scenarioId)
                    .build(),
                executionRowMapper);
        } catch (EmptyResultDataAccessException e) {
            throw new ReportNotFoundException(scenarioId, reportId);
        }
    }

    @Override
    public void update(String scenarioId, Execution updatedExecution) throws ReportNotFoundException {
        int updatedEntries = update(updatedExecution);

        if (updatedEntries == 0) {
            throw new ReportNotFoundException(scenarioId, updatedExecution.executionId());
        }
    }

    private int update(Execution updatedExecution) throws ReportNotFoundException {
        Map executionParameters = executionParameters(updatedExecution);
        executionParameters.put("id", updatedExecution.executionId());

        return namedParameterJdbcTemplate.update(
            "UPDATE SCENARIO_EXECUTION_HISTORY SET "
                + "EXECUTION_TIME = :executionTime, DURATION = :duration, STATUS = :status, INFORMATION = :information, ERROR = :error, REPORT = :report "
                + "WHERE ID = :id",
            executionParameters);
    }

    @Override
    public int setAllRunningExecutionsToKO() {
        List runningExecutions = getExecutionsWithStatus(ServerReportStatus.RUNNING);
        updateExecutionsToKO(runningExecutions);

        List pausedExecutions = getExecutionsWithStatus(ServerReportStatus.PAUSED);
        updateExecutionsToKO(pausedExecutions);

        return runningExecutions.size() + pausedExecutions.size();
    }

    @Override
    public List getExecutionsWithStatus(ServerReportStatus status) {
        return namedParameterJdbcTemplate.query(
            "SELECT ID, EXECUTION_TIME, DURATION, STATUS, INFORMATION, ERROR, TEST_CASE_TITLE, ENVIRONMENT, DATASET_ID, DATASET_VERSION, USER_ID FROM SCENARIO_EXECUTION_HISTORY WHERE STATUS = :status",
            ImmutableMap.builder().put("status", status.name()).build(),
            executionSummaryRowMapper);
    }

    private void updateExecutionsToKO(List executions) {
        executions.stream()
            .map(this::buildKnockoutExecutionFrom)
            .forEach(this::update);
    }

    private ImmutableExecutionHistory.Execution buildKnockoutExecutionFrom(ExecutionSummary executionSummary) {
        return ImmutableExecutionHistory.Execution.builder()
            .executionId(executionSummary.executionId())
            .status(ServerReportStatus.FAILURE)
            .time(executionSummary.time())
            .duration(executionSummary.duration())
            .info(executionSummary.info())
            .error("Execution was interrupted !")
            .report("")
            .testCaseTitle(executionSummary.testCaseTitle())
            .environment(executionSummary.environment())
            .user(executionSummary.user())
            .build();
    }

    private Map executionParameters(Execution execution) {
        Map executionParameters = new HashMap<>();
        executionParameters.put("executionTime", execution.time().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
        executionParameters.put("duration", execution.duration());
        executionParameters.put("status", execution.status().name());
        executionParameters.put("information", execution.info().map(info -> StringUtils.substring(info, 0, 512)).orElse(null));
        executionParameters.put("error", execution.error().map(error -> StringUtils.substring(error, 0, 512)).orElse(null));
        executionParameters.put("report", execution.report());
        executionParameters.put("title", execution.testCaseTitle());
        executionParameters.put("environment", execution.environment());
        executionParameters.put("datasetId", execution.datasetId().orElse(null));
        executionParameters.put("datasetVersion", execution.datasetVersion().orElse(null));
        executionParameters.put("user", execution.user());
        return executionParameters;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy