Skip to content

Commit

Permalink
added pure Esper tests for testing a possible state change pattern, t…
Browse files Browse the repository at this point in the history
…wo of them still failing because inital build failure events are not yet matched correctly
  • Loading branch information
phil3k committed Jan 12, 2018
1 parent 471134a commit 3a5f759
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ hs_err_pid*

# IntelliJ project files
*.iml
.idea/*
5 changes: 5 additions & 0 deletions flink-esper/src/test/java/at/datasciencelabs/BuildEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package at.datasciencelabs;


interface BuildEvent {
}
47 changes: 47 additions & 0 deletions flink-esper/src/test/java/at/datasciencelabs/BuildFailure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package at.datasciencelabs;

public class BuildFailure implements BuildEvent {

private String project;
private int buildId;

BuildFailure(String project, int buildId) {
this.project = project;
this.buildId = buildId;
}

public int getBuildId() {
return buildId;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

BuildFailure that = (BuildFailure) o;

if (buildId != that.buildId) return false;
return project != null ? project.equals(that.project) : that.project == null;
}

@Override
public int hashCode() {
int result = project != null ? project.hashCode() : 0;
result = 31 * result + buildId;
return result;
}

public void setBuildId(int buildId) {
this.buildId = buildId;
}

public String getProject() {
return project;
}

public void setProject(String project) {
this.project = project;
}

}
47 changes: 47 additions & 0 deletions flink-esper/src/test/java/at/datasciencelabs/BuildSuccess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package at.datasciencelabs;

public class BuildSuccess implements BuildEvent {

private String project;
private int buildId;

BuildSuccess(String project, int buildId) {
this.project = project;
this.buildId = buildId;
}

public String getProject() {
return project;
}

public void setProject(String project) {
this.project = project;
}


public int getBuildId() {
return buildId;
}

public void setBuildId(int buildId) {
this.buildId = buildId;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

BuildSuccess that = (BuildSuccess) o;

if (buildId != that.buildId) return false;
return project != null ? project.equals(that.project) : that.project == null;
}

@Override
public int hashCode() {
int result = project != null ? project.hashCode() : 0;
result = 31 * result + buildId;
return result;
}
}
4 changes: 4 additions & 0 deletions flink-esper/src/test/java/at/datasciencelabs/EsperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import com.espertech.esper.event.map.MapEventBean;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import org.junit.Ignore;
import org.junit.Test;

public class EsperTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package at.datasciencelabs;

import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.event.map.MapEventBean;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StateChangePatternTest {

private EPServiceProvider engine;

private static final String STATE_CHANGE_PATTERN = "(every(A=BuildSuccess) -> (B=BuildFailure(project=A.project) and not A=BuildSuccess))";

private EventBuilder eventsBuilder;

@Before
public void onBefore() {
engine = EPServiceProviderManager.getDefaultProvider();

engine.getEPAdministrator().getConfiguration().addEventType(BuildSuccess.class);
engine.getEPAdministrator().getConfiguration().addEventType(BuildFailure.class);

eventsBuilder = new EventBuilder();
}

@Test
public void patternDetected() {
List<BuildEvent> buildEvents = Lists.newArrayList(
eventsBuilder.expectedBuildSuccess(),
eventsBuilder.expectedBuildFailure()
);

runWithPatternAndEvents(STATE_CHANGE_PATTERN, buildEvents);
}

@Test
public void precedingBuildSuccessIgnored() {
List<BuildEvent> buildEvents = Lists.newArrayList(
eventsBuilder.buildSuccess(),
eventsBuilder.expectedBuildSuccess(),
eventsBuilder.expectedBuildFailure()
);

runWithPatternAndEvents(STATE_CHANGE_PATTERN, buildEvents);
}

@Test
public void patternDetectedTwice() {

List<BuildEvent> buildEvents = Lists.newArrayList(
eventsBuilder.expectedBuildSuccess(),
eventsBuilder.expectedBuildFailure(),
eventsBuilder.expectedBuildSuccess(),
eventsBuilder.expectedBuildFailure()
);

runWithPatternAndEvents(STATE_CHANGE_PATTERN, buildEvents);
}

@Test
public void eventsSeparatedByProject() {

List<BuildEvent> buildEvents = Lists.newArrayList(
eventsBuilder.buildSuccess(),
eventsBuilder.expectedBuildSuccess("project2"),
eventsBuilder.expectedBuildFailure("project2"),
eventsBuilder.expectedBuildSuccess(),
eventsBuilder.expectedBuildFailure()
);

runWithPatternAndEvents(STATE_CHANGE_PATTERN, buildEvents);
}

@Test
public void singleBuildFailureDetected() {
List<BuildEvent> buildEvents = Lists.newArrayList(
eventsBuilder.expectedBuildFailure()
);
runWithPatternAndEvents(STATE_CHANGE_PATTERN, buildEvents);
}

@Test
public void precedingBuildFailureDetected() {

List<BuildEvent> buildEvents = Lists.newArrayList(
eventsBuilder.expectedBuildFailure(),
eventsBuilder.expectedBuildSuccess(),
eventsBuilder.expectedBuildFailure()
);

runWithPatternAndEvents(STATE_CHANGE_PATTERN, buildEvents);
}

private void runWithPatternAndEvents(String pattern, List<BuildEvent> buildEvents) {
EPStatement epStatement = engine.getEPAdministrator().createPattern(pattern);

Map<String, List<BuildEvent>> actualBuildEvents = Maps.newHashMap();
epStatement.addListener((newData, oldData) -> {
Joiner.MapJoiner joiner = Joiner.on(",").withKeyValueSeparator("=");
Lists.newArrayList(newData).forEach(___ -> System.out.println(joiner.join(((MapEventBean) ___).getProperties())));
Lists.newArrayList(newData).forEach(___ -> ((MapEventBean) ___).getProperties().entrySet().forEach(entry -> {
actualBuildEvents.putIfAbsent(entry.getKey(), new ArrayList<>());
actualBuildEvents.get(entry.getKey()).add((BuildEvent) ((EventBean) entry.getValue()).getUnderlying());
}));
});

buildEvents.forEach(___ -> engine.getEPRuntime().sendEvent(___));

Map<String, List<BuildEvent>> expectedBuildEvents = Maps.newHashMap();
for (BuildEvent buildEvent : buildEvents) {
if (buildEvent instanceof Expected) {
String key = ((Expected) buildEvent).getKey();
expectedBuildEvents.putIfAbsent(key, new ArrayList<>());
expectedBuildEvents.get(key).add(buildEvent);
}
}

Assert.assertEquals(expectedBuildEvents, actualBuildEvents);
}

private class ExpectedBuildFailure extends BuildFailure implements Expected {

private final String key;

ExpectedBuildFailure(String project, int buildId, String key) {
super(project, buildId);
this.key = key;
}

@Override
public String getKey() {
return key;
}
}

private class ExpectedBuildSuccess extends BuildSuccess implements Expected {
private String key;

ExpectedBuildSuccess(String project, int buildId, String key) {
super(project, buildId);
this.key = key;
}

@Override
public String getKey() {
return key;
}
}

private class EventBuilder {
private int buildId = 1;

BuildFailure expectedBuildFailure(String project) {
return new ExpectedBuildFailure(project, buildId++, "B");
}

BuildSuccess expectedBuildSuccess(String project) {
return new ExpectedBuildSuccess(project, buildId++, "A");
}

BuildSuccess expectedBuildSuccess() {
return expectedBuildSuccess("project1");
}

BuildFailure expectedBuildFailure() {
return expectedBuildFailure("project1");
}

BuildSuccess buildSuccess() {
return new BuildSuccess("project1", buildId++);
}
}

private interface Expected {
String getKey();
}
}

0 comments on commit 3a5f759

Please sign in to comment.