/* * Copyright 2011-2021 the original author or authors. * * 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.springframework.data.neo4j.integration.imperative; import static org.assertj.core.api.Assertions.assertThat; import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; import org.neo4j.driver.Driver; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.neo4j.config.AbstractNeo4jConfig; import org.springframework.data.neo4j.core.DatabaseSelectionProvider; import org.springframework.data.neo4j.core.Neo4jClient; import org.springframework.data.neo4j.integration.shared.common.AltHobby; import org.springframework.data.neo4j.integration.shared.common.AltLikedByPersonRelationship; import org.springframework.data.neo4j.integration.shared.common.AltPerson; import org.springframework.data.neo4j.repository.Neo4jRepository; import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; import org.springframework.data.neo4j.test.Neo4jExtension; import org.springframework.data.neo4j.test.Neo4jIntegrationTest; import org.springframework.test.annotation.Commit; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Transactional; @Neo4jIntegrationTest @EnabledIfEnvironmentVariable(named = "SDN_NEO4J_URL", matches = ".*") @EnabledIfEnvironmentVariable(named = "SDN_NEO4J_PASSWORD", matches = ".*") public class DatabaseSelectionProviderIT { protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport; protected Neo4jClient neo4jClient; private HobbyWithRelationshipWithPropertiesRepository repository; private Long id; protected DatabaseSelectionProviderIT(@Autowired Neo4jClient neo4jClient, @Autowired HobbyWithRelationshipWithPropertiesRepository repository) { this.neo4jClient = neo4jClient; this.repository = repository; } @BeforeEach protected void setUp() { neo4jClient.query("MATCH (n) DETACH DELETE n").in("neo4jtest").run(); //neo4jClient.query("MATCH (n) DETACH DELETE n").run(); } @Test @Transactional //@Commit void saveSameNodeWithDoubleRelationship() { AltHobby hobby = new AltHobby(); hobby.setName("Music"); hobby = repository.save(hobby); AltPerson altPerson = new AltPerson("Freddie"); AltLikedByPersonRelationship rel1 = new AltLikedByPersonRelationship(); rel1.setRating(5); rel1.setAltPerson(altPerson); AltLikedByPersonRelationship rel2 = new AltLikedByPersonRelationship(); rel2.setRating(1); rel2.setAltPerson(altPerson); hobby.getLikedBy().add(rel1); hobby.getLikedBy().add(rel2); id = repository.save(hobby).getId(); Optional optHobby = repository.findById(id); hobby = optHobby.get(); assertThat(hobby.getName()).isEqualTo("Music"); } interface HobbyWithRelationshipWithPropertiesRepository extends Neo4jRepository{}; @Configuration @EnableNeo4jRepositories(considerNestedRepositories = true) @EnableTransactionManagement static class Config extends AbstractNeo4jConfig { @Bean public Driver driver() { return neo4jConnectionSupport.getDriver(); } @Override @Bean protected DatabaseSelectionProvider databaseSelectionProvider() { return DatabaseSelectionProvider.createStaticDatabaseSelectionProvider("neo4jtest"); } } }