@Test
  public void storeAndValidateContains() throws Exception {
    final ProjectVersionRef r = new SimpleProjectVersionRef("org.test", "root", "1");
    final ProjectVersionRef d = new SimpleProjectVersionRef("org.test", "dep", "1");
    final EProjectDirectRelationships root =
        new EProjectDirectRelationships.Builder(sourceUri, r)
            .withDependency(d, null, null, null, false, false)
            .build();

    graph.storeRelationships(root.getExactAllRelationships());

    assertThat(graph.containsGraph(r), equalTo(true));
  }
  @Test
  public void storeProjectWithParentAndVerifyParentRelationship() throws Exception {
    final ProjectVersionRef project =
        new SimpleProjectVersionRef("org.apache.maven", "maven-core", "3.0.3");
    final ProjectVersionRef parent =
        new SimpleProjectVersionRef("org.apache.maven", "maven", "3.0.3");

    final EProjectDirectRelationships rels =
        new EProjectDirectRelationships.Builder(sourceUri, project).withParent(parent).build();

    graph.storeRelationships(rels.getExactAllRelationships());

    final ProjectVersionRef parentResult = CartoGraphUtils.getParent(project, graph);
    assertThat(parentResult, equalTo(parent));
  }
  @Test
  public void storeProjectWithParentAndRetrieveAsChild() throws Exception {
    final ProjectVersionRef project =
        new SimpleProjectVersionRef("org.apache.maven", "maven-core", "3.0.3");
    final ProjectVersionRef parent =
        new SimpleProjectVersionRef("org.apache.maven", "maven", "3.0.3");

    final EProjectDirectRelationships rels =
        new EProjectDirectRelationships.Builder(sourceUri, project).withParent(parent).build();

    graph.storeRelationships(rels.getExactAllRelationships());

    final Set<ProjectVersionRef> children = CartoGraphUtils.getKnownChildren(parent, graph);

    assertThat(children.size(), equalTo(1));
    assertThat(children.iterator().next(), equalTo(project));
  }
  @Test
  public void storeProjectWithOneExtensionAndVerifyUsageRelationship() throws Exception {
    final ProjectVersionRef project =
        new SimpleProjectVersionRef("org.apache.maven", "maven-core", "3.0.3");
    final ProjectVersionRef ext =
        new SimpleProjectVersionRef("org.apache.maven.wagon", "wagon-provider-webdav", "2.0");

    final EProjectDirectRelationships rels =
        new EProjectDirectRelationships.Builder(sourceUri, project)
            .withExtension(ext, false)
            .build();

    graph.storeRelationships(rels.getExactAllRelationships());

    final Set<ProjectRelationship<?, ?>> exts =
        graph.findDirectRelationshipsFrom(project, false, RelationshipType.EXTENSION);
    assertThat(exts.size(), equalTo(1));
    assertThat(exts.iterator().next().getTarget(), equalTo(ext));
  }
  @Test
  public void storeParentChildDescendantProjectsAndRetrieveAncestry() throws Exception {
    final ProjectVersionRef r = new SimpleProjectVersionRef("org.test", "root", "1");
    final ProjectVersionRef p = new SimpleProjectVersionRef("org.test", "parent", "1.0");
    final ProjectVersionRef c = new SimpleProjectVersionRef("org.test", "child", "1.0");

    final EProjectDirectRelationships root =
        new EProjectDirectRelationships.Builder(sourceUri, r).build();
    final EProjectDirectRelationships parent =
        new EProjectDirectRelationships.Builder(sourceUri, p).withParent(r).build();
    final EProjectDirectRelationships child =
        new EProjectDirectRelationships.Builder(sourceUri, c).withParent(p).build();

    graph.storeRelationships(root.getExactAllRelationships());
    graph.storeRelationships(parent.getExactAllRelationships());
    graph.storeRelationships(child.getExactAllRelationships());

    final List<ProjectVersionRef> ancestry = CartoGraphUtils.getAncestry(c, graph);
    assertThat(ancestry, notNullValue());
    assertThat(ancestry.size(), equalTo(3));

    final Iterator<ProjectVersionRef> iterator = ancestry.iterator();
    assertThat(iterator.next(), equalTo(c));
    assertThat(iterator.next(), equalTo(p));
    assertThat(iterator.next(), equalTo(r));
  }
  @Test
  public void storeProjectWithOnePluginAndVerifyAnyUsageRelationship() throws Exception {
    final ProjectVersionRef project =
        new SimpleProjectVersionRef("org.apache.maven", "maven-core", "3.0.3");
    final ProjectVersionRef plugin =
        new SimpleProjectVersionRef("org.apache.maven.plugins", "maven-compiler-plugin", "2.3.2");

    final EProjectDirectRelationships rels =
        new EProjectDirectRelationships.Builder(sourceUri, project)
            .withPlugin(plugin, false, false)
            .build();

    graph.storeRelationships(rels.getExactAllRelationships());

    final Set<ProjectRelationship<?, ?>> storedRels =
        graph.findDirectRelationshipsTo(plugin, false, RelationshipType.PLUGIN);

    assertThat(storedRels.size(), equalTo(1));

    final ProjectVersionRef declaring = storedRels.iterator().next().getDeclaring();

    assertThat(declaring, equalTo(project));
  }
  @Test
  public void storeProjectWithOneDependencyAndVerifyDependencyRelationship() throws Exception {
    final ProjectVersionRef p =
        new SimpleProjectVersionRef("org.apache.maven", "maven-core", "3.0.3");
    final ProjectVersionRef d =
        new SimpleProjectVersionRef("org.apache.maven", "maven-artifact", "3.0.3");

    final EProjectDirectRelationships rels =
        new EProjectDirectRelationships.Builder(sourceUri, p)
            .withDependency(d, null, null, null, false, false)
            .build();

    graph.storeRelationships(rels.getExactAllRelationships());

    final Set<ProjectRelationship<?, ?>> storedRels =
        graph.findDirectRelationshipsFrom(p, false, RelationshipType.DEPENDENCY);

    assertThat(storedRels.size(), equalTo(1));

    final ArtifactRef target = (ArtifactRef) storedRels.iterator().next().getTarget();

    assertThat(target.asProjectVersionRef(), equalTo(d));
  }
  @Test
  public void storeProjectWithOneDependencyAndRetrieveAsDependent() throws Exception {
    final EProjectDirectRelationships rels =
        new EProjectDirectRelationships.Builder(
                sourceUri, new SimpleProjectVersionRef("org.apache.maven", "maven-core", "3.0.3"))
            .withDependency(
                new SimpleProjectVersionRef("org.apache.maven", "maven-artifact", "3.0.3"),
                null,
                null,
                null,
                false,
                false)
            .build();

    graph.storeRelationships(rels.getExactAllRelationships());

    final Set<ProjectRelationship<?, ?>> dependents =
        graph.findDirectRelationshipsTo(
            rels.getDependencies().get(0).getTarget(), false, RelationshipType.DEPENDENCY);

    assertThat(dependents, notNullValue());
    assertThat(dependents.size(), equalTo(1));
    assertThat(dependents.iterator().next().getDeclaring(), equalTo(rels.getProjectRef()));
  }
  @Test
  public void storeProjectAndRetrieveDependentProjectForEach() throws Exception {
    //        graph.reindex();
    //
    final ProjectVersionRef p =
        new SimpleProjectVersionRef("org.apache.maven", "maven-core", "3.0.3");

    final EProjectDirectRelationships.Builder prb =
        new EProjectDirectRelationships.Builder(sourceUri, p);

    final ProjectVersionRef parent =
        new SimpleProjectVersionRef("org.apache.maven", "maven", "3.0.3");

    int idx = 0;
    int pidx = 0;
    final DependencyRelationship papi =
        new SimpleDependencyRelationship(
            sourceUri,
            p,
            new SimpleArtifactRef(
                "org.apache.maven", "maven-plugin-api", "3.0.3", null, null, false),
            DependencyScope.compile,
            idx++,
            false,
            false);
    final DependencyRelationship art =
        new SimpleDependencyRelationship(
            sourceUri,
            p,
            new SimpleArtifactRef("org.apache.maven", "maven-artifact", "3.0.3", null, null, false),
            DependencyScope.compile,
            idx++,
            false,
            false);
    final PluginRelationship jarp =
        new SimplePluginRelationship(
            sourceUri,
            p,
            new SimpleProjectVersionRef("org.apache.maven.plugins", "maven-jar-plugin", "2.2"),
            pidx++,
            false,
            false);
    final PluginRelationship comp =
        new SimplePluginRelationship(
            sourceUri,
            p,
            new SimpleProjectVersionRef(
                "org.apache.maven.plugins", "maven-compiler-plugin", "2.3.2"),
            pidx++,
            false,
            false);
    final ExtensionRelationship wag =
        new SimpleExtensionRelationship(
            sourceUri,
            p,
            new SimpleProjectVersionRef("org.apache.maven.wagon", "wagon-provider-webdav", "1.0"),
            0,
            false);

    prb.withParent(parent);
    prb.withDependencies(papi);
    prb.withDependencies(art);
    prb.withPlugins(jarp);
    prb.withPlugins(comp);
    prb.withExtensions(wag);

    final EProjectDirectRelationships rels = prb.build();

    final Set<ProjectRelationship<?, ?>> rejected =
        graph.storeRelationships(rels.getExactAllRelationships());
    System.out.println("Rejects: " + rejected);

    final Map<ProjectVersionRef, Set<ProjectRelationship<?, ?>>> byTarget =
        new HashMap<ProjectVersionRef, Set<ProjectRelationship<?, ?>>>() {
          private static final long serialVersionUID = 1L;

          {
            put(parent, graph.findDirectRelationshipsTo(parent, true, RelationshipType.values()));
            put(
                papi.getTarget(),
                graph.findDirectRelationshipsTo(papi.getTarget(), true, RelationshipType.values()));
            put(
                art.getTarget(),
                graph.findDirectRelationshipsTo(art.getTarget(), true, RelationshipType.values()));
            put(
                jarp.getTarget(),
                graph.findDirectRelationshipsTo(jarp.getTarget(), true, RelationshipType.values()));
            put(
                comp.getTarget(),
                graph.findDirectRelationshipsTo(comp.getTarget(), true, RelationshipType.values()));
            put(
                wag.getTarget(),
                graph.findDirectRelationshipsTo(wag.getTarget(), true, RelationshipType.values()));
          }
        };

    for (final Map.Entry<ProjectVersionRef, Set<ProjectRelationship<?, ?>>> entry :
        byTarget.entrySet()) {
      System.out.printf(
          "\n\n\nFor key: %s, dependencies:\n  %s\n\n\n",
          entry.getKey(), formatWithClassname(entry.getValue(), "\n  "));

      assertThat(
          "Null dependents set for: " + entry.getKey() + "!", entry.getValue(), notNullValue());

      assertThat(
          "Should have exactly one matching dependent of: " + entry.getKey(),
          entry.getValue().size(),
          equalTo(1));
      assertThat(
          "Invalid dependent of: " + entry.getKey(),
          entry.getValue().iterator().next().getDeclaring(),
          equalTo(rels.getProjectRef()));
    }
  }
  @Test
  public void storeProjectAndRetrieveAllRelationshipsInOneGo() throws Exception {
    final ProjectVersionRef p =
        new SimpleProjectVersionRef("org.apache.maven", "maven-core", "3.0.3");

    final EProjectDirectRelationships.Builder prb =
        new EProjectDirectRelationships.Builder(sourceUri, p);

    final ProjectVersionRef parent =
        new SimpleProjectVersionRef("org.apache.maven", "maven", "3.0.3");

    int idx = 0;
    int pidx = 0;
    final DependencyRelationship papi =
        new SimpleDependencyRelationship(
            sourceUri,
            p,
            new SimpleArtifactRef(
                "org.apache.maven", "maven-plugin-api", "3.0.3", null, null, false),
            DependencyScope.compile,
            idx++,
            false,
            false);
    final DependencyRelationship art =
        new SimpleDependencyRelationship(
            sourceUri,
            p,
            new SimpleArtifactRef("org.apache.maven", "maven-artifact", "3.0.3", null, null, false),
            DependencyScope.compile,
            idx++,
            false,
            false);
    final PluginRelationship jarp =
        new SimplePluginRelationship(
            sourceUri,
            p,
            new SimpleProjectVersionRef("org.apache.maven.plugins", "maven-jar-plugin", "2.2"),
            pidx++,
            false,
            false);
    final PluginRelationship comp =
        new SimplePluginRelationship(
            sourceUri,
            p,
            new SimpleProjectVersionRef(
                "org.apache.maven.plugins", "maven-compiler-plugin", "2.3.2"),
            pidx++,
            false,
            false);
    final ExtensionRelationship wag =
        new SimpleExtensionRelationship(
            sourceUri,
            p,
            new SimpleProjectVersionRef("org.apache.maven.wagon", "wagon-provider-webdav", "1.0"),
            0,
            false);

    prb.withParent(parent);
    prb.withDependencies(papi, art);
    prb.withPlugins(jarp, comp);
    prb.withExtensions(wag);

    final EProjectDirectRelationships rels = prb.build();

    assertThat(rels.getAllRelationships().size(), equalTo(6));

    graph.storeRelationships(rels.getExactAllRelationships());

    final Set<ProjectRelationship<?, ?>> resulting =
        graph.findDirectRelationshipsFrom(rels.getProjectRef(), false, RelationshipType.values());

    final Set<ProjectVersionRef> targets = RelationshipUtils.targets(resulting);

    assertThat(targets.size(), equalTo(6));
    assertThat(targets.contains(parent), equalTo(true));
    assertThat(targets.contains(papi.getTarget()), equalTo(true));
    assertThat(targets.contains(art.getTarget()), equalTo(true));
    assertThat(targets.contains(jarp.getTarget()), equalTo(true));
    assertThat(targets.contains(comp.getTarget()), equalTo(true));
    assertThat(targets.contains(wag.getTarget()), equalTo(true));
  }