Skip to content

Commit

Permalink
getRequests: added tests for get request response codes
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyZ1435 committed Oct 28, 2022
1 parent 1976a0e commit 46f8df5
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 30 deletions.
2 changes: 0 additions & 2 deletions src/main/java/ca/utoronto/utm/mcs/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -228,15 +227,15 @@ 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);
}

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);
}
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/ca/utoronto/utm/mcs/ReqHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -49,6 +48,7 @@ public void handle(HttpExchange exchange) throws IOException {
this.computeBaconNumber(exchange);
break;
default:
this.apiDNE(exchange);
break;
}
break;
Expand All @@ -64,6 +64,7 @@ public void handle(HttpExchange exchange) throws IOException {
this.addRelationship(exchange);
break;
default:
this.apiDNE(exchange);
break;
}
break;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
230 changes: 207 additions & 23 deletions src/test/java/ca/utoronto/utm/mcs/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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<String> 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 {
Expand All @@ -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<String> 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<String> 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 {
Expand All @@ -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<String> 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<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());
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());

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<String> 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()
Expand All @@ -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<String> 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()
Expand Down Expand Up @@ -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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> confirmRes = sendRequest("/api/v1/computeBaconPath", "GET", confirmReq.toString());

assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, confirmRes.statusCode());
}
}

0 comments on commit 46f8df5

Please sign in to comment.