From 725a6f071c99c126d6a703a00f452cbe40f37ded Mon Sep 17 00:00:00 2001 From: Raymond Weng Date: Thu, 27 Oct 2022 18:57:03 -0400 Subject: [PATCH 1/3] deleteActor, deteMovie: Added api and tests Added new api to delete actor and movie for the tests. Added 200 status tests for getActor getMovie and hasRelationship --- .../java/ca/utoronto/utm/mcs/Neo4jDAO.java | 17 +++++ .../java/ca/utoronto/utm/mcs/ReqHandler.java | 50 +++++++++++++++ .../java/ca/utoronto/utm/mcs/AppTest.java | 64 +++++++++++++++++++ 3 files changed, 131 insertions(+) diff --git a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java index 0c242c4..0c19d6d 100644 --- a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java +++ b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java @@ -169,4 +169,21 @@ public String hasRelationship(String actorId, String movieId) throws JSONExcepti } return response.toString(); } + + public void deleteMovie(String movieId) throws JSONException { + JSONObject response = new JSONObject(); + String query; + + query = "MATCH (m:movie { movieId: \"%s\"}) DETACH DELETE m"; + query = String.format(query, movieId); + Result result = this.session.run(query); + } + + public void deleteActor(String actorId) throws JSONException { + JSONObject response = new JSONObject(); + String query; + query = "MATCH (a:actor { actorId: \"%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 8eba8c2..116552c 100644 --- a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java +++ b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java @@ -61,6 +61,18 @@ public void handle(HttpExchange exchange) throws IOException { break; } break; + case "DELETE": + switch (request) { + case "deleteActor": + this.deleteActor(exchange); + break; + case "deleteMovie": + this.deleteMovie(exchange); + break; + default: + break; + } + break; default: break; } @@ -261,4 +273,42 @@ public void hasRelationship(HttpExchange r) throws IOException { r.sendResponseHeaders(500, -1); } } + public void deleteActor(HttpExchange r) throws IOException { + String body = Utils.convert(r.getRequestBody()); + try { + JSONObject deserialized = new JSONObject(body); + String actorId; + + if (deserialized.has("actorId")) { + actorId = deserialized.getString("actorId"); + } else { + r.sendResponseHeaders(400, -1); + return; + } + this.dao.deleteActor(actorId); + r.sendResponseHeaders(200, -1); + } catch (Exception e) { + e.printStackTrace(); + r.sendResponseHeaders(500, -1); + } + } + public void deleteMovie(HttpExchange r) throws IOException { + String body = Utils.convert(r.getRequestBody()); + try { + JSONObject deserialized = new JSONObject(body); + String movieId; + + if (deserialized.has("movieId")) { + movieId = deserialized.getString("movieId"); + } else { + r.sendResponseHeaders(400, -1); + return; + } + this.dao.deleteMovie(movieId); + r.sendResponseHeaders(200, -1); + } catch (Exception e) { + e.printStackTrace(); + r.sendResponseHeaders(500, -1); + } + } } \ 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 ed527a0..ecc88ac 100644 --- a/src/test/java/ca/utoronto/utm/mcs/AppTest.java +++ b/src/test/java/ca/utoronto/utm/mcs/AppTest.java @@ -78,4 +78,68 @@ public void addRelationshipPass() throws JSONException, IOException, Interrupted 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()); + + 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()); + assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); + } + + @Test + public void getMoviePass() throws JSONException, IOException, InterruptedException { + JSONObject setupReq = new JSONObject() + .put("name", "TestMovie") + .put("movieId", "12345678901"); + HttpResponse setupRes = sendRequest("/api/v1/addMovie", "PUT", setupReq.toString()); + + JSONObject confirmReq = new JSONObject() + .put("movieId", "12345678901"); + HttpResponse confirmRes = sendRequest("/api/v1/getMovie", "GET", confirmReq.toString()); + + JSONObject deleteReq = new JSONObject() + .put("movieId", "12345678901"); + HttpResponse deleteRes = sendRequest("/api/v1/deleteMovie", "DELETE", confirmReq.toString()); + assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); + } + + @Test + public void hasRelationshipPass() throws JSONException, IOException, InterruptedException { + JSONObject setupReq = new JSONObject() + .put("name", "TestMovie") + .put("movieId", "12345678901"); + HttpResponse setupRes = sendRequest("/api/v1/addMovie", "PUT", setupReq.toString()); + + setupReq = new JSONObject() + .put("name", "TestActor") + .put("actorId", "12345678902"); + setupRes = sendRequest("/api/v1/addActor", "PUT", setupReq.toString()); + + setupReq = new JSONObject() + .put("movieId", "12345678901") + .put("actorId", "12345678902"); + setupRes = sendRequest("/api/v1/addRelationship", "PUT", setupReq.toString()); + + JSONObject confirmReq = new JSONObject() + .put("movieId", "12345678901") + .put("actorId", "12345678902"); + HttpResponse confirmRes = sendRequest("/api/v1/hasRelationship", "GET", confirmReq.toString()); + + JSONObject deleteReq = new JSONObject() + .put("movieId", "12345678901"); + HttpResponse deleteRes = sendRequest("/api/v1/deleteMovie", "DELETE", confirmReq.toString()); + deleteReq = new JSONObject() + .put("actorId", "12345678902"); + deleteRes = sendRequest("/api/v1/deleteActor", "DELETE", confirmReq.toString()); + assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); + } } From 7fd324e6e9f2f608059882782bbafbff7300d1ba Mon Sep 17 00:00:00 2001 From: BillyZ1435 Date: Thu, 27 Oct 2022 19:04:40 -0400 Subject: [PATCH 2/3] getRequests: implemented bacon path and number --- .../java/ca/utoronto/utm/mcs/Neo4jDAO.java | 119 +++++++++++++----- .../java/ca/utoronto/utm/mcs/ReqHandler.java | 86 +++++++++++-- 2 files changed, 162 insertions(+), 43 deletions(-) diff --git a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java index 0ad3921..50b7078 100644 --- a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java +++ b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java @@ -3,7 +3,7 @@ import org.json.*; import org.neo4j.driver.*; import org.neo4j.driver.Record; - +import org.neo4j.driver.types.Path; import java.util.List; @@ -15,9 +15,7 @@ public class Neo4jDAO { private final Session session; private final Driver driver; - private final String uriDb = "bolt://localhost:7687"; - private final String username = "neo4j"; - private final String password = "123456"; + private final String kevinBid = "nm0000102"; @Inject public Neo4jDAO(Driver driver) { @@ -26,39 +24,39 @@ public Neo4jDAO(Driver driver) { } - public boolean addActor(String name, String actorId) { + public int addActor(String Name, String id) { String query; - query = "MATCH (a:actor { actorId: \"%s\"}) RETURN a.actorId"; - query = String.format(query, actorId); + query = "MATCH (a:actor { id: \"%s\"}) RETURN a.id"; + query = String.format(query, id); Result result = this.session.run(query); if(result.hasNext()){ - System.out.println("Actor Found: " + actorId); - return false; + System.out.println("Actor Found: " + id); + return 400; } - query = "CREATE (a:actor {name: \"%s\", actorId: \"%s\"})"; - query = String.format(query, name, actorId); + query = "CREATE (a:actor {Name: \"%s\", id: \"%s\"})"; + query = String.format(query, Name, id); this.session.run(query); - return true; + return 200; } - public boolean addMovie(String name, String movieId) { + public int addMovie(String Name, String id) { String query; - query = "MATCH (m:movie { movieId: \"%s\"}) RETURN m.movieId"; - query = String.format(query, movieId); + query = "MATCH (m:movie { id: \"%s\"}) RETURN m.id"; + query = String.format(query, id); Result result = this.session.run(query); if(result.hasNext()){ - System.out.println("Movie Found: " + movieId); - return false; + System.out.println("Movie Found: " + id); + return 400; } - query = "CREATE (m:movie {name: \"%s\", movieId: \"%s\"})"; - query = String.format(query, name, movieId); + query = "CREATE (m:movie {Name: \"%s\", id: \"%s\"})"; + query = String.format(query, Name, id); this.session.run(query); - return true; + return 200; } public int addRelationship(String actorId, String movieId) { String query; - query = "MATCH (a:actor { actorId: \"%s\"}) RETURN a.actorId"; + query = "MATCH (a:actor { id: \"%s\"}) RETURN a.id"; query = String.format(query, actorId); Result result = this.session.run(query); if(!result.hasNext()){ @@ -66,7 +64,7 @@ public int addRelationship(String actorId, String movieId) { return 404; } - query = "MATCH (m:movie { movieId: \"%s\"}) RETURN m.movieId"; + query = "MATCH (m:movie { id: \"%s\"}) RETURN m.id"; query = String.format(query, movieId); result = this.session.run(query); if(!result.hasNext()){ @@ -74,25 +72,25 @@ public int addRelationship(String actorId, String movieId) { return 404; } - query = "MATCH (a:actor {actorId: \"%s\"})-[:ACTED_IN]->(m:movie {movieId: \"%s\"}) RETURN a.actorId"; + query = "MATCH (a:actor {id: \"%s\"})-[:ACTED_IN]->(m:movie {id: \"%s\"}) RETURN a.id"; query = String.format(query, actorId, movieId); result = this.session.run(query); if(result.hasNext()){ - System.out.println("Relationship Found: " + movieId); + System.out.println("Relationship Found: " + actorId +"->"+ movieId); return 400; } - query = "MATCH (a:actor {actorId: \"%s\"}), (m:movie {movieId: \"%s\"}) CREATE (a)-[r:ACTED_IN]->(m)"; + query = "MATCH (a:actor {id: \"%s\"}), (m:movie {id: \"%s\"}) CREATE (a)-[r:ACTED_IN]->(m)"; query = String.format(query, actorId, movieId); this.session.run(query); return 200; } - public String getActor(String actorId) throws JSONException { + public String getActor(String id) throws JSONException { JSONObject response = new JSONObject(); String query; - query = "MATCH (a:actor { actorId: \"%s\"}) RETURN a.name"; - query = String.format(query, actorId); + query = "MATCH (a:actor { id: \"%s\"}) RETURN a.id"; + query = String.format(query, id); Result result = this.session.run(query); if(!result.hasNext()){ @@ -100,16 +98,71 @@ public String getActor(String actorId) throws JSONException { return "404"; } List resultValues = result.list(); - response.put("actorId", actorId); - response.put("name", resultValues.get(0).get("a.name").asString()); + response.put("id", id); + response.put("Name", resultValues.get(0).get("a.Name").asString()); - query = "MATCH (a:actor { actorId: \"%s\"})-[r:ACTED_IN]->(m:movie) RETURN m.movieId";//maybe change Movie to lowercase - query = String.format(query, actorId); + query = "MATCH (a:actor { id: \"%s\"})-[r:ACTED_IN]->(m:movie) RETURN m.id";//maybe change Movie to lowercase + query = String.format(query, id); result = this.session.run(query); resultValues = result.list(); JSONArray movies = new JSONArray(); - resultValues.forEach((record)->{movies.put(record.get("m.movieId").asString());}); + resultValues.forEach((record)->{movies.put(record.get("m.id").asString());}); + //movies.put(record.get("m.id").asString()); response.put("movies", movies); return response.toString(); } + + public String computeBaconNumber(String actorId) throws JSONException{ + JSONObject response = new JSONObject(); + String query; + int baconNumber = 0; + + if(actorId.equals(kevinBid)){ + response.put("baconNumber", 0); + return response.toString(); + } + query = "MATCH (a:actor {id: \"%s\"}), (KevinB:actor {id: \"%s\"} ), p = shortestPath((a)-[:ACTED_IN*]-(KevinB)) RETURN nodes(p)"; + query = String.format(query, actorId, kevinBid); + + Result result = this.session.run(query); + if(!result.hasNext()){ + System.out.println("No path"); + return "404"; + } + Record record = result.next(); + for(int i=0; i Date: Thu, 27 Oct 2022 23:13:10 -0400 Subject: [PATCH 3/3] getRequests: added tests for get request response codes --- src/main/java/ca/utoronto/utm/mcs/App.java | 2 - .../java/ca/utoronto/utm/mcs/Neo4jDAO.java | 5 +- .../java/ca/utoronto/utm/mcs/ReqHandler.java | 13 +- .../java/ca/utoronto/utm/mcs/AppTest.java | 230 ++++++++++++++++-- 4 files changed, 220 insertions(+), 30 deletions(-) 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()); + } }