You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/02/16 17:57:18 UTC
[incubator-plc4x] branch feature/Beckhoff_ADS_protocol updated:
added tests for Plc4XADSProtocol
This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a commit to branch feature/Beckhoff_ADS_protocol
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
The following commit(s) were added to refs/heads/feature/Beckhoff_ADS_protocol by this push:
new 911135e added tests for Plc4XADSProtocol
911135e is described below
commit 911135eb10bbef5fc8e49c182e1ed22c17224d20
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Feb 16 18:57:14 2018 +0100
added tests for Plc4XADSProtocol
---
.../plc4x/java/ads/netty/Plc4XADSProtocol.java | 12 ++-
.../plc4x/java/ads/netty/Plc4XADSProtocolTest.java | 101 +++++++++++++++++++++
2 files changed, 108 insertions(+), 5 deletions(-)
diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/Plc4XADSProtocol.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/Plc4XADSProtocol.java
index 95f90f5..871374d 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/Plc4XADSProtocol.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/Plc4XADSProtocol.java
@@ -56,7 +56,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;
-public class Plc4XADSProtocol extends MessageToMessageCodec<AMSTCPPaket, PlcRequestContainer> {
+public class Plc4XADSProtocol extends MessageToMessageCodec<AMSTCPPaket, PlcRequestContainer<PlcRequest, PlcResponse>> {
private static final Logger LOGGER = LoggerFactory.getLogger(Plc4XADSProtocol.class);
@@ -78,7 +78,7 @@ public class Plc4XADSProtocol extends MessageToMessageCodec<AMSTCPPaket, PlcRequ
}
@Override
- protected void encode(ChannelHandlerContext ctx, PlcRequestContainer msg, List<Object> out) throws Exception {
+ protected void encode(ChannelHandlerContext ctx, PlcRequestContainer<PlcRequest, PlcResponse> msg, List<Object> out) throws Exception {
PlcRequest request = msg.getRequest();
if (request instanceof PlcReadRequest) {
encodeReadRequest(msg, out);
@@ -88,7 +88,7 @@ public class Plc4XADSProtocol extends MessageToMessageCodec<AMSTCPPaket, PlcRequ
}
- private void encodeWriteRequest(PlcRequestContainer msg, List<Object> out) throws PlcException {
+ private void encodeWriteRequest(PlcRequestContainer<PlcRequest, PlcResponse> msg, List<Object> out) throws PlcException {
PlcWriteRequest writeRequest = (PlcWriteRequest) msg.getRequest();
if (writeRequest.getRequestItems().size() != 1) {
throw new PlcProtocolException("Only one item supported");
@@ -113,13 +113,13 @@ public class Plc4XADSProtocol extends MessageToMessageCodec<AMSTCPPaket, PlcRequ
}
}
byte[] bytes = byteArrayOutputStream.toByteArray();
- Length length = Length.of(bytes.length);
Data data = Data.of(bytes);
AMSTCPPaket amstcpPaket = ADSWriteRequest.of(targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort, invokeId, indexGroup, indexOffset, data);
out.add(amstcpPaket);
+ requests.put(invokeId.getAsLong(), msg);
}
- private void encodeReadRequest(PlcRequestContainer msg, List<Object> out) throws PlcException {
+ private void encodeReadRequest(PlcRequestContainer<PlcRequest, PlcResponse> msg, List<Object> out) throws PlcException {
PlcReadRequest readRequest = (PlcReadRequest) msg.getRequest();
if (readRequest.getRequestItems().size() != 1) {
@@ -137,6 +137,7 @@ public class Plc4XADSProtocol extends MessageToMessageCodec<AMSTCPPaket, PlcRequ
Length length = Length.of(readRequestItem.getSize());
AMSTCPPaket amstcpPaket = ADSReadRequest.of(targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort, invokeId, indexGroup, indexOffset, length);
out.add(amstcpPaket);
+ requests.put(invokeId.getAsLong(), msg);
}
@Override
@@ -168,6 +169,7 @@ public class Plc4XADSProtocol extends MessageToMessageCodec<AMSTCPPaket, PlcRequ
if (response != null) {
plcRequestContainer.getResponseFuture().complete(response);
}
+ out.add(plcRequestContainer);
}
@SuppressWarnings("unchecked")
diff --git a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/netty/Plc4XADSProtocolTest.java b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/netty/Plc4XADSProtocolTest.java
new file mode 100644
index 0000000..75dc911
--- /dev/null
+++ b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/netty/Plc4XADSProtocolTest.java
@@ -0,0 +1,101 @@
+/*
+ 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.plc4x.java.ads.netty;
+
+import org.apache.plc4x.java.ads.api.generic.AMSTCPPaket;
+import org.apache.plc4x.java.ads.api.generic.types.AMSNetId;
+import org.apache.plc4x.java.ads.api.generic.types.AMSPort;
+import org.apache.plc4x.java.ads.model.ADSAddress;
+import org.apache.plc4x.java.api.messages.PlcReadRequest;
+import org.apache.plc4x.java.api.messages.PlcRequestContainer;
+import org.apache.plc4x.java.api.messages.PlcWriteRequest;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(Parameterized.class)
+public class Plc4XADSProtocolTest {
+ private static final Logger LOGGER = LoggerFactory.getLogger(ADSProtocolTest.class);
+
+ private Plc4XADSProtocol SUT;
+
+ @Parameterized.Parameter(0)
+ public PlcRequestContainer plcRequestContainer;
+
+ @Parameterized.Parameter(1)
+ public CompletableFuture completableFuture;
+
+ @Parameterized.Parameters(name = "{0} {index}")
+ public static Collection<Object[]> data() {
+ return Stream.of(
+ new PlcRequestContainer<>(
+ PlcWriteRequest
+ .builder()
+ .addItem(ADSAddress.of(1, 2), "HelloWorld!")
+ .build(), new CompletableFuture<>()),
+ new PlcRequestContainer<>(
+ PlcReadRequest
+ .builder()
+ .addItem(String.class, ADSAddress.of(1, 2))
+ .build(), new CompletableFuture<>())
+ ).map(plcRequestContainer -> new Object[]{plcRequestContainer, plcRequestContainer.getResponseFuture()}).collect(Collectors.toList());
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ AMSNetId targetAmsNetId = AMSNetId.of("1.2.3.4.5.6");
+ AMSPort targetAmsPort = AMSPort.of(7);
+ AMSNetId sourceAmsNetId = AMSNetId.of("8.9.10.11.12.13");
+ AMSPort sourceAmsPort = AMSPort.of(14);
+ SUT = new Plc4XADSProtocol(targetAmsNetId, targetAmsPort, sourceAmsNetId, sourceAmsPort);
+ }
+
+ @Test
+ public void encode() throws Exception {
+ ArrayList<Object> out = new ArrayList<>();
+ SUT.encode(null, plcRequestContainer, out);
+ assertEquals(1, out.size());
+ // TODO: replace with hamcrest
+ //assertThat(out, hasSize(1));
+ }
+
+ @Ignore("This doesn't work as the correlation requires a response to a request not another response")
+ @Test
+ public void decode() throws Exception {
+ ArrayList<Object> in = new ArrayList<>();
+ SUT.encode(null, plcRequestContainer, in);
+ ArrayList<Object> out = new ArrayList<>();
+ SUT.decode(null, ((AMSTCPPaket) in.get(0)), out);
+ assertEquals(1, out.size());
+ // TODO: replace with hamcrest
+ //assertThat(out, hasSize(1));
+ }
+}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
sruehl@apache.org.