/*
   * Deploy a new channel, send messages, assert
   * that:
   * - The message was stored in the database
   * - The MessageResponse returned is not null
   * - The response is null
   * - The source connector response was not stored
   *
   * Call storeMessageResponse using a null MessageResponse assert:
   * - The source connector response was not stored
   *
   * Call storeMessageResponse using the returned MessageResponse, assert:
   * - The source connector response was not stored
   *
   * Modify the MessageResponse, creating/setting a new Response object
   * Call storeMessageResponse using the returned MessageResponse, assert:
   * - The source connector response was stored
   */
  @Test
  public final void testStoreMessageResponse() throws Exception {
    TestChannel channel = (TestChannel) TestUtils.createDefaultChannel(channelId, serverId);

    TestSourceConnector sourceConnector = (TestSourceConnector) channel.getSourceConnector();
    sourceConnector.setRespondAfterProcessing(true);
    channel
        .getResponseSelector()
        .setRespondFromName(ResponseConnectorProperties.RESPONSE_SOURCE_TRANSFORMED);

    channel.deploy();
    channel.start();

    for (int i = 1; i <= TEST_SIZE; i++) {
      RawMessage rawMessage = new RawMessage(testMessage);
      DispatchResult dispatchResult = null;

      try {
        dispatchResult = sourceConnector.dispatchRawMessage(rawMessage);

        if (dispatchResult.getSelectedResponse() != null) {
          dispatchResult.getSelectedResponse().setMessage("response");
        }
      } finally {
        sourceConnector.finishDispatch(dispatchResult, true, null);
      }

      if (dispatchResult != null) {
        sourceConnector.getMessageIds().add(dispatchResult.getMessageId());
      }

      // Assert that the message was created
      Message message = new Message();
      message.setChannelId(channel.getChannelId());
      message.setMessageId(dispatchResult.getMessageId());
      message.setServerId(channel.getServerId());
      message.setProcessed(true);
      TestUtils.assertMessageExists(message, false);

      // Assert that the message response is not null
      assertNotNull(dispatchResult);

      // Assert that the response is not null
      assertNotNull(dispatchResult.getSelectedResponse());

      // Assert that the source connector response was created
      TestUtils.assertResponseExists(channel.getChannelId(), dispatchResult.getMessageId());
    }

    channel.stop();
    channel.undeploy();

    ChannelController.getInstance().removeChannel(channel.getChannelId());
  }
  @Override
  protected List<Message> getItems(int offset, int limit) throws Exception {
    List<Map<String, Object>> maps;
    SqlSession session = SqlConfig.getSqlSessionManager().openSession();
    params.put("offset", offset);
    params.put("limit", limit);

    try {
      maps = session.selectList("Message.getMessagesToPrune", params);
    } finally {
      session.close();
    }

    List<Message> messages = new ArrayList<Message>();
    DonkeyDao dao = daoFactory.getDao();

    try {
      for (Map<String, Object> map : maps) {
        Long messageId = (Long) map.get("id");
        long connectorReceivedDateMillis =
            ((Calendar) map.get("mm_received_date")).getTimeInMillis();

        Map<Integer, ConnectorMessage> connectorMessages = null;
        connectorMessages = dao.getConnectorMessages(channelId, messageId);

        Message message = new Message();
        message.setMessageId(messageId);
        message.setChannelId(channelId);
        message.setReceivedDate((Calendar) map.get("received_date"));
        message.setProcessed((Boolean) map.get("processed"));
        message.setServerId((String) map.get("server_id"));
        message.setImportId((Long) map.get("import_id"));
        message.getConnectorMessages().putAll(connectorMessages);

        messages.add(message);

        contentMessageIds.add(messageId);

        if (messageDateThreshold != null
            && connectorReceivedDateMillis < messageDateThreshold.getTimeInMillis()) {
          messageIds.add(messageId);
        }
      }

      return messages;
    } finally {
      dao.close();
    }
  }
Example #3
0
  public static Message createTestProcessedMessage(
      String channelId, String serverId, long messageId, String content) {
    Calendar receivedDate = Calendar.getInstance();

    Message message = new Message();
    message.setMessageId(messageId);
    message.setChannelId(channelId);
    message.setServerId(serverId);
    message.setReceivedDate(receivedDate);
    message.setProcessed(true);

    ConnectorMessage sourceMessage =
        new ConnectorMessage(
            channelId,
            message.getMessageId(),
            0,
            serverId,
            message.getReceivedDate(),
            Status.TRANSFORMED);
    message.getConnectorMessages().put(0, sourceMessage);

    ConnectorMessage destinationMessage =
        new ConnectorMessage(
            channelId, message.getMessageId(), 1, serverId, message.getReceivedDate(), Status.SENT);
    message.getConnectorMessages().put(1, destinationMessage);

    sourceMessage.setRaw(
        new MessageContent(
            channelId, message.getMessageId(), 0, ContentType.RAW, content, null, false));
    destinationMessage.setRaw(
        new MessageContent(
            channelId, message.getMessageId(), 1, ContentType.RAW, content, null, false));

    return message;
  }
  /*
   * Deploy a new channel, assert that:
   * - The default behaviour for the source connector is to wait for
   * destinations
   *
   * Send messages that should immediately process, assert that:
   * - The message was created and marked as processed in the database
   * - The MessageResponse returned is not null
   * - The response status is correct (TRANSFORMED)
   * - The channel's final transaction was created
   * - The source connector response was stored
   *
   * Send messages that should immediately queue, assert that:
   * - The message was stored in the database
   * - The MessageResponse returned is not null
   * - The response is not null
   * - The source connector response was not stored
   */
  @Test
  public final void testHandleRawMessage() throws Exception {
    TestChannel channel = (TestChannel) TestUtils.createDefaultChannel(channelId, serverId);
    TestSourceConnector sourceConnector = (TestSourceConnector) channel.getSourceConnector();
    sourceConnector.setRespondAfterProcessing(true);
    channel
        .getResponseSelector()
        .setRespondFromName(ResponseConnectorProperties.RESPONSE_SOURCE_TRANSFORMED);

    channel.deploy();
    channel.start();

    // Assert that the default source connector behaviour is to wait for destinations
    assertTrue(sourceConnector.isRespondAfterProcessing());

    // Send messages that immediately process
    for (int i = 1; i <= TEST_SIZE; i++) {
      RawMessage rawMessage = new RawMessage(testMessage);
      DispatchResult dispatchResult = null;

      try {
        dispatchResult = sourceConnector.dispatchRawMessage(rawMessage);

        if (dispatchResult.getSelectedResponse() != null) {
          dispatchResult.getSelectedResponse().setMessage("response");
        }
      } finally {
        sourceConnector.finishDispatch(dispatchResult, true, null);
      }

      if (dispatchResult != null) {
        sourceConnector.getMessageIds().add(dispatchResult.getMessageId());
      }

      // Assert that the message was created and processed
      Message message = new Message();
      message.setChannelId(channel.getChannelId());
      message.setMessageId(dispatchResult.getMessageId());
      message.setServerId(channel.getServerId());
      message.setProcessed(true);
      TestUtils.assertMessageExists(message, false);

      // Assert that the message response is not null
      assertNotNull(dispatchResult);
      // Assert that the response is not null
      assertNotNull(dispatchResult.getSelectedResponse());
      // Assert that the response status is correct
      assertEquals(Status.TRANSFORMED, dispatchResult.getSelectedResponse().getStatus());
      // Assert that the source connector response was created
      TestUtils.assertResponseExists(channel.getChannelId(), dispatchResult.getMessageId());
    }

    // Send messages that queue
    sourceConnector.setRespondAfterProcessing(false);
    channel.getResponseSelector().setRespondFromName(null);

    for (int i = 1; i <= TEST_SIZE; i++) {
      RawMessage rawMessage = new RawMessage(testMessage);
      DispatchResult dispatchResult = null;

      try {
        dispatchResult = sourceConnector.dispatchRawMessage(rawMessage);
      } finally {
        sourceConnector.finishDispatch(dispatchResult);
      }

      if (dispatchResult != null) {
        sourceConnector.getMessageIds().add(dispatchResult.getMessageId());
      }

      // Assert that the message was created
      Message message = new Message();
      message.setChannelId(channel.getChannelId());
      message.setMessageId(dispatchResult.getMessageId());
      message.setServerId(channel.getServerId());
      message.setProcessed(false);
      try {
        TestUtils.assertMessageExists(message, false);
      } catch (Exception e) {
        message.setProcessed(true);
        TestUtils.assertMessageExists(message, false);
      }

      // Assert that the message response is not null
      assertNotNull(dispatchResult);
      // Assert that the response is null
      assertNull(dispatchResult.getSelectedResponse());
      // Assert that the source connector response was not created
      TestUtils.assertResponseDoesNotExist(channel.getChannelId(), dispatchResult.getMessageId());
    }

    channel.stop();
    channel.undeploy();

    ChannelController.getInstance().removeChannel(channel.getChannelId());
  }