From e29477bad04669da7bca497660968194b86e8104 Mon Sep 17 00:00:00 2001 From: agibalov Date: Thu, 17 May 2012 11:02:02 +0400 Subject: [PATCH] DATAGRAPH-175: added tests, replaced embarassing repository-related classes with GalaxyService --- .../examples/hellograph/GalaxyMapper.java | 27 ++++++++ .../examples/hellograph/GalaxyService.java | 64 ++++++++++++++++++ .../neo4j/examples/hellograph/WorldDto.java | 30 +++++++++ .../hellograph/GalaxyServiceTest.java | 58 +++++++++++++++++ .../examples/hellograph/GalaxyMapper.java | 27 ++++++++ .../examples/hellograph/GalaxyService.java | 65 +++++++++++++++++++ .../neo4j/examples/hellograph/WorldDto.java | 30 +++++++++ .../hellograph/GalaxyServiceTest.java | 58 +++++++++++++++++ 8 files changed, 359 insertions(+) create mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java create mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java create mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java create mode 100644 spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java create mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java create mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java create mode 100644 spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java create mode 100644 spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java new file mode 100644 index 000000000..97cdb12ce --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java @@ -0,0 +1,27 @@ +package org.springframework.data.neo4j.examples.hellograph; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.stereotype.Service; + +@Service +public class GalaxyMapper { + public WorldDto worldDtoFromWorld(World world) { + if(world == null) { + return null; + } + + return new WorldDto(world.getId(), world.getName(), world.getMoons()); + } + + public List worldDtosFromWorlds(Iterable worlds) { + List worldDtos = new ArrayList(); + for(World world : worlds) { + WorldDto worldDto = worldDtoFromWorld(world); + worldDtos.add(worldDto); + } + + return worldDtos; + } +} \ No newline at end of file diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java new file mode 100644 index 000000000..c443d89aa --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java @@ -0,0 +1,64 @@ +package org.springframework.data.neo4j.examples.hellograph; + + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class GalaxyService { + @Autowired + private WorldRepository worldRepository; + + @Autowired + private GalaxyMapper mapper; + + @Transactional + public void makeSureGalaxyIsNotEmpty() { + // Solar worlds + resolveWorld("Mercury", 0); + resolveWorld("Venus", 0); + + World earth = resolveWorld("Earth", 1); + World mars = resolveWorld("Mars", 2); + mars.addRocketRouteTo(earth); + + resolveWorld("Jupiter", 63); + resolveWorld("Saturn", 62); + resolveWorld("Uranus", 27); + resolveWorld("Neptune", 13); + + // Norse worlds + resolveWorld("Alfheimr", 0); + resolveWorld("Midgard", 1); + resolveWorld("Muspellheim", 2); + resolveWorld("Asgard", 63); + resolveWorld("Hel", 62); + } + + private World resolveWorld(String name, int moons) { + World createdWorld = worldRepository.findByPropertyValue("name", name); + if (createdWorld == null) { + createdWorld = new World(name, moons); + createdWorld = worldRepository.save(createdWorld); + } + return createdWorld; + } + + public WorldDto findWorldNamed(String name) { + World world = worldRepository.findByPropertyValue("name", name); + return mapper.worldDtoFromWorld(world); + } + + public List findReachableWorlds(Long worldId) { + World world = worldRepository.findOne(worldId); + return mapper.worldDtosFromWorlds(world.getReachableWorlds()); + } + + public List findAllWorlds() { + Iterable worlds = worldRepository.findAll(); + return mapper.worldDtosFromWorlds(worlds); + } +} diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java new file mode 100644 index 000000000..9860fe2b8 --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java @@ -0,0 +1,30 @@ +package org.springframework.data.neo4j.examples.hellograph; + +public class WorldDto { + private final Long id; + private final String name; + private final int moons; + + public WorldDto(Long id, String name, int moons) { + this.id = id; + this.name = name; + this.moons = moons; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public int getMoons() { + return moons; + } + + @Override + public String toString() { + return String.format("WorldDto{id=%d, name='%s', moons=%d}", id, name, moons); + } +} \ No newline at end of file diff --git a/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java b/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java new file mode 100644 index 000000000..a8b9ed43b --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds-aspects/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java @@ -0,0 +1,58 @@ +package org.springframework.data.neo4j.examples.hellograph; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.neo4j.support.Neo4jTemplate; +import org.springframework.data.neo4j.support.node.Neo4jHelper; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@ContextConfiguration(locations = "classpath:/spring/helloWorldContext.xml") +@RunWith(SpringJUnit4ClassRunner.class) +public class GalaxyServiceTest { + + @Autowired + private GalaxyService galaxyService; + + @Autowired + private Neo4jTemplate template; + + @Before + public void cleanUpGraph() { + Neo4jHelper.cleanDb(template); + } + + @Test + public void galaxyShouldBeEmptyUntilExplicitlyPopulated() { + assertTrue(galaxyService.findAllWorlds().isEmpty()); + } + + @Test + public void populatedGalaxyShouldHaveEarth() { + galaxyService.makeSureGalaxyIsNotEmpty(); + WorldDto earth = galaxyService.findWorldNamed("Earth"); + assertNotNull(earth); + assertEquals("Earth", earth.getName()); + } + + @Test + public void earthShouldHaveOnlyOneReachableWorld() { + galaxyService.makeSureGalaxyIsNotEmpty(); + WorldDto earth = galaxyService.findWorldNamed("Earth"); + List reachableWorlds = galaxyService.findReachableWorlds(earth.getId()); + assertEquals(1, reachableWorlds.size()); + } + + @Test + public void populatedGalaxyShouldNotHaveKrypton() { + galaxyService.makeSureGalaxyIsNotEmpty(); + WorldDto planetPopsicle = galaxyService.findWorldNamed("Krypton"); + assertNull(planetPopsicle); + } +} diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java new file mode 100644 index 000000000..97cdb12ce --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyMapper.java @@ -0,0 +1,27 @@ +package org.springframework.data.neo4j.examples.hellograph; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.stereotype.Service; + +@Service +public class GalaxyMapper { + public WorldDto worldDtoFromWorld(World world) { + if(world == null) { + return null; + } + + return new WorldDto(world.getId(), world.getName(), world.getMoons()); + } + + public List worldDtosFromWorlds(Iterable worlds) { + List worldDtos = new ArrayList(); + for(World world : worlds) { + WorldDto worldDto = worldDtoFromWorld(world); + worldDtos.add(worldDto); + } + + return worldDtos; + } +} \ No newline at end of file diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java new file mode 100644 index 000000000..a7fa425b7 --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/GalaxyService.java @@ -0,0 +1,65 @@ +package org.springframework.data.neo4j.examples.hellograph; + + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class GalaxyService { + @Autowired + private WorldRepository worldRepository; + + @Autowired + private GalaxyMapper mapper; + + @Transactional + public void makeSureGalaxyIsNotEmpty() { + // Solar worlds + resolveWorld("Mercury", 0); + resolveWorld("Venus", 0); + + World earth = resolveWorld("Earth", 1); + World mars = resolveWorld("Mars", 2); + mars.addRocketRouteTo(earth); + worldRepository.save(mars); + + resolveWorld("Jupiter", 63); + resolveWorld("Saturn", 62); + resolveWorld("Uranus", 27); + resolveWorld("Neptune", 13); + + // Norse worlds + resolveWorld("Alfheimr", 0); + resolveWorld("Midgard", 1); + resolveWorld("Muspellheim", 2); + resolveWorld("Asgard", 63); + resolveWorld("Hel", 62); + } + + private World resolveWorld(String name, int moons) { + World createdWorld = worldRepository.findByPropertyValue("name", name); + if (createdWorld == null) { + createdWorld = new World(name, moons); + createdWorld = worldRepository.save(createdWorld); + } + return createdWorld; + } + + public WorldDto findWorldNamed(String name) { + World world = worldRepository.findByPropertyValue("name", name); + return mapper.worldDtoFromWorld(world); + } + + public List findReachableWorlds(Long worldId) { + World world = worldRepository.findOne(worldId); + return mapper.worldDtosFromWorlds(world.getReachableWorlds()); + } + + public List findAllWorlds() { + Iterable worlds = worldRepository.findAll(); + return mapper.worldDtosFromWorlds(worlds); + } +} diff --git a/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java new file mode 100644 index 000000000..9860fe2b8 --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds/src/main/java/org/springframework/data/neo4j/examples/hellograph/WorldDto.java @@ -0,0 +1,30 @@ +package org.springframework.data.neo4j.examples.hellograph; + +public class WorldDto { + private final Long id; + private final String name; + private final int moons; + + public WorldDto(Long id, String name, int moons) { + this.id = id; + this.name = name; + this.moons = moons; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public int getMoons() { + return moons; + } + + @Override + public String toString() { + return String.format("WorldDto{id=%d, name='%s', moons=%d}", id, name, moons); + } +} \ No newline at end of file diff --git a/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java new file mode 100644 index 000000000..a8b9ed43b --- /dev/null +++ b/spring-data-neo4j-examples/hello-worlds/src/test/java/org/springframework/data/neo4j/examples/hellograph/GalaxyServiceTest.java @@ -0,0 +1,58 @@ +package org.springframework.data.neo4j.examples.hellograph; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.neo4j.support.Neo4jTemplate; +import org.springframework.data.neo4j.support.node.Neo4jHelper; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@ContextConfiguration(locations = "classpath:/spring/helloWorldContext.xml") +@RunWith(SpringJUnit4ClassRunner.class) +public class GalaxyServiceTest { + + @Autowired + private GalaxyService galaxyService; + + @Autowired + private Neo4jTemplate template; + + @Before + public void cleanUpGraph() { + Neo4jHelper.cleanDb(template); + } + + @Test + public void galaxyShouldBeEmptyUntilExplicitlyPopulated() { + assertTrue(galaxyService.findAllWorlds().isEmpty()); + } + + @Test + public void populatedGalaxyShouldHaveEarth() { + galaxyService.makeSureGalaxyIsNotEmpty(); + WorldDto earth = galaxyService.findWorldNamed("Earth"); + assertNotNull(earth); + assertEquals("Earth", earth.getName()); + } + + @Test + public void earthShouldHaveOnlyOneReachableWorld() { + galaxyService.makeSureGalaxyIsNotEmpty(); + WorldDto earth = galaxyService.findWorldNamed("Earth"); + List reachableWorlds = galaxyService.findReachableWorlds(earth.getId()); + assertEquals(1, reachableWorlds.size()); + } + + @Test + public void populatedGalaxyShouldNotHaveKrypton() { + galaxyService.makeSureGalaxyIsNotEmpty(); + WorldDto planetPopsicle = galaxyService.findWorldNamed("Krypton"); + assertNull(planetPopsicle); + } +}