Skip to content

Commit

Permalink
deleteActor, deteMovie: Added api and tests
Browse files Browse the repository at this point in the history
Added new api to delete actor and movie for the tests. Added 200 status tests for getActor getMovie and hasRelationship
  • Loading branch information
Amarygdala committed Oct 27, 2022
1 parent 229250d commit 725a6f0
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
50 changes: 50 additions & 0 deletions src/main/java/ca/utoronto/utm/mcs/ReqHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
}
64 changes: 64 additions & 0 deletions src/test/java/ca/utoronto/utm/mcs/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,68 @@ public void addRelationshipPass() throws JSONException, IOException, Interrupted
HttpResponse<String> 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<String> setupRes = sendRequest("/api/v1/addActor", "PUT", setupReq.toString());

JSONObject confirmReq = new JSONObject()
.put("actorId", "12345678901");
HttpResponse<String> confirmRes = sendRequest("/api/v1/getActor", "GET", confirmReq.toString());

JSONObject deleteReq = new JSONObject()
.put("actorId", "12345678901");
HttpResponse<String> 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<String> setupRes = sendRequest("/api/v1/addMovie", "PUT", setupReq.toString());

JSONObject confirmReq = new JSONObject()
.put("movieId", "12345678901");
HttpResponse<String> confirmRes = sendRequest("/api/v1/getMovie", "GET", confirmReq.toString());

JSONObject deleteReq = new JSONObject()
.put("movieId", "12345678901");
HttpResponse<String> 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<String> 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<String> confirmRes = sendRequest("/api/v1/hasRelationship", "GET", confirmReq.toString());

JSONObject deleteReq = new JSONObject()
.put("movieId", "12345678901");
HttpResponse<String> 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());
}
}

0 comments on commit 725a6f0

Please sign in to comment.