package geniusweb.partiesserver.websocket; import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.io.IOException; import javax.websocket.CloseReason; import javax.websocket.RemoteEndpoint.Basic; import javax.websocket.Session; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; import com.fasterxml.jackson.databind.ObjectMapper; import geniusweb.actions.Action; import geniusweb.actions.EndNegotiation; import geniusweb.actions.PartyId; import geniusweb.connection.ConnectionEnd; import geniusweb.inform.Inform; import geniusweb.inform.YourTurn; import geniusweb.partiesserver.repository.RunningPartiesRepo; import geniusweb.partiesserver.repository.RunningParty; import geniusweb.party.Party; import tudelft.utilities.logging.Reporter; /** * Note this is a junit test. No real sockets are ever used here. */ public class PartySocketTest { private PartySocket socket; private Session session = mock(Session.class); private PartyId party1id = new PartyId("party1"); private Basic basicremote = mock(Basic.class); private RunningParty runningparty1 = mock(RunningParty.class); private RunningPartiesRepo repo = mock(RunningPartiesRepo.class); private final static ObjectMapper jackson = new ObjectMapper(); @Before public void before() { this.socket = new PartySocket(repo, mock(Reporter.class)); when(runningparty1.getID()).thenReturn(party1id); when(session.getBasicRemote()).thenReturn(basicremote); } @Test public void smokeTest() { } @Test public void initTestNoSuchParty() throws IOException { ArgumentCaptor argument = ArgumentCaptor .forClass(CloseReason.class); socket.start(session, "test1"); verify(session, times(1)).close(argument.capture()); assertEquals("No such party: test1", argument.getValue().getReasonPhrase()); } @Test public void initTest() throws IOException { when(repo.get(any())).thenReturn(runningparty1); socket.start(session, party1id.getName()); verify(session, times(0)).close(any()); } private void initparty1(Party party1) throws IOException { when(repo.get(any())).thenReturn(runningparty1); when(runningparty1.getParty()).thenReturn(party1); socket.start(session, "test2"); @SuppressWarnings("unchecked") ConnectionEnd connection = mock(ConnectionEnd.class); /* * we must call runningparty directly on this one because * SessionConnection can not be serialized. possible as this runs * locally on the server. */ party1.connect(connection); } // class TestConnection extends DefaultListenable implements Connection { // @Override // public void send(Action data) throws IOException { // // TODO Auto-generated method stub // } // // } /** * more tests needed for onMessage etc. */ @Test public void connectionMessageTest() throws IOException { initparty1(mock(Party.class)); verify(session, times(0)).close(any()); } /** * Check that party is informed when we send it YourTurn. */ @Test public void checkInform() throws IOException { initparty1(mock(Party.class)); ArgumentCaptor argument = ArgumentCaptor.forClass(Inform.class); Inform info = new YourTurn(); socket.onMessage(jackson.writeValueAsString(info), true,session); // check that party.inform was actually called with YourTurn verify(runningparty1, times(1)).inform(argument.capture()); // somewhere else we should test that that invokes party.inform assertEquals(info, argument.getValue()); } /** * Check that action is forwarded properly as serialized json into the * session stream. * * @throws InterruptedException */ @Test public void actionTest() throws IOException, InterruptedException { initparty1(mock(Party.class)); EndNegotiation action = new EndNegotiation(party1id); socket.send(action); Thread.sleep(500); ArgumentCaptor argument = ArgumentCaptor.forClass(String.class); verify(basicremote, times(1)).sendText(argument.capture()); assertEquals("{\"EndNegotiation\":{\"actor\":\"party1\"}}", argument.getValue()); } }