DATAGRAPH-1156 - Verify custom ID types for related nodes.

This commit is contained in:
Michael Simons
2020-08-27 09:34:48 +02:00
parent c7c100bff9
commit ebdf0f18a3
5 changed files with 195 additions and 4 deletions

View File

@@ -15,7 +15,7 @@
*/
package org.springframework.data.neo4j.conversion;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.Map;
@@ -26,12 +26,17 @@ import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.conversion.datagraph1156.Hobby;
import org.springframework.data.neo4j.conversion.datagraph1156.HobbyRepository;
import org.springframework.data.neo4j.conversion.datagraph1156.User;
import org.springframework.data.neo4j.conversion.datagraph1156.UserRepository;
import org.springframework.data.neo4j.conversion.support.ConvertedClass;
import org.springframework.data.neo4j.conversion.support.EntityRepository;
import org.springframework.data.neo4j.conversion.support.EntityWithConvertedAttributes;
import org.springframework.data.neo4j.test.Neo4jIntegrationTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.support.TransactionTemplate;
/**
* @author Michael J. Simons
@@ -45,6 +50,12 @@ public class MetaDataDrivenConversionServiceIntegrationTests {
@Autowired private EntityRepository entityRepository;
@Autowired private UserRepository userRepository;
@Autowired private HobbyRepository hobbyRepository;
@Autowired private TransactionTemplate transactionTemplate;
@Test // DATAGRAPH-1131
public void conversionWithConverterHierarchyShouldWork() {
@@ -68,8 +79,27 @@ public class MetaDataDrivenConversionServiceIntegrationTests {
}
@Test // DATAGRAPH-1156
public void relatedNodesWithCustomIdsShouldWork() {
User testUser = transactionTemplate.execute(tx -> userRepository.save(new User("test@test.com")));
User user = userRepository.findById(testUser.getId())
.orElseThrow(() -> new RuntimeException("User not saved."));
Hobby newHobby = transactionTemplate.execute(tx -> hobbyRepository.save(new Hobby("Cycling", user)));
assertThat(hobbyRepository.findById(newHobby.getId())).isPresent().hasValueSatisfying(hobby -> {
assertThat(hobby.getId()).isEqualTo(newHobby.getId());
assertThat(hobby.getHobbyist()).extracting(User::getId).isEqualTo(user.getId());
});
}
@Configuration
@Neo4jIntegrationTest(domainPackages = "org.springframework.data.neo4j.conversion.support",
repositoryPackages = "org.springframework.data.neo4j.conversion.support")
static class Config {}
@Neo4jIntegrationTest(
domainPackages = { "org.springframework.data.neo4j.conversion.support",
"org.springframework.data.neo4j.conversion.datagraph1156" },
repositoryPackages = { "org.springframework.data.neo4j.conversion.support",
"org.springframework.data.neo4j.conversion.datagraph1156" }
)
static class Config {
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2011-2020 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.conversion.datagraph1156;
import java.util.UUID;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;
import org.neo4j.ogm.annotation.Relationship;
import org.neo4j.ogm.annotation.typeconversion.Convert;
import org.neo4j.ogm.typeconversion.UuidStringConverter;
/**
* @author Michael J. Simons
*/
@NodeEntity
public class Hobby {
@Id
@Property("id")
@Convert(value = UuidStringConverter.class)
private UUID id = UUID.randomUUID();
@Property("name")
private String name;
@Relationship(value = "HAS", direction = Relationship.INCOMING)
private User hobbyist;
public Hobby(String name, User hobbyist) {
this.name = name;
this.hobbyist = hobbyist;
}
public UUID getId() {
return id;
}
public String getName() {
return name;
}
public User getHobbyist() {
return hobbyist;
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2011-2020 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.conversion.datagraph1156;
import java.util.UUID;
import org.springframework.data.neo4j.repository.Neo4jRepository;
/**
* @author Michael J. Simons
*/
public interface HobbyRepository extends Neo4jRepository<Hobby, UUID> {
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2011-2020 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.conversion.datagraph1156;
import java.util.UUID;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;
import org.neo4j.ogm.annotation.typeconversion.Convert;
import org.neo4j.ogm.typeconversion.UuidStringConverter;
/**
* @author Michael J. Simons
*/
@NodeEntity
public class User {
@Id
@Property("id")
@Convert(value = UuidStringConverter.class)
private UUID id = UUID.randomUUID();
@Property("email")
private String email;
public User(String email) {
this.email = email;
}
public UUID getId() {
return id;
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2011-2020 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.conversion.datagraph1156;
import java.util.Optional;
import java.util.UUID;
import org.springframework.data.neo4j.repository.Neo4jRepository;
/**
* @author Michael J. Simons
*/
public interface UserRepository extends Neo4jRepository<User, UUID> {
Optional<User> findByEmail(String email);
}