use of org.springframework.data.neo4j.integration.shared.common.Person in project spring-data-neo4j by spring-projects.
the class DynamicRelationshipsIT method shouldWriteDynamicRelationships.
// DATAGRAPH-1447
@Test
void shouldWriteDynamicRelationships(@Autowired PersonWithRelativesRepository repository) {
PersonWithRelatives newPerson = new PersonWithRelatives("Test");
Map<TypeOfRelative, Person> relatives = newPerson.getRelatives();
Person d = new Person();
ReflectionTestUtils.setField(d, "firstName", "R1");
relatives.put(TypeOfRelative.RELATIVE_1, d);
d = new Person();
ReflectionTestUtils.setField(d, "firstName", "R2");
relatives.put(TypeOfRelative.RELATIVE_2, d);
Map<TypeOfClub, ClubRelationship> clubs = newPerson.getClubs();
ClubRelationship clubRelationship = new ClubRelationship("Brunswick");
Club club1 = new Club();
club1.setName("BTSV");
clubRelationship.setClub(club1);
clubs.put(TypeOfClub.FOOTBALL, clubRelationship);
clubRelationship = new ClubRelationship("Boston");
Club club2 = new Club();
club2.setName("Red Sox");
clubRelationship.setClub(club2);
clubs.put(TypeOfClub.BASEBALL, clubRelationship);
newPerson = repository.findById(repository.save(newPerson).getId()).get();
assertThat(newPerson.getRelatives()).containsOnlyKeys(TypeOfRelative.RELATIVE_1, TypeOfRelative.RELATIVE_2);
assertThat(newPerson.getClubs()).containsOnlyKeys(TypeOfClub.BASEBALL, TypeOfClub.FOOTBALL);
try (Transaction transaction = driver.session().beginTransaction()) {
long numberOfRelations = transaction.run(("MATCH (t:%s) WHERE id(t) = $id RETURN size((t)-->(:Person)) as numberOfRelations").formatted(labelOfTestSubject), Values.parameters("id", newPerson.getId())).single().get("numberOfRelations").asLong();
assertThat(numberOfRelations).isEqualTo(2L);
numberOfRelations = transaction.run(("MATCH (t:%s) WHERE id(t) = $id RETURN size((t)-->(:Club)) as numberOfRelations").formatted(labelOfTestSubject), Values.parameters("id", newPerson.getId())).single().get("numberOfRelations").asLong();
assertThat(numberOfRelations).isEqualTo(2L);
}
}
use of org.springframework.data.neo4j.integration.shared.common.Person in project spring-data-neo4j by spring-projects.
the class DynamicRelationshipsIT method shouldReadDynamicRelationshipsWithCustomQuery.
// DATAGRAPH-1411
@Test
void shouldReadDynamicRelationshipsWithCustomQuery(@Autowired PersonWithRelativesRepository repository) {
PersonWithRelatives person = repository.byCustomQuery(idOfExistingPerson);
assertThat(person).isNotNull();
assertThat(person.getName()).isEqualTo("A");
Map<TypeOfRelative, Person> relatives = person.getRelatives();
assertThat(relatives).containsOnlyKeys(TypeOfRelative.HAS_WIFE, TypeOfRelative.HAS_DAUGHTER);
assertThat(relatives.get(TypeOfRelative.HAS_WIFE).getFirstName()).isEqualTo("B");
assertThat(relatives.get(TypeOfRelative.HAS_DAUGHTER).getFirstName()).isEqualTo("C");
Map<TypeOfClub, ClubRelationship> clubs = person.getClubs();
assertThat(clubs).containsOnlyKeys(TypeOfClub.FOOTBALL);
assertThat(clubs.get(TypeOfClub.FOOTBALL).getPlace()).isEqualTo("Brunswick");
assertThat(clubs.get(TypeOfClub.FOOTBALL).getClub().getName()).isEqualTo("BTSV");
}
use of org.springframework.data.neo4j.integration.shared.common.Person in project spring-data-neo4j by spring-projects.
the class Neo4jTemplateIT method saveAllAsWithDynamicProjectionOnSecondLevelShouldWork.
// GH-2420
@Test
void saveAllAsWithDynamicProjectionOnSecondLevelShouldWork() {
Person p = neo4jTemplate.findOne("MATCH (p:Person {lastName: $lastName})-[r:LIVES_AT]-(a:Address) RETURN p, collect(r), collect(a)", Collections.singletonMap("lastName", "Siemons"), Person.class).get();
p.setFirstName("Klaus");
p.setLastName("Simons");
p.getAddress().setCity("Braunschweig");
p.getAddress().setStreet("Single Trail");
Person.Address.Country country = new Person.Address.Country();
country.setName("Foo");
country.setCountryCode("DE");
p.getAddress().setCountry(country);
BiPredicate<PropertyPath, Neo4jPersistentProperty> predicate = create2LevelProjectingPredicate();
List<Person> projections = neo4jTemplate.saveAllAs(Collections.singletonList(p), predicate);
assertThat(projections).hasSize(1).first().satisfies(projection -> {
assertThat(projection.getAddress().getStreet()).isEqualTo("Single Trail");
assertThat(projection.getAddress().getCountry().getName()).isEqualTo("Foo");
});
p = neo4jTemplate.findById(p.getId(), Person.class).get();
assertThat(p.getFirstName()).isEqualTo("Michael");
assertThat(p.getLastName()).isEqualTo("Simons");
assertThat(p.getAddress().getCity()).isEqualTo("Aachen");
assertThat(p.getAddress().getStreet()).isEqualTo("Single Trail");
assertThat(p.getAddress().getCountry().getName()).isEqualTo("Foo");
}
use of org.springframework.data.neo4j.integration.shared.common.Person in project spring-data-neo4j by spring-projects.
the class Neo4jTemplateIT method saveProjectionShouldWork.
// GH-2215
@Test
void saveProjectionShouldWork() {
// Using a query on purpose so that the address is null
DtoPersonProjection dtoPersonProjection = neo4jTemplate.find(Person.class).as(DtoPersonProjection.class).matching("MATCH (p:Person {lastName: $lastName}) RETURN p", Collections.singletonMap("lastName", "Siemons")).one().get();
dtoPersonProjection.setFirstName("Micha");
dtoPersonProjection.setLastName("Simons");
DtoPersonProjection savedProjection = neo4jTemplate.save(Person.class).one(dtoPersonProjection);
// Assert that we saved and returned the correct data
assertThat(savedProjection.getFirstName()).isEqualTo("Micha");
assertThat(savedProjection.getLastName()).isEqualTo("Simons");
// Assert the result inside the database.
Person person = neo4jTemplate.findById(savedProjection.getId(), Person.class).get();
assertThat(person.getFirstName()).isEqualTo("Micha");
assertThat(person.getLastName()).isEqualTo("Simons");
assertThat(person.getAddress()).isNotNull();
}
use of org.springframework.data.neo4j.integration.shared.common.Person in project spring-data-neo4j by spring-projects.
the class Neo4jTemplateIT method statementWithParamsShouldWork.
@Test
void statementWithParamsShouldWork() {
Node person = Cypher.node("Person");
List<Person> people = neo4jTemplate.find(Person.class).matching(Cypher.match(person).where(person.property("lastName").isEqualTo(Cypher.parameter("lastName", "Siemons"))).returning(person).build(), Collections.singletonMap("lastName", "Schnitzel")).all();
assertThat(people).extracting(Person::getLastName).containsExactly("Schnitzel");
}
Aggregations