Search in sources :

Example 6 with Movie

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

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

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

the class QueryCapabilityTest method shouldLoadNodesWithUnmappedOrNoLabels.

// #150
@Test
public void shouldLoadNodesWithUnmappedOrNoLabels() {
    int movieCount = 0;
    int userCount = 0;
    int unmappedCount = 0;
    int noLabelCount = 0;
    session.query("CREATE (unknown), (m:Unmapped), (n:Movie), (n)-[:UNKNOWN]->(m)", emptyMap());
    Result result = session.query("MATCH (n) return n", emptyMap());
    assertThat(result).isNotNull();
    Iterator<Map<String, Object>> resultIterator = result.iterator();
    while (resultIterator.hasNext()) {
        Map<String, Object> row = resultIterator.next();
        Object n = row.get("n");
        if (n instanceof User) {
            userCount++;
        } else if (n instanceof Movie) {
            movieCount++;
        } else if (n instanceof NodeModel) {
            if (((NodeModel) n).getLabels().length == 0) {
                noLabelCount++;
            } else if (((NodeModel) n).getLabels()[0].equals("Unmapped")) {
                unmappedCount++;
            }
        }
    }
    assertThat(unmappedCount).isEqualTo(1);
    assertThat(noLabelCount).isEqualTo(1);
    assertThat(movieCount).isEqualTo(4);
    assertThat(userCount).isEqualTo(5);
}
Also used : Movie(org.neo4j.ogm.domain.cineasts.annotated.Movie) NodeModel(org.neo4j.ogm.response.model.NodeModel) ExtendedUser(org.neo4j.ogm.domain.cineasts.annotated.ExtendedUser) User(org.neo4j.ogm.domain.cineasts.annotated.User) Map(java.util.Map) HashMap(java.util.HashMap) Result(org.neo4j.ogm.model.Result) Test(org.junit.Test)

Example 9 with Movie

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

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

the class CineastsIntegrationTest method shouldBeAbleToSetNodePropertiesToNull.

// GH-128
@Test
public void shouldBeAbleToSetNodePropertiesToNull() throws MalformedURLException {
    Movie movie = new Movie("Zootopia", 2016);
    movie.setImdbUrl(new URL("http://www.imdb.com/title/tt2948356/"));
    session.save(movie);
    movie.setTitle(null);
    movie.setImdbUrl(null);
    session.save(movie);
    movie = session.load(Movie.class, movie.getUuid());
    assertThat(movie.getTitle()).isNull();
    assertThat(movie.getImdbUrl()).isNull();
    session.clear();
    movie = session.load(Movie.class, movie.getUuid());
    assertThat(movie.getTitle()).isNull();
    assertThat(movie.getImdbUrl()).isNull();
}
Also used : Movie(org.neo4j.ogm.domain.cineasts.annotated.Movie) URL(java.net.URL) 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