|
| 1 | +/* |
| 2 | + * Copyright 2011-2022 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.gh2474; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.neo4j.driver.Driver; |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Configuration; |
| 29 | +import org.springframework.data.domain.Sort; |
| 30 | +import org.springframework.data.neo4j.config.AbstractNeo4jConfig; |
| 31 | +import org.springframework.data.neo4j.core.DatabaseSelectionProvider; |
| 32 | +import org.springframework.data.neo4j.core.transaction.Neo4jBookmarkManager; |
| 33 | +import org.springframework.data.neo4j.core.transaction.Neo4jTransactionManager; |
| 34 | +import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; |
| 35 | +import org.springframework.data.neo4j.test.BookmarkCapture; |
| 36 | +import org.springframework.data.neo4j.test.Neo4jExtension; |
| 37 | +import org.springframework.data.neo4j.test.Neo4jIntegrationTest; |
| 38 | +import org.springframework.transaction.PlatformTransactionManager; |
| 39 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author Stephen Jackson |
| 43 | + * @author Michael J. Simons |
| 44 | + */ |
| 45 | +@Neo4jIntegrationTest |
| 46 | +public class GH2474IT { |
| 47 | + |
| 48 | + protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport; |
| 49 | + |
| 50 | + @Autowired |
| 51 | + Driver driver; |
| 52 | + |
| 53 | + @Autowired |
| 54 | + BookmarkCapture bookmarkCapture; |
| 55 | + |
| 56 | + @Autowired |
| 57 | + CityModelRepository cityModelRepository; |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + void setupData() { |
| 61 | + |
| 62 | + cityModelRepository.deleteAll(); |
| 63 | + |
| 64 | + CityModel aachen = new CityModel(); |
| 65 | + aachen.setName("Aachen"); |
| 66 | + aachen.setExoticProperty("Cars"); |
| 67 | + |
| 68 | + CityModel utrecht = new CityModel(); |
| 69 | + utrecht.setName("Utrecht"); |
| 70 | + utrecht.setExoticProperty("Bikes"); |
| 71 | + |
| 72 | + cityModelRepository.saveAll(Arrays.asList(aachen, utrecht)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testStoreExoticProperty() { |
| 77 | + |
| 78 | + CityModel cityModel = new CityModel(); |
| 79 | + cityModel.setName("The Jungle"); |
| 80 | + cityModel.setExoticProperty("lions"); |
| 81 | + cityModel = cityModelRepository.save(cityModel); |
| 82 | + |
| 83 | + CityModel reloaded = cityModelRepository.findById(cityModel.getCityId()) |
| 84 | + .orElseThrow(RuntimeException::new); |
| 85 | + assertThat(reloaded.getExoticProperty()).isEqualTo("lions"); |
| 86 | + |
| 87 | + long cnt = cityModelRepository.deleteAllByExoticProperty("lions"); |
| 88 | + assertThat(cnt).isOne(); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testSortOnExoticProperty() { |
| 93 | + |
| 94 | + Sort sort = Sort.by(Sort.Order.asc("exoticProperty")); |
| 95 | + List<CityModel> cityModels = cityModelRepository.findAll(sort); |
| 96 | + |
| 97 | + assertThat(cityModels).extracting(CityModel::getExoticProperty).containsExactly("Bikes", "Cars"); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testSortOnExoticPropertyCustomQuery_MakeSureIUnderstand() { |
| 102 | + |
| 103 | + Sort sort = Sort.by(Sort.Order.asc("n.name")); |
| 104 | + List<CityModel> cityModels = cityModelRepository.customQuery(sort); |
| 105 | + |
| 106 | + assertThat(cityModels).extracting(CityModel::getExoticProperty).containsExactly("Cars", "Bikes"); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testSortOnExoticPropertyCustomQuery() { |
| 111 | + Sort sort = Sort.by(Sort.Order.asc("n.`exotic.property`")); |
| 112 | + List<CityModel> cityModels = cityModelRepository.customQuery(sort); |
| 113 | + |
| 114 | + assertThat(cityModels).extracting(CityModel::getExoticProperty).containsExactly("Bikes", "Cars"); |
| 115 | + } |
| 116 | + |
| 117 | + @Configuration |
| 118 | + @EnableTransactionManagement |
| 119 | + @EnableNeo4jRepositories |
| 120 | + static class Config extends AbstractNeo4jConfig { |
| 121 | + |
| 122 | + @Bean |
| 123 | + public BookmarkCapture bookmarkCapture() { |
| 124 | + return new BookmarkCapture(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public PlatformTransactionManager transactionManager( |
| 129 | + Driver driver, DatabaseSelectionProvider databaseNameProvider) { |
| 130 | + |
| 131 | + BookmarkCapture bookmarkCapture = bookmarkCapture(); |
| 132 | + return new Neo4jTransactionManager(driver, databaseNameProvider, |
| 133 | + Neo4jBookmarkManager.create(bookmarkCapture)); |
| 134 | + } |
| 135 | + |
| 136 | + @Bean |
| 137 | + public Driver driver() { |
| 138 | + |
| 139 | + return neo4jConnectionSupport.getDriver(); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments