diff --git a/src/main/java/ca/utoronto/utm/mcs/App.java b/src/main/java/ca/utoronto/utm/mcs/App.java index be082bc..b29c0b8 100644 --- a/src/main/java/ca/utoronto/utm/mcs/App.java +++ b/src/main/java/ca/utoronto/utm/mcs/App.java @@ -2,8 +2,6 @@ import io.github.cdimascio.dotenv.Dotenv; import java.io.IOException; -import com.sun.net.httpserver.HttpServer; -import java.net.InetSocketAddress; public class App { diff --git a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java index d3fc597..311c728 100644 --- a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java +++ b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java @@ -3,7 +3,6 @@ import org.json.*; import org.neo4j.driver.*; import org.neo4j.driver.Record; -import org.neo4j.driver.types.Path; import java.util.List; @@ -228,7 +227,7 @@ public void deleteMovie(String movieId) throws JSONException { JSONObject response = new JSONObject(); String query; - query = "MATCH (m:movie { movieId: \"%s\"}) DETACH DELETE m"; + query = "MATCH (m:movie { id: \"%s\"}) DETACH DELETE m"; query = String.format(query, movieId); Result result = this.session.run(query); } @@ -236,7 +235,7 @@ public void deleteMovie(String movieId) throws JSONException { public void deleteActor(String actorId) throws JSONException { JSONObject response = new JSONObject(); String query; - query = "MATCH (a:actor { actorId: \"%s\"}) DETACH DELETE a"; + query = "MATCH (a:actor { id: \"%s\"}) DETACH DELETE a"; query = String.format(query, actorId); Result result = this.session.run(query); } diff --git a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java index abd0d73..c4369d3 100644 --- a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java +++ b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java @@ -26,7 +26,6 @@ public ReqHandler(Neo4jDAO dao) { public void handle(HttpExchange exchange) throws IOException { URI requestURI = exchange.getRequestURI(); - String query = requestURI.getQuery(); String request = requestURI.toString().split("/")[3]; System.out.println(request); try { @@ -49,6 +48,7 @@ public void handle(HttpExchange exchange) throws IOException { this.computeBaconNumber(exchange); break; default: + this.apiDNE(exchange); break; } break; @@ -64,6 +64,7 @@ public void handle(HttpExchange exchange) throws IOException { this.addRelationship(exchange); break; default: + this.apiDNE(exchange); break; } break; @@ -76,10 +77,12 @@ public void handle(HttpExchange exchange) throws IOException { this.deleteMovie(exchange); break; default: + this.apiDNE(exchange); break; } break; default: + this.apiDNE(exchange); break; } @@ -280,10 +283,11 @@ public void computeBaconNumber(HttpExchange r) throws IOException, JSONException try { JSONObject deserialized = new JSONObject(body); String actorId; - + if (deserialized.has("actorId")) { actorId = deserialized.getString("actorId"); } else { + r.sendResponseHeaders(400, -1); return; } @@ -379,4 +383,9 @@ public void deleteMovie(HttpExchange r) throws IOException { r.sendResponseHeaders(500, -1); } } + + public void apiDNE(HttpExchange r) throws IOException { + r.sendResponseHeaders(404, -1); + return; + } } \ No newline at end of file diff --git a/src/test/java/ca/utoronto/utm/mcs/AppTest.java b/src/test/java/ca/utoronto/utm/mcs/AppTest.java index ecc88ac..3ada8ea 100644 --- a/src/test/java/ca/utoronto/utm/mcs/AppTest.java +++ b/src/test/java/ca/utoronto/utm/mcs/AppTest.java @@ -5,7 +5,6 @@ import org.junit.jupiter.api.Test; import org.json.JSONException; import org.json.JSONObject; -import org.junit.jupiter.api.Test; import java.io.IOException; import java.net.HttpURLConnection; @@ -37,14 +36,15 @@ public void exampleTest() { assertTrue(true); } - // @Test - // public void addActorPass() throws JSONException, IOException, InterruptedException { - // JSONObject confirmReq = new JSONObject() - // .put("name", "TestActor") - // .put("actorId", "12345678901"); - // HttpResponse confirmRes = sendRequest("/api/v1/addActor", "PUT", confirmReq.toString()); - // assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); - // } + @Test + public void addActorPass() throws JSONException, IOException, InterruptedException { + JSONObject confirmReq = new JSONObject() + .put("name", "TestActor") + .put("actorId", "12345678901"); + HttpResponse confirmRes = sendRequest("/api/v1/addActor", "PUT", confirmReq.toString()); + sendRequest("/api/v1/deleteActor", "DELETE", confirmReq.toString()); + assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); + } @Test public void addActorFail() throws JSONException, IOException, InterruptedException { @@ -54,14 +54,16 @@ public void addActorFail() throws JSONException, IOException, InterruptedExcepti assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode()); } - // @Test - // public void addMoviePass() throws JSONException, IOException, InterruptedException { - // JSONObject confirmReq = new JSONObject() - // .put("name", "TestMovie") - // .put("movieId", "12345678901"); - // HttpResponse confirmRes = sendRequest("/api/v1/addMovie", "PUT", confirmReq.toString()); - // assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); - // } + @Test + public void addMoviePass() throws JSONException, IOException, InterruptedException { + + JSONObject confirmReq = new JSONObject() + .put("name", "TestMovie") + .put("movieId", "12345678901"); + HttpResponse confirmRes = sendRequest("/api/v1/addMovie", "PUT", confirmReq.toString()); + sendRequest("/api/v1/deleteMovie", "DELETE", confirmReq.toString()); + assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); + } @Test public void addMovieFail() throws JSONException, IOException, InterruptedException { @@ -74,27 +76,60 @@ public void addMovieFail() throws JSONException, IOException, InterruptedExcepti @Test public void addRelationshipPass() throws JSONException, IOException, InterruptedException { JSONObject confirmReq = new JSONObject() - .put("movieId", "x"); + .put("name", "TestActor") + .put("actorId", "123456789011"); + HttpResponse confirmRes = sendRequest("/api/v1/addActor", "PUT", confirmReq.toString()); + confirmReq = new JSONObject() + .put("name", "TestMovie") + .put("movieId", "123456789012"); + confirmRes = sendRequest("/api/v1/addMovie", "PUT", confirmReq.toString()); + confirmReq = new JSONObject() + .put("actorId", "123456789011") + .put("movieId", "123456789012"); + confirmRes = sendRequest("/api/v1/addRelationship", "PUT", confirmReq.toString()); + sendRequest("/api/v1/deleteActor", "DELETE", confirmReq.toString()); + sendRequest("/api/v1/deleteMovie", "DELETE", confirmReq.toString()); + assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); + } + + @Test + public void addRelationshipFail() throws JSONException, IOException, InterruptedException { + JSONObject confirmReq = new JSONObject() + .put("movieId", "123123"); HttpResponse confirmRes = sendRequest("/api/v1/addRelationship", "PUT", confirmReq.toString()); assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode()); } + @Test public void getActorPass() throws JSONException, IOException, InterruptedException { JSONObject setupReq = new JSONObject() .put("name", "TestActor") .put("actorId", "12345678901"); - HttpResponse setupRes = sendRequest("/api/v1/addActor", "PUT", setupReq.toString()); + sendRequest("/api/v1/addActor", "PUT", setupReq.toString()); JSONObject confirmReq = new JSONObject() .put("actorId", "12345678901"); HttpResponse confirmRes = sendRequest("/api/v1/getActor", "GET", confirmReq.toString()); - - JSONObject deleteReq = new JSONObject() - .put("actorId", "12345678901"); - HttpResponse deleteRes = sendRequest("/api/v1/deleteActor", "DELETE", confirmReq.toString()); + + sendRequest("/api/v1/deleteActor", "DELETE", setupReq.toString()); assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); } + @Test + public void getActorFail() throws JSONException, IOException, InterruptedException { + JSONObject setupReq = new JSONObject() + .put("name", "TestActor") + .put("actorId", "12345678901"); + sendRequest("/api/v1/addActor", "PUT", setupReq.toString()); + + JSONObject confirmReq = new JSONObject() + .put("name", "TestActor"); + HttpResponse confirmRes = sendRequest("/api/v1/getActor", "GET", confirmReq.toString()); + + sendRequest("/api/v1/deleteActor", "DELETE", setupReq.toString()); + assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode()); + } + @Test public void getMoviePass() throws JSONException, IOException, InterruptedException { JSONObject setupReq = new JSONObject() @@ -112,6 +147,21 @@ public void getMoviePass() throws JSONException, IOException, InterruptedExcepti assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); } + @Test + public void getMovieFail() throws JSONException, IOException, InterruptedException { + JSONObject setupReq = new JSONObject() + .put("name", "TestMovie") + .put("actorId", "12345678901"); + sendRequest("/api/v1/addMovie", "PUT", setupReq.toString()); + + JSONObject confirmReq = new JSONObject() + .put("name", "TestMovie"); + HttpResponse confirmRes = sendRequest("/api/v1/getMovie", "GET", confirmReq.toString()); + + sendRequest("/api/v1/deleteMovie", "DELETE", setupReq.toString()); + assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode()); + } + @Test public void hasRelationshipPass() throws JSONException, IOException, InterruptedException { JSONObject setupReq = new JSONObject() @@ -142,4 +192,138 @@ public void hasRelationshipPass() throws JSONException, IOException, Interrupted deleteRes = sendRequest("/api/v1/deleteActor", "DELETE", confirmReq.toString()); assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); } + + @Test + public void hasRelationshipFail() throws JSONException, IOException, InterruptedException { + JSONObject setupReq1 = new JSONObject() + .put("name", "TestMovie") + .put("movieId", "12345678901"); + sendRequest("/api/v1/addMovie", "PUT", setupReq1.toString()); + + JSONObject setupReq2 = new JSONObject() + .put("name", "TestActor") + .put("actorId", "12345678902"); + sendRequest("/api/v1/addActor", "PUT", setupReq2.toString()); + + JSONObject setupReq3 = new JSONObject() + .put("movieId", "12345678901") + .put("actorId", "12345678902"); + sendRequest("/api/v1/addRelationship", "PUT", setupReq3.toString()); + + JSONObject confirmReq = new JSONObject() + .put("movieId", "12345678901"); + HttpResponse confirmRes = sendRequest("/api/v1/hasRelationship", "GET", confirmReq.toString()); + + sendRequest("/api/v1/deleteMovie", "DELETE", setupReq1.toString()); + sendRequest("/api/v1/deleteActor", "DELETE", setupReq2.toString()); + assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode()); + } + + @Test + public void computeBaconNumberPass() throws JSONException, IOException, InterruptedException { + boolean deleteKB = false; + JSONObject setupReq1 = new JSONObject() + .put("name", "Kevin Bacon") + .put("actorId", "nm0000102"); + HttpResponse setupRes = sendRequest("/api/v1/addActor", "PUT", setupReq1.toString()); + + if(setupRes.statusCode() == 200){ + deleteKB = true; + } + + JSONObject setupReq2 = new JSONObject() + .put("name", "TestActor") + .put("actorId", "12345678902"); + sendRequest("/api/v1/addActor", "PUT", setupReq2.toString()); + + JSONObject setupReq3 = new JSONObject() + .put("name", "TestMovie") + .put("movieId", "12345678901"); + sendRequest("/api/v1/addMovie", "PUT", setupReq3.toString()); + + JSONObject confirmReq = new JSONObject() + .put("actorId", "12345678902") + .put("movieId", "12345678901"); + sendRequest("/api/v1/addRelationship", "PUT", confirmReq.toString()); + + confirmReq = new JSONObject() + .put("actorId", "nm0000102") + .put("movieId", "12345678901"); + sendRequest("/api/v1/addRelationship", "PUT", confirmReq.toString()); + + confirmReq = new JSONObject() + .put("actorId", "12345678902"); + HttpResponse confirmRes = sendRequest("/api/v1/computeBaconNumber", "GET", confirmReq.toString()); + + if(deleteKB){ + sendRequest("/api/v1/deleteActor", "DELETE", setupReq1.toString()); + } + sendRequest("/api/v1/deleteActor", "DELETE", setupReq2.toString()); + sendRequest("/api/v1/deleteMovie", "DELETE", setupReq3.toString()); + + assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); + } + + @Test + public void computeBaconNumberFail() throws JSONException, IOException, InterruptedException { + JSONObject confirmReq = new JSONObject() + .put("name", "Kevin Bacon"); + HttpResponse confirmRes = sendRequest("/api/v1/computeBaconNumber", "GET", confirmReq.toString()); + + assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode()); + } + + @Test + public void computeBaconPathPass() throws JSONException, IOException, InterruptedException { + boolean deleteKB = false; + JSONObject setupReq1 = new JSONObject() + .put("name", "Kevin Bacon") + .put("actorId", "nm0000102"); + HttpResponse setupRes = sendRequest("/api/v1/addActor", "PUT", setupReq1.toString()); + + if(setupRes.statusCode() == 200){ + deleteKB = true; + } + + JSONObject setupReq2 = new JSONObject() + .put("name", "TestActor") + .put("actorId", "12345678902"); + sendRequest("/api/v1/addActor", "PUT", setupReq2.toString()); + + JSONObject setupReq3 = new JSONObject() + .put("name", "TestMovie") + .put("movieId", "12345678901"); + sendRequest("/api/v1/addMovie", "PUT", setupReq3.toString()); + + JSONObject confirmReq = new JSONObject() + .put("actorId", "12345678902") + .put("movieId", "12345678901"); + sendRequest("/api/v1/addRelationship", "PUT", confirmReq.toString()); + + confirmReq = new JSONObject() + .put("actorId", "nm0000102") + .put("movieId", "12345678901"); + sendRequest("/api/v1/addRelationship", "PUT", confirmReq.toString()); + + confirmReq = new JSONObject() + .put("actorId", "12345678902"); + HttpResponse confirmRes = sendRequest("/api/v1/computeBaconPath", "GET", confirmReq.toString()); + + if(deleteKB){ + sendRequest("/api/v1/deleteActor", "DELETE", setupReq1.toString()); + } + sendRequest("/api/v1/deleteActor", "DELETE", setupReq2.toString()); + sendRequest("/api/v1/deleteMovie", "DELETE", setupReq3.toString()); + + assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); + } + + @Test + public void computeBaconPathFail() throws JSONException, IOException, InterruptedException { + JSONObject confirmReq = new JSONObject() + .put("name", "Kevin Bacon"); + HttpResponse confirmRes = sendRequest("/api/v1/computeBaconPath", "GET", confirmReq.toString()); + + assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode()); + } }