diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/crud/Attribute.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/crud/Attribute.java index abc5c63a800..97fdeab7d3b 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/crud/Attribute.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/crud/Attribute.java @@ -22,7 +22,7 @@ import static io.fabric8.mockwebserver.crud.AttributeType.WITH; -public class Attribute { +public class Attribute implements Comparable { private final Key key; private final List values; @@ -88,4 +88,9 @@ public String toString() { public AttributeType getType() { return type; } + + @Override + public int compareTo(Attribute o) { + return key.compareTo(o.key); + } } diff --git a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/crud/Key.java b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/crud/Key.java index ca41a2803ea..f32ad04fe68 100644 --- a/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/crud/Key.java +++ b/junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/crud/Key.java @@ -17,7 +17,7 @@ import java.util.Objects; -public class Key { +public class Key implements Comparable { private final String name; @@ -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; diff --git a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/AttributeSetTest.groovy b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/AttributeSetTest.groovy index 6433d79ac3e..11bb29ad2e4 100644 --- a/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/AttributeSetTest.groovy +++ b/junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/AttributeSetTest.groovy @@ -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"() { @@ -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"() {