|
| 1 | +/* |
| 2 | + * Copyright 2011-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.neo4j.integration.issues.gh2168; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.BeforeAll; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.neo4j.driver.Driver; |
| 26 | +import org.neo4j.driver.Session; |
| 27 | +import org.neo4j.driver.Transaction; |
| 28 | +import org.neo4j.driver.types.Node; |
| 29 | +import org.springframework.beans.factory.annotation.Autowired; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.data.neo4j.config.AbstractNeo4jConfig; |
| 33 | +import org.springframework.data.neo4j.repository.Neo4jRepository; |
| 34 | +import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; |
| 35 | +import org.springframework.data.neo4j.test.Neo4jExtension; |
| 36 | +import org.springframework.data.neo4j.test.Neo4jIntegrationTest; |
| 37 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author Michael J. Simons |
| 41 | + */ |
| 42 | +@Neo4jIntegrationTest |
| 43 | +public class Gh2168IT { |
| 44 | + |
| 45 | + protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport; |
| 46 | + |
| 47 | + @BeforeAll |
| 48 | + protected static void setupData() { |
| 49 | + try (Transaction transaction = neo4jConnectionSupport.getDriver().session().beginTransaction()) { |
| 50 | + transaction.run("MATCH (n) detach delete n"); |
| 51 | + transaction.run("CREATE (:DomainObject{id: 'A'})"); |
| 52 | + transaction.commit(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test // GH-2168 |
| 57 | + void findByIdShouldWork(@Autowired DomainObjectRepository domainObjectRepository) { |
| 58 | + |
| 59 | + Optional<DomainObject> optionalResult = domainObjectRepository.findById("A"); |
| 60 | + assertThat(optionalResult) |
| 61 | + .map(DomainObject::getId) |
| 62 | + .hasValue("A"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test // GH-2168 |
| 66 | + void compositePropertyCustomConverterDefaultPrefixShouldWork( |
| 67 | + @Autowired DomainObjectRepository repository, |
| 68 | + @Autowired Driver driver |
| 69 | + ) { |
| 70 | + |
| 71 | + DomainObject domainObject = new DomainObject(); |
| 72 | + domainObject.setStoredAsMultipleProperties(new UnrelatedObject(true, 4711L)); |
| 73 | + domainObject = repository.save(domainObject); |
| 74 | + |
| 75 | + try (Session session = driver.session()) { |
| 76 | + Node node = session |
| 77 | + .run("MATCH (n:DomainObject {id: $id}) RETURN n", Collections.singletonMap("id", domainObject.getId())) |
| 78 | + .single().get(0).asNode(); |
| 79 | + assertThat(node.get("storedAsMultipleProperties.aBooleanValue").asBoolean()).isTrue(); |
| 80 | + assertThat(node.get("storedAsMultipleProperties.aLongValue").asLong()).isEqualTo(4711L); |
| 81 | + } |
| 82 | + |
| 83 | + domainObject = repository.findById(domainObject.getId()).get(); |
| 84 | + assertThat(domainObject.getStoredAsMultipleProperties()) |
| 85 | + .satisfies(t -> { |
| 86 | + assertThat(t.isABooleanValue()).isTrue(); |
| 87 | + assertThat(t.getALongValue()).isEqualTo(4711L); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + // That test and the underlying mapping cause the original issue to fail, as `@ConvertWith` was missing for non-simple |
| 94 | + // types in the lookup that checked whether something is an association or not |
| 95 | + @Test // GH-2168 |
| 96 | + void propertyCustomConverterDefaultPrefixShouldWork( |
| 97 | + @Autowired DomainObjectRepository repository, |
| 98 | + @Autowired Driver driver |
| 99 | + ) { |
| 100 | + |
| 101 | + DomainObject domainObject = new DomainObject(); |
| 102 | + domainObject.setStoredAsSingleProperty(new UnrelatedObject(true, 4711L)); |
| 103 | + domainObject = repository.save(domainObject); |
| 104 | + |
| 105 | + try (Session session = driver.session()) { |
| 106 | + Node node = session |
| 107 | + .run("MATCH (n:DomainObject {id: $id}) RETURN n", Collections.singletonMap("id", domainObject.getId())) |
| 108 | + .single().get(0).asNode(); |
| 109 | + assertThat(node.get("storedAsSingleProperty").asString()).isEqualTo("true;4711"); |
| 110 | + } |
| 111 | + |
| 112 | + domainObject = repository.findById(domainObject.getId()).get(); |
| 113 | + assertThat(domainObject.getStoredAsSingleProperty()) |
| 114 | + .satisfies(t -> { |
| 115 | + assertThat(t.isABooleanValue()).isTrue(); |
| 116 | + assertThat(t.getALongValue()).isEqualTo(4711L); |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + interface DomainObjectRepository extends Neo4jRepository<DomainObject, String> { |
| 121 | + } |
| 122 | + |
| 123 | + @Configuration |
| 124 | + @EnableTransactionManagement |
| 125 | + @EnableNeo4jRepositories(considerNestedRepositories = true) |
| 126 | + static class Config extends AbstractNeo4jConfig { |
| 127 | + |
| 128 | + @Bean |
| 129 | + public Driver driver() { |
| 130 | + |
| 131 | + return neo4jConnectionSupport.getDriver(); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments