Esempio n. 1
0
  @Test
  public void shouldAllowVoterToChangeHerMind() {

    Candidate a = new Candidate("A");
    Candidate b = new Candidate("B");
    Voter v = new Voter("V");

    v.candidateVotedFor = b;

    session.save(a);
    session.save(v);

    MappingContext context = ((Neo4jSession) session).context();

    assertTrue(
        context.containsRelationship(
            new MappedRelationship(
                v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), Voter.class, Candidate.class)));
    session.clear();

    System.out.println("reloading objects");
    a = session.load(Candidate.class, a.getId());
    v = session.load(Voter.class, v.getId());

    assertEquals(b.getId(), v.candidateVotedFor.getId());

    assertTrue(
        context.containsRelationship(
            new MappedRelationship(
                v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), Voter.class, Candidate.class)));

    v.candidateVotedFor = a;

    session.save(v);

    System.out.println("reloading objects");
    session.clear();
    session.load(Candidate.class, b.getId());
    session.load(Voter.class, v.getId());

    assertEquals(a.getId(), v.candidateVotedFor.getId());

    assertTrue(
        context.containsRelationship(
            new MappedRelationship(
                v.getId(), "CANDIDATE_VOTED_FOR", a.getId(), Voter.class, Candidate.class)));
    assertFalse(
        context.containsRelationship(
            new MappedRelationship(
                v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), Voter.class, Candidate.class)));

    session.clear();
  }
Esempio n. 2
0
  @Test
  public void shouldAllowASelfReferenceToBeSavedFromTheReferredSide() {

    Candidate candidate = new Candidate("Hilary Clinton");
    candidate.candidateVotedFor = candidate;

    session.save(candidate.candidateVotedFor);

    session.clear();

    Long voterId = candidate.candidateVotedFor.getId();

    Voter voter = session.load(Voter.class, voterId);

    assertNotNull(voter.getId());
    assertNotNull(voter.candidateVotedFor.getId());
    assertEquals(voter.getId(), voter.candidateVotedFor.getId());
  }
Esempio n. 3
0
  @Test
  public void shouldAllowACandidateToVoteForHerself() {

    Candidate candidate = new Candidate("Hilary Clinton");
    candidate.candidateVotedFor = candidate;

    session.save(candidate);

    assertNotNull(candidate.getId());
    assertNotNull(candidate.candidateVotedFor.getId());
    assertEquals(candidate.getId(), candidate.candidateVotedFor.getId());

    session.clear();

    Long voterId = candidate.getId();

    Voter voter = session.load(Voter.class, voterId);

    assertNotNull(voter.getId());
    assertNotNull(voter.candidateVotedFor.getId());
    assertEquals(voter.getId(), voter.candidateVotedFor.getId());
  }
  /** @see DATAGRAPH-703 */
  @Test
  public void createPersonAndFriendsInLongTransaction() {
    try (Transaction tx = session.beginTransaction()) {
      assertEquals(Transaction.Status.OPEN, tx.status());
      Person john = new Person("John");
      session.save(john);

      Person bob = new Person("Bob");
      session.save(bob);

      Person bill = new Person("Bill");
      session.save(bill);

      john = session.load(Person.class, john.getId());
      bob = session.load(Person.class, bob.getId());
      john.addFriend(bob);
      session.save(john);

      john = session.load(Person.class, john.getId());
      bill = session.load(Person.class, bill.getId());
      john.addFriend(bill);
      session.save(john);

      session.clear();
      session.load(Person.class, john.getId());
      assertEquals(2, john.getFriends().size());
    }
  }