### Question:
SchemaObjectSorter { public static Iterable<AttributeType> hierarchicalOrdered( List<AttributeType> attributeTypes ) { return new SchemaObjectIterable<>( attributeTypes, new ReferenceCallback<AttributeType>() { @Override public Collection<String> getSuperiorOids( AttributeType at ) { return Collections.singleton( at.getSuperiorOid() ); } } ); } private  SchemaObjectSorter(); static Iterable<AttributeType> hierarchicalOrdered( List<AttributeType> attributeTypes ); static Iterable<ObjectClass> sortObjectClasses( List<ObjectClass> objectClasses );  }### Answer:
@Test public void testSortAttributeTypesAlreadySorted() { List<AttributeType> attributeTypes = new ArrayList<AttributeType>(); addAttributeType( attributeTypes, "1.1.1", "att1", null ); addAttributeType( attributeTypes, "1.1.2", "att2", "att1" ); addAttributeType( attributeTypes, "1.1.3", "att3", "att2" ); addAttributeType( attributeTypes, "1.1.4", "att4", "att3" ); addAttributeType( attributeTypes, "1.1.5", "att5", "att1" ); addAttributeType( attributeTypes, "1.1.6", "att6", null ); addAttributeType( attributeTypes, "1.1.7", "att7", "other" ); Iterable<AttributeType> sorted = SchemaObjectSorter.hierarchicalOrdered( attributeTypes ); assertHierarchicalOrderAT( sorted ); }
@Test public void testSortAttributeTypesShuffled() { List<String> oids = Arrays.asList( "1.1.1", "1.1.2", "1.1.3", "1.1.4", "1.1.5", "1.1.6", "1.1.7" ); for ( int i = 0; i < 1000; i++ ) { Collections.shuffle( oids ); Iterator<String> oidIterator = oids.iterator(); List<AttributeType> attributeTypes = new ArrayList<AttributeType>(); addAttributeType( attributeTypes, oidIterator.next(), "att1", null ); addAttributeType( attributeTypes, oidIterator.next(), "aTT2", "att1" ); addAttributeType( attributeTypes, oidIterator.next(), "att3", "att2" ); addAttributeType( attributeTypes, oidIterator.next(), "att4", "atT3" ); addAttributeType( attributeTypes, oidIterator.next(), "att5", "aTt1" ); addAttributeType( attributeTypes, oidIterator.next(), "att6", null ); addAttributeType( attributeTypes, oidIterator.next(), "att7", "other" ); Iterable<AttributeType> sorted = SchemaObjectSorter.hierarchicalOrdered( attributeTypes ); assertHierarchicalOrderAT( sorted ); } }
@Test public void testSortAttributeTypesLoop() { List<AttributeType> attributeTypes = new ArrayList<AttributeType>(); addAttributeType( attributeTypes, "1.1.1", "att1", "att4" ); addAttributeType( attributeTypes, "1.1.2", "att2", "att1" ); addAttributeType( attributeTypes, "1.1.3", "att3", "att2" ); addAttributeType( attributeTypes, "1.1.4", "att4", "att3" ); Iterable<AttributeType> sorted = SchemaObjectSorter.hierarchicalOrdered( attributeTypes ); assertThrows( IllegalStateException.class, () -> { sorted.iterator().next(); } ); }