diff --git a/src/main/java/ca/utoronto/utm/mcs/App.java b/src/main/java/ca/utoronto/utm/mcs/App.java index ab4cd5c..f087b96 100644 --- a/src/main/java/ca/utoronto/utm/mcs/App.java +++ b/src/main/java/ca/utoronto/utm/mcs/App.java @@ -2,6 +2,8 @@ import io.github.cdimascio.dotenv.Dotenv; import java.io.IOException; +import com.sun.net.httpserver.HttpServer; +import java.net.InetSocketAddress; public class App { @@ -9,6 +11,10 @@ public class App public static void main(String[] args) throws IOException { + HttpServer server = HttpServer.create(new InetSocketAddress("0.0.0.0", port), 0); + server.createContext("/api/v1/", new ReqHandler()); + server.start(); + // TODO Create Your Server Context Here, There Should Only Be One Context System.out.printf("Server started on port %d\n", port); diff --git a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java index 767a431..a72043e 100644 --- a/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java +++ b/src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java @@ -1,7 +1,36 @@ package ca.utoronto.utm.mcs; +import org.neo4j.driver.*; + // All your database transactions or queries should // go in this class public class Neo4jDAO { - // TODO Complete This Class + 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"; + + + public Neo4jDAO() { + this.driver = GraphDatabase.driver(this.uriDb, AuthTokens.basic(this.username, this.password)); + this.session = this.driver.session(); + + } + + public boolean addActor(String name, String actorId) { + 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 Found: " + actorId); + return false; + } + query = "CREATE (a:actor {name: \"%s\", actorId: \"%s\"})"; + query = String.format(query, name, actorId); + this.session.run(query); + return true; + } } diff --git a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java index 85a5aed..e90ca21 100644 --- a/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java +++ b/src/main/java/ca/utoronto/utm/mcs/ReqHandler.java @@ -1,15 +1,81 @@ package ca.utoronto.utm.mcs; import java.io.IOException; +import java.net.URI; + import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; +import org.json.JSONException; +import org.json.*; public class ReqHandler implements HttpHandler { // TODO Complete This Class + public Neo4jDAO dao; + + public ReqHandler() { + this.dao = new Neo4jDAO(); + } + @Override 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 { + switch (exchange.getRequestMethod()) { + case "GET": + break; + case "POST": + switch (request) { + case "addActor": + this.addActor(exchange); + break; + default: + break; + } + break; + default: + break; + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void addActor(HttpExchange r) throws IOException, JSONException { + String body = Utils.convert(r.getRequestBody()); + try { + JSONObject deserialized = new JSONObject(body); + + String name, actorId; + + if (deserialized.length() == 2 && deserialized.has("name") && deserialized.has("actorId")) { + name = deserialized.getString("name"); + actorId = deserialized.getString("actorId"); + } else { + r.sendResponseHeaders(400, -1); + return; + } + + try { + if(this.dao.addActor(name, actorId) == 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); + } } } \ 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 9359797..9e49f18 100644 --- a/src/test/java/ca/utoronto/utm/mcs/AppTest.java +++ b/src/test/java/ca/utoronto/utm/mcs/AppTest.java @@ -1,15 +1,48 @@ package ca.utoronto.utm.mcs; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; 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; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + // TODO Please Write Your Tests For CI/CD In This Class. You will see // these tests pass/fail on github under github actions. public class AppTest { + final static String API_URL = "http://localhost:8080"; + + private static HttpResponse sendRequest(String endpoint, String method, String reqBody) throws IOException, InterruptedException { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(API_URL + endpoint)) + .method(method, HttpRequest.BodyPublishers.ofString(reqBody)) + .build(); + + return client.send(request, HttpResponse.BodyHandlers.ofString()); + } + + @Test public void exampleTest() { assertTrue(true); } + @Test + public void addActorPass() throws JSONException, IOException, InterruptedException { + JSONObject confirmReq = new JSONObject() + .put("name", "TestActor") + .put("secondNumber", "1234567890"); + HttpResponse confirmRes = sendRequest("/api/v1/addActor", "POST", confirmReq.toString()); + assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode()); + } }