DocumentGraphValidator { public void validateDocumentGraph(List<SDDocumentType> documents) { for (SDDocumentType document : documents) { validateRoot(document); } }  void validateDocumentGraph(List<SDDocumentType> documents);  }
@Test public void simple_ref_dag_is_allowed() { Search advertiserSearch = createSearchWithName("advertiser"); Search campaignSearch = createSearchWithName("campaign"); Search adSearch = createSearchWithName("ad"); createDocumentReference(adSearch, advertiserSearch, "advertiser_ref"); createDocumentReference(adSearch, campaignSearch, "campaign_ref"); DocumentGraphValidator validator = new DocumentGraphValidator(); validator.validateDocumentGraph(documentListOf(advertiserSearch, campaignSearch, adSearch)); }
@Test public void simple_inheritance_dag_is_allowed() { Search grandfather = createSearchWithName("grandfather"); Search father = createSearchWithName("father", grandfather); Search son = createSearchWithName("son", father); DocumentGraphValidator validator = new DocumentGraphValidator(); validator.validateDocumentGraph(documentListOf(son, father, grandfather)); }
@Test public void complex_dag_is_allowed() { Search grandfather = createSearchWithName("grandfather"); Search father = createSearchWithName("father", grandfather); Search mother = createSearchWithName("mother", grandfather); createDocumentReference(father, mother, "wife_ref"); Search son = createSearchWithName("son", father, mother); Search daughter = createSearchWithName("daughter", father, mother); createDocumentReference(daughter, son, "brother_ref"); Search randomGuy1 = createSearchWithName("randomguy1"); Search randomGuy2 = createSearchWithName("randomguy2"); createDocumentReference(randomGuy1, mother, "secret_ref"); DocumentGraphValidator validator = new DocumentGraphValidator(); validator.validateDocumentGraph(documentListOf(son, father, grandfather, son, daughter, randomGuy1, randomGuy2)); }
@Test public void ref_cycle_is_forbidden() { Search search1 = createSearchWithName("doc1"); Search search2 = createSearchWithName("doc2"); Search search3 = createSearchWithName("doc3"); createDocumentReference(search1, search2, "ref_2"); createDocumentReference(search2, search3, "ref_3"); createDocumentReference(search3, search1, "ref_1"); DocumentGraphValidator validator = new DocumentGraphValidator(); exceptionRule.expect(DocumentGraphValidator.DocumentGraphException.class); exceptionRule.expectMessage("Document dependency cycle detected: doc1->doc2->doc3->doc1."); validator.validateDocumentGraph(documentListOf(search1, search2, search3)); }
@Test public void inherit_cycle_is_forbidden() { Search search1 = createSearchWithName("doc1"); Search search2 = createSearchWithName("doc2", search1); Search search3 = createSearchWithName("doc3", search2); search1.getDocument().inherit(search3.getDocument()); DocumentGraphValidator validator = new DocumentGraphValidator(); exceptionRule.expect(DocumentGraphValidator.DocumentGraphException.class); exceptionRule.expectMessage("Document dependency cycle detected: doc1->doc3->doc2->doc1."); validator.validateDocumentGraph(documentListOf(search1, search2, search3)); }
@Test public void combined_inherit_and_ref_cycle_is_forbidden() { Search search1 = createSearchWithName("doc1"); Search search2 = createSearchWithName("doc2", search1); Search search3 = createSearchWithName("doc3", search2); createDocumentReference(search1, search3, "ref_1"); DocumentGraphValidator validator = new DocumentGraphValidator(); exceptionRule.expect(DocumentGraphValidator.DocumentGraphException.class); exceptionRule.expectMessage("Document dependency cycle detected: doc1->doc3->doc2->doc1."); validator.validateDocumentGraph(documentListOf(search1, search2, search3)); }
@Test public void self_reference_is_forbidden() { Search adSearch = createSearchWithName("ad"); createDocumentReference(adSearch, adSearch, "ad_ref"); DocumentGraphValidator validator = new DocumentGraphValidator(); exceptionRule.expect(DocumentGraphValidator.DocumentGraphException.class); exceptionRule.expectMessage("Document dependency cycle detected: ad->ad."); validator.validateDocumentGraph(documentListOf(adSearch)); }
@Test public void self_inheritance_forbidden() { Search adSearch = createSearchWithName("ad"); SDDocumentType document = adSearch.getDocument(); document.inherit(document); DocumentGraphValidator validator = new DocumentGraphValidator(); exceptionRule.expect(DocumentGraphValidator.DocumentGraphException.class); exceptionRule.expectMessage("Document dependency cycle detected: ad->ad."); validator.validateDocumentGraph(documentListOf(adSearch)); }