Search in sources :

Example 11 with User

use of org.neo4j.ogm.domain.social.User in project neo4j-ogm by neo4j.

the class SocialIntegrationTest method shouldFetchFriendsUndirected.

@Test
public void shouldFetchFriendsUndirected() {
    User adam = new User("Adam");
    User daniela = new User("Daniela");
    User michal = new User("Michal");
    User vince = new User("Vince");
    adam.befriend(daniela);
    daniela.befriend(michal);
    michal.befriend(vince);
    session.save(adam);
    session.clear();
    adam = session.load(User.class, adam.getId());
    assertThat(adam.getFriends()).hasSize(1);
    daniela = session.load(User.class, daniela.getId());
    assertThat(daniela.getFriends()).hasSize(2);
    List<String> friendNames = new ArrayList<>();
    for (User friend : daniela.getFriends()) {
        friendNames.add(friend.getName());
    }
    assertThat(friendNames.contains("Adam")).isTrue();
    assertThat(friendNames.contains("Michal")).isTrue();
    session.clear();
    michal = session.load(User.class, michal.getId());
    assertThat(michal.getFriends()).hasSize(2);
    session.clear();
    vince = session.load(User.class, vince.getId());
    assertThat(vince.getFriends()).hasSize(1);
}
Also used : User(org.neo4j.ogm.domain.social.User) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 12 with User

use of org.neo4j.ogm.domain.social.User in project neo4j-ogm by neo4j.

the class SocialIntegrationTest method shouldFetchFriendsForUndirectedRelationship.

/**
 * @see DATAGRAPH-594
 */
@Test
public void shouldFetchFriendsForUndirectedRelationship() {
    session.query("create (p1:User {name:'A'}) create (p2:User {name:'B'}) create (p3:User {name:'C'})" + " create (p4:User {name:'D'}) create (p1)-[:FRIEND]->(p2) create (p1)-[:FRIEND]->(p3) create (p4)-[:FRIEND]->(p1)", Collections.EMPTY_MAP);
    User userA = session.loadAll(User.class, new Filter("name", ComparisonOperator.EQUALS, "A")).iterator().next();
    assertThat(userA).isNotNull();
    assertThat(userA.getFriends()).hasSize(3);
    User userB = session.loadAll(User.class, new Filter("name", ComparisonOperator.EQUALS, "B")).iterator().next();
    assertThat(userB).isNotNull();
    assertThat(userB.getFriends()).hasSize(1);
    assertThat(userB.getFriends().get(0)).isEqualTo(userA);
    User userD = session.loadAll(User.class, new Filter("name", ComparisonOperator.EQUALS, "D")).iterator().next();
    assertThat(userD).isNotNull();
    assertThat(userD.getFriends()).hasSize(1);
    assertThat(userD.getFriends().get(0)).isEqualTo(userA);
}
Also used : User(org.neo4j.ogm.domain.social.User) Filter(org.neo4j.ogm.cypher.Filter) Test(org.junit.Test)

Example 13 with User

use of org.neo4j.ogm.domain.social.User in project neo4j-ogm by neo4j.

the class SocialIntegrationTest method shouldSaveUndirectedFriends.

/**
 * @see DATAGRAPH-594
 */
@Test
public void shouldSaveUndirectedFriends() {
    User userA = new User("A");
    User userB = new User("B");
    User userC = new User("C");
    User userD = new User("D");
    userA.getFriends().add(userB);
    userA.getFriends().add(userC);
    userD.getFriends().add(userA);
    session.save(userA);
    session.save(userB);
    session.save(userC);
    session.save(userD);
    session.clear();
    userA = session.loadAll(User.class, new Filter("name", ComparisonOperator.EQUALS, "A")).iterator().next();
    assertThat(userA).isNotNull();
    assertThat(userA.getFriends()).hasSize(3);
    userB = session.loadAll(User.class, new Filter("name", ComparisonOperator.EQUALS, "B")).iterator().next();
    assertThat(userB).isNotNull();
    assertThat(userB.getFriends()).hasSize(1);
    assertThat(userB.getFriends().get(0).getName()).isEqualTo(userA.getName());
    userD = session.loadAll(User.class, new Filter("name", ComparisonOperator.EQUALS, "D")).iterator().next();
    assertThat(userD).isNotNull();
    assertThat(userD.getFriends()).hasSize(1);
    assertThat(userD.getFriends().get(0).getName()).isEqualTo(userA.getName());
}
Also used : User(org.neo4j.ogm.domain.social.User) Filter(org.neo4j.ogm.cypher.Filter) Test(org.junit.Test)

Example 14 with User

use of org.neo4j.ogm.domain.social.User in project neo4j-ogm by neo4j.

the class SocialRelationshipsIntegrationTest method saveUndirectedSavesOutgoingRelationship.

// DATAGRAPH-594
@Test
public void saveUndirectedSavesOutgoingRelationship() {
    User userA = new User("A");
    User userB = new User("B");
    userA.getFriends().add(userB);
    session.save(userA);
    session.clear();
    assertThat(session.query("MATCH (a:User {name:'A'}), (b:User {name:'B'}) WHERE (a)-[:FRIEND]->(b) " + "return a,b", emptyMap()).queryResults()).hasSize(1);
}
Also used : SocialUser(org.neo4j.ogm.domain.social.SocialUser) User(org.neo4j.ogm.domain.social.User) Test(org.junit.Test)

Example 15 with User

use of org.neo4j.ogm.domain.social.User in project neo4j-ogm by neo4j.

the class SocialRelationshipsIntegrationTest method removeUndirectedRelationship.

// GH-112
@Test
public void removeUndirectedRelationship() {
    User userA = new User("A");
    User userB = new User("B");
    userA.getFriends().add(userB);
    session.save(userA);
    assertThat(session.query("MATCH (a:User {name:'A'}), (b:User {name:'B'}) WHERE (a)-[:FRIEND]->(b) return a,b", emptyMap(), true).queryResults()).hasSize(1);
    userA.unfriend(userB);
    session.save(userA);
    assertThat(session.query("MATCH (a:User {name:'A'}), (b:User {name:'B'}) WHERE (a)-[:FRIEND]->(b) return a, b", emptyMap(), true).queryResults()).hasSize(0);
}
Also used : SocialUser(org.neo4j.ogm.domain.social.SocialUser) User(org.neo4j.ogm.domain.social.User) Test(org.junit.Test)

Aggregations

User (org.neo4j.ogm.domain.social.User)25 Test (org.junit.Test)23 Transaction (org.neo4j.ogm.transaction.Transaction)5 Filter (org.neo4j.ogm.cypher.Filter)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 Result (org.neo4j.ogm.model.Result)3 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)3 SocialUser (org.neo4j.ogm.domain.social.SocialUser)2 CypherException (org.neo4j.ogm.exception.CypherException)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Predicate (java.util.function.Predicate)1 Stream (java.util.stream.Stream)1 Assertions (org.assertj.core.api.Assertions)1 Before (org.junit.Before)1 BeforeClass (org.junit.BeforeClass)1 Ignore (org.junit.Ignore)1 WriteProtectionTarget (org.neo4j.ogm.context.WriteProtectionTarget)1