Search in sources :

Example 21 with Rating

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

the class CineastsRelationshipEntityTest method shouldCreateREWithExistingStartAndEndNodes.

@Test
public void shouldCreateREWithExistingStartAndEndNodes() {
    bootstrap("org/neo4j/ogm/cql/cineasts.cql");
    Collection<Movie> films = session.loadAll(Movie.class, new Filter("title", ComparisonOperator.EQUALS, "Top Gear"));
    Movie movie = films.iterator().next();
    assertThat(movie.getRatings()).hasSize(2);
    User michal = null;
    for (Rating rating : movie.getRatings()) {
        if (rating.getUser().getName().equals("Michal")) {
            michal = rating.getUser();
            break;
        }
    }
    assertThat(michal).isNotNull();
    Set<Rating> ratings = new HashSet<>();
    Rating awesome = new Rating();
    awesome.setComment("Awesome");
    awesome.setMovie(movie);
    awesome.setUser(michal);
    awesome.setStars(5);
    ratings.add(awesome);
    // Overwrite Michal's earlier rating
    michal.setRatings(ratings);
    movie.setRatings(ratings);
    session.save(movie);
    Collection<Movie> movies = session.loadAll(Movie.class, new Filter("title", ComparisonOperator.EQUALS, "Top Gear"));
    movie = movies.iterator().next();
    assertThat(movie.getRatings()).isNotNull();
    assertThat(movie.getRatings()).hasSize(1);
    assertThat(movie.getRatings().iterator().next().getUser().getName()).isEqualTo("Michal");
}
Also used : Movie(org.neo4j.ogm.domain.cineasts.annotated.Movie) User(org.neo4j.ogm.domain.cineasts.annotated.User) Filter(org.neo4j.ogm.cypher.Filter) Rating(org.neo4j.ogm.domain.cineasts.annotated.Rating) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 22 with Rating

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

the class CineastsRelationshipEntityTest method shouldLoadASingleRating.

@Test
public void shouldLoadASingleRating() {
    Movie movie = new Movie("Pulp Fiction", 1994);
    session.save(movie);
    User michal = new User();
    michal.setName("Michal");
    michal.setLogin("bachmania");
    Rating awesome = new Rating();
    awesome.setMovie(movie);
    awesome.setUser(michal);
    awesome.setStars(5);
    michal.setRatings(Collections.singleton(awesome));
    session.save(michal);
    // Add a rating from luanne for the same movie
    User luanne = new User();
    luanne.setName("luanne");
    luanne.setLogin("luanne");
    luanne.setPassword("luanne");
    Rating rating = new Rating();
    rating.setMovie(movie);
    rating.setUser(luanne);
    rating.setStars(3);
    luanne.setRatings(Collections.singleton(rating));
    session.save(luanne);
    session.clear();
    Rating loadedRating = session.load(Rating.class, rating.getId(), 3);
    assertThat(loadedRating).isNotNull();
    assertThat(loadedRating.getId()).isEqualTo(rating.getId());
    assertThat(loadedRating.getStars()).isEqualTo(rating.getStars());
}
Also used : Movie(org.neo4j.ogm.domain.cineasts.annotated.Movie) User(org.neo4j.ogm.domain.cineasts.annotated.User) Rating(org.neo4j.ogm.domain.cineasts.annotated.Rating) Test(org.junit.Test)

Example 23 with Rating

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

the class QueryCapabilityTest method shouldBeAbleToMapRelationshipEntities.

// DATAGRAPH-700
@Test
public void shouldBeAbleToMapRelationshipEntities() {
    Iterator<Map<String, Object>> results = session.query("MATCH (u:User {name:$name})-[r:RATED]->(m) RETURN u,r,m", Collections.singletonMap("name", "Vince")).iterator();
    assertThat(results).isNotNull();
    Map<String, Object> result = results.next();
    assertThat(result).isNotNull();
    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");
    Rating rating = (Rating) result.get("r");
    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());
    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) Map(java.util.Map) HashMap(java.util.HashMap) 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