Java Code Examples for org.apache.camel.CamelContext#close()

The following examples show how to use org.apache.camel.CamelContext#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NagiosIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendToNagiosWarn() throws Exception {

    CamelContext camelctx = createCamelContext();

    MessagePayload expectedPayload1 = new MessagePayload("localhost", Level.WARNING, camelctx.getName(),  "Hello Nagios");

    MockEndpoint mock = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    mock.expectedMessageCount(1);
    mock.expectedBodiesReceived("Hello Nagios");

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("direct:start", "Hello Nagios", NagiosConstants.LEVEL, Level.WARNING);

        mock.assertIsSatisfied();
        Mockito.verify(nagiosPassiveCheckSender).send(expectedPayload1);
    } finally {
        camelctx.close();
    }
}
 
Example 2
Source File: BeanValidatorXMLIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testBeanValidationSuccess() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .to("bean-validator://validate");
        }
    });

    camelctx.start();

    try {
        CarWithoutAnnotations car = new CarWithoutAnnotations("BMW", "DD-AB-123");

        ProducerTemplate template = camelctx.createProducerTemplate();
        CarWithoutAnnotations result = template.requestBody("direct:start", car, CarWithoutAnnotations.class);

        Assert.assertSame(car, result);
    } finally {
        camelctx.close();
    }
}
 
Example 3
Source File: ReactorIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testTo() throws Exception {
    CamelContext camelctx = createWildFlyCamelContext();
    camelctx.start();
    try {
        CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);

        Set<String> values = Collections.synchronizedSet(new TreeSet<>());
        CountDownLatch latch = new CountDownLatch(3);

        Flux.just(1, 2, 3)
            .flatMap(e -> crs.to("bean:hello", e, String.class))
            .doOnNext(res -> values.add(res))
            .doOnNext(res -> latch.countDown())
            .subscribe();

        Assert.assertTrue(latch.await(2, TimeUnit.SECONDS));
        Assert.assertEquals(new TreeSet<>(Arrays.asList("Hello 1", "Hello 2", "Hello 3")), values);
    } finally {
        camelctx.close();
    }
}
 
Example 4
Source File: DockerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testDockerComponentForUnixSocket() throws Exception {
    File dockerSocket = new File("/var/run/docker.sock");

    Assume.assumeTrue("Docker socket /var/run/docker.sock does not exist or is not writable", dockerSocket.exists() && dockerSocket.canWrite());

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .toF("docker:version?socket=true&host=%s", dockerSocket.getPath());
        }
    });

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        Version dockerVersion = template.requestBody("direct:start", null, Version.class);
        Assert.assertNotNull("Docker version not null", dockerVersion);
        Assert.assertFalse("Docker version was empty", dockerVersion.getVersion().isEmpty());
    } finally {
        camelctx.close();
    }
}
 
Example 5
Source File: DigitalOceanIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void getImages() throws Exception {
    CamelContext camelctx = createCamelContext(oauthToken);
    camelctx.addRoutes(createRouteBuilder());

    camelctx.start();
    try {
        MockEndpoint mockResult = camelctx.getEndpoint("mock:result", MockEndpoint.class);
        mockResult.expectedMinimumMessageCount(1);

        ProducerTemplate producer = camelctx.createProducerTemplate();
        List<Image> images = producer.requestBody("direct:getImages", null, List.class);

        mockResult.assertIsSatisfied();
        Assert.assertNotEquals(1, images.size());
    } finally {
        camelctx.close();
    }
}
 
Example 6
Source File: JSONDataFormatTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testMarshalJohnzon() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .marshal().json(JsonLibrary.Johnzon);
        }
    });

    String expected = "{'firstName':'John','lastName':'Doe'}";

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        String result = producer.requestBody("direct:start", new Customer("John", "Doe"), String.class);
        Assert.assertEquals(expected.replace('\'', '"'), result);
    } finally {
        camelctx.close();
    }
}
 
Example 7
Source File: JingIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testJingRngSchemaValidationSuccess() throws Exception {
    CamelContext camelctx = createCamelContext(false);

    MockEndpoint mockEndpointValid = camelctx.getEndpoint("mock:valid", MockEndpoint.class);
    mockEndpointValid.expectedMessageCount(1);

    MockEndpoint mockEndpointInvalid = camelctx.getEndpoint("mock:invalid", MockEndpoint.class);
    mockEndpointInvalid.expectedMessageCount(0);

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBody("direct:start", ORDER_XML_VALID);

        mockEndpointValid.assertIsSatisfied();
        mockEndpointInvalid.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 8
Source File: TwitterIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testPostStatusUpdate() throws Exception {

    final CamelTwitterSupport twitter = new CamelTwitterSupport();

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").to("twitter-timeline://user?" + twitter.getUriTokens());
        }
    });

    Assume.assumeTrue("[#1672] Enable Twitter testing in Jenkins", twitter.hasUriTokens());

    camelctx.start();
    try {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss dd-MMM-yyyy");
        String message = "Its a good day to test Twitter on " + format.format(new Date());
        ProducerTemplate producer = camelctx.createProducerTemplate();
        String result = producer.requestBody("direct:start", message, String.class);
        Assert.assertTrue("Unexpected: " + result, result.contains("(wfcameltest) Its a good day to test Twitter"));
    } finally {
        camelctx.close();
    }
}
 
Example 9
Source File: UndertowWsIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void wsClientSingleText() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            final int port = getPort();
            from("undertow:ws://localhost:" + port + "/app1")
                    .log(">>> Message received from WebSocket Client : ${body}").to("mock:result1");
        }

    });

    camelctx.start();
    try {
        TestClient websocket = new TestClient("ws://localhost:" + getPort() + "/app1").connect();

        MockEndpoint result = camelctx.getEndpoint("mock:result1", MockEndpoint.class);
        result.expectedBodiesReceived("Test");

        websocket.sendTextMessage("Test");

        result.await(60, TimeUnit.SECONDS);
        result.assertIsSatisfied();

        websocket.close();
    } finally {
        camelctx.close();
    }

}
 
Example 10
Source File: UndertowWsIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void wsClientSingleBytesStreaming() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            final int port = getPort();
            from("undertow:ws://localhost:" + port + "/app2?useStreaming=true").to("mock:result2");
        }
    });

    camelctx.start();
    try {
        TestClient websocket = new TestClient("ws://localhost:" + getPort() + "/app2").connect();

        MockEndpoint result = camelctx.getEndpoint("mock:result2", MockEndpoint.class);
        result.expectedMessageCount(1);

        final byte[] testmessage = "Test".getBytes("utf-8");
        websocket.sendBytesMessage(testmessage);

        result.await(60, TimeUnit.SECONDS);
        List<Exchange> exchanges = result.getReceivedExchanges();
        Assert.assertEquals(1, exchanges.size());
        Object body = result.getReceivedExchanges().get(0).getIn().getBody();
        Assert.assertTrue("body is " + body.getClass().getName(), body instanceof InputStream);
        InputStream in = (InputStream) body;
        Assert.assertArrayEquals(testmessage, IOConverter.toBytes(in));

        websocket.close();
    } finally {
        camelctx.close();
    }
}
 
Example 11
Source File: UnivocityIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testMarshalUnmarshalCSV() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            UniVocityCsvDataFormat dataFormat = new UniVocityCsvDataFormat();
            from("direct:marshal").marshal(dataFormat);
            from("direct:unmarshal").unmarshal(dataFormat);
        }
    });

    camelctx.start();
    try {
        ProducerTemplate producer = camelctx.createProducerTemplate();
        Map<String, String> input = asMap("A", "1", "B", "2", "C", "3");
        String res1 = producer.requestBody("direct:marshal", input, String.class);
        Assert.assertEquals(join("1,2,3"), res1);

        List<?> res2 = producer.requestBody("direct:unmarshal", res1, List.class);
        Assert.assertEquals("Expected one row", 1, res2.size());
        Assert.assertEquals(Arrays.asList("1" ,"2", "3"), res2.get(0));
    } finally {
        camelctx.close();
    }
}
 
Example 12
Source File: ReactorIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromStreamMultipleSubscriptionsWithDirect() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:reactive")
                .to("reactive-streams:direct");
        }
    });

    camelctx.start();
    try {
        CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);

        CountDownLatch latch1 = new CountDownLatch(2);
        Flux.from(crs.fromStream("direct", Integer.class))
            .doOnNext(res -> latch1.countDown())
            .subscribe();

        CountDownLatch latch2 = new CountDownLatch(2);
        Flux.from(crs.fromStream("direct", Integer.class))
            .doOnNext(res -> latch2.countDown())
            .subscribe();

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBody("direct:reactive", 1);
        template.sendBody("direct:reactive", 2);

        Assert.assertTrue(latch1.await(5, TimeUnit.SECONDS));
        Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));
    } finally {
        camelctx.close();
    }
}
 
Example 13
Source File: JCloudsBlobStoreIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlobStoreProducer() throws Exception {
    BlobStore blobStore = getBlobStore();

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .setHeader(JcloudsConstants.BLOB_NAME, constant(BLOB_NAME_WITH_DIR))
            .setHeader(JcloudsConstants.CONTAINER_NAME, constant(CONTAINER_NAME_WITH_DIR))
            .to("jclouds:blobstore:transient");
        }
    });

    List<BlobStore> blobStores = new ArrayList<>();
    blobStores.add(blobStore);

    JcloudsComponent jclouds = camelctx.getComponent("jclouds", JcloudsComponent.class);
    jclouds.setBlobStores(blobStores);

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        String result = template.requestBody("direct:start", "Hello Kermit", String.class);
        Assert.assertEquals("Hello Kermit", result);
    } finally {
        camelctx.close();
    }

}
 
Example 14
Source File: ConsulIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testListDatacenters() throws Exception {
    List<String> ref = getConsul().catalogClient().getDatacenters();

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.getComponent("consul", ConsulComponent.class).getConfiguration().setUrl(consulUrl);
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:consul")
            .to("consul:catalog");
        }
    });

    camelctx.start();
    try {
        List<?> res = fluentTemplate(camelctx)
                .withHeader(ConsulConstants.CONSUL_ACTION, ConsulCatalogActions.LIST_DATACENTERS)
                .to("direct:consul")
                .request(List.class);

        Assert.assertFalse(ref.isEmpty());
        Assert.assertFalse(res.isEmpty());
        Assert.assertEquals(ref, res);
    } finally {
        camelctx.close();
    }
}
 
Example 15
Source File: SJMS2IntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testSJMS2Consumer() throws Exception {
    CamelContext camelctx = new DefaultCamelContext(new JndiBeanRepository());
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("sjms2:queue:" + QUEUE_NAME + "?connectionFactory=#ConnectionFactory")
            .setBody(simple("Hello ${body}"))
            .to("mock:result");
        }
    });

    MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    mockEndpoint.expectedBodiesReceived("Hello Kermit");

    camelctx.start();

    try {
        // Send a message to the queue
        ConnectionFactory cfactory = (ConnectionFactory) initialctx.lookup("java:/ConnectionFactory");
        Connection connection = cfactory.createConnection();
        try {
            sendMessage(connection, QUEUE_JNDI_NAME, "Kermit");
            mockEndpoint.assertIsSatisfied();
        } finally {
            connection.close();
        }
    } finally {
        camelctx.close();
    }
}
 
Example 16
Source File: StompIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testMessageConsumerRoute() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            fromF("stomp:%s?login=%s&passcode=%s&brokerURL=%s", QUEUE_NAME, USERNAME, PASSWORD, BROKER_URL)
            .transform(body().prepend("Hello "))
            .to("mock:result");
        }
    });

    camelctx.start();

    MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    mockEndpoint.expectedBodiesReceived("Hello Kermit");
    mockEndpoint.setResultWaitTime(30000);

    Stomp stomp = new Stomp(BROKER_URL);
    stomp.setLogin(USERNAME);
    stomp.setPasscode(PASSWORD);

    final BlockingConnection producerConnection = stomp.connectBlocking();
    try {
        StompFrame outFrame = new StompFrame(SEND);
        outFrame.addHeader(DESTINATION, StompFrame.encodeHeader(QUEUE_NAME));
        outFrame.addHeader(MESSAGE_ID, StompFrame
            .encodeHeader("StompIntegrationTest.testMessageConsumerRoute" + UUID.randomUUID().toString()));
        outFrame.content(utf8("Kermit"));
        producerConnection.send(outFrame);

        mockEndpoint.assertIsSatisfied();
    } finally {
        if (producerConnection != null) {
            producerConnection.close();
        }
        camelctx.close();
    }
}
 
Example 17
Source File: SMPPIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testSMPPComponent() throws Exception {
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .to("smpp://smppuser@" + TestUtils.getDockerHost() + ":2775?password=password&systemType=producer");

            from("smpp://smppuser@" + TestUtils.getDockerHost() + ":2775?password=password&systemType=consumer")
            .setBody(simple("Hello ${body}"))
            .to("mock:result");
        }
    });

    MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    mockEndpoint.expectedBodiesReceived("Hello Kermit");

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBody("direct:start", "Kermit");
        mockEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 18
Source File: NsqIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testNsqComponent() throws Exception {

    // [#2862] NsqIntegrationTest cannot access DOCKER_HOST
    Assume.assumeFalse(EnvironmentUtils.isMac());

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            fromF("nsq://%s:4161?topic=%s&lookupInterval=2s&autoFinish=false&requeueInterval=1s", TestUtils.getDockerHost(), TOPIC_NAME)
            .to("mock:result");
        }
    });

    camelctx.start();
    try {
        MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
        mockEndpoint.expectedBodiesReceived("Hello Kermit");

        NSQProducer producer = new NSQProducer();
        producer.addAddress(TestUtils.getDockerHost(), 4150);
        producer.start();
        producer.produce(TOPIC_NAME, "Hello Kermit".getBytes());

        mockEndpoint.assertIsSatisfied(5000);
    } finally {
        camelctx.close();
    }
}
 
Example 19
Source File: ServiceNowIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testSearchIncidents() throws Exception {
    Map<String, Object> serviceNowOptions = createServiceNowOptions();

    Assume.assumeTrue("[#1674] Enable ServiceNow testing in Jenkins",
            serviceNowOptions.size() == ServiceNowIntegrationTest.ServiceNowOption.values().length);

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .to("servicenow:{{env:SERVICENOW_INSTANCE}}"
                + "?userName={{env:SERVICENOW_USERNAME}}"
                + "&password={{env:SERVICENOW_PASSWORD}}"
                + "&oauthClientId={{env:SERVICENOW_CLIENT_ID}}"
                + "&oauthClientSecret={{env:SERVICENOW_CLIENT_SECRET}}"
                + "&release={{env:SERVICENOW_RELEASE}}"
                + "&model.incident=org.wildfly.camel.test.servicenow.subA.Incident");
        }
    });

    camelctx.start();
    try {
        Map<String, Object> headers = new HashMap<>();
        headers.put(ServiceNowConstants.RESOURCE, "table");
        headers.put(ServiceNowConstants.ACTION, ServiceNowConstants.ACTION_RETRIEVE);
        headers.put(ServiceNowParams.PARAM_TABLE_NAME.getHeader(), "incident");

        ProducerTemplate producer = camelctx.createProducerTemplate();
        List<Incident> result = producer.requestBodyAndHeaders("direct:start", null, headers, List.class);

        Assert.assertNotNull(result);
        Assert.assertTrue(result.size() > 0);
    } finally {
        camelctx.close();
    }
}
 
Example 20
Source File: ReactorIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleSubscriptionsWithTimer() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("timer:tick?period=50")
                .setBody().header(Exchange.TIMER_COUNTER)
                .to("reactive-streams:tick");
        }
    });

    CountDownLatch latch1 = new CountDownLatch(5);
    CamelReactiveStreamsService crs = CamelReactiveStreams.get(camelctx);
    Disposable disp1 = Flux.from(crs.fromStream("tick", Integer.class)).subscribe(res -> latch1.countDown());

    camelctx.start();
    try {
        // Add another subscription
        CountDownLatch latch2 = new CountDownLatch(5);
        Disposable disp2 = Flux.from(crs.fromStream("tick", Integer.class)).subscribe(res -> latch2.countDown());

        Assert.assertTrue(latch1.await(5, TimeUnit.SECONDS));
        Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));

        // Un subscribe both
        disp1.dispose();
        disp2.dispose();

        // No active subscriptions, warnings expected
        Thread.sleep(60);

        // Add another subscription
        CountDownLatch latch3 = new CountDownLatch(5);
        Disposable disp3 = Flux.from(crs.fromStream("tick", Integer.class)).subscribe(res -> latch3.countDown());

        Assert.assertTrue(latch3.await(5, TimeUnit.SECONDS));
        disp3.dispose();
    } finally {
        camelctx.close();
    }
}