DATAGRAPH-175: added tests, replaced embarassing repository-related classes with GalaxyService

This commit is contained in:
agibalov
2012-05-17 11:02:02 +04:00
parent 2fd22d77f6
commit e29477bad0
8 changed files with 359 additions and 0 deletions

View File

@@ -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<WorldDto> worldDtosFromWorlds(Iterable<World> worlds) {
List<WorldDto> worldDtos = new ArrayList<WorldDto>();
for(World world : worlds) {
WorldDto worldDto = worldDtoFromWorld(world);
worldDtos.add(worldDto);
}
return worldDtos;
}
}

View File

@@ -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<WorldDto> findReachableWorlds(Long worldId) {
World world = worldRepository.findOne(worldId);
return mapper.worldDtosFromWorlds(world.getReachableWorlds());
}
public List<WorldDto> findAllWorlds() {
Iterable<World> worlds = worldRepository.findAll();
return mapper.worldDtosFromWorlds(worlds);
}
}

View File

@@ -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);
}
}

View File

@@ -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<WorldDto> reachableWorlds = galaxyService.findReachableWorlds(earth.getId());
assertEquals(1, reachableWorlds.size());
}
@Test
public void populatedGalaxyShouldNotHaveKrypton() {
galaxyService.makeSureGalaxyIsNotEmpty();
WorldDto planetPopsicle = galaxyService.findWorldNamed("Krypton");
assertNull(planetPopsicle);
}
}

View File

@@ -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<WorldDto> worldDtosFromWorlds(Iterable<World> worlds) {
List<WorldDto> worldDtos = new ArrayList<WorldDto>();
for(World world : worlds) {
WorldDto worldDto = worldDtoFromWorld(world);
worldDtos.add(worldDto);
}
return worldDtos;
}
}

View File

@@ -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<WorldDto> findReachableWorlds(Long worldId) {
World world = worldRepository.findOne(worldId);
return mapper.worldDtosFromWorlds(world.getReachableWorlds());
}
public List<WorldDto> findAllWorlds() {
Iterable<World> worlds = worldRepository.findAll();
return mapper.worldDtosFromWorlds(worlds);
}
}

View File

@@ -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);
}
}

View File

@@ -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<WorldDto> reachableWorlds = galaxyService.findReachableWorlds(earth.getId());
assertEquals(1, reachableWorlds.size());
}
@Test
public void populatedGalaxyShouldNotHaveKrypton() {
galaxyService.makeSureGalaxyIsNotEmpty();
WorldDto planetPopsicle = galaxyService.findWorldNamed("Krypton");
assertNull(planetPopsicle);
}
}