Skip to content
This repository has been archived by the owner on Mar 4, 2021. It is now read-only.

Commit

Permalink
MKReporterTest: verifies events match expectation
Browse files Browse the repository at this point in the history
  • Loading branch information
bassosimone committed Aug 27, 2019
1 parent 6ba9e4c commit 387a1d3
Showing 1 changed file with 113 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import androidx.test.filters.SmallTest;
import com.google.common.truth.Truth;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import org.junit.Test;

import io.ooni.mk.MKReporterResults;
Expand All @@ -33,7 +34,7 @@ private MKReporterTask initialize() {
MKResourcesManager.getCABundlePath(context));
}

private MKReporterResults submit(MKReporterTask task, MKTMeasurement measurement) {
private MKReporterResults submit(MKReporterTask task, MKTMeasurement measurement, Stats expect) {
final long timeout = 300;
MKReporterResults results = task.maybeDiscoverAndSubmit(
gson.toJson(measurement), timeout
Expand All @@ -44,25 +45,132 @@ private MKReporterResults submit(MKReporterTask task, MKTMeasurement measurement
System.out.println("serializedStats : " + results.getSerializedStats());
Truth.assertThat(results.isGood()).isTrue();
Truth.assertThat(results.getUpdatedReportID().length()).isGreaterThan(0);
Stats real = gson.fromJson(results.getSerializedStats(), Stats.class);
Truth.assertThat(real.equals(expect)).isTrue();
return results;
}

@Test public void submitOne() {
Stats expect = new Stats();
expect.bouncer_okay = 1;
expect.load_request_okay = 1;
expect.open_report_okay = 1;
expect.update_report_okay = 1;
submit(initialize(), new MKTMeasurement(
testName, testVersion, "", ""
));
), expect);
}

@Test public void submitMany() {
MKReporterTask task = initialize();

Stats expect = new Stats();
expect.bouncer_okay = 1;
expect.load_request_okay = 1;
expect.open_report_okay = 1;
expect.update_report_okay = 1;
submit(task, new MKTMeasurement(
testName, testVersion, "a", ""
));
), expect);

expect = new Stats();
expect.load_request_okay = 1;
expect.update_report_okay = 1;
submit(task, new MKTMeasurement(
testName, testVersion, "b", ""
));
), expect);

expect = new Stats();
expect.close_report_okay = 1;
expect.load_request_okay = 1;
expect.open_report_okay = 1;
expect.update_report_okay = 1;
submit(task, new MKTMeasurement(
testName + "x", testVersion, "c", ""
));
), expect);
}

private class Stats {
@SerializedName("bouncer_error")
public long bouncer_error = 0;

@SerializedName("bouncer_no_collectors")
public long bouncer_no_collectors = 0;

@SerializedName("bouncer_okay")
public long bouncer_okay = 0;

@SerializedName("close_report_error")
public long close_report_error = 0;

@SerializedName("close_report_okay")
public long close_report_okay = 0;

@SerializedName("load_request_error")
public long load_request_error = 0;

@SerializedName("load_request_okay")
public long load_request_okay = 0;

@SerializedName("open_report_error")
public long open_report_error = 0;

@SerializedName("open_report_okay")
public long open_report_okay = 0;

@SerializedName("report_id_empty")
public long report_id_empty = 0;

@SerializedName("serialize_measurement_error")
public long serialize_measurement_error = 0;

@SerializedName("update_report_error")
public long update_report_error = 0;

@SerializedName("update_report_okay")
public long update_report_okay = 0;

public boolean equals(Stats other) {
if (bouncer_error != other.bouncer_error) {
return false;
}
if (bouncer_no_collectors != other.bouncer_no_collectors) {
return false;
}
if (bouncer_okay != other.bouncer_okay) {
return false;
}
if (close_report_error != other.close_report_error) {
return false;
}
if (close_report_okay != other.close_report_okay) {
return false;
}
if (load_request_error != other.load_request_error) {
return false;
}
if (load_request_okay != other.load_request_okay) {
return false;
}
if (open_report_error != other.open_report_error) {
return false;
}
if (open_report_okay != other.open_report_okay) {
return false;
}
if (report_id_empty != other.report_id_empty) {
return false;
}
if (serialize_measurement_error != other.serialize_measurement_error) {
return false;
}
if (update_report_error != other.update_report_error) {
return false;
}
if (update_report_okay != other.update_report_okay) {
return false;
}
return true;
}
}
}

0 comments on commit 387a1d3

Please sign in to comment.