Search in sources :

Example 1 with Rating

use of org.neo4j.ogm.domain.cineasts.annotated.Rating 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 2 with Rating

use of org.neo4j.ogm.domain.cineasts.annotated.Rating 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)

Example 3 with Rating

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

the class QueryCapabilityTest method shouldBeAbleToMapCollectionsFromPath.

// DATAGRAPH-700
@Test
public void shouldBeAbleToMapCollectionsFromPath() {
    Iterator<Map<String, Object>> results = session.query("MATCH p=(u:User {name:$name})-[r:RATED]->(m) RETURN nodes(p) as nodes, relationships(p) as rels", Collections.singletonMap("name", "Vince")).iterator();
    assertThat(results).isNotNull();
    Map<String, Object> result = results.next();
    assertThat(result).isNotNull();
    List<Object> nodes = (List) result.get("nodes");
    List<Object> rels = (List) result.get("rels");
    assertThat(nodes).hasSize(2);
    assertThat(rels).hasSize(1);
    for (Object o : nodes) {
        if (o instanceof User) {
            User user = (User) o;
            assertThat(user.getName()).isEqualTo("Vince");
            assertThat(user.getRatings()).hasSize(1);
            Movie movie = user.getRatings().iterator().next().getMovie();
            assertThat(movie).isNotNull();
            assertThat(movie.getTitle()).isEqualTo("Top Gear");
            Rating rating = movie.getRatings().iterator().next();
            assertThat(rating).isNotNull();
            assertThat(rating.getStars()).isEqualTo(4);
        }
    }
}
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 Rating

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

the class CineastsIntegrationTest method loadParticularUserRatingsAndComments.

@Test
public void loadParticularUserRatingsAndComments() {
    Collection<User> filmCritics = session.loadAll(User.class, new Filter("name", ComparisonOperator.EQUALS, "Michal"));
    assertThat(filmCritics).hasSize(1);
    User critic = filmCritics.iterator().next();
    assertThat(critic.getRatings()).hasSize(2);
    for (Rating rating : critic.getRatings()) {
        assertThat(rating.getComment()).as("The comment should've been mapped").isNotNull();
        assertThat(rating.getStars() > 0).as("The star rating should've been mapped").isTrue();
        assertThat(rating.getUser()).as("The user start node should've been mapped").isNotNull();
        assertThat(rating.getMovie()).as("The movie end node should've been mapped").isNotNull();
    }
}
Also used : User(org.neo4j.ogm.domain.cineasts.annotated.User) Filter(org.neo4j.ogm.cypher.Filter) Rating(org.neo4j.ogm.domain.cineasts.annotated.Rating) Test(org.junit.Test)

Example 5 with Rating

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

the class CineastsIntegrationTest method loadRatingsForSpecificFilm.

@Test
public void loadRatingsForSpecificFilm() {
    Collection<Movie> films = session.loadAll(Movie.class, new Filter("title", ComparisonOperator.EQUALS, "Top Gear"));
    assertThat(films).hasSize(1);
    Movie film = films.iterator().next();
    assertThat(film.getRatings()).hasSize(2);
    for (Rating rating : film.getRatings()) {
        assertThat(rating.getStars() > 0).as("The star rating should've been mapped").isTrue();
        assertThat(rating.getUser()).as("The user start node should've been mapped").isNotNull();
        assertThat(rating.getMovie()).as("The wrong film was mapped to the rating").isSameAs(film);
    }
}
Also used : Movie(org.neo4j.ogm.domain.cineasts.annotated.Movie) Filter(org.neo4j.ogm.cypher.Filter) Rating(org.neo4j.ogm.domain.cineasts.annotated.Rating) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)23 Rating (org.neo4j.ogm.domain.cineasts.annotated.Rating)23 Movie (org.neo4j.ogm.domain.cineasts.annotated.Movie)19 User (org.neo4j.ogm.domain.cineasts.annotated.User)19 Filter (org.neo4j.ogm.cypher.Filter)12 HashSet (java.util.HashSet)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ExtendedUser (org.neo4j.ogm.domain.cineasts.annotated.ExtendedUser)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Pagination (org.neo4j.ogm.cypher.query.Pagination)1 SortOrder (org.neo4j.ogm.cypher.query.SortOrder)1 Actor (org.neo4j.ogm.domain.cineasts.annotated.Actor)1 MetaData (org.neo4j.ogm.metadata.MetaData)1 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)1