Skip to content

Commit

Permalink
Use Neo4jManagedTypes to populate the mapping context.
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-simons committed Sep 25, 2023
1 parent 32c91af commit 6efb490
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.neo4j.aot.Neo4jManagedTypes;
import org.springframework.data.neo4j.core.DatabaseSelectionProvider;
import org.springframework.data.neo4j.core.Neo4jClient;
import org.springframework.data.neo4j.core.Neo4jOperations;
Expand Down Expand Up @@ -71,12 +72,19 @@ public Neo4jConversions neo4jConversions() {

@Bean
@ConditionalOnMissingBean
public Neo4jMappingContext neo4jMappingContext(ApplicationContext applicationContext,
Neo4jConversions neo4jConversions) throws ClassNotFoundException {
Neo4jManagedTypes neo4jManagedTypes(ApplicationContext applicationContext) throws ClassNotFoundException {
Set<Class<?>> initialEntityClasses = new EntityScanner(applicationContext).scan(Node.class,
RelationshipProperties.class);
return Neo4jManagedTypes.fromIterable(initialEntityClasses);
}

@Bean
@ConditionalOnMissingBean
public Neo4jMappingContext neo4jMappingContext(Neo4jManagedTypes managedTypes,
Neo4jConversions neo4jConversions) {

Neo4jMappingContext context = new Neo4jMappingContext(neo4jConversions);
context.setInitialEntitySet(initialEntityClasses);
context.setManagedTypes(managedTypes);
return context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
package org.springframework.boot.test.autoconfigure.data.neo4j;

import java.time.Duration;
import java.util.Map;
import java.util.Optional;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.neo4j.driver.Driver;
import org.testcontainers.containers.Neo4jContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.neo4j.ParentNode.ChildNode;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -61,6 +66,15 @@ class DataNeo4jTestIntegrationTests {
@Autowired
private ApplicationContext applicationContext;

@BeforeAll
static void prepareData(@Autowired Driver driver) {
try (var session = driver.session(); var tx = session.beginTransaction()) {
tx.run("MATCH (n:ParentLabel:ChildLabel) DETACH DELETE n");
tx.run("CREATE (:ParentLabel:ChildLabel {id: randomUuid(), name: 'BothLabelsMustBeManagedTypes'})");
tx.commit();
}
}

@Test
void testRepository() {
ExampleGraph exampleGraph = new ExampleGraph("Look, new @DataNeo4jTest!");
Expand All @@ -70,6 +84,19 @@ void testRepository() {
assertThat(this.neo4jTemplate.count(ExampleGraph.class)).isOne();
}

// This test will fail in native mode without using Neo4jManagedTypes in the configuration
@Test
void typeInheritanceShouldWorkOnNative() {

Optional<ParentNode> optionalResult = this.neo4jTemplate.findOne("MATCH (t:ParentLabel {name: $name}) RETURN t", Map.of("name", "BothLabelsMustBeManagedTypes"), ParentNode.class);
assertThat(optionalResult)
.isPresent()
.hasValueSatisfying((node) -> {
assertThat(node).isInstanceOf(ChildNode.class);
assertThat(node.getId()).isNotNull();
});
}

@Test
void didNotInjectExampleService() {
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2023 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.boot.test.autoconfigure.data.neo4j;

import java.util.UUID;

import org.springframework.boot.test.autoconfigure.data.neo4j.ParentNode.ChildNode;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;

/**
* @author Michael Simons
*/
@Node("ParentLabel")
public abstract sealed class ParentNode permits ChildNode {

@Id
@GeneratedValue
private UUID id;

private String name;

public UUID getId() {
return this.id;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

@Node("ChildLabel")
public static final class ChildNode extends ParentNode {
}
}

0 comments on commit 6efb490

Please sign in to comment.