Skip to content
Snippets Groups Projects
Commit 82724562 authored by Sven Kästle's avatar Sven Kästle
Browse files

refactor: Adjust controller and test classes

parent f776bdcc
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@ import unipotsdam.gf.core.database.mysql.MysqlConnect;
import unipotsdam.gf.core.database.mysql.VereinfachtesResultSet;
import unipotsdam.gf.interfaces.IAnnotation;
import unipotsdam.gf.modules.annotation.model.Annotation;
import unipotsdam.gf.modules.annotation.model.AnnotationBody;
import unipotsdam.gf.modules.annotation.model.AnnotationPatchRequest;
import unipotsdam.gf.modules.annotation.model.AnnotationPostRequest;
......@@ -26,8 +27,8 @@ public class AnnotationController implements IAnnotation {
connection.connect();
// build and execute request
String request = "INSERT INTO annotations (`id`, `userId`, `targetId`, `body`, `startCharacter`, `endCharacter`) VALUES (?,?,?,?,?,?);";
connection.issueInsertOrDeleteStatement(request, uuid, annotationPostRequest.getUserId(), annotationPostRequest.getTargetId(), annotationPostRequest.getBody(), annotationPostRequest.getStartCharacter(), annotationPostRequest.getEndCharacter());
String request = "INSERT INTO annotations (`id`, `userToken`, `targetId`, `title`, `comment`, `startCharacter`, `endCharacter`) VALUES (?,?,?,?,?,?,?);";
connection.issueInsertOrDeleteStatement(request, uuid, annotationPostRequest.getUserToken(), annotationPostRequest.getTargetId(), annotationPostRequest.getBody().getTitle(), annotationPostRequest.getBody().getComment(),annotationPostRequest.getBody().getStartCharacter(), annotationPostRequest.getBody().getEndCharacter());
// close connection
connection.close();
......@@ -47,8 +48,8 @@ public class AnnotationController implements IAnnotation {
connection.connect();
// build and execute request
String request = "UPDATE `annotations` SET `body` = ? WHERE `id` = ?";
connection.issueUpdateStatement(request, annotationPatchRequest.getBody(), annotationId);
String request = "UPDATE `annotations` SET `title` = ?, `comment` = ? WHERE `id` = ?";
connection.issueUpdateStatement(request, annotationPatchRequest.getTitle(), annotationPatchRequest.getComment(), annotationId);
// close connection
connection.close();
......@@ -159,17 +160,27 @@ public class AnnotationController implements IAnnotation {
}
/**
* Build an annotation object from a given result set
*
* @param rs The result set from a database query
* @return A new annotation object
*/
private Annotation getAnnotationFromResultSet(VereinfachtesResultSet rs) {
String id = rs.getString("id");
long timestamp = rs.getTimestamp(2).getTime();
int userId = rs.getInt("userId");
String userToken = rs.getString("userToken");
int targetId = rs.getInt("targetId");
String body = rs.getString("body");
// initialize new annotation body
String title = rs.getString("title");
String comment = rs.getString("comment");
int startCharacter = rs.getInt("startCharacter");
int endCharacter = rs.getInt("endCharacter");
AnnotationBody body = new AnnotationBody(title, comment, startCharacter, endCharacter);
return new Annotation(id, timestamp, userId, targetId, body, startCharacter, endCharacter);
return new Annotation(id, timestamp, userToken, targetId, body);
}
}
......@@ -4,6 +4,7 @@ import org.junit.Before;
import org.junit.Test;
import unipotsdam.gf.modules.annotation.controller.AnnotationController;
import unipotsdam.gf.modules.annotation.model.Annotation;
import unipotsdam.gf.modules.annotation.model.AnnotationBody;
import unipotsdam.gf.modules.annotation.model.AnnotationPatchRequest;
import unipotsdam.gf.modules.annotation.model.AnnotationPostRequest;
......@@ -31,11 +32,12 @@ public class AnnotationTest {
@Test
public void testAddAnnotation() {
// initialize body
String body = "body_testAddAnnotation";
// initialize title and comment of body
String title = "title_testAddAnnotation";
String comment = "comment_testAddAnnotation";
// prepare and execute request
AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest(1, 2, body, 4, 5);
AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 1, new AnnotationBody(title, comment, 1, 2));
Annotation response = controller.addAnnotation(annotationPostRequest);
// the new annotation should be found in the database
......@@ -49,44 +51,52 @@ public class AnnotationTest {
@Test
public void testAlterAnnotation() {
// initialize old and new body
String oldBody = "bodyOld_testAlterAnnotation";
String newBody = "bodyNew_testAlterAnnotation";
// initialize old and new title and comment of body
String titleOld = "titleOld_testAlterAnnotation";
String commentOld = "commentOld_testAlterAnnotation";
String titleNew = "titleNew_testAlterAnnotation";
String commentNew = "commentNew_testAlterAnnotation";
// save new annotation in database
AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest(0, 0, oldBody, 0, 0);
AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 0, new AnnotationBody(titleOld, commentOld, 1, 2));
Annotation response = controller.addAnnotation(annotationPostRequest);
// the new annotation should be found in the database
assertTrue("Can't find annotation with the id " + response.getId(), controller.existsAnnotationId(response.getId()));
// the annotation's body should be "testAlterAnnotation_oldBody"
assertEquals("The body of the annotation should be " + oldBody + " but was " + response.getBody(), oldBody, response.getBody());
// the annotation's title should be "titleOld_testAlterAnnotation"
assertEquals("The title of the annotation should be " + titleOld + " but was " + response.getBody().getTitle(), titleOld, response.getBody().getTitle());
// the annotation's comment should be "commentOld_testAlterAnnotation"
assertEquals("The comment of the annotation should be " + commentOld + " but was " + response.getBody().getComment(), commentOld, response.getBody().getComment());
// alter the annotation and update the database
AnnotationPatchRequest annotationPatchRequest = new AnnotationPatchRequest(newBody);
AnnotationPatchRequest annotationPatchRequest = new AnnotationPatchRequest(titleNew, commentNew);
controller.alterAnnotation(response.getId(), annotationPatchRequest);
// receive the new annotation
Annotation newResponse = controller.getAnnotation(response.getId());
// the annotation's body should be "testAlterAnnotation_newBody"
assertEquals("The body of the annotation should be " + newBody + " but was " + newResponse.getBody(), newBody, newResponse.getBody());
// the annotation's title should be "titleNew_testAlterAnnotation"
assertEquals("The title of the annotation should be " + titleNew + " but was " + newResponse.getBody().getTitle(), titleNew, newResponse.getBody().getTitle());
// the annotation's comment should be "commentNew_testAlterAnnotation"
assertEquals("The comment of the annotation should be " + commentNew + " but was " + newResponse.getBody().getComment(), commentNew, newResponse.getBody().getComment());
// delete the annotation
controller.deleteAnnotation(response.getId());
}
@Test
public void testDeleteAnnotation() {
// initialize old and new body
String body = "body_testDeleteAnnotation";
// initialize title and comment of body
String title = "title_testDeleteAnnotation";
String comment = "comment_testDeleteAnnotation";
// save new annotation in database
AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest(0, 0, body, 0, 0);
// prepare and execute request
AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 1, new AnnotationBody(title, comment, 1, 2));
Annotation response = controller.addAnnotation(annotationPostRequest);
// the new annotation should be found in the database
......@@ -103,18 +113,22 @@ public class AnnotationTest {
@Test
public void testGetAnnotation() {
// initialize body
String body = "body_testGetAnnotation";
// initialize title and comment of body
String title = "title_testGetAnnotation";
String comment = "comment_testGetAnnotation";
// save new annotation in database
AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest(0, 0, body, 0, 0);
// prepare and execute request
AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 1, new AnnotationBody(title, comment, 1, 2));
Annotation response = controller.addAnnotation(annotationPostRequest);
// receive the new annotation
Annotation getResponse = controller.getAnnotation(response.getId());
// the annotation's body should be "testAlterAnnotation_newBody"
assertEquals("The body of the annotation should be " + body + " but was " + getResponse.getBody(), body, getResponse.getBody());
// the annotation's title should be "title_testAlterAnnotation"
assertEquals("The title of the annotation should be " + title + " but was " + response.getBody().getTitle(), title, response.getBody().getTitle());
// the annotation's comment should be "comment_testAlterAnnotation"
assertEquals("The comment of the annotation should be " + comment + " but was " + response.getBody().getComment(), comment, response.getBody().getComment());
// delete the new annotation
controller.deleteAnnotation(response.getId());
......@@ -124,10 +138,9 @@ public class AnnotationTest {
@Test
public void testGetAnnotations() {
// initialize bodys
String body1 = "body1_testGetAnnotations";
String body2 = "body2_testGetAnnotations";
String body3 = "body3_testGetAnnotations";
// initialize title and comment of bodys
String title = "title_testGetAnnotations";
String comment = "comment_testGetAnnotations";
// initialize targetIds
ArrayList<Integer> targetIds = new ArrayList<>();
......@@ -135,23 +148,23 @@ public class AnnotationTest {
targetIds.add(-2);
// save new annotations in database
AnnotationPostRequest request1 = new AnnotationPostRequest(0, targetIds.get(0), body1, 0, 0);
AnnotationPostRequest request2 = new AnnotationPostRequest(0, targetIds.get(1), body2, 0, 0);
AnnotationPostRequest request3 = new AnnotationPostRequest(0, targetIds.get(1), body3, 0, 0);
AnnotationPostRequest request1 = new AnnotationPostRequest("userToken", targetIds.get(0), new AnnotationBody(title, comment, 1, 2));
AnnotationPostRequest request2 = new AnnotationPostRequest("userToken", targetIds.get(1), new AnnotationBody(title, comment, 1, 2));
AnnotationPostRequest request3 = new AnnotationPostRequest("userToken", targetIds.get(1), new AnnotationBody(title, comment, 1, 2));
controller.addAnnotation(request1);
controller.addAnnotation(request2);
controller.addAnnotation(request3);
// receive the new annotations
// receive the new annotations with targetId = -2
ArrayList<Annotation> getResponse = controller.getAnnotations(targetIds.get(1));
// the size of the getResponse should be 2
// the size of the getResponse should be 2
assertEquals("The size of the response should be 2 but was " + getResponse.size(), 2, getResponse.size());
// receive the new annotations
// receive the new annotations with targetId = -1
ArrayList<Annotation> getResponseNew = controller.getAnnotations(targetIds.get(0));
// the size of the getResponse should be 2
// the size of the getResponse should be 1
assertEquals("The size of the response should be 1 but was " + getResponseNew.size(), 1, getResponseNew.size());
// delete annotations from database
......@@ -164,12 +177,13 @@ public class AnnotationTest {
@Test
public void testExistsAnnotationId() {
// initialize body and bad id
String body = "body_testExistsAnnotationId";
// initialize title and comment of body and bad id
String title = "title_testExistsAnnotationId";
String comment = "comment_testExistsAnnotationId";
String badId = "badId";
// save new annotation in database
AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest(0, 0, body, 0, 0);
AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 0, new AnnotationBody(title, comment, 1, 2));
Annotation response = controller.addAnnotation(annotationPostRequest);
// the annotation shouldn't be found in the database
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment