Skip to content

Commit

Permalink
feat(mockwebserver): crud attributes are comparable
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Jan 3, 2024
1 parent 73df8d3 commit 74be92b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import static io.fabric8.mockwebserver.crud.AttributeType.WITH;

public class Attribute {
public class Attribute implements Comparable<Attribute> {

private final Key key;
private final List<Value> values;
Expand Down Expand Up @@ -88,4 +88,9 @@ public String toString() {
public AttributeType getType() {
return type;
}

@Override
public int compareTo(Attribute o) {
return key.compareTo(o.key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import java.util.Objects;

public class Key {
public class Key implements Comparable<Key> {

private final String name;

Expand All @@ -40,6 +40,11 @@ public int hashCode() {
return Objects.hash(name);
}

@Override
public int compareTo(Key o) {
return name.compareTo(o.name);
}

@Override
public String toString() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package io.fabric8.mockwebserver.crud

import spock.lang.Specification

import java.util.stream.Collectors

class AttributeSetTest extends Specification {

def "when two feature set are empty the should be equals"() {
Expand Down Expand Up @@ -98,24 +100,31 @@ class AttributeSetTest extends Specification {
assert attributeSet.matches(selector)
}

def "when multiple attributes are specified it should examine all"() {
given:
// Naming is important here as it controls the hashed order
Attribute a2 = new Attribute("key2", "value2")
Attribute a3 = new Attribute("key3", "", AttributeType.EXISTS)
when:
AttributeSet attributeSet = new AttributeSet(a2)
AttributeSet selectorWithOne = new AttributeSet(a2)
AttributeSet selectorWithTwo = new AttributeSet(a2, a3)
def orderedAttributes = new LinkedHashSet([a2, a3]);
then:

// Assert that the order is suitable for testing. The failing attribute should
// be in the *second* position to ensure we're examining all the values of the selector
assert new ArrayList<>(orderedAttributes).indexOf(a3) == 1;
def "when multiple attributes are specified it should not match an attribute set with a single attribute"() {
given: "multiple attributes"
def a2 = new Attribute("key2", "value2")
def a3 = new Attribute("key3", "", AttributeType.EXISTS)
and: "an AttributeSet with only one attribute"
def attributeSet = new AttributeSet(a2)
and: "an AttributeSet with two attributes as selector"
def selectorWithTwo = new AttributeSet(a2, a3)
when: "matching"
def matches = attributeSet.matches(selectorWithTwo)
then: "it should not match"
assert !matches
}

assert attributeSet.matches(selectorWithOne)
assert !attributeSet.matches(selectorWithTwo)
def "when multiple attributes are specified it should examine all"() {
given: "multiple attributes"
def a2 = new Attribute("key2", "value2")
def a3 = new Attribute("key3", "", AttributeType.EXISTS)
and: "an AttributeSet with all attributes"
def attributeSet = new AttributeSet(a2, a3)
when: "listing its values"
def attributes = attributeSet.attributes.values().stream().sorted().collect(Collectors.toList())
then: "it should contain all attributes"
assert attributes.indexOf(a3) == 1
assert attributes.size() == 2
}

def "when IN attribute in selector"() {
Expand Down

0 comments on commit 74be92b

Please sign in to comment.