Java Code Examples for org.neo4j.driver.types.Node#id()

The following examples show how to use org.neo4j.driver.types.Node#id() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RepositoryIT.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void findEntityWithBidirectionalRelationship(@Autowired BidirectionalStartRepository repository) {

	long startId;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (n:BidirectionalStart{name:'Ernie'})-[:CONNECTED]->(e:BidirectionalEnd{name:'Bert'}), "
				+ "(e)<-[:ANOTHER_CONNECTION]-(anotherStart:BidirectionalStart{name:'Elmo'})"
				+ "RETURN n").single();

		Node startNode = record.get("n").asNode();
		startId = startNode.id();
	}

	Optional<BidirectionalStart> entityOptional = repository.findById(startId);
	assertThat(entityOptional).isPresent();
	BidirectionalStart entity = entityOptional.get();
	assertThat(entity.getEnds()).hasSize(1);

	BidirectionalEnd end = entity.getEnds().iterator().next();
	assertThat(end.getAnotherStart()).isNotNull();
	assertThat(end.getAnotherStart().getName()).isEqualTo("Elmo");

}
 
Example 2
Source File: RepositoryIT.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void findEntityWithBidirectionalRelationshipFromIncomingSide(@Autowired BidirectionalEndRepository repository) {

	long endId;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (n:BidirectionalStart{name:'Ernie'})-[:CONNECTED]->(e:BidirectionalEnd{name:'Bert'}) "
				+ "RETURN e").single();

		Node endNode = record.get("e").asNode();
		endId = endNode.id();
	}

	Optional<BidirectionalEnd> entityOptional = repository.findById(endId);
	assertThat(entityOptional).isPresent();
	BidirectionalEnd entity = entityOptional.get();
	assertThat(entity.getStart()).isNotNull();

}
 
Example 3
Source File: RepositoryIT.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void findEntityWithRelationshipWithAssignedId(@Autowired PetRepository repository) {

	long petNodeId;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (p:Pet{name:'Jerry'})-[:Has]->(t:Thing{theId:'t1', name:'Thing1'}) "
				+ "RETURN p, t").single();

		Node petNode = record.get("p").asNode();
		petNodeId = petNode.id();
	}

	Pet pet = repository.findById(petNodeId).get();
	ThingWithAssignedId relatedThing = pet.getThings().get(0);
	assertThat(relatedThing.getTheId()).isEqualTo("t1");
	assertThat(relatedThing.getName()).isEqualTo("Thing1");
}
 
Example 4
Source File: ReactiveRepositoryIT.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void loadEntityWithBidirectionalRelationship(@Autowired BidirectionalStartRepository repository) {

	long startId;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (n:BidirectionalStart{name:'Ernie'})-[:CONNECTED]->(e:BidirectionalEnd{name:'Bert'}) "
				+ "RETURN n").single();

		Node startNode = record.get("n").asNode();
		startId = startNode.id();
	}

	StepVerifier.create(repository.findById(startId))
		.assertNext(entity -> {
			assertThat(entity.getEnds()).hasSize(1);
		})
		.verifyComplete();
}
 
Example 5
Source File: ReactiveRepositoryIT.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void loadEntityWithBidirectionalRelationshipFromIncomingSide(@Autowired BidirectionalEndRepository repository) {

	long endId;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (n:BidirectionalStart{name:'Ernie'})-[:CONNECTED]->(e:BidirectionalEnd{name:'Bert'}) "
				+ "RETURN e").single();

		Node endNode = record.get("e").asNode();
		endId = endNode.id();
	}

	StepVerifier.create(repository.findById(endId))
		.assertNext(entity -> {
			assertThat(entity.getStart()).isNotNull();
		})
		.verifyComplete();
}
 
Example 6
Source File: ReactiveRepositoryIT.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Test
void loadEntityWithRelationshipWithAssignedId(@Autowired ReactivePetRepository repository) {

	long petNodeId;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (p:Pet{name:'Jerry'})-[:Has]->(t:Thing{theId:'t1', name:'Thing1'}) "
				+ "RETURN p, t").single();

		Node petNode = record.get("p").asNode();
		petNodeId = petNode.id();
	}

	StepVerifier.create(repository.findById(petNodeId))
		.assertNext(pet -> {
			ThingWithAssignedId relatedThing = pet.getThings().get(0);
			assertThat(relatedThing.getTheId()).isEqualTo("t1");
			assertThat(relatedThing.getName()).isEqualTo("Thing1");
		})
		.verifyComplete();
}
 
Example 7
Source File: RepositoryIT.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void findEntityWithRelationshipViaQuery(@Autowired RelationshipRepository repository) {

	long personId;
	long hobbyNodeId;
	long petNode1Id;
	long petNode2Id;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (n:PersonWithRelationship{name:'Freddie'})-[:Has]->(h1:Hobby{name:'Music'}), "
				+ "(n)-[:Has]->(p1:Pet{name: 'Jerry'}), (n)-[:Has]->(p2:Pet{name: 'Tom'}) "
				+ "RETURN n, h1, p1, p2").single();

		Node personNode = record.get("n").asNode();
		Node hobbyNode1 = record.get("h1").asNode();
		Node petNode1 = record.get("p1").asNode();
		Node petNode2 = record.get("p2").asNode();

		personId = personNode.id();
		hobbyNodeId = hobbyNode1.id();
		petNode1Id = petNode1.id();
		petNode2Id = petNode2.id();
	}

	PersonWithRelationship loadedPerson = repository.getPersonWithRelationshipsViaQuery();
	assertThat(loadedPerson.getName()).isEqualTo("Freddie");
	assertThat(loadedPerson.getId()).isEqualTo(personId);
	Hobby hobby = loadedPerson.getHobbies();
	assertThat(hobby).isNotNull();
	assertThat(hobby.getId()).isEqualTo(hobbyNodeId);
	assertThat(hobby.getName()).isEqualTo("Music");

	List<Pet> pets = loadedPerson.getPets();
	Pet comparisonPet1 = new Pet(petNode1Id, "Jerry");
	Pet comparisonPet2 = new Pet(petNode2Id, "Tom");
	assertThat(pets).containsExactlyInAnyOrder(comparisonPet1, comparisonPet2);

}
 
Example 8
Source File: MovieRepository.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
public Movie save(Movie movie) {
	try (Session session = driver.session()) {
		Record r = session.run("CREATE (m:Movie {title: $title, tagline: $tagline}) RETURN m", Values.parameters("title", movie.getTitle(), "tagline", movie.getTagline()))
			.single();
		Node movieNode = r.get("m").asNode();
		return new Movie(movieNode.id(), movieNode.get("title").asString(), movieNode.get("tagline").asString());
	}
}
 
Example 9
Source File: Neo4JSession.java    From neo4j-gremlin-bolt with Apache License 2.0 4 votes vote down vote up
private Edge loadEdge(Record record) {
    // relationship
    Relationship relationship = record.get(1).asRelationship();
    // edge id
    Object edgeId = edgeIdProvider.get(relationship);
    // check edge has been deleted
    if (!deletedEdges.contains(edgeId)) {
        // check we have record in memory
        Neo4JEdge edge = edges.get(edgeId);
        if (edge == null) {
            // nodes
            Node firstNode = record.get(0).asNode();
            Node secondNode = record.get(2).asNode();
            // node ids
            Object firstNodeId = vertexIdProvider.get(firstNode);
            Object secondNodeId = vertexIdProvider.get(secondNode);
            // check edge has been deleted (one of the vertices was deleted) or the vertices are not in the read partition
            if (deletedVertices.contains(firstNodeId) || deletedVertices.contains(secondNodeId) || !partition.containsVertex(StreamSupport.stream(firstNode.labels().spliterator(), false).collect(Collectors.toSet())) || !partition.containsVertex(StreamSupport.stream(secondNode.labels().spliterator(), false).collect(Collectors.toSet())))
                return null;
            // check we have first vertex in memory
            Neo4JVertex firstVertex = vertices.get(firstNodeId);
            if (firstVertex == null) {
                // create vertex
                firstVertex = new Neo4JVertex(graph, this, vertexIdProvider, edgeIdProvider, firstNode);
                // register it
                registerVertex(firstVertex);
            }
            // check we have second vertex in memory
            Neo4JVertex secondVertex = vertices.get(secondNodeId);
            if (secondVertex == null) {
                // create vertex
                secondVertex = new Neo4JVertex(graph, this, vertexIdProvider, edgeIdProvider, secondNode);
                // register it
                registerVertex(secondVertex);
            }
            // find out start and end of the relationship (edge could come in either direction)
            Neo4JVertex out = relationship.startNodeId() == firstNode.id() ? firstVertex : secondVertex;
            Neo4JVertex in = relationship.endNodeId() == firstNode.id() ? firstVertex : secondVertex;
            // create edge
            edge = new Neo4JEdge(graph, this, edgeIdProvider, out, relationship, in);
            // register with adjacent vertices
            out.addOutEdge(edge);
            in.addInEdge(edge);
            // register edge
            return registerEdge(edge);
        }
        // return edge
        return edge;
    }
    // skip edge
    return null;
}
 
Example 10
Source File: RepositoryIT.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
@Test
void findEntityWithRelationship(@Autowired RelationshipRepository repository) {

	long personId;
	long clubId;
	long hobbyNode1Id;
	long hobbyNode2Id;
	long petNode1Id;
	long petNode2Id;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (n:PersonWithRelationship{name:'Freddie'})-[:Has]->(h1:Hobby{name:'Music'}), "
				+ "(n)-[:Has]->(p1:Pet{name: 'Jerry'}), (n)-[:Has]->(p2:Pet{name: 'Tom'}), "
				+ "(n)<-[:Has]-(c:Club{name:'ClownsClub'}), "
				+ "(p1)-[:Has]->(h2:Hobby{name:'sleeping'}), "
				+ "(p1)-[:Has]->(p2)"
				+ "RETURN n, h1, h2, p1, p2, c").single();

		Node personNode = record.get("n").asNode();
		Node clubNode = record.get("c").asNode();
		Node hobbyNode1 = record.get("h1").asNode();
		Node hobbyNode2 = record.get("h2").asNode();
		Node petNode1 = record.get("p1").asNode();
		Node petNode2 = record.get("p2").asNode();

		personId = personNode.id();
		clubId = clubNode.id();
		hobbyNode1Id = hobbyNode1.id();
		hobbyNode2Id = hobbyNode2.id();
		petNode1Id = petNode1.id();
		petNode2Id = petNode2.id();
	}

	PersonWithRelationship loadedPerson = repository.findById(personId).get();
	assertThat(loadedPerson.getName()).isEqualTo("Freddie");
	Hobby hobby = loadedPerson.getHobbies();
	assertThat(hobby).isNotNull();
	assertThat(hobby.getId()).isEqualTo(hobbyNode1Id);
	assertThat(hobby.getName()).isEqualTo("Music");

	Club club = loadedPerson.getClub();
	assertThat(club).isNotNull();
	assertThat(club.getId()).isEqualTo(clubId);
	assertThat(club.getName()).isEqualTo("ClownsClub");

	List<Pet> pets = loadedPerson.getPets();
	Pet comparisonPet1 = new Pet(petNode1Id, "Jerry");
	Pet comparisonPet2 = new Pet(petNode2Id, "Tom");
	assertThat(pets).containsExactlyInAnyOrder(comparisonPet1, comparisonPet2);

	Pet pet1 = pets.get(pets.indexOf(comparisonPet1));
	Pet pet2 = pets.get(pets.indexOf(comparisonPet2));
	Hobby petHobby = pet1.getHobbies().iterator().next();
	assertThat(petHobby.getId()).isEqualTo(hobbyNode2Id);
	assertThat(petHobby.getName()).isEqualTo("sleeping");

	assertThat(pet1.getFriends()).containsExactly(pet2);

}
 
Example 11
Source File: Fruit.java    From quarkus-quickstarts with Apache License 2.0 4 votes vote down vote up
public static Fruit from(Node node) {
    return new Fruit(node.id(), node.get("name").asString());
}
 
Example 12
Source File: MovieRepository.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
private static Movie mapToMovie(Record r) {
	Node movieNode = r.get("m").asNode();
	return new Movie(movieNode.id(), movieNode.get("title").asString(), movieNode.get("tagline").asString());
}
 
Example 13
Source File: ReactiveRepositoryIT.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
@Test
void loadEntityWithRelationshipWithPropertiesFromCustomQuery(@Autowired ReactivePersonWithRelationshipWithPropertiesRepository repository) {

		long personId;
		long hobbyNode1Id;
		long hobbyNode2Id;

		try (Session session = createSession()) {
			Record record = session
				.run("CREATE (n:PersonWithRelationshipWithProperties{name:'Freddie'}),"
					+ " (n)-[l1:LIKES"
					+ "{since: 1995, active: true, localDate: date('1995-02-26'), myEnum: 'SOMETHING', point: point({x: 0, y: 1})}"
					+ "]->(h1:Hobby{name:'Music'}),"
					+ " (n)-[l2:LIKES"
					+ "{since: 2000, active: false, localDate: date('2000-06-28'), myEnum: 'SOMETHING_DIFFERENT', point: point({x: 2, y: 3})}"
					+ "]->(h2:Hobby{name:'Something else'})"
					+ "RETURN n, h1, h2").single();

			Node personNode = record.get("n").asNode();
			Node hobbyNode1 = record.get("h1").asNode();
			Node hobbyNode2 = record.get("h2").asNode();

			personId = personNode.id();
			hobbyNode1Id = hobbyNode1.id();
			hobbyNode2Id = hobbyNode2.id();
		}

		StepVerifier.create(repository.loadFromCustomQuery(personId))
			.assertNext(person -> {
				assertThat(person.getName()).isEqualTo("Freddie");

				Hobby hobby1 = new Hobby();
				hobby1.setName("Music");
				hobby1.setId(hobbyNode1Id);
				LikesHobbyRelationship rel1 = new LikesHobbyRelationship(1995);
				rel1.setActive(true);
				rel1.setLocalDate(LocalDate.of(1995, 2, 26));
				rel1.setMyEnum(LikesHobbyRelationship.MyEnum.SOMETHING);
				rel1.setPoint(new CartesianPoint2d(0d, 1d));

				Hobby hobby2 = new Hobby();
				hobby2.setName("Something else");
				hobby2.setId(hobbyNode2Id);
				LikesHobbyRelationship rel2 = new LikesHobbyRelationship(2000);
				rel2.setActive(false);
				rel2.setLocalDate(LocalDate.of(2000, 6, 28));
				rel2.setMyEnum(LikesHobbyRelationship.MyEnum.SOMETHING_DIFFERENT);
				rel2.setPoint(new CartesianPoint2d(2d, 3d));

				assertThat(person.getHobbies()).contains(MapEntry.entry(hobby1, rel1), MapEntry.entry(hobby2, rel2));
			})
			.verifyComplete();

	}
 
Example 14
Source File: ReactiveRepositoryIT.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
@Test
void loadEntityWithRelationshipWithProperties(@Autowired ReactivePersonWithRelationshipWithPropertiesRepository repository) {

	long personId;
	long hobbyNode1Id;
	long hobbyNode2Id;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (n:PersonWithRelationshipWithProperties{name:'Freddie'}),"
				+ " (n)-[l1:LIKES"
				+ "{since: 1995, active: true, localDate: date('1995-02-26'), myEnum: 'SOMETHING', point: point({x: 0, y: 1})}"
				+ "]->(h1:Hobby{name:'Music'}),"
				+ " (n)-[l2:LIKES"
				+ "{since: 2000, active: false, localDate: date('2000-06-28'), myEnum: 'SOMETHING_DIFFERENT', point: point({x: 2, y: 3})}"
				+ "]->(h2:Hobby{name:'Something else'})"
				+ "RETURN n, h1, h2").single();

		Node personNode = record.get("n").asNode();
		Node hobbyNode1 = record.get("h1").asNode();
		Node hobbyNode2 = record.get("h2").asNode();

		personId = personNode.id();
		hobbyNode1Id = hobbyNode1.id();
		hobbyNode2Id = hobbyNode2.id();
	}

	StepVerifier.create(repository.findById(personId))
		.assertNext(person -> {
			assertThat(person.getName()).isEqualTo("Freddie");

			Hobby hobby1 = new Hobby();
			hobby1.setName("Music");
			hobby1.setId(hobbyNode1Id);
			LikesHobbyRelationship rel1 = new LikesHobbyRelationship(1995);
			rel1.setActive(true);
			rel1.setLocalDate(LocalDate.of(1995, 2, 26));
			rel1.setMyEnum(LikesHobbyRelationship.MyEnum.SOMETHING);
			rel1.setPoint(new CartesianPoint2d(0d, 1d));

			Hobby hobby2 = new Hobby();
			hobby2.setName("Something else");
			hobby2.setId(hobbyNode2Id);
			LikesHobbyRelationship rel2 = new LikesHobbyRelationship(2000);
			rel2.setActive(false);
			rel2.setLocalDate(LocalDate.of(2000, 6, 28));
			rel2.setMyEnum(LikesHobbyRelationship.MyEnum.SOMETHING_DIFFERENT);
			rel2.setPoint(new CartesianPoint2d(2d, 3d));

			assertThat(person.getHobbies()).contains(MapEntry.entry(hobby1, rel1), MapEntry.entry(hobby2, rel2));
		})
		.verifyComplete();

}
 
Example 15
Source File: ReactiveRepositoryIT.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
@Test
void loadEntityWithRelationshipViaQuery(@Autowired ReactiveRelationshipRepository repository) {

	long personId;
	long hobbyNodeId;
	long petNode1Id;
	long petNode2Id;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (n:PersonWithRelationship{name:'Freddie'})-[:Has]->(h1:Hobby{name:'Music'}), "
				+ "(n)-[:Has]->(p1:Pet{name: 'Jerry'}), (n)-[:Has]->(p2:Pet{name: 'Tom'}) "
				+ "RETURN n, h1, p1, p2").single();

		Node personNode = record.get("n").asNode();
		Node hobbyNode1 = record.get("h1").asNode();
		Node petNode1 = record.get("p1").asNode();
		Node petNode2 = record.get("p2").asNode();

		personId = personNode.id();
		hobbyNodeId = hobbyNode1.id();
		petNode1Id = petNode1.id();
		petNode2Id = petNode2.id();
	}

	StepVerifier.create(repository.getPersonWithRelationshipsViaQuery())
		.assertNext(loadedPerson -> {
			assertThat(loadedPerson.getName()).isEqualTo("Freddie");
			assertThat(loadedPerson.getId()).isEqualTo(personId);
			Hobby hobby = loadedPerson.getHobbies();
			assertThat(hobby).isNotNull();
			assertThat(hobby.getId()).isEqualTo(hobbyNodeId);
			assertThat(hobby.getName()).isEqualTo("Music");

			List<Pet> pets = loadedPerson.getPets();
			Pet comparisonPet1 = new Pet(petNode1Id, "Jerry");
			Pet comparisonPet2 = new Pet(petNode2Id, "Tom");
			assertThat(pets).containsExactlyInAnyOrder(comparisonPet1, comparisonPet2);
		})
		.verifyComplete();
}
 
Example 16
Source File: ReactiveRepositoryIT.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
@Test
void loadEntityWithRelationshipToTheSameNode(@Autowired ReactiveRelationshipRepository repository) {

	long personId;
	long hobbyNode1Id;
	long petNode1Id;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (n:PersonWithRelationship{name:'Freddie'})-[:Has]->(h1:Hobby{name:'Music'}), "
				+ "(n)-[:Has]->(p1:Pet{name: 'Jerry'}), "
				+ "(p1)-[:Has]->(h1)"
				+ "RETURN n, h1, p1").single();

		Node personNode = record.get("n").asNode();
		Node hobbyNode1 = record.get("h1").asNode();
		Node petNode1 = record.get("p1").asNode();

		personId = personNode.id();
		hobbyNode1Id = hobbyNode1.id();
		petNode1Id = petNode1.id();
	}

	StepVerifier.create(repository.findById(personId))
		.assertNext(loadedPerson -> {

			assertThat(loadedPerson.getName()).isEqualTo("Freddie");
			Hobby hobby = loadedPerson.getHobbies();
			assertThat(hobby).isNotNull();
			assertThat(hobby.getId()).isEqualTo(hobbyNode1Id);
			assertThat(hobby.getName()).isEqualTo("Music");

			List<Pet> pets = loadedPerson.getPets();
			Pet comparisonPet1 = new Pet(petNode1Id, "Jerry");
			assertThat(pets).containsExactlyInAnyOrder(comparisonPet1);

			Pet pet1 = pets.get(pets.indexOf(comparisonPet1));
			Hobby petHobby = pet1.getHobbies().iterator().next();
			assertThat(petHobby.getName()).isEqualTo("Music");

			assertThat(petHobby).isSameAs(hobby);
		})
		.verifyComplete();
}
 
Example 17
Source File: ReactiveRepositoryIT.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
@Test
void loadEntityWithRelationship(@Autowired ReactiveRelationshipRepository repository) {

	long personId;
	long clubId;
	long hobbyNode1Id;
	long hobbyNode2Id;
	long petNode1Id;
	long petNode2Id;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (n:PersonWithRelationship{name:'Freddie'})-[:Has]->(h1:Hobby{name:'Music'}), "
				+ "(n)-[:Has]->(p1:Pet{name: 'Jerry'}), (n)-[:Has]->(p2:Pet{name: 'Tom'}), "
				+ "(n)<-[:Has]-(c:Club{name:'ClownsClub'}), "
				+ "(p1)-[:Has]->(h2:Hobby{name:'sleeping'}), "
				+ "(p1)-[:Has]->(p2)"
				+ "RETURN n, h1, h2, p1, p2, c").single();

		Node personNode = record.get("n").asNode();
		Node clubNode = record.get("c").asNode();
		Node hobbyNode1 = record.get("h1").asNode();
		Node hobbyNode2 = record.get("h2").asNode();
		Node petNode1 = record.get("p1").asNode();
		Node petNode2 = record.get("p2").asNode();

		personId = personNode.id();
		clubId = clubNode.id();
		hobbyNode1Id = hobbyNode1.id();
		hobbyNode2Id = hobbyNode2.id();
		petNode1Id = petNode1.id();
		petNode2Id = petNode2.id();
	}

	StepVerifier.create(repository.findById(personId))
		.assertNext(loadedPerson -> {

			assertThat(loadedPerson.getName()).isEqualTo("Freddie");
			Hobby hobby = loadedPerson.getHobbies();
			assertThat(hobby).isNotNull();
			assertThat(hobby.getId()).isEqualTo(hobbyNode1Id);
			assertThat(hobby.getName()).isEqualTo("Music");

			Club club = loadedPerson.getClub();
			assertThat(club).isNotNull();
			assertThat(club.getId()).isEqualTo(clubId);
			assertThat(club.getName()).isEqualTo("ClownsClub");

			List<Pet> pets = loadedPerson.getPets();
			Pet comparisonPet1 = new Pet(petNode1Id, "Jerry");
			Pet comparisonPet2 = new Pet(petNode2Id, "Tom");
			assertThat(pets).containsExactlyInAnyOrder(comparisonPet1, comparisonPet2);

			Pet pet1 = pets.get(pets.indexOf(comparisonPet1));
			Pet pet2 = pets.get(pets.indexOf(comparisonPet2));
			Hobby petHobby = pet1.getHobbies().iterator().next();
			assertThat(petHobby.getId()).isEqualTo(hobbyNode2Id);
			assertThat(petHobby.getName()).isEqualTo("sleeping");

			assertThat(pet1.getFriends()).containsExactly(pet2);
		})
		.verifyComplete();
}
 
Example 18
Source File: RepositoryIT.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
@Test
void findEntityWithRelationshipWithPropertiesFromCustomQuery(
	@Autowired PersonWithRelationshipWithPropertiesRepository repository) {

	long personId;
	long hobbyNode1Id;
	long hobbyNode2Id;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (n:PersonWithRelationshipWithProperties{name:'Freddie'}),"
				+ " (n)-[l1:LIKES"
				+ "{since: 1995, active: true, localDate: date('1995-02-26'), myEnum: 'SOMETHING', point: point({x: 0, y: 1})}"
				+ "]->(h1:Hobby{name:'Music'}),"
				+ " (n)-[l2:LIKES"
				+ "{since: 2000, active: false, localDate: date('2000-06-28'), myEnum: 'SOMETHING_DIFFERENT', point: point({x: 2, y: 3})}"
				+ "]->(h2:Hobby{name:'Something else'})"
				+ "RETURN n, h1, h2").single();

		Node personNode = record.get("n").asNode();
		Node hobbyNode1 = record.get("h1").asNode();
		Node hobbyNode2 = record.get("h2").asNode();

		personId = personNode.id();
		hobbyNode1Id = hobbyNode1.id();
		hobbyNode2Id = hobbyNode2.id();
	}

	PersonWithRelationshipWithProperties person = repository.loadFromCustomQuery(personId);
	assertThat(person.getName()).isEqualTo("Freddie");

	Hobby hobby1 = new Hobby();
	hobby1.setName("Music");
	hobby1.setId(hobbyNode1Id);
	LikesHobbyRelationship rel1 = new LikesHobbyRelationship(1995);
	rel1.setActive(true);
	rel1.setLocalDate(LocalDate.of(1995, 2, 26));
	rel1.setMyEnum(LikesHobbyRelationship.MyEnum.SOMETHING);
	rel1.setPoint(new CartesianPoint2d(0d, 1d));

	Hobby hobby2 = new Hobby();
	hobby2.setName("Something else");
	hobby2.setId(hobbyNode2Id);
	LikesHobbyRelationship rel2 = new LikesHobbyRelationship(2000);
	rel2.setActive(false);
	rel2.setLocalDate(LocalDate.of(2000, 6, 28));
	rel2.setMyEnum(LikesHobbyRelationship.MyEnum.SOMETHING_DIFFERENT);
	rel2.setPoint(new CartesianPoint2d(2d, 3d));

	assertThat(person.getHobbies()).contains(MapEntry.entry(hobby1, rel1), MapEntry.entry(hobby2, rel2));
}
 
Example 19
Source File: RepositoryIT.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
@Test
void findEntityWithRelationshipWithProperties(
	@Autowired PersonWithRelationshipWithPropertiesRepository repository) {

	long personId;
	long hobbyNode1Id;
	long hobbyNode2Id;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (n:PersonWithRelationshipWithProperties{name:'Freddie'}),"
				+ " (n)-[l1:LIKES"
				+ "{since: 1995, active: true, localDate: date('1995-02-26'), myEnum: 'SOMETHING', point: point({x: 0, y: 1})}"
				+ "]->(h1:Hobby{name:'Music'}),"
				+ " (n)-[l2:LIKES"
				+ "{since: 2000, active: false, localDate: date('2000-06-28'), myEnum: 'SOMETHING_DIFFERENT', point: point({x: 2, y: 3})}"
				+ "]->(h2:Hobby{name:'Something else'})"
				+ "RETURN n, h1, h2").single();

		Node personNode = record.get("n").asNode();
		Node hobbyNode1 = record.get("h1").asNode();
		Node hobbyNode2 = record.get("h2").asNode();

		personId = personNode.id();
		hobbyNode1Id = hobbyNode1.id();
		hobbyNode2Id = hobbyNode2.id();
	}

	Optional<PersonWithRelationshipWithProperties> optionalPerson = repository.findById(personId);
	assertThat(optionalPerson).isPresent();
	PersonWithRelationshipWithProperties person = optionalPerson.get();
	assertThat(person.getName()).isEqualTo("Freddie");

	Hobby hobby1 = new Hobby();
	hobby1.setName("Music");
	hobby1.setId(hobbyNode1Id);
	LikesHobbyRelationship rel1 = new LikesHobbyRelationship(1995);
	rel1.setActive(true);
	rel1.setLocalDate(LocalDate.of(1995, 2, 26));
	rel1.setMyEnum(LikesHobbyRelationship.MyEnum.SOMETHING);
	rel1.setPoint(new CartesianPoint2d(0d, 1d));

	Hobby hobby2 = new Hobby();
	hobby2.setName("Something else");
	hobby2.setId(hobbyNode2Id);
	LikesHobbyRelationship rel2 = new LikesHobbyRelationship(2000);
	rel2.setActive(false);
	rel2.setLocalDate(LocalDate.of(2000, 6, 28));
	rel2.setMyEnum(LikesHobbyRelationship.MyEnum.SOMETHING_DIFFERENT);
	rel2.setPoint(new CartesianPoint2d(2d, 3d));

	assertThat(person.getHobbies()).contains(MapEntry.entry(hobby1, rel1), MapEntry.entry(hobby2, rel2));
}
 
Example 20
Source File: RepositoryIT.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
@Test
void findEntityWithRelationshipToTheSameNode(@Autowired RelationshipRepository repository) {

	long personId;
	long hobbyNode1Id;
	long petNode1Id;

	try (Session session = createSession()) {
		Record record = session
			.run("CREATE (n:PersonWithRelationship{name:'Freddie'})-[:Has]->(h1:Hobby{name:'Music'}), "
				+ "(n)-[:Has]->(p1:Pet{name: 'Jerry'}), "
				+ "(p1)-[:Has]->(h1)"
				+ "RETURN n, h1, p1").single();

		Node personNode = record.get("n").asNode();
		Node hobbyNode1 = record.get("h1").asNode();
		Node petNode1 = record.get("p1").asNode();

		personId = personNode.id();
		hobbyNode1Id = hobbyNode1.id();
		petNode1Id = petNode1.id();
	}

	PersonWithRelationship loadedPerson = repository.findById(personId).get();
	assertThat(loadedPerson.getName()).isEqualTo("Freddie");
	Hobby hobby = loadedPerson.getHobbies();
	assertThat(hobby).isNotNull();
	assertThat(hobby.getId()).isEqualTo(hobbyNode1Id);
	assertThat(hobby.getName()).isEqualTo("Music");

	List<Pet> pets = loadedPerson.getPets();
	Pet comparisonPet1 = new Pet(petNode1Id, "Jerry");
	assertThat(pets).containsExactlyInAnyOrder(comparisonPet1);

	Pet pet1 = pets.get(pets.indexOf(comparisonPet1));
	Hobby petHobby = pet1.getHobbies().iterator().next();
	assertThat(petHobby.getName()).isEqualTo("Music");

	assertThat(petHobby).isSameAs(hobby);

}