This is an automated email from the ASF dual-hosted git repository. tabish pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-protonj2.git
The following commit(s) were added to refs/heads/main by this push: new f456aea6 PROTON-2739 Update the AmqpValue matcher to handle map order f456aea6 is described below commit f456aea6427b8085852b489815bad56d07fbbc46 Author: Timothy Bish <tabish...@gmail.com> AuthorDate: Thu Jun 1 14:42:44 2023 -0400 PROTON-2739 Update the AmqpValue matcher to handle map order The AmqpValue matcher should compare maps without concern for order of the entries as they can get out of order depending on iteration of the type of Map on encode and decode. --- .../protonj2/test/driver/codec/MapElement.java | 6 +- .../matchers/types/EncodedAmqpTypeMatcher.java | 37 ++++++- .../matches/types/EncodedAmqpValueMatcherTest.java | 117 +++++++++++++++++++++ 3 files changed, 157 insertions(+), 3 deletions(-) diff --git a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/codec/MapElement.java b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/codec/MapElement.java index 912145f5..6564751d 100644 --- a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/codec/MapElement.java +++ b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/codec/MapElement.java @@ -46,11 +46,13 @@ class MapElement extends AbstractElement<Map<Object, Object>> { int count = 0; int size = 0; Element<?> elt = first; + while (elt != null) { count++; size += elt.size(); elt = elt.next(); } + if (isElementOfArray()) { ArrayElement parent = (ArrayElement) parent(); @@ -77,8 +79,10 @@ class MapElement extends AbstractElement<Map<Object, Object>> { @Override public Map<Object, Object> getValue() { - LinkedHashMap<Object, Object> map = new LinkedHashMap<>(); + final Map<Object, Object> map = new LinkedHashMap<>(); + Element<?> elt = first; + while (elt != null) { Object key = elt.getValue(); Object value; diff --git a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/matchers/types/EncodedAmqpTypeMatcher.java b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/matchers/types/EncodedAmqpTypeMatcher.java index 59743d27..2c6539a3 100644 --- a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/matchers/types/EncodedAmqpTypeMatcher.java +++ b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/matchers/types/EncodedAmqpTypeMatcher.java @@ -18,7 +18,13 @@ */ package org.apache.qpid.protonj2.test.driver.matchers.types; +import static org.hamcrest.Matchers.arrayContainingInAnyOrder; +import static org.hamcrest.Matchers.everyItem; +import static org.hamcrest.Matchers.in; +import static org.hamcrest.Matchers.is; + import java.nio.ByteBuffer; +import java.util.Map; import org.apache.qpid.protonj2.test.driver.codec.Codec; import org.apache.qpid.protonj2.test.driver.codec.primitives.DescribedType; @@ -26,6 +32,7 @@ import org.apache.qpid.protonj2.test.driver.codec.primitives.Symbol; import org.apache.qpid.protonj2.test.driver.codec.primitives.UnsignedLong; import org.hamcrest.Description; import org.hamcrest.Matcher; +import org.hamcrest.Matchers; import org.hamcrest.TypeSafeMatcher; public abstract class EncodedAmqpTypeMatcher extends TypeSafeMatcher<ByteBuffer> { @@ -72,8 +79,34 @@ public abstract class EncodedAmqpTypeMatcher extends TypeSafeMatcher<ByteBuffer> if (!matcher.matches(decodedDescribedType.getDescribed())) { return false; } - } else if (!expectedValue.equals(decodedDescribedType.getDescribed())) { - return false; + } else if (expectedValue instanceof Map<?, ?>) { + final Map<?, ?> expectedMap = (Map<?, ?>) expectedValue; + + if (!(decodedDescribedType.getDescribed() instanceof Map)) { + return false; + } + + final Map<?, ?> receivedMap = (Map<?, ?>) decodedDescribedType.getDescribed(); + + final Matcher<?> everyItemMatcher = everyItem(is(in(expectedMap.entrySet()))); + final Matcher<?> containsInAnyOrder = arrayContainingInAnyOrder(expectedMap.entrySet().toArray()); + + if (receivedMap.size() != expectedMap.size()) { + return false; + } + + if (!everyItemMatcher.matches(receivedMap.entrySet())) { + return false; + } + + if (!containsInAnyOrder.matches(receivedMap.entrySet().toArray())) { + return false; + } + } else { + final Matcher<?> expectedValueMatcher = Matchers.is(expectedValue); + if (!expectedValueMatcher.matches(decodedDescribedType.getDescribed())) { + return false; + } } } diff --git a/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/matches/types/EncodedAmqpValueMatcherTest.java b/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/matches/types/EncodedAmqpValueMatcherTest.java new file mode 100644 index 00000000..9916ad58 --- /dev/null +++ b/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/matches/types/EncodedAmqpValueMatcherTest.java @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.qpid.protonj2.test.driver.matches.types; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.qpid.protonj2.test.driver.codec.Codec; +import org.apache.qpid.protonj2.test.driver.codec.messaging.AmqpValue; +import org.apache.qpid.protonj2.test.driver.codec.primitives.DescribedType; +import org.apache.qpid.protonj2.test.driver.matchers.types.EncodedAmqpValueMatcher; +import org.junit.jupiter.api.Test; + +/** + * Test encoded AMQP value type matcher + */ +public class EncodedAmqpValueMatcherTest { + + private final Codec codec = Codec.Factory.create(); + + @Test + public void testMatchesString() { + final EncodedAmqpValueMatcher matcher = new EncodedAmqpValueMatcher("test"); + + final ByteBuffer passingValue = encodeProtonPerformative(new AmqpValue("test")); + final ByteBuffer failingValue = encodeProtonPerformative(new AmqpValue("fail")); + + assertTrue(matcher.matches(passingValue)); + assertFalse(matcher.matches(failingValue)); + } + + @Test + public void testMatchingMaps() { + final List<String> list1 = new ArrayList<>(); + list1.add("list1"); + final List<String> list2 = new ArrayList<>(); + list1.add("list2"); + + final Map<String, Object> passingMap = new HashMap<>(); + passingMap.put("entry1", "entry-pass"); + passingMap.put("entry2", list1); + final Map<String, Object> failingMap = new HashMap<>(); + failingMap.put("entry1", "entry-fail"); + failingMap.put("entry2", list2); + + final EncodedAmqpValueMatcher matcher = new EncodedAmqpValueMatcher(passingMap); + + final ByteBuffer passingValue = encodeProtonPerformative(new AmqpValue(passingMap)); + final ByteBuffer failingValue = encodeProtonPerformative(new AmqpValue(failingMap)); + + assertTrue(matcher.matches(passingValue)); + assertFalse(matcher.matches(failingValue)); + } + + @Test + public void testMatchingMapsOfLists() { + final List<String> list1 = new ArrayList<>(); + list1.add("list1"); + final List<String> list2 = new ArrayList<>(); + list1.add("list2"); + + final Map<String, Object> passingMap = new HashMap<>(); + passingMap.put("entry2", list1); + final Map<String, Object> failingMap = new HashMap<>(); + failingMap.put("entry2", list2); + + final EncodedAmqpValueMatcher matcher = new EncodedAmqpValueMatcher(passingMap); + + final ByteBuffer passingValue = encodeProtonPerformative(new AmqpValue(passingMap)); + final ByteBuffer failingValue = encodeProtonPerformative(new AmqpValue(failingMap)); + + assertTrue(matcher.matches(passingValue)); + assertFalse(matcher.matches(failingValue)); + } + + private ByteBuffer encodeProtonPerformative(DescribedType performative) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(256); + + if (performative != null) { + try (DataOutputStream output = new DataOutputStream(baos)) { + codec.putDescribedType(performative); + codec.encode(output); + } catch (IOException e) { + throw new UncheckedIOException(e); + } finally { + codec.clear(); + } + } + + return ByteBuffer.wrap(baos.toByteArray()); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org