DocumentReferenceResolver { public void resolveReferences(SDDocumentType documentType) { var references = new DocumentReferences(createFieldToDocumentReferenceMapping(documentType)); documentType.setDocumentReferences(references); } DocumentReferenceResolver(List<Search> schemas); void resolveReferences(SDDocumentType documentType); void resolveInheritedReferences(SDDocumentType documentType);  }
@Test public void reference_from_one_document_to_another_is_resolved() { Search barSearch = new Search(); SDDocumentType barDocument = new SDDocumentType("bar", barSearch); barSearch.addDocument(barDocument); SDField fooRefToBarField = new SDField ("bar_ref", ReferenceDataType.createWithInferredId(barDocument.getDocumentType())); AttributeUtils.addAttributeAspect(fooRefToBarField); SDField irrelevantField = new SDField("irrelevant_stuff", DataType.INT); Search fooSearch = new Search(); SDDocumentType fooDocument = new SDDocumentType("foo", fooSearch); fooDocument.addField(fooRefToBarField); fooDocument.addField(irrelevantField); fooSearch.addDocument(fooDocument); DocumentReferenceResolver resolver = new DocumentReferenceResolver(asList(fooSearch, barSearch)); resolver.resolveReferences(fooDocument); assertTrue(fooDocument.getDocumentReferences().isPresent()); Map<String, DocumentReference> fooReferenceMap = fooDocument.getDocumentReferences().get().referenceMap(); assertEquals(1, fooReferenceMap.size()); assertSame(barSearch, fooReferenceMap.get("bar_ref").targetSearch()); assertSame(fooRefToBarField, fooReferenceMap.get("bar_ref").referenceField()); }
@Test public void throws_user_friendly_exception_if_referenced_document_does_not_exist() { SDField fooRefToBarField = new SDField( "bar_ref", ReferenceDataType.createWithInferredId(TemporaryStructuredDataType.create("bar"))); AttributeUtils.addAttributeAspect(fooRefToBarField); Search fooSearch = new Search(); SDDocumentType fooDocument = new SDDocumentType("foo", fooSearch); fooDocument.addField(fooRefToBarField); fooSearch.addDocument(fooDocument); DocumentReferenceResolver resolver = new DocumentReferenceResolver(singletonList(fooSearch)); exceptionRule.expect(IllegalArgumentException.class); exceptionRule.expectMessage( "Invalid document reference 'bar_ref': Could not find document type 'bar'"); resolver.resolveReferences(fooDocument); }
@Test public void throws_exception_if_reference_is_not_an_attribute() { Search barSearch = new Search(); SDDocumentType barDocument = new SDDocumentType("bar", barSearch); barSearch.addDocument(barDocument); SDField fooRefToBarField = new SDField ("bar_ref", ReferenceDataType.createWithInferredId(barDocument.getDocumentType())); Search fooSearch = new Search(); SDDocumentType fooDocument = new SDDocumentType("foo", fooSearch); fooDocument.addField(fooRefToBarField); fooSearch.addDocument(fooDocument); DocumentReferenceResolver resolver = new DocumentReferenceResolver(asList(fooSearch, barSearch)); exceptionRule.expect(IllegalArgumentException.class); exceptionRule.expectMessage( "The field 'bar_ref' is an invalid document reference. The field must be an attribute."); resolver.resolveReferences(fooDocument); }