Search in sources :

Example 31 with Movie

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

the class CineastsRelationshipEntityTest method shouldRetainREsWhenAStartOrEndNodeIsLoaded.

/**
 * @see DATAGRAPH-704
 */
@Test
public void shouldRetainREsWhenAStartOrEndNodeIsLoaded() {
    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);
    session.loadAll(User.class, new Filter("name", ComparisonOperator.EQUALS, "Michal")).iterator().next();
    assertThat(movie.getRatings()).hasSize(// should not lose one rating because Michal is loaded; ratings should me merged and not overwritten
    2);
}
Also used : Movie(org.neo4j.ogm.domain.cineasts.annotated.Movie) Filter(org.neo4j.ogm.cypher.Filter) Test(org.junit.Test)

Example 32 with Movie

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

the class CineastsRelationshipEntityTest method shouldSaveAndRetrieveRelationshipEntitiesDirectly.

// DATAGRAPH-552
@Test
public void shouldSaveAndRetrieveRelationshipEntitiesDirectly() {
    // we need some stuff in the database
    session.query("CREATE " + "(nc:NotAClass {name:'Colin'}), " + "(g:NotAClass {age: 39}), " + "(g)-[:TEST {comment : 'test'}]->(nc)", Collections.emptyMap());
    User critic = new User();
    critic.setName("Gary");
    critic.setLogin("gman");
    Movie film = new Movie("Fast and Furious XVII", 2015);
    Rating filmRating = new Rating();
    filmRating.setUser(critic);
    critic.setRatings(Collections.singleton(filmRating));
    filmRating.setMovie(film);
    film.setRatings(Collections.singleton(filmRating));
    filmRating.setStars(2);
    filmRating.setComment("They've made far too many of these films now!");
    session.save(filmRating);
    // load the rating by id
    Rating loadedRating = session.load(Rating.class, filmRating.getId());
    assertThat(loadedRating).as("The loaded rating shouldn't be null").isNotNull();
    assertThat(loadedRating.getStars()).as("The relationship properties weren't saved correctly").isEqualTo(filmRating.getStars());
    assertThat(loadedRating.getMovie().getTitle()).as("The rated film wasn't saved correctly").isEqualTo(film.getTitle());
    assertThat(loadedRating.getUser().getLogin()).as("The critic wasn't saved correctly").isEqualTo(critic.getLogin());
}
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 33 with Movie

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

the class CineastsRelationshipEntityTest method shouldLoadActorsForAPersistedMovie.

@Test
public void shouldLoadActorsForAPersistedMovie() {
    session.query("CREATE " + "(dh:Movie {title:'Die Hard'}), " + "(bw:Actor {name: 'Bruce Willis'}), " + "(bw)-[:ACTS_IN {role : 'John'}]->(dh)", Collections.emptyMap());
    // Movie dieHard = TestUtils.firstOrNull(session.loadByProperty(Movie.class, new Parameter("title", "Die Hard")));
    Movie dieHard = session.loadAll(Movie.class).iterator().next();
    assertThat(dieHard).isNotNull();
    assertThat(dieHard.getRoles()).isNotNull();
    assertThat(dieHard.getRoles()).hasSize(1);
}
Also used : Movie(org.neo4j.ogm.domain.cineasts.annotated.Movie) Test(org.junit.Test)

Example 34 with Movie

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

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

the class CineastsRelationshipEntityTest method shouldSaveSameRoleTwiceRelationshipBetweenTheSameTwoObjects.

@Test
public void shouldSaveSameRoleTwiceRelationshipBetweenTheSameTwoObjects() {
    Movie movie = new Movie("The big John Travolta Party", 2016);
    Actor actor = new Actor("John Travolta");
    actor.playedIn(movie, "He danced mostly");
    assertThat(actor.getRoles()).hasSize(1);
    session.save(actor);
    session.clear();
    Actor loadedActor = session.load(Actor.class, actor.getUuid());
    assertThat(loadedActor.getRoles()).hasSize(1);
    // Add an identical role
    actor.playedIn(movie, "He danced mostly");
    assertThat(actor.getRoles()).hasSize(2);
    session.save(actor);
    session.clear();
    loadedActor = session.load(Actor.class, actor.getUuid());
    assertThat(loadedActor.getRoles()).hasSize(2);
}
Also used : Movie(org.neo4j.ogm.domain.cineasts.annotated.Movie) Actor(org.neo4j.ogm.domain.cineasts.annotated.Actor) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)41 Movie (org.neo4j.ogm.domain.cineasts.annotated.Movie)41 User (org.neo4j.ogm.domain.cineasts.annotated.User)25 Rating (org.neo4j.ogm.domain.cineasts.annotated.Rating)24 Filter (org.neo4j.ogm.cypher.Filter)17 HashMap (java.util.HashMap)11 Map (java.util.Map)11 ExtendedUser (org.neo4j.ogm.domain.cineasts.annotated.ExtendedUser)8 Actor (org.neo4j.ogm.domain.cineasts.annotated.Actor)7 HashSet (java.util.HashSet)6 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Result (org.neo4j.ogm.model.Result)3 Filters (org.neo4j.ogm.cypher.Filters)2 Pet (org.neo4j.ogm.domain.cineasts.annotated.Pet)2 Plays (org.neo4j.ogm.domain.cineasts.annotated.Plays)2 Role (org.neo4j.ogm.domain.cineasts.annotated.Role)2 URL (java.net.URL)1 Pagination (org.neo4j.ogm.cypher.query.Pagination)1 SortOrder (org.neo4j.ogm.cypher.query.SortOrder)1