<commit_before>package integration.tests;

import static org.junit.Assert.assertEquals;
import gr.ntua.vision.monitoring.events.MonitoringEvent;
import gr.ntua.vision.monitoring.rules.PassThroughRule;
import gr.ntua.vision.monitoring.rules.RulesStore;
import gr.ntua.vision.monitoring.rules.StorletLoggingRule;
import gr.ntua.vision.monitoring.rules.VismoRulesEngine;
import gr.ntua.vision.monitoring.sinks.InMemoryEventSink;
import helpers.InMemoryEventDispatcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.junit.Before;
import org.junit.Test;


/**
 * 
 */
public class StorletLoggingRuleTest {
    /***/
    private static final String              EXPECTED_TOPIC    = "_SLLOG";
    /***/
    private static final int                 GROUPS_OF_EVENTS  = 4;
    /***/
    private static final int                 NO_EVENTS_TO_SEND = 100;
    /** the rule's period in seconds. */
    private static final long                PERIOD            = 1000;
    /***/
    private final VismoRulesEngine           engine            = new VismoRulesEngine(new RulesStore());
    /***/
    private final ArrayList<MonitoringEvent> eventsList        = new ArrayList<MonitoringEvent>();
    /***/
    private final InMemoryEventSink          sink              = new InMemoryEventSink(eventsList);
    /***/
    private final FakeStorletEngine          storletEngine     = new FakeStorletEngine(new InMemoryEventDispatcher(engine, "SRE"));


    /***/
    @Before
    public void setUp() {
        engine.appendSink(sink);
    }


    /**
     * @throws InterruptedException
     */
    @Test
    public void storletLoggingRuleAggregatesProperly() throws InterruptedException {
        new PassThroughRule(engine).submit();
        new StorletLoggingRule(engine, PERIOD).submit();

        sendEvents(NO_EVENTS_TO_SEND);
        waitForAggregation();
        assertProperAggregationResult();
        assertLastEventIsFromStorletEngineRule();

        final MonitoringEvent e = eventsList.get(eventsList.size() - 1);
        @SuppressWarnings("unchecked")
        final List<HashMap<String, Object>> groups = (List<HashMap<String, Object>>) e.get("groups");

        assertEquals(GROUPS_OF_EVENTS, groups.size());
        assertProperOrderOfGroupMessages(groups);
    }


    /***/
    private void assertLastEventIsFromStorletEngineRule() {
        assertEquals(eventsList.get(eventsList.size() - 1).topic(), EXPECTED_TOPIC);
    }


    /***/
    private void assertProperAggregationResult() {
        assertEquals(NO_EVENTS_TO_SEND + 1, eventsList.size());
    }


    /**
     * @param noEventsToSend
     */
    private void sendEvents(final int noEventsToSend) {
        for (int i = 0; i < GROUPS_OF_EVENTS; ++i)
            for (int j = 0; j < noEventsToSend / GROUPS_OF_EVENTS; ++j)
                storletEngine.sendEvent("id" + i, "activ-" + i, "foo[ " + i * j + " ]bar");
    }


    /**
     * @param groups
     */
    private static void assertProperOrderOfGroupMessages(final List<HashMap<String, Object>> groups) {
        for (final HashMap<String, Object> group : groups) {
            @SuppressWarnings("unchecked")
            final ArrayList<HashMap<String, Object>> messages = (ArrayList<HashMap<String, Object>>) group.get("messages");

            for (int i = 0; i < messages.size() - 1; ++i) {
                final long currTs = (Long) messages.get(i).get("timestamp");
                final long nextTs = (Long) messages.get(i + 1).get("timestamp");<commit_msg>cleaning up StorletLoggingRuleTest<commit_after>package integration.tests;

import static org.junit.Assert.assertEquals;
import gr.ntua.vision.monitoring.events.MonitoringEvent;
import gr.ntua.vision.monitoring.rules.PassThroughRule;
import gr.ntua.vision.monitoring.rules.RulesStore;
import gr.ntua.vision.monitoring.rules.StorletLoggingRule;
import gr.ntua.vision.monitoring.rules.VismoRulesEngine;
import gr.ntua.vision.monitoring.sinks.InMemoryEventSink;
import helpers.InMemoryEventDispatcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.junit.Before;
import org.junit.Test;


/**
 * 
 */
public class StorletLoggingRuleTest {
    /***/
    private static final String              EXPECTED_TOPIC    = "_SLLOG";
    /***/
    private static final int                 GROUPS_OF_EVENTS  = 4;
    /***/
    private static final int                 NO_EVENTS_TO_SEND = 100;
    /** the rule's period in seconds. */
    private static final long                PERIOD            = 1000;
    /***/
    private final VismoRulesEngine           engine            = new VismoRulesEngine(new RulesStore());
    /***/
    private final ArrayList<MonitoringEvent> eventsList        = new ArrayList<MonitoringEvent>();
    /***/
    private final InMemoryEventSink          sink              = new InMemoryEventSink(eventsList);
    /***/
    private final FakeStorletEngine          storletEngine     = new FakeStorletEngine(new InMemoryEventDispatcher(engine, "SRE"));


    /***/
    @Before
    public void setUp() {
        engine.appendSink(sink);
    }


    /**
     * @throws InterruptedException
     */
    @Test
    public void storletLoggingRuleAggregatesProperly() throws InterruptedException {
        new PassThroughRule(engine).submit();
        new StorletLoggingRule(engine, PERIOD).submit();

        sendEvents(NO_EVENTS_TO_SEND);
        waitForAggregation();
        assertHaveExpectedNoEvents();
        assertLastEventIsFromStorletEngineRule();

        final MonitoringEvent e = eventsList.get(eventsList.size() - 1);
        @SuppressWarnings("unchecked")
        final List<HashMap<String, Object>> groups = (List<HashMap<String, Object>>) e.get("groups");

        assertEquals(GROUPS_OF_EVENTS, groups.size());
        assertProperOrderOfGroupMessages(groups);
    }


    /***/
    private void assertLastEventIsFromStorletEngineRule() {
        assertEquals("last event should've come out of StorletLoggingRule", eventsList.get(eventsList.size() - 1).topic(),
                     EXPECTED_TOPIC);
    }


    /***/
    private void assertHaveExpectedNoEvents() {
        assertEquals("did not collect expected no of events", NO_EVENTS_TO_SEND + 1, eventsList.size());
    }


    /**
     * @param noEventsToSend
     */
    private void sendEvents(final int noEventsToSend) {
        for (int i = 0; i < GROUPS_OF_EVENTS; ++i)
            for (int j = 0; j < noEventsToSend / GROUPS_OF_EVENTS; ++j)
                storletEngine.sendEvent("id" + i, "activ-" + i, "foo[ " + i * j + " ]bar");
    }


    /**
     * @param groups
     */
    private static void assertProperOrderOfGroupMessages(final List<HashMap<String, Object>> groups) {
        for (final HashMap<String, Object> group : groups) {
            @SuppressWarnings("unchecked")
            final ArrayList<HashMap<String, Object>> messages = (ArrayList<HashMap<String, Object>>) group.get("messages");

            for (int i = 0; i < messages.size() - 1; ++i) {
                final long currTs = (Long) messages.get(i).get("timestamp");
                final long nextTs = (Long) messages.get(i + 1).get("timestamp");
{'fraction_non_alphanumeric': 0.11681982734614313, 'fraction_numerical': 0.004455583402951824, 'mean_word_length': 2.7646750524109014, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5564707', 'n_tokens_mistral': 2126, 'n_tokens_neox': 2088, 'n_words': 435}