ImportedFieldsEnumerator { public void enumerateImportedFields(SDDocumentType documentType) { var search = this.schemas.stream() .filter(s -> s.getDocument() != null) .filter(s -> s.getDocument().getName().equals(documentType.getName())) .findFirst(); if (search.isEmpty()) { return; } search.get().temporaryImportedFields().ifPresent(documentType::setTemporaryImportedFields); } ImportedFieldsEnumerator(List<Search> schemas); void enumerateImportedFields(SDDocumentType documentType);  }
@Test public void imported_fields_are_enumerated_and_copied_from_correct_search_instance() { Search parentSearch = new Search(); SDDocumentType parentDocument = new SDDocumentType("parent", parentSearch); var parentField = new SDField("their_field", DataType.INT); AttributeUtils.addAttributeAspect(parentField); parentDocument.addField(parentField); parentSearch.addDocument(parentDocument); Search fooSearch = new Search(); SDField fooRefToParent = new SDField( "foo_ref", ReferenceDataType.createWithInferredId(parentDocument.getDocumentType())); AttributeUtils.addAttributeAspect(fooRefToParent); var fooImports = fooSearch.temporaryImportedFields().get(); fooImports.add(new TemporaryImportedField("my_first_import", "foo_ref", "their_field")); fooImports.add(new TemporaryImportedField("my_second_import", "foo_ref", "their_field")); SDDocumentType fooDocument = new SDDocumentType("foo", fooSearch); fooSearch.addDocument(fooDocument); Search barSearch = new Search(); SDField barRefToParent = new SDField( "bar_ref", ReferenceDataType.createWithInferredId(parentDocument.getDocumentType())); AttributeUtils.addAttributeAspect(barRefToParent); var barImports = barSearch.temporaryImportedFields().get(); barImports.add(new TemporaryImportedField("my_cool_import", "my_ref", "their_field")); SDDocumentType barDocument = new SDDocumentType("bar", barSearch); barSearch.addDocument(barDocument); var enumerator = new ImportedFieldsEnumerator(List.of(parentSearch, fooSearch, barSearch)); enumerator.enumerateImportedFields(parentDocument); assertImportedFieldsAre(parentDocument, List.of()); enumerator.enumerateImportedFields(fooDocument); assertImportedFieldsAre(fooDocument, List.of("my_first_import", "my_second_import")); enumerator.enumerateImportedFields(barDocument); assertImportedFieldsAre(barDocument, List.of("my_cool_import")); }