Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
PROC-1525: Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharygoodwin committed Jun 18, 2024
1 parent f175040 commit c566c00
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ public class ProctorGroupStubber {
new TestBucket("control", 0, "control", new Payload("controlPayload"));
public static final TestBucket GROUP_1_BUCKET_WITH_PAYLOAD =
new TestBucket("group1", 1, "group1", new Payload("activePayload"));

public static final TestBucket JSON_BUCKET;

static {
try {
JSON_BUCKET =
new TestBucket(
"group1",
1,
"group1",
new Payload(new ObjectMapper().readTree("{\"foo\": 1, \"bar\": 2}")));
} catch (final JsonProcessingException e) {
throw new RuntimeException(e);
}
}

public static final TestBucket GROUP_1_BUCKET = new TestBucket("group1", 2, "group1");

public static final TestBucket GROUP_1_BUCKET_PROPERTY_PAYLOAD;
Expand Down Expand Up @@ -64,7 +80,7 @@ public class ProctorGroupStubber {
public static class ProctorResultStubBuilder {

private final Map<String, ConsumableTestDefinition> definitions = new TreeMap<>();
private final Map<StubTest, TestBucket> resolvedBuckets = new TreeMap<>();
private final Map<String, TestBucket> resolvedBuckets = new TreeMap<>();
private final Map<String, PayloadProperty> properties = new TreeMap<>();

public ProctorResultStubBuilder withStubTest(
Expand Down Expand Up @@ -92,7 +108,7 @@ public ProctorResultStubBuilder withStubTest(
@Nullable final TestBucket resolved,
final ConsumableTestDefinition definition) {
if (resolved != null) {
resolvedBuckets.put(stubTest, resolved);
resolvedBuckets.put(stubTest.getName(), resolved);
}
if (definition != null) {
definitions.put(stubTest.getName(), definition);
Expand All @@ -103,7 +119,7 @@ public ProctorResultStubBuilder withStubTest(
public ProctorResultStubBuilder withStubProperty(
final StubTest stubTest, @Nullable final TestBucket resolved) {
if (resolved != null) {
resolvedBuckets.put(stubTest, resolved);
resolvedBuckets.put(stubTest.getName(), resolved);
assert resolved.getPayload() != null;
assert resolved.getPayload().getJson() != null;
resolved.getPayload()
Expand All @@ -123,17 +139,39 @@ public ProctorResultStubBuilder withStubProperty(
return this;
}

public ProctorResultStubBuilder withStubProperty(
final String testName,
final ConsumableTestDefinition td,
@Nullable final TestBucket resolved) {
if (resolved != null) {
definitions.put(testName, td);
resolvedBuckets.put(testName, resolved);
assert resolved.getPayload() != null;
assert resolved.getPayload().getJson() != null;
resolved.getPayload()
.getJson()
.fields()
.forEachRemaining(
field ->
properties.put(
field.getKey(),
PayloadProperty.builder()
.testName(testName)
.value(field.getValue())
.build()));
}
return this;
}

public ProctorResult build() {
return new ProctorResult(
"0",
resolvedBuckets.entrySet().stream()
.collect(
Collectors.toMap(
e -> e.getKey().getName(), Map.Entry::getValue)),
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)),
resolvedBuckets.entrySet().stream()
.collect(
Collectors.toMap(
e -> e.getKey().getName(),
Map.Entry::getKey,
e ->
new Allocation(
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
Expand All @@ -26,6 +27,7 @@
import static com.indeed.proctor.consumer.ProctorGroupStubber.GROUP_1_BUCKET_PROPERTY_PAYLOAD;
import static com.indeed.proctor.consumer.ProctorGroupStubber.GROUP_1_BUCKET_WITH_PAYLOAD;
import static com.indeed.proctor.consumer.ProctorGroupStubber.INACTIVE_BUCKET;
import static com.indeed.proctor.consumer.ProctorGroupStubber.JSON_BUCKET;
import static com.indeed.proctor.consumer.ProctorGroupStubber.StubTest.CONTROL_SELECTED_TEST;
import static com.indeed.proctor.consumer.ProctorGroupStubber.StubTest.GROUP1_SELECTED_TEST;
import static com.indeed.proctor.consumer.ProctorGroupStubber.StubTest.GROUP_WITH_FALLBACK_TEST;
Expand Down Expand Up @@ -235,6 +237,43 @@ public void testToVerifyGroupsString() {
.isEqualTo("#A1:suppress_logging_example_tst0");
}

@Test
public void testToLoggingWithProperties() {
final ConsumableTestDefinition td =
ConsumableTestDefinition.fromTestDefinition(
TestDefinition.builder()
.setTestType(TestType.RANDOM)
.setSalt("foo")
.setForceLogging(true)
.setPayloadExperimentConfig(
PayloadExperimentConfig.builder()
.priority("123")
.namespaces(ImmutableList.of("test"))
.build())
.build());
final ConsumableTestDefinition tdWithHigherPriority =
ConsumableTestDefinition.fromTestDefinition(
TestDefinition.builder()
.setTestType(TestType.RANDOM)
.setSalt("foo")
.setForceLogging(true)
.setPayloadExperimentConfig(
PayloadExperimentConfig.builder()
.priority("20000")
.namespaces(ImmutableList.of("test"))
.build())
.build());
final ProctorResult result =
new ProctorGroupStubber.ProctorResultStubBuilder()
.withStubProperty(CONTROL_SELECTED_TEST.getName(), td, JSON_BUCKET)
.withStubProperty(
PROPERTY_TEST.getName(), tdWithHigherPriority, JSON_BUCKET)
.build();

assertThat((new AbstractGroups(result) {}).toLoggingString())
.isEqualTo("#A1:propertytest1");
}

@Test
public void testCheckRolledOutAllocation() {
final ConsumableTestDefinition td =
Expand Down

0 comments on commit c566c00

Please sign in to comment.