Skip to content

Commit 74be92b

Browse files
committed
feat(mockwebserver): crud attributes are comparable
Signed-off-by: Marc Nuri <[email protected]>
1 parent 73df8d3 commit 74be92b

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/crud/Attribute.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

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

25-
public class Attribute {
25+
public class Attribute implements Comparable<Attribute> {
2626

2727
private final Key key;
2828
private final List<Value> values;
@@ -88,4 +88,9 @@ public String toString() {
8888
public AttributeType getType() {
8989
return type;
9090
}
91+
92+
@Override
93+
public int compareTo(Attribute o) {
94+
return key.compareTo(o.key);
95+
}
9196
}

junit/mockwebserver/src/main/java/io/fabric8/mockwebserver/crud/Key.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import java.util.Objects;
1919

20-
public class Key {
20+
public class Key implements Comparable<Key> {
2121

2222
private final String name;
2323

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

43+
@Override
44+
public int compareTo(Key o) {
45+
return name.compareTo(o.name);
46+
}
47+
4348
@Override
4449
public String toString() {
4550
return name;

junit/mockwebserver/src/test/groovy/io/fabric8/mockwebserver/crud/AttributeSetTest.groovy

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package io.fabric8.mockwebserver.crud
1717

1818
import spock.lang.Specification
1919

20+
import java.util.stream.Collectors
21+
2022
class AttributeSetTest extends Specification {
2123

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

101-
def "when multiple attributes are specified it should examine all"() {
102-
given:
103-
// Naming is important here as it controls the hashed order
104-
Attribute a2 = new Attribute("key2", "value2")
105-
Attribute a3 = new Attribute("key3", "", AttributeType.EXISTS)
106-
when:
107-
AttributeSet attributeSet = new AttributeSet(a2)
108-
AttributeSet selectorWithOne = new AttributeSet(a2)
109-
AttributeSet selectorWithTwo = new AttributeSet(a2, a3)
110-
def orderedAttributes = new LinkedHashSet([a2, a3]);
111-
then:
112-
113-
// Assert that the order is suitable for testing. The failing attribute should
114-
// be in the *second* position to ensure we're examining all the values of the selector
115-
assert new ArrayList<>(orderedAttributes).indexOf(a3) == 1;
103+
def "when multiple attributes are specified it should not match an attribute set with a single attribute"() {
104+
given: "multiple attributes"
105+
def a2 = new Attribute("key2", "value2")
106+
def a3 = new Attribute("key3", "", AttributeType.EXISTS)
107+
and: "an AttributeSet with only one attribute"
108+
def attributeSet = new AttributeSet(a2)
109+
and: "an AttributeSet with two attributes as selector"
110+
def selectorWithTwo = new AttributeSet(a2, a3)
111+
when: "matching"
112+
def matches = attributeSet.matches(selectorWithTwo)
113+
then: "it should not match"
114+
assert !matches
115+
}
116116

117-
assert attributeSet.matches(selectorWithOne)
118-
assert !attributeSet.matches(selectorWithTwo)
117+
def "when multiple attributes are specified it should examine all"() {
118+
given: "multiple attributes"
119+
def a2 = new Attribute("key2", "value2")
120+
def a3 = new Attribute("key3", "", AttributeType.EXISTS)
121+
and: "an AttributeSet with all attributes"
122+
def attributeSet = new AttributeSet(a2, a3)
123+
when: "listing its values"
124+
def attributes = attributeSet.attributes.values().stream().sorted().collect(Collectors.toList())
125+
then: "it should contain all attributes"
126+
assert attributes.indexOf(a3) == 1
127+
assert attributes.size() == 2
119128
}
120129

121130
def "when IN attribute in selector"() {

0 commit comments

Comments
 (0)