diff --git a/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/typerepresentation/LabelingNodeTypeRepresentationStrategyTests.java b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/typerepresentation/LabelingNodeTypeRepresentationStrategyTests.java new file mode 100644 index 000000000..6956e79b7 --- /dev/null +++ b/spring-data-neo4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/typerepresentation/LabelingNodeTypeRepresentationStrategyTests.java @@ -0,0 +1,183 @@ +/** + * Copyright 2011 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 + * + * http://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.aspects.support.typerepresentation; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.neo4j.graphdb.Node; +import org.neo4j.graphdb.PropertyContainer; +import org.neo4j.graphdb.Transaction; +import org.neo4j.helpers.collection.IteratorUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.neo4j.annotation.NodeEntity; +import org.springframework.data.neo4j.aspects.support.EntityTestBase; +import org.springframework.data.neo4j.support.Neo4jTemplate; +import org.springframework.data.neo4j.support.mapping.Neo4jMappingContext; +import org.springframework.data.neo4j.support.mapping.StoredEntityType; +import org.springframework.data.neo4j.support.typerepresentation.CoreAPIBasedLabelingNodeTypeRepresentationStrategy; +import org.springframework.test.context.CleanContextCacheTestExecutionListener; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; +import org.springframework.test.context.transaction.BeforeTransaction; +import org.springframework.test.context.transaction.TransactionalTestExecutionListener; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Arrays; +import java.util.HashSet; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath:org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTests-context.xml", + "classpath:org/springframework/data/neo4j/aspects/support/LabelingTypeRepresentationStrategyOverride-context.xml"}) +@TestExecutionListeners({CleanContextCacheTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) +public class LabelingNodeTypeRepresentationStrategyTests extends EntityTestBase { + + @Autowired + private CoreAPIBasedLabelingNodeTypeRepresentationStrategy nodeTypeRepresentationStrategy; + + @Autowired + Neo4jTemplate neo4jTemplate; + @Autowired + Neo4jMappingContext ctx; + + private Thing thing; + private SubThing subThing; + private StoredEntityType thingType; + private StoredEntityType subThingType; + + @BeforeTransaction + public void cleanDb() { + super.cleanDb(); + } + + @Before + public void setUp() throws Exception { + if (thing == null) { + createThingsAndLinks(); + } + thingType = typeOf(Thing.class); + subThingType = typeOf(SubThing.class); + } + + @Test + @Transactional + public void testPostEntityCreation() throws Exception { + // Anything to test here?? + } + + @Test + public void testPreEntityRemoval() throws Exception { + // preEntityRemoval is a no op method, so nothing to test here! + } + + @Test + @Transactional + public void testFindAll() throws Exception { + + assertEquals("Did not find all things.", + new HashSet(Arrays.asList(neo4jTemplate.getPersistentState(subThing), neo4jTemplate.getPersistentState(thing))), + IteratorUtil.addToCollection(nodeTypeRepresentationStrategy.findAll(thingType), new HashSet())); + } + + @Test + @Transactional + public void testCount() throws Exception { + assertEquals(2, nodeTypeRepresentationStrategy.count(thingType)); + } + + @Test + @Transactional + public void testGetJavaType() throws Exception { + assertEquals(thingType.getAlias(), nodeTypeRepresentationStrategy.readAliasFrom(node(thing))); + assertEquals(subThingType.getAlias(), nodeTypeRepresentationStrategy.readAliasFrom(node(subThing))); + assertEquals(Thing.class, neo4jTemplate.getStoredJavaType(node(thing))); + assertEquals(SubThing.class, neo4jTemplate.getStoredJavaType(node(subThing))); + } + + @Test + @Transactional + public void testCreateEntityAndInferType() throws Exception { + Thing newThing = neo4jTemplate.createEntityFromStoredType(node(thing), neo4jTemplate.getMappingPolicy(thing)); + assertEquals(thing, newThing); + } + + @Test + @Transactional + public void testCreateEntityAndSpecifyType() throws Exception { + Thing newThing = neo4jTemplate.createEntityFromState(node(subThing), Thing.class, neo4jTemplate.getMappingPolicy(subThing)); + assertEquals(subThing, newThing); + } + + @Test + @Transactional + public void testProjectEntity() throws Exception { + Unrelated other = neo4jTemplate.projectTo(node(thing), Unrelated.class); + assertEquals("thing", other.getName()); + } + + private Node node(Thing thing) { + return getNodeState(thing); + } + + private Thing createThingsAndLinks() { + Transaction tx = graphDatabaseService.beginTx(); + try { + Node n1 = graphDatabaseService.createNode(); + thing = neo4jTemplate.setPersistentState(new Thing(),n1); + nodeTypeRepresentationStrategy.writeTypeTo(n1, neo4jTemplate.getEntityType(Thing.class)); + thing.setName("thing"); + Node n2 = graphDatabaseService.createNode(); + subThing = neo4jTemplate.setPersistentState(new SubThing(),n2); + nodeTypeRepresentationStrategy.writeTypeTo(n2, neo4jTemplate.getEntityType(SubThing.class)); + subThing.setName("subThing"); + tx.success(); + return thing; + } finally { + tx.finish(); + } + } + + @NodeEntity + public static class Unrelated { + String name; + + public String getName() { + return name; + } + } + + @NodeEntity + public static class Thing { + String name; + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } + + public static class SubThing extends Thing { + } +} diff --git a/spring-data-neo4j-aspects/src/test/resources/org/springframework/data/neo4j/aspects/support/LabelingTypeRepresentationStrategyOverride-context.xml b/spring-data-neo4j-aspects/src/test/resources/org/springframework/data/neo4j/aspects/support/LabelingTypeRepresentationStrategyOverride-context.xml new file mode 100644 index 000000000..c940237b9 --- /dev/null +++ b/spring-data-neo4j-aspects/src/test/resources/org/springframework/data/neo4j/aspects/support/LabelingTypeRepresentationStrategyOverride-context.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/spring-data-neo4j-rest/src/main/java/org/springframework/data/neo4j/rest/SpringRestGraphDatabase.java b/spring-data-neo4j-rest/src/main/java/org/springframework/data/neo4j/rest/SpringRestGraphDatabase.java index 3f419425d..72f041244 100644 --- a/spring-data-neo4j-rest/src/main/java/org/springframework/data/neo4j/rest/SpringRestGraphDatabase.java +++ b/spring-data-neo4j-rest/src/main/java/org/springframework/data/neo4j/rest/SpringRestGraphDatabase.java @@ -33,6 +33,8 @@ import org.springframework.data.neo4j.annotation.QueryType; import org.springframework.data.neo4j.conversion.DefaultConverter; import org.springframework.data.neo4j.conversion.ResultConverter; import org.springframework.data.neo4j.core.GraphDatabase; +import org.springframework.data.neo4j.core.GraphDatabaseGlobalOperations; +import org.springframework.data.neo4j.support.DelegatingGraphDatabaseGlobalOperations; import org.springframework.data.neo4j.support.index.NoSuchIndexException; import org.springframework.data.neo4j.support.query.ConversionServiceQueryResultConverter; import org.springframework.data.neo4j.support.query.QueryEngine; @@ -46,6 +48,7 @@ public class SpringRestGraphDatabase extends org.neo4j.rest.graphdb.RestGraphDat } private ConversionService conversionService; private ResultConverter resultConverter; + private GraphDatabaseGlobalOperations globalOperations; public SpringRestGraphDatabase( RestAPI api){ super(api); @@ -59,6 +62,14 @@ public class SpringRestGraphDatabase extends org.neo4j.rest.graphdb.RestGraphDat this(new RestAPIFacade( uri, user, password )); } + @Override + public GraphDatabaseGlobalOperations getGlobalGraphOperations() { + if (this.globalOperations == null) { + this.globalOperations = new DelegatingGraphDatabaseGlobalOperations(this); + } + return globalOperations; + } + @Override public Node createNode(Map props) { return super.getRestAPI().createNode(props); diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/core/GraphDatabase.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/core/GraphDatabase.java index 92c30350b..fae280af2 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/core/GraphDatabase.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/core/GraphDatabase.java @@ -30,6 +30,13 @@ import java.util.Map; public interface GraphDatabase { + + /** + * @return an object which is able to provide global graph + * type operations + */ + GraphDatabaseGlobalOperations getGlobalGraphOperations(); + /** * @return the reference node of the underlying graph database */ diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/core/GraphDatabaseGlobalOperations.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/core/GraphDatabaseGlobalOperations.java new file mode 100644 index 000000000..fa4bba736 --- /dev/null +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/core/GraphDatabaseGlobalOperations.java @@ -0,0 +1,19 @@ +package org.springframework.data.neo4j.core; + +import org.neo4j.graphdb.*; + +/** + * Defines the list of global graph operations which can be used + * within SDN. (At present restricted to those providing Label + * based functionality) + * + * @author Nicki Watt + * @since 24-09-2013 + */ +public interface GraphDatabaseGlobalOperations { + + public ResourceIterable