method2testcases
stringlengths
118
6.63k
### Question: NestedIntervalMap implements IntervalMap<K, V> { @Override public void clear() { children.clear(); } NestedIntervalMap(); NestedIntervalMap(NestedIntervalMap<K, V> source); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); @Override void clear(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); }### Answer: @Test public void clear() { subject.put(N1_12, N1_1); subject.clear(); assertThat(subject.findExact(N1_12), not(contains(N1_12))); assertThat(subject.findExact(N1_12), not(contains(N1_1))); }
### Question: NestedIntervalMap implements IntervalMap<K, V> { @Override public void put(K key, V value) { Validate.notNull(key); Validate.notNull(value); children.addChild(new InternalNode<>(key, value)); } NestedIntervalMap(); NestedIntervalMap(NestedIntervalMap<K, V> source); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); @Override void clear(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); }### Answer: @Test public void fail_on_intersecting_siblings() { try { subject.put(new Ipv4Resource(8, 13), N1_1); fail("Exception expected"); } catch (IntersectingIntervalException expected) { assertEquals(new Ipv4Resource(8, 13), expected.getInterval()); assertEquals(asList(N1_12), expected.getIntersections()); } }
### Question: NestedIntervalMap implements IntervalMap<K, V> { @Override public void remove(K key) { Validate.notNull(key); children.removeChild(key); } NestedIntervalMap(); NestedIntervalMap(NestedIntervalMap<K, V> source); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); @Override void clear(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); }### Answer: @Test public void test_remove_key_value_nonexistant() { NestedIntervalMap<Ipv4Resource, Ipv4Resource> copy = new NestedIntervalMap<>(subject); final Ipv4Resource resource = new Ipv4Resource(0, 100); subject.remove(resource, resource); assertEquals(copy, subject); } @Test public void test_remove_nonexistant() { NestedIntervalMap<Ipv4Resource, Ipv4Resource> copy = new NestedIntervalMap<>(subject); subject.remove(new Ipv4Resource(0, 100)); assertEquals(copy, subject); subject.remove(new Ipv4Resource(1, 7)); assertEquals(copy, subject); subject.remove(new Ipv4Resource(12, 12)); assertEquals(copy, subject); }
### Question: NestedIntervalMap implements IntervalMap<K, V> { @Override public List<V> findAllLessSpecific(K key) { Validate.notNull(key); return mapToValues(internalFindAllLessSpecific(key)); } NestedIntervalMap(); NestedIntervalMap(NestedIntervalMap<K, V> source); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); @Override void clear(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); }### Answer: @Test public void test_find_all_less_specific() { assertEquals(Collections.emptyList(), subject.findAllLessSpecific(new Ipv4Resource(0, 100))); assertEquals(Collections.emptyList(), subject.findAllLessSpecific(new Ipv4Resource(5, 13))); assertEquals(Collections.emptyList(), subject.findAllLessSpecific(N1_12)); assertEquals(asList(N1_12, N5_10, N5_8), subject.findAllLessSpecific(N6_6)); assertEquals(asList(N1_12, N5_10, N5_8), subject.findAllLessSpecific(N8_8)); assertEquals(asList(N1_12, N1_4), subject.findAllLessSpecific(N2_2)); }
### Question: NestedIntervalMap implements IntervalMap<K, V> { @Override public List<V> findExactAndAllLessSpecific(K key) { Validate.notNull(key); return mapToValues(internalFindExactAndAllLessSpecific(key)); } NestedIntervalMap(); NestedIntervalMap(NestedIntervalMap<K, V> source); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); @Override void clear(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); }### Answer: @Test public void test_find_exact_and_all_less_specific() { assertEquals(Collections.emptyList(), subject.findExactAndAllLessSpecific(new Ipv4Resource(0, 100))); assertEquals(Collections.emptyList(), subject.findExactAndAllLessSpecific(new Ipv4Resource(5, 13))); assertEquals(asList(N1_12), subject.findExactAndAllLessSpecific(N1_12)); assertEquals(asList(N1_12, N5_10, N5_8, N6_6), subject.findExactAndAllLessSpecific(N6_6)); assertEquals(asList(N1_12, N5_10, N5_8), subject.findExactAndAllLessSpecific(N8_8)); assertEquals(asList(N1_12, N1_4, N2_2), subject.findExactAndAllLessSpecific(N2_2)); }
### Question: NestedIntervalMap implements IntervalMap<K, V> { @Override public List<V> findExactOrFirstLessSpecific(K key) { Validate.notNull(key); return mapToValues(internalFindExactOrFirstLessSpecific(key)); } NestedIntervalMap(); NestedIntervalMap(NestedIntervalMap<K, V> source); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); @Override void clear(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); }### Answer: @Test public void test_find_exact_or_first_less_specific() { assertThat(subject.findExactOrFirstLessSpecific(new Ipv4Resource(0, 100)), hasSize(0)); assertThat(subject.findExactOrFirstLessSpecific(new Ipv4Resource(5, 13)), hasSize(0)); assertThat(subject.findExactOrFirstLessSpecific(N1_12), contains(N1_12)); assertThat(subject.findExactOrFirstLessSpecific(N6_6), contains(N6_6)); assertThat(subject.findExactOrFirstLessSpecific(N8_8), contains(N5_8)); assertThat(subject.findExactOrFirstLessSpecific(N2_2), contains(N2_2)); }
### Question: NestedIntervalMap implements IntervalMap<K, V> { @Override public List<V> findFirstLessSpecific(K key) { Validate.notNull(key); InternalNode<K, V> node = internalFindFirstLessSpecific(key); return mapToValues(node); } NestedIntervalMap(); NestedIntervalMap(NestedIntervalMap<K, V> source); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); @Override void clear(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); }### Answer: @Test public void testFindFirstLessSpecific() { assertThat(subject.findFirstLessSpecific(N1_12), hasSize(0)); assertThat(subject.findFirstLessSpecific(N6_6), contains(N5_8)); assertThat(subject.findFirstLessSpecific(N8_8), contains(N5_8)); assertThat(subject.findFirstLessSpecific(N2_2), contains(N1_4)); assertThat(subject.findFirstLessSpecific(new Ipv4Resource(3, 7)), contains(N1_12)); }
### Question: NestedIntervalMap implements IntervalMap<K, V> { @Override public List<V> findFirstMoreSpecific(K key) { Validate.notNull(key); return mapToValues(internalFindFirstMoreSpecific(key)); } NestedIntervalMap(); NestedIntervalMap(NestedIntervalMap<K, V> source); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); @Override void clear(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); }### Answer: @Test public void testFindFirstMoreSpecific() { assertEquals(asList(N5_8, N9_10), subject.findFirstMoreSpecific(N5_10)); assertEquals(asList(N1_1, N2_2, N3_4), subject.findFirstMoreSpecific(N1_4)); assertEquals(asList(N7_7, N9_9), subject.findFirstMoreSpecific(new Ipv4Resource(7, 9))); assertEquals(asList(N9_9), subject.findFirstMoreSpecific(new Ipv4Resource(8, 9))); }
### Question: NestedIntervalMap implements IntervalMap<K, V> { @Override public List<V> findExact(K key) { Validate.notNull(key); InternalNode<K, V> node = internalFindExact(key); return mapToValues(node); } NestedIntervalMap(); NestedIntervalMap(NestedIntervalMap<K, V> source); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); @Override void clear(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); }### Answer: @Test public void testFindExact() { for (Ipv4Resource n : all) { assertThat(subject.findExact(n), contains(n)); } }
### Question: NestedIntervalMap implements IntervalMap<K, V> { @Override public List<V> findAllMoreSpecific(K key) { Validate.notNull(key); return mapToValues(internalFindAllMoreSpecific(key)); } NestedIntervalMap(); NestedIntervalMap(NestedIntervalMap<K, V> source); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); @Override void clear(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); }### Answer: @Test public void testFindAllMoreSpecific() { assertEquals(all.subList(1, all.size()), subject.findAllMoreSpecific(N1_12)); assertEquals(asList(N3_4, N3_3, N4_4, N5_5, N6_6, N7_7), subject.findAllMoreSpecific(new Ipv4Resource(3, 7))); assertEquals(asList(N9_9), subject.findAllMoreSpecific(new Ipv4Resource(8, 9))); }
### Question: NestedIntervalMap implements IntervalMap<K, V> { @Override public List<V> findExactAndAllMoreSpecific(K key) { Validate.notNull(key); return mapToValues(internalFindExactAndAllMoreSpecific(key)); } NestedIntervalMap(); NestedIntervalMap(NestedIntervalMap<K, V> source); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); @Override void clear(); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); }### Answer: @Test public void testFindExactAndAllMoreSpecific() { assertEquals(all, subject.findExactAndAllMoreSpecific(N1_12)); assertEquals(asList(N1_4, N1_1, N2_2, N3_4, N3_3, N4_4), subject.findExactAndAllMoreSpecific(N1_4)); }
### Question: MultiValueIntervalMap implements IntervalMap<K, V> { @Override public List<V> findExact(K key) { return unroll(wrapped.findExact(key)); } MultiValueIntervalMap(); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override void clear(); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); }### Answer: @Test public void findExact_k11() { final List<String> result = subject.findExact(k_11); assertThat(result, contains(v_11)); } @Test public void findExact_k13() { final List<String> result = subject.findExact(k_13); assertThat(result, contains(v_131, v_132, v_133)); } @Test public void findExact() { final List<String> result = subject.findExact(k_12); assertThat(result, contains(v_121, v_122)); }
### Question: MultiValueIntervalMap implements IntervalMap<K, V> { @Override public void remove(K key) { wrapped.remove(key); } MultiValueIntervalMap(); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override void clear(); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); }### Answer: @Test public void remove() { subject.remove(k_11); final List<String> result = subject.findExact(k_11); assertThat(result, hasSize(0)); }
### Question: MultiValueIntervalMap implements IntervalMap<K, V> { @Override public void clear() { wrapped.clear(); } MultiValueIntervalMap(); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override void clear(); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); }### Answer: @Test public void clear() { subject.clear(); final List<String> result = subject.findAllMoreSpecific(Ipv4Resource.MAX_RANGE); assertThat(result, hasSize(0)); }
### Question: MultiValueIntervalMap implements IntervalMap<K, V> { @Override public List<V> findFirstLessSpecific(K key) { return unroll(wrapped.findFirstLessSpecific(key)); } MultiValueIntervalMap(); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override void clear(); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); }### Answer: @Test public void findFirstLessSpecific() { final List<String> result = subject.findFirstLessSpecific(k_11); assertThat(result, contains(v_121, v_122)); }
### Question: MultiValueIntervalMap implements IntervalMap<K, V> { @Override public List<V> findExactOrFirstLessSpecific(K key) { return unroll(wrapped.findExactOrFirstLessSpecific(key)); } MultiValueIntervalMap(); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override void clear(); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); }### Answer: @Test public void findExactOrFirstLessSpecific() { final List<String> result = subject.findExactOrFirstLessSpecific(k_12); assertThat(result, contains(v_121, v_122)); }
### Question: MultiValueIntervalMap implements IntervalMap<K, V> { @Override public List<V> findAllLessSpecific(K key) { return unroll(wrapped.findAllLessSpecific(key)); } MultiValueIntervalMap(); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override void clear(); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); }### Answer: @Test public void findAllLessSpecific() { final List<String> result = subject.findAllLessSpecific(k_11); assertThat(result, contains(v_131, v_132, v_133, v_121, v_122)); }
### Question: MultiValueIntervalMap implements IntervalMap<K, V> { @Override public List<V> findExactAndAllLessSpecific(K key) { return unroll(wrapped.findExactAndAllLessSpecific(key)); } MultiValueIntervalMap(); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override void clear(); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); }### Answer: @Test public void findExactAndAllLessSpecific() { final List<String> result = subject.findExactAndAllLessSpecific(k_12); assertThat(result, contains(v_131, v_132, v_133, v_121, v_122)); }
### Question: MultiValueIntervalMap implements IntervalMap<K, V> { @Override public List<V> findFirstMoreSpecific(K key) { return unroll(wrapped.findFirstMoreSpecific(key)); } MultiValueIntervalMap(); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override void clear(); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); }### Answer: @Test public void findFirstMoreSpecific() { final List<String> result = subject.findFirstMoreSpecific(k_12); assertThat(result, contains(v_11)); }
### Question: MultiValueIntervalMap implements IntervalMap<K, V> { @Override public List<V> findAllMoreSpecific(K key) { return unroll(wrapped.findAllMoreSpecific(key)); } MultiValueIntervalMap(); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override void clear(); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); }### Answer: @Test public void findAllMoreSpecific() { final List<String> result = subject.findAllMoreSpecific(Ipv4Resource.MAX_RANGE); assertThat(result, contains(v_131, v_132, v_133, v_121, v_122, v_11)); }
### Question: MultiValueIntervalMap implements IntervalMap<K, V> { @Override public List<V> findExactAndAllMoreSpecific(K key) { return unroll(wrapped.findExactAndAllMoreSpecific(key)); } MultiValueIntervalMap(); @Override void put(K key, V value); @Override void remove(K key); @Override void remove(K key, V value); @Override void clear(); @Override List<V> findFirstLessSpecific(K key); @Override List<V> findExact(K key); @Override List<V> findExactOrFirstLessSpecific(K key); @Override List<V> findAllLessSpecific(K key); @Override List<V> findExactAndAllLessSpecific(K key); @Override List<V> findFirstMoreSpecific(K key); @Override List<V> findAllMoreSpecific(K key); @Override List<V> findExactAndAllMoreSpecific(K key); }### Answer: @Test public void findExactAndAllMoreSpecific() { final List<String> result = subject.findExactAndAllMoreSpecific(k_12); assertThat(result, contains(v_121, v_122, v_11)); }
### Question: AuthoritativeResourceJsonLoader extends AbstractAuthoritativeResourceLoader { public AuthoritativeResource load(final JsonNode root) { handleAllocations(root.get("asnResources"), "asn"); handleFreeResources(root.get("asnResources"), "asn"); handleReservedResources(root.get("asnResources"), "asn"); handleAssignments(root.get("ipv4Resources"), "ipv4"); handleAllocations(root.get("ipv4Resources"), "ipv4"); handleFreeResources(root.get("ipv4Resources"), "ipv4"); handleReservedResources(root.get("ipv4Resources"), "ipv4"); handleAssignments(root.get("ipv6Resources"), "ipv6"); handleAllocations(root.get("ipv6Resources"), "ipv6"); handleFreeResources(root.get("ipv6Resources"), "ipv6"); handleReservedResources(root.get("ipv6Resources"), "ipv6"); return new AuthoritativeResource(autNums, ipv4Space, ipv6Space); } AuthoritativeResourceJsonLoader(final Logger logger); AuthoritativeResource load(final JsonNode root); }### Answer: @Test public void load() throws IOException { final AuthoritativeResource authoritativeResource = new AuthoritativeResourceJsonLoader(logger).load( new ObjectMapper().readValue(IOUtils.toString(getClass().getResourceAsStream("/grs/rirstats.json")), JsonNode.class) ); assertTrue(authoritativeResource.isMaintainedInRirSpace(ObjectType.AUT_NUM, ciString("AS7"))); assertTrue(authoritativeResource.isMaintainedInRirSpace(ObjectType.AUT_NUM, ciString("AS1877"))); assertTrue(authoritativeResource.isMaintainedInRirSpace(ObjectType.AUT_NUM, ciString("AS2849"))); assertTrue(authoritativeResource.isMaintainedInRirSpace(ObjectType.INETNUM, ciString("2.0.0.0-2.15.255.255"))); assertTrue(authoritativeResource.isMaintainedInRirSpace(ObjectType.INETNUM, ciString("2.56.168.0-2.56.171.255"))); assertTrue(authoritativeResource.isMaintainedInRirSpace(ObjectType.INETNUM, ciString("5.44.248.0-5.44.255.255"))); assertTrue(authoritativeResource.isMaintainedInRirSpace(ObjectType.INETNUM, ciString("13.116.0.0-13.123.255.255"))); assertTrue(authoritativeResource.isMaintainedInRirSpace(ObjectType.INET6NUM, ciString("2001:600::/32"))); assertTrue(authoritativeResource.isMaintainedInRirSpace(ObjectType.INET6NUM, ciString("2001:678::/48"))); assertTrue(authoritativeResource.isMaintainedInRirSpace(ObjectType.INET6NUM, ciString("2001:678:1::/48"))); assertTrue(authoritativeResource.isMaintainedInRirSpace(ObjectType.INET6NUM, ciString("2001:601::/32"))); }
### Question: AuthoritativeResourceDataValidator { void checkOverlaps(final Writer writer) throws IOException { for (int i1 = 0; i1 < sources.size(); i1++) { for (int i2 = i1 + 1; i2 < sources.size(); i2++) { final CIString source1 = sources.get(i1); final CIString source2 = sources.get(i2); checkOverlaps(writer, source1, authoritativeResourceData.getAuthoritativeResource(source1), source2, authoritativeResourceData.getAuthoritativeResource(source2)); } } } @Autowired AuthoritativeResourceDataValidator( @Value("${grs.sources}") final String[] grsSourceNames, final AuthoritativeResourceData authoritativeResourceData); }### Answer: @Test public void checkOverlaps() throws IOException { final StringWriter writer = new StringWriter(); subject.checkOverlaps(writer); final String output = writer.getBuffer().toString(); LOGGER.debug("overlaps:\n{}", output); final List<String> overlaps = Splitter.on("\n").splitToList(output); assertThat(overlaps, hasSize(11)); assertThat(overlaps, containsInAnyOrder( "GRS1 GRS2 aut-num AS1-AS2", "GRS1 GRS2 inetnum 10.0.0.0-10.0.0.0", "GRS1 GRS2 inetnum 192.0.0.1-192.0.0.2", "GRS1 GRS2 inet6num ::/0", "GRS1 GRS3 aut-num AS1-AS1", "GRS1 GRS3 inetnum 193.0.0.10-193.0.0.11", "GRS1 GRS3 inet6num ::/0", "GRS2 GRS3 aut-num AS10-AS10", "GRS2 GRS3 aut-num AS1-AS1", "GRS2 GRS3 inet6num ::/0", "")); }
### Question: AuthoritativeResourceData { public AuthoritativeResource getAuthoritativeResource(final CIString source) { final String sourceName = StringUtils.removeEnd(source.toLowerCase(), "-grs"); final AuthoritativeResource authoritativeResource = authoritativeResourceCache.get(sourceName); if (authoritativeResource == null) { throw new IllegalSourceException(source); } return authoritativeResource; } @Autowired AuthoritativeResourceData(@Value("${grs.sources}") final String grsSourceNames, @Value("${whois.source}") final String source, final ResourceDataDao resourceDataDao); synchronized void refreshGrsSources(); synchronized void refreshActiveSource(); AuthoritativeResource getAuthoritativeResource(final CIString source); AuthoritativeResource getAuthoritativeResource(); }### Answer: @Test(expected = IllegalSourceException.class) public void nonexistant_source_throws_exception() { authoritativeResourceData.getAuthoritativeResource(ciString("BLAH")); }
### Question: AuthoritativeResource { public static AuthoritativeResource loadFromFile(final Logger logger, final String name, final Path path) { try (final Scanner scanner = new Scanner(path)) { return loadFromScanner(logger, name, scanner); } catch (IOException e) { throw new IllegalArgumentException(e); } } AuthoritativeResource(final SortedRangeSet<Asn, AsnRange> autNums, final SortedRangeSet<Ipv4, Ipv4Range> inetRanges, final SortedRangeSet<Ipv6, Ipv6Range> inet6Ranges); static AuthoritativeResource loadFromFile(final Logger logger, final String name, final Path path); static AuthoritativeResource unknown(); static AuthoritativeResource loadFromScanner(final Logger logger, final String name, final Scanner scanner); int getNrAutNums(); int getNrInetnums(); int getNrInet6nums(); boolean isMaintainedInRirSpace(final RpslObject rpslObject); boolean isMaintainedInRirSpace(final ObjectType objectType, final CIString pkey); boolean isRouteMaintainedInRirSpace(final RpslObject rpslObject); boolean isRouteMaintainedInRirSpace(final ObjectType objectType, CIString key); Set<ObjectType> getResourceTypes(); Iterable<String> findAutnumOverlaps(AuthoritativeResource other); Iterable<String> findInetnumOverlaps(AuthoritativeResource other); Iterable<String> findInet6numOverlaps(AuthoritativeResource other); @Override boolean equals(Object o); @Override int hashCode(); List<String> getResources(); }### Answer: @Test(expected = IllegalArgumentException.class) public void unknown_file() throws IOException { AuthoritativeResource.loadFromFile(logger, "unknown", folder.getRoot().toPath().resolve("unknown")); }
### Question: AuthoritativeResource { public static AuthoritativeResource loadFromScanner(final Logger logger, final String name, final Scanner scanner) { return new AuthoritativeResourceLoader(logger, name, scanner).load(); } AuthoritativeResource(final SortedRangeSet<Asn, AsnRange> autNums, final SortedRangeSet<Ipv4, Ipv4Range> inetRanges, final SortedRangeSet<Ipv6, Ipv6Range> inet6Ranges); static AuthoritativeResource loadFromFile(final Logger logger, final String name, final Path path); static AuthoritativeResource unknown(); static AuthoritativeResource loadFromScanner(final Logger logger, final String name, final Scanner scanner); int getNrAutNums(); int getNrInetnums(); int getNrInet6nums(); boolean isMaintainedInRirSpace(final RpslObject rpslObject); boolean isMaintainedInRirSpace(final ObjectType objectType, final CIString pkey); boolean isRouteMaintainedInRirSpace(final RpslObject rpslObject); boolean isRouteMaintainedInRirSpace(final ObjectType objectType, CIString key); Set<ObjectType> getResourceTypes(); Iterable<String> findAutnumOverlaps(AuthoritativeResource other); Iterable<String> findInetnumOverlaps(AuthoritativeResource other); Iterable<String> findInet6numOverlaps(AuthoritativeResource other); @Override boolean equals(Object o); @Override int hashCode(); List<String> getResources(); }### Answer: @Test public void test_frankenranges_using_maintained_in_rir_space_in_order() { final AuthoritativeResource resourceData = AuthoritativeResource.loadFromScanner(logger, "RIPE-GRS", new Scanner( first + second + third)); testFranken(resourceData); } @Test public void test_frankenranges_using_maintained_in_rir_space_reverse_order() { final AuthoritativeResource resourceData = AuthoritativeResource.loadFromScanner(logger, "RIPE-GRS", new Scanner( third + second + first)); testFranken(resourceData); } @Test public void test_frankenranges_using_maintained_in_rir_space_random_order() { final AuthoritativeResource resourceData = AuthoritativeResource.loadFromScanner(logger, "RIPE-GRS", new Scanner( first + third + second)); testFranken(resourceData); } @Test public void test_frankenranges_using_maintained_in_rir_space_duplicates() { final AuthoritativeResource resourceData = AuthoritativeResource.loadFromScanner(logger, "RIPE-GRS", new Scanner( first + third + second + third + second + first)); testFranken(resourceData); }
### Question: AuthoritativeResourceDataJmx extends JmxBase { @ManagedOperation(description = "Refresh authoritative resource cache") @ManagedOperationParameters({ @ManagedOperationParameter(name = "comment", description = "Optional comment for invoking the operation") }) public String refreshCache(final String comment) { return invokeOperation("Refresh authoritative resource cache", comment, () -> { authoritativeResourceRefreshTask.refreshGrsAuthoritativeResourceCaches(); return "Refreshed caches"; }); } @Autowired AuthoritativeResourceDataJmx(final AuthoritativeResourceRefreshTask authoritativeResourceRefreshTask, final AuthoritativeResourceDataValidator authoritativeResourceDataValidator); @ManagedOperation(description = "Refresh authoritative resource cache") @ManagedOperationParameters({ @ManagedOperationParameter(name = "comment", description = "Optional comment for invoking the operation") }) String refreshCache(final String comment); @ManagedOperation(description = "Check overlaps in authoritative resource definitions and output to file") @ManagedOperationParameters({ @ManagedOperationParameter(name = "outputFile", description = "The file to write overlaps to"), @ManagedOperationParameter(name = "comment", description = "Optional comment for invoking the operation") }) String checkOverlaps(final String outputFile, final String comment); }### Answer: @Test public void refreshCache() { final String msg = subject.refreshCache("comment"); assertThat(msg, is("Refreshed caches")); verify(authoritativeResourceRefreshTask).refreshGrsAuthoritativeResourceCaches(); }
### Question: AuthoritativeResourceDataJmx extends JmxBase { @ManagedOperation(description = "Check overlaps in authoritative resource definitions and output to file") @ManagedOperationParameters({ @ManagedOperationParameter(name = "outputFile", description = "The file to write overlaps to"), @ManagedOperationParameter(name = "comment", description = "Optional comment for invoking the operation") }) public String checkOverlaps(final String outputFile, final String comment) { return invokeOperation(String.format("Writing overlaps to %s", outputFile), comment, () -> { final File output = new File(outputFile); final String absolutePath = output.getAbsolutePath(); BufferedWriter writer = null; try { if (!output.createNewFile()) { return String.format("Abort, file already exists: %s", absolutePath); } writer = new BufferedWriter(new FileWriter(output)); authoritativeResourceDataValidator.checkOverlaps(writer); } catch (IOException e) { LOGGER.error("Checking overlaps", e); return String.format("Failed writing to: %s, %s", absolutePath, e.getMessage()); } finally { IOUtils.closeQuietly(writer); } return String.format("Overlaps written to: %s", absolutePath); }); } @Autowired AuthoritativeResourceDataJmx(final AuthoritativeResourceRefreshTask authoritativeResourceRefreshTask, final AuthoritativeResourceDataValidator authoritativeResourceDataValidator); @ManagedOperation(description = "Refresh authoritative resource cache") @ManagedOperationParameters({ @ManagedOperationParameter(name = "comment", description = "Optional comment for invoking the operation") }) String refreshCache(final String comment); @ManagedOperation(description = "Check overlaps in authoritative resource definitions and output to file") @ManagedOperationParameters({ @ManagedOperationParameter(name = "outputFile", description = "The file to write overlaps to"), @ManagedOperationParameter(name = "comment", description = "Optional comment for invoking the operation") }) String checkOverlaps(final String outputFile, final String comment); }### Answer: @Test public void checkOverlaps_existing_file() throws IOException { final File file = File.createTempFile("overlaps", "test"); final String msg = subject.checkOverlaps(file.getAbsolutePath(), ""); assertThat(msg, startsWith("Abort, file already exists")); verifyZeroInteractions(authoritativeResourceDataValidator); } @Test public void checkOverlaps_unwriteable_file() throws IOException { final File file = new File("/some/unexisting/dir/overlaps"); final String msg = subject.checkOverlaps(file.getAbsolutePath(), ""); assertThat(msg, startsWith("Failed writing to: /some/unexisting/dir/overlaps")); verifyZeroInteractions(authoritativeResourceDataValidator); } @Test public void checkOverlaps() throws IOException { folder.getRoot().mkdirs(); final File file = new File(folder.getRoot(), "overlaps.txt"); final String msg = subject.checkOverlaps(file.getAbsolutePath(), ""); assertThat(msg, startsWith("Overlaps written to")); verify(authoritativeResourceDataValidator).checkOverlaps(any(Writer.class)); }
### Question: AuthoritativeResourceImportTask extends AbstractAutoritativeResourceImportTask implements DailyScheduledTask, EmbeddedValueResolverAware { @Override @Scheduled(cron = "0 15 0 * * *") @SchedulerLock(name = TASK_NAME) public void run() { doImport(sourceNames); } @Autowired AuthoritativeResourceImportTask(@Value("${grs.sources}") final String grsSourceNames, final ResourceDataDao resourceDataDao, final Downloader downloader, @Value("${dir.grs.import.download:}") final String downloadDir, @Value("${grs.import.enabled}") final boolean enabled, @Value("${rsng.base.url:}") final String rsngBaseUrl); @Override void setEmbeddedValueResolver(final StringValueResolver valueResolver); @Override @Scheduled(cron = "0 15 0 * * *") @SchedulerLock(name = TASK_NAME) void run(); }### Answer: @Test public void init_url_not_defined() throws IOException { subject.run(); verify(resourceDataDao).store(eq("test"), resourceCaptor.capture()); assertThat(resourceCaptor.getValue().getNrAutNums(), is(0)); assertThat(resourceCaptor.getValue().getNrInet6nums(), is(0)); assertThat(resourceCaptor.getValue().getNrInetnums(), is(0)); } @Test public void download_not_resource_data() throws IOException { when(valueResolver.resolveStringValue(anyString())).thenReturn("http: subject.run(); verify(resourceDataDao, never()).store(anyString(), Mockito.<AuthoritativeResource>anyObject()); } @Test public void downloaded_fails() throws IOException { when(valueResolver.resolveStringValue(anyString())).thenReturn("http: doThrow(IOException.class).when(downloader).downloadToWithMd5Check(any(Logger.class), any(URL.class), any(Path.class)); subject.run(); } @Test public void download() throws IOException { when(valueResolver.resolveStringValue(anyString())).thenReturn("http: doAnswer(invocation -> { final Path path = (Path) invocation.getArguments()[2]; Files.createFile(path); return null; }).when(downloader).downloadToWithMd5Check(any(Logger.class), any(URL.class), any(Path.class)); subject.run(); verify(resourceDataDao).store(eq("test"), resourceCaptor.capture()); assertThat(resourceCaptor.getValue().getNrAutNums(), is(0)); assertThat(resourceCaptor.getValue().getNrInet6nums(), is(0)); assertThat(resourceCaptor.getValue().getNrInetnums(), is(0)); }
### Question: RpslObjectFilter { public static boolean isFiltered(final RpslObject rpslObject) { final List<RpslAttribute> attributes = rpslObject.findAttributes(AttributeType.SOURCE); return !attributes.isEmpty() && attributes.get(0).getValue().contains(FILTERED); } private RpslObjectFilter(); static String getCertificateFromKeyCert(final RpslObject object); static String diff(final RpslObject original, final RpslObject revised); static RpslObjectBuilder keepKeyAttributesOnly(final RpslObjectBuilder builder); static RpslObjectBuilder setFiltered(final RpslObjectBuilder builder); static void addFilteredSourceReplacement(final RpslObject object, final Map<RpslAttribute, RpslAttribute> replacementsMap); static boolean isFiltered(final RpslObject rpslObject); static RpslObject buildGenericObject(final RpslObject object, final String ... attributes); static RpslObject buildGenericObject(final String object, final String ... attributes); static RpslObject buildGenericObject(final RpslObjectBuilder builder, final String ... attributes); static boolean ignoreGeneratedAttributesEqual(final RpslObject object1, final RpslObject object2); }### Answer: @Test public void isFiltered() { final boolean filtered = RpslObjectFilter.isFiltered(mntner); assertThat(filtered, is(false)); }
### Question: AsBlockRange { public static AsBlockRange parse(final String range) { final Matcher match = AS_BLOCK_PATTERN.matcher(range); if (match.find()) { long begin = Long.valueOf(match.group(1)); long end = Long.valueOf(match.group(2)); if (end < begin) { throw new AttributeParseException(end + " < " + begin, range); } return new AsBlockRange(begin, end); } throw new AttributeParseException("invalid asblock", range); } private AsBlockRange(final long begin, final long end); static AsBlockRange parse(final String range); @Override boolean equals(final Object o); @Override int hashCode(); boolean contains(final AsBlockRange asBlockRange); long getBegin(); long getEnd(); String getEndWithPrefix(); String getBeginWithPrefix(); @Override String toString(); }### Answer: @Test public void validAsBlockRanges() { checkAsBlockRange(AsBlockRange.parse("AS1-AS2"), 1, 2); checkAsBlockRange(AsBlockRange.parse("as1-as2"), 1, 2); checkAsBlockRange(AsBlockRange.parse("AS1 -AS2"), 1, 2); checkAsBlockRange(AsBlockRange.parse("AS1 - AS2"), 1, 2); checkAsBlockRange(AsBlockRange.parse("AS1 - AS4294967295"), 1, 4294967295L); checkAsBlockRange(AsBlockRange.parse("AS1 - AS1"), 1, 1); } @Test(expected = AttributeParseException.class) public void invalidRange() { AsBlockRange.parse("AS2 - AS1"); } @Test(expected = AttributeParseException.class) public void nonNumericAsBlockSingleArgument() { AsBlockRange.parse("ASx"); } @Test(expected = AttributeParseException.class) public void singleAsBlockWithSeparator() { AsBlockRange.parse("AS1-"); } @Test(expected = AttributeParseException.class) public void nonNumericAsBlockRangeFirstArgument() { AsBlockRange.parse("ASx-AS1"); } @Test(expected = AttributeParseException.class) public void nonNumericAsBlockRangeSecondArgument() { AsBlockRange.parse("AS1-ASx"); } @Test(expected = AttributeParseException.class) public void asBlockRangeThirdArgument() { AsBlockRange.parse("AS1-AS2-AS3"); } @Test(expected = AttributeParseException.class) public void asBlockArgumentWithoutPrefix() { AsBlockRange.parse("1-2"); } @Test(expected = AttributeParseException.class) public void emptyAsBlockRangeString() { AsBlockRange.parse(""); }
### Question: Changed { public static Changed parse(final CIString value) { return parse(value.toString()); } Changed(final CIString email, final LocalDate date); Changed(final String email, final LocalDate date); String getEmail(); @CheckForNull LocalDate getDate(); @CheckForNull String getDateString(); @Override String toString(); static Changed parse(final CIString value); static Changed parse(final String value); }### Answer: @Test(expected = AttributeParseException.class) public void empty() { Changed.parse(""); } @Test(expected = AttributeParseException.class) public void no_email() { Changed.parse("20010101"); } @Test(expected = AttributeParseException.class) public void invalid_date() { Changed.parse("a@a.a 13131313"); } @Test(expected = AttributeParseException.class) public void too_long() { Changed.parse("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz 20010101"); } @Test(expected = AttributeParseException.class) public void mixedUpDateAndEmail() { Changed subject = Changed.parse("20130112 b.was@infbud.pl"); }
### Question: AddressPrefixRange { public static AddressPrefixRange parse(final CIString value) { return parse(value.toString()); } private AddressPrefixRange(final String value, final IpInterval ipInterval, final RangeOperation rangeOperation); IpInterval getIpInterval(); RangeOperation getRangeOperation(); @Override String toString(); @SuppressWarnings("unchecked") BoundaryCheckResult checkWithinBounds(final IpInterval bounds); @SuppressWarnings("unchecked") BoundaryCheckResult checkRange(final IpInterval contained); static AddressPrefixRange parse(final CIString value); static AddressPrefixRange parse(final String value); }### Answer: @Test(expected = AttributeParseException.class) public void empty() { AddressPrefixRange.parse(""); } @Test(expected = AttributeParseException.class) public void invalid_address() { AddressPrefixRange.parse("300.104.182.0/12"); } @Test(expected = AttributeParseException.class) public void range_too_long() { AddressPrefixRange.parse("194.104.182.0/33"); } @Test(expected = AttributeParseException.class) public void range_too_long_ipv6() { AddressPrefixRange.parse("2a00:c00::/129"); } @Test(expected = AttributeParseException.class) public void operation_range_ipv4_too_long() { AddressPrefixRange.parse("193.151.47.0/24^24-33"); } @Test(expected = AttributeParseException.class) public void operation_range_ipv4_invalid_order() { AddressPrefixRange.parse("193.151.47.0/24^24-12"); } @Test(expected = AttributeParseException.class) public void operation_length_too_long() { AddressPrefixRange.parse("77.74.152.0/23^33"); } @Test(expected = AttributeParseException.class) public void n_lower_than_prefix_range() { AddressPrefixRange.parse("77.74.152.0/23^22"); }
### Question: NServer { public static NServer parse(final CIString value) { return parse(value.toString()); } private NServer(final CIString hostname, final IpInterval ipInterval); CIString getHostname(); @CheckForNull IpInterval getIpInterval(); @Override String toString(); static NServer parse(final CIString value); static NServer parse(final String value); }### Answer: @Test(expected = AttributeParseException.class) public void empty() { NServer.parse(""); } @Test(expected = AttributeParseException.class) public void hostname_invalid() { NServer.parse("$"); } @Test(expected = AttributeParseException.class) public void hostname_and_ipv4_range_24() { NServer.parse("dns.comcor.ru 194.0.0.0/24"); } @Test(expected = AttributeParseException.class) public void hostname_and_ipv4_list() { NServer.parse("dns.comcor.ru 194.0.0.0 194.0.0.0"); } @Test(expected = AttributeParseException.class) public void hostname_and_invalid_ip() { NServer.parse("dns.comcor.ru dns.comcor.ru"); }
### Question: Domain { public static Domain parse(final CIString value) { return parse(value.toString()); } private Domain(final CIString value, final IpInterval<?> reverseIp, final Type type, final boolean dashNotation); CIString getValue(); Type getType(); @CheckForNull IpInterval<?> getReverseIp(); boolean endsWithDomain(final CIString hostname); static Domain parse(final CIString value); static Domain parse(final String domain); }### Answer: @Test(expected = AttributeParseException.class) public void empty() { Domain.parse(""); } @Test(expected = AttributeParseException.class) public void hostname() { Domain.parse("hostname"); } @Test(expected = AttributeParseException.class) public void ipv4_dash_invalid_position() { Domain.parse("0-127.10.10.in-addr.arpa"); } @Test(expected = AttributeParseException.class) public void ipv4_dash_range_0_255() { Domain.parse("0-255.10.10.in-addr.arpa"); } @Test(expected = AttributeParseException.class) public void ipv4_dash_range_start_is_range_end() { Domain.parse("1-1.10.10.in-addr.arpa"); } @Test(expected = AttributeParseException.class) public void suffix() { Domain.parse("200.193.193.193.some-suffix."); } @Test(expected = AttributeParseException.class) public void suffix_almost_correct() { Domain.parse("200.193.193.in-addraarpa"); }
### Question: ObjectMessages { public Messages getMessages() { return messages; } Messages getMessages(); Map<RpslAttribute, Messages> getAttributeMessages(); boolean contains(final Message message); void addMessage(final Message message); void addMessage(final RpslAttribute attribute, final Message message); Messages getMessages(final RpslAttribute attribute); boolean hasErrors(); boolean hasMessages(); int getErrorCount(); void addAll(final ObjectMessages objectMessages); @Override String toString(); }### Answer: @Test public void empty() { assertThat(subject.getMessages().getAllMessages(), hasSize(0)); }
### Question: ObjectMessages { public void addAll(final ObjectMessages objectMessages) { messages.addAll(objectMessages.messages); for (final Map.Entry<RpslAttribute, Messages> entry : objectMessages.attributeMessages.entrySet()) { getMessages(entry.getKey()).addAll(entry.getValue()); } } Messages getMessages(); Map<RpslAttribute, Messages> getAttributeMessages(); boolean contains(final Message message); void addMessage(final Message message); void addMessage(final RpslAttribute attribute, final Message message); Messages getMessages(final RpslAttribute attribute); boolean hasErrors(); boolean hasMessages(); int getErrorCount(); void addAll(final ObjectMessages objectMessages); @Override String toString(); }### Answer: @Test public void addAll() { final RpslAttribute attribute = object.getAttributes().get(0); final ObjectMessages other = new ObjectMessages(); other.addMessage(error); other.addMessage(attribute, warning); subject.addAll(other); assertThat(subject.contains(error), is(true)); assertThat(subject.getMessages(attribute).getWarnings(), contains(warning)); }
### Question: ObjectTemplate implements Comparable<ObjectTemplate> { public static ObjectTemplate getTemplate(final ObjectType type) { final ObjectTemplate objectTemplate = TEMPLATE_MAP.get(type); if (objectTemplate == null) { throw new IllegalStateException("No template for " + type); } return objectTemplate; } private ObjectTemplate(final ObjectType objectType, final int orderPosition, final AttributeTemplate... attributeTemplates); static ObjectTemplate getTemplate(final ObjectType type); static Collection<ObjectTemplate> getTemplates(); ObjectType getObjectType(); List<AttributeTemplate> getAttributeTemplates(); Set<AttributeType> getAllAttributes(); Set<AttributeType> getKeyAttributes(); Set<AttributeType> getLookupAttributes(); AttributeType getKeyLookupAttribute(); Set<AttributeType> getMandatoryAttributes(); Set<AttributeType> getInverseLookupAttributes(); Set<AttributeType> getGeneratedAttributes(); Set<AttributeType> getMultipleAttributes(); Comparator<RpslAttribute> getAttributeTypeComparator(); boolean isSet(); String getNameToFirstUpper( ); String getNameToFirstLower( ); @Override boolean equals(final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final ObjectTemplate o); void validateStructure(final RpslObject rpslObject, final ObjectMessages objectMessages); void validateSyntax(final RpslObject rpslObject, final ObjectMessages objectMessages, final boolean skipGenerated); ObjectMessages validate(final RpslObject rpslObject); @Override String toString(); String toVerboseString(); boolean hasAttribute(final AttributeType attributeType); }### Answer: @Test(expected = IllegalStateException.class) public void getObjectSpec_null() { ObjectTemplate.getTemplate(null); } @Test public void allObjectTypesSupported() { for (final ObjectType objectType : ObjectType.values()) { ObjectTemplate.getTemplate(objectType); } }
### Question: ObjectTemplate implements Comparable<ObjectTemplate> { public ObjectType getObjectType() { return objectType; } private ObjectTemplate(final ObjectType objectType, final int orderPosition, final AttributeTemplate... attributeTemplates); static ObjectTemplate getTemplate(final ObjectType type); static Collection<ObjectTemplate> getTemplates(); ObjectType getObjectType(); List<AttributeTemplate> getAttributeTemplates(); Set<AttributeType> getAllAttributes(); Set<AttributeType> getKeyAttributes(); Set<AttributeType> getLookupAttributes(); AttributeType getKeyLookupAttribute(); Set<AttributeType> getMandatoryAttributes(); Set<AttributeType> getInverseLookupAttributes(); Set<AttributeType> getGeneratedAttributes(); Set<AttributeType> getMultipleAttributes(); Comparator<RpslAttribute> getAttributeTypeComparator(); boolean isSet(); String getNameToFirstUpper( ); String getNameToFirstLower( ); @Override boolean equals(final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final ObjectTemplate o); void validateStructure(final RpslObject rpslObject, final ObjectMessages objectMessages); void validateSyntax(final RpslObject rpslObject, final ObjectMessages objectMessages, final boolean skipGenerated); ObjectMessages validate(final RpslObject rpslObject); @Override String toString(); String toVerboseString(); boolean hasAttribute(final AttributeType attributeType); }### Answer: @Test public void getObjectType() { assertThat(subject.getObjectType(), is(ObjectType.MNTNER)); }
### Question: ObjectTemplate implements Comparable<ObjectTemplate> { public ObjectMessages validate(final RpslObject rpslObject) { final ObjectMessages objectMessages = new ObjectMessages(); validateStructure(rpslObject, objectMessages); validateSyntax(rpslObject, objectMessages, false); return objectMessages; } private ObjectTemplate(final ObjectType objectType, final int orderPosition, final AttributeTemplate... attributeTemplates); static ObjectTemplate getTemplate(final ObjectType type); static Collection<ObjectTemplate> getTemplates(); ObjectType getObjectType(); List<AttributeTemplate> getAttributeTemplates(); Set<AttributeType> getAllAttributes(); Set<AttributeType> getKeyAttributes(); Set<AttributeType> getLookupAttributes(); AttributeType getKeyLookupAttribute(); Set<AttributeType> getMandatoryAttributes(); Set<AttributeType> getInverseLookupAttributes(); Set<AttributeType> getGeneratedAttributes(); Set<AttributeType> getMultipleAttributes(); Comparator<RpslAttribute> getAttributeTypeComparator(); boolean isSet(); String getNameToFirstUpper( ); String getNameToFirstLower( ); @Override boolean equals(final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final ObjectTemplate o); void validateStructure(final RpslObject rpslObject, final ObjectMessages objectMessages); void validateSyntax(final RpslObject rpslObject, final ObjectMessages objectMessages, final boolean skipGenerated); ObjectMessages validate(final RpslObject rpslObject); @Override String toString(); String toVerboseString(); boolean hasAttribute(final AttributeType attributeType); }### Answer: @Test public void validate_no_errors() { final ObjectMessages objectMessages = subject.validate(RpslObject.parse(MAINTAINER_OBJECT_STRING)); assertThat(objectMessages.hasErrors(), is(false)); } @Test public void validate_mandatory_missing() { final RpslObject rpslObject = RpslObject.parse(MAINTAINER_OBJECT_STRING.substring(0, MAINTAINER_OBJECT_STRING.lastIndexOf('\n') + 1)); final ObjectMessages objectMessages = subject.validate(rpslObject); assertThat(objectMessages.hasErrors(), is(true)); assertThat(objectMessages.getMessages().getErrors(), contains(ValidationMessages.missingMandatoryAttribute(AttributeType.SOURCE))); assertZeroAttributeErrors(rpslObject, objectMessages); } @Test public void validate_single_occurs_multiple_times() { final RpslObject rpslObject = RpslObject.parse(MAINTAINER_OBJECT_STRING + "\nsource: RIPE\n"); final ObjectMessages objectMessages = subject.validate(rpslObject); assertThat(objectMessages.hasErrors(), is(true)); assertThat(objectMessages.getMessages().getErrors(), contains(ValidationMessages.tooManyAttributesOfType(AttributeType.SOURCE))); assertZeroAttributeErrors(rpslObject, objectMessages); }
### Question: ObjectTemplate implements Comparable<ObjectTemplate> { public Set<AttributeType> getMultipleAttributes() { return multipleAttributes; } private ObjectTemplate(final ObjectType objectType, final int orderPosition, final AttributeTemplate... attributeTemplates); static ObjectTemplate getTemplate(final ObjectType type); static Collection<ObjectTemplate> getTemplates(); ObjectType getObjectType(); List<AttributeTemplate> getAttributeTemplates(); Set<AttributeType> getAllAttributes(); Set<AttributeType> getKeyAttributes(); Set<AttributeType> getLookupAttributes(); AttributeType getKeyLookupAttribute(); Set<AttributeType> getMandatoryAttributes(); Set<AttributeType> getInverseLookupAttributes(); Set<AttributeType> getGeneratedAttributes(); Set<AttributeType> getMultipleAttributes(); Comparator<RpslAttribute> getAttributeTypeComparator(); boolean isSet(); String getNameToFirstUpper( ); String getNameToFirstLower( ); @Override boolean equals(final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final ObjectTemplate o); void validateStructure(final RpslObject rpslObject, final ObjectMessages objectMessages); void validateSyntax(final RpslObject rpslObject, final ObjectMessages objectMessages, final boolean skipGenerated); ObjectMessages validate(final RpslObject rpslObject); @Override String toString(); String toVerboseString(); boolean hasAttribute(final AttributeType attributeType); }### Answer: @Test public void getMultipleAttributes(){ final ObjectTemplate template = ObjectTemplate.getTemplate(ObjectType.AS_BLOCK); Set<AttributeType> multipleAttributes = template.getMultipleAttributes(); assertThat(multipleAttributes.size(), is(6)); }
### Question: ObjectTemplate implements Comparable<ObjectTemplate> { public boolean isSet() { return ObjectType.getSets().contains(objectType); } private ObjectTemplate(final ObjectType objectType, final int orderPosition, final AttributeTemplate... attributeTemplates); static ObjectTemplate getTemplate(final ObjectType type); static Collection<ObjectTemplate> getTemplates(); ObjectType getObjectType(); List<AttributeTemplate> getAttributeTemplates(); Set<AttributeType> getAllAttributes(); Set<AttributeType> getKeyAttributes(); Set<AttributeType> getLookupAttributes(); AttributeType getKeyLookupAttribute(); Set<AttributeType> getMandatoryAttributes(); Set<AttributeType> getInverseLookupAttributes(); Set<AttributeType> getGeneratedAttributes(); Set<AttributeType> getMultipleAttributes(); Comparator<RpslAttribute> getAttributeTypeComparator(); boolean isSet(); String getNameToFirstUpper( ); String getNameToFirstLower( ); @Override boolean equals(final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final ObjectTemplate o); void validateStructure(final RpslObject rpslObject, final ObjectMessages objectMessages); void validateSyntax(final RpslObject rpslObject, final ObjectMessages objectMessages, final boolean skipGenerated); ObjectMessages validate(final RpslObject rpslObject); @Override String toString(); String toVerboseString(); boolean hasAttribute(final AttributeType attributeType); }### Answer: @Test public void isSet() { for (ObjectType objectType : ObjectType.values()) { assertThat(objectType.getName().toLowerCase().contains("set"), is(ObjectTemplate.getTemplate(objectType).isSet())); } }
### Question: RpslObject implements Identifiable, ResponseObject { @Override public String toString() { try { final StringWriter writer = new StringWriter(); for (final RpslAttribute attribute : getAttributes()) { attribute.writeTo(writer); } return writer.toString(); } catch (IOException e) { throw new IllegalStateException("Should never occur", e); } } RpslObject(final RpslObject oldObject, final List<RpslAttribute> attributes); RpslObject(final Integer objectId, final List<RpslAttribute> attributes); RpslObject(final Integer objectId, final RpslObject rpslObject); RpslObject(final List<RpslAttribute> attributes); static RpslObject parse(final String input); static RpslObject parse(final byte[] input); static RpslObject parse(final Integer objectId, final String input); static RpslObject parse(final Integer objectId, final byte[] input); @Override int getObjectId(); ObjectType getType(); List<RpslAttribute> getAttributes(); int size(); final CIString getKey(); String getFormattedKey(); @Override boolean equals(final Object obj); @Override int hashCode(); RpslAttribute getTypeAttribute(); RpslAttribute findAttribute(final AttributeType attributeType); List<RpslAttribute> findAttributes(final AttributeType attributeType); List<RpslAttribute> findAttributes(final Iterable<AttributeType> attributeTypes); List<RpslAttribute> findAttributes(final AttributeType... attributeTypes); boolean containsAttributes(final Collection<AttributeType> attributeTypes); boolean containsAttribute(final AttributeType attributeType); @Override void writeTo(final OutputStream out); void writeTo(final Writer writer); @Override byte[] toByteArray(); @Override String toString(); CIString getValueForAttribute(final AttributeType attributeType); @Nullable CIString getValueOrNullForAttribute(final AttributeType attributeType); Set<CIString> getValuesForAttribute(final AttributeType... attributeType); }### Answer: @Test public void checkThatSpecialCharactersGetThrough() { parseAndAssign("person: New Test Person\n" + "address: Flughafenstraße 120\n" + "address: D - 40474 Düsseldorf\n" + "nic-hdl: ABC-RIPE\n"); assertThat(subject.toString(), containsString("Flughafenstraße")); assertThat(subject.toString(), containsString("Düsseldorf")); }
### Question: RpslObject implements Identifiable, ResponseObject { public List<RpslAttribute> findAttributes(final AttributeType attributeType) { final List<RpslAttribute> list = getOrCreateCache().get(attributeType); return list == null ? Collections.<RpslAttribute>emptyList() : Collections.unmodifiableList(list); } RpslObject(final RpslObject oldObject, final List<RpslAttribute> attributes); RpslObject(final Integer objectId, final List<RpslAttribute> attributes); RpslObject(final Integer objectId, final RpslObject rpslObject); RpslObject(final List<RpslAttribute> attributes); static RpslObject parse(final String input); static RpslObject parse(final byte[] input); static RpslObject parse(final Integer objectId, final String input); static RpslObject parse(final Integer objectId, final byte[] input); @Override int getObjectId(); ObjectType getType(); List<RpslAttribute> getAttributes(); int size(); final CIString getKey(); String getFormattedKey(); @Override boolean equals(final Object obj); @Override int hashCode(); RpslAttribute getTypeAttribute(); RpslAttribute findAttribute(final AttributeType attributeType); List<RpslAttribute> findAttributes(final AttributeType attributeType); List<RpslAttribute> findAttributes(final Iterable<AttributeType> attributeTypes); List<RpslAttribute> findAttributes(final AttributeType... attributeTypes); boolean containsAttributes(final Collection<AttributeType> attributeTypes); boolean containsAttribute(final AttributeType attributeType); @Override void writeTo(final OutputStream out); void writeTo(final Writer writer); @Override byte[] toByteArray(); @Override String toString(); CIString getValueForAttribute(final AttributeType attributeType); @Nullable CIString getValueOrNullForAttribute(final AttributeType attributeType); Set<CIString> getValuesForAttribute(final AttributeType... attributeType); }### Answer: @Test public void checkThatUtf8CharactersGetThroughAndConverted() { parseAndAssign("person: New Test Person\n" + "address: Flughafenstraße 120/Σ\n" + "address: D - 40474 Düsseldorf\n" + "nic-hdl: ABC-RIPE\n"); List<RpslAttribute> addresses = subject.findAttributes(AttributeType.ADDRESS); assertThat(addresses, hasSize(2)); assertThat(addresses.get(0).getValue(), containsString("Flughafenstraße 120/?")); assertThat(addresses.get(1).getValue(), containsString("Düsseldorf")); } @Test public void checkThatUtf8CharactersGetConverted() { parseAndAssign("person: New Test Person\n" + "address: Тверская улица,москва\n" + "nic-hdl: ABC-RIPE\n"); List<RpslAttribute> addresses = subject.findAttributes(AttributeType.ADDRESS); assertThat(addresses, hasSize(1)); assertThat(addresses.get(0).getValue(), containsString("???????? ?????,??????")); } @Test public void parseMultipleIdenticalKeys() { String key = "descr"; int amount = 1000; parseAndAssign("mntner: DEV-MNT\n" + StringUtils.repeat(key + ": value", "\n", amount)); assertThat(subject.findAttributes(AttributeType.DESCR), hasSize(amount)); } @Test public void findAttributes_multiple() { parseAndAssign(maintainer); assertThat(subject.findAttributes(AttributeType.MNTNER, AttributeType.ADMIN_C, AttributeType.TECH_C), hasSize(3)); }
### Question: RpslObject implements Identifiable, ResponseObject { public final CIString getKey() { return key; } RpslObject(final RpslObject oldObject, final List<RpslAttribute> attributes); RpslObject(final Integer objectId, final List<RpslAttribute> attributes); RpslObject(final Integer objectId, final RpslObject rpslObject); RpslObject(final List<RpslAttribute> attributes); static RpslObject parse(final String input); static RpslObject parse(final byte[] input); static RpslObject parse(final Integer objectId, final String input); static RpslObject parse(final Integer objectId, final byte[] input); @Override int getObjectId(); ObjectType getType(); List<RpslAttribute> getAttributes(); int size(); final CIString getKey(); String getFormattedKey(); @Override boolean equals(final Object obj); @Override int hashCode(); RpslAttribute getTypeAttribute(); RpslAttribute findAttribute(final AttributeType attributeType); List<RpslAttribute> findAttributes(final AttributeType attributeType); List<RpslAttribute> findAttributes(final Iterable<AttributeType> attributeTypes); List<RpslAttribute> findAttributes(final AttributeType... attributeTypes); boolean containsAttributes(final Collection<AttributeType> attributeTypes); boolean containsAttribute(final AttributeType attributeType); @Override void writeTo(final OutputStream out); void writeTo(final Writer writer); @Override byte[] toByteArray(); @Override String toString(); CIString getValueForAttribute(final AttributeType attributeType); @Nullable CIString getValueOrNullForAttribute(final AttributeType attributeType); Set<CIString> getValuesForAttribute(final AttributeType... attributeType); }### Answer: @Test(expected = IllegalArgumentException.class) public void test_get_key_person_empty() { parseAndAssign("person: foo # Comment \n"); subject.getKey(); }
### Question: RpslObject implements Identifiable, ResponseObject { public boolean containsAttribute(final AttributeType attributeType) { return getOrCreateCache().containsKey(attributeType); } RpslObject(final RpslObject oldObject, final List<RpslAttribute> attributes); RpslObject(final Integer objectId, final List<RpslAttribute> attributes); RpslObject(final Integer objectId, final RpslObject rpslObject); RpslObject(final List<RpslAttribute> attributes); static RpslObject parse(final String input); static RpslObject parse(final byte[] input); static RpslObject parse(final Integer objectId, final String input); static RpslObject parse(final Integer objectId, final byte[] input); @Override int getObjectId(); ObjectType getType(); List<RpslAttribute> getAttributes(); int size(); final CIString getKey(); String getFormattedKey(); @Override boolean equals(final Object obj); @Override int hashCode(); RpslAttribute getTypeAttribute(); RpslAttribute findAttribute(final AttributeType attributeType); List<RpslAttribute> findAttributes(final AttributeType attributeType); List<RpslAttribute> findAttributes(final Iterable<AttributeType> attributeTypes); List<RpslAttribute> findAttributes(final AttributeType... attributeTypes); boolean containsAttributes(final Collection<AttributeType> attributeTypes); boolean containsAttribute(final AttributeType attributeType); @Override void writeTo(final OutputStream out); void writeTo(final Writer writer); @Override byte[] toByteArray(); @Override String toString(); CIString getValueForAttribute(final AttributeType attributeType); @Nullable CIString getValueOrNullForAttribute(final AttributeType attributeType); Set<CIString> getValuesForAttribute(final AttributeType... attributeType); }### Answer: @Test public void containsAttribute_works() { parseAndAssign(maintainer); assertThat(subject.containsAttribute(AttributeType.SOURCE), is(true)); } @Test public void containsAttribute_unknown() { parseAndAssign(maintainer); assertThat(subject.containsAttribute(AttributeType.PERSON), is(false)); }
### Question: RpslObject implements Identifiable, ResponseObject { public CIString getValueForAttribute(final AttributeType attributeType) { return findAttribute(attributeType).getCleanValue(); } RpslObject(final RpslObject oldObject, final List<RpslAttribute> attributes); RpslObject(final Integer objectId, final List<RpslAttribute> attributes); RpslObject(final Integer objectId, final RpslObject rpslObject); RpslObject(final List<RpslAttribute> attributes); static RpslObject parse(final String input); static RpslObject parse(final byte[] input); static RpslObject parse(final Integer objectId, final String input); static RpslObject parse(final Integer objectId, final byte[] input); @Override int getObjectId(); ObjectType getType(); List<RpslAttribute> getAttributes(); int size(); final CIString getKey(); String getFormattedKey(); @Override boolean equals(final Object obj); @Override int hashCode(); RpslAttribute getTypeAttribute(); RpslAttribute findAttribute(final AttributeType attributeType); List<RpslAttribute> findAttributes(final AttributeType attributeType); List<RpslAttribute> findAttributes(final Iterable<AttributeType> attributeTypes); List<RpslAttribute> findAttributes(final AttributeType... attributeTypes); boolean containsAttributes(final Collection<AttributeType> attributeTypes); boolean containsAttribute(final AttributeType attributeType); @Override void writeTo(final OutputStream out); void writeTo(final Writer writer); @Override byte[] toByteArray(); @Override String toString(); CIString getValueForAttribute(final AttributeType attributeType); @Nullable CIString getValueOrNullForAttribute(final AttributeType attributeType); Set<CIString> getValuesForAttribute(final AttributeType... attributeType); }### Answer: @Test public void getValueForAttribute() { assertThat(RpslObject.parse("mntner: DEV-MNT\n").getValueForAttribute(AttributeType.MNTNER).toString(), is("DEV-MNT")); }
### Question: RpslObject implements Identifiable, ResponseObject { public Set<CIString> getValuesForAttribute(final AttributeType... attributeType) { final Set<CIString> values = Sets.newLinkedHashSet(); for (AttributeType attrType : attributeType) { final List<RpslAttribute> rpslAttributes = getOrCreateCache().get(attrType); if (rpslAttributes != null && !rpslAttributes.isEmpty()) { for (RpslAttribute rpslAttribute : rpslAttributes) { values.addAll(rpslAttribute.getCleanValues()); } } } return values; } RpslObject(final RpslObject oldObject, final List<RpslAttribute> attributes); RpslObject(final Integer objectId, final List<RpslAttribute> attributes); RpslObject(final Integer objectId, final RpslObject rpslObject); RpslObject(final List<RpslAttribute> attributes); static RpslObject parse(final String input); static RpslObject parse(final byte[] input); static RpslObject parse(final Integer objectId, final String input); static RpslObject parse(final Integer objectId, final byte[] input); @Override int getObjectId(); ObjectType getType(); List<RpslAttribute> getAttributes(); int size(); final CIString getKey(); String getFormattedKey(); @Override boolean equals(final Object obj); @Override int hashCode(); RpslAttribute getTypeAttribute(); RpslAttribute findAttribute(final AttributeType attributeType); List<RpslAttribute> findAttributes(final AttributeType attributeType); List<RpslAttribute> findAttributes(final Iterable<AttributeType> attributeTypes); List<RpslAttribute> findAttributes(final AttributeType... attributeTypes); boolean containsAttributes(final Collection<AttributeType> attributeTypes); boolean containsAttribute(final AttributeType attributeType); @Override void writeTo(final OutputStream out); void writeTo(final Writer writer); @Override byte[] toByteArray(); @Override String toString(); CIString getValueForAttribute(final AttributeType attributeType); @Nullable CIString getValueOrNullForAttribute(final AttributeType attributeType); Set<CIString> getValuesForAttribute(final AttributeType... attributeType); }### Answer: @Test public void getValuesForAttribute() { final RpslObject object = RpslObject.parse("" + "mntner: DEV-MNT\n" + "mnt-by: DEV-MNT5\n" + "mnt-by: DEV-MNT4, DEV-MNT4\n" + "mnt-by: DEV-MNT3, DEV-MNT2\n" + "mnt-by: DEV-MNT1, DEV-MNT2\n"); assertThat(convertToString(object.getValuesForAttribute(AttributeType.MNT_BY)), contains("DEV-MNT5", "DEV-MNT4", "DEV-MNT3", "DEV-MNT2", "DEV-MNT1")); assertThat(object.getValuesForAttribute(AttributeType.ADMIN_C), hasSize(0)); }
### Question: RpslAttribute { public String getValue() { return value; } RpslAttribute(final AttributeType attributeType, final CIString value); RpslAttribute(final AttributeType attributeType, final String value); RpslAttribute(final String key, final CIString value); RpslAttribute(final String key, final String value); String getKey(); String getValue(); String getCleanComment(); CIString getCleanValue(); Set<CIString> getCleanValues(); Set<CIString> getReferenceValues(); CIString getReferenceValue(); void validateSyntax(final ObjectType objectType, final ObjectMessages objectMessages); void writeTo(final Writer writer); String getFormattedValue(); void writeAttributeValueTo(final Writer writer); @Override boolean equals(Object o); @Override int hashCode(); @CheckForNull AttributeType getType(); @Override String toString(); }### Answer: @Test public void short_hand_plain() { subject = new RpslAttribute("domain", "foobar"); assertThat(RpslAttributeFilter.getValueForShortHand(subject.getValue()), is(" foobar")); } @Test public void short_hand_spaced_prefix() { subject = new RpslAttribute("domain", " foobar"); assertThat(RpslAttributeFilter.getValueForShortHand(subject.getValue()), is(" foobar")); } @Test public void short_hand_any_continuation() { subject = new RpslAttribute("domain", "foo\n bar\n+qux\n\tzot"); assertThat(RpslAttributeFilter.getValueForShortHand(subject.getValue()), is(" foo\n+ bar\n+ qux\n+ zot")); } @Test public void short_hand_only_touches_continuation_whitespace() { subject = new RpslAttribute("domain", "foo\t # \t\n bar\n+ qux\n\tzot"); assertThat(RpslAttributeFilter.getValueForShortHand(subject.getValue()), is(" foo\t # \t\n+ bar\n+ qux\n+ zot")); } @Test public void short_hand_continuation_spaces() { subject = new RpslAttribute("domain", "foo\n bar\n+ qux\n\t zot"); assertThat(RpslAttributeFilter.getValueForShortHand(subject.getValue()), is(" foo\n+ bar\n+ qux\n+ zot")); }
### Question: RpslAttribute { @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } final RpslAttribute attribute = (RpslAttribute) o; if (type == null) { if (attribute.type != null) { return false; } return key.equals(attribute.key); } else { if (type != attribute.type) { return false; } return Iterables.elementsEqual(getCleanValues(), attribute.getCleanValues()); } } RpslAttribute(final AttributeType attributeType, final CIString value); RpslAttribute(final AttributeType attributeType, final String value); RpslAttribute(final String key, final CIString value); RpslAttribute(final String key, final String value); String getKey(); String getValue(); String getCleanComment(); CIString getCleanValue(); Set<CIString> getCleanValues(); Set<CIString> getReferenceValues(); CIString getReferenceValue(); void validateSyntax(final ObjectType objectType, final ObjectMessages objectMessages); void writeTo(final Writer writer); String getFormattedValue(); void writeAttributeValueTo(final Writer writer); @Override boolean equals(Object o); @Override int hashCode(); @CheckForNull AttributeType getType(); @Override String toString(); }### Answer: @Test public void equals_is_case_insensitive() { subject = new RpslAttribute("remarks", "The quick brown fox."); assertThat(subject.equals(new RpslAttribute("remarks", "THE QUICK BROWN FOX.")), is(true)); } @Test public void equals_unknown_type() { subject = new RpslAttribute("unknown", "The quick brown fox."); assertThat(subject.equals(new RpslAttribute("unknown", "THE QUICK BROWN FOX.")), is(true)); } @Test public void equals_Integer() { subject = new RpslAttribute("remarks", "The quick brown fox."); assertFalse(subject.equals(1)); } @Test public void equals_null() { subject = new RpslAttribute("remarks", "The quick brown fox."); assertThat(subject, not(is(nullValue()))); assertFalse(subject.equals(null)); } @Test public void equals_shorthand() { subject = new RpslAttribute("remarks", "The quick brown fox."); assertThat(subject.equals(new RpslAttribute("*rm", "THE QUICK BROWN FOX.")), is(true)); }
### Question: RpslAttribute { public void validateSyntax(final ObjectType objectType, final ObjectMessages objectMessages) { for (final CIString cleanValue : getCleanValues()) { if (!type.getSyntax().matches(objectType, cleanValue.toString())) { objectMessages.addMessage(this, ValidationMessages.syntaxError(cleanValue.toString())); } } } RpslAttribute(final AttributeType attributeType, final CIString value); RpslAttribute(final AttributeType attributeType, final String value); RpslAttribute(final String key, final CIString value); RpslAttribute(final String key, final String value); String getKey(); String getValue(); String getCleanComment(); CIString getCleanValue(); Set<CIString> getCleanValues(); Set<CIString> getReferenceValues(); CIString getReferenceValue(); void validateSyntax(final ObjectType objectType, final ObjectMessages objectMessages); void writeTo(final Writer writer); String getFormattedValue(); void writeAttributeValueTo(final Writer writer); @Override boolean equals(Object o); @Override int hashCode(); @CheckForNull AttributeType getType(); @Override String toString(); }### Answer: @Test public void validateSyntax_syntax_error_and_invalid_email() { final ObjectMessages objectMessages = new ObjectMessages(); final RpslAttribute rpslAttribute = new RpslAttribute("inetnum", "auto-dbm@ripe.net"); rpslAttribute.validateSyntax(ObjectType.INETNUM, objectMessages); assertThat(objectMessages.getMessages(rpslAttribute).getAllMessages(), contains(ValidationMessages.syntaxError("auto-dbm@ripe.net"))); }
### Question: RpslAttribute { @Override public String toString() { try { final StringWriter writer = new StringWriter(); writeTo(writer); return writer.toString(); } catch (IOException e) { throw new IllegalStateException("Should never occur", e); } } RpslAttribute(final AttributeType attributeType, final CIString value); RpslAttribute(final AttributeType attributeType, final String value); RpslAttribute(final String key, final CIString value); RpslAttribute(final String key, final String value); String getKey(); String getValue(); String getCleanComment(); CIString getCleanValue(); Set<CIString> getCleanValues(); Set<CIString> getReferenceValues(); CIString getReferenceValue(); void validateSyntax(final ObjectType objectType, final ObjectMessages objectMessages); void writeTo(final Writer writer); String getFormattedValue(); void writeAttributeValueTo(final Writer writer); @Override boolean equals(Object o); @Override int hashCode(); @CheckForNull AttributeType getType(); @Override String toString(); }### Answer: @Test public void format_single_line_no_spaces() { final RpslAttribute subject = new RpslAttribute("person", "Brian Riddle"); assertThat(subject.toString(), is("person: Brian Riddle\n")); } @Test public void format_single_line_some_spaces() { final RpslAttribute subject = new RpslAttribute("person", " Brian Riddle"); assertThat(subject.toString(), is("person: Brian Riddle\n")); } @Test public void format_single_line_too_many_spaces() { final RpslAttribute subject = new RpslAttribute("person", " Brian Riddle"); assertThat(subject.toString(), is("person: Brian Riddle\n")); }
### Question: RpslAttribute { public String getCleanComment() { if (cleanValues == null) { extractCleanValueAndComment(value); } return cleanComment; } RpslAttribute(final AttributeType attributeType, final CIString value); RpslAttribute(final AttributeType attributeType, final String value); RpslAttribute(final String key, final CIString value); RpslAttribute(final String key, final String value); String getKey(); String getValue(); String getCleanComment(); CIString getCleanValue(); Set<CIString> getCleanValues(); Set<CIString> getReferenceValues(); CIString getReferenceValue(); void validateSyntax(final ObjectType objectType, final ObjectMessages objectMessages); void writeTo(final Writer writer); String getFormattedValue(); void writeAttributeValueTo(final Writer writer); @Override boolean equals(Object o); @Override int hashCode(); @CheckForNull AttributeType getType(); @Override String toString(); }### Answer: @Test public void get_comment_in_second_line() { subject = new RpslAttribute("remarks", "remark1\n remark2 # comment"); assertThat(subject.getCleanComment(), is("comment")); subject = new RpslAttribute("remarks", "foo\t # comment1 \n bar # \t comment2\n+ bla"); assertThat(subject.getCleanComment(), is("comment1 comment2")); }
### Question: ParserHelper { public static void validateMoreSpecificsOperator(final String yytext) { int val = Integer.valueOf(yytext.substring(1)); if (val > MAX_BIT_LENGTH_IPV4) { syntaxError("more specifics operator " + yytext.substring(1) + " not 0 to 32 bits"); } } private ParserHelper(); static void check16bit(final String number); static void check32bit(final String number); static void checkMaskLength(final String maskLength); static void checkMaskLengthv6(final String maskLength); static void checkStringLength(final String ip, final int maxAllowedLength); static void validateMoreSpecificsOperator(final String yytext); static void validateRangeMoreSpecificsOperators(final String yytext); static void validateAsNumber(final String yytext); static void validateIpv4PrefixRange(final String yytext); static void validateIpv6PrefixRange(final String yytext); static void validateIpv4Prefix(final String yytext); static void validateIpv6Prefix(final String yytext); static void validateIpv4(final String yytext); static void validateIpv6(final String yytext); static void validateCommunity(final String yytext); static void validateDomainName(final String yytext); static void validateDomainNameLabel(final String yytext); static void validateSmallInt(final String yytext); static void validateAsRange(final String yytext); static void log(final String message); static void log(final Exception exception); static void syntaxError(final String message); static void parserError(final String message); }### Answer: @Test public void validateMoreSpecificsOperator() { ParserHelper.validateMoreSpecificsOperator("^0"); ParserHelper.validateMoreSpecificsOperator("^32"); } @Test(expected = IllegalArgumentException.class) public void validateMoreSpecificsOperatorFailsTooLarge() { ParserHelper.validateMoreSpecificsOperator("^33"); }
### Question: ParserHelper { public static void validateRangeMoreSpecificsOperators(final String yytext) { final int dash = yytext.indexOf('-'); int from = Integer.valueOf(yytext.substring(1, dash)); if (from > MAX_BIT_LENGTH_IPV4) { syntaxError("more specifics operator " + yytext + " not 0 to 32 bits"); } int to = Integer.valueOf(yytext.substring(dash + 1)); if (to > MAX_BIT_LENGTH_IPV4) { syntaxError("more specifics operator " + yytext + " not 0 to 32 bits"); } if (to < from) { syntaxError("more specifics operator " + yytext + " not 0 to 32 bits"); } } private ParserHelper(); static void check16bit(final String number); static void check32bit(final String number); static void checkMaskLength(final String maskLength); static void checkMaskLengthv6(final String maskLength); static void checkStringLength(final String ip, final int maxAllowedLength); static void validateMoreSpecificsOperator(final String yytext); static void validateRangeMoreSpecificsOperators(final String yytext); static void validateAsNumber(final String yytext); static void validateIpv4PrefixRange(final String yytext); static void validateIpv6PrefixRange(final String yytext); static void validateIpv4Prefix(final String yytext); static void validateIpv6Prefix(final String yytext); static void validateIpv4(final String yytext); static void validateIpv6(final String yytext); static void validateCommunity(final String yytext); static void validateDomainName(final String yytext); static void validateDomainNameLabel(final String yytext); static void validateSmallInt(final String yytext); static void validateAsRange(final String yytext); static void log(final String message); static void log(final Exception exception); static void syntaxError(final String message); static void parserError(final String message); }### Answer: @Test public void validateRangeMoreSpecificsOperators() { ParserHelper.validateRangeMoreSpecificsOperators("^0-0"); ParserHelper.validateRangeMoreSpecificsOperators("^0-1"); ParserHelper.validateRangeMoreSpecificsOperators("^0-32"); ParserHelper.validateRangeMoreSpecificsOperators("^31-32"); } @Test(expected = IllegalArgumentException.class) public void validateRangeMoreSpecificsOperatorsInverseRange() { ParserHelper.validateRangeMoreSpecificsOperators("^1-0"); } @Test(expected = IllegalArgumentException.class) public void validateRangeMoreSpecificsOperatorsOutOfRange() { ParserHelper.validateRangeMoreSpecificsOperators("^0-33"); }
### Question: ParserHelper { public static void validateAsNumber(final String yytext) { final long value = Long.valueOf(yytext.substring(2)); if (value > MAX_32BIT_NUMBER) { syntaxError("AS Number " + yytext + " length is invalid"); } } private ParserHelper(); static void check16bit(final String number); static void check32bit(final String number); static void checkMaskLength(final String maskLength); static void checkMaskLengthv6(final String maskLength); static void checkStringLength(final String ip, final int maxAllowedLength); static void validateMoreSpecificsOperator(final String yytext); static void validateRangeMoreSpecificsOperators(final String yytext); static void validateAsNumber(final String yytext); static void validateIpv4PrefixRange(final String yytext); static void validateIpv6PrefixRange(final String yytext); static void validateIpv4Prefix(final String yytext); static void validateIpv6Prefix(final String yytext); static void validateIpv4(final String yytext); static void validateIpv6(final String yytext); static void validateCommunity(final String yytext); static void validateDomainName(final String yytext); static void validateDomainNameLabel(final String yytext); static void validateSmallInt(final String yytext); static void validateAsRange(final String yytext); static void log(final String message); static void log(final Exception exception); static void syntaxError(final String message); static void parserError(final String message); }### Answer: @Test public void validateAsNumber() { ParserHelper.validateAsNumber("AS0"); ParserHelper.validateAsNumber("AS" + ParserHelper.MAX_32BIT_NUMBER); } @Test(expected = IllegalArgumentException.class) public void validateAsNumberOutOfRange() { ParserHelper.validateAsNumber("AS" + (ParserHelper.MAX_32BIT_NUMBER + 1L)); }
### Question: ParserHelper { public static void validateIpv4PrefixRange(final String yytext) { Matcher m = ADDRESS_PREFIX_RANGE_PATTERN.matcher(yytext); if (m.matches()) { try { Ipv4Resource.parse(m.group(1)); } catch (IllegalArgumentException e) { syntaxError("IP prefix " + yytext + " contains an invalid octet"); } int prefix = Integer.valueOf(m.group(2)); if (prefix > MAX_BIT_LENGTH_IPV4) { syntaxError("IP prefix range " + yytext + " contains an invalid prefix length"); } String fromString = m.group(3); int from = 0; if (fromString != null) { from = Integer.valueOf(fromString); if (from > MAX_BIT_LENGTH_IPV4) { syntaxError("IP prefix range " + yytext + " contains an invalid range"); } } String toString = m.group(4); if (toString != null) { int to = Integer.valueOf(toString); if (to > MAX_BIT_LENGTH_IPV4) { syntaxError("IP prefix range " + yytext + " contains an invalid range"); } if (to < from) { syntaxError("IP prefix " + yytext + " range end is less than range start"); } } } } private ParserHelper(); static void check16bit(final String number); static void check32bit(final String number); static void checkMaskLength(final String maskLength); static void checkMaskLengthv6(final String maskLength); static void checkStringLength(final String ip, final int maxAllowedLength); static void validateMoreSpecificsOperator(final String yytext); static void validateRangeMoreSpecificsOperators(final String yytext); static void validateAsNumber(final String yytext); static void validateIpv4PrefixRange(final String yytext); static void validateIpv6PrefixRange(final String yytext); static void validateIpv4Prefix(final String yytext); static void validateIpv6Prefix(final String yytext); static void validateIpv4(final String yytext); static void validateIpv6(final String yytext); static void validateCommunity(final String yytext); static void validateDomainName(final String yytext); static void validateDomainNameLabel(final String yytext); static void validateSmallInt(final String yytext); static void validateAsRange(final String yytext); static void log(final String message); static void log(final Exception exception); static void syntaxError(final String message); static void parserError(final String message); }### Answer: @Test public void validateIpv4PrefixRange() { ParserHelper.validateIpv4PrefixRange("128.9.0.0/16^-"); ParserHelper.validateIpv4PrefixRange("5.0.0.0/8^+"); ParserHelper.validateIpv4PrefixRange("30.0.0.0/8^16"); ParserHelper.validateIpv4PrefixRange("30.0.0.0/8^24-32"); } @Test(expected = IllegalArgumentException.class) public void validateIpv4PrefixRangeInvalidPrefix() { ParserHelper.validateIpv4PrefixRange("128.9.0.0/33^-"); } @Test(expected = IllegalArgumentException.class) public void validateIpv4PrefixRangeInvalidPrefixPlus() { ParserHelper.validateIpv4PrefixRange("128.9.0.0/33^+"); } @Test(expected = IllegalArgumentException.class) public void validateIpv4PrefixRangeInvalidPrefixCaretLength() { ParserHelper.validateIpv4PrefixRange("128.9.0.0/8^33"); } @Test(expected = IllegalArgumentException.class) public void validateIpv4PrefixRangeInvalidPrefixCaretLengthEndRange() { ParserHelper.validateIpv4PrefixRange("128.9.0.0/8^24-33"); } @Test(expected = IllegalArgumentException.class) public void validateIpv4PrefixRangeInvalidPrefixCaretLengthInverseRange() { ParserHelper.validateIpv4PrefixRange("128.9.0.0/8^32-24"); }
### Question: RadbGrsSource extends GrsSource { @Override public void handleObjects(final File file, final ObjectHandler handler) throws IOException { FileInputStream is = null; try { is = new FileInputStream(file); final BufferedReader reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(is), StandardCharsets.UTF_8)); handleLines(reader, new LineHandler() { @Override public void handleLines(final List<String> lines) { if (!lines.isEmpty() && !lines.get(0).startsWith("*xx")) { handler.handle(lines); } } }); } finally { IOUtils.closeQuietly(is); } } @Autowired RadbGrsSource( @Value("${grs.import.radb.source:}") final String source, final SourceContext sourceContext, final DateTimeProvider dateTimeProvider, final AuthoritativeResourceData authoritativeResourceData, final Downloader downloader, @Value("${grs.import.radb.download:}") final String download); @Override void acquireDump(final Path path); @Override void handleObjects(final File file, final ObjectHandler handler); }### Answer: @Test public void handleObjects() throws Exception { final File file = new File(getClass().getResource("/grs/radb.test.gz").toURI()); subject.handleObjects(file, objectHandler); assertThat(objectHandler.getObjects(), hasSize(0)); assertThat(objectHandler.getLines(), hasSize(2)); assertThat(objectHandler.getLines(), contains((List<String>) Lists.newArrayList( "aut-num: AS1263\n", "as-name: TEST-AS\n", "descr: TEST-AS\n", "admin-c: Not available\n", "tech-c: See TEST-MNT\n", "mnt-by: TEST-MNT\n", "changed: test@foo.edu 19950201\n", "source: RADB\n"), Lists.newArrayList( "route: 167.96.0.0/16\n", "" + "descr: Company 2\n" + " Address 2\n" + " Postcode\n" + " State\n" + " Country\n", "origin: AS2900\n", "member-of: RS-TEST\n", "mnt-by: TEST-MNT\n", "changed: test@bar.net 19950506\n", "source: RADB\n") )); }
### Question: ParserHelper { public static void validateIpv4Prefix(final String yytext) { final int slash = yytext.indexOf('/'); try { Ipv4Resource.parse(yytext.substring(0, slash)); } catch (IllegalArgumentException e) { syntaxError("IP prefix " + yytext + " contains an invalid octet"); } final int length = Integer.valueOf(yytext.substring(slash + 1)); if (length > MAX_BIT_LENGTH_IPV4) { syntaxError("IP prefix " + yytext + " contains an invalid prefix length"); } } private ParserHelper(); static void check16bit(final String number); static void check32bit(final String number); static void checkMaskLength(final String maskLength); static void checkMaskLengthv6(final String maskLength); static void checkStringLength(final String ip, final int maxAllowedLength); static void validateMoreSpecificsOperator(final String yytext); static void validateRangeMoreSpecificsOperators(final String yytext); static void validateAsNumber(final String yytext); static void validateIpv4PrefixRange(final String yytext); static void validateIpv6PrefixRange(final String yytext); static void validateIpv4Prefix(final String yytext); static void validateIpv6Prefix(final String yytext); static void validateIpv4(final String yytext); static void validateIpv6(final String yytext); static void validateCommunity(final String yytext); static void validateDomainName(final String yytext); static void validateDomainNameLabel(final String yytext); static void validateSmallInt(final String yytext); static void validateAsRange(final String yytext); static void log(final String message); static void log(final Exception exception); static void syntaxError(final String message); static void parserError(final String message); }### Answer: @Test public void validateIpv4Prefix() { ParserHelper.validateIpv4Prefix("0.0.0.0/0"); ParserHelper.validateIpv4Prefix("1.2.3.4/32"); } @Test(expected = IllegalArgumentException.class) public void validateIpv4PrefixOutOfRange() { ParserHelper.validateIpv4Prefix("1.2.3.4/33"); }
### Question: ParserHelper { public static void validateIpv6Prefix(final String yytext) { final int slash = yytext.indexOf('/'); try { Ipv6Resource.parse(yytext.substring(0, slash)); } catch (IllegalArgumentException e) { syntaxError("IPv6 prefix " + yytext + " contains an invalid quad"); } final int length = Integer.valueOf(yytext.substring(slash + 1)); if (length > MAX_BIT_LENGTH_IPV6) { syntaxError("IPv6 prefix " + yytext + " contains an invalid prefix length"); } } private ParserHelper(); static void check16bit(final String number); static void check32bit(final String number); static void checkMaskLength(final String maskLength); static void checkMaskLengthv6(final String maskLength); static void checkStringLength(final String ip, final int maxAllowedLength); static void validateMoreSpecificsOperator(final String yytext); static void validateRangeMoreSpecificsOperators(final String yytext); static void validateAsNumber(final String yytext); static void validateIpv4PrefixRange(final String yytext); static void validateIpv6PrefixRange(final String yytext); static void validateIpv4Prefix(final String yytext); static void validateIpv6Prefix(final String yytext); static void validateIpv4(final String yytext); static void validateIpv6(final String yytext); static void validateCommunity(final String yytext); static void validateDomainName(final String yytext); static void validateDomainNameLabel(final String yytext); static void validateSmallInt(final String yytext); static void validateAsRange(final String yytext); static void log(final String message); static void log(final Exception exception); static void syntaxError(final String message); static void parserError(final String message); }### Answer: @Test public void validateIpv6Prefix() { ParserHelper.validateIpv6Prefix("2001:0DB8::/32"); ParserHelper.validateIpv6Prefix("2001:06f0:0041:0000:0000:0000:3c00:0004/128"); ParserHelper.validateIpv6Prefix("::/0"); ParserHelper.validateIpv6Prefix("2001:503:231d::/48"); } @Test(expected = IllegalArgumentException.class) public void validateIpv6PrefixOutOfRange() { ParserHelper.validateIpv6Prefix("2001:0DB8::/129"); } @Test(expected = IllegalArgumentException.class) public void validateIpv6PrefixDoubleColonOutOfRange() { ParserHelper.validateIpv6Prefix("2001:503:231d::/129"); }
### Question: ParserHelper { public static void validateIpv4(final String yytext) { try { Ipv4Resource.parse(yytext); } catch (IllegalArgumentException e) { syntaxError("IP address " + yytext + " contains an invalid octet"); } } private ParserHelper(); static void check16bit(final String number); static void check32bit(final String number); static void checkMaskLength(final String maskLength); static void checkMaskLengthv6(final String maskLength); static void checkStringLength(final String ip, final int maxAllowedLength); static void validateMoreSpecificsOperator(final String yytext); static void validateRangeMoreSpecificsOperators(final String yytext); static void validateAsNumber(final String yytext); static void validateIpv4PrefixRange(final String yytext); static void validateIpv6PrefixRange(final String yytext); static void validateIpv4Prefix(final String yytext); static void validateIpv6Prefix(final String yytext); static void validateIpv4(final String yytext); static void validateIpv6(final String yytext); static void validateCommunity(final String yytext); static void validateDomainName(final String yytext); static void validateDomainNameLabel(final String yytext); static void validateSmallInt(final String yytext); static void validateAsRange(final String yytext); static void log(final String message); static void log(final Exception exception); static void syntaxError(final String message); static void parserError(final String message); }### Answer: @Test public void validateIpv4() { ParserHelper.validateIpv4("1.2.3.4/32"); } @Test(expected = IllegalArgumentException.class) public void validateIpv4InvalidPrefixLength() { ParserHelper.validateIpv4("1.2.3.4/33"); }
### Question: ParserHelper { public static void validateIpv6(final String yytext) { try { Ipv6Resource.parse(yytext); } catch (IllegalArgumentException e) { syntaxError("IP address " + yytext + " contains an invalid quad"); } } private ParserHelper(); static void check16bit(final String number); static void check32bit(final String number); static void checkMaskLength(final String maskLength); static void checkMaskLengthv6(final String maskLength); static void checkStringLength(final String ip, final int maxAllowedLength); static void validateMoreSpecificsOperator(final String yytext); static void validateRangeMoreSpecificsOperators(final String yytext); static void validateAsNumber(final String yytext); static void validateIpv4PrefixRange(final String yytext); static void validateIpv6PrefixRange(final String yytext); static void validateIpv4Prefix(final String yytext); static void validateIpv6Prefix(final String yytext); static void validateIpv4(final String yytext); static void validateIpv6(final String yytext); static void validateCommunity(final String yytext); static void validateDomainName(final String yytext); static void validateDomainNameLabel(final String yytext); static void validateSmallInt(final String yytext); static void validateAsRange(final String yytext); static void log(final String message); static void log(final Exception exception); static void syntaxError(final String message); static void parserError(final String message); }### Answer: @Test public void validateIpv6() { ParserHelper.validateIpv6("2001:0DB8::/32"); ParserHelper.validateIpv6("2001:06f0:0041:0000:0000:0000:3c00:0004/128"); ParserHelper.validateIpv6("2001:470:1:35e::1"); ParserHelper.validateIpv6("2001:7F8:14::58:2"); ParserHelper.validateIpv6("2001:7f8:13::a500:8587:1"); ParserHelper.validateIpv6("2001:504:1::"); } @Test(expected = IllegalArgumentException.class) public void validateIpv6InvalidPrefixLength() { ParserHelper.validateIpv6("2001:0DB8::/129"); }
### Question: ParserHelper { public static void validateCommunity(final String yytext) { final int colon = yytext.indexOf(':'); final long from = Long.valueOf(yytext.substring(0, colon)); if (from > MAX_16BIT_NUMBER) { syntaxError("Community number " + yytext + " contains an invalid number"); } final long to = Long.valueOf(yytext.substring(colon + 1)); if (to > MAX_16BIT_NUMBER) { syntaxError("Community number " + yytext + " contains an invalid number"); } } private ParserHelper(); static void check16bit(final String number); static void check32bit(final String number); static void checkMaskLength(final String maskLength); static void checkMaskLengthv6(final String maskLength); static void checkStringLength(final String ip, final int maxAllowedLength); static void validateMoreSpecificsOperator(final String yytext); static void validateRangeMoreSpecificsOperators(final String yytext); static void validateAsNumber(final String yytext); static void validateIpv4PrefixRange(final String yytext); static void validateIpv6PrefixRange(final String yytext); static void validateIpv4Prefix(final String yytext); static void validateIpv6Prefix(final String yytext); static void validateIpv4(final String yytext); static void validateIpv6(final String yytext); static void validateCommunity(final String yytext); static void validateDomainName(final String yytext); static void validateDomainNameLabel(final String yytext); static void validateSmallInt(final String yytext); static void validateAsRange(final String yytext); static void log(final String message); static void log(final Exception exception); static void syntaxError(final String message); static void parserError(final String message); }### Answer: @Test public void validateCommunity() { ParserHelper.validateCommunity("1:1"); ParserHelper.validateCommunity("65535:65535"); } @Test(expected = IllegalArgumentException.class) public void validateCommunityBeforeColonOutOfRange() { ParserHelper.validateCommunity("65536:1"); } @Test(expected = IllegalArgumentException.class) public void validateCommunityAfterColonOutOfRange() { ParserHelper.validateCommunity("1:65536"); }
### Question: ParserHelper { public static void validateDomainName(final String yytext) { if (yytext.length() > DOMAIN_NAME_MAX_LENGTH) { syntaxError("Domain name " + yytext + " is longer than 255 characters"); } } private ParserHelper(); static void check16bit(final String number); static void check32bit(final String number); static void checkMaskLength(final String maskLength); static void checkMaskLengthv6(final String maskLength); static void checkStringLength(final String ip, final int maxAllowedLength); static void validateMoreSpecificsOperator(final String yytext); static void validateRangeMoreSpecificsOperators(final String yytext); static void validateAsNumber(final String yytext); static void validateIpv4PrefixRange(final String yytext); static void validateIpv6PrefixRange(final String yytext); static void validateIpv4Prefix(final String yytext); static void validateIpv6Prefix(final String yytext); static void validateIpv4(final String yytext); static void validateIpv6(final String yytext); static void validateCommunity(final String yytext); static void validateDomainName(final String yytext); static void validateDomainNameLabel(final String yytext); static void validateSmallInt(final String yytext); static void validateAsRange(final String yytext); static void log(final String message); static void log(final Exception exception); static void syntaxError(final String message); static void parserError(final String message); }### Answer: @Test public void validateDomainName() { ParserHelper.validateDomainName("12345678901234567890123456789012345678901234567890" + "12345678901234567890123456789012345678901234567890" + "12345678901234567890123456789012345678901234567890" + "12345678901234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890123456789012345"); } @Test(expected = IllegalArgumentException.class) public void validateDomainNameTooLong() { ParserHelper.validateDomainName("12345678901234567890123456789012345678901234567890" + "12345678901234567890123456789012345678901234567890" + "12345678901234567890123456789012345678901234567890" + "12345678901234567890123456789012345678901234567890" + "12345678901234567890123456789012345678901234567890123456"); }
### Question: ParserHelper { public static void validateDomainNameLabel(final String yytext) { if (yytext.length() > DOMAIN_NAME_LABEL_MAX_LENGTH) { syntaxError("Domain name label " + yytext + " is longer than 63 characters"); } } private ParserHelper(); static void check16bit(final String number); static void check32bit(final String number); static void checkMaskLength(final String maskLength); static void checkMaskLengthv6(final String maskLength); static void checkStringLength(final String ip, final int maxAllowedLength); static void validateMoreSpecificsOperator(final String yytext); static void validateRangeMoreSpecificsOperators(final String yytext); static void validateAsNumber(final String yytext); static void validateIpv4PrefixRange(final String yytext); static void validateIpv6PrefixRange(final String yytext); static void validateIpv4Prefix(final String yytext); static void validateIpv6Prefix(final String yytext); static void validateIpv4(final String yytext); static void validateIpv6(final String yytext); static void validateCommunity(final String yytext); static void validateDomainName(final String yytext); static void validateDomainNameLabel(final String yytext); static void validateSmallInt(final String yytext); static void validateAsRange(final String yytext); static void log(final String message); static void log(final Exception exception); static void syntaxError(final String message); static void parserError(final String message); }### Answer: @Test public void validateDomainNameLabel() { ParserHelper.validateDomainNameLabel("abcdefghijklmnopqrstuvwxyz"); ParserHelper.validateDomainNameLabel("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); ParserHelper.validateDomainNameLabel("123456789012345678901234567890123456789012345678901234567890123"); } @Test(expected = IllegalArgumentException.class) public void validateDomainNameLabelTooLong() { ParserHelper.validateDomainNameLabel("1234567890123456789012345678901234567890123456789012345678901234"); }
### Question: ParserHelper { public static void validateAsRange(final String yytext) { Matcher matcher = AS_RANGE_PATTERN.matcher(yytext); if (matcher.find()) { long begin = Long.valueOf(matcher.group(1)); if (begin > MAX_32BIT_NUMBER) { syntaxError("AS Number " + yytext + " is invalid"); } long end = Long.valueOf(matcher.group(2)); if (end > MAX_32BIT_NUMBER) { syntaxError("AS Number " + yytext + " is invalid"); } if (end < begin) { syntaxError("AS Range " + yytext + " is invalid"); } } } private ParserHelper(); static void check16bit(final String number); static void check32bit(final String number); static void checkMaskLength(final String maskLength); static void checkMaskLengthv6(final String maskLength); static void checkStringLength(final String ip, final int maxAllowedLength); static void validateMoreSpecificsOperator(final String yytext); static void validateRangeMoreSpecificsOperators(final String yytext); static void validateAsNumber(final String yytext); static void validateIpv4PrefixRange(final String yytext); static void validateIpv6PrefixRange(final String yytext); static void validateIpv4Prefix(final String yytext); static void validateIpv6Prefix(final String yytext); static void validateIpv4(final String yytext); static void validateIpv6(final String yytext); static void validateCommunity(final String yytext); static void validateDomainName(final String yytext); static void validateDomainNameLabel(final String yytext); static void validateSmallInt(final String yytext); static void validateAsRange(final String yytext); static void log(final String message); static void log(final Exception exception); static void syntaxError(final String message); static void parserError(final String message); }### Answer: @Test public void validateAsRange() { ParserHelper.validateAsRange("AS1 - AS2"); ParserHelper.validateAsRange("AS1 - AS2"); } @Test(expected = IllegalArgumentException.class) public void validateAsRangeInverseRange() { ParserHelper.validateAsRange("AS2 - AS1"); } @Test(expected = IllegalArgumentException.class) public void validateAsRangeInvalidToNoSpaces() { ParserHelper.validateAsRange("AS1-AS" + (1L << 32)); } @Test(expected = IllegalArgumentException.class) public void validateAsRangeInvalidToWithSpaces() { ParserHelper.validateAsRange("AS1 - AS" + (1L << 32)); }
### Question: ByteArrayOutput extends OutputStream { public void write(final int b) { final int newcount = count + 1; if (newcount > buf.length) { buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount)); } buf[count] = (byte) b; count = newcount; } ByteArrayOutput(); ByteArrayOutput(final int initialCapacity); void write(final int b); void write(final byte b[]); void write(final byte b[], final int off, final int len); void reset(); byte[] toByteArray(); int size(); }### Answer: @Test(expected = IndexOutOfBoundsException.class) public void write_bytes_offset_out_of_bounds() { subject.write(buffer, 2, buffer.length); }
### Question: ByteArrayOutput extends OutputStream { public void reset() { count = 0; } ByteArrayOutput(); ByteArrayOutput(final int initialCapacity); void write(final int b); void write(final byte b[]); void write(final byte b[], final int off, final int len); void reset(); byte[] toByteArray(); int size(); }### Answer: @Test public void reset() { subject.write(buffer); subject.reset(); assertThat(subject.toByteArray(), is(new byte[]{})); subject.write(buffer); assertThat(subject.toByteArray(), is(buffer)); }
### Question: ByteArrayOutput extends OutputStream { public int size() { return count; } ByteArrayOutput(); ByteArrayOutput(final int initialCapacity); void write(final int b); void write(final byte b[]); void write(final byte b[], final int off, final int len); void reset(); byte[] toByteArray(); int size(); }### Answer: @Test public void size() { subject.write(buffer); assertThat(subject.size(), is(buffer.length)); }
### Question: Latin1Conversion { public static String convertString(@Nonnull final String value) { final CharsetEncoder charsetEncoder = StandardCharsets.ISO_8859_1.newEncoder(); charsetEncoder.onMalformedInput(CodingErrorAction.REPLACE); charsetEncoder.onUnmappableCharacter(CodingErrorAction.REPLACE); try { final ByteBuffer encoded = charsetEncoder.encode(CharBuffer.wrap(value)); convert(encoded); return new String(encoded.array(), StandardCharsets.ISO_8859_1); } catch (CharacterCodingException e) { LOGGER.error(value, e); throw new IllegalStateException(e); } } private Latin1Conversion(); static Latin1ConversionResult convert(@Nonnull final String value); static String convertString(@Nonnull final String value); }### Answer: @Test public void convert_ascii_string() { final String ascii = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~"; assertThat(Latin1Conversion.convertString(ascii), is(ascii)); } @Test public void convert_supplement_latin1_string() { assertThat(Latin1Conversion.convertString(SUPPLEMENT), is(SUPPLEMENT)); } @Test public void convert_control_characters_string() { final String control = new String(new byte[]{0x0, 0x0a}, StandardCharsets.ISO_8859_1); assertThat(Latin1Conversion.convertString(control), is("?\n")); } @Test public void convert_non_break_space_string() { final String control = new String(new byte[]{(byte)0xa0}, StandardCharsets.ISO_8859_1); assertThat(Latin1Conversion.convertString(control), is(" ")); } @Test public void convert_silent_hyphen_string() { final String control = new String(new byte[]{(byte)0xad}, StandardCharsets.ISO_8859_1); assertThat(Latin1Conversion.convertString(control), is("-")); } @Test public void convert_non_latin1_string() { assertThat(Latin1Conversion.convertString("ΣΔ"), is("??") ); assertThat(Latin1Conversion.convertString("привет"), is("??????") ); assertThat(Latin1Conversion.convertString("مرحبا"), is("?????") ); assertThat(Latin1Conversion.convertString("你好ا"), is("???") ); } @Test public void convert_empty_input_string() { assertThat(Latin1Conversion.convertString(""), is("") ); }
### Question: Latin1Conversion { private static byte[] convert(@Nonnull final byte[] input) { for (int offset = 0; offset < input.length; offset++) { input[offset] = SUBSTITUTIONS[((int) input[offset]) & 0xff]; } return input; } private Latin1Conversion(); static Latin1ConversionResult convert(@Nonnull final String value); static String convertString(@Nonnull final String value); }### Answer: @Test public void convert_rpsl_with_supplement() { assertThat( Latin1Conversion.convert("person: test\nnic-hdl: TP1-TEST\ndescr: " + SUPPLEMENT).getRpslObject(), is(RpslObject.parse("person: test\nnic-hdl: TP1-TEST\ndescr: " + SUPPLEMENT)) ); } @Test public void convert_control_characters_rpsl() { assertThat( Latin1Conversion.convert( "person: Test\u0000 Person\nnic-hdl: TP1-TEST\nsource: TEST").getRpslObject(), is( RpslObject.parse("person: Test? Person\nnic-hdl: TP1-TEST\nsource: TEST"))); } @Test public void convert_non_break_space_rpsl() { assertThat( Latin1Conversion.convert( "person: Test\u00a0Person\nnic-hdl: TP1-TEST\nsource: TEST").getRpslObject(), is( RpslObject.parse("person: Test Person\nnic-hdl: TP1-TEST\nsource: TEST"))); } @Test public void unicode_umlaut_substituted_correctly() { assertThat( Latin1Conversion.convert( "person: Test P\u00FCrson\nnic-hdl: TP1-TEST\nsource: TEST").getRpslObject(), is( RpslObject.parse("person: Test Pürson\nnic-hdl: TP1-TEST\nsource: TEST"))); } @Test public void utf16_umlaut_not_substituted_rpsl() { assertThat( Latin1Conversion.convert( "person: Test Pürson\nnic-hdl: TP1-TEST\nsource: TEST").getRpslObject(), is( RpslObject.parse("person: Test Pürson\nnic-hdl: TP1-TEST\nsource: TEST"))); }
### Question: Ipv4Resource extends IpInterval<Ipv4Resource> implements Comparable<Ipv4Resource> { public static Ipv4Resource parse(final CIString resource) { return parse(resource.toString()); } private Ipv4Resource(final int begin, final int end); Ipv4Resource(final long begin, final long end); Ipv4Resource(final InetAddress inetAddress); static Ipv4Resource parse(final CIString resource); static Ipv4Resource parse(final String resource); static Ipv4Resource parseIPv4Resource(final String resource); static Ipv4Resource parsePrefixWithLength(final long prefix, final int prefixLength); static Ipv4Resource parseReverseDomain(final String address); @Override AttributeType getAttributeType(); long begin(); long end(); @Override boolean contains(final Ipv4Resource that); @Override boolean intersects(final Ipv4Resource that); @Override int hashCode(); @Override boolean equals(final Object obj); @Override String toString(); String toRangeString(); @Override int compareTo(final Ipv4Resource that); @Override Ipv4Resource singletonIntervalAtLowerBound(); @Override int compareUpperBound(final Ipv4Resource that); @Override InetAddress beginAsInetAddress(); @Override InetAddress endAsInetAddress(); @Override int getPrefixLength(); static final String IPV4_REVERSE_DOMAIN; static final Ipv4Resource MAX_RANGE; }### Answer: @Test(expected=IllegalArgumentException.class) public void ipv4_with_prefix_21_fails() { subject = Ipv4Resource.parse("151.64.0.1/21\r\n"); } @Test(expected=IllegalArgumentException.class) public void ipv4_with_prefix_23_fails() { subject = Ipv4Resource.parse("109.73.65.0/23\r\n"); } @Test(expected=IllegalArgumentException.class) public void ipv4_with_prefix_28_fails() { subject = Ipv4Resource.parse("62.219.43.72/28\r\n"); } @Test(expected=IllegalArgumentException.class) public void ipv4_with_huge_prefix_fails() { subject = Ipv4Resource.parse("128.0.0.0/0\r\n"); } @Test(expected=IllegalArgumentException.class) public void ipv4_with_tiny_prefix_fails() { subject = Ipv4Resource.parse("192.192.192.1/31\r\n"); } @Test(expected=IllegalArgumentException.class) public void ipv4_with_prefix_21() { subject = Ipv4Resource.parse("151.64.0.1/21\r\n"); } @Test(expected = IllegalArgumentException.class) public void invalidResource() { Ipv4Resource.parse("invalid resource"); } @Test(expected = IllegalArgumentException.class) public void invalidResourceType() { Ipv4Resource.parse("::0"); }
### Question: Ipv4Resource extends IpInterval<Ipv4Resource> implements Comparable<Ipv4Resource> { @Override public Ipv4Resource singletonIntervalAtLowerBound() { return new Ipv4Resource(this.begin(), this.begin()); } private Ipv4Resource(final int begin, final int end); Ipv4Resource(final long begin, final long end); Ipv4Resource(final InetAddress inetAddress); static Ipv4Resource parse(final CIString resource); static Ipv4Resource parse(final String resource); static Ipv4Resource parseIPv4Resource(final String resource); static Ipv4Resource parsePrefixWithLength(final long prefix, final int prefixLength); static Ipv4Resource parseReverseDomain(final String address); @Override AttributeType getAttributeType(); long begin(); long end(); @Override boolean contains(final Ipv4Resource that); @Override boolean intersects(final Ipv4Resource that); @Override int hashCode(); @Override boolean equals(final Object obj); @Override String toString(); String toRangeString(); @Override int compareTo(final Ipv4Resource that); @Override Ipv4Resource singletonIntervalAtLowerBound(); @Override int compareUpperBound(final Ipv4Resource that); @Override InetAddress beginAsInetAddress(); @Override InetAddress endAsInetAddress(); @Override int getPrefixLength(); static final String IPV4_REVERSE_DOMAIN; static final Ipv4Resource MAX_RANGE; }### Answer: @Test public void singletonIntervalAtLowerBound() { assertEquals(Ipv4Resource.parse("127.0.0.0/32"), Ipv4Resource.parse("127.0.0.0/8").singletonIntervalAtLowerBound()); }
### Question: Ipv4Resource extends IpInterval<Ipv4Resource> implements Comparable<Ipv4Resource> { @Override public int compareTo(final Ipv4Resource that) { if (this.begin() < that.begin()) { return -1; } else if (this.begin() > that.begin()) { return 1; } else if (that.end() < this.end()) { return -1; } else if (that.end() > this.end()) { return 1; } else { return 0; } } private Ipv4Resource(final int begin, final int end); Ipv4Resource(final long begin, final long end); Ipv4Resource(final InetAddress inetAddress); static Ipv4Resource parse(final CIString resource); static Ipv4Resource parse(final String resource); static Ipv4Resource parseIPv4Resource(final String resource); static Ipv4Resource parsePrefixWithLength(final long prefix, final int prefixLength); static Ipv4Resource parseReverseDomain(final String address); @Override AttributeType getAttributeType(); long begin(); long end(); @Override boolean contains(final Ipv4Resource that); @Override boolean intersects(final Ipv4Resource that); @Override int hashCode(); @Override boolean equals(final Object obj); @Override String toString(); String toRangeString(); @Override int compareTo(final Ipv4Resource that); @Override Ipv4Resource singletonIntervalAtLowerBound(); @Override int compareUpperBound(final Ipv4Resource that); @Override InetAddress beginAsInetAddress(); @Override InetAddress endAsInetAddress(); @Override int getPrefixLength(); static final String IPV4_REVERSE_DOMAIN; static final Ipv4Resource MAX_RANGE; }### Answer: @Test public void compareWorks() { subject = new Ipv4Resource(10, 20); assertThat(0, eq(subject.compareTo(subject))); assertThat(1, eq(subject.compareTo(new Ipv4Resource(0, 9)))); assertThat(-1, eq(subject.compareTo(new Ipv4Resource(21, 30)))); assertThat(1, eq(subject.compareTo(new Ipv4Resource(0, 15)))); assertThat(-1, eq(subject.compareTo(new Ipv4Resource(15, 30)))); assertThat(1, eq(subject.compareTo(new Ipv4Resource(0, 20)))); assertThat(1, eq(subject.compareTo(new Ipv4Resource(10, 30)))); assertThat(-1, eq(subject.compareTo(new Ipv4Resource(11, 30)))); assertThat(-1, eq(subject.compareTo(new Ipv4Resource(10, 19)))); assertThat(-1, eq(subject.compareTo(new Ipv4Resource(11, 20)))); }
### Question: ResourceTagger { void tagObjects(final GrsSource grsSource) { final Stopwatch stopwatch = Stopwatch.createStarted(); try { sourceContext.setCurrent(Source.master(grsSource.getName())); if (sourceContext.isTagRoutes()) { tagRouteObjectsInContext(grsSource); } tagsDao.deleteOrphanedTags(); } finally { sourceContext.removeCurrentSource(); grsSource.getLogger().info("Tagging objects complete in {}", stopwatch.stop()); } } @Autowired ResourceTagger(final SourceContext sourceContext, final TagsDao tagsDao); }### Answer: @Test public void tagObjects() { subject.tagObjects(grsSource); verify(sourceContext).setCurrent(any(Source.class)); verify(sourceContext).removeCurrentSource(); verify(tagsDao).updateTags(any(Iterable.class), any(List.class), any(List.class)); verify(tagsDao).deleteOrphanedTags(); }
### Question: Ipv4Resource extends IpInterval<Ipv4Resource> implements Comparable<Ipv4Resource> { @Override public boolean intersects(final Ipv4Resource that) { return (isIPWithinRange(this.begin(), that) || isIPWithinRange(this.end(), that) || isIPWithinRange(that.begin(), this)); } private Ipv4Resource(final int begin, final int end); Ipv4Resource(final long begin, final long end); Ipv4Resource(final InetAddress inetAddress); static Ipv4Resource parse(final CIString resource); static Ipv4Resource parse(final String resource); static Ipv4Resource parseIPv4Resource(final String resource); static Ipv4Resource parsePrefixWithLength(final long prefix, final int prefixLength); static Ipv4Resource parseReverseDomain(final String address); @Override AttributeType getAttributeType(); long begin(); long end(); @Override boolean contains(final Ipv4Resource that); @Override boolean intersects(final Ipv4Resource that); @Override int hashCode(); @Override boolean equals(final Object obj); @Override String toString(); String toRangeString(); @Override int compareTo(final Ipv4Resource that); @Override Ipv4Resource singletonIntervalAtLowerBound(); @Override int compareUpperBound(final Ipv4Resource that); @Override InetAddress beginAsInetAddress(); @Override InetAddress endAsInetAddress(); @Override int getPrefixLength(); static final String IPV4_REVERSE_DOMAIN; static final Ipv4Resource MAX_RANGE; }### Answer: @Test public void verifyIntersects() { subject = new Ipv4Resource(10, 20); assertTrue(subject.intersects(subject)); assertTrue(subject.intersects(Ipv4Resource.MAX_RANGE)); assertFalse(subject.intersects(new Ipv4Resource(9, 9))); assertTrue(subject.intersects(new Ipv4Resource(9, 10))); assertTrue(subject.intersects(new Ipv4Resource(10, 11))); assertTrue(subject.intersects(new Ipv4Resource(5, 15))); assertFalse(subject.intersects(new Ipv4Resource(21, 21))); assertTrue(subject.intersects(new Ipv4Resource(19, 20))); assertTrue(subject.intersects(new Ipv4Resource(20, 21))); assertTrue(subject.intersects(new Ipv4Resource(15, 25))); }
### Question: GrsSource implements InitializingBean { @Override public String toString() { return name.toString(); } GrsSource(final String name, final SourceContext sourceContext, final DateTimeProvider dateTimeProvider, final AuthoritativeResourceData authoritativeResourceData, final Downloader downloader); @Override void afterPropertiesSet(); Logger getLogger(); @Override String toString(); }### Answer: @Test public void string() { assertThat(subject.toString(), is("SOME-GRS")); }
### Question: Ipv4Resource extends IpInterval<Ipv4Resource> implements Comparable<Ipv4Resource> { public static Ipv4Resource parsePrefixWithLength(final long prefix, final int prefixLength) { final long mask = (1L << (32 - prefixLength)) - 1; return new Ipv4Resource((prefix & ~mask) & 0xFFFFFFFFL, (prefix | mask) & 0xFFFFFFFFL); } private Ipv4Resource(final int begin, final int end); Ipv4Resource(final long begin, final long end); Ipv4Resource(final InetAddress inetAddress); static Ipv4Resource parse(final CIString resource); static Ipv4Resource parse(final String resource); static Ipv4Resource parseIPv4Resource(final String resource); static Ipv4Resource parsePrefixWithLength(final long prefix, final int prefixLength); static Ipv4Resource parseReverseDomain(final String address); @Override AttributeType getAttributeType(); long begin(); long end(); @Override boolean contains(final Ipv4Resource that); @Override boolean intersects(final Ipv4Resource that); @Override int hashCode(); @Override boolean equals(final Object obj); @Override String toString(); String toRangeString(); @Override int compareTo(final Ipv4Resource that); @Override Ipv4Resource singletonIntervalAtLowerBound(); @Override int compareUpperBound(final Ipv4Resource that); @Override InetAddress beginAsInetAddress(); @Override InetAddress endAsInetAddress(); @Override int getPrefixLength(); static final String IPV4_REVERSE_DOMAIN; static final Ipv4Resource MAX_RANGE; }### Answer: @Test public void parsePrefixWithLength() { assertThat(Ipv4Resource.parsePrefixWithLength(0, 0).toString(), is("0.0.0.0/0")); assertThat(Ipv4Resource.parsePrefixWithLength(0xffffffff, 0).toString(), is("0.0.0.0/0")); assertThat(Ipv4Resource.parsePrefixWithLength(0, 32).toString(), is("0.0.0.0/32")); assertThat(Ipv4Resource.parsePrefixWithLength(0xffffffff, 32).toString(), is("255.255.255.255/32")); assertThat(Ipv4Resource.parsePrefixWithLength(0xDEADBEEF, 13).toString(), is("222.168.0.0/13")); assertThat(Ipv4Resource.parsePrefixWithLength(0xCAFEBABE, 26).toString(), is("202.254.186.128/26")); }
### Question: IpInterval implements Interval<K> { public static IpInterval<?> parseReverseDomain(final String reverse) { final String result = removeTrailingDot(reverse).toLowerCase(); if (result.endsWith(Ipv4Resource.IPV4_REVERSE_DOMAIN)) { return Ipv4Resource.parseReverseDomain(result); } return Ipv6Resource.parseReverseDomain(result); } static String removeTrailingDot(final String address); abstract AttributeType getAttributeType(); static IpInterval<?> parse(final CIString prefix); static IpInterval<?> parse(final String prefix); static IpInterval<?> parseReverseDomain(final String reverse); static IpInterval<?> asIpInterval(final InetAddress address); abstract InetAddress beginAsInetAddress(); abstract InetAddress endAsInetAddress(); abstract int getPrefixLength(); }### Answer: @Test public void parseReverseDomain() { assertThat(IpInterval.parseReverseDomain("0.0.193.in-ADDR.arpA").toString(), is("193.0.0.0/24")); assertThat(IpInterval.parseReverseDomain("a.b.0.0.1.iP6.arpA").toString(), is("100b:a000::/20")); assertThat(IpInterval.parseReverseDomain("0.0.193.in-ADDR.arpA.").toString(), is("193.0.0.0/24")); assertThat(IpInterval.parseReverseDomain("a.b.0.0.1.iP6.arpA.").toString(), is("100b:a000::/20")); }
### Question: IpInterval implements Interval<K> { public static IpInterval<?> parse(final CIString prefix) { return parse(prefix.toString()); } static String removeTrailingDot(final String address); abstract AttributeType getAttributeType(); static IpInterval<?> parse(final CIString prefix); static IpInterval<?> parse(final String prefix); static IpInterval<?> parseReverseDomain(final String reverse); static IpInterval<?> asIpInterval(final InetAddress address); abstract InetAddress beginAsInetAddress(); abstract InetAddress endAsInetAddress(); abstract int getPrefixLength(); }### Answer: @Test public void parse() { assertThat(IpInterval.parse("193.0.0/24").toString(), is("193.0.0.0/24")); assertThat(IpInterval.parse("00ab:cd::").toString(), is("ab:cd::/128")); }
### Question: Ipv6Resource extends IpInterval<Ipv6Resource> implements Comparable<Ipv6Resource> { public static Ipv6Resource parse(final InetAddress ipv6Address) { final long[] res = byteArrayToLongArray(ipv6Address.getAddress()); return new Ipv6Resource(res[0], res[1], IPV6_BITCOUNT); } private Ipv6Resource(final long msb, final long lsb, final int prefixLength); Ipv6Resource(final BigInteger begin, final BigInteger end); static long lsb(final BigInteger begin); static long msb(final BigInteger begin); static Ipv6Resource parseFromStrings(final String msb, final String lsb, final int len); static Ipv6Resource parse(final InetAddress ipv6Address); static Ipv6Resource parse(final CIString prefixOrAddress); static Ipv6Resource parse(final String prefixOrAddress); static Ipv6Resource parseReverseDomain(final String address); @Override final AttributeType getAttributeType(); BigInteger begin(); BigInteger end(); static int compare(final long aMsb, final long aLsb, final long bMsb, final long bLsb); @Override int compareTo(final Ipv6Resource that); @Override boolean contains(final Ipv6Resource that); @Override boolean intersects(final Ipv6Resource that); @Override Ipv6Resource singletonIntervalAtLowerBound(); @Override int compareUpperBound(final Ipv6Resource that); @Override InetAddress beginAsInetAddress(); @Override InetAddress endAsInetAddress(); @Override int getPrefixLength(); @Override String toString(); @Override int hashCode(); @Override boolean equals(final Object obj); static Ipv6Resource parseIPv6Resource(final String resource); static final String IPV6_REVERSE_DOMAIN; static final Ipv6Resource MAX_RANGE; }### Answer: @Ignore @Test public void ipv4_mapped_ipv6_address() { subject = Ipv6Resource.parse("::ffff:c16e:370c/128"); } @Test(expected = IllegalArgumentException.class) public void parseIpv6MappedIpv4Fails() { subject = Ipv6Resource.parse("::ffff:192.0.2.128"); } @Test(expected = IllegalArgumentException.class) public void invalidResource() { Ipv6Resource.parse("invalid resource"); } @Test(expected = IllegalArgumentException.class) public void invalidResourceType() { Ipv6Resource.parse("12.0.0.1"); } @Test(expected = IllegalArgumentException.class) public void Ipv6RangeThrowsIllegalArgumentException() { Ipv6Resource.parse("2001:: - 2020::"); } @Test(expected = IllegalArgumentException.class) public void invalid_prefix_length() { Ipv6Resource.parse("2001::/129"); }
### Question: Ipv6Resource extends IpInterval<Ipv6Resource> implements Comparable<Ipv6Resource> { @Override public Ipv6Resource singletonIntervalAtLowerBound() { return new Ipv6Resource(beginMsb, beginLsb, IPV6_BITCOUNT); } private Ipv6Resource(final long msb, final long lsb, final int prefixLength); Ipv6Resource(final BigInteger begin, final BigInteger end); static long lsb(final BigInteger begin); static long msb(final BigInteger begin); static Ipv6Resource parseFromStrings(final String msb, final String lsb, final int len); static Ipv6Resource parse(final InetAddress ipv6Address); static Ipv6Resource parse(final CIString prefixOrAddress); static Ipv6Resource parse(final String prefixOrAddress); static Ipv6Resource parseReverseDomain(final String address); @Override final AttributeType getAttributeType(); BigInteger begin(); BigInteger end(); static int compare(final long aMsb, final long aLsb, final long bMsb, final long bLsb); @Override int compareTo(final Ipv6Resource that); @Override boolean contains(final Ipv6Resource that); @Override boolean intersects(final Ipv6Resource that); @Override Ipv6Resource singletonIntervalAtLowerBound(); @Override int compareUpperBound(final Ipv6Resource that); @Override InetAddress beginAsInetAddress(); @Override InetAddress endAsInetAddress(); @Override int getPrefixLength(); @Override String toString(); @Override int hashCode(); @Override boolean equals(final Object obj); static Ipv6Resource parseIPv6Resource(final String resource); static final String IPV6_REVERSE_DOMAIN; static final Ipv6Resource MAX_RANGE; }### Answer: @Test public void singletonIntervalAtLowerBound() { assertEquals(Ipv6Resource.parse("2001::/128"), Ipv6Resource.parse("2001::/77").singletonIntervalAtLowerBound()); }
### Question: Ipv6Resource extends IpInterval<Ipv6Resource> implements Comparable<Ipv6Resource> { @Override public int compareTo(final Ipv6Resource that) { int comp = compare(beginMsb, beginLsb, that.beginMsb, that.beginLsb); if (comp == 0) { comp = compare(that.endMsb, that.endLsb, endMsb, endLsb); } return comp; } private Ipv6Resource(final long msb, final long lsb, final int prefixLength); Ipv6Resource(final BigInteger begin, final BigInteger end); static long lsb(final BigInteger begin); static long msb(final BigInteger begin); static Ipv6Resource parseFromStrings(final String msb, final String lsb, final int len); static Ipv6Resource parse(final InetAddress ipv6Address); static Ipv6Resource parse(final CIString prefixOrAddress); static Ipv6Resource parse(final String prefixOrAddress); static Ipv6Resource parseReverseDomain(final String address); @Override final AttributeType getAttributeType(); BigInteger begin(); BigInteger end(); static int compare(final long aMsb, final long aLsb, final long bMsb, final long bLsb); @Override int compareTo(final Ipv6Resource that); @Override boolean contains(final Ipv6Resource that); @Override boolean intersects(final Ipv6Resource that); @Override Ipv6Resource singletonIntervalAtLowerBound(); @Override int compareUpperBound(final Ipv6Resource that); @Override InetAddress beginAsInetAddress(); @Override InetAddress endAsInetAddress(); @Override int getPrefixLength(); @Override String toString(); @Override int hashCode(); @Override boolean equals(final Object obj); static Ipv6Resource parseIPv6Resource(final String resource); static final String IPV6_REVERSE_DOMAIN; static final Ipv6Resource MAX_RANGE; }### Answer: @Test public void compareWorks() { subject = resource(10, 20); assertThat(0, is(subject.compareTo(subject))); assertThat(1, is(subject.compareTo(resource(0, 9)))); assertThat(-1, is(subject.compareTo(resource(21, 30)))); assertThat(1, is(subject.compareTo(resource(0, 15)))); assertThat(-1, is(subject.compareTo(resource(15, 30)))); assertThat(1, is(subject.compareTo(resource(0, 20)))); assertThat(1, is(subject.compareTo(resource(10, 30)))); assertThat(-1, is(subject.compareTo(resource(11, 30)))); assertThat(-1, is(subject.compareTo(resource(10, 19)))); assertThat(-1, is(subject.compareTo(resource(11, 20)))); }
### Question: Ipv6Resource extends IpInterval<Ipv6Resource> implements Comparable<Ipv6Resource> { @Override public boolean intersects(final Ipv6Resource that) { return (compare(beginMsb, beginLsb, that.beginMsb, that.beginLsb) >= 0 && compare(beginMsb, beginLsb, that.endMsb, that.endLsb) <= 0) || (compare(endMsb, endLsb, that.beginMsb, that.beginLsb) >= 0 && compare(endMsb, endLsb, that.endMsb, that.endLsb) <= 0) || contains(that); } private Ipv6Resource(final long msb, final long lsb, final int prefixLength); Ipv6Resource(final BigInteger begin, final BigInteger end); static long lsb(final BigInteger begin); static long msb(final BigInteger begin); static Ipv6Resource parseFromStrings(final String msb, final String lsb, final int len); static Ipv6Resource parse(final InetAddress ipv6Address); static Ipv6Resource parse(final CIString prefixOrAddress); static Ipv6Resource parse(final String prefixOrAddress); static Ipv6Resource parseReverseDomain(final String address); @Override final AttributeType getAttributeType(); BigInteger begin(); BigInteger end(); static int compare(final long aMsb, final long aLsb, final long bMsb, final long bLsb); @Override int compareTo(final Ipv6Resource that); @Override boolean contains(final Ipv6Resource that); @Override boolean intersects(final Ipv6Resource that); @Override Ipv6Resource singletonIntervalAtLowerBound(); @Override int compareUpperBound(final Ipv6Resource that); @Override InetAddress beginAsInetAddress(); @Override InetAddress endAsInetAddress(); @Override int getPrefixLength(); @Override String toString(); @Override int hashCode(); @Override boolean equals(final Object obj); static Ipv6Resource parseIPv6Resource(final String resource); static final String IPV6_REVERSE_DOMAIN; static final Ipv6Resource MAX_RANGE; }### Answer: @Test public void verifyIntersects() { subject = resource(10, 20); assertTrue(subject.intersects(subject)); assertTrue(subject.intersects(Ipv6Resource.MAX_RANGE)); assertFalse(subject.intersects(resource(9, 9))); assertTrue(subject.intersects(resource(9, 10))); assertTrue(subject.intersects(resource(9, 15))); assertTrue(subject.intersects(resource(9, 20))); assertTrue(subject.intersects(resource(9, 21))); assertTrue(subject.intersects(resource(10, 10))); assertTrue(subject.intersects(resource(10, 15))); assertTrue(subject.intersects(resource(10, 20))); assertTrue(subject.intersects(resource(10, 21))); assertTrue(subject.intersects(resource(15, 15))); assertTrue(subject.intersects(resource(15, 20))); assertTrue(subject.intersects(resource(15, 21))); assertTrue(subject.intersects(resource(20, 20))); assertTrue(subject.intersects(resource(20, 21))); assertFalse(subject.intersects(resource(21, 21))); }
### Question: Ipv6Resource extends IpInterval<Ipv6Resource> implements Comparable<Ipv6Resource> { public static Ipv6Resource parseReverseDomain(final String address) { Validate.notEmpty(address, "Address cannot be empty"); String cleanAddress = removeTrailingDot(address.trim()).toLowerCase(); Validate.isTrue(cleanAddress.endsWith(IPV6_REVERSE_DOMAIN), "Invalid reverse domain: ", address); cleanAddress = cleanAddress.substring(0, cleanAddress.length() - IPV6_REVERSE_DOMAIN.length()); Validate.isTrue(REVERSE_PATTERN.matcher(cleanAddress).matches(), "Invalid reverse domain: ", address); final StringBuilder builder = new StringBuilder(); int netmask = 0; for (int index = cleanAddress.length() - 1; index >= 0; index -= 2) { builder.append(cleanAddress.charAt(index)); netmask += 4; if (netmask % 16 == 0 && index > 0) { builder.append(':'); } } if (netmask % 16 != 0) { for (int i = 4 - ((netmask / 4) % 4); i > 0; i--) { builder.append('0'); } } if (netmask <= 112) { builder.append("::"); } builder.append('/'); builder.append(netmask); return parse(builder.toString()); } private Ipv6Resource(final long msb, final long lsb, final int prefixLength); Ipv6Resource(final BigInteger begin, final BigInteger end); static long lsb(final BigInteger begin); static long msb(final BigInteger begin); static Ipv6Resource parseFromStrings(final String msb, final String lsb, final int len); static Ipv6Resource parse(final InetAddress ipv6Address); static Ipv6Resource parse(final CIString prefixOrAddress); static Ipv6Resource parse(final String prefixOrAddress); static Ipv6Resource parseReverseDomain(final String address); @Override final AttributeType getAttributeType(); BigInteger begin(); BigInteger end(); static int compare(final long aMsb, final long aLsb, final long bMsb, final long bLsb); @Override int compareTo(final Ipv6Resource that); @Override boolean contains(final Ipv6Resource that); @Override boolean intersects(final Ipv6Resource that); @Override Ipv6Resource singletonIntervalAtLowerBound(); @Override int compareUpperBound(final Ipv6Resource that); @Override InetAddress beginAsInetAddress(); @Override InetAddress endAsInetAddress(); @Override int getPrefixLength(); @Override String toString(); @Override int hashCode(); @Override boolean equals(final Object obj); static Ipv6Resource parseIPv6Resource(final String resource); static final String IPV6_REVERSE_DOMAIN; static final Ipv6Resource MAX_RANGE; }### Answer: @Test(expected = IllegalArgumentException.class) public void reverse_empty() { Ipv6Resource.parseReverseDomain(""); } @Test(expected = IllegalArgumentException.class) public void reverse_null() { Ipv6Resource.parseReverseDomain(null); } @Test(expected = IllegalArgumentException.class) public void reverse_no_ip6arpa() { Ipv6Resource.parseReverseDomain("1.2.3.4"); } @Test(expected = IllegalArgumentException.class) public void reverse_no_octets() { Ipv6Resource.parseReverseDomain(".ip6.arpa"); } @Test(expected = IllegalArgumentException.class) public void reverse_more_than_four_octets() { Ipv6Resource.parseReverseDomain("8.7.6.5.4.3.2.1.in-addr.arpa"); } @Test(expected = IllegalArgumentException.class) public void reverse_invalid_nibbles_dash() { Ipv6Resource.parseReverseDomain("1-1.1.a.ip6.arpa"); } @Test(expected = IllegalArgumentException.class) public void reverse_invalid_nibbles_non_hex() { Ipv6Resource.parseReverseDomain("g.ip6.arpa"); }
### Question: CIString implements Comparable<CIString>, CharSequence { @Nullable public static CIString ciString(final String value) { if (value == null) { return null; } return new CIString(value); } private CIString(final String value); @Nullable static CIString ciString(final String value); static Set<CIString> ciSet(final String... values); static Set<CIString> ciImmutableSet(final String... values); static Set<CIString> ciSet(final Iterable<String> values); static Set<CIString> ciImmutableSet(final Iterable<String> values); static boolean isBlank(final CIString ciString); @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final CIString o); @Override @Nonnull String toString(); String toLowerCase(); String toUpperCase(); int toInt(); @Override int length(); @Override char charAt(final int index); @Override CharSequence subSequence(final int start, final int end); boolean startsWith(final CIString value); boolean startsWith(final String value); boolean contains(final CIString value); boolean contains(final String value); boolean endsWith(final CIString value); boolean endsWith(final String value); CIString append(final CIString other); }### Answer: @Test public void create_null() { assertNull(ciString(null)); }
### Question: CIString implements Comparable<CIString>, CharSequence { @Override public boolean equals(@Nullable final Object o) { if (this == o) { return true; } if (o == null) { return false; } if (o instanceof String) { return value.equalsIgnoreCase((String) o); } return getClass() == o.getClass() && lcValue.equals(((CIString) o).lcValue); } private CIString(final String value); @Nullable static CIString ciString(final String value); static Set<CIString> ciSet(final String... values); static Set<CIString> ciImmutableSet(final String... values); static Set<CIString> ciSet(final Iterable<String> values); static Set<CIString> ciImmutableSet(final Iterable<String> values); static boolean isBlank(final CIString ciString); @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final CIString o); @Override @Nonnull String toString(); String toLowerCase(); String toUpperCase(); int toInt(); @Override int length(); @Override char charAt(final int index); @Override CharSequence subSequence(final int start, final int end); boolean startsWith(final CIString value); boolean startsWith(final String value); boolean contains(final CIString value); boolean contains(final String value); boolean endsWith(final CIString value); boolean endsWith(final String value); CIString append(final CIString other); }### Answer: @Test public void equals() { assertThat(ciString("ABC"), is(ciString("ABC"))); assertThat(ciString("ABC"), is(ciString("abc"))); assertThat(ciString("ABC"), is(ciString("aBc"))); final CIString ripe = CIString.ciString("RIPE"); assertFalse(ripe.equals(null)); assertTrue(ripe.equals(ripe)); assertTrue(ripe.equals(CIString.ciString("RIPE"))); assertTrue(ripe.equals("ripe")); assertTrue(ripe.equals("RIPE")); }
### Question: CIString implements Comparable<CIString>, CharSequence { public static Set<CIString> ciSet(final String... values) { final Set<CIString> result = Sets.newLinkedHashSetWithExpectedSize(values.length); for (final String value : values) { result.add(ciString(value)); } return result; } private CIString(final String value); @Nullable static CIString ciString(final String value); static Set<CIString> ciSet(final String... values); static Set<CIString> ciImmutableSet(final String... values); static Set<CIString> ciSet(final Iterable<String> values); static Set<CIString> ciImmutableSet(final Iterable<String> values); static boolean isBlank(final CIString ciString); @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final CIString o); @Override @Nonnull String toString(); String toLowerCase(); String toUpperCase(); int toInt(); @Override int length(); @Override char charAt(final int index); @Override CharSequence subSequence(final int start, final int end); boolean startsWith(final CIString value); boolean startsWith(final String value); boolean contains(final CIString value); boolean contains(final String value); boolean endsWith(final CIString value); boolean endsWith(final String value); CIString append(final CIString other); }### Answer: @Test public void ciset() { assertThat(ciSet("a", "b"), is(ciSet("A", "b"))); assertThat(ciSet("a", "b").contains(ciString("A")), is(true)); assertThat(ciSet("a", "b").contains(ciString("c")), is(false)); }
### Question: CIString implements Comparable<CIString>, CharSequence { @Override @Nonnull public String toString() { return value; } private CIString(final String value); @Nullable static CIString ciString(final String value); static Set<CIString> ciSet(final String... values); static Set<CIString> ciImmutableSet(final String... values); static Set<CIString> ciSet(final Iterable<String> values); static Set<CIString> ciImmutableSet(final Iterable<String> values); static boolean isBlank(final CIString ciString); @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final CIString o); @Override @Nonnull String toString(); String toLowerCase(); String toUpperCase(); int toInt(); @Override int length(); @Override char charAt(final int index); @Override CharSequence subSequence(final int start, final int end); boolean startsWith(final CIString value); boolean startsWith(final String value); boolean contains(final CIString value); boolean contains(final String value); boolean endsWith(final CIString value); boolean endsWith(final String value); CIString append(final CIString other); }### Answer: @Test public void tostring() { assertThat(ciString("SoMe WeIrd CasIng").toString(), is("SoMe WeIrd CasIng")); }
### Question: CIString implements Comparable<CIString>, CharSequence { @Override public CharSequence subSequence(final int start, final int end) { return value.subSequence(start, end); } private CIString(final String value); @Nullable static CIString ciString(final String value); static Set<CIString> ciSet(final String... values); static Set<CIString> ciImmutableSet(final String... values); static Set<CIString> ciSet(final Iterable<String> values); static Set<CIString> ciImmutableSet(final Iterable<String> values); static boolean isBlank(final CIString ciString); @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final CIString o); @Override @Nonnull String toString(); String toLowerCase(); String toUpperCase(); int toInt(); @Override int length(); @Override char charAt(final int index); @Override CharSequence subSequence(final int start, final int end); boolean startsWith(final CIString value); boolean startsWith(final String value); boolean contains(final CIString value); boolean contains(final String value); boolean endsWith(final CIString value); boolean endsWith(final String value); CIString append(final CIString other); }### Answer: @Test public void subsequence() { assertThat(ciString("abcdef").subSequence(1, 3), is((CharSequence) "bc")); assertThat(ciString("ABCDEF").subSequence(1, 3), is((CharSequence) "BC")); }
### Question: CIString implements Comparable<CIString>, CharSequence { @Override public char charAt(final int index) { return value.charAt(index); } private CIString(final String value); @Nullable static CIString ciString(final String value); static Set<CIString> ciSet(final String... values); static Set<CIString> ciImmutableSet(final String... values); static Set<CIString> ciSet(final Iterable<String> values); static Set<CIString> ciImmutableSet(final Iterable<String> values); static boolean isBlank(final CIString ciString); @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final CIString o); @Override @Nonnull String toString(); String toLowerCase(); String toUpperCase(); int toInt(); @Override int length(); @Override char charAt(final int index); @Override CharSequence subSequence(final int start, final int end); boolean startsWith(final CIString value); boolean startsWith(final String value); boolean contains(final CIString value); boolean contains(final String value); boolean endsWith(final CIString value); boolean endsWith(final String value); CIString append(final CIString other); }### Answer: @Test public void charAt() { assertThat(ciString("abcdef").charAt(1), is('b')); assertThat(ciString("ABCDEF").charAt(1), is('B')); }
### Question: CIString implements Comparable<CIString>, CharSequence { @Override public int length() { return value.length(); } private CIString(final String value); @Nullable static CIString ciString(final String value); static Set<CIString> ciSet(final String... values); static Set<CIString> ciImmutableSet(final String... values); static Set<CIString> ciSet(final Iterable<String> values); static Set<CIString> ciImmutableSet(final Iterable<String> values); static boolean isBlank(final CIString ciString); @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final CIString o); @Override @Nonnull String toString(); String toLowerCase(); String toUpperCase(); int toInt(); @Override int length(); @Override char charAt(final int index); @Override CharSequence subSequence(final int start, final int end); boolean startsWith(final CIString value); boolean startsWith(final String value); boolean contains(final CIString value); boolean contains(final String value); boolean endsWith(final CIString value); boolean endsWith(final String value); CIString append(final CIString other); }### Answer: @Test public void length() { assertThat(ciString("").length(), is(0)); assertThat(ciString("abcdef").length(), is(6)); }
### Question: CIString implements Comparable<CIString>, CharSequence { public boolean startsWith(final CIString value) { return lcValue.startsWith(value.lcValue); } private CIString(final String value); @Nullable static CIString ciString(final String value); static Set<CIString> ciSet(final String... values); static Set<CIString> ciImmutableSet(final String... values); static Set<CIString> ciSet(final Iterable<String> values); static Set<CIString> ciImmutableSet(final Iterable<String> values); static boolean isBlank(final CIString ciString); @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final CIString o); @Override @Nonnull String toString(); String toLowerCase(); String toUpperCase(); int toInt(); @Override int length(); @Override char charAt(final int index); @Override CharSequence subSequence(final int start, final int end); boolean startsWith(final CIString value); boolean startsWith(final String value); boolean contains(final CIString value); boolean contains(final String value); boolean endsWith(final CIString value); boolean endsWith(final String value); CIString append(final CIString other); }### Answer: @Test public void startsWith() { assertThat(ciString("").startsWith(ciString("")), is(true)); assertThat(ciString("ab").startsWith(ciString("AB")), is(true)); assertThat(ciString("ab").startsWith(ciString("ab")), is(true)); assertThat(ciString("abcdef").startsWith(ciString("AB")), is(true)); assertThat(ciString("ABCDEF").startsWith(ciString("aB")), is(true)); assertThat(ciString("ABCDEF").startsWith(ciString("def")), is(false)); assertThat(ciString("").startsWith(""), is(true)); assertThat(ciString("ab").startsWith("AB"), is(true)); assertThat(ciString("ab").startsWith("ab"), is(true)); assertThat(ciString("abcdef").startsWith("AB"), is(true)); assertThat(ciString("ABCDEF").startsWith("aB"), is(true)); assertThat(ciString("ABCDEF").startsWith("def"), is(false)); }
### Question: CIString implements Comparable<CIString>, CharSequence { public boolean contains(final CIString value) { return lcValue.contains(value.lcValue); } private CIString(final String value); @Nullable static CIString ciString(final String value); static Set<CIString> ciSet(final String... values); static Set<CIString> ciImmutableSet(final String... values); static Set<CIString> ciSet(final Iterable<String> values); static Set<CIString> ciImmutableSet(final Iterable<String> values); static boolean isBlank(final CIString ciString); @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final CIString o); @Override @Nonnull String toString(); String toLowerCase(); String toUpperCase(); int toInt(); @Override int length(); @Override char charAt(final int index); @Override CharSequence subSequence(final int start, final int end); boolean startsWith(final CIString value); boolean startsWith(final String value); boolean contains(final CIString value); boolean contains(final String value); boolean endsWith(final CIString value); boolean endsWith(final String value); CIString append(final CIString other); }### Answer: @Test public void contains() { assertThat(ciString("ABCDEF").contains(ciString("abcdef")), is(true)); assertThat(ciString("ABCDEF").contains(ciString("cd")), is(true)); assertThat(ciString("ABCDEF").contains(ciString("CD")), is(true)); assertThat(ciString("ABCDEF").contains("abcdef"), is(true)); assertThat(ciString("ABCDEF").contains("cd"), is(true)); assertThat(ciString("ABCDEF").contains("CD"), is(true)); }
### Question: CIString implements Comparable<CIString>, CharSequence { public boolean endsWith(final CIString value) { return lcValue.endsWith(value.lcValue); } private CIString(final String value); @Nullable static CIString ciString(final String value); static Set<CIString> ciSet(final String... values); static Set<CIString> ciImmutableSet(final String... values); static Set<CIString> ciSet(final Iterable<String> values); static Set<CIString> ciImmutableSet(final Iterable<String> values); static boolean isBlank(final CIString ciString); @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final CIString o); @Override @Nonnull String toString(); String toLowerCase(); String toUpperCase(); int toInt(); @Override int length(); @Override char charAt(final int index); @Override CharSequence subSequence(final int start, final int end); boolean startsWith(final CIString value); boolean startsWith(final String value); boolean contains(final CIString value); boolean contains(final String value); boolean endsWith(final CIString value); boolean endsWith(final String value); CIString append(final CIString other); }### Answer: @Test public void endsWith() { assertThat(ciString("ABCDEF").endsWith(ciString("def")), is(true)); assertThat(ciString("ABCDEF").endsWith(ciString("DEF")), is(true)); assertThat(ciString("ABCDEF").endsWith(ciString("ABC")), is(false)); assertThat(ciString("ABCDEF").endsWith("def"), is(true)); assertThat(ciString("ABCDEF").endsWith("DEF"), is(true)); assertThat(ciString("ABCDEF").endsWith("ABC"), is(false)); }
### Question: CIString implements Comparable<CIString>, CharSequence { public CIString append(final CIString other) { return ciString(value + other.value); } private CIString(final String value); @Nullable static CIString ciString(final String value); static Set<CIString> ciSet(final String... values); static Set<CIString> ciImmutableSet(final String... values); static Set<CIString> ciSet(final Iterable<String> values); static Set<CIString> ciImmutableSet(final Iterable<String> values); static boolean isBlank(final CIString ciString); @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final CIString o); @Override @Nonnull String toString(); String toLowerCase(); String toUpperCase(); int toInt(); @Override int length(); @Override char charAt(final int index); @Override CharSequence subSequence(final int start, final int end); boolean startsWith(final CIString value); boolean startsWith(final String value); boolean contains(final CIString value); boolean contains(final String value); boolean endsWith(final CIString value); boolean endsWith(final String value); CIString append(final CIString other); }### Answer: @Test public void append() { assertThat(ciString("").append(ciString("")), is(ciString(""))); assertThat(ciString("a").append(ciString("b")), is(ciString("ab"))); assertThat(ciString("a").append(ciString("b")), is(ciString("AB"))); }
### Question: CIString implements Comparable<CIString>, CharSequence { public int toInt() { return Integer.parseInt(value); } private CIString(final String value); @Nullable static CIString ciString(final String value); static Set<CIString> ciSet(final String... values); static Set<CIString> ciImmutableSet(final String... values); static Set<CIString> ciSet(final Iterable<String> values); static Set<CIString> ciImmutableSet(final Iterable<String> values); static boolean isBlank(final CIString ciString); @Override boolean equals(@Nullable final Object o); @Override int hashCode(); @Override int compareTo(@Nonnull final CIString o); @Override @Nonnull String toString(); String toLowerCase(); String toUpperCase(); int toInt(); @Override int length(); @Override char charAt(final int index); @Override CharSequence subSequence(final int start, final int end); boolean startsWith(final CIString value); boolean startsWith(final String value); boolean contains(final CIString value); boolean contains(final String value); boolean endsWith(final CIString value); boolean endsWith(final String value); CIString append(final CIString other); }### Answer: @Test public void toInt() { assertThat(ciString("1").toInt(), is(1)); assertThat(ciString("312").toInt(), is(312)); }
### Question: QueryMessages { public static Message termsAndConditions() { return new Message(Type.INFO, "" + "% This is the RIPE Database query service.\n" + "% The objects are in RPSL format.\n" + "%\n" + "% The RIPE Database is subject to Terms and Conditions.\n" + "% See http: } private QueryMessages(); static Message termsAndConditions(); static Message servedByNotice(final CharSequence version); static Message termsAndConditionsDump(); static Message relatedTo(final CharSequence key); static Message noPersonal(); static Message abuseCShown(final CharSequence key, final CharSequence value); static Message unvalidatedAbuseCShown(final CharSequence key, final CharSequence value, final CharSequence orgId); static Message unvalidatedAbuseCShown(final CharSequence key, final CharSequence value); static Message abuseCNotRegistered(final CharSequence key); static Message outputFilterNotice(); static Message primaryKeysOnlyNotice(); static Message versionListStart(final CharSequence type, final CharSequence key); static Message versionInformation(final int version, final boolean isCurrentVersion, final CIString key, final String operation, final String timestamp); static Message versionDifferenceHeader(final int earlierVersion, final int laterVersion, final CIString key); static Message versionDeleted(final CharSequence deletionTime); static Message versionPersonRole(final CharSequence type, final CharSequence key); static Message internalErroroccurred(); static Message noResults(final CharSequence source); static Message unknownSource(final CharSequence source); static Message invalidObjectType(final CharSequence type); static Message invalidAttributeType(final CharSequence type); static Message attributeNotSearchable(final CharSequence type); static Message noSearchKeySpecified(); static Message inputTooLong(); static Message invalidCombinationOfFlags(final CharSequence flag, final CharSequence otherFlag); static Message invalidMultipleFlags(final CharSequence flag); static Message malformedQuery(); static Message malformedQuery(final String reason); static Message illegalRange(); static Message unsupportedQuery(); static Message invalidSearchKey(); static Message unsupportedVersionObjectType(); static Message versionOutOfRange(final int max); static Message tooManyArguments(); static Message accessDeniedPermanently(final InetAddress remoteAddress); static Message accessDeniedTemporarily(final InetAddress remoteAddress); static Message notAllowedToProxy(); static Message timeout(); static Message connectionsExceeded(final int connectionLimit); static Message duplicateIpFlagsPassed(); static Message uselessIpFlagPassed(); static Message tagInfoStart(final CharSequence pkey); static Message tagInfo(final CharSequence tagType, final CharSequence tagValue); static Message unreferencedTagInfo(final CharSequence pkey, final CharSequence value); static Message filterTagNote(final Set<? extends CharSequence> includeArgs, final Set<? extends CharSequence> excludeArgs); static Message invalidSyntax(final CharSequence objectKey); static Message validSyntax(final CharSequence objectKey); static Message inverseSearchNotAllowed(); static Message valueChangedDueToLatin1Conversion(); }### Answer: @Test public void headerShouldContainLinkToTermsAndConditions() { assertThat(QueryMessages.termsAndConditions().toString(), containsString("http: }
### Question: QueryMessages { public static Message duplicateIpFlagsPassed() { return new QueryMessage(Type.ERROR, "" + "ERROR:901: duplicate IP flags passed\n" + "\n" + "More than one IP flag (-l, -L, -m, -M or -x) passed to the server."); } private QueryMessages(); static Message termsAndConditions(); static Message servedByNotice(final CharSequence version); static Message termsAndConditionsDump(); static Message relatedTo(final CharSequence key); static Message noPersonal(); static Message abuseCShown(final CharSequence key, final CharSequence value); static Message unvalidatedAbuseCShown(final CharSequence key, final CharSequence value, final CharSequence orgId); static Message unvalidatedAbuseCShown(final CharSequence key, final CharSequence value); static Message abuseCNotRegistered(final CharSequence key); static Message outputFilterNotice(); static Message primaryKeysOnlyNotice(); static Message versionListStart(final CharSequence type, final CharSequence key); static Message versionInformation(final int version, final boolean isCurrentVersion, final CIString key, final String operation, final String timestamp); static Message versionDifferenceHeader(final int earlierVersion, final int laterVersion, final CIString key); static Message versionDeleted(final CharSequence deletionTime); static Message versionPersonRole(final CharSequence type, final CharSequence key); static Message internalErroroccurred(); static Message noResults(final CharSequence source); static Message unknownSource(final CharSequence source); static Message invalidObjectType(final CharSequence type); static Message invalidAttributeType(final CharSequence type); static Message attributeNotSearchable(final CharSequence type); static Message noSearchKeySpecified(); static Message inputTooLong(); static Message invalidCombinationOfFlags(final CharSequence flag, final CharSequence otherFlag); static Message invalidMultipleFlags(final CharSequence flag); static Message malformedQuery(); static Message malformedQuery(final String reason); static Message illegalRange(); static Message unsupportedQuery(); static Message invalidSearchKey(); static Message unsupportedVersionObjectType(); static Message versionOutOfRange(final int max); static Message tooManyArguments(); static Message accessDeniedPermanently(final InetAddress remoteAddress); static Message accessDeniedTemporarily(final InetAddress remoteAddress); static Message notAllowedToProxy(); static Message timeout(); static Message connectionsExceeded(final int connectionLimit); static Message duplicateIpFlagsPassed(); static Message uselessIpFlagPassed(); static Message tagInfoStart(final CharSequence pkey); static Message tagInfo(final CharSequence tagType, final CharSequence tagValue); static Message unreferencedTagInfo(final CharSequence pkey, final CharSequence value); static Message filterTagNote(final Set<? extends CharSequence> includeArgs, final Set<? extends CharSequence> excludeArgs); static Message invalidSyntax(final CharSequence objectKey); static Message validSyntax(final CharSequence objectKey); static Message inverseSearchNotAllowed(); static Message valueChangedDueToLatin1Conversion(); }### Answer: @Test public void duplicateIpFlagsPassedShouldContainError() { assertThat(QueryMessages.duplicateIpFlagsPassed().toString(), containsString("%ERROR:901:")); }
### Question: QueryMessages { public static Message abuseCShown(final CharSequence key, final CharSequence value) { return new QueryMessage(Type.INFO, "Abuse contact for '%s' is '%s'", key, value); } private QueryMessages(); static Message termsAndConditions(); static Message servedByNotice(final CharSequence version); static Message termsAndConditionsDump(); static Message relatedTo(final CharSequence key); static Message noPersonal(); static Message abuseCShown(final CharSequence key, final CharSequence value); static Message unvalidatedAbuseCShown(final CharSequence key, final CharSequence value, final CharSequence orgId); static Message unvalidatedAbuseCShown(final CharSequence key, final CharSequence value); static Message abuseCNotRegistered(final CharSequence key); static Message outputFilterNotice(); static Message primaryKeysOnlyNotice(); static Message versionListStart(final CharSequence type, final CharSequence key); static Message versionInformation(final int version, final boolean isCurrentVersion, final CIString key, final String operation, final String timestamp); static Message versionDifferenceHeader(final int earlierVersion, final int laterVersion, final CIString key); static Message versionDeleted(final CharSequence deletionTime); static Message versionPersonRole(final CharSequence type, final CharSequence key); static Message internalErroroccurred(); static Message noResults(final CharSequence source); static Message unknownSource(final CharSequence source); static Message invalidObjectType(final CharSequence type); static Message invalidAttributeType(final CharSequence type); static Message attributeNotSearchable(final CharSequence type); static Message noSearchKeySpecified(); static Message inputTooLong(); static Message invalidCombinationOfFlags(final CharSequence flag, final CharSequence otherFlag); static Message invalidMultipleFlags(final CharSequence flag); static Message malformedQuery(); static Message malformedQuery(final String reason); static Message illegalRange(); static Message unsupportedQuery(); static Message invalidSearchKey(); static Message unsupportedVersionObjectType(); static Message versionOutOfRange(final int max); static Message tooManyArguments(); static Message accessDeniedPermanently(final InetAddress remoteAddress); static Message accessDeniedTemporarily(final InetAddress remoteAddress); static Message notAllowedToProxy(); static Message timeout(); static Message connectionsExceeded(final int connectionLimit); static Message duplicateIpFlagsPassed(); static Message uselessIpFlagPassed(); static Message tagInfoStart(final CharSequence pkey); static Message tagInfo(final CharSequence tagType, final CharSequence tagValue); static Message unreferencedTagInfo(final CharSequence pkey, final CharSequence value); static Message filterTagNote(final Set<? extends CharSequence> includeArgs, final Set<? extends CharSequence> excludeArgs); static Message invalidSyntax(final CharSequence objectKey); static Message validSyntax(final CharSequence objectKey); static Message inverseSearchNotAllowed(); static Message valueChangedDueToLatin1Conversion(); }### Answer: @Test public void restApiExpectsAbuseContactsInSpecificFormat() { assertThat(QueryMessages.abuseCShown("193.0.0.0 - 193.0.7.255", "abuse@ripe.net").toString(), is("% Abuse contact for '193.0.0.0 - 193.0.7.255' is 'abuse@ripe.net'\n")); }