Skip to content

Instantly share code, notes, and snippets.

@dozed

dozed/World.java Secret

Created November 19, 2011 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dozed/5bcbf7693c9465e8771a to your computer and use it in GitHub Desktop.
Save dozed/5bcbf7693c9465e8771a to your computer and use it in GitHub Desktop.
package org.springframework.data.neo4j.examples.hellograph;
import org.neo4j.graphdb.Direction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.Indexed;
import org.springframework.data.neo4j.annotation.NodeEntity;
import org.springframework.data.neo4j.annotation.RelatedTo;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import java.util.HashSet;
import java.util.Set;
/**
* A Spring Data Neo4j enhanced World entity.
* <p/>
* This is the initial POJO in the Universe.
*/
@NodeEntity
public class World
{
@GraphId Long id;
@Indexed
private String name;
@Indexed(indexName = "moon-index")
private int moons;
@RelatedTo(type = "REACHABLE_BY_ROCKET", elementClass = World.class, direction = Direction.BOTH)
private Set<World> reachableByRocket = new HashSet<World>();
public World( String name, int moons )
{
this.name = name;
this.moons = moons;
}
public World()
{
}
public String getName()
{
return name;
}
public int getMoons()
{
return moons;
}
@Override
public String toString()
{
return String.format("World{name='%s, moons=%d}", name, moons);
}
public void addRocketRouteTo( World otherWorld )
{
reachableByRocket.add(otherWorld);
}
public boolean canBeReachedFrom( World otherWorld )
{
return reachableByRocket.contains( otherWorld );
}
public Set<World> getReachableWorlds() {
return reachableByRocket;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
World other = (World) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
package org.springframework.data.neo4j.examples.hellograph;
import org.junit.Ignore;
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.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collection;
import java.util.Iterator;
import static junit.framework.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static org.junit.internal.matchers.StringContains.containsString;
/**
* Exploratory testing of Spring Data Neo4j using
* the WorldRepositoryImpl.
*/
@ContextConfiguration(locations = "classpath:spring/helloWorldContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public class WorldRepositoryTest
{
@Autowired
private WorldRepository galaxy;
@Autowired
private Neo4jTemplate template;
@Rollback(false)
@BeforeTransaction
public void clearDatabase()
{
Neo4jHelper.cleanDb(template);
}
@Test
public void shouldAllowDirectWorldCreation()
{
assertEquals(0, (long) template.count(World.class));
World myWorld = galaxy.save(new World( "mine", 0 ));
assertEquals(1, (long) template.count(World.class));
World foundWorld = galaxy.findOne(myWorld.id);
assertEquals(myWorld.getName(), foundWorld.getName());
}
@Test
public void shouldPopulateGalaxyWithWorlds()
{
Iterable<World> worlds = galaxy.makeSomeWorlds();
assertNotNull( worlds );
}
@Test
public void shouldHaveCorrectNumberOfWorlds()
{
galaxy.makeSomeWorlds();
assertEquals(13, (long) galaxy.count());
}
@Test
public void shouldFindAllWorlds()
{
Collection<World> madeWorlds = galaxy.makeSomeWorlds();
Iterable<World> foundWorlds = galaxy.findAll();
int countOfFoundWorlds = 0;
for ( World foundWorld : foundWorlds )
{
assertTrue( madeWorlds.contains( foundWorld ) );
countOfFoundWorlds++;
}
assertEquals( madeWorlds.size(), countOfFoundWorlds );
}
@Test
public void shouldFindWorldsByName()
{
for ( World w : galaxy.makeSomeWorlds() )
{
assertNotNull( galaxy.findWorldNamed( w.getName() ) );
}
}
@SuppressWarnings("unchecked")
@Test
public void shouldFindWorldsWith1Moon()
{
galaxy.makeSomeWorlds();
for ( World worldWithOneMoon : galaxy.findWorldsWithMoons( 1 ) )
{
assertThat( worldWithOneMoon.getName(), is( anyOf( containsString( "Earth" ), containsString( "Midgard" ) ) ) );
}
}
@Test
public void shouldReachMarsFromEarth()
{
galaxy.makeSomeWorlds();
World earth = galaxy.findWorldNamed( "Earth" );
World mars = galaxy.findWorldNamed( "Mars" );
assertTrue( mars.canBeReachedFrom( earth ) );
assertTrue( earth.canBeReachedFrom( mars ) );
Iterator<World> it = earth.getReachableWorlds().iterator();
assertEquals(it.next().getName(), "Mars");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment