Skip to content

Commit

Permalink
addActor: pr into dev (#1)
Browse files Browse the repository at this point in the history
* addActor: Incorporated tut code

* addActor: Implemented addActor

* addActor: Created test for addActorPass
  • Loading branch information
BillyZ1435 authored Oct 26, 2022
1 parent b52ed5a commit 84a9374
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/main/java/ca/utoronto/utm/mcs/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import io.github.cdimascio.dotenv.Dotenv;
import java.io.IOException;
import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress;

public class App
{
static int port = 8080;

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);

Expand Down
31 changes: 30 additions & 1 deletion src/main/java/ca/utoronto/utm/mcs/Neo4jDAO.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
66 changes: 66 additions & 0 deletions src/main/java/ca/utoronto/utm/mcs/ReqHandler.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
33 changes: 33 additions & 0 deletions src/test/java/ca/utoronto/utm/mcs/AppTest.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> confirmRes = sendRequest("/api/v1/addActor", "POST", confirmReq.toString());
assertEquals(HttpURLConnection.HTTP_OK, confirmRes.statusCode());
}
}

0 comments on commit 84a9374

Please sign in to comment.