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/01/12 13:44:12 UTC

[incubator-plc4x] branch master updated: added some test coverage to camel-component

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/master by this push:
     new e1d1bb8  added some test coverage to camel-component
e1d1bb8 is described below

commit e1d1bb8d6a832b80b6598eff0219922d1be94a6b
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Jan 12 14:44:06 2018 +0100

    added some test coverage to camel-component
---
 .../java/org/apache/plc4x/camel/PLC4XEndpoint.java | 18 +++++-
 .../java/org/apache/plc4x/camel/PLC4XProducer.java |  2 +-
 .../java/org/apache/plc4x/camel/ConstantsTest.java | 12 +++-
 .../org/apache/plc4x/camel/PLC4XComponentTest.java |  8 +++
 .../org/apache/plc4x/camel/PLC4XEndpointTest.java  | 34 ++++++++++++
 .../org/apache/plc4x/camel/PLC4XProducerTest.java  | 64 ++++++++++++++++++++++
 6 files changed, 133 insertions(+), 5 deletions(-)

diff --git a/integrations/apache-camel/src/main/java/org/apache/plc4x/camel/PLC4XEndpoint.java b/integrations/apache-camel/src/main/java/org/apache/plc4x/camel/PLC4XEndpoint.java
index 4fd84e4..f396574 100644
--- a/integrations/apache-camel/src/main/java/org/apache/plc4x/camel/PLC4XEndpoint.java
+++ b/integrations/apache-camel/src/main/java/org/apache/plc4x/camel/PLC4XEndpoint.java
@@ -37,7 +37,7 @@ public class PLC4XEndpoint extends DefaultEndpoint {
     @UriPath
     @Metadata(required = "true")
     @SuppressWarnings("unused")
-    String driver;
+    private String driver;
 
     /**
      * The address for the PLC4X driver
@@ -45,9 +45,9 @@ public class PLC4XEndpoint extends DefaultEndpoint {
     @UriPath
     @Metadata(required = "true")
     @SuppressWarnings("unused")
-    String address;
+    private String address;
 
-    final PlcDriverManager plcDriverManager;
+    private final PlcDriverManager plcDriverManager;
 
     public PLC4XEndpoint(String endpointUri, Component component) {
         super(endpointUri, component);
@@ -68,4 +68,16 @@ public class PLC4XEndpoint extends DefaultEndpoint {
     public boolean isSingleton() {
         return true;
     }
+
+    public String getDriver() {
+        return driver;
+    }
+
+    public String getAddress() {
+        return address;
+    }
+
+    public PlcDriverManager getPlcDriverManager() {
+        return plcDriverManager;
+    }
 }
diff --git a/integrations/apache-camel/src/main/java/org/apache/plc4x/camel/PLC4XProducer.java b/integrations/apache-camel/src/main/java/org/apache/plc4x/camel/PLC4XProducer.java
index 891de5b..ab67d46 100644
--- a/integrations/apache-camel/src/main/java/org/apache/plc4x/camel/PLC4XProducer.java
+++ b/integrations/apache-camel/src/main/java/org/apache/plc4x/camel/PLC4XProducer.java
@@ -44,7 +44,7 @@ public class PLC4XProducer extends DefaultAsyncProducer {
         super(endpoint);
         this.endpoint = endpoint;
         String plc4xURI = endpoint.getEndpointUri().replaceFirst("plc4x:/?/?", "");
-        plcConnection = endpoint.plcDriverManager.getConnection(plc4xURI);
+        plcConnection = endpoint.getPlcDriverManager().getConnection(plc4xURI);
         openRequests = new AtomicInteger();
     }
 
diff --git a/integrations/apache-camel/src/test/java/org/apache/plc4x/camel/ConstantsTest.java b/integrations/apache-camel/src/test/java/org/apache/plc4x/camel/ConstantsTest.java
index 6e34be1..51a6aac 100644
--- a/integrations/apache-camel/src/test/java/org/apache/plc4x/camel/ConstantsTest.java
+++ b/integrations/apache-camel/src/test/java/org/apache/plc4x/camel/ConstantsTest.java
@@ -4,10 +4,20 @@ package org.apache.plc4x.camel;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
+import java.lang.reflect.Constructor;
+
 public class ConstantsTest {
 
     @Test
     public void testConstantsNotInstanceable() throws Exception {
-        Assertions.assertThrows(IllegalAccessException.class, Constants.class::newInstance);
+        Assertions.assertThrows(IllegalStateException.class, () -> {
+            try {
+                Constructor<Constants> constructor = Constants.class.getDeclaredConstructor();
+                constructor.setAccessible(true);
+                constructor.newInstance();
+            } catch (Exception e) {
+                throw e.getCause();
+            }
+        });
     }
 }
\ No newline at end of file
diff --git a/integrations/apache-camel/src/test/java/org/apache/plc4x/camel/PLC4XComponentTest.java b/integrations/apache-camel/src/test/java/org/apache/plc4x/camel/PLC4XComponentTest.java
index 30dc30c..117e9c4 100644
--- a/integrations/apache-camel/src/test/java/org/apache/plc4x/camel/PLC4XComponentTest.java
+++ b/integrations/apache-camel/src/test/java/org/apache/plc4x/camel/PLC4XComponentTest.java
@@ -27,6 +27,7 @@ import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
+import java.util.Arrays;
 import java.util.concurrent.TimeUnit;
 
 public class PLC4XComponentTest extends CamelTestSupport {
@@ -45,8 +46,10 @@ public class PLC4XComponentTest extends CamelTestSupport {
     public void testSimpleRouting() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMinimumMessageCount(1);
+        mock.expectedMessageCount(2);
 
         template.asyncSendBody("direct:plc4x", "irrelevant");
+        template.asyncSendBody("direct:plc4x2", "irrelevant");
 
         assertMockEndpointsSatisfied(2, TimeUnit.SECONDS);
     }
@@ -60,6 +63,11 @@ public class PLC4XComponentTest extends CamelTestSupport {
                     .setBody(constant((byte) 0x0))
                     .to("plc4x:mock:10.10.10.1/1/1")
                     .to("mock:result");
+                from("direct:plc4x2")
+                    .setHeader(Constants.ADDRESS_HEADER, constant(new S7Address(MemoryArea.INPUTS, (short) 0x44)))
+                    .setBody(constant(Arrays.asList((byte) 0x0, (byte) 0x1, (byte) 0x2, (byte) 0x3)))
+                    .to("plc4x:mock:10.10.10.1/1/1")
+                    .to("mock:result");
             }
         };
     }
diff --git a/integrations/apache-camel/src/test/java/org/apache/plc4x/camel/PLC4XEndpointTest.java b/integrations/apache-camel/src/test/java/org/apache/plc4x/camel/PLC4XEndpointTest.java
new file mode 100644
index 0000000..e28dcd8
--- /dev/null
+++ b/integrations/apache-camel/src/test/java/org/apache/plc4x/camel/PLC4XEndpointTest.java
@@ -0,0 +1,34 @@
+package org.apache.plc4x.camel;
+
+import org.apache.camel.Component;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.mockito.Mockito.mock;
+
+class PLC4XEndpointTest {
+
+    PLC4XEndpoint SUT;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        SUT = new PLC4XEndpoint("plc4x:mock:10.10.10.1/1/1", mock(Component.class));
+    }
+
+    @Test
+    void createProducer() throws Exception {
+        Assertions.assertNotNull(SUT.createProducer());
+    }
+
+    @Test
+    void createConsumer() throws Exception {
+        Assertions.assertThrows(UnsupportedOperationException.class, () -> SUT.createConsumer(null));
+    }
+
+    @Test
+    void isSingleton() throws Exception {
+        Assertions.assertTrue(SUT.isSingleton());
+    }
+
+}
\ No newline at end of file
diff --git a/integrations/apache-camel/src/test/java/org/apache/plc4x/camel/PLC4XProducerTest.java b/integrations/apache-camel/src/test/java/org/apache/plc4x/camel/PLC4XProducerTest.java
new file mode 100644
index 0000000..3ed5b27
--- /dev/null
+++ b/integrations/apache-camel/src/test/java/org/apache/plc4x/camel/PLC4XProducerTest.java
@@ -0,0 +1,64 @@
+package org.apache.plc4x.camel;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.plc4x.java.PlcDriverManager;
+import org.apache.plc4x.java.api.connection.PlcWriter;
+import org.apache.plc4x.java.api.model.Address;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Optional;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+import static org.mockito.Mockito.*;
+
+class PLC4XProducerTest {
+
+    private PLC4XProducer SUT;
+
+    private Exchange testExchange;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        PLC4XEndpoint endpointMock = mock(PLC4XEndpoint.class, RETURNS_DEEP_STUBS);
+        when(endpointMock.getEndpointUri()).thenReturn("plc4x:mock:10.10.10.1/1/1");
+        PlcDriverManager plcDriverManagerMock = mock(PlcDriverManager.class, RETURNS_DEEP_STUBS);
+        when(plcDriverManagerMock.getConnection(anyString()).getWriter())
+            .thenReturn(Optional.of(mock(PlcWriter.class, RETURNS_DEEP_STUBS)));
+        when(endpointMock.getPlcDriverManager()).thenReturn(plcDriverManagerMock);
+        SUT = new PLC4XProducer(endpointMock);
+        testExchange = mock(Exchange.class, RETURNS_DEEP_STUBS);
+        when(testExchange.getIn().getHeader(eq(Constants.ADDRESS_HEADER), eq(Address.class)))
+            .thenReturn(mock(Address.class));
+    }
+
+    @Test
+    void process() throws Exception {
+        when(testExchange.getPattern()).thenReturn(ExchangePattern.InOnly);
+        SUT.process(testExchange);
+        when(testExchange.getPattern()).thenReturn(ExchangePattern.InOut);
+        SUT.process(testExchange);
+        when(testExchange.getPattern()).thenReturn(ExchangePattern.OutOnly);
+        SUT.process(testExchange);
+    }
+
+    @Test
+    void process_Async() throws Exception {
+        SUT.process(testExchange, doneSync -> { });
+        when(testExchange.getPattern()).thenReturn(ExchangePattern.InOnly);
+        SUT.process(testExchange, doneSync -> { });
+        when(testExchange.getPattern()).thenReturn(ExchangePattern.InOut);
+        SUT.process(testExchange, doneSync -> { });
+        when(testExchange.getPattern()).thenReturn(ExchangePattern.OutOnly);
+        SUT.process(testExchange, doneSync -> { });
+    }
+
+    @Test
+    void doStop() throws Exception {
+        SUT.doStop();
+    }
+
+}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
['"commits@plc4x.apache.org" <co...@plc4x.apache.org>'].