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

io.cloudslang.lang.systemtests.TriggerFlows Maven / Gradle / Ivy

There is a newer version: 2.0.94
Show newest version
/*******************************************************************************
 * (c) Copyright 2016 Hewlett-Packard Development Company, L.P.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0 which accompany this distribution.
 *
 * The Apache License is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *******************************************************************************/
package io.cloudslang.lang.systemtests;

import com.google.common.collect.Sets;
import io.cloudslang.lang.api.Slang;
import io.cloudslang.lang.entities.CompilationArtifact;
import io.cloudslang.lang.entities.ScoreLangConstants;
import io.cloudslang.lang.entities.SystemProperty;
import io.cloudslang.lang.entities.bindings.values.Value;
import io.cloudslang.lang.runtime.events.LanguageEventData;
import io.cloudslang.score.events.ScoreEvent;
import io.cloudslang.score.events.ScoreEventListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.springframework.beans.factory.annotation.Autowired;

public class TriggerFlows {

    private static final HashSet FINISHED_EVENTS =
        Sets.newHashSet(ScoreLangConstants.EVENT_EXECUTION_FINISHED, ScoreLangConstants.SLANG_EXECUTION_EXCEPTION);

    private static final HashSet STEP_EVENTS =
        Sets.newHashSet(
            ScoreLangConstants.EVENT_INPUT_END,
            ScoreLangConstants.EVENT_OUTPUT_END,
            ScoreLangConstants.EVENT_ARGUMENT_START,
            ScoreLangConstants.EVENT_ARGUMENT_END
        );

    private static final HashSet BRANCH_EVENTS = Sets.newHashSet(ScoreLangConstants.EVENT_BRANCH_END);

    private static final HashSet PARALLEL_LOOP_EVENTS =
        Sets.newHashSet(ScoreLangConstants.EVENT_JOIN_BRANCHES_END);

    private static final HashSet ALL_CLOUDSLANG_EVENTS =
        Sets.newHashSet(
            ScoreLangConstants.SLANG_EXECUTION_EXCEPTION,
            ScoreLangConstants.EVENT_ACTION_START,
            ScoreLangConstants.EVENT_ACTION_END,
            ScoreLangConstants.EVENT_ACTION_ERROR,
            ScoreLangConstants.EVENT_INPUT_START,
            ScoreLangConstants.EVENT_INPUT_END,
            ScoreLangConstants.EVENT_STEP_START,
            ScoreLangConstants.EVENT_ARGUMENT_START,
            ScoreLangConstants.EVENT_ARGUMENT_END,
            ScoreLangConstants.EVENT_OUTPUT_START,
            ScoreLangConstants.EVENT_OUTPUT_END,
            ScoreLangConstants.EVENT_EXECUTION_FINISHED,
            ScoreLangConstants.EVENT_BRANCH_START,
            ScoreLangConstants.EVENT_BRANCH_END,
            ScoreLangConstants.EVENT_SPLIT_BRANCHES,
            ScoreLangConstants.EVENT_JOIN_BRANCHES_START,
            ScoreLangConstants.EVENT_JOIN_BRANCHES_END
        );

    @Autowired
    private Slang slang;

    public ScoreEvent runSync(
        CompilationArtifact compilationArtifact,
        Map userInputs,
        Set systemProperties) {
        return runSync(compilationArtifact, userInputs, systemProperties, true);
    }

    public ScoreEvent runSync(
        CompilationArtifact compilationArtifact,
        Map userInputs,
        Set systemProperties,
        boolean shouldFail) {
        final BlockingQueue finishEvent = new LinkedBlockingQueue<>();
        ScoreEventListener finishListener = new ScoreEventListener() {
            @Override
            public synchronized void onEvent(ScoreEvent event) throws InterruptedException {
                finishEvent.add(event);
            }
        };
        slang.subscribeOnEvents(finishListener, FINISHED_EVENTS);

        long executionId = slang.run(compilationArtifact, userInputs, systemProperties);

        try {
            ScoreEvent event = null;
            boolean finishEventReceived = false;
            while (!finishEventReceived) {
                event = finishEvent.take();
                long executionIdFromEvent = (long) ((Map) event.getData()).get(LanguageEventData.EXECUTION_ID);
                finishEventReceived = executionId == executionIdFromEvent;
            }
            if (event.getEventType().equals(ScoreLangConstants.SLANG_EXECUTION_EXCEPTION) && shouldFail) {
                LanguageEventData languageEvent = (LanguageEventData) event.getData();
                throw new RuntimeException(languageEvent.getException());
            }
            slang.unSubscribeOnEvents(finishListener);
            return event;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public RuntimeInformation runWithData(
        CompilationArtifact compilationArtifact,
        Map userInputs,
        Set systemProperties) {
        RunDataAggregatorListener runDataAggregatorListener = new RunDataAggregatorListener();
        slang.subscribeOnEvents(runDataAggregatorListener, STEP_EVENTS);

        BranchAggregatorListener branchAggregatorListener = new BranchAggregatorListener();
        slang.subscribeOnEvents(branchAggregatorListener, BRANCH_EVENTS);

        JoinAggregatorListener joinAggregatorListener = new JoinAggregatorListener();
        slang.subscribeOnEvents(joinAggregatorListener, PARALLEL_LOOP_EVENTS);

        runSync(compilationArtifact, userInputs, systemProperties);

        Map steps = runDataAggregatorListener.aggregate();
        Map> branchesByPath = branchAggregatorListener.aggregate();
        Map parallelSteps = joinAggregatorListener.aggregate();

        final RuntimeInformation runtimeInformation = new RuntimeInformation(steps, branchesByPath, parallelSteps);

        slang.unSubscribeOnEvents(joinAggregatorListener);
        slang.unSubscribeOnEvents(branchAggregatorListener);
        slang.unSubscribeOnEvents(runDataAggregatorListener);

        return runtimeInformation;
    }


    public List runAndCollectAllEvents(
        CompilationArtifact compilationArtifact,
        Map userInputs,
        Set systemProperties) {
        final List events = Collections.synchronizedList(new ArrayList());

        ScoreEventListener allEventsListener = new ScoreEventListener() {
            @Override
            public synchronized void onEvent(ScoreEvent event) throws InterruptedException {
                events.add(event);
            }
        };

        slang.subscribeOnEvents(allEventsListener, ALL_CLOUDSLANG_EVENTS);
        ScoreEvent finishEvent = runSync(compilationArtifact, userInputs, systemProperties, false);
        slang.unSubscribeOnEvents(allEventsListener);

        // make sure finish event is saved
        for (ScoreEvent scoreEvent : events) {
            String eventType = scoreEvent.getEventType();
            if (ScoreLangConstants.SLANG_EXECUTION_EXCEPTION.equals(eventType) ||
                ScoreLangConstants.EVENT_EXECUTION_FINISHED.equals(eventType)) {
                return events;
            }
        }
        events.add(finishEvent);

        return events;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy