-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* addActor: Incorporated tut code * addActor: Implemented addActor * addActor: Created test for addActorPass
- Loading branch information
1 parent
b52ed5a
commit 84a9374
Showing
4 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |