import decorator.DecoratorChain;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Stack;
import model.Exam;
import model.Interview;
import model.Lecturer;
import model.Lva;
import model.Practice;
import model.PracticeGroup;
import model.PracticeSheet;
import model.Student;
import model.User;
import notification.Notificator;
import notification.event.EventBasedNotificator;
import notification.event.NotificationEvent;
import notification.event.NotificationEventObserver;
import service.AppointmentService;
import service.decorator.LvaAppointmentServiceDecorator;
import service.decorator.NotificationAppointmentServiceDecorator;
import service.impl.AppointmentServiceImpl;
import service.exception.AlreadyRegisteredException;
import service.exception.AppointmentNotFoundException;
import service.exception.MaximumMemberCountExceededException;
import service.exception.NotRegisteredException;
import service.exception.RegisterPeriodOverException;
import service.exception.RequirementsNotFullfilledException;
import service.exception.UnregisterPeriodOverException;
/**
* This is a stateful integration test the interactions between all parts of the
* system.
*
* @author Nicole Wagner, Christian Beikov
*/
public class AppointmentServiceTest extends AbstractTest {
private Notificator notificator = new EventBasedNotificator();
// In a real application use CDI to inject the implementation
@SuppressWarnings("unchecked")
private AppointmentService<Lva> lvaService = DecoratorChain.getChain(AppointmentService.class,
new AppointmentServiceImpl<Lva>(Lva.class),
new NotificationAppointmentServiceDecorator(notificator),
new LvaAppointmentServiceDecorator());
@SuppressWarnings("unchecked")
private AppointmentService<Interview> interviewService = DecoratorChain.getChain(AppointmentService.class,
new AppointmentServiceImpl<Interview>(Interview.class),
new NotificationAppointmentServiceDecorator(notificator));
@SuppressWarnings("unchecked")
private AppointmentService<Practice> practiceService = DecoratorChain.getChain(AppointmentService.class,
new AppointmentServiceImpl<Practice>(Practice.class),
new NotificationAppointmentServiceDecorator(notificator));
@SuppressWarnings("unchecked")
private AppointmentService<PracticeGroup> practiceGroupService = DecoratorChain.getChain(AppointmentService.class,
new AppointmentServiceImpl<PracticeGroup>(PracticeGroup.class),
new NotificationAppointmentServiceDecorator(notificator));
// For testing
private Date today = new Date();
private Date yesterday = new Date(new Date().getTime() - 1000 * 60 * 60 * 24);
private Date tomorrow = new Date(new Date().getTime() + 1000 * 60 * 60 * 24);
private Date endAppointment = new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 360);
private Lecturer lect1 = new Lecturer("Hugo", "Karl");
private Lecturer lect2 = new Lecturer("Franz", "Mauss");
private Lecturer lect3 = new Lecturer("Otto", "Reichl");
// Lva's for testing
private Lva unknownLva = new Lva(0, null, null, null, null, null, null, null, 0, null, null, null, 1, new ArrayList<Exam>(), new ArrayList<PracticeGroup>(), new ArrayList<Interview>(), new ArrayList<User>());
private Lva lva1 = new Lva(1, "OOP", today, endAppointment, yesterday, yesterday, tomorrow, lect1, 5, null, null, null, 1, new ArrayList<Exam>(), new ArrayList<PracticeGroup>(), new ArrayList<Interview>(), new ArrayList<User>());
private Lva lva2 = new Lva(2, "Mathematics 2", null, null, today, tomorrow, tomorrow, lect2, 5, null, null, null, 1, new ArrayList<Exam>(), new ArrayList<PracticeGroup>(), new ArrayList<Interview>(), new ArrayList<User>());
private Lva lva3 = new Lva(3, "Functional programming", null, null, today, tomorrow, yesterday, lect3, 5, null, null, null, 1, new ArrayList<Exam>(), new ArrayList<PracticeGroup>(), new ArrayList<Interview>(), new ArrayList<User>());
private Lva lva4 = new Lva(4, "Operating systems", null, null, today, tomorrow, tomorrow, lect1, 5, null, null, null, 1, new ArrayList<Exam>(), new ArrayList<PracticeGroup>(), new ArrayList<Interview>(), new ArrayList<User>());
private Lva lva5 = new Lva(5, "Introduction to Security", null, null, yesterday, yesterday, tomorrow, lect2, 5, null, null, null, 1, new ArrayList<Exam>(), new ArrayList<PracticeGroup>(), new ArrayList<Interview>(), new ArrayList<User>());
private Lva lva6 = new Lva(6, "International Negotiation", null, null, today, tomorrow, yesterday, lect3, 5, null, null, null, 1, new ArrayList<Exam>(), new ArrayList<PracticeGroup>(), new ArrayList<Interview>(), new ArrayList<User>());
// Students for testing
private Student student1 = new Student(1, "John", "Doe");
private Student student2 = new Student(2, "Jane", "Does");
private Student student10 = new Student(10, "J.D.", "Dorian");
private Student student11 = new Student(11, "Elliot", "Reid");
private Student student12 = new Student(12, "Christopher", "Turk");
public void testSaveLva() throws Exception {
// Cant register to this lva
lvaService.saveAppointment(lva1, User.ADMIN);
// Can register to this lva
lvaService.saveAppointment(lva2, User.ADMIN);
// Cant unregister from this lva
lvaService.saveAppointment(lva3, User.ADMIN);
// Can register to this lva
lvaService.saveAppointment(lva4, User.ADMIN);
// Cant register to this lva
lvaService.saveAppointment(lva5, User.ADMIN);
// Cant unregister from this lva
lvaService.saveAppointment(lva6, User.ADMIN);
List<Lva> lvas = lvaService.getAppointments();
System.out.println("" + lvas.size() + " Lehrveranstaltungen gespeichert.");
}
public void testGetLvas() throws Exception {
List<Lva> lvas = lvaService.getAppointments();
assertEqual(lvas.size(), 6);
assertTrue(lvas.contains(lva2));
assertFalse(lvas.contains(unknownLva));
for (Lva l : lvas) {
System.out.println("" + l.toString());
}
}
public void testRegister() throws Exception {
lvaService.register(student1, lva2, student1);
System.out.println(student1.toString() + " wurde erfolgreich zur " + lva2.toString2() + " hinzugefuegt.");
lvaService.register(student2, lva2, student2);
System.out.println(student2.toString() + " wurde erfolgreich zur " + lva2.toString2() + " hinzugefuegt.");
lvaService.register(student1, lva3, student1);
System.out.println(student1.toString() + " wurde erfolgreich zur " + lva3.toString2() + " hinzugefuegt.");
lvaService.register(student2, lva3, student2);
System.out.println(student2.toString() + " wurde erfolgreich zur " + lva3.toString2() + " hinzugefuegt.");
// Anmeldedaten okay
lvaService.register(student10, lva3, student10);
System.out.println(student10.toString() + " wurde erfolgreich zur " + lva3.toString2() + " hinzugefuegt.");
lvaService.register(student10, lva4, student10);
System.out.println(student10.toString() + " wurde erfolgreich zur " + lva4.toString2() + " hinzugefuegt.");
lvaService.register(student11, lva4, student11);
System.out.println(student11.toString() + " wurde erfolgreich zur " + lva4.toString2() + " hinzugefuegt.");
lvaService.register(student12, lva4, student12);
System.out.println(student12.toString() + " wurde erfolgreich zur " + lva4.toString2() + " hinzugefuegt.");
lvaService.register(student12, lva6, student12);
System.out.println(student12.toString() + " wurde erfolgreich zur " + lva6.toString2() + " hinzugefuegt.");
try {
lvaService.register(student1, unknownLva, student1);
//System.out.println(student1.toString()+" wurde nicht erfolgreich zur "+lva0.toString2()+" hinzugefuegt.");
fail();
} catch (AppointmentNotFoundException ex) {
// This has to happen
}
try {
lvaService.register(student1, lva2, student1);
//System.out.println(student1.toString()+" wurde nicht erfolgreich zur "+lva2.toString2()+" hinzugefuegt.");
fail();
} catch (AlreadyRegisteredException ex) {
// This has to happen
}
try {
lvaService.register(student1, lva1, student1);
//System.out.println(student1.toString()+" wurde nicht erfolgreich zur "+lva1.toString2()+" hinzugefuegt.");
fail();
} catch (RegisterPeriodOverException ex) {
// This has to happen
}
try { // Abmeldezeitraum bereits abgelaufen
lvaService.register(student11, lva5, student11);
//System.out.println(student11.toString()+" wurde nicht erfolgreich zur "+lva5.toString2()+" hinzugefuegt.");
fail();
} catch (RegisterPeriodOverException ex) {
// This has to happen
}
try { // zweites Mal zur gleichen Lva registrieren
lvaService.register(student12, lva4, student12);
//System.out.println(student12.toString()+" wurde erfolgreich zur "+lva4.toString2()+" hinzugefuegt.");
fail();
} catch (AlreadyRegisteredException ex) {
// This has to happen
}
}
public void testUnregister() throws Exception {
lvaService.unregister(student1, lva2);
System.out.println(student1.toString() + " wurde erfolgreich von der " + lva2.toString2() + " abgemeldet.");
lvaService.unregister(student2, lva2);
System.out.println(student2.toString() + " wurde erfolgreich von der " + lva2.toString2() + " abgemeldet.");
// Abmelden von der Lva geht okay
lvaService.unregister(student10, lva4);
System.out.println(student10.toString() + " wurde erfolgreich von der " + lva4.toString2() + " abgemeldet.");
lvaService.unregister(student12, lva4);
System.out.println(student12.toString() + " wurde erfolgreich von der " + lva4.toString2() + " abgemeldet.");
try {
lvaService.unregister(new Student(1, "John", "Doe"), unknownLva);
fail();
} catch (AppointmentNotFoundException ex) {
// This has to happen
}
try {
lvaService.unregister(new Student(1, "John", "Doe"), lva1);
fail();
} catch (NotRegisteredException ex) {
// This has to happen
}
try {
lvaService.unregister(new Student(1, "John", "Doe"), lva3);
fail();
} catch (UnregisterPeriodOverException ex) {
// This has to happen
}
try { // aus der Lva kann man sich nicht mehr abmelden
lvaService.unregister(new Student(12, "Christopher", "Turk"), lva6);
fail();
} catch (UnregisterPeriodOverException ex) {
// This has to happen
}
}
public void testGetRegisteredStudentCount() throws Exception {
assertEqual(lvaService.getRegisteredUserCount(lva1, User.ADMIN), 0);
assertEqual(lvaService.getRegisteredUserCount(lva2, User.ADMIN), 0);
assertEqual(lvaService.getRegisteredUserCount(lva3, User.ADMIN), 3);
assertEqual(lvaService.getRegisteredUserCount(lva4, User.ADMIN), 1);
assertEqual(lvaService.getRegisteredUserCount(lva5, User.ADMIN), 0);
assertEqual(lvaService.getRegisteredUserCount(lva6, User.ADMIN), 1);
System.out.println("Angemeldete StudentInnen bei " + lva1.getLvaNr() + " " + lva1.getTitle() + ": " + lvaService.getRegisteredUserCount(lva1, User.ADMIN));
System.out.println("Angemeldete StudentInnen bei " + lva2.getLvaNr() + " " + lva2.getTitle() + ": " + lvaService.getRegisteredUserCount(lva2, User.ADMIN));
System.out.println("Angemeldete StudentInnen bei " + lva3.getLvaNr() + " " + lva3.getTitle() + ": " + lvaService.getRegisteredUserCount(lva3, User.ADMIN));
System.out.println("Angemeldete StudentInnen bei " + lva4.getLvaNr() + " " + lva4.getTitle() + ": " + lvaService.getRegisteredUserCount(lva4, User.ADMIN));
System.out.println("Angemeldete StudentInnen bei " + lva5.getLvaNr() + " " + lva5.getTitle() + ": " + lvaService.getRegisteredUserCount(lva5, User.ADMIN));
System.out.println("Angemeldete StudentInnen bei " + lva6.getLvaNr() + " " + lva6.getTitle() + ": " + lvaService.getRegisteredUserCount(lva6, User.ADMIN));
}
public void testGetRegisteredStudents() throws Exception {
List<User> students = lvaService.getRegisteredUsers(lva1, User.ADMIN);
assertNotNull(students);
assertEmpty(students);
if (!students.isEmpty()) {
System.out.println("Fuer die LVA: " + lva1.getLvaNr() + " " + lva1.getTitle() + " sind folgende StudentInnen registiert: \n");
for (User s : students) {
System.out.println("" + s.toString());
}
}
students = lvaService.getRegisteredUsers(lva2, User.ADMIN);
assertNotNull(students);
assertEmpty(students);
if (!students.isEmpty()) {
System.out.println("Fuer die LVA: " + lva2.getLvaNr() + " " + lva2.getTitle() + " sind folgende StudentInnen registiert: \n");
for (User s : students) {
System.out.println("" + s.toString());
}
}
students = lvaService.getRegisteredUsers(lva3, User.ADMIN);
assertNotNull(students);
assertEqual(students.size(), 3);
assertNotNull(students.get(0));
assertNotNull(students.get(1));
assertNotNull(students.get(2));
// Check if first student is John Doe
assertEqual(student1, students.get(0));
assertEqual("John", students.get(0).getFirstName());
assertEqual("Doe", students.get(0).getLastName());
// Check if second student is Jane Does
assertEqual(student2, students.get(1));
assertEqual("Jane", students.get(1).getFirstName());
assertEqual("Does", students.get(1).getLastName());
// Check if third student is J.D. Dorian
assertEqual(student10, students.get(2));
assertEqual("J.D.", students.get(2).getFirstName());
assertEqual("Dorian", students.get(2).getLastName());
if (!students.isEmpty()) {
System.out.println("Fuer die LVA: " + lva3.getLvaNr() + " " + lva3.getTitle() + " sind folgende StudentInnen registiert: \n");
for (User s : students) {
System.out.println("" + s.toString());
}
}
students = lvaService.getRegisteredUsers(lva4, User.ADMIN);
assertNotNull(students);
assertEqual(students.size(), 1);
assertNotNull(students.get(0));
// Check if student is Elliot Reid
assertEqual(student11, students.get(0));
assertEqual("Elliot", students.get(0).getFirstName());
assertEqual("Reid", students.get(0).getLastName());
if (!students.isEmpty()) {
System.out.println("Fuer die LVA: " + lva4.getLvaNr() + " " + lva4.getTitle() + " sind folgende StudentInnen registiert: \n");
for (User s : students) {
System.out.println("" + s.toString());
}
}
students = lvaService.getRegisteredUsers(lva5, User.ADMIN);
assertNotNull(students);
assertEmpty(students);
if (!students.isEmpty()) {
System.out.println("Fuer die LVA: " + lva5.getLvaNr() + " " + lva5.getTitle() + " sind folgende StudentInnen registiert: \n");
for (User s : students) {
System.out.println("" + s.toString());
}
}
students = lvaService.getRegisteredUsers(lva6, User.ADMIN);
assertNotNull(students);
assertEqual(students.size(), 1);
assertNotNull(students.get(0));
// Check if student is Christopher Turk
assertEqual(student12, students.get(0));
assertEqual("Christopher", students.get(0).getFirstName());
assertEqual("Turk", students.get(0).getLastName());
if (!students.isEmpty()) {
System.out.println("Fuer die LVA: " + lva6.getLvaNr() + " " + lva6.getTitle() + " sind folgende StudentInnen registiert: \n");
for (User s : students) {
System.out.println("" + s.toString());
}
}
}
public void testDeleteAndRestoreLva() throws Exception {
lvaService.removeAppointment(lva6, lect3);
assertTrue(!lvaService.getAppointments().contains(lva6)
&& lvaService.getAppointments(User.ADMIN).contains(lva6));
System.out.println("Successfully set the deleted flag for LVA: " + lva6);
lvaService.saveAppointment(lva6.restore(), lect3);
assertTrue(lvaService.getAppointments().contains(lva6));
System.out.println("Successfully set the restored LVA: " + lva6);
}
public void testGetMaximumMemberCount() throws Exception {
Lva l1 = new Lva(50, "Example", tomorrow, tomorrow, today, tomorrow, tomorrow, lect1, 1, "STEOP", "STEG", null, 1, new ArrayList<Exam>(), new ArrayList<PracticeGroup>(), new ArrayList<Interview>(), new ArrayList<User>());
lvaService.saveAppointment(l1, lect1);
lvaService.register(student1, l1, student1);
try{
lvaService.register(student2, l1, student2);
fail();
}catch(MaximumMemberCountExceededException ex){
// This has to happen
}
System.out.println("LVA: " + lva1 + " successfully tested for maximum member count!");
}
public void testCancelAndRescheduleAppointment() throws Exception {
final Stack<NotificationEvent> s = new Stack<NotificationEvent>();
NotificationEventObserver obs = new NotificationEventObserver() {
public void observe(NotificationEvent event) {
s.push(event);
}
};
notificator.addObserver(obs);
lvaService.cancelAppointment(lva1, lect1);
assertNotEmpty(s);
System.out.println("LVA: " + lva1 + " successfully canceled and notifications have been observed!");
s.clear();
lvaService.rescheduleAppointment(lva1, lect1);
assertNotEmpty(s);
System.out.println("LVA: " + lva1 + " successfully rescheduled and notifications have been observed!");
s.clear();
notificator.removeObserver(obs);
}
public void testCondition() throws Exception {
Lva l1 = new Lva(100, "STEG Condition", tomorrow, tomorrow, today, tomorrow, tomorrow, lect1, 1, "STEOP", "STEG", null, 1, new ArrayList<Exam>(), new ArrayList<PracticeGroup>(), new ArrayList<Interview>(), new ArrayList<User>());
Lva l2 = new Lva(101, "STEG Fullfillment", tomorrow, tomorrow, today, tomorrow, tomorrow, lect1, 1, "STEG", null, null, 1, new ArrayList<Exam>(), new ArrayList<PracticeGroup>(), new ArrayList<Interview>(), new ArrayList<User>());
lvaService.saveAppointment(l1, lect1);
lvaService.saveAppointment(l2, lect1);
try{
lvaService.register(student1, l1, student1);
fail();
}catch(RequirementsNotFullfilledException ex){
// This has to happen
}
try{
lvaService.register(student1, l1, User.ADMIN);
}catch(RequirementsNotFullfilledException ex){
fail();
}
System.out.println("Successfully tested STEG condition of LVA: " + l1);
}
public void testOtherRegistrations() throws Exception{
Interview interview = new Interview(2000, today, tomorrow, yesterday, tomorrow, tomorrow, new Lecturer("Peter", "Zaruba"), 30, new ArrayList<User>());
Practice practice = new Practice(today, tomorrow, yesterday, tomorrow, tomorrow, 2);
PracticeGroup practiceGroup = new PracticeGroup(today, tomorrow, yesterday, tomorrow, tomorrow, 2, new ArrayList<Practice>(), new ArrayList<PracticeSheet>(), new ArrayList<User>());
interviewService.saveAppointment(interview, lect1);
interviewService.register(student1, interview, student1);
System.out.println("Successfully tested saving and registering to interview: " + interview);
practiceService.saveAppointment(practice, lect1);
practiceService.register(student1, practice, student1);
System.out.println("Successfully tested saving and registering to practice: " + practice);
practiceGroupService.saveAppointment(practiceGroup, lect1);
practiceGroupService.register(student1, practiceGroup, student1);
System.out.println("Successfully tested saving and registering to practice group: " + practiceGroup);
}
}