#131 - Polished Spring Data Neo4j example.

Switched to milestone version of Spring Data Neo4j. Used Lombok in domain types. Used assertThat(…) matchers in test case for consistency. Inlined Spring configuration into test case. Upgraded to Lombok 1.16.6 along the way.

Fixed indentation in pom.xml files to use tabs instead of spaces.

Original pull requests: #129, #130.
This commit is contained in:
Oliver Gierke
2015-09-03 17:06:05 +02:00
parent f90bed38e2
commit 0fa8c12806
9 changed files with 152 additions and 233 deletions

View File

@@ -13,52 +13,75 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.neo4j;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import example.springdata.neo4j.domain.Actor;
import example.springdata.neo4j.domain.Movie;
import example.springdata.neo4j.domain.Role;
import example.springdata.neo4j.repo.ActorRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.neo4j.ogm.session.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.server.InProcessServer;
import org.springframework.data.neo4j.server.Neo4jServer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
/**
* Simple integration test demonstrating the use of the ActorRepository
*
* @author Luanne Misquitta
* @author Oliver Gierke
*/
@ContextConfiguration(classes = {ExampleConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@ContextConfiguration
@Transactional
public class ActorRepositoryIntegrationTest {
@Configuration
@EnableTransactionManagement
@EnableNeo4jRepositories
static class ExampleConfig extends Neo4jConfiguration {
@Override
public Neo4jServer neo4jServer() {
return new InProcessServer();
}
@Override
public SessionFactory getSessionFactory() {
return new SessionFactory("example.springdata.neo4j");
}
}
@Autowired ActorRepository actorRepository;
/**
* @see DATAGRAPH-752
* Test case to demonstrate saving node and relationship entities, and loading them via the repository.
* @see #131
*/
@Test
public void shouldBeAbleToSaveAndLoadActor() {
Actor daniel = new Actor("Daniel Radcliffe");
Movie goblet = new Movie("Harry Potter and the Goblet of Fire");
daniel.actedIn(goblet,"Harry Potter");
actorRepository.save(daniel); //saves the actor and the movie
Actor daniel = new Actor("Daniel Radcliffe");
daniel.actedIn(goblet, "Harry Potter");
actorRepository.save(daniel); // saves the actor and the movie
Actor actor = actorRepository.findOne(daniel.getId());
assertNotNull(actor);
assertEquals(daniel.getName(),actor.getName());
assertEquals(1,actor.getRoles().size());
Role role = actor.getRoles().iterator().next();
assertEquals("Harry Potter", role.getRole());
}
assertThat(actor, is(notNullValue()));
assertThat(actor.getName(), is(daniel.getName()));
assertThat(actor.getRoles(), hasSize(1));
Role role = actor.getRoles().iterator().next();
assertThat(role.getRole(), is("Harry Potter"));
}
}

View File

@@ -1,44 +0,0 @@
/*
* Copyright 2015 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 example.springdata.neo4j;
import org.neo4j.ogm.session.SessionFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.server.InProcessServer;
import org.springframework.data.neo4j.server.Neo4jServer;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @author Luanne Misquitta
*/
@Configuration
@EnableTransactionManagement
@EnableNeo4jRepositories("example.springdata.neo4j.repo")
public class ExampleConfig extends Neo4jConfiguration{
@Override
public Neo4jServer neo4jServer() {
return new InProcessServer();
}
@Override
public SessionFactory getSessionFactory() {
return new SessionFactory("example.springdata.neo4j.domain");
}
}