package geniusweb.progress; import static org.junit.Assert.assertEquals; 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 geniusweb.progress.Progress; import geniusweb.progress.ProgressRounds; import geniusweb.progress.ProgressTime; import tudelft.utilities.junit.GeneralTests; public class ProgressRoundsTest extends GeneralTests { private DeadlineRounds deadline = mock(DeadlineRounds.class); private final Date date = new Date(999l); private final Date date0 = new Date(0l); 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 static Integer TESTROUNDS = 10; private final ObjectMapper jackson = new ObjectMapper(); private final String progressstring = "{\"rounds\":{\"duration\":10,\"currentRound\":0,\"endtime\":999}}"; @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\\[0.*20ms\\]", "ProgressTime\\[999.*20ms\\]"); } @Test public void testInit() { assertEquals((Double) 0d, progress.get(1l)); } @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)); } @Test(expected = IllegalArgumentException.class) public void testIllegalArg1() { new ProgressRounds(10, 11, date); } @Test(expected = IllegalArgumentException.class) public void testIllegalArg2() { new ProgressRounds(10, -1, date); } }