package geniusweb.partiesserver.repository; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import java.util.Arrays; import java.util.Date; import java.util.List; import org.junit.Test; import org.mockito.ArgumentCaptor; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import geniusweb.actions.PartyId; import geniusweb.inform.Inform; import geniusweb.partiesserver.websocket.PartySocket; import geniusweb.party.Capabilities; import geniusweb.party.DefaultParty; import geniusweb.party.Party; import tudelft.utilities.junit.GeneralTests; public class RunningPartyTest extends GeneralTests { private static final int DEFAULT_RUNTIME = 10000; private static final String NAME1 = "name1"; private final Party party1 = mock(Party.class); private final Party party2 = mock(Party.class); private final PartyId id1 = new PartyId("party1"); private final PartyId id2 = new PartyId("party2"); private final Date now = new Date(); private final Date end = new Date(now.getTime() + DEFAULT_RUNTIME); private final RunningParty running1 = new RunningParty(party1, id1, NAME1, now, end, null), running1a = new RunningParty(party1, id1, NAME1, now, end, null), running2 = new RunningParty(party2, id2, "name2", now, end, null), running3 = new RunningParty(party2, id2, "name1", now, end, null); private final String asString = "{\"id\":\"party1\",\"startDate\":" + now.getTime() + ",\"endDate\":" + end.getTime() + ",\"name\":\"name1\"}"; @Override public List> getGeneralTestData() { return Arrays.asList(Arrays.asList(running1, running1a), Arrays.asList(running2), Arrays.asList(running3)); } @Override public List getGeneralTestStrings() { return Arrays.asList( "RunningParty.*party.*class geniusweb.party.Party.*name1.*", "RunningParty.*party.*class geniusweb.party.Party.*name2.*", "RunningParty.*party.*class geniusweb.party.Party.*name1.*"); } @Test public void constructorTest() { new RunningParty(party1, id1, "name1", now, end, null); } @Test public void constructor2Test() throws ClassNotFoundException, InstantiationFailedException { AvailableParty availableParty = new AvailableJavaParty("simple party", SimpleParty.class); // mocking AvailableParty gives troubles with implemening // getPartyClass... RunningParty.create(availableParty, DEFAULT_RUNTIME); } @Test public void testSerialize() throws JsonProcessingException { ObjectMapper jackson = new ObjectMapper(); String json = jackson.writeValueAsString(running1).trim(); System.out.println(json); assertEquals(asString, json); } // no deserializer test for RunningParty because it can't be deserialized. @Test public void testConnect() { RunningParty rparty = new RunningParty(party1, id1, "name1", now, end, null); PartySocket connection = mock(PartySocket.class); rparty = rparty.withConnection(connection); } @Test(expected = IllegalStateException.class) public void testConnectTwice() { RunningParty rparty = new RunningParty(party1, id1, "name1", now, end, null); PartySocket connection = mock(PartySocket.class); rparty = rparty.withConnection(connection); rparty = rparty.withConnection(connection); } /** * Check that inform messages are forwarded to actual party */ @Test public void testInform() { RunningParty rparty = new RunningParty(party1, id1, "name1", now, end, null); PartySocket connection = mock(PartySocket.class); rparty = rparty.withConnection(connection); Inform info = mock(Inform.class); rparty.inform(info); ArgumentCaptor argument = ArgumentCaptor.forClass(Inform.class); verify(connection, times(1)).notifyListeners(argument.capture()); assertEquals(info, argument.getValue()); } @Test public void testSquash() { assertEquals("12__3", running1.squash("12&$3")); } @Test public void testSquash1() { assertEquals("p12__3", running1.squash1("12&$3")); assertEquals("party_32_12", running1.squash1("party+32=12")); assertEquals("party_32_12", running1.squash1("party 32 12")); } @Test public void testWeirdNames() { new RunningParty(party1, id1, "bla#@12- @1/3", now, end, null); new RunningParty(party1, id1, "12-bla#@12- @1/3", now, end, null); } } class SimpleParty extends DefaultParty { @Override public Capabilities getCapabilities() { return null; } @Override public String getDescription() { return null; } @Override public void notifyChange(Inform data) { } }