Skip to content

Commit

Permalink
Feature/put-requests (#4)
Browse files Browse the repository at this point in the history
* put-requests: created addMovie and addRelatioship and their tests

* put-requests: request can have extra parameters
  • Loading branch information
BillyZ1435 authored Oct 27, 2022
1 parent 3e55669 commit 92c34be
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 6 deletions.
47 changes: 47 additions & 0 deletions src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,53 @@ public boolean addActor(String name, String actorId) {
return true;
}

public boolean addMovie(String name, String movieId) {
String query;
query = "MATCH (m:movie { movieId: \"%s\"}) RETURN m.movieId";
query = String.format(query, movieId);
Result result = this.session.run(query);
if(result.hasNext()){
System.out.println("Movie Found: " + movieId);
return false;
}
query = "CREATE (m:movie {name: \"%s\", movieId: \"%s\"})";
query = String.format(query, name, movieId);
this.session.run(query);
return true;
}

public int addRelationship(String actorId, String movieId) {
String query;
query = "MATCH (a:actor { actorId: \"%s\"}) RETURN a.actorId";
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 { movieId: \"%s\"}) RETURN m.movieId";
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 {actorId: \"%s\"})-[:ACTED_IN]->(m:movie {movieId: \"%s\"}) RETURN a.actorId";
query = String.format(query, actorId, movieId);
result = this.session.run(query);
if(result.hasNext()){
System.out.println("Relationship Found: " + movieId);
return 400;
}

query = "MATCH (a:actor {actorId: \"%s\"}), (m:movie {movieId: \"%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 {
JSONObject response = new JSONObject();
String query;
Expand Down
70 changes: 69 additions & 1 deletion src/main/java/ca/utoronto/utm/mcs/ReqHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public void handle(HttpExchange exchange) throws IOException {
case "addActor":
this.addActor(exchange);
break;
case "addMovie":
this.addMovie(exchange);
break;
case "addRelationship":
this.addRelationship(exchange);
break;
default:
break;
}
Expand All @@ -62,7 +68,7 @@ public void addActor(HttpExchange r) throws IOException, JSONException {

String name, actorId;

if (deserialized.length() == 2 && deserialized.has("name") && deserialized.has("actorId")) {
if (deserialized.has("name") && deserialized.has("actorId")) {
name = deserialized.getString("name");
actorId = deserialized.getString("actorId");
} else {
Expand All @@ -87,6 +93,68 @@ public void addActor(HttpExchange r) throws IOException, JSONException {
}
}

public void addMovie(HttpExchange r) throws IOException, JSONException {
String body = Utils.convert(r.getRequestBody());
try {
JSONObject deserialized = new JSONObject(body);

String name, movieId;

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

try {
if(this.dao.addMovie(name, movieId) == false){
r.sendResponseHeaders(400, -1);
return;
}
} catch (Exception e) {
r.sendResponseHeaders(500, -1);
e.printStackTrace();
return;
}
r.sendResponseHeaders(200, -1);
} catch (Exception e) {
e.printStackTrace();
r.sendResponseHeaders(500, -1);
}
}

public void addRelationship(HttpExchange r) throws IOException, JSONException {
String body = Utils.convert(r.getRequestBody());
try {
JSONObject deserialized = new JSONObject(body);

String actorId, movieId;

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

try {
int rCode = this.dao.addRelationship(actorId, movieId);
r.sendResponseHeaders(rCode, -1);
return;
} catch (Exception e) {
r.sendResponseHeaders(500, -1);
e.printStackTrace();
return;
}
} catch (Exception e) {
e.printStackTrace();
r.sendResponseHeaders(500, -1);
}
}

public void getActor(HttpExchange r) throws IOException, JSONException {
String body = Utils.convert(r.getRequestBody());
try {
Expand Down
43 changes: 38 additions & 5 deletions src/test/java/ca/utoronto/utm/mcs/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,45 @@ public void exampleTest() {
assertTrue(true);
}

// @Test
// public void addActorPass() throws JSONException, IOException, InterruptedException {
// JSONObject confirmReq = new JSONObject()
// .put("name", "TestActor")
// .put("actorId", "12345678901");
// HttpResponse<String> confirmRes = sendRequest("/api/v1/addActor", "PUT", confirmReq.toString());
// assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode());
// }

@Test
public void addActorFail() throws JSONException, IOException, InterruptedException {
JSONObject confirmReq = new JSONObject()
.put("name", "TestActor");
HttpResponse<String> confirmRes = sendRequest("/api/v1/addActor", "PUT", confirmReq.toString());
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<String> confirmRes = sendRequest("/api/v1/addMovie", "PUT", confirmReq.toString());
// assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode());
// }

@Test
public void addMovieFail() throws JSONException, IOException, InterruptedException {
JSONObject confirmReq = new JSONObject()
.put("name", "TestMovie");
HttpResponse<String> confirmRes = sendRequest("/api/v1/addMovie", "PUT", confirmReq.toString());
assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode());
}

@Test
public void addActorPass() throws JSONException, IOException, InterruptedException {
public void addRelationshipPass() throws JSONException, IOException, InterruptedException {
JSONObject confirmReq = new JSONObject()
.put("name", "TestActor")
.put("secondNumber", "1234567890");
HttpResponse<String> confirmRes = sendRequest("/api/v1/addActor", "POST", confirmReq.toString());
assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode());
.put("movieId", "x");
HttpResponse<String> confirmRes = sendRequest("/api/v1/addRelationship", "PUT", confirmReq.toString());
assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode());
}
}

0 comments on commit 92c34be

Please sign in to comment.