### Question:
StreamRanges { public static HashStreamRanges ofHash(RangeKeyType keyType, NavigableMap<Long, RangeProperties> ranges) { checkArgument(RangeKeyType.HASH == keyType, "Only hash routing is supported now. %s is not supported.", keyType); NavigableMap<Long, RangeProperties> readOnlyRanges = Collections.unmodifiableNavigableMap(ranges); long maxRangeId = 0L; for (RangeProperties props : ranges.values()) { maxRangeId = Math.max(maxRangeId, props.getRangeId()); } return new HashStreamRanges(readOnlyRanges, maxRangeId); }  static HashStreamRanges ofHash(RangeKeyType keyType,
                                          NavigableMap<Long, RangeProperties> ranges);  }### Answer:
@Test(expected = IllegalArgumentException.class) public void testInvalidRangeKeyType() { StreamRanges.ofHash(RangeKeyType.RAW, Maps.newTreeMap()); }
@Test public void testConstructor() { NavigableMap<Long, RangeProperties> ranges = Maps.newTreeMap(); for (long hashKey = 0L; hashKey < 10L; hashKey++) { RangeProperties props = RangeProperties.newBuilder() .setStorageContainerId(hashKey) .setRangeId(hashKey) .setStartHashKey(hashKey) .setEndHashKey(hashKey) .build(); ranges.put(hashKey, props); } HashStreamRanges hsr = StreamRanges.ofHash( RangeKeyType.HASH, ranges); assertEquals(RangeKeyType.HASH, hsr.getKeyType()); assertEquals(ranges, hsr.getRanges()); assertEquals(9L, hsr.getMaxRangeId()); }
@Test public void testEqual() { NavigableMap<Long, RangeProperties> ranges1 = Maps.newTreeMap(); NavigableMap<Long, RangeProperties> ranges2 = Maps.newTreeMap(); for (long hashKey = 0L; hashKey < 10L; hashKey++) { RangeProperties props1 = RangeProperties.newBuilder() .setStorageContainerId(hashKey) .setRangeId(hashKey) .setStartHashKey(hashKey) .setEndHashKey(hashKey) .build(); ranges1.put(hashKey, props1); RangeProperties props2 = RangeProperties.newBuilder() .setStorageContainerId(hashKey) .setRangeId(hashKey) .setStartHashKey(hashKey) .setEndHashKey(hashKey) .build(); ranges2.put(hashKey, props2); } HashStreamRanges hsr1 = StreamRanges.ofHash( RangeKeyType.HASH, ranges1); HashStreamRanges hsr2 = StreamRanges.ofHash( RangeKeyType.HASH, ranges2); assertEquals(hsr1, hsr2); }
@Test public void testNotEqual() { NavigableMap<Long, RangeProperties> ranges1 = Maps.newTreeMap(); NavigableMap<Long, RangeProperties> ranges2 = Maps.newTreeMap(); for (long hashKey = 0L; hashKey < 10L; hashKey++) { RangeProperties props1 = RangeProperties.newBuilder() .setStorageContainerId(hashKey) .setRangeId(hashKey) .setStartHashKey(hashKey) .setEndHashKey(hashKey) .build(); ranges1.put(hashKey, props1); RangeProperties props2 = RangeProperties.newBuilder() .setStorageContainerId(hashKey) .setRangeId(hashKey + 1) .setStartHashKey(hashKey + 1) .setEndHashKey(hashKey + 1) .build(); ranges2.put(hashKey, props2); } HashStreamRanges hsr1 = StreamRanges.ofHash( RangeKeyType.HASH, ranges1); HashStreamRanges hsr2 = StreamRanges.ofHash( RangeKeyType.HASH, ranges2); assertNotEquals(hsr1, hsr2); }