ServerSearchManager { public Observable<ServerEntity> searchServer(NetworkAddressEntity networkAddressEntity) { this.networkAddressEntity = networkAddressEntity; return searchServer(); } ServerSearchManager(ServerSearchSettings serverSearchSettings,
      NetworkConnectionlessTransmitter networkConnectionlessTransmitter,
      BroadcastAddressProvider broadcastAddressProvider,
      ProtocolMessageComposer protocolMessageComposer, MessageJsonMapper messageJsonMapper,
      ServerProtocolMapper serverProtocolMapper, ServerMessageValidator serverValidator); void setSubscribers(int subscribers); Observable<ServerEntity> searchServer(NetworkAddressEntity networkAddressEntity); Observable<ServerEntity> searchServer();  }
@Test public void shouldFindServerGivenAValidNetworkResponse() throws Exception { final ServerProtocol mockServerProtocol = getMockServerProtocol(); final ServerEntity mockServerEntity = getMockServerEntity(); CommandMessage mockCommandMessage = mock(CommandMessage.class); when(mockProtocolMessageComposer.composeGetServerInfoCommand()).thenReturn(mockCommandMessage); when(mockMessageJsonMapper.transformMessage(mockCommandMessage)).thenReturn("whatever"); Message mockMessage = mock(Message.class); when(mockMessage.isSuccess()).thenReturn(true); when(mockMessage.isServer()).thenReturn(true); when(mockMessage.getServer()).thenReturn(mockServerProtocol); when(mockMessageJsonMapper.transformMessage(anyString())).thenReturn(mockMessage); when(mockServerMessageValidator.isValid(mockMessage)).thenReturn(true); when(mockServerMessageValidator.cast(mockMessage)).thenReturn(mockServerProtocol); when(mockServerProtocolMapper.transform(mockServerProtocol)).thenReturn(mockServerEntity); Observable<ServerEntity> serverEntityObservable = serverSearchManager.searchServer(); ServerEntity serverEntity = serverEntityObservable.blockingFirst(); assertThat(serverEntity, notNullValue()); assertThat(serverEntity.worldName(), is("Za warudo")); verify(mockNetworkConnectionlessTransmitter, times(1)).setBroadcast(true); verify(mockProtocolMessageComposer, atLeastOnce()).composeGetServerInfoCommand(); verify(mockMessageJsonMapper, atLeastOnce()).transformMessage(any(CommandMessage.class)); verify(mockNetworkConnectionlessTransmitter, atLeastOnce()).send(any(NetworkPacket.class)); verify(mockBroadcastAddressProvider, only()).getBroadcastAddresses(); verify(mockNetworkConnectionlessTransmitter, atLeastOnce()).setTimeout( mockServerSearchSettings.getTimeout()); verify(mockNetworkConnectionlessTransmitter, atLeastOnce()).receive(any(NetworkPacket.class)); verify(mockMessageJsonMapper).transformMessage(anyString()); }
@Test(expected = RuntimeException.class) public void shouldNotFindServerGivenAnUnsuccessfulResponse() throws Exception { final ServerProtocol mockServerProtocol = getMockServerProtocol(); final ServerEntity mockServerEntity = getMockServerEntity(); CommandMessage mockCommandMessage = mock(CommandMessage.class); when(mockProtocolMessageComposer.composeGetServerInfoCommand()).thenReturn(mockCommandMessage); when(mockMessageJsonMapper.transformMessage(mockCommandMessage)).thenReturn("whatever"); Message mockMessage = mock(Message.class); when(mockMessage.isSuccess()).thenReturn(false); when(mockMessage.isServer()).thenReturn(true); when(mockMessage.getServer()).thenReturn(mockServerProtocol); when(mockMessageJsonMapper.transformMessage(anyString())).thenReturn(mockMessage); when(mockServerMessageValidator.isValid(mockMessage)).thenReturn(true); when(mockServerMessageValidator.cast(mockMessage)).thenReturn(mockServerProtocol); when(mockServerProtocolMapper.transform(mockServerProtocol)).thenReturn(mockServerEntity); Observable<ServerEntity> serverEntityObservable = serverSearchManager.searchServer(); ServerEntity serverEntity = serverEntityObservable.blockingFirst(); }
@Test(expected = RuntimeException.class) public void shouldNotFindServerGivenANonServerResponse() throws Exception { final ServerProtocol mockServerProtocol = getMockServerProtocol(); final ServerEntity mockServerEntity = getMockServerEntity(); CommandMessage mockCommandMessage = mock(CommandMessage.class); when(mockProtocolMessageComposer.composeGetServerInfoCommand()).thenReturn(mockCommandMessage); when(mockMessageJsonMapper.transformMessage(mockCommandMessage)).thenReturn("whatever"); Message mockMessage = mock(Message.class); when(mockMessage.isSuccess()).thenReturn(true); when(mockMessage.isServer()).thenReturn(false); when(mockMessage.getServer()).thenReturn(mockServerProtocol); when(mockMessageJsonMapper.transformMessage(anyString())).thenReturn(mockMessage); when(mockServerMessageValidator.isValid(mockMessage)).thenReturn(true); when(mockServerMessageValidator.cast(mockMessage)).thenReturn(mockServerProtocol); when(mockServerProtocolMapper.transform(mockServerProtocol)).thenReturn(mockServerEntity); Observable<ServerEntity> serverEntityObservable = serverSearchManager.searchServer(); ServerEntity serverEntity = serverEntityObservable.blockingFirst(); }
@Test public void shouldFindServerForIpAddressGivenAValidNetworkResponse() throws Exception { final ServerProtocol mockServerProtocol = getMockServerProtocol(); final ServerEntity mockServerEntity = getMockServerEntity(); CommandMessage mockCommandMessage = mock(CommandMessage.class); when(mockProtocolMessageComposer.composeGetServerInfoCommand()).thenReturn(mockCommandMessage); when(mockMessageJsonMapper.transformMessage(mockCommandMessage)).thenReturn("whatever"); Message mockMessage = mock(Message.class); when(mockMessage.isSuccess()).thenReturn(true); when(mockMessage.isServer()).thenReturn(true); when(mockMessage.getServer()).thenReturn(mockServerProtocol); when(mockMessageJsonMapper.transformMessage(anyString())).thenReturn(mockMessage); when(mockServerMessageValidator.isValid(mockMessage)).thenReturn(true); when(mockServerMessageValidator.cast(mockMessage)).thenReturn(mockServerProtocol); when(mockServerProtocolMapper.transform(mockServerProtocol)).thenReturn(mockServerEntity); NetworkAddressEntity networkAddressEntity = NetworkAddressEntity.builder() .ip("127.0.0.1") .port(9999) .build(); Observable<ServerEntity> serverEntityObservable = serverSearchManager.searchServer(networkAddressEntity); ServerEntity serverEntity = serverEntityObservable.blockingFirst(); assertThat(serverEntity, notNullValue()); assertThat(serverEntity.worldName(), is("Za warudo")); verify(mockNetworkConnectionlessTransmitter, never()).setBroadcast(true); verify(mockProtocolMessageComposer, atLeastOnce()).composeGetServerInfoCommand(); verify(mockMessageJsonMapper, atLeastOnce()).transformMessage(any(CommandMessage.class)); verify(mockNetworkConnectionlessTransmitter, atLeastOnce()).send(any(NetworkPacket.class)); verify(mockBroadcastAddressProvider, never()).getBroadcastAddresses(); verify(mockNetworkConnectionlessTransmitter, atLeastOnce()).setTimeout( mockServerSearchSettings.getTimeout()); verify(mockNetworkConnectionlessTransmitter, atLeastOnce()).receive(any(NetworkPacket.class)); verify(mockMessageJsonMapper).transformMessage(anyString()); }