コード例 #1
0
ファイル: CamelContextTest.java プロジェクト: dlacy27/main
  @Test
  public void testNotificationBroker() throws Exception {

    notificationBrokerMockEndpoint.expectedMessageCount(1);
    loggingEndpoint.expectedMessageCount(1);

    // Create a new exchange
    Exchange senderExchange = new DefaultExchange(context);

    // Set the WS-Address Message ID
    Map<String, Object> requestContext = OJBUtils.setWSAddressingMessageID("123456789");

    // Set the operation name and operation namespace for the CXF exchange
    senderExchange.getIn().setHeader(Client.REQUEST_CONTEXT, requestContext);

    Document doc = createDocument();
    List<SoapHeader> soapHeaders = new ArrayList<SoapHeader>();
    soapHeaders.add(
        makeSoapHeader(doc, "http://www.w3.org/2005/08/addressing", "MessageID", "12345"));
    senderExchange.getIn().setHeader(Header.HEADER_LIST, soapHeaders);

    org.apache.cxf.message.Message message = new MessageImpl();

    senderExchange.getIn().setHeader(CxfConstants.CAMEL_CXF_MESSAGE, message);
    senderExchange
        .getIn()
        .setHeader(CxfConstants.OPERATION_NAME, CXF_OPERATION_NAME_CRIMINAL_HISTORY);
    senderExchange
        .getIn()
        .setHeader(CxfConstants.OPERATION_NAMESPACE, CXF_OPERATION_NAMESPACE_CRIMINAL_HISTORY);

    // Read the firearm search request file from the file system
    File inputFile =
        new File(
            "src/test/resources/xmlInstances/cycleTrackingIdentifierAssignmentReport/Cycle-Tracking-Identifier-Assignment-Report.xml");
    String inputStr = FileUtils.readFileToString(inputFile);

    assertNotNull(inputStr);

    log.debug(inputStr);

    // Set it as the message message body
    senderExchange.getIn().setBody(inputStr);

    // Send the one-way exchange.  Using template.send will send an one way message
    Exchange returnExchange =
        template.send("direct:criminalHistoryUpdatedReportingService", senderExchange);

    // Use getException to see if we received an exception
    if (returnExchange.getException() != null) {
      throw new Exception(returnExchange.getException());
    }

    // Sleep while a response is generated
    Thread.sleep(3000);

    // Assert that the mock endpoint expectations are satisfied
    notificationBrokerMockEndpoint.assertIsSatisfied();
    loggingEndpoint.assertIsSatisfied();

    // Get the first exchange (the only one)
    Exchange ex = notificationBrokerMockEndpoint.getExchanges().get(0);

    String opName = (String) ex.getIn().getHeader("operationName");
    assertEquals("Notify", opName);

    String opNamespace = (String) ex.getIn().getHeader("operationNamespace");
    assertEquals("http://docs.oasis-open.org/wsn/brw-2", opNamespace);

    Document returnDocumentNotificationBroker = ex.getIn().getBody(Document.class);

    // Do some very basic assertions to assure the message is transformed.
    // The XSLT test does a more complete examination of the transformation.
    Node notifyNode = XmlUtils.xPathNodeSearch(returnDocumentNotificationBroker, "/b-2:Notify");
    Node notifyMesssageNode = XmlUtils.xPathNodeSearch(notifyNode, "b-2:NotificationMessage");

    Node messageNode = XmlUtils.xPathNodeSearch(notifyMesssageNode, "b-2:Message");
    assertNotNull(messageNode);

    // Get the first exchange (the only one) to the logger
    // This is what would be sent to the implementation specific route
    Exchange exImplementationSpecific = loggingEndpoint.getExchanges().get(0);

    Document returnDocumentImplementationSpecific =
        exImplementationSpecific.getIn().getBody(Document.class);

    // Make sure the root node here is the message to the original exchange
    Node rootNode =
        XmlUtils.xPathNodeSearch(
            returnDocumentImplementationSpecific,
            "/crimhistory-update-exch:CycleTrackingIdentifierAssignmentReport");
    assertNotNull(rootNode);

    // XmlUtils.printNode(returnDocumentImplementationSpecific);
  }