@Test
  public void testStoreCodeSystemInvalidCyclicLoop() {
    CodeSystem codeSystem = new CodeSystem();
    codeSystem.setUrl(CS_URL);
    codeSystem.setContent(CodeSystemContentMode.NOTPRESENT);
    IIdType id =
        myCodeSystemDao.create(codeSystem, new ServletRequestDetails()).getId().toUnqualified();

    ResourceTable table = myResourceTableDao.findOne(id.getIdPartAsLong());

    TermCodeSystemVersion cs = new TermCodeSystemVersion();
    cs.setResource(table);
    cs.setResourceVersionId(table.getVersion());

    TermConcept parent = new TermConcept();
    parent.setCodeSystem(cs);
    parent.setCode("parent");
    cs.getConcepts().add(parent);

    TermConcept child = new TermConcept();
    child.setCodeSystem(cs);
    child.setCode("child");
    parent.addChild(child, RelationshipTypeEnum.ISA);

    child.addChild(parent, RelationshipTypeEnum.ISA);

    try {
      myTermSvc.storeNewCodeSystemVersion(table.getId(), "http://foo", cs);
      fail();
    } catch (InvalidRequestException e) {
      assertEquals("CodeSystem contains circular reference around code parent", e.getMessage());
    }
  }
  private IIdType createCodeSystem() {
    CodeSystem codeSystem = new CodeSystem();
    codeSystem.setUrl(CS_URL);
    codeSystem.setContent(CodeSystemContentMode.NOTPRESENT);
    IIdType id =
        myCodeSystemDao.create(codeSystem, new ServletRequestDetails()).getId().toUnqualified();

    ResourceTable table = myResourceTableDao.findOne(id.getIdPartAsLong());

    TermCodeSystemVersion cs = new TermCodeSystemVersion();
    cs.setResource(table);
    cs.setResourceVersionId(table.getVersion());

    TermConcept parentA = new TermConcept(cs, "ParentA");
    cs.getConcepts().add(parentA);

    TermConcept childAA = new TermConcept(cs, "childAA");
    parentA.addChild(childAA, RelationshipTypeEnum.ISA);

    TermConcept childAAA = new TermConcept(cs, "childAAA");
    childAA.addChild(childAAA, RelationshipTypeEnum.ISA);

    TermConcept childAAB = new TermConcept(cs, "childAAB");
    childAA.addChild(childAAB, RelationshipTypeEnum.ISA);

    TermConcept childAB = new TermConcept(cs, "childAB");
    parentA.addChild(childAB, RelationshipTypeEnum.ISA);

    TermConcept parentB = new TermConcept(cs, "ParentB");
    cs.getConcepts().add(parentB);

    myTermSvc.storeNewCodeSystemVersion(table.getId(), "http://foo", cs);
    return id;
  }
  @Test
  public void testCreateDuplicateCodeSystemUri() {
    CodeSystem codeSystem = new CodeSystem();
    codeSystem.setUrl(CS_URL);
    codeSystem.setContent(CodeSystemContentMode.NOTPRESENT);
    IIdType id =
        myCodeSystemDao.create(codeSystem, new ServletRequestDetails()).getId().toUnqualified();

    ResourceTable table = myResourceTableDao.findOne(id.getIdPartAsLong());

    TermCodeSystemVersion cs = new TermCodeSystemVersion();
    cs.setResource(table);
    cs.setResourceVersionId(table.getVersion());

    myTermSvc.storeNewCodeSystemVersion(table.getId(), CS_URL, cs);

    // Update
    cs = new TermCodeSystemVersion();
    TermConcept parentA = new TermConcept(cs, "ParentA");
    cs.getConcepts().add(parentA);
    id = myCodeSystemDao.update(codeSystem, new ServletRequestDetails()).getId().toUnqualified();
    table = myResourceTableDao.findOne(id.getIdPartAsLong());
    cs.setResource(table);
    cs.setResourceVersionId(table.getVersion());
    myTermSvc.storeNewCodeSystemVersion(table.getId(), CS_URL, cs);

    // Try to update to a different resource
    codeSystem = new CodeSystem();
    codeSystem.setUrl(CS_URL);
    codeSystem.setContent(CodeSystemContentMode.NOTPRESENT);
    id = myCodeSystemDao.create(codeSystem, new ServletRequestDetails()).getId().toUnqualified();
    table = myResourceTableDao.findOne(id.getIdPartAsLong());
    cs.setResource(table);
    cs.setResourceVersionId(table.getVersion());
    try {
      myTermSvc.storeNewCodeSystemVersion(table.getId(), CS_URL, cs);
      fail();
    } catch (UnprocessableEntityException e) {
      assertThat(
          e.getMessage(),
          containsString(
              "Can not create multiple code systems with URI \"http://example.com/my_code_system\", already have one with resource ID: CodeSystem/"));
    }
  }