Search in sources :

Example 31 with User

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

the class CineastsRelationshipEntityTest method shouldBeAbleToDeleteAllRatings.

// DATAGRAPH-586
@Test
public void shouldBeAbleToDeleteAllRatings() {
    Set<Rating> gobletRatings = new HashSet<>();
    Set<Rating> phoenixRatings = new HashSet<>();
    Movie goblet = new Movie("Harry Potter and the Goblet of Fire", 2006);
    session.save(goblet);
    Movie phoenix = new Movie("Harry Potter and the Order of the Phoenix", 2009);
    session.save(phoenix);
    User adam = new User();
    adam.setName("Adam");
    adam.setLogin("adamg");
    Rating good = new Rating();
    good.setUser(adam);
    good.setMovie(goblet);
    good.setStars(3);
    gobletRatings.add(good);
    goblet.setRatings(gobletRatings);
    Rating okay = new Rating();
    okay.setMovie(phoenix);
    okay.setUser(adam);
    okay.setStars(2);
    phoenixRatings.add(okay);
    phoenix.setRatings(phoenixRatings);
    Set<Rating> adamsRatings = new HashSet<>();
    adamsRatings.add(good);
    adamsRatings.add(okay);
    adam.setRatings(adamsRatings);
    session.save(adam);
    adam = session.loadAll(User.class, new Filter("name", ComparisonOperator.EQUALS, "Adam")).iterator().next();
    assertThat(adam.getRatings()).hasSize(2);
    // delete all ratings
    session.deleteAll(Rating.class);
    assertThat(session.loadAll(Rating.class)).isEmpty();
    phoenix = session.loadAll(Movie.class, new Filter("title", ComparisonOperator.EQUALS, "Harry Potter and the Order of the Phoenix")).iterator().next();
    assertThat(phoenix.getRatings()).isNull();
    goblet = session.loadAll(Movie.class, new Filter("title", ComparisonOperator.EQUALS, "Harry Potter and the Goblet of Fire")).iterator().next();
    assertThat(goblet.getRatings()).isNull();
    adam = session.loadAll(User.class, new Filter("name", ComparisonOperator.EQUALS, "Adam")).iterator().next();
    assertThat(adam.getRatings()).isNull();
}
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 32 with User

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

the class CineastsRelationshipEntityTest method shouldBeAbleToSaveAndUpdateMultipleUserRatings.

// DATAGRAPH-569
@Test
public void shouldBeAbleToSaveAndUpdateMultipleUserRatings() {
    Set<Rating> gobletRatings = new HashSet<>();
    Set<Rating> phoenixRatings = new HashSet<>();
    Movie goblet = new Movie("Harry Potter and the Goblet of Fire", 2006);
    session.save(goblet);
    Movie phoenix = new Movie("Harry Potter and the Order of the Phoenix", 2009);
    session.save(phoenix);
    User adam = new User();
    adam.setName("Adam");
    adam.setLogin("adamg");
    Rating good = new Rating();
    good.setUser(adam);
    good.setMovie(goblet);
    good.setStars(3);
    gobletRatings.add(good);
    goblet.setRatings(gobletRatings);
    Rating okay = new Rating();
    okay.setMovie(phoenix);
    okay.setUser(adam);
    okay.setStars(2);
    phoenixRatings.add(okay);
    phoenix.setRatings(phoenixRatings);
    Set<Rating> adamsRatings = new HashSet<>();
    adamsRatings.add(good);
    adamsRatings.add(okay);
    adam.setRatings(adamsRatings);
    session.save(adam);
    Collection<Movie> movies = session.loadAll(Movie.class, new Filter("title", ComparisonOperator.EQUALS, "Harry Potter and the Goblet of Fire"));
    goblet = movies.iterator().next();
    assertThat(goblet.getRatings()).isNotNull();
    assertThat(goblet.getRatings()).hasSize(1);
    movies = session.loadAll(Movie.class, new Filter("title", ComparisonOperator.EQUALS, "Harry Potter and the Order of the Phoenix"));
    phoenix = movies.iterator().next();
    assertThat(phoenix.getRatings()).isNotNull();
    assertThat(phoenix.getRatings()).hasSize(1);
    adam = session.loadAll(User.class, new Filter("name", ComparisonOperator.EQUALS, "Adam")).iterator().next();
    assertThat(adam.getRatings()).hasSize(2);
    // Get rid of the Phoenix rating
    adam.setRatings(gobletRatings);
    session.save(adam);
    adam = session.loadAll(User.class, new Filter("name", ComparisonOperator.EQUALS, "Adam")).iterator().next();
    assertThat(adam.getRatings()).hasSize(1);
    movies = session.loadAll(Movie.class, new Filter("title", ComparisonOperator.EQUALS, "Harry Potter and the Order of the Phoenix"));
    phoenix = movies.iterator().next();
    assertThat(phoenix.getRatings()).isNull();
    movies = session.loadAll(Movie.class, new Filter("title", ComparisonOperator.EQUALS, "Harry Potter and the Goblet of Fire"));
    goblet = movies.iterator().next();
    assertThat(goblet.getRatings()).isNotNull();
    assertThat(goblet.getRatings()).hasSize(1);
}
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 33 with User

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

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

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

the class CineastsRelationshipEntityTest method testFilterOnRelatedNode.

@Test
public void testFilterOnRelatedNode() throws Exception {
    User frantisek = new User();
    frantisek.setName("Frantisek");
    frantisek.setLogin("frantisek");
    User michal = new User();
    michal.setName("Michal");
    michal.setLogin("michal");
    User ottoH = new User();
    ottoH.setName("Otto");
    ottoH.setLogin("otto");
    User ottoB = new User();
    ottoB.setName("Otto von Bismarc");
    ottoB.setLogin("ottob");
    frantisek.addFriends(ottoH, ottoB);
    session.save(frantisek);
    michal.addFriends(ottoH, ottoB);
    session.save(michal);
    // name contains ' '
    Filter filter = new Filter("name", ComparisonOperator.CONTAINING, "Otto");
    filter.setNestedPropertyName("friends");
    filter.setNestedPropertyType(User.class);
    SortOrder sortOrder = new SortOrder();
    sortOrder.add("name");
    Collection<User> users = session.loadAll(User.class, filter, sortOrder, new Pagination(0, 2));
    assertThat(users).hasSize(2);
}
Also used : Pagination(org.neo4j.ogm.cypher.query.Pagination) User(org.neo4j.ogm.domain.cineasts.annotated.User) Filter(org.neo4j.ogm.cypher.Filter) SortOrder(org.neo4j.ogm.cypher.query.SortOrder) 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