/*
* CommunicationControllerTest.java
* JUnit based test
*
* Created on 12 July 2007, 18:09
*/
package org.spw.controller;
import java.util.ArrayList;
import java.util.Date;
import junit.framework.*;
import java.util.List;
import org.spw.model.Communication;
import org.spw.model.Contact;
import org.spw.model.Person;
/**
*
* @author PSe
*/
public class CommunicationControllerTest extends TestCase {
private static final long TEST_ID_COMPLETED = 9999991L;
private static final long TEST_ID_NO_ACTION = 9999992L;
private static final long TEST_ID_NOT_COMPLETED = 9999993L;
private static final long TEST_ID_CONTACT1 = 9999980L;
private static final CommunicationController comCtrl = new CommunicationController();
private static final PersonController pCtrl = new PersonController();
public CommunicationControllerTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
//Action completed
Communication com1 = new Communication();
com1.setIdCommunication(TEST_ID_COMPLETED);
com1.setActionToBeConducted("Test already done");
com1.setDateOfCompletion(new Date());
com1.setTopic(getName());
comCtrl.create(com1);
//No action to complete
Communication com2 = new Communication();
com2.setIdCommunication(TEST_ID_NO_ACTION);
com2.setTopic(getName());
comCtrl.create(com2);
//Action to complete
Communication com3 = new Communication();
com3.setIdCommunication(TEST_ID_NOT_COMPLETED);
com3.setActionToBeConducted("Test to be done");
com3.setTopic(getName());
comCtrl.create(com3);
}
protected void tearDown() throws Exception {
Communication com;
comCtrl.delete(comCtrl.read(TEST_ID_COMPLETED));
comCtrl.delete(comCtrl.read(TEST_ID_NO_ACTION));
comCtrl.delete(comCtrl.read(TEST_ID_NOT_COMPLETED));
Person person;
for(int i=0; i < 10; i++) {
person = pCtrl.read(TEST_ID_CONTACT1 + i);
if (person != null)
pCtrl.delete(person);
}
}
/**
* Test of getActionsNotCompleted method, of class org.spw.controller.CommunicationController.
*/
public void testGetActionsNotCompleted() {
System.out.println("getActionsNotCompleted");
List<Communication> result = comCtrl.getActionsNotCompleted();
assertFalse("The action not completed must not be empty",
result.isEmpty());
boolean find = false;
for (Communication com : result) {
if (com.getIdCommunication() == TEST_ID_COMPLETED) fail("An action completed " +
"should not apear in actionNotCompleted");
if (com.getIdCommunication() == TEST_ID_NO_ACTION) fail("A communication " +
"without action " +
"should not apear in actionNotCompleted");
if (com.getIdCommunication() == TEST_ID_NOT_COMPLETED) find = true;
}
assertTrue("Action to be completed not found", find);
}
/**
* Test of getCommunications method, of class org.spw.controller.CommunicationController.
*/
public void testGetCommunications() {
System.out.println("getCommunications");
boolean find_COMPLETED = false;
boolean find_NO_ACTION = false;
boolean find_NOT_COMPLETED = false;
List<Communication> result = comCtrl.getCommunications();
assertNotNull(result);
for(Communication com : result) {
if (com.getIdCommunication() == TEST_ID_COMPLETED) find_COMPLETED = true;
if (com.getIdCommunication() == TEST_ID_NOT_COMPLETED) find_NOT_COMPLETED = true;
if (com.getIdCommunication() == TEST_ID_NO_ACTION) find_NO_ACTION = true;
}
assertTrue("Action com to be completed not found", find_COMPLETED);
assertTrue("Action not completed com not found", find_NOT_COMPLETED);
assertTrue("No action com not found", find_NO_ACTION);
}
/**
* Test of create method, of class org.spw.controller.CommunicationController.
*/
public void testCreate() {
System.out.println("create");
//assume JPA is bug free
}
/**
* Test of read method, of class org.spw.controller.CommunicationController.
*/
public void testRead() {
System.out.println("read");
//assume JPA is bug free
}
/**
* Test of update method, of class org.spw.controller.CommunicationController.
*/
public void testUpdate() {
System.out.println("update");
//assume JPA is bug free
}
/**
* Test of delete method, of class org.spw.controller.CommunicationController.
*/
public void testDelete() {
System.out.println("delete");
//assume JPA is bug free
}
/**
* Test of updateParticipants method, of class org.spw.controller.CommunicationController.
*/
public void testUpdateParticipants() {
System.out.println("updateParticipants");
List<Contact> participants = new ArrayList<Contact>();
for(int i=0; i < 10; i++) {
Person person = new Person();
person.setIdContact(TEST_ID_CONTACT1 + i);
person.setFirstName(getName());
person.setLastName(getName() + " #" + i);
person = pCtrl.update(person);
participants.add(person);
}
Communication com = new Communication();
com.setIdCommunication(TEST_ID_COMPLETED);
com.setActionToBeConducted(getName());
com.setDateCommunication(new Date());
com.setDateOfCompletion(new Date());
com.setMedia("test");
com.setTopic(getName());
com = comCtrl.update(com);
com = comCtrl.updateParticipants(com, participants);
assertEquals(10, com.getParticipants().size());
Person person;
for(int i=0; i < 10; i++) {
person = pCtrl.read(TEST_ID_CONTACT1 + i);
assertTrue(person.getParticipate().contains(com));
}
//try to remove one participant and check if the relation is updated for both side
participants = new ArrayList(com.getParticipants());
person = pCtrl.read(TEST_ID_CONTACT1);
assertTrue(participants.remove(person));
com = comCtrl.updateParticipants(com, participants);
// check the removed participant doesn't contain the communication
person = pCtrl.read(person.getIdContact());
assertFalse(person.getParticipate().contains(com));
//check new list size
assertEquals(9, com.getParticipants().size());
}
}