Skip to content
Snippets Groups Projects
Commit 15f6e093 authored by tudtianus's avatar tudtianus
Browse files

#76 feat: unitTests for JournalDAO, fix: Bug in getAllByProject where some...

#76 feat: unitTests for JournalDAO, fix: Bug in getAllByProject where some wrong Journals where returned
parent 2924727e
No related branches found
No related tags found
No related merge requests found
...@@ -17,7 +17,7 @@ public interface JournalDAO { ...@@ -17,7 +17,7 @@ public interface JournalDAO {
Journal getJournal(String id); Journal getJournal(String id);
ArrayList<Journal> getAllByProject(String project); ArrayList<Journal> getAllByProject(String project, String student);
ArrayList<Journal> getAllByProjectAndFilter(String project, String student, JournalFilter filter); ArrayList<Journal> getAllByProjectAndFilter(String project, String student, JournalFilter filter);
......
...@@ -96,7 +96,7 @@ public class JournalDAOImpl implements JournalDAO { ...@@ -96,7 +96,7 @@ public class JournalDAOImpl implements JournalDAO {
} }
@Override @Override
public ArrayList<Journal> getAllByProject(String project) { public ArrayList<Journal> getAllByProject(String project, String student) {
ArrayList<Journal> journals = new ArrayList<>(); ArrayList<Journal> journals = new ArrayList<>();
...@@ -105,8 +105,8 @@ public class JournalDAOImpl implements JournalDAO { ...@@ -105,8 +105,8 @@ public class JournalDAOImpl implements JournalDAO {
connection.connect(); connection.connect();
// build and execute request // build and execute request
String request = "SELECT * FROM journals WHERE project= ?;"; String request = "SELECT * FROM journals WHERE project= ? AND (author = ? OR visibility = \"ALL\" or visibility = \"GROUP\");";
VereinfachtesResultSet rs = connection.issueSelectStatement(request, project); VereinfachtesResultSet rs = connection.issueSelectStatement(request, project, student);
while (rs.next()) { while (rs.next()) {
journals.add(getJournalFromResultSet(rs)); journals.add(getJournalFromResultSet(rs));
...@@ -145,7 +145,7 @@ public class JournalDAOImpl implements JournalDAO { ...@@ -145,7 +145,7 @@ public class JournalDAOImpl implements JournalDAO {
@Override @Override
public ArrayList<Journal> getAllByProjectAndFilter(String project, String student, JournalFilter filter) { public ArrayList<Journal> getAllByProjectAndFilter(String project, String student, JournalFilter filter) {
if (filter == JournalFilter.ALL) { if (filter == JournalFilter.ALL) {
return getAllByProject(project); return getAllByProject(project, student);
} else { } else {
return getAllByStudent(student); return getAllByStudent(student);
} }
...@@ -176,7 +176,7 @@ public class JournalDAOImpl implements JournalDAO { ...@@ -176,7 +176,7 @@ public class JournalDAOImpl implements JournalDAO {
// build and execute request // build and execute request
String request = "SELECT * FROM journals WHERE project = ? AND open = ?;"; String request = "SELECT * FROM journals WHERE project = ? AND open = ?;";
VereinfachtesResultSet rs = connection.issueSelectStatement(request, project, true); VereinfachtesResultSet rs = connection.issueSelectStatement(request, project.getId(), true);
while (rs.next()) { while (rs.next()) {
userIds.add(getJournalFromResultSet(rs).getStudentIdentifier().getStudentId()); userIds.add(getJournalFromResultSet(rs).getStudentIdentifier().getStudentId());
......
...@@ -63,7 +63,7 @@ public class JournalServiceImpl implements JournalService { ...@@ -63,7 +63,7 @@ public class JournalServiceImpl implements JournalService {
public ArrayList<Journal> getAllJournals(String student, String project) { public ArrayList<Journal> getAllJournals(String student, String project) {
log.debug(">> get all journals(" + student + "," + project + ")"); log.debug(">> get all journals(" + student + "," + project + ")");
return journalDAO.getAllByProject(project); return journalDAO.getAllByProject(project, student);
} }
@Override @Override
......
...@@ -70,7 +70,7 @@ public class JournalUtils { ...@@ -70,7 +70,7 @@ public class JournalUtils {
//TODO Formatstring //TODO Formatstring
String request = "SELECT COUNT(*) > 0 AS `exists` FROM " + table+ " WHERE id = ?;"; String request = "SELECT COUNT(*) > 0 AS `exists` FROM " + table+ " WHERE id = ?;";
VereinfachtesResultSet rs = connection.issueSelectStatement(request,id); VereinfachtesResultSet rs = connection.issueSelectStatement(request,id);
log.debug("querry: " + rs.toString()); JournalUtils.log.debug("querry: " + rs.toString());
if (rs.next()) { if (rs.next()) {
// save the response // save the response
int count = rs.getInt("exists"); int count = rs.getInt("exists");
......
package unipotsdam.gf.modules.journal.model.dao;
import org.junit.Test;
import unipotsdam.gf.core.database.mysql.MysqlConnect;
import unipotsdam.gf.core.database.mysql.VereinfachtesResultSet;
import unipotsdam.gf.core.management.project.Project;
import unipotsdam.gf.modules.assessment.controller.model.StudentIdentifier;
import unipotsdam.gf.modules.journal.model.Journal;
import unipotsdam.gf.modules.journal.model.JournalFilter;
import unipotsdam.gf.modules.journal.model.Visibility;
import unipotsdam.gf.modules.journal.util.JournalUtils;
import unipotsdam.gf.modules.peer2peerfeedback.Category;
import java.util.ArrayList;
import static org.junit.Assert.*;
public class JournalDAOImplTest {
private final JournalDAO journalDAO = new JournalDAOImpl();
private final MysqlConnect connection = new MysqlConnect();
private final String testId = "-1";
private final String testStudent = "testStudent";
private final String testProject = "testProject";
private final String testEntry = "This is a Test";
private final Visibility testVisibility = Visibility.ALL;
private final Category testCategory = Category.TITEL;
private final Journal testJournal = new Journal(testId, new StudentIdentifier(testProject, testStudent), testEntry, testVisibility, testCategory);
@Test
public void createJournal() {
// Create Journal
Journal createJournal = testJournal;
journalDAO.createJournal(createJournal);
connection.connect();
// Get that Journal
ArrayList<Journal> resultJournals = getJournals();
//Only one Journal should exist
assertEquals(1, resultJournals.size());
Journal resultJournal = resultJournals.get(0);
//check if data correct
assertEquals(resultJournal.getStudentIdentifier().getStudentId(), testStudent);
assertEquals(resultJournal.getStudentIdentifier().getProjectId(), testProject);
assertEquals(resultJournal.getEntryMD(), testEntry);
//Journal should get real id while create
assertFalse(resultJournal.getId().equals(testId));
assertEquals(resultJournal.getVisibility(), testVisibility);
assertEquals(resultJournal.getCategory(), testCategory);
//cleanup
cleanup(resultJournal.getId());
connection.close();
}
@Test
public void updateJournal() {
Journal updateJournal = testJournal;
//createJournal
connection.connect();
// build and execute request
create(updateJournal);
//change Data
Visibility newVisibility = Visibility.MINE;
Category newCategory = Category.DURCHFUEHRUNG;
String newEntry = "Still a test";
updateJournal.setVisibility(newVisibility);
updateJournal.setCategory(newCategory);
updateJournal.setEntry(newEntry);
//update that Journal
journalDAO.updateJournal(updateJournal);
//check if update successful
ArrayList<Journal> resultJournals = getJournals();
//Only one Journal should exist
assertEquals(1, resultJournals.size());
Journal resultJournal = resultJournals.get(0);
//check if data correct
assertEquals(resultJournal.getStudentIdentifier().getStudentId(), testStudent);
assertEquals(resultJournal.getStudentIdentifier().getProjectId(), testProject);
assertEquals(resultJournal.getEntryMD(), newEntry);
assertEquals(resultJournal.getId(), testId);
assertEquals(resultJournal.getVisibility(), newVisibility);
assertEquals(resultJournal.getCategory(), newCategory);
//cleanup
cleanup(updateJournal.getId());
connection.close();
}
@Test
public void deleteJournal() {
Journal deleteJournal = testJournal;
//createJournal
connection.connect();
// build and execute request
create(deleteJournal);
//check if Journal was added
ArrayList<Journal> resultJournals = getJournals();
assertEquals(1, resultJournals.size());
Journal resultJournal = resultJournals.get(0);
//delete Journal
journalDAO.deleteJournal(resultJournal.getId());
//check if deleted
resultJournals = getJournals();
assertEquals(0, resultJournals.size());
connection.close();
}
@Test
public void getJournal() {
//create Journal
Journal getJournal = testJournal;
//createJournal
connection.connect();
// build and execute request
create(getJournal);
//get that Journal
Journal resultJournal = journalDAO.getJournal(testId);
//check data
assertEquals(resultJournal.getStudentIdentifier().getStudentId(), testStudent);
assertEquals(resultJournal.getStudentIdentifier().getProjectId(), testProject);
assertEquals(resultJournal.getEntryMD(), testEntry);
assertEquals(resultJournal.getId(), testId);
assertEquals(resultJournal.getVisibility(), testVisibility);
assertEquals(resultJournal.getCategory(), testCategory);
//cleanup
cleanup(resultJournal.getId());
connection.close();
}
@Test
public void getAllByProject() {
connection.connect();
//add Some Journals
Journal j1 = testJournal;
create(j1);
j1.setId("-2");
create(j1);
j1.setId("-3");
j1.getStudentIdentifier().setProjectId("otherProject");
create(j1);
//get for project
ArrayList<Journal> resultJournals = journalDAO.getAllByProject(testProject, testStudent);
//should be 2 Journals
assertEquals(2, resultJournals.size());
//should be j1 and j2
for (Journal j : resultJournals) {
assertTrue(j.getId().equals("-1") || j.getId().equals("-2"));
}
//cleanup
cleanup("-1", "-2", "-3");
}
@Test
public void getAllByProjectAndFilter() {
connection.connect();
//Create some journals
Journal j1 = testJournal;
create(j1);
j1.getStudentIdentifier().setStudentId("otherStudent");
j1.setId("-2");
create(j1);
j1.setId("-3");
j1.setVisibility(Visibility.MINE);
create(j1);
//all should return 2 Journals
assertEquals(2, journalDAO.getAllByProjectAndFilter(testProject, testStudent, JournalFilter.ALL).size());
//Own should return 1 Journal
assertEquals(1, journalDAO.getAllByProjectAndFilter(testProject, testStudent, JournalFilter.OWN).size());
//Cleanup
cleanup("-1", "-2", "-3");
connection.close();
}
@Test
public void closeJournal() {
connection.connect();
//create Journal
Journal closeJournal = testJournal;
create(closeJournal);
Journal resultJournal = getJournals().get(0);
//check if open
assertTrue(resultJournal.isOpen());
//close Journal
journalDAO.closeJournal(resultJournal.getId());
//check if closed
resultJournal = getJournals().get(0);
assertFalse(resultJournal.isOpen());
//cleanup
cleanup(resultJournal.getId());
}
@Test
public void getOpenJournals() {
connection.connect();
Project openProject = new Project();
openProject.setId(testProject);
//create some Journals
Journal j1 = testJournal;
create(j1);
j1.setOpen(false);
j1.setId("-2");
create(j1);
j1.setId("-3");
j1.getStudentIdentifier().setProjectId("otherProject");
create(j1);
//getOpenJournals
ArrayList<String> resultJournals = journalDAO.getOpenJournals(openProject);
//should be 1 Journal
assertEquals(1, resultJournals.size());
//should be journal -1
assertEquals(testStudent, resultJournals.get(0));
cleanup("-1", "-2", "-3");
connection.close();
}
//utility
private ArrayList<Journal> getJournals() {
String request = "SELECT * FROM journals WHERE project= ?;";
VereinfachtesResultSet rs = connection.issueSelectStatement(request, testProject);
ArrayList<Journal> resultJournals = new ArrayList<>();
while (rs.next()) {
resultJournals.add(getJournalFromResultSet(rs));
}
return resultJournals;
}
private void create(Journal getJournal) {
String createRequest = "INSERT INTO journals (`id`, `author`, `project`, `text`, `visibility`,`category`, `open` ) VALUES (?,?,?,?,?,?,?);";
connection.issueInsertOrDeleteStatement(createRequest, getJournal.getId(), getJournal.getStudentIdentifier().getStudentId(),
getJournal.getStudentIdentifier().getProjectId(), getJournal.getEntryMD(), getJournal.getVisibility(), getJournal.getCategory(), getJournal.isOpen());
}
private void cleanup(String... ids) {
for (String id : ids) {
String deleteRequest = "DELETE FROM journals WHERE id = ?;";
connection.issueInsertOrDeleteStatement(deleteRequest, id);
}
}
private Journal getJournalFromResultSet(VereinfachtesResultSet rs) {
String id = rs.getString("id");
long timestamp = rs.getTimestamp(2).getTime();
String student = rs.getString("author");
String project = rs.getString("project");
String text = rs.getString("text");
String visibility = rs.getString("visibility");
String category = rs.getString("category");
boolean open = rs.getBoolean("open");
return new Journal(id, new StudentIdentifier(project, student), text, timestamp, JournalUtils.stringToVisibility(visibility), JournalUtils.stringToCategory(category), open);
}
}
\ No newline at end of file
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