Skip to content

Commit

Permalink
Merge pull request #10 from UTSCCSCC01/feature/getRequests
Browse files Browse the repository at this point in the history
Feature/get requests fix app tests
  • Loading branch information
Amarygdala authored Oct 28, 2022
2 parents 03fc9f6 + 1280ed1 commit b649b09
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 42 deletions.
26 changes: 6 additions & 20 deletions src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public int addActor(String Name, String id) {
query = String.format(query, id);
Result result = this.session.run(query);
if(result.hasNext()){
System.out.println("Actor Found: " + id);
return 400;
}
query = "CREATE (a:actor {Name: \"%s\", id: \"%s\"})";
Expand All @@ -44,7 +43,6 @@ public int addMovie(String Name, String id) {
query = String.format(query, id);
Result result = this.session.run(query);
if(result.hasNext()){
System.out.println("Movie Found: " + id);
return 400;
}
query = "CREATE (m:movie {Name: \"%s\", id: \"%s\"})";
Expand All @@ -59,23 +57,20 @@ public int addRelationship(String actorId, String movieId) {
query = String.format(query, actorId);
Result result = this.session.run(query);
if(!result.hasNext()){
System.out.println("Actor not found: " + actorId);
return 404;
}

query = "MATCH (m:movie { id: \"%s\"}) RETURN m.id";
query = String.format(query, movieId);
result = this.session.run(query);
if(!result.hasNext()){
System.out.println("Movie not found: " + movieId);
return 404;
}

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: " + actorId +"->"+ movieId);
return 400;
}

Expand All @@ -93,7 +88,6 @@ public String getActor(String id) throws JSONException {

Result result = this.session.run(query);
if(!result.hasNext()){
System.out.println("No actor with this ID");
return "404";
}
List<Record> resultValues = result.list();
Expand All @@ -114,17 +108,16 @@ public String getActor(String id) throws JSONException {
public String getMovie(String movieId) throws JSONException {
JSONObject response = new JSONObject();
String query;
query = "MATCH (m:movie { id: \"%s\"}) RETURN m.name";
query = "MATCH (m:movie { id: \"%s\"}) RETURN m.Name";
query = String.format(query, movieId);

Result result = this.session.run(query);
if(!result.hasNext()){
System.out.println("No movie with this ID");
return "404";
}
List<Record> resultValues = result.list();
response.put("movieId", movieId);
response.put("name", resultValues.get(0).get("m.name").asString());
response.put("name", resultValues.get(0).get("m.Name").asString());

query = "MATCH (a:actor)-[r:ACTED_IN]->(m:movie { id: \"%s\"}) RETURN a.id";
query = String.format(query, movieId);
Expand All @@ -139,19 +132,17 @@ public String hasRelationship(String actorId, String movieId) throws JSONExcepti
JSONObject response = new JSONObject();
String query;

query = "MATCH (m:movie { id: \"%s\"}) RETURN m.name";
query = "MATCH (m:movie { id: \"%s\"}) RETURN m.Name";
query = String.format(query, movieId);
Result result = this.session.run(query);
if(!result.hasNext()){
System.out.println("No movie with this ID");
return "404";
}

query = "MATCH (a:actor { id: \"%s\"}) RETURN a.name";
query = "MATCH (a:actor { id: \"%s\"}) RETURN a.Name";
query = String.format(query, actorId);
result = this.session.run(query);
if(!result.hasNext()){
System.out.println("No actor with this ID");
return "404";
}

Expand Down Expand Up @@ -183,7 +174,6 @@ public String computeBaconNumber(String actorId) throws JSONException{

Result result = this.session.run(query);
if(!result.hasNext()){
System.out.println("No path");
return "404";
}
Record record = result.next();
Expand All @@ -210,9 +200,7 @@ public String computeBaconPath(String actorId) throws JSONException {
query = String.format(query, actorId, kevinBid);

Result result = this.session.run(query);
System.out.println(result);
if(!result.hasNext()){
System.out.println("No path");
return "404";
}
Record record = result.next();
Expand All @@ -224,19 +212,17 @@ public String computeBaconPath(String actorId) throws JSONException {
}

public void deleteMovie(String movieId) throws JSONException {
JSONObject response = new JSONObject();
String query;

query = "MATCH (m:movie { id: \"%s\"}) DETACH DELETE m";
query = String.format(query, movieId);
Result result = this.session.run(query);
this.session.run(query);
}

public void deleteActor(String actorId) throws JSONException {
JSONObject response = new JSONObject();
String query;
query = "MATCH (a:actor { id: \"%s\"}) DETACH DELETE a";
query = String.format(query, actorId);
Result result = this.session.run(query);
this.session.run(query);
}
}
33 changes: 32 additions & 1 deletion src/main/java/ca/utoronto/utm/mcs/ReqHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public void handle(HttpExchange exchange) throws IOException {

URI requestURI = exchange.getRequestURI();
String request = requestURI.toString().split("/")[3];
System.out.println(request);
try {
switch (exchange.getRequestMethod()) {
case "GET":
Expand Down Expand Up @@ -101,6 +100,10 @@ public void addActor(HttpExchange r) throws IOException, JSONException {
if (deserialized.has("name") && deserialized.has("actorId")) {
name = deserialized.getString("name");
actorId = deserialized.getString("actorId");
if(name.isEmpty() || actorId.isEmpty()){
r.sendResponseHeaders(400, -1);
return;
}
} else {
r.sendResponseHeaders(400, -1);
return;
Expand Down Expand Up @@ -130,6 +133,10 @@ public void addMovie(HttpExchange r) throws IOException, JSONException {
if (deserialized.has("name") && deserialized.has("movieId")) {
name = deserialized.getString("name");
movieId = deserialized.getString("movieId");
if(name.isEmpty() || movieId.isEmpty()){
r.sendResponseHeaders(400, -1);
return;
}
} else {
r.sendResponseHeaders(400, -1);
return;
Expand Down Expand Up @@ -159,6 +166,10 @@ public void addRelationship(HttpExchange r) throws IOException, JSONException {
if (deserialized.has("actorId") && deserialized.has("movieId")) {
actorId = deserialized.getString("actorId");
movieId = deserialized.getString("movieId");
if(movieId.isEmpty() || actorId.isEmpty()){
r.sendResponseHeaders(400, -1);
return;
}
} else {
r.sendResponseHeaders(400, -1);
return;
Expand Down Expand Up @@ -187,6 +198,10 @@ public void getActor(HttpExchange r) throws IOException {

if (deserialized.has("actorId")) {
actorId = deserialized.getString("actorId");
if(actorId.isEmpty()){
r.sendResponseHeaders(400, -1);
return;
}
} else {
r.sendResponseHeaders(400, -1);
return;
Expand Down Expand Up @@ -220,6 +235,10 @@ public void getMovie(HttpExchange r) throws IOException {

if (deserialized.has("movieId")) {
movieId = deserialized.getString("movieId");
if(movieId.isEmpty()){
r.sendResponseHeaders(400, -1);
return;
}
} else {
r.sendResponseHeaders(400, -1);
return;
Expand Down Expand Up @@ -253,6 +272,10 @@ public void hasRelationship(HttpExchange r) throws IOException {
if (deserialized.has("actorId") && deserialized.has("movieId")) {
movieId = deserialized.getString("movieId");
actorId = deserialized.getString("actorId");
if(movieId.isEmpty() || actorId.isEmpty()){
r.sendResponseHeaders(400, -1);
return;
}
} else {
r.sendResponseHeaders(400, -1);
return;
Expand Down Expand Up @@ -286,6 +309,10 @@ public void computeBaconNumber(HttpExchange r) throws IOException, JSONException

if (deserialized.has("actorId")) {
actorId = deserialized.getString("actorId");
if(actorId.isEmpty()){
r.sendResponseHeaders(400, -1);
return;
}
} else {

r.sendResponseHeaders(400, -1);
Expand Down Expand Up @@ -320,6 +347,10 @@ public void computeBaconPath(HttpExchange r) throws IOException, JSONException{

if (deserialized.has("actorId")) {
actorId = deserialized.getString("actorId");
if(actorId.isEmpty()){
r.sendResponseHeaders(400, -1);
return;
}
} else {
r.sendResponseHeaders(400, -1);
return;
Expand Down
30 changes: 9 additions & 21 deletions src/test/java/ca/utoronto/utm/mcs/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void exampleTest() {
public void addActorPass() throws JSONException, IOException, InterruptedException {
JSONObject confirmReq = new JSONObject()
.put("name", "TestActor")
.put("actorId", "12345678901");
.put("actorId", "123123");
HttpResponse<String> confirmRes = sendRequest("/api/v1/addActor", "PUT", confirmReq.toString());
sendRequest("/api/v1/deleteActor", "DELETE", confirmReq.toString());
assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode());
Expand Down Expand Up @@ -135,30 +135,22 @@ public void getMoviePass() throws JSONException, IOException, InterruptedExcepti
JSONObject setupReq = new JSONObject()
.put("name", "TestMovie")
.put("movieId", "12345678901");
HttpResponse<String> setupRes = sendRequest("/api/v1/addMovie", "PUT", setupReq.toString());
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());
sendRequest("/api/v1/deleteMovie", "DELETE", confirmReq.toString());
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<String> confirmRes = sendRequest("/api/v1/getMovie", "GET", confirmReq.toString());

sendRequest("/api/v1/deleteMovie", "DELETE", setupReq.toString());
assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode());
}

Expand All @@ -167,29 +159,25 @@ public void hasRelationshipPass() throws JSONException, IOException, Interrupted
JSONObject setupReq = new JSONObject()
.put("name", "TestMovie")
.put("movieId", "12345678901");
HttpResponse<String> setupRes = sendRequest("/api/v1/addMovie", "PUT", setupReq.toString());
sendRequest("/api/v1/addMovie", "PUT", setupReq.toString());

setupReq = new JSONObject()
setupReq = new JSONObject()
.put("name", "TestActor")
.put("actorId", "12345678902");
setupRes = sendRequest("/api/v1/addActor", "PUT", setupReq.toString());
sendRequest("/api/v1/addActor", "PUT", setupReq.toString());

setupReq = new JSONObject()
.put("movieId", "12345678901")
.put("actorId", "12345678902");
setupRes = sendRequest("/api/v1/addRelationship", "PUT", setupReq.toString());
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());
sendRequest("/api/v1/deleteMovie", "DELETE", confirmReq.toString());
sendRequest("/api/v1/deleteActor", "DELETE", confirmReq.toString());
assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode());
}

Expand Down

0 comments on commit b649b09

Please sign in to comment.