Search in sources :

Example 1 with User

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

the class QueryCapabilityTest method shouldBeAbleToMapEntities.

// DATAGRAPH-700
@Test
public void shouldBeAbleToMapEntities() {
    Iterator<Map<String, Object>> results = session.query("MATCH (u:User {name:$name})-[:RATED]->(m) RETURN u as user, m as movie", Collections.singletonMap("name", "Vince")).iterator();
    assertThat(results).isNotNull();
    Map<String, Object> result = results.next();
    assertThat(result).isNotNull();
    User user = (User) result.get("user");
    assertThat(user).isNotNull();
    Movie movie = (Movie) result.get("movie");
    assertThat(movie).isNotNull();
    assertThat(user.getName()).isEqualTo("Vince");
    assertThat(movie.getTitle()).isEqualTo("Top Gear");
    assertThat(results.hasNext()).isFalse();
}
Also used : Movie(org.neo4j.ogm.domain.cineasts.annotated.Movie) ExtendedUser(org.neo4j.ogm.domain.cineasts.annotated.ExtendedUser) User(org.neo4j.ogm.domain.cineasts.annotated.User) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 2 with User

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

the class QueryCapabilityTest method shouldBeAbleToMapVariableDepthRelationshipsWithCompletePaths.

// DATAGRAPH-700
@Test
public void shouldBeAbleToMapVariableDepthRelationshipsWithCompletePaths() {
    Iterator<Map<String, Object>> results = session.query("match (u:User {name:$name}) match (u)-[r*0..1]-(n) return u,r,n", Collections.singletonMap("name", "Vince")).iterator();
    assertThat(results).isNotNull();
    Map<String, Object> result = results.next();
    assertThat(result).isNotNull();
    /*
            Expect at max 4 rows:
             one with (vince)-[:FRIENDS]-(michal)
             one with (vince)-[:RATED]-(top gear)
             one with (vince)-[:EXTENDED_FRIEND]->(extended)
             one with (vince)
         */
    User user = (User) result.get("u");
    assertThat(user).isNotNull();
    assertThat(user.getName()).isEqualTo("Vince");
    boolean foundMichal = checkForMichal(result);
    while (!foundMichal && results.hasNext()) {
        result = results.next();
        assertThat(result).isNotNull();
        foundMichal = checkForMichal(result);
    }
    assertThat(foundMichal).isTrue();
}
Also used : ExtendedUser(org.neo4j.ogm.domain.cineasts.annotated.ExtendedUser) User(org.neo4j.ogm.domain.cineasts.annotated.User) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 3 with User

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

the class QueryCapabilityTest method shouldBeAbleToMapCollectionsOfNodes.

// DATAGRAPH-700
@Test
public void shouldBeAbleToMapCollectionsOfNodes() {
    Iterator<Map<String, Object>> results = session.query("match (u:User {name:$name})-[r:RATED]->(m) return u as user,collect(r), collect(m) as movies", Collections.singletonMap("name", "Michal")).iterator();
    assertThat(results).isNotNull();
    Map<String, Object> result = results.next();
    assertThat(result).isNotNull();
    assertThat(((User) result.get("user")).getName()).isEqualTo("Michal");
    List<Rating> ratings = (List) result.get("collect(r)");
    assertThat(ratings).hasSize(2);
    for (Rating rating : ratings) {
        assertThat(rating.getUser().getName()).isEqualTo("Michal");
    }
    List<Movie> movies = (List) result.get("movies");
    assertThat(movies).hasSize(2);
    for (Movie movie : movies) {
        if (movie.getRatings().iterator().next().getStars() == 3) {
            assertThat(movie.getTitle()).isEqualTo("Top Gear");
        } else {
            assertThat(movie.getTitle()).isEqualTo("Pulp Fiction");
            assertThat(movie.getRatings().iterator().next().getStars()).isEqualTo(5);
        }
    }
    assertThat(results.hasNext()).isFalse();
}
Also used : Movie(org.neo4j.ogm.domain.cineasts.annotated.Movie) ExtendedUser(org.neo4j.ogm.domain.cineasts.annotated.ExtendedUser) User(org.neo4j.ogm.domain.cineasts.annotated.User) Rating(org.neo4j.ogm.domain.cineasts.annotated.Rating) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 4 with User

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

the class QueryCapabilityTest method modifyingQueryShouldBeAbleToMapEntitiesAndReturnStatistics.

// DATAGRAPH-700
@Test
public void modifyingQueryShouldBeAbleToMapEntitiesAndReturnStatistics() {
    Map<String, Object> params = new HashMap<>();
    params.put("name", "Vince");
    params.put("age", 20);
    Result result = session.query("MATCH (u:User {name:$name})-[:RATED]->(m) WITH u,m SET u.age=$age RETURN u as user, m as movie", params);
    Iterator<Map<String, Object>> results = result.queryResults().iterator();
    assertThat(results).isNotNull();
    Map<String, Object> row = results.next();
    assertThat(row).isNotNull();
    User user = (User) row.get("user");
    assertThat(user).isNotNull();
    Movie movie = (Movie) row.get("movie");
    assertThat(movie).isNotNull();
    assertThat(user.getName()).isEqualTo("Vince");
    assertThat(movie.getTitle()).isEqualTo("Top Gear");
    assertThat(results.hasNext()).isFalse();
}
Also used : Movie(org.neo4j.ogm.domain.cineasts.annotated.Movie) ExtendedUser(org.neo4j.ogm.domain.cineasts.annotated.ExtendedUser) User(org.neo4j.ogm.domain.cineasts.annotated.User) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Result(org.neo4j.ogm.model.Result) Test(org.junit.Test)

Example 5 with User

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

the class QueryCapabilityTest method shouldBeAbleToMapVariableDepthRelationshipsWithIncompletePaths.

// DATAGRAPH-700
@Test
public void shouldBeAbleToMapVariableDepthRelationshipsWithIncompletePaths() {
    Map<String, Object> params = new HashMap<>();
    params.put("name", "Vince");
    params.put("title", "Top Gear");
    Iterator<Map<String, Object>> results = session.query("match (u:User {name:$name}) match (m:Movie {title:$title}) match (u)-[r*0..2]-(m) return u,r,m", params).iterator();
    assertThat(results).isNotNull();
    Map<String, Object> result = results.next();
    assertThat(result).isNotNull();
    /*
            Expect 2 rows:
             one with (vince)-[:FRIENDS]-(michal)-[:RATED]-(topgear) where FRIENDS cannot be mapped because michal isn't known
             one with (vince)-[:RATED]-(top gear) where RATED can be mapped
         */
    boolean ratedRelationshipFound = false;
    Movie movie = (Movie) result.get("m");
    assertThat(movie).isNotNull();
    assertThat(movie.getTitle()).isEqualTo("Top Gear");
    User user = (User) result.get("u");
    assertThat(user).isNotNull();
    assertThat(user.getName()).isEqualTo("Vince");
    List<Rating> ratings = (List) result.get("r");
    if (ratings.size() == 1) {
        // because the list of ratings with size 2= friends,rated relationships
        Rating rating = ratings.get(0);
        assertThat(rating).isNotNull();
        assertThat(rating.getStars()).isEqualTo(4);
        assertThat(movie.getRatings().iterator().next().getId()).isEqualTo(rating.getId());
        assertThat(user.getRatings().iterator().next().getId()).isEqualTo(rating.getId());
        ratedRelationshipFound = true;
    }
    assertThat(user.getFriends()).isNull();
    result = results.next();
    assertThat(result).isNotNull();
    movie = (Movie) result.get("m");
    assertThat(movie).isNotNull();
    assertThat(movie.getTitle()).isEqualTo("Top Gear");
    user = (User) result.get("u");
    assertThat(user).isNotNull();
    assertThat(user.getName()).isEqualTo("Vince");
    ratings = (List) result.get("r");
    if (ratings.size() == 1) {
        // because the list of ratings with size 2= friends,rated relationships
        Rating rating = ratings.get(0);
        assertThat(rating).isNotNull();
        assertThat(rating.getStars()).isEqualTo(4);
        assertThat(movie.getRatings().iterator().next().getId()).isEqualTo(rating.getId());
        assertThat(user.getRatings().iterator().next().getId()).isEqualTo(rating.getId());
        ratedRelationshipFound = true;
    }
    assertThat(ratedRelationshipFound).isTrue();
    assertThat(user.getFriends()).isNull();
}
Also used : Movie(org.neo4j.ogm.domain.cineasts.annotated.Movie) ExtendedUser(org.neo4j.ogm.domain.cineasts.annotated.ExtendedUser) User(org.neo4j.ogm.domain.cineasts.annotated.User) HashMap(java.util.HashMap) Rating(org.neo4j.ogm.domain.cineasts.annotated.Rating) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

User (org.neo4j.ogm.domain.cineasts.annotated.User)46 Test (org.junit.Test)45 Movie (org.neo4j.ogm.domain.cineasts.annotated.Movie)25 Rating (org.neo4j.ogm.domain.cineasts.annotated.Rating)24 Filter (org.neo4j.ogm.cypher.Filter)19 ExtendedUser (org.neo4j.ogm.domain.cineasts.annotated.ExtendedUser)17 Map (java.util.Map)12 HashMap (java.util.HashMap)11 HashSet (java.util.HashSet)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Filters (org.neo4j.ogm.cypher.Filters)3 Pet (org.neo4j.ogm.domain.cineasts.annotated.Pet)3 Pagination (org.neo4j.ogm.cypher.query.Pagination)2 SortOrder (org.neo4j.ogm.cypher.query.SortOrder)2 Plays (org.neo4j.ogm.domain.cineasts.annotated.Plays)2 Result (org.neo4j.ogm.model.Result)2 Session (org.neo4j.ogm.session.Session)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URL (java.net.URL)1