Skip to content

Commit

Permalink
Cleaned up namespace horrors from IntelliJ; removed GraphDatabase wra…
Browse files Browse the repository at this point in the history
…pper
  • Loading branch information
Bobby Norton committed Apr 10, 2012
1 parent b7ae628 commit ccd861b
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 94 deletions.
2 changes: 2 additions & 0 deletions indie.ipr
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<profile version="1.0" is_locked="false">
<option name="myName" value="Project Default" />
<option name="myLocal" value="false" />
<inspection_tool class="AssignmentUsedAsCondition" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="IgnoreResultOfCall" enabled="false" level="WARNING" enabled_by_default="false">
<option name="m_reportAllNonLibraryCalls" value="false" />
<option name="callCheckString" value="java.io.InputStream,read,java.io.InputStream,skip,java.lang.StringBuffer,toString,java.lang.StringBuilder,toString,java.lang.String,.*,java.math.BigInteger,.*,java.math.BigDecimal,.*,java.net.InetAddress,.*,java.io.File,.*,java.lang.Object,equals|hashCode" />
Expand All @@ -163,6 +164,7 @@
<option name="processLiterals" value="false" />
<option name="processComments" value="true" />
</inspection_tool>
<inspection_tool class="UseOfObsoleteAssert" enabled="true" level="WARNING" enabled_by_default="true" />
</profile>
</profiles>
<option name="PROJECT_PROFILE" value="Project Default" />
Expand Down
47 changes: 0 additions & 47 deletions src/main/java/com/graphutils/indie/daos/GraphDatabase.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.graphutils.indie.handlers;

import com.graphutils.indie.presenters.PingPresenter;

public class PingRequestHandler {

public StringRequestHandler ping() {
return new StringRequestHandler() {
@Override
public String handle() {
return new com.graphutils.indie.presenters.PingPresenter().toJson();
return new PingPresenter().toJson();
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package com.graphutils.indie.handlers;

import com.graphutils.indie.daos.TinkerGraphData;
import com.graphutils.indie.presenters.ProjectPresenter;
import com.graphutils.indie.traversals.BasicTraversals;
import com.tinkerpop.blueprints.pgm.Graph;

import java.util.List;

public class ProjectRequestHandler {
private Graph g;

public ProjectRequestHandler(com.graphutils.indie.daos.GraphDatabase graphDatabase) {
g = graphDatabase.g();
public ProjectRequestHandler(Graph g) {
this.g = g;
}

public StringRequestHandler projectsFor(final String id) {
return new StringRequestHandler() {
@Override
public String handle() {
List projects = com.graphutils.indie.traversals.BasicTraversals.findProjects(g, id);
return new com.graphutils.indie.presenters.ProjectPresenter().toJson(projects);
List projects = BasicTraversals.findProjects(g, id);
return new ProjectPresenter().toJson(projects);
}
};
}
Expand All @@ -25,7 +28,7 @@ public RequestHandler loadTinkerGraphData() {
return new RequestHandler() {
@Override
public void handle() {
com.graphutils.indie.daos.TinkerGraphData.populate(g);
TinkerGraphData.populate(g);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@
public abstract class JsonPresenter {
protected static ObjectMapper mapper = new ObjectMapper();
protected static JsonFactory factory = new JsonFactory();

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.graphutils.indie.resources;

import com.graphutils.indie.handlers.ProjectRequestHandler;
import com.sun.jersey.spi.CloseableService;
import com.tinkerpop.blueprints.pgm.impls.neo4j.Neo4jGraph;
import org.neo4j.server.database.Database;

import javax.ws.rs.*;
Expand All @@ -11,13 +13,12 @@
@Path("/projects")
public class ProjectResource extends Resource {

private com.graphutils.indie.handlers.ProjectRequestHandler handler;
private ProjectRequestHandler handler;

public ProjectResource(@Context final CloseableService cs, @Context final Database db) {
this.closeableService = cs;
this.db = db;
com.graphutils.indie.daos.GraphDatabase graphDatabase = com.graphutils.indie.daos.GraphDatabase.getInstance(db.graph);
this.handler = new com.graphutils.indie.handlers.ProjectRequestHandler(graphDatabase);
this.handler = new ProjectRequestHandler(new Neo4jGraph(db.graph));
}

@GET
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/com/graphutils/indie/resources/Resource.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.graphutils.indie.resources;

import com.graphutils.indie.exceptions.MissingNodeException;
import com.graphutils.indie.handlers.BooleanRequestHandler;
import com.graphutils.indie.handlers.RequestHandler;
import com.graphutils.indie.handlers.StreamingRequestHandler;
import com.graphutils.indie.handlers.StringRequestHandler;
import com.sun.jersey.spi.CloseableService;
import org.neo4j.server.database.Database;

Expand All @@ -17,18 +22,18 @@ public class Resource {
protected Database db;
protected CloseableService closeableService;

protected Response getJson(com.graphutils.indie.handlers.StringRequestHandler handler) {
protected Response getJson(StringRequestHandler handler) {
try {
return json(handler.handle());
} catch (com.graphutils.indie.exceptions.MissingNodeException ex) {
} catch (MissingNodeException ex) {
return missing();
} catch (Exception e) {
e.printStackTrace();
return error();
}
}

protected StreamingOutput getStream(final com.graphutils.indie.handlers.StreamingRequestHandler handler) {
protected StreamingOutput getStream(final StreamingRequestHandler handler) {
return new StreamingOutput() {
public void write(OutputStream output) throws IOException, WebApplicationException {
try {
Expand All @@ -43,7 +48,7 @@ public void write(OutputStream output) throws IOException, WebApplicationExcepti
};
}

protected Response post(com.graphutils.indie.handlers.RequestHandler handler) {
protected Response post(RequestHandler handler) {
final CloseableTransaction transaction = new CloseableTransaction(db.graph.beginTx());
closeableService.add(transaction);
try {
Expand All @@ -57,15 +62,15 @@ protected Response post(com.graphutils.indie.handlers.RequestHandler handler) {
}
}

protected Response post(com.graphutils.indie.handlers.BooleanRequestHandler handler) {
protected Response post(BooleanRequestHandler handler) {
return withStatusCode(created(), handler);
}

protected Response delete(com.graphutils.indie.handlers.BooleanRequestHandler handler) {
protected Response delete(BooleanRequestHandler handler) {
return withStatusCode(deleted(), handler);
}

protected Response withStatusCode(Response response, com.graphutils.indie.handlers.BooleanRequestHandler handler) {
protected Response withStatusCode(Response response, BooleanRequestHandler handler) {
final CloseableTransaction transaction = new CloseableTransaction(db.graph.beginTx());
closeableService.add(transaction);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.graphutils.indie.resources;

import com.graphutils.indie.daos.GraphDatabase;
import com.graphutils.indie.handlers.PingRequestHandler;
import com.sun.jersey.spi.CloseableService;
import org.neo4j.server.database.Database;
Expand All @@ -16,13 +15,12 @@
@Path("/")
public class RootResource extends Resource {

private com.graphutils.indie.handlers.PingRequestHandler handler;
private PingRequestHandler handler;

public RootResource(@Context final CloseableService cs, @Context final Database db) {
this.closeableService = cs;
this.db = db;
com.graphutils.indie.daos.GraphDatabase graphDatabase = com.graphutils.indie.daos.GraphDatabase.getInstance(db.graph);
this.handler = new com.graphutils.indie.handlers.PingRequestHandler();
this.handler = new PingRequestHandler();
}

@GET
Expand All @@ -31,5 +29,4 @@ public RootResource(@Context final CloseableService cs, @Context final Database
public Response ping() {
return getJson(handler.ping());
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.graphutils.indie.daos;

import com.graphutils.indie.daos.TinkerGraphData;
import com.graphutils.indie.test.utils.AutoDbTestBase;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;

public class TinkerGraphDataTest extends com.graphutils.indie.test.utils.AutoDbTestBase {
public class TinkerGraphDataTest extends AutoDbTestBase {

@Test
public void shouldPopulateGraphOnce() {
com.graphutils.indie.daos.TinkerGraphData.populate(g);
TinkerGraphData.populate(g);
// ensure that the call is idempotent
com.graphutils.indie.daos.TinkerGraphData.populate(g);
TinkerGraphData.populate(g);
long count = new GremlinPipeline().start(g.getVertices()).count();

// neo4j's root node in adv. and ent. edition, along with 6 in the TinkerGraph
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import junit.framework.Assert;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;

public class PingRequestHandlerTest {
@Test
public void shouldSendAcknowledgementMessage() {
Assert.assertEquals("{\"message\":\"indie loaded successfully\"}", new com.graphutils.indie.handlers.PingRequestHandler().ping().handle());
Assert.assertEquals("{\"message\":\"indie loaded successfully\"}", new PingRequestHandler().ping().handle());
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package com.graphutils.indie.handlers;

import com.graphutils.indie.daos.TinkerGraphData;
import com.graphutils.indie.handlers.ProjectRequestHandler;
import com.graphutils.indie.test.utils.AutoDbTestBase;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;

public class ProjectRequestHandlerTest extends com.graphutils.indie.test.utils.AutoDbTestBase {
public class ProjectRequestHandlerTest extends AutoDbTestBase {
@Test
public void shouldFindProjects() {
com.graphutils.indie.daos.TinkerGraphData.populate(g);
com.graphutils.indie.handlers.ProjectRequestHandler handler = new com.graphutils.indie.handlers.ProjectRequestHandler(graphDb);
TinkerGraphData.populate(g);
ProjectRequestHandler handler = new ProjectRequestHandler(g);
String results = handler.projectsFor("1").handle();
assertEquals("{\"projects\":[\"ripple\",\"lop\"]}", results);
}

@Test
public void shouldLoadProjects() {
com.graphutils.indie.handlers.ProjectRequestHandler handler = new com.graphutils.indie.handlers.ProjectRequestHandler(graphDb);
ProjectRequestHandler handler = new ProjectRequestHandler(g);

// initially, we start off only with the Neo4j root node in adv. and enterprise edition, but not community
assertTrue(vCount() <= 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.graphutils.indie.test.functional;

import com.graphutils.indie.test.utils.FileUtils;

public class Server {
private static String databaseFiles;
private String neo4jCommand;
private static boolean justStarted = true;

public Server() {
String version = com.graphutils.indie.test.utils.FileUtils.readServerVersion();
String version = FileUtils.readServerVersion();
databaseFiles = String.format("%s/data/graph.db", version);
neo4jCommand = String.format("%s/bin/neo4j", version);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.graphutils.indie.test.utils;

import com.graphutils.indie.daos.GraphDatabase;
import com.tinkerpop.blueprints.pgm.impls.neo4j.Neo4jGraph;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -15,26 +14,25 @@ public class ManualDbTestBase {
protected Transaction transaction;
protected static String dbFilename;
protected static Database database;
protected static com.graphutils.indie.daos.GraphDatabase graphDb;
protected static Neo4jGraph g;

@BeforeClass
public static void initializeDatabase() {
dbFilename = "out/graph_db-" + new Date().getTime();
database = new Database(new EmbeddedGraphDatabase(dbFilename));
graphDb = new com.graphutils.indie.daos.GraphDatabase(database.graph);
g = graphDb.g();
g = new Neo4jGraph(database.graph);

}

@AfterClass
public static void closeDatabase() {
graphDb.shutdown();
database.shutdown();
File dbFile = new File(dbFilename);
dbFile.delete();
}

public void startTransaction() {
transaction = graphDb.graphDatabaseService().beginTx();
transaction = database.graph.beginTx();
}

public void rollbackTransaction() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.graphutils.indie.traversals;

import com.graphutils.indie.daos.TinkerGraphData;
import com.graphutils.indie.traversals.BasicTraversals;
import com.graphutils.indie.test.utils.AutoDbTestBase;
import org.junit.Test;

import java.util.List;

import static junit.framework.Assert.assertEquals;

public class TraversalTest extends com.graphutils.indie.test.utils.AutoDbTestBase {
public class TraversalTest extends AutoDbTestBase {

@Test
public void shouldFindFriends() {
com.graphutils.indie.daos.TinkerGraphData.populate(g);
TinkerGraphData.populate(g);

// lets traverse to marko's 30+ year old friends' created projects
List projects = com.graphutils.indie.traversals.BasicTraversals.findProjects(g, "1");
List projects = BasicTraversals.findProjects(g, "1");

assertEquals(2, projects.size());
assertEquals("ripple", projects.get(0));
Expand Down

0 comments on commit ccd861b

Please sign in to comment.