|
| 1 | +/* |
| 2 | + * Copyright (c) 2019-2020 "Neo4j," |
| 3 | + * Neo4j Sweden AB [https://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.doc.driver.springframework.boot.ogm_integration.domain; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.*; |
| 22 | + |
| 23 | +import java.util.Optional; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.neo4j.driver.Driver; |
| 27 | +import org.neo4j.driver.springframework.boot.autoconfigure.Neo4jDriverAutoConfiguration; |
| 28 | +import org.neo4j.driver.springframework.boot.test.autoconfigure.Neo4jTestHarnessAutoConfiguration; |
| 29 | +import org.neo4j.harness.ServerControls; |
| 30 | +import org.neo4j.harness.TestServerBuilders; |
| 31 | +import org.neo4j.ogm.drivers.bolt.driver.BoltDriver; |
| 32 | +import org.neo4j.ogm.session.SessionFactory; |
| 33 | +import org.springframework.beans.factory.annotation.Autowired; |
| 34 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 35 | +import org.springframework.boot.test.autoconfigure.data.neo4j.DataNeo4jTest; |
| 36 | +import org.springframework.boot.test.context.TestConfiguration; |
| 37 | +import org.springframework.context.annotation.Bean; |
| 38 | + |
| 39 | +/** |
| 40 | + * This tests shows that the Neo4j-OGM with the Java Driver autoconfiguration + test harness works fine with test slices. |
| 41 | + * |
| 42 | + * @author Michael J. Simons |
| 43 | + * @soundtrack Katey Sagal - Covered |
| 44 | + */ |
| 45 | +@DataNeo4jTest |
| 46 | +// This is necessary, as the test slice cannot know about it |
| 47 | +@ImportAutoConfiguration({ Neo4jTestHarnessAutoConfiguration.class, Neo4jDriverAutoConfiguration.class }) |
| 48 | +class TestSliceTestWithHarness { |
| 49 | + |
| 50 | + @TestConfiguration |
| 51 | + static class TestHarnessConfig { |
| 52 | + |
| 53 | + @Bean |
| 54 | + public ServerControls neo4j() { |
| 55 | + return TestServerBuilders.newInProcessBuilder() |
| 56 | + .withFixture("" |
| 57 | + + "CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})\n" |
| 58 | + + "CREATE (TheMatrixReloaded:Movie {title:'The Matrix Reloaded', released:2003, tagline:'Free your mind'})\n" |
| 59 | + + "CREATE (TheMatrixRevolutions:Movie {title:'The Matrix Revolutions', released:2003, tagline:'Everything that has a beginning has an end'})\n" |
| 60 | + ) |
| 61 | + .newServer(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void sessionFactoryShouldUseOGMBoltDriver(@Autowired Driver driver, |
| 67 | + @Autowired SessionFactory sessionFactory) { |
| 68 | + assertThat(sessionFactory.unwrap(org.neo4j.ogm.driver.Driver.class)).isInstanceOf(BoltDriver.class); |
| 69 | + |
| 70 | + BoltDriver boltDriver = (BoltDriver) sessionFactory.unwrap(org.neo4j.ogm.driver.Driver.class); |
| 71 | + assertThat(boltDriver.unwrap(Driver.class)).isSameAs(driver); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void repositoriesShouldWork(@Autowired MovieRepository movieRepository) { |
| 76 | + |
| 77 | + MovieEntity movieEntity = movieRepository.save(new MovieEntity("Life of Brian")); |
| 78 | + assertThat(movieEntity.getId()).isNotNull(); |
| 79 | + |
| 80 | + String title = "The Matrix"; |
| 81 | + Optional<MovieEntity> theMatrix = movieRepository.findByTitle(title); |
| 82 | + assertThat(theMatrix).isPresent().map(MovieEntity::getTitle).hasValue(title); |
| 83 | + } |
| 84 | +} |
0 commit comments