package se.chalmers.dat255.sleepfighter.model;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.joda.time.DateTime;

import se.chalmers.dat255.sleepfighter.utils.message.Message;
import se.chalmers.dat255.sleepfighter.utils.message.MessageBus;

public class AlarmListTest extends TestCase {

	public void testGetEarliestInfo() {
		// Bootstrap.
		Alarm first = new Alarm(0, 2);
		first.setId( 1 );
		Alarm second = new Alarm(0, 3);
		second.setId( 2 );

		List<Alarm> list = new ArrayList<Alarm>();
		list.add(first);
		list.add( second );

		AlarmList manager = new AlarmList( list );

		long now = new DateTime(0,1,1,0,0).getMillis();

		// Test index correctness.
		AlarmTimestamp info = manager.getEarliestAlarm( now );
		assertTrue( info != AlarmTimestamp.INVALID );
		assertEquals( info.getAlarm(), first );

		second.setTime( 0, 1 );
		info = manager.getEarliestAlarm( now );
		assertTrue( info != AlarmTimestamp.INVALID );
		assertEquals( info.getAlarm(), second );

		// Test isReal() correctness.
		first.setActivated( false );
		second.setActivated( false );
		info = manager.getEarliestAlarm( now );
		assertTrue( info == AlarmTimestamp.INVALID );
	}

	public void testSetMessageBus(  ) {
		Alarm first = new Alarm(12, 2);
		Alarm second = new Alarm(12, 3);

		List<Alarm> list = new ArrayList<Alarm>();
		list.add(first);
		list.add( second );

		AlarmList manager = new AlarmList( list );

		MessageBus<Message> bus = new MessageBus<Message>();
		manager.setMessageBus(bus);
		
		assertEquals(bus, first.getMessageBus());
		assertEquals(bus, second.getMessageBus());
	}
	
	public void testFireEvent() {
		List<Alarm> list = new ArrayList<Alarm>();
		AlarmList manager = new AlarmList( list );
		MessageBus<Message> bus = new MessageBus<Message>();
		manager.setMessageBus(bus);
		
		Alarm first = new Alarm(12, 2);
		manager.add(first);
		assertEquals(bus, first.getMessageBus());
		
		Alarm second = new Alarm(12,3);
		manager.set(0, second);
		assertEquals(bus, second.getMessageBus());
	
	}
}
@@ -2050,9 +2050,692 @@
 );%0A%09%0A%09%7D%0A
-%7D
+%09%0A%09public void testGetById() %7B%0A%09%09AlarmList alarmList = new AlarmList();%0A%09%09%0A%09%09// Test with no elements in list%0A%09%09assertEquals(null, alarmList.getById(0));%0A%09%09%0A%09%09// Add one element to list%0A%09%09Alarm alarm1 = new Alarm();%0A%09%09alarm1.setId(5);%0A%09%09alarmList.add(alarm1);%0A%09%09%0A%09%09// Test with 1 element in list%0A%09%09assertEquals(alarm1, alarmList.getById(5));%0A%09%09%0A%09%09// Add a second element%0A%09%09Alarm alarm2 = new Alarm();%0A%09%09alarm2.setId(7);%0A%09%09alarmList.add(alarm2);%0A%09%09%0A%09%09// Look for both IDs when both in list%0A%09%09assertEquals(alarm1, alarmList.getById(5));%0A%09%09assertEquals(alarm2, alarmList.getById(7));%0A%09%09%0A%09%09// Test looking for ID that no alarm in list has%0A%09%09assertEquals(null, alarmList.getById(6));%0A%09%7D%0A%7D%0A

Test for getById() in AlarmList