This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from zacharygoodwin/PROC-1523
PROC-1523: Add namespaces filter
- Loading branch information
Showing
3 changed files
with
181 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
proctor-common/src/main/java/com/indeed/proctor/common/dynamic/NamespacesFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.indeed.proctor.common.dynamic; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.indeed.proctor.common.model.ConsumableTestDefinition; | ||
import com.indeed.proctor.common.model.PayloadExperimentConfig; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.Collections; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
@JsonTypeName("namespaces_filter") | ||
public class NamespacesFilter implements DynamicFilter { | ||
private final Set<String> namespaces; | ||
|
||
public NamespacesFilter(@JsonProperty("namespaces") final Set<String> namespaces) { | ||
Preconditions.checkArgument( | ||
!CollectionUtils.isEmpty(namespaces), | ||
"namespaces should be non-empty string list."); | ||
this.namespaces = ImmutableSet.copyOf(namespaces); | ||
} | ||
|
||
@JsonProperty("namespaces") | ||
public Set<String> getNamespaces() { | ||
return this.namespaces; | ||
} | ||
|
||
@Override | ||
public boolean matches(final String testName, final ConsumableTestDefinition testDefinition) { | ||
final boolean isMatched = | ||
Optional.ofNullable(testDefinition.getPayloadExperimentConfig()) | ||
.map(PayloadExperimentConfig::getNamespaces).orElse(Collections.emptyList()) | ||
.stream() | ||
.anyMatch(this.namespaces::contains); | ||
testDefinition.setDynamic(isMatched); | ||
return isMatched; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
final NamespacesFilter that = (NamespacesFilter) o; | ||
return namespaces.equals(that.namespaces); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(namespaces); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
proctor-common/src/test/java/com/indeed/proctor/common/dynamic/TestNamespacesFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.indeed.proctor.common.dynamic; | ||
|
||
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.indeed.proctor.common.Serializers; | ||
import com.indeed.proctor.common.model.ConsumableTestDefinition; | ||
import com.indeed.proctor.common.model.PayloadExperimentConfig; | ||
import com.indeed.proctor.common.model.TestType; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
import static java.util.Collections.*; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class TestNamespacesFilter { | ||
private static final ObjectMapper OBJECT_MAPPER = Serializers.lenient(); | ||
|
||
@Test | ||
public void testMatchEmptyNamespaces() { | ||
assertThatThrownBy(() -> new NamespacesFilter(emptySet())) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("namespaces should be non-empty string list."); | ||
} | ||
|
||
@Test | ||
public void testMatchSingleNamespaces() { | ||
final NamespacesFilter filter = new NamespacesFilter(ImmutableSet.of("test")); | ||
|
||
assertFilterMatchesEquals(true, filter, ImmutableList.of("test")); | ||
assertFilterMatchesEquals(true, filter, ImmutableList.of("foo", "test")); | ||
|
||
assertFilterMatchesEquals(false, filter, emptyList()); | ||
assertFilterMatchesEquals(false, filter, ImmutableList.of("foo_test")); | ||
} | ||
|
||
@Test | ||
public void testMatchNamespaces() { | ||
final NamespacesFilter filter = new NamespacesFilter(ImmutableSet.of("test1", "test2")); | ||
|
||
assertFilterMatchesEquals(true, filter, ImmutableList.of("test1")); | ||
assertFilterMatchesEquals(true, filter, ImmutableList.of("test2")); | ||
assertFilterMatchesEquals(true, filter, ImmutableList.of("test1", "test2")); | ||
assertFilterMatchesEquals(true, filter, ImmutableList.of("test2", "test1")); | ||
assertFilterMatchesEquals(true, filter, ImmutableList.of("foo", "test1", "test2")); | ||
|
||
assertFilterMatchesEquals(false, filter, emptyList()); | ||
} | ||
|
||
private void assertFilterMatchesEquals( | ||
final boolean expected, | ||
final NamespacesFilter filter, | ||
final List<String> testNamespaces) { | ||
final ConsumableTestDefinition definition = | ||
new ConsumableTestDefinition( | ||
"", | ||
null, | ||
TestType.EMAIL_ADDRESS, | ||
null, | ||
emptyList(), | ||
emptyList(), | ||
false, | ||
emptyMap(), | ||
null, | ||
EMPTY_LIST); | ||
definition.setPayloadExperimentConfig( | ||
PayloadExperimentConfig.builder().namespaces(testNamespaces).build()); | ||
|
||
assertEquals(expected, filter.matches("", definition)); | ||
assertEquals(expected, definition.getDynamic()); | ||
} | ||
|
||
@Test | ||
public void testSerializeMetaTagsFilter() throws JsonProcessingException { | ||
final NamespacesFilter filter = new NamespacesFilter(ImmutableSet.of("test1", "test2")); | ||
|
||
final String serializedFilter = OBJECT_MAPPER.writeValueAsString(filter); | ||
|
||
assertEquals( | ||
"{\"type\":\"namespaces_filter\",\"namespaces\":[\"test1\",\"test2\"]}", | ||
serializedFilter); | ||
} | ||
} |