package geniusweb.progress; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.IOException; import java.util.Arrays; import java.util.Date; import java.util.LinkedList; import java.util.List; import org.junit.Before; import org.junit.Test; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import geniusweb.deadline.DeadlineRounds; import tudelft.utilities.junit.GeneralTests; public class ProgressRoundsTest extends GeneralTests { private DeadlineRounds deadline = mock(DeadlineRounds.class); private final static Integer TESTROUNDS = 10; private final static long TESTTIME = 10; // milliseconds! private final Date date = new Date(98765l); private final Date date0 = new Date(100000l); private final Progress deadline1 = new ProgressRounds(20, 0, date); private final Progress deadline2 = new ProgressRounds(20, 0, date); private final Progress deadline3 = new ProgressRounds(30, 0, date); private final Progress deadline4 = new ProgressTime(20l, date0); private final Progress deadline5 = new ProgressTime(20l, date); private ProgressRounds progress; private final ObjectMapper jackson = new ObjectMapper(); private final String progressstring = "{\"ProgressRounds\":{\"duration\":10,\"currentRound\":0,\"endtime\":98765}}"; private final long start = 1234; private final Date end = new Date(start + TESTTIME); private final ProgressRounds pr = new ProgressRounds(TESTROUNDS, 4, end); @Before public void before() { when(deadline.getRounds()).thenReturn(TESTROUNDS); progress = new ProgressRounds(deadline.getRounds(), 0, date); } @Override public List> getGeneralTestData() { List> list = new LinkedList<>(); list.add(Arrays.asList(deadline1, deadline2)); list.add(Arrays.asList(deadline3)); list.add(Arrays.asList(deadline4)); list.add(Arrays.asList(deadline5)); return list; } @Override public List getGeneralTestStrings() { return Arrays.asList("ProgressRounds\\[0.*20\\]", "ProgressRounds\\[0.*30\\]", "ProgressTime\\[100000.*20ms\\]", "ProgressTime\\[98765.*20ms\\]"); } @Test public void testInit() { assertEquals((Double) 0d, progress.get(1l)); } @Test public void testCurrentRound() { assertEquals((Integer) 0, progress.getCurrentRound()); } @Test public void testAdvance() { ProgressRounds p = progress; int n = 0; while (n <= TESTROUNDS) { assertEquals((Double) ((double) n / TESTROUNDS), p.get(1l)); n++; p = p.advance(); } } @Test public void testAdvanceWhenDeadlineReached() { ProgressRounds p = new ProgressRounds(TESTROUNDS, 9, date); p = p.advance(); assertEquals((Double) 1d, p.get(1l)); } @Test public void testSerialize() throws JsonProcessingException { System.out.println(jackson.writeValueAsString(progress)); assertEquals(progressstring, jackson.writeValueAsString(progress)); } @Test public void testDeserialize() throws JsonParseException, JsonMappingException, IOException { assertEquals(progress, jackson.readValue(progressstring, Progress.class)); } @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void testIllegalArg1() { new ProgressRounds(10, 11, date); } @SuppressWarnings("unused") @Test(expected = IllegalArgumentException.class) public void testIllegalArg2() { new ProgressRounds(10, -1, date); } @Test public void pastDeadline() { assertFalse(pr.isPastDeadline(start)); assertTrue(pr.isPastDeadline(start + TESTTIME + 1)); assertFalse(pr.isPastDeadline(start + TESTTIME - 1)); assertTrue(new ProgressRounds(TESTROUNDS, TESTROUNDS, end) .isPastDeadline(start + TESTTIME + 1)); } @Test public void terminationTime() { assertEquals(start + TESTTIME, pr.getTerminationTime().getTime()); } @Test public void get() { // time does not matter for rounds-based deadline assertEquals((Double) 0.4d, pr.get(start)); assertEquals((Double) 0.4d, pr.get(start + TESTTIME + 1)); assertEquals((Double) 0.4d, pr.get(start + TESTTIME)); assertEquals((Double) 0.4d, pr.get(start + TESTTIME / 2)); assertEquals((Double) 0.4d, pr.get(start - 10)); assertEquals((Double) 0.4d, pr.get(start + TESTTIME + 10)); assertEquals((Double) 0.1d, new ProgressRounds(TESTROUNDS, 1, end).get(start)); assertEquals((Double) 1d, new ProgressRounds(TESTROUNDS, TESTROUNDS, end).get(start)); } @Test public void totalRounds() { assertEquals((Integer) 10, pr.getTotalRounds()); } }