@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());
  }
  @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());
  }
 @Test
 public void shouldDeletePendingEmails() throws Exception {
   Message<?> message =
       new AllMessage<String>(MessEngineConstants.DELETE_PENDING_EMAILS_TYPE, "delete");
   messEngine.send(message);
   verify(defaultTemplate).delete(anyString(), eq(message.getBody()));
 }
  @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());
  }
  @SuppressWarnings("unchecked")
  @Test
  // TODO Hacer m�s complejo este test
  public void shouldProcessContactListRequest() throws Exception {
    String email = "*****@*****.**";
    List<ContactInfo> contactlist = new ArrayList<ContactInfo>();
    AllMessage<String> request =
        new AllMessage<String>(MessEngineConstants.CONTACT_LIST_REQUEST_TYPE, email);
    when(defaultTemplate.getForObject(anyString(), any(Class.class), eq(request)))
        .thenReturn(contactlist);

    messEngine.send(request);

    Message<List<ContactInfo>> response =
        (Message<List<ContactInfo>>)
            messEngine.getMessage(MessEngineConstants.CONTACT_LIST_RESPONSE_TYPE);
    assertNotNull(response);
  }
  @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());
  }
  @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());
  }
  @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()));
  }
 @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());
 }
  @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());
  }
  @Test
  public void shouldProcessDeleteContactsMessage() throws Exception {
    String body = "contactIds";
    String sender = "sender";
    AllMessage<String> message =
        new AllMessage<String>(MessEngineConstants.DELETE_CONTACTS_TYPE, body);
    message.putProperty(MessEngineConstants.SENDER_ID, sender);

    messEngine.send(message);

    verify(defaultTemplate).delete(anyString(), eq(sender), eq(body));
  }
  @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());
  }
  @Test
  public void shouldDeleteAlert() throws Exception {
    final String id = "id";
    Alert alert =
        new AbstractAlert() {
          @Override
          public String getId() {
            return id;
          }
        };
    messEngine.send(new AllMessage<Alert>(MessEngineConstants.DELETE_ALERT_TYPE, alert));

    verify(defaultTemplate).delete(anyString(), eq(alert.getId()));
  }
  @Before
  public void setup() {
    MockitoAnnotations.initMocks(this);
    messEngine.setup(allServerAdapter);
    when(clientSettings.getProperty(anyString())).thenReturn("some url");

    allServerAdapter.initialize();

    mergeRequest =
        new AllMessage<SyncValueObject>(MessEngineConstants.SYNC_LIBRARY_MERGE_REQUEST, request);
    commitRequest =
        new AllMessage<SyncValueObject>(MessEngineConstants.SYNC_SEND_DELTA_REQUEST, request);
    initFeedRequests();
  }