From 4a7afb85ddc2312a00b389d2919e8f27224cccfd Mon Sep 17 00:00:00 2001 From: Michael Simons Date: Thu, 18 Jun 2020 16:28:38 +0200 Subject: [PATCH] Convert id values if necessary. --- .../data/core/Neo4jTemplate.java | 4 +- .../data/core/ReactiveNeo4jTemplate.java | 4 +- .../imperative/TypeConversionIT.java | 14 ++ .../reactive/ReactiveTypeConversionIT.java | 122 ++++++++++++++++++ .../integration/shared/ThingWithUUIDID.java | 10 ++ 5 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 spring-data-neo4j/src/test/java/org/neo4j/springframework/data/integration/reactive/ReactiveTypeConversionIT.java diff --git a/spring-data-neo4j/src/main/java/org/neo4j/springframework/data/core/Neo4jTemplate.java b/spring-data-neo4j/src/main/java/org/neo4j/springframework/data/core/Neo4jTemplate.java index 43814e7d9..2699dfff4 100644 --- a/spring-data-neo4j/src/main/java/org/neo4j/springframework/data/core/Neo4jTemplate.java +++ b/spring-data-neo4j/src/main/java/org/neo4j/springframework/data/core/Neo4jTemplate.java @@ -438,7 +438,7 @@ public final class Neo4jTemplate implements Neo4jOperations, BeanFactoryAware { neo4jClient.query(renderer.render(relationshipRemoveQuery)) .in(inDatabase) - .bind(fromId).to(FROM_ID_PARAMETER_NAME).run(); + .bind(convertIdValues(fromId)).to(FROM_ID_PARAMETER_NAME).run(); } // nothing to do because there is nothing to map @@ -465,7 +465,7 @@ public final class Neo4jTemplate implements Neo4jOperations, BeanFactoryAware { neo4jClient.query(renderer.render(statementHolder.getRelationshipCreationQuery())) .in(inDatabase) - .bind(fromId).to(FROM_ID_PARAMETER_NAME) + .bind(convertIdValues(fromId)).to(FROM_ID_PARAMETER_NAME) .bindAll(statementHolder.getProperties()) .run(); diff --git a/spring-data-neo4j/src/main/java/org/neo4j/springframework/data/core/ReactiveNeo4jTemplate.java b/spring-data-neo4j/src/main/java/org/neo4j/springframework/data/core/ReactiveNeo4jTemplate.java index 8e0f57309..19caa0e3d 100644 --- a/spring-data-neo4j/src/main/java/org/neo4j/springframework/data/core/ReactiveNeo4jTemplate.java +++ b/spring-data-neo4j/src/main/java/org/neo4j/springframework/data/core/ReactiveNeo4jTemplate.java @@ -445,7 +445,7 @@ public final class ReactiveNeo4jTemplate implements ReactiveNeo4jOperations, Bea relationshipCreationMonos.add( neo4jClient.query(renderer.render(relationshipRemoveQuery)) .in(inDatabase) - .bind(fromId).to(FROM_ID_PARAMETER_NAME) + .bind(convertIdValues(fromId)).to(FROM_ID_PARAMETER_NAME) .run().checkpoint("delete relationships").then()); } @@ -484,7 +484,7 @@ public final class ReactiveNeo4jTemplate implements ReactiveNeo4jOperations, Bea Mono relationshipCreationMonoNested = neo4jClient .query(renderer.render(statementHolder.getRelationshipCreationQuery())) .in(inDatabase) - .bind(fromId).to(FROM_ID_PARAMETER_NAME) + .bind(convertIdValues(fromId)).to(FROM_ID_PARAMETER_NAME) .bindAll(statementHolder.getProperties()) .run(); diff --git a/spring-data-neo4j/src/test/java/org/neo4j/springframework/data/integration/imperative/TypeConversionIT.java b/spring-data-neo4j/src/test/java/org/neo4j/springframework/data/integration/imperative/TypeConversionIT.java index 9f902827a..5f0d33632 100644 --- a/spring-data-neo4j/src/test/java/org/neo4j/springframework/data/integration/imperative/TypeConversionIT.java +++ b/spring-data-neo4j/src/test/java/org/neo4j/springframework/data/integration/imperative/TypeConversionIT.java @@ -198,6 +198,20 @@ class TypeConversionIT extends Neo4jConversionsITBase { assertThat(repository.findById(thing.getId())).isPresent(); } + @Test + void relatedIdsShouldBeConverted(@Autowired ConvertedIDsRepository repository) { + + ThingWithUUIDID aThing = new ThingWithUUIDID("a thing"); + aThing.setAnotherThing(new ThingWithUUIDID("Another thing")); + + ThingWithUUIDID savedThing = repository.save(aThing); + + assertThat(savedThing.getId()).isNotNull(); + assertThat(repository.findById(savedThing.getId())).isPresent(); + assertThat(savedThing.getAnotherThing().getId()).isNotNull(); + assertThat(repository.findById(savedThing.getAnotherThing().getId())).isPresent(); + } + public interface ConvertedIDsRepository extends Neo4jRepository { } diff --git a/spring-data-neo4j/src/test/java/org/neo4j/springframework/data/integration/reactive/ReactiveTypeConversionIT.java b/spring-data-neo4j/src/test/java/org/neo4j/springframework/data/integration/reactive/ReactiveTypeConversionIT.java new file mode 100644 index 000000000..09a1c7dc8 --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/neo4j/springframework/data/integration/reactive/ReactiveTypeConversionIT.java @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2019-2020 "Neo4j," + * Neo4j Sweden AB [https://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.neo4j.springframework.data.integration.reactive; + +import static org.assertj.core.api.Assertions.*; +import static org.neo4j.springframework.data.test.Neo4jExtension.*; + +import reactor.core.publisher.Flux; +import reactor.test.StepVerifier; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.neo4j.driver.Driver; +import org.neo4j.springframework.data.config.AbstractReactiveNeo4jConfig; +import org.neo4j.springframework.data.core.convert.Neo4jConversions; +import org.neo4j.springframework.data.integration.shared.ThingWithCustomTypes; +import org.neo4j.springframework.data.integration.shared.ThingWithUUIDID; +import org.neo4j.springframework.data.repository.ReactiveNeo4jRepository; +import org.neo4j.springframework.data.repository.config.EnableReactiveNeo4jRepositories; +import org.neo4j.springframework.data.test.Neo4jExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +/** + * @author Michael J. Simons + * @soundtrack Tom Morello - The Atlas Underground + */ +@ExtendWith(Neo4jExtension.class) +@SpringJUnitConfig +@DirtiesContext +@Tag(NEEDS_REACTIVE_SUPPORT) +class ReactiveTypeConversionIT { + + protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport; + + private final Driver driver; + + @Autowired ReactiveTypeConversionIT(Driver driver) { + this.driver = driver; + } + + @Test + void idsShouldBeConverted(@Autowired ConvertedIDsRepository repository) { + + List stored = new ArrayList<>(); + StepVerifier.create(repository.save(new ThingWithUUIDID("a thing"))) + .recordWith(() -> stored) + .expectNextCount(1L) + .verifyComplete(); + + StepVerifier.create(repository.findById(stored.get(0).getId())) + .expectNextCount(1L) + .verifyComplete(); + } + + @Test + void relatedIdsShouldBeConverted(@Autowired ConvertedIDsRepository repository) { + + ThingWithUUIDID aThing = new ThingWithUUIDID("a thing"); + aThing.setAnotherThing(new ThingWithUUIDID("Another thing")); + + List stored = new ArrayList<>(); + StepVerifier.create(repository.save(aThing)) + .recordWith(() -> stored) + .expectNextCount(1L) + .verifyComplete(); + + ThingWithUUIDID savedThing = stored.get(0); + assertThat(savedThing.getId()).isNotNull(); + assertThat(savedThing.getAnotherThing().getId()).isNotNull(); + + StepVerifier.create(Flux.concat(repository.findById(savedThing.getId()), + repository.findById(savedThing.getAnotherThing().getId()))) + .expectNextCount(2L) + .verifyComplete(); + } + + public interface ConvertedIDsRepository extends ReactiveNeo4jRepository { + } + + @Configuration + @EnableReactiveNeo4jRepositories(considerNestedRepositories = true) + @EnableTransactionManagement + static class Config extends AbstractReactiveNeo4jConfig { + + @Bean + public Driver driver() { + return neo4jConnectionSupport.getDriver(); + } + + @Override + public Neo4jConversions neo4jConversions() { + return new Neo4jConversions(Collections.singleton(new ThingWithCustomTypes.CustomTypeConverter())); + } + } +} diff --git a/spring-data-neo4j/src/test/java/org/neo4j/springframework/data/integration/shared/ThingWithUUIDID.java b/spring-data-neo4j/src/test/java/org/neo4j/springframework/data/integration/shared/ThingWithUUIDID.java index 441242887..c9e6100a3 100644 --- a/spring-data-neo4j/src/test/java/org/neo4j/springframework/data/integration/shared/ThingWithUUIDID.java +++ b/spring-data-neo4j/src/test/java/org/neo4j/springframework/data/integration/shared/ThingWithUUIDID.java @@ -36,6 +36,8 @@ public class ThingWithUUIDID { private String name; + private ThingWithUUIDID anotherThing; + public ThingWithUUIDID(String name) { this.name = name; } @@ -47,4 +49,12 @@ public class ThingWithUUIDID { public String getName() { return name; } + + public ThingWithUUIDID getAnotherThing() { + return anotherThing; + } + + public void setAnotherThing(ThingWithUUIDID anotherThing) { + this.anotherThing = anotherThing; + } }