Implement a cancel command for VoteBot
[cacert-votebot.git] / src / test / java / org / cacert / votebot / shared / CAcertVoteMechanicsTest.java
index b41b6d9..6b46b25 100644 (file)
@@ -31,6 +31,10 @@ import java.util.ResourceBundle;
 
 import static org.cacert.votebot.shared.CAcertVoteMechanics.State.IDLE;
 import static org.cacert.votebot.shared.CAcertVoteMechanics.State.RUNNING;
+import static org.cacert.votebot.shared.CAcertVoteMechanics.State.STOPPING;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
 
 /**
  * @author Jan Dittberner
@@ -38,14 +42,17 @@ import static org.cacert.votebot.shared.CAcertVoteMechanics.State.RUNNING;
 public class CAcertVoteMechanicsTest {
     private CAcertVoteMechanics subject;
     private ResourceBundle messages;
+    private static final long TEST_TIMEOUT = 120;
+    private static final long TEST_WARN = 30;
 
     @Test
     public void testStateEnum() {
         State[] values = State.values();
-        Assert.assertEquals(2, values.length);
+        Assert.assertEquals(3, values.length);
         List<State> states = Arrays.asList(values);
         Assert.assertTrue(states.contains(RUNNING));
         Assert.assertTrue(states.contains(IDLE));
+        Assert.assertTrue(states.contains(STOPPING));
     }
 
     @Before
@@ -62,7 +69,7 @@ public class CAcertVoteMechanicsTest {
 
     @Test
     public void testCallVote() {
-        String response = subject.callVote("test vote");
+        String response = subject.callVote("test vote", TEST_WARN, TEST_TIMEOUT);
         Assert.assertEquals(messages.getString("vote_started"), response);
         Assert.assertEquals("test vote", subject.getTopic());
         Assert.assertEquals(RUNNING, subject.getState());
@@ -70,8 +77,8 @@ public class CAcertVoteMechanicsTest {
 
     @Test
     public void testRefuseParallelCallVote() {
-        subject.callVote("first");
-        String response = subject.callVote("second");
+        subject.callVote("first", 30, TEST_TIMEOUT);
+        String response = subject.callVote("second", TEST_WARN, TEST_TIMEOUT);
         Assert.assertEquals(messages.getString("vote_running"), response);
         Assert.assertEquals("first", subject.getTopic());
         Assert.assertEquals(RUNNING, subject.getState());
@@ -79,13 +86,13 @@ public class CAcertVoteMechanicsTest {
 
     @Test
     public void testFreshVoteResult() {
-        subject.callVote("fresh vote");
+        subject.callVote("fresh vote", TEST_WARN, TEST_TIMEOUT);
         Assert.assertEquals("{}", subject.getCurrentResult());
     }
 
     @Test
     public void testVote() {
-        subject.callVote("test");
+        subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
         String response = subject.evaluateVote("alice", "aye");
         Assert.assertEquals(
                 MessageFormat.format(messages.getString("count_vote"), "alice", "AYE"), response);
@@ -94,7 +101,7 @@ public class CAcertVoteMechanicsTest {
 
     @Test
     public void testProxyVote() {
-        subject.callVote("test");
+        subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
         String response = subject.evaluateVote("alice", "proxy bob aye");
         Assert.assertEquals(
                 MessageFormat.format(messages.getString("count_proxy_vote"), "alice", "bob", "AYE"),
@@ -104,7 +111,7 @@ public class CAcertVoteMechanicsTest {
 
     @Test
     public void testInvalidVote() {
-        subject.callVote("test");
+        subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
         String response = subject.evaluateVote("alice", "moo");
         Assert.assertEquals(
                 MessageFormat.format(messages.getString("vote_not_understood"), "alice"), response);
@@ -113,7 +120,7 @@ public class CAcertVoteMechanicsTest {
 
     @Test
     public void testChangeVote() {
-        subject.callVote("test");
+        subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
         String response = subject.evaluateVote("alice", "aye");
         Assert.assertEquals(
                 MessageFormat.format(messages.getString("count_vote"), "alice", "AYE"), response);
@@ -126,7 +133,7 @@ public class CAcertVoteMechanicsTest {
 
     @Test
     public void testNoChangeForInvalidVote() {
-        subject.callVote("test");
+        subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
         String response = subject.evaluateVote("alice", "aye");
         Assert.assertEquals(
                 MessageFormat.format(messages.getString("count_vote"), "alice", "AYE"), response);
@@ -139,7 +146,7 @@ public class CAcertVoteMechanicsTest {
 
     @Test
     public void testInvalidProxyVote() {
-        subject.callVote("test");
+        subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
         String response = subject.evaluateVote("alice", "proxy bob moo");
         Assert.assertEquals(
                 MessageFormat.format(messages.getString("vote_not_understood"), "alice"), response);
@@ -148,7 +155,7 @@ public class CAcertVoteMechanicsTest {
 
     @Test
     public void testInvalidProxyVoteTokenCount() {
-        subject.callVote("test");
+        subject.callVote("test", TEST_WARN, TEST_TIMEOUT);
         String response = subject.evaluateVote("alice", "proxy ");
         Assert.assertEquals(
                 MessageFormat.format(messages.getString("invalid_proxy_vote"), "alice"), response);
@@ -157,16 +164,41 @@ public class CAcertVoteMechanicsTest {
 
     @Test
     public void testCloseFreshVote() {
-        subject.callVote("fresh vote");
+        subject.callVote("fresh vote", TEST_WARN, TEST_TIMEOUT);
+        String stopResponse = subject.stopVote("timeout");
+        assertThat(stopResponse, equalTo(
+                MessageFormat.format(
+                        messages.getString("finishing_vote"), subject.getTopic(), "timeout")));
         String[] response = subject.closeVote();
-        Assert.assertArrayEquals(new String[]{"AYE: 0", "NAYE: 0", "ABSTAIN: 0"}, response);
-        Assert.assertEquals("", subject.getTopic());
-        Assert.assertEquals(IDLE, subject.getState());
+        assertThat(response, equalTo(new String[]{"AYE: 0", "NAYE: 0", "ABSTAIN: 0"}));
+        assertThat(subject.getTopic(), equalTo(""));
+        assertThat(subject.getState(), equalTo(IDLE));
+    }
+
+    @Test
+    public void testFailStopNoVote() {
+        try {
+            subject.stopVote("test");
+            fail("Expected IllegalStateException has not been thrown.");
+        } catch (IllegalStateException e) {
+            assertThat(e.getMessage(), equalTo(messages.getString("no_vote_running_private")));
+        }
+    }
+
+    @Test
+    public void testFailCloseRunningVote() {
+        subject.callVote("running vote", TEST_WARN, TEST_TIMEOUT);
+        try {
+            subject.closeVote();
+            fail("Expected IllegalStateException has not been thrown.");
+        } catch (IllegalStateException e) {
+            assertThat(e.getMessage(), equalTo(messages.getString("cannot_close_running_vote")));
+        }
     }
 
     @Test
     public void testCloseVote() {
-        subject.callVote("fresh vote");
+        subject.callVote("fresh vote", TEST_WARN, TEST_TIMEOUT);
         subject.evaluateVote("alice", "AyE");
         subject.evaluateVote("bob", "NaYe");
         subject.evaluateVote("claire", "yes");
@@ -174,6 +206,7 @@ public class CAcertVoteMechanicsTest {
         subject.evaluateVote("alice", "proxy mike no");
         subject.evaluateVote("debra", "ja");
         subject.evaluateVote("malory", "evil");
+        subject.stopVote("test");
         String[] response = subject.closeVote();
         Assert.assertArrayEquals(new String[]{"AYE: 3", "NAYE: 2", "ABSTAIN: 0"}, response);
         Assert.assertEquals("", subject.getTopic());