[10] | 1 | package geniusweb.protocol.tournament.allpermutations;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 | import static org.junit.Assert.assertNotNull;
|
---|
| 5 | import static org.junit.Assert.assertNull;
|
---|
| 6 | import static org.mockito.Matchers.any;
|
---|
| 7 | import static org.mockito.Matchers.anyLong;
|
---|
| 8 | import static org.mockito.Mockito.mock;
|
---|
| 9 | import static org.mockito.Mockito.times;
|
---|
| 10 | import static org.mockito.Mockito.verify;
|
---|
| 11 | import static org.mockito.Mockito.when;
|
---|
| 12 |
|
---|
| 13 | import java.math.BigInteger;
|
---|
| 14 | import java.util.LinkedList;
|
---|
| 15 | import java.util.List;
|
---|
| 16 |
|
---|
| 17 | import org.junit.Before;
|
---|
| 18 | import org.junit.Test;
|
---|
| 19 | import org.mockito.ArgumentCaptor;
|
---|
| 20 |
|
---|
| 21 | import geniusweb.events.ProtocolEvent;
|
---|
| 22 | import geniusweb.issuevalue.Bid;
|
---|
| 23 | import geniusweb.protocol.CurrentNegoState;
|
---|
| 24 | import geniusweb.protocol.NegoState;
|
---|
| 25 | import geniusweb.protocol.partyconnection.ProtocolToPartyConnFactory;
|
---|
| 26 | import geniusweb.protocol.session.SessionProtocol;
|
---|
[20] | 27 | import geniusweb.protocol.session.SessionResult;
|
---|
[10] | 28 | import geniusweb.protocol.session.SessionSettings;
|
---|
| 29 | import geniusweb.protocol.session.SessionState;
|
---|
| 30 | import geniusweb.references.PartyWithProfile;
|
---|
| 31 | import geniusweb.references.ProtocolRef;
|
---|
| 32 | import tudelft.utilities.listener.Listener;
|
---|
| 33 | import tudelft.utilities.logging.ReportToLogger;
|
---|
| 34 |
|
---|
| 35 | public class AllPermutationsProtocolTest {
|
---|
| 36 |
|
---|
| 37 | private final AllPermutationsState state = mock(AllPermutationsState.class),
|
---|
| 38 | newstate = mock(AllPermutationsState.class);
|
---|
| 39 | private final ReportToLogger log = new ReportToLogger("test");
|
---|
| 40 | private final AllPermutationsProtocol app = new AllPermutationsProtocol(
|
---|
| 41 | state, log);
|
---|
| 42 | private final ProtocolToPartyConnFactory factory = mock(
|
---|
| 43 | ProtocolToPartyConnFactory.class);
|
---|
| 44 | private final SessionSettings settings = mock(SessionSettings.class);
|
---|
| 45 | private final SessionState finalsessionstate = mock(SessionState.class);
|
---|
| 46 | private Bid agreement = mock(Bid.class);
|
---|
| 47 | private final long NOW = 1000;
|
---|
[20] | 48 | private SessionResult finalstate = mock(SessionResult.class);
|
---|
[10] | 49 |
|
---|
| 50 | @Before
|
---|
| 51 | public void before() {
|
---|
| 52 | when(state.getSize()).thenReturn(BigInteger.ZERO);
|
---|
| 53 | when(finalsessionstate.isFinal(anyLong())).thenReturn(true);
|
---|
| 54 | when(finalsessionstate.getSettings()).thenReturn(settings);
|
---|
| 55 | when(finalsessionstate.getAgreement()).thenReturn(agreement);
|
---|
[20] | 56 | when(finalsessionstate.getResult()).thenReturn(finalstate);
|
---|
| 57 | when(finalstate.getAgreement()).thenReturn(agreement);
|
---|
[10] | 58 | when(state.with(any())).thenReturn(newstate);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | @Test
|
---|
| 62 | public void smokeTest() {
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | @Test
|
---|
| 66 | public void startTestNoSessions() {
|
---|
| 67 | AllPermutationsProtocol app = new AllPermutationsProtocol(state, log);
|
---|
| 68 | app.start(factory);
|
---|
| 69 |
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | @Test
|
---|
| 73 | public void getDescrTest() {
|
---|
| 74 | assertNotNull(app.getDescription());
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | @Test
|
---|
| 78 | public void getStateTest() {
|
---|
| 79 | assertEquals(state, app.getState());
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | @Test
|
---|
| 83 | public void getRefTest() {
|
---|
| 84 | assertEquals("APP", app.getRef().getURI().toString());
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | @Test
|
---|
| 88 | public void runOneSessionTest() throws InterruptedException {
|
---|
| 89 | MockSessionProtocol sessionprotocol = startSession();
|
---|
| 90 | sessionprotocol.getListener();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | @Test
|
---|
| 94 | public void runOneSessionAndUnhandledEvent() throws InterruptedException {
|
---|
| 95 | MockSessionProtocol sessionprotocol = startSession();
|
---|
| 96 | sessionprotocol.getListener().notifyChange(mock(ProtocolEvent.class));
|
---|
| 97 | verify(state, times(0)).with(any());
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /**
|
---|
| 101 | * Check that a started session correctly handles a session event.
|
---|
| 102 | */
|
---|
| 103 | @Test
|
---|
| 104 | public void CurrentNegoStateEventTest() throws InterruptedException {
|
---|
| 105 | MockSessionProtocol sessionprotocol = startSession();
|
---|
| 106 |
|
---|
| 107 | CurrentNegoState evt = mock(CurrentNegoState.class);
|
---|
| 108 | when(evt.getState()).thenReturn(finalsessionstate);
|
---|
| 109 | sessionprotocol.getListener().notifyChange(evt);
|
---|
| 110 |
|
---|
| 111 | ArgumentCaptor<SessionResult> argument = ArgumentCaptor
|
---|
| 112 | .forClass(SessionResult.class);
|
---|
| 113 | verify(state, times(1)).with(argument.capture());
|
---|
| 114 | assertEquals(agreement, argument.getValue().getAgreement());
|
---|
| 115 | assertNull(argument.getValue().getError());
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | private MockSessionProtocol startSession() throws InterruptedException {
|
---|
| 119 | SessionSettings setting = mock(SessionSettings.class);
|
---|
| 120 | MockSessionProtocol sessionprotocol = new MockSessionProtocol();
|
---|
| 121 | when(setting.getProtocol(log)).thenReturn(sessionprotocol);
|
---|
| 122 | when(state.getNextSettings()).thenReturn(setting);
|
---|
| 123 |
|
---|
| 124 | app.start(factory);
|
---|
| 125 | Thread.sleep(500); // allow app to start session
|
---|
| 126 | // this call should return fine, and have listener registered to our
|
---|
| 127 | // protocol. app now waits for a notification
|
---|
| 128 | return sessionprotocol;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | *
|
---|
| 135 | * The protocol needs to record the listners, we need a listener to trigger
|
---|
| 136 | * further protocol actions. Mockito can't handle this, we mneed our own mock...
|
---|
| 137 | *
|
---|
| 138 | */
|
---|
| 139 | class MockSessionProtocol implements SessionProtocol {
|
---|
| 140 | private final List<Listener<ProtocolEvent>> listeners = new LinkedList<>();
|
---|
| 141 |
|
---|
| 142 | @Override
|
---|
| 143 | public void start(ProtocolToPartyConnFactory connectionfactory) {
|
---|
| 144 |
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | @Override
|
---|
| 148 | public String getDescription() {
|
---|
| 149 | return null;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | @Override
|
---|
| 153 | public NegoState getState() {
|
---|
| 154 | return null;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | @Override
|
---|
| 158 | public ProtocolRef getRef() {
|
---|
| 159 | return null;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | @Override
|
---|
| 163 | public void addParticipant(PartyWithProfile party) {
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | @Override
|
---|
| 167 | public void addListener(Listener<ProtocolEvent> l) {
|
---|
| 168 | listeners.add(l);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | @Override
|
---|
| 172 | public void removeListener(Listener<ProtocolEvent> l) {
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | public Listener<ProtocolEvent> getListener() {
|
---|
| 176 | if (listeners.size() != 1) {
|
---|
| 177 | throw new IllegalStateException("Not having exactly 1 listener");
|
---|
| 178 | }
|
---|
| 179 | return listeners.get(0);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | } |
---|