示例#1
0
  @Test
  public void shouldForwardMergeRequestsToLibraryBackend() throws Exception {
    when(syncTemplate.postForObject(
            anyString(), eq(JsonConverter.toJson(request)), eq(String.class)))
        .thenReturn(JsonConverter.toJson(expectedResponse));

    messEngine.send(mergeRequest);

    @SuppressWarnings("unchecked")
    Message<SyncValueObject> response =
        (Message<SyncValueObject>)
            messEngine.getMessage(MessEngineConstants.SYNC_LIBRARY_MERGE_RESPONSE);
    assertNotNull(response);
    assertEquals(expectedResponse, response.getBody());
  }
示例#2
0
  @Test
  public void shouldPutAlert() throws Exception {
    @SuppressWarnings("deprecation")
    MusicContentAlert alert =
        new MusicContentAlert(
            new ContactInfo(), new ContactInfo(), new Date(), new ModelCollection(), "message");
    messEngine.send(new AllMessage<Alert>(MessEngineConstants.PUT_ALERT_TYPE, alert));

    verify(defaultTemplate).put(anyString(), eq(JsonConverter.toJson(alert)), eq(alert.getId()));
  }
示例#3
0
 @Test
 public void shouldUpdateContactProfile() throws Exception {
   @SuppressWarnings("deprecation")
   ContactInfo contactInfo = new ContactInfo();
   contactInfo.setEmail("*****@*****.**");
   contactInfo.setId(1L);
   Message<?> message =
       new AllMessage<ContactInfo>(
           MessEngineConstants.UPDATE_CONTACT_PROFILE_REQUEST, contactInfo);
   when(defaultTemplate.postForObject(
           anyString(), eq(JsonConverter.toJson(contactInfo)), eq(String.class)))
       .thenReturn(JsonConverter.toJson(contactInfo));
   messEngine.send(message);
   verify(defaultTemplate)
       .postForObject(anyString(), eq(JsonConverter.toJson(contactInfo)), eq(String.class));
   Message<?> response =
       messEngine.getMessage(MessEngineConstants.UPDATE_CONTACT_PROFILE_RESPONSE);
   assertNotNull(response);
   assertEquals(contactInfo, response.getBody());
 }
示例#4
0
  @Test
  public void shouldForwardCommitRequestToLibraryBackendAndReturnResponseWithoutEvents()
      throws Exception {
    when(syncTemplate.postForObject(
            anyString(), eq(JsonConverter.toJson(request)), eq(String.class)))
        .thenReturn(JsonConverter.toJson(expectedResponse));
    assertNotNull(request.getEvents());
    assertNotNull(expectedResponse.getEvents());

    messEngine.send(commitRequest);

    @SuppressWarnings("unchecked")
    Message<SyncValueObject> response =
        (Message<SyncValueObject>)
            messEngine.getMessage(MessEngineConstants.SYNC_SEND_DELTA_RESPONSE);
    assertNotNull(response);
    SyncValueObject actualResponse = response.getBody();
    assertEquals(expectedResponse, actualResponse);
    assertNull(actualResponse.getEvents());
  }
示例#5
0
  @Test
  public void shouldReturnNullResponseIfLibraryBackendFails() throws Exception {
    when(syncTemplate.postForObject(
            anyString(), eq(JsonConverter.toJson(request)), eq(String.class)))
        .thenThrow(new RuntimeException("Some server error"));

    messEngine.send(mergeRequest);

    @SuppressWarnings("unchecked")
    Message<SyncValueObject> response =
        (Message<SyncValueObject>)
            messEngine.getMessage(MessEngineConstants.SYNC_LIBRARY_MERGE_RESPONSE);
    assertNotNull(response);
    assertNull(response.getBody());
  }
示例#6
0
  @Test
  public void shouldGetLocalFeeds() throws Exception {
    when(defaultTemplate.getForObject(anyString(), eq(String.class), eq(feedUserIdRequest)))
        .thenReturn(JsonConverter.toJson(expectedFeedResponse));

    messEngine.send(localFeedsRequest);
    @SuppressWarnings("unchecked")
    Message<FeedsResponse> response =
        (Message<FeedsResponse>) messEngine.getMessage(MessEngineConstants.FEEDS_LOCAL_RESPONSE);

    assertNotNull(response);
    assertEquals(expectedFeedResponse.getFeeds(), response.getBody().getFeeds());
    assertEquals(expectedFeedResponse.getOwnerId(), response.getBody().getOwnerId());
    assertEquals(
        expectedFeedResponse.getCurrentServerTime(), response.getBody().getCurrentServerTime());
  }
示例#7
0
  @Test
  public void shouldReturnSameRequestWithoutEventsIfCannotCommitDeltaToLibraryBackend()
      throws Exception {
    when(syncTemplate.postForObject(
            anyString(), eq(JsonConverter.toJson(request)), eq(String.class)))
        .thenThrow(new RuntimeException("Some server error"));
    assertNotNull(request.getEvents());

    messEngine.send(commitRequest);

    @SuppressWarnings("unchecked")
    Message<SyncValueObject> response =
        (Message<SyncValueObject>)
            messEngine.getMessage(MessEngineConstants.SYNC_SEND_DELTA_RESPONSE);
    assertNotNull(response);
    assertEquals(request, response.getBody());
    assertNull(request.getEvents());
  }
示例#8
0
  @Test
  public void shouldGetAlerts() throws Exception {
    @SuppressWarnings("deprecation")
    MusicContentAlert alert =
        new MusicContentAlert(
            new ContactInfo(), new ContactInfo(), new Date(), new ModelCollection(), "message");
    List<Alert> expectedList = new ArrayList<Alert>();
    expectedList.add(alert);
    String userId = "userID";
    when(defaultTemplate.getForObject(anyString(), eq(String.class), eq(userId)))
        .thenReturn(JsonConverter.toJson(expectedList));
    messEngine.send(new AllMessage<String>(MessEngineConstants.ALERTS_REQUEST_TYPE, userId));

    verify(defaultTemplate).getForObject(anyString(), eq(String.class), eq(userId));

    Message<?> responseMessage = messEngine.getMessage(MessEngineConstants.ALERTS_RESPONSE_TYPE);
    assertNotNull(responseMessage);
    assertEquals(expectedList, responseMessage.getBody());
  }
示例#9
0
  @Test
  public void shouldCreatePendingEmail() throws Exception {
    final PendingEmail pendingEmail = new PendingEmail(1L, "*****@*****.**");
    Message<?> message =
        new AllMessage<PendingEmail>(MessEngineConstants.SEND_EMAIL_TYPE, pendingEmail);
    when(defaultTemplate.postForObject(
            anyString(), eq(JsonConverter.toJson(message.getBody())), eq(String.class)))
        .thenAnswer(
            new Answer<String>() {
              @Override
              public String answer(InvocationOnMock invocation) throws Throwable {
                pendingEmail.setId(100L);
                return JsonConverter.toJson(pendingEmail);
              }
            });
    messEngine.send(message);

    assertNotNull(pendingEmail.getId());
    Message<?> response = messEngine.getMessage(MessEngineConstants.PUSH_PENDING_EMAIL_TYPE);
    assertNotNull(response);
    PendingEmail actual = (PendingEmail) response.getBody();
    assertEquals(pendingEmail.getId(), actual.getId());
  }
示例#10
0
 public FeedStat(AllFeed feed) {
   email = feed.getOwner().getEmail();
   type = feed.getType();
   json = JsonConverter.toJson(feed);
 }
@RunWith(MockInyectRunner.class)
public class TestLanNetworkingService {

  @UnderTest private LanNetworkingService networkingService;
  @Mock private IServer acceptor;
  @Mock private LanDownloaderMessageListener listener;
  @Mock private LanNodeFactory nodeFactory;
  @Mock private LanNode lanNode;
  @Mock private InetAddress remoteInetAddress;
  private String remoteAddress = "192.168.1.xxr";
  @Mock private InetAddress localInetAddress;
  private String localAddress = "192.168.1.xxl";
  @Mock private INonBlockingConnection connection;
  @Mock private LanDiscoveryService discoveryService;

  private LanDownloaderMessage message =
      new LanDownloaderMessage("127.0.0.1", TRACK_REQUEST, "downloadId");
  private String encodedMessage =
      new String(Base64.encode(JsonConverter.toJson(message).getBytes()));

  @Before
  public void init() throws Exception {
    networkingService.start();

    verify(discoveryService).addLanDiscoveryListener(networkingService);
    when(acceptor.isOpen()).thenReturn(true);
    networkingService.addMessageListener(listener);
    when(nodeFactory.newNode(anyString())).thenReturn(lanNode);
    when(localInetAddress.getHostAddress()).thenReturn(localAddress);
    when(lanNode.getConnection()).thenReturn(connection);
    when(connection.getLocalAddress()).thenReturn(localInetAddress);
    when(connection.getRemoteAddress()).thenReturn(remoteInetAddress);
  }

  @After
  public void tearDown() throws IOException {
    networkingService.removeMessageListener(listener);
    networkingService.stop();
    verify(acceptor).close();
  }

  @Test
  public void shouldAddNode() throws Exception {
    when(lanNode.isConnected()).thenReturn(false, true);

    networkingService.onDiscoveredAddress(remoteAddress);

    verify(nodeFactory).newNode(remoteAddress);
    verify(lanNode).connect();
  }

  @Test
  public void shouldNotConnectMoreThanOnceToSameNode() throws Exception {
    when(lanNode.isConnected()).thenReturn(false, true, true, true);
    networkingService.onDiscoveredAddress(remoteAddress);
    networkingService.onDiscoveredAddress(remoteAddress);
    networkingService.onDiscoveredAddress(remoteAddress);

    verify(nodeFactory).newNode(remoteAddress);
    verify(lanNode).connect();
    verify(lanNode, times(3)).isConnected();
  }

  // @Test
  // public void shouldNotRetryConnectionToBlacklistedNodes() throws Exception {
  // when(lanNode.isConnected()).thenReturn(false);
  // when(lanNode.isBlacklisted()).thenReturn(false, false, true);
  //
  // networkingService.onDiscoveredAddress(remoteAddress);
  // networkingService.onDiscoveredAddress(remoteAddress);
  // networkingService.onDiscoveredAddress(remoteAddress);
  // networkingService.onDiscoveredAddress(remoteAddress);
  //
  // verify(nodeFactory).newNode(remoteAddress);
  // verify(lanNode, times(2)).connect();
  // }

  @Test
  public void shouldSendMessageToAParticularNode() throws Exception {
    when(lanNode.isConnected()).thenReturn(true);

    networkingService.sendTo(message, remoteAddress);

    verify(connection).write(anyString());
  }

  // @Test
  // public void shouldNotSendMessageToABlacklistedNode() throws Exception {
  // when(lanNode.isConnected()).thenReturn(false);
  // networkingService.onDiscoveredAddress(remoteAddress);
  // verify(lanNode).connect();
  //
  // when(lanNode.isBlacklisted()).thenReturn(true);
  //
  // networkingService.sendTo(message, remoteAddress);
  //
  // verify(nodeFactory).newNode(remoteAddress);
  // verify(connection, never()).write(anyString());
  // }

  @Test
  public void shouldSendMessageToAllKnownNodes() throws Exception {
    networkingService.onDiscoveredAddress(remoteAddress);
    String otherAddress = "192.168.1.xxo";
    networkingService.onDiscoveredAddress(otherAddress);
    when(lanNode.isConnected()).thenReturn(true);

    networkingService.send(message);

    verify(connection, times(2)).write(anyString());
  }

  @Test
  public void shouldCallListenersAsynchronouslyOnMessageReceived() throws Exception {
    when(connection.readStringByDelimiter(anyString())).thenReturn(encodedMessage);

    networkingService.onData(connection);

    verify(listener, timeout(2000)).onMessage(any(LanDownloaderMessage.class));
  }

  @Test
  public void shouldKnowWhenNewNodesConnect() throws Exception {

    assertTrue(networkingService.onConnect(connection));

    verify(connection).getRemoteAddress();
  }
}