CAMEL-10112: Camel-CoAP: Add Ping operation to CoAP Producer
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/dd32693e Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/dd32693e Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/dd32693e Branch: refs/heads/master Commit: dd32693ecf142334fd04598d7e1e85107ba3fe51 Parents: 52ad120 Author: Andrea Cosentino <anco...@gmail.com> Authored: Fri Jul 1 11:54:56 2016 +0200 Committer: Andrea Cosentino <anco...@gmail.com> Committed: Fri Jul 1 12:00:54 2016 +0200 ---------------------------------------------------------------------- .../org/apache/camel/coap/CoAPProducer.java | 9 +++ .../org/apache/camel/coap/CoAPPingTest.java | 61 ++++++++++++++++++++ 2 files changed, 70 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/dd32693e/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPProducer.java b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPProducer.java index a3fead9..0e9ec13 100644 --- a/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPProducer.java +++ b/components/camel-coap/src/main/java/org/apache/camel/coap/CoAPProducer.java @@ -58,6 +58,7 @@ public class CoAPProducer extends DefaultProducer { } int mediaType = MediaTypeRegistry.parse(ct); CoapResponse response = null; + boolean pingResponse = false; switch (method) { case "GET": response = client.get(); @@ -73,6 +74,9 @@ public class CoAPProducer extends DefaultProducer { byte[] bodyPut = exchange.getIn().getBody(byte[].class); response = client.put(bodyPut, mediaType); break; + case "PING": + pingResponse = client.ping(); + break; default: break; } @@ -83,6 +87,11 @@ public class CoAPProducer extends DefaultProducer { resp.setHeader(org.apache.camel.Exchange.CONTENT_TYPE, mt); resp.setBody(response.getPayload()); } + + if (method.equalsIgnoreCase("PING")) { + Message resp = exchange.getOut(); + resp.setBody(pingResponse); + } } private synchronized CoapClient getClient(Exchange exchange) { http://git-wip-us.apache.org/repos/asf/camel/blob/dd32693e/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPPingTest.java ---------------------------------------------------------------------- diff --git a/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPPingTest.java b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPPingTest.java new file mode 100644 index 0000000..9ed98c1 --- /dev/null +++ b/components/camel-coap/src/test/java/org/apache/camel/coap/CoAPPingTest.java @@ -0,0 +1,61 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.coap; + +import org.apache.camel.Exchange; +import org.apache.camel.ExchangePattern; +import org.apache.camel.Processor; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.AvailablePortFinder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.eclipse.californium.core.network.config.NetworkConfig; +import org.junit.Test; + +public class CoAPPingTest extends CamelTestSupport { + static final int PORT = AvailablePortFinder.getNextAvailable(); + + @Produce(uri = "direct:start") + protected ProducerTemplate sender; + + @Test + public void testCoAP() throws Exception { + NetworkConfig.createStandardWithoutFile(); + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMinimumMessageCount(1); + mock.expectedBodiesReceived(true); + sender.sendBody("Hello"); + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("coap://localhost:" + PORT + "/TestResource?coapMethod=PING") + .to("log:exch") + .transform(body().convertTo(Boolean.class)) + .to("log:exch"); + + from("direct:start").to("coap://localhost:" + PORT + "/TestResource?coapMethod=PING").to("mock:result"); + } + }; + } +}