ByteArrayMapMatcher.java

package redis.clients.jedis.util;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import java.util.Map;
import java.util.Arrays;

public class ByteArrayMapMatcher extends TypeSafeMatcher<Map<byte[], byte[]>> {

  private final Map<byte[], byte[]> expected;

  public ByteArrayMapMatcher(Map<byte[], byte[]> expected) {
    this.expected = expected;
  }

  @Override
  protected boolean matchesSafely(Map<byte[], byte[]> actual) {
    if (actual == null) {
      return expected == null;
    }

    if (actual.size() != expected.size()) return false;

    // For each expected key, find the matching key in actual and verify the value matches
    for (Map.Entry<byte[], byte[]> expectedEntry : expected.entrySet()) {
      byte[] expectedKey = expectedEntry.getKey();
      byte[] expectedValue = expectedEntry.getValue();

      // Find the actual entry with matching key
      boolean keyFound = false;
      for (Map.Entry<byte[], byte[]> actualEntry : actual.entrySet()) {
        if (Arrays.equals(expectedKey, actualEntry.getKey())) {
          keyFound = true;
          // Verify the value for this key matches
          if (!Arrays.equals(expectedValue, actualEntry.getValue())) {
            return false; // Key found but value doesn't match
          }
          break;
        }
      }

      if (!keyFound) {
        return false; // Expected key not found in actual
      }
    }

    return true;
  }

  @Override
  public void describeTo(Description description) {
    description.appendText("maps to be equal by byte[] content");
  }

  public static ByteArrayMapMatcher contentEquals(Map<byte[], byte[]> expected) {
    return new ByteArrayMapMatcher(expected);
  }
}