You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2018/11/01 20:18:57 UTC

[incubator-plc4x] branch add-simple-mock-driver updated (fecb42a -> fdb2d29)

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

jfeinauer pushed a change to branch add-simple-mock-driver
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git.


    from fecb42a  [plc4j-pool] added PoolKeyFactory to produce different keys for different protocols.
     add 0d7c25d  - Cleaning up in the examples - Added an mqtt example (untested)
     add b978413  - Made the MqttConnector actually work with a dummy public server using the test-driver.
     add 2ce3b2f  - Get rid of an annoying sonar report ...
     add 5de43ac  - Fixing sonar issues
     add 2c1a2fc  - Fixing sonar issues
     add 1122287  - Fixing sonar issues
     add 19d445f  - Fixing sonar issues
     add 6fc11d9  [driver-bases] re-enabled unsubscription support on SingleItemToSingleRequestProtocol
     add f257a6d  [driver-bases] SingleItemToSingleRequestProtocol: added SplitConfig for granular config of item splitting.
     add 4e440a9  [plc4j-pool] use native KeyedObjectPool and ditch custom implementation Custom keys per protocol are still needed
     add b41d3fb  - Fixing sonar issues
     add e2a42a9  - Fixing sonar issues
     add 98fae0a  [plc4j-ads] added encoder/decoder round trip tests (disabled)
     add ac811be  - Fixing sonar issues
     add 4f88afe  - Managing some transitive dependency versions to avoid CVEs reported starting this afternoon.
     add 6c3e50c  - Added "*Test(s)" to the test-suite
     add 6cf60a0  - Fixing bugs - Increasing test coverage
     add a37d40c  - Removed obsolete classes (S7TypeDecoder and S7TypeEncoder) - Added tests for S7RequestSizeCalculator and S7RequestSizeCalculator
     add 6f0c499  - Added more tests
     add 624b9d0  - Fixed a broken refactoring ... (Don't know why this slipped last time)
     add f0123ef  - Added some Integration-Tests to the S7 driver that actually test the full stack.
     add c897bc6  [OPM] Improved Exception handling, when field could not be fetched.
     add b259084  [OPM] Added MWE to HelloPlx4X Module.
     add a9618eb  [OPM] Several Sonar fixes and small refactorings.
     add 1f2d4d8  Minor fix in the HelloOpm.java example.
     add 1972638  - Added more tests - Fixed the exception handling in the S7Protocol
     add 609bda7  Merge branch 'master' of https://gitbox.apache.org/repos/asf/incubator-plc4x
     add 7fd2259  Throw exception when retrieving non-existing Field in response.
     add f452493  [plc4j-ads] fixed encoder and decoder and enabled round-trip test
     add 8f687de  Merge branch 'master' of https://gitbox.apache.org/repos/asf/incubator-plc4x
     add d541b3b  [plc4j-ads] re-enable test
     add 04f3e47  Moved the Hello OPM example into a dedicated module.
     add 3c1510b  [OPM] Improved Error message.
     add 4f34630  [plc4j-s7] switch to switch on field handler
     add 77ab269  [plc4j-ads] added AdsPlcFieldHandlerTest and fixed some bugs while at it
     add be27e62  [plc4j-ads] fixed small problems with time api and added a todo
     add fa2688a  [plc4j-pool] added PoolKeyFactory to produce different keys for different protocols.
     new c4de71d  Add Simple Mock Driver.
     new 88a669a  Refactoring, fixed some stuff.
     new f92d4ef  First draft.
     new e872936  Added Example for Mock Driver.
     new fdb2d29  Merge remote-tracking branch 'gitlab/add-simple-mock-driver' into add-simple-mock-driver

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:


[incubator-plc4x] 04/05: Added Example for Mock Driver.

Posted by jf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jfeinauer pushed a commit to branch add-simple-mock-driver
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit e872936bac8390272a7e50d5b15555e32264e944
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Thu Nov 1 21:14:58 2018 +0100

    Added Example for Mock Driver.
---
 .../org/apache/plc4x/java/mock/PlcMockDriver.java  |  2 +-
 .../apache/plc4x/java/mock/PlcMockDriverTest.java  | 50 +++++++++++++++++++++-
 2 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/PlcMockDriver.java b/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/PlcMockDriver.java
index 3a38a86..1b293ae 100644
--- a/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/PlcMockDriver.java
+++ b/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/PlcMockDriver.java
@@ -28,7 +28,7 @@ import java.util.WeakHashMap;
 
 /**
  * Mocking Driver that keeps a Map of references to Connections so that you can fetch a reference to a connection
- * which will be acquired by someone else (via the string).
+ * which will be acquired by someone else (via the connection string).
  * This allows for efficient Mocking.
  */
 public class PlcMockDriver implements PlcDriver {
diff --git a/plc4j/protocols/test/src/test/java/org/apache/plc4x/java/mock/PlcMockDriverTest.java b/plc4j/protocols/test/src/test/java/org/apache/plc4x/java/mock/PlcMockDriverTest.java
index b2cae5e..1da4853 100644
--- a/plc4j/protocols/test/src/test/java/org/apache/plc4x/java/mock/PlcMockDriverTest.java
+++ b/plc4j/protocols/test/src/test/java/org/apache/plc4x/java/mock/PlcMockDriverTest.java
@@ -19,11 +19,23 @@
 
 package org.apache.plc4x.java.mock;
 
+import org.apache.commons.lang3.tuple.Pair;
 import org.apache.plc4x.java.api.PlcConnection;
 import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.apache.plc4x.java.api.messages.PlcReadResponse;
+import org.apache.plc4x.java.api.types.PlcResponseCode;
+import org.apache.plc4x.java.base.messages.items.DefaultIntegerFieldItem;
+import org.apache.plc4x.java.base.messages.items.DefaultLongFieldItem;
 import org.junit.Test;
+import org.mockito.Mockito;
 
-import static org.junit.Assert.*;
+import java.util.concurrent.ExecutionException;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 
 public class PlcMockDriverTest {
@@ -38,8 +50,42 @@ public class PlcMockDriverTest {
         assertEquals(conn1, conn2);
     }
 
+    /**
+     * Example of Mock Usage.
+     * The idea is to fetch a mock connection with a specific name and prepare a {@link MockDevice} which is set there.
+     *
+     * Some application code which uses the same Connection String will then automatically get the same connection
+     * and operate against the same {@link MockDevice} without the necessity to also mock field queries or other things.
+     *
+     * In this example the {@link #someCodeWhichShouldDoPlcManipulation(String)} function represents the Business Logic
+     * which should be tested and where only the connection string is manipulated for the test.
+     */
     @Test
-    public void exampleUsage() {
+    public void testScenarioExample() throws PlcConnectionException, ExecutionException, InterruptedException {
+        PlcMockConnection preparingConnection = ((PlcMockConnection) driver.connect("test:123"));
+        MockDevice mock = Mockito.mock(MockDevice.class);
+        when(mock.read(any())).thenReturn(Pair.of(PlcResponseCode.OK, new DefaultIntegerFieldItem(1)));
+        preparingConnection.setDevice(mock);
+
+        // Now we can simply inject this URL into our code and automatically have our mock
+        someCodeWhichShouldDoPlcManipulation("test:123");
+
+        // Verify that the code did indeed what we wanted it to do
+        verify(mock, times(1)).read("DB2.DBD17:INT");
+    }
 
+    /**
+     * Example function that does some reading from a siemens plc using Siemens Syntax
+     * @param connection Connection String, e.g., from config
+     */
+    private void someCodeWhichShouldDoPlcManipulation(String connection) throws PlcConnectionException, ExecutionException, InterruptedException {
+        // Normally this would be from the driver manager
+        PlcConnection connect = driver.connect(connection);
+        PlcReadResponse response = connect.readRequestBuilder()
+            .addItem("value", "DB2.DBD17:INT")
+            .build()
+            .execute()
+            .get();
+        // Normally do something with the response
     }
 }
\ No newline at end of file


[incubator-plc4x] 05/05: Merge remote-tracking branch 'gitlab/add-simple-mock-driver' into add-simple-mock-driver

Posted by jf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jfeinauer pushed a commit to branch add-simple-mock-driver
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit fdb2d2937f49cd88911d1fc84d13089efd2eaa77
Merge: e872936 fecb42a
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Thu Nov 1 21:18:50 2018 +0100

    Merge remote-tracking branch 'gitlab/add-simple-mock-driver' into add-simple-mock-driver



[incubator-plc4x] 03/05: First draft.

Posted by jf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jfeinauer pushed a commit to branch add-simple-mock-driver
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit f92d4ef3dcb21905ab08b2b5cc31d698ab907cc7
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Thu Nov 1 21:06:31 2018 +0100

    First draft.
---
 .../org/apache/plc4x/java/mock/MockReader.java     | 51 ----------------------
 .../org/apache/plc4x/java/mock/MockDevice.java     |  3 ++
 .../java/org/apache/plc4x/java/mock/MockField.java |  0
 .../apache/plc4x/java/mock/MockFieldHandler.java   |  0
 .../apache/plc4x/java/mock/PlcMockConnection.java  | 30 ++++++++++---
 .../org/apache/plc4x/java/mock/PlcMockDriver.java  | 22 ++++++++--
 .../apache/plc4x/java/mock/PlcMockDriverTest.java} | 27 ++++++++----
 7 files changed, 62 insertions(+), 71 deletions(-)

diff --git a/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockReader.java b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockReader.java
deleted file mode 100644
index 10febee..0000000
--- a/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockReader.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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.mock;
-
-import org.apache.commons.lang3.tuple.Pair;
-import org.apache.plc4x.java.api.messages.PlcReadRequest;
-import org.apache.plc4x.java.api.messages.PlcReadResponse;
-import org.apache.plc4x.java.api.types.PlcResponseCode;
-import org.apache.plc4x.java.base.messages.DefaultPlcReadRequest;
-import org.apache.plc4x.java.base.messages.DefaultPlcReadResponse;
-import org.apache.plc4x.java.base.messages.PlcReader;
-import org.apache.plc4x.java.base.messages.items.BaseDefaultFieldItem;
-
-import java.util.Map;
-import java.util.concurrent.CompletableFuture;
-import java.util.function.Function;
-import java.util.stream.Collectors;
-
-public class MockReader implements PlcReader {
-
-    private final MockDevice device;
-
-    public MockReader(MockDevice device) {
-        this.device = device;
-    }
-
-    @Override
-    public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
-        Map<String, Pair<PlcResponseCode, BaseDefaultFieldItem>> response = readRequest.getFieldNames().stream()
-            .collect(Collectors.toMap(Function.identity(), name -> device.read(((MockField) readRequest.getField(name)).getFieldQuery())));
-        return CompletableFuture.completedFuture(new DefaultPlcReadResponse((DefaultPlcReadRequest)readRequest, response));
-    }
-
-}
diff --git a/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockDevice.java b/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/MockDevice.java
similarity index 92%
rename from plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockDevice.java
rename to plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/MockDevice.java
index def0589..b0e1075 100644
--- a/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockDevice.java
+++ b/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/MockDevice.java
@@ -31,4 +31,7 @@ public interface MockDevice {
 
     Pair<PlcResponseCode, BaseDefaultFieldItem> read(String fieldQuery);
 
+    // TODO Implement this
+    // void write(String fieldQuery, BaseDefaultFieldItem value);
+
 }
diff --git a/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockField.java b/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/MockField.java
similarity index 100%
copy from plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockField.java
copy to plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/MockField.java
diff --git a/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockFieldHandler.java b/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/MockFieldHandler.java
similarity index 100%
rename from plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockFieldHandler.java
rename to plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/MockFieldHandler.java
diff --git a/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/PlcMockConnection.java b/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/PlcMockConnection.java
similarity index 70%
rename from plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/PlcMockConnection.java
rename to plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/PlcMockConnection.java
index 9e834ec..277f69f 100644
--- a/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/PlcMockConnection.java
+++ b/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/PlcMockConnection.java
@@ -18,25 +18,34 @@ under the License.
 */
 package org.apache.plc4x.java.mock;
 
+import org.apache.commons.lang3.tuple.Pair;
 import org.apache.plc4x.java.api.PlcConnection;
 import org.apache.plc4x.java.api.authentication.PlcAuthentication;
 import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
 import org.apache.plc4x.java.api.exceptions.PlcUnsupportedOperationException;
-import org.apache.plc4x.java.api.messages.PlcReadRequest;
-import org.apache.plc4x.java.api.messages.PlcSubscriptionRequest;
-import org.apache.plc4x.java.api.messages.PlcUnsubscriptionRequest;
-import org.apache.plc4x.java.api.messages.PlcWriteRequest;
+import org.apache.plc4x.java.api.messages.*;
 import org.apache.plc4x.java.api.metadata.PlcConnectionMetadata;
+import org.apache.plc4x.java.api.types.PlcResponseCode;
 import org.apache.plc4x.java.base.messages.DefaultPlcReadRequest;
+import org.apache.plc4x.java.base.messages.DefaultPlcReadResponse;
+import org.apache.plc4x.java.base.messages.PlcReader;
+import org.apache.plc4x.java.base.messages.items.BaseDefaultFieldItem;
 
-public class PlcMockConnection implements PlcConnection {
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
+public class PlcMockConnection implements PlcConnection, PlcReader {
+
+    private final String name;
     private final PlcAuthentication authentication;
 
     private boolean isConnected = false;
     private MockDevice device;
 
-    PlcMockConnection(PlcAuthentication authentication) {
+    PlcMockConnection(String name, PlcAuthentication authentication) {
+        this.name = name;
         this.authentication = authentication;
     }
 
@@ -87,7 +96,14 @@ public class PlcMockConnection implements PlcConnection {
 
     @Override
     public PlcReadRequest.Builder readRequestBuilder() {
-        return new DefaultPlcReadRequest.Builder(new MockReader(device), new MockFieldHandler());
+        return new DefaultPlcReadRequest.Builder(this, new MockFieldHandler());
+    }
+
+    @Override
+    public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
+        Map<String, Pair<PlcResponseCode, BaseDefaultFieldItem>> response = readRequest.getFieldNames().stream()
+            .collect(Collectors.toMap(Function.identity(), name -> device.read(((MockField) readRequest.getField(name)).getFieldQuery())));
+        return CompletableFuture.completedFuture(new DefaultPlcReadResponse((DefaultPlcReadRequest)readRequest, response));
     }
 
     @Override
diff --git a/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/PlcMockDriver.java b/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/PlcMockDriver.java
similarity index 63%
rename from plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/PlcMockDriver.java
rename to plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/PlcMockDriver.java
index c693b84..3a38a86 100644
--- a/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/PlcMockDriver.java
+++ b/plc4j/protocols/test/src/main/java/org/apache/plc4x/java/mock/PlcMockDriver.java
@@ -23,8 +23,18 @@ import org.apache.plc4x.java.api.authentication.PlcAuthentication;
 import org.apache.plc4x.java.api.PlcConnection;
 import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
 
+import java.util.Map;
+import java.util.WeakHashMap;
+
+/**
+ * Mocking Driver that keeps a Map of references to Connections so that you can fetch a reference to a connection
+ * which will be acquired by someone else (via the string).
+ * This allows for efficient Mocking.
+ */
 public class PlcMockDriver implements PlcDriver {
 
+    private Map<String, PlcConnection> connectionMap = new WeakHashMap<>();
+
     @Override
     public String getProtocolCode() {
         return "mock";
@@ -36,13 +46,17 @@ public class PlcMockDriver implements PlcDriver {
     }
 
     @Override
-    public PlcConnection connect(String url) {
-        return new PlcMockConnection(null);
+    public PlcConnection connect(String url) throws PlcConnectionException {
+        return connect(url, null);
     }
 
     @Override
-    public PlcConnection connect(String url, PlcAuthentication authentication) {
-        return new PlcMockConnection(authentication);
+    public PlcConnection connect(String url, PlcAuthentication authentication) throws PlcConnectionException {
+        String deviceName = url.substring(5);
+        if (deviceName.isEmpty()) {
+            throw new PlcConnectionException("Invalid URL: no device name given.");
+        }
+        return connectionMap.computeIfAbsent(deviceName, name -> new PlcMockConnection(name, authentication));
     }
 
 }
diff --git a/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockField.java b/plc4j/protocols/test/src/test/java/org/apache/plc4x/java/mock/PlcMockDriverTest.java
similarity index 60%
rename from plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockField.java
rename to plc4j/protocols/test/src/test/java/org/apache/plc4x/java/mock/PlcMockDriverTest.java
index e570b2b..b2cae5e 100644
--- a/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockField.java
+++ b/plc4j/protocols/test/src/test/java/org/apache/plc4x/java/mock/PlcMockDriverTest.java
@@ -19,18 +19,27 @@
 
 package org.apache.plc4x.java.mock;
 
-import org.apache.plc4x.java.api.model.PlcField;
+import org.apache.plc4x.java.api.PlcConnection;
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.junit.Test;
 
-public class MockField implements PlcField {
+import static org.junit.Assert.*;
 
-    private final String fieldQuery;
 
-    public MockField(String fieldQuery) {
-        this.fieldQuery = fieldQuery;
-    }
+public class PlcMockDriverTest {
+
+    private final PlcMockDriver driver = new PlcMockDriver();
+
+    @Test
+    public void fetchTwoConnections_areEqual() throws PlcConnectionException {
+        PlcConnection conn1 = driver.connect("mock:123");
+        PlcConnection conn2 = driver.connect("mock:123");
 
-    public String getFieldQuery() {
-        return fieldQuery;
+        assertEquals(conn1, conn2);
     }
 
-}
+    @Test
+    public void exampleUsage() {
+
+    }
+}
\ No newline at end of file


[incubator-plc4x] 01/05: Add Simple Mock Driver.

Posted by jf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jfeinauer pushed a commit to branch add-simple-mock-driver
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit c4de71da42c09d339f4abc294b36f68610337321
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Wed Oct 31 22:05:40 2018 +0100

    Add Simple Mock Driver.
---
 .../org/apache/plc4x/java/mock/MockConnection.java | 85 ++++++++++++++++++----
 .../org/apache/plc4x/java/mock/MockDevice.java     | 34 +++++++++
 .../org/apache/plc4x/java/mock/MockDriverTest.java | 73 +++++++++++++++++++
 .../java/org/apache/plc4x/java/mock/MockField.java | 36 +++++++++
 .../apache/plc4x/java/mock/MockFieldHandler.java   | 33 +++++++++
 .../org/apache/plc4x/java/mock/MockReader.java     | 51 +++++++++++++
 .../base/connection/DefaultPlcFieldHandler.java    |  4 +
 .../java/base/connection/PlcFieldHandler.java      |  3 +
 .../java/mock/connection/PlcMockConnection.java    | 26 +++++++
 .../plc4x/java/mock/connection/PlcMockDriver.java  | 50 +++++++++++++
 10 files changed, 382 insertions(+), 13 deletions(-)

diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockConnection.java b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockConnection.java
index d44fc5d..aaa077d 100644
--- a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockConnection.java
+++ b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockConnection.java
@@ -18,35 +18,94 @@ under the License.
 */
 package org.apache.plc4x.java.mock;
 
-import io.netty.channel.Channel;
-import io.netty.channel.ChannelHandler;
-import io.netty.channel.ChannelInitializer;
+import org.apache.plc4x.java.api.PlcConnection;
 import org.apache.plc4x.java.api.authentication.PlcAuthentication;
-import org.apache.plc4x.java.base.connection.AbstractPlcConnection;
-import org.apache.plc4x.java.base.connection.TestChannelFactory;
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.apache.plc4x.java.api.exceptions.PlcUnsupportedOperationException;
+import org.apache.plc4x.java.api.messages.PlcReadRequest;
+import org.apache.plc4x.java.api.messages.PlcSubscriptionRequest;
+import org.apache.plc4x.java.api.messages.PlcUnsubscriptionRequest;
+import org.apache.plc4x.java.api.messages.PlcWriteRequest;
+import org.apache.plc4x.java.api.metadata.PlcConnectionMetadata;
+import org.apache.plc4x.java.base.messages.DefaultPlcReadRequest;
 
-import java.util.concurrent.CompletableFuture;
-
-public class MockConnection extends AbstractPlcConnection {
+public class MockConnection implements PlcConnection {
 
     private final PlcAuthentication authentication;
 
+    private boolean isConnected = false;
+    private MockDevice device;
+
     MockConnection(PlcAuthentication authentication) {
-        super(new TestChannelFactory());
         this.authentication = authentication;
     }
 
+    public MockDevice getDevice() {
+        return device;
+    }
+
+    public void setDevice(MockDevice device) {
+        this.device = device;
+    }
+
+    @Override
+    public void connect() {
+        // do nothing
+    }
+
     @Override
-    protected ChannelHandler getChannelHandler(CompletableFuture<Void> sessionSetupCompleteFuture) {
-        return new ChannelInitializer() {
+    public boolean isConnected() {
+        // is connected if a device is set
+        return device != null;
+    }
+
+    @Override
+    public void close() {
+        // unset device
+        this.device = null;
+    }
+
+    @Override
+    public PlcConnectionMetadata getMetadata() {
+        return new PlcConnectionMetadata() {
+            @Override
+            public boolean canRead() {
+                return true;
+            }
+
             @Override
-            protected void initChannel(Channel channel) {
+            public boolean canWrite() {
+                return false;
+            }
+
+            @Override
+            public boolean canSubscribe() {
+                return false;
             }
         };
     }
 
+    @Override
+    public PlcReadRequest.Builder readRequestBuilder() {
+        return new DefaultPlcReadRequest.Builder(new MockReader(device), new MockFieldHandler());
+    }
+
+    @Override
+    public PlcWriteRequest.Builder writeRequestBuilder() {
+        throw new PlcUnsupportedOperationException("Write not supported by Mock Driver");
+    }
+
+    @Override
+    public PlcSubscriptionRequest.Builder subscriptionRequestBuilder() {
+        throw new PlcUnsupportedOperationException("Subscription not supported by Mock Driver");
+    }
+
+    @Override
+    public PlcUnsubscriptionRequest.Builder unsubscriptionRequestBuilder() {
+        throw new PlcUnsupportedOperationException("Subscription not supported by Mock Driver");
+    }
+
     public PlcAuthentication getAuthentication() {
         return authentication;
     }
-
 }
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDevice.java b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDevice.java
new file mode 100644
index 0000000..ed32c3f
--- /dev/null
+++ b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDevice.java
@@ -0,0 +1,34 @@
+/*
+ * 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.mock;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.plc4x.java.api.model.PlcField;
+import org.apache.plc4x.java.api.types.PlcResponseCode;
+import org.apache.plc4x.java.base.messages.items.BaseDefaultFieldItem;
+
+/**
+ * Mock Object to do assertions on.
+ */
+public interface MockDevice {
+
+    Pair<PlcResponseCode, BaseDefaultFieldItem> read(PlcField field);
+
+}
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDriverTest.java b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDriverTest.java
new file mode 100644
index 0000000..05bddcf
--- /dev/null
+++ b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDriverTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.mock;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.plc4x.java.PlcDriverManager;
+import org.apache.plc4x.java.api.PlcConnection;
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.apache.plc4x.java.api.types.PlcResponseCode;
+import org.apache.plc4x.java.base.messages.items.DefaultLongFieldItem;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import static junit.framework.TestCase.assertFalse;
+import static junit.framework.TestCase.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class MockDriverTest {
+
+    private final PlcDriverManager driverManager = new PlcDriverManager();
+
+    @Test
+    public void useMockDriver_noDevice_isNotConnected() throws Exception {
+        PlcConnection connection = driverManager.getConnection("mock:dummy");
+
+        assertFalse(connection.isConnected());
+    }
+
+    @Test
+    public void useMockDriver_deviceSet_isConnected() throws Exception {
+        MockConnection connection = (MockConnection) driverManager.getConnection("mock:dummy");
+        MockDevice mock = Mockito.mock(MockDevice.class);
+        connection.setDevice(mock);
+
+        assertTrue(connection.isConnected());
+    }
+
+    @Test
+    public void mockDriver_assertSimpleRequest() throws PlcConnectionException {
+        MockConnection connection = (MockConnection) driverManager.getConnection("mock:dummy");
+        MockDevice mock = Mockito.mock(MockDevice.class);
+        when(mock.read(any())).thenReturn(Pair.of(PlcResponseCode.OK, new DefaultLongFieldItem(1L)));
+        connection.setDevice(mock);
+
+        connection.readRequestBuilder()
+            .addItem("item1", "myPlcField")
+            .build()
+            .execute();
+
+        // Verify the call
+        verify(mock, times(1)).read(any());
+    }
+}
\ No newline at end of file
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockField.java b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockField.java
new file mode 100644
index 0000000..2762407
--- /dev/null
+++ b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockField.java
@@ -0,0 +1,36 @@
+/*
+ * 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.mock;
+
+import org.apache.plc4x.java.api.model.PlcField;
+
+public class MockField implements PlcField {
+
+    private final String fieldQuery;
+
+    public MockField(String fieldQuery) {
+
+        this.fieldQuery = fieldQuery;
+    }
+
+    public String getFieldQuery() {
+        return fieldQuery;
+    }
+}
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockFieldHandler.java b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockFieldHandler.java
new file mode 100644
index 0000000..a8893e2
--- /dev/null
+++ b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockFieldHandler.java
@@ -0,0 +1,33 @@
+/*
+ * 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.mock;
+
+import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
+import org.apache.plc4x.java.api.model.PlcField;
+import org.apache.plc4x.java.base.connection.DefaultPlcFieldHandler;
+
+public class MockFieldHandler extends DefaultPlcFieldHandler {
+
+    @Override
+    public PlcField createField(String fieldQuery) throws PlcInvalidFieldException {
+        return new MockField(fieldQuery);
+    }
+
+}
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockReader.java b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockReader.java
new file mode 100644
index 0000000..79e887f
--- /dev/null
+++ b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockReader.java
@@ -0,0 +1,51 @@
+/*
+ * 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.mock;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.plc4x.java.api.messages.PlcReadRequest;
+import org.apache.plc4x.java.api.messages.PlcReadResponse;
+import org.apache.plc4x.java.api.types.PlcResponseCode;
+import org.apache.plc4x.java.base.messages.DefaultPlcReadRequest;
+import org.apache.plc4x.java.base.messages.DefaultPlcReadResponse;
+import org.apache.plc4x.java.base.messages.PlcReader;
+import org.apache.plc4x.java.base.messages.items.BaseDefaultFieldItem;
+
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+public class MockReader implements PlcReader {
+
+    private final MockDevice device;
+
+    public MockReader(MockDevice device) {
+        this.device = device;
+    }
+
+    @Override
+    public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
+        Map<String, Pair<PlcResponseCode, BaseDefaultFieldItem>> response = readRequest.getFieldNames().stream()
+            .collect(Collectors.toMap(Function.identity(), name -> device.read(readRequest.getField(name))));
+        return CompletableFuture.completedFuture(new DefaultPlcReadResponse((DefaultPlcReadRequest)readRequest, response));
+    }
+
+}
diff --git a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/DefaultPlcFieldHandler.java b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/DefaultPlcFieldHandler.java
index e74be1a..6b90bab 100644
--- a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/DefaultPlcFieldHandler.java
+++ b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/DefaultPlcFieldHandler.java
@@ -23,6 +23,10 @@ import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
 import org.apache.plc4x.java.api.model.PlcField;
 import org.apache.plc4x.java.base.messages.items.BaseDefaultFieldItem;
 
+/**
+ * Base Implementation of {@link PlcFieldHandler} which throws a {@link PlcRuntimeException} for all
+ * encodeXXX methods.
+ */
 public abstract class DefaultPlcFieldHandler implements PlcFieldHandler {
 
     @Override
diff --git a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/PlcFieldHandler.java b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/PlcFieldHandler.java
index cddf0c7..146e685 100644
--- a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/PlcFieldHandler.java
+++ b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/base/connection/PlcFieldHandler.java
@@ -22,6 +22,9 @@ import org.apache.plc4x.java.api.exceptions.PlcInvalidFieldException;
 import org.apache.plc4x.java.api.model.PlcField;
 import org.apache.plc4x.java.base.messages.items.BaseDefaultFieldItem;
 
+/**
+ * Field Handler which handles the parsing of string to {@link PlcField} and the encoding of retrieved plc values.
+ */
 public interface PlcFieldHandler {
 
     PlcField createField(String fieldQuery) throws PlcInvalidFieldException;
diff --git a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/mock/connection/PlcMockConnection.java b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/mock/connection/PlcMockConnection.java
new file mode 100644
index 0000000..dbae61f
--- /dev/null
+++ b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/mock/connection/PlcMockConnection.java
@@ -0,0 +1,26 @@
+/*
+ * 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.mock.connection;
+
+/**
+ * Connection which is used for mocking.
+ */
+public class PlcMockConnection {
+}
diff --git a/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/mock/connection/PlcMockDriver.java b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/mock/connection/PlcMockDriver.java
new file mode 100644
index 0000000..5effc7e
--- /dev/null
+++ b/plc4j/protocols/driver-bases/base/src/main/java/org/apache/plc4x/java/mock/connection/PlcMockDriver.java
@@ -0,0 +1,50 @@
+/*
+ * 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.mock.connection;
+
+import org.apache.plc4x.java.api.PlcConnection;
+import org.apache.plc4x.java.api.authentication.PlcAuthentication;
+import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
+import org.apache.plc4x.java.spi.PlcDriver;
+
+/**
+ * Driver which can be used for mocking.
+ */
+public class PlcMockDriver implements PlcDriver {
+    @Override
+    public String getProtocolCode() {
+        return "mock";
+    }
+
+    @Override
+    public String getProtocolName() {
+        return null;
+    }
+
+    @Override
+    public PlcConnection connect(String url) {
+        return null;
+    }
+
+    @Override
+    public PlcConnection connect(String url, PlcAuthentication authentication) {
+        return null;
+    }
+}


[incubator-plc4x] 02/05: Refactoring, fixed some stuff.

Posted by jf...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jfeinauer pushed a commit to branch add-simple-mock-driver
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit 88a669a8680e0736d023821c40eee613b9d169c6
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Wed Oct 31 22:28:39 2018 +0100

    Refactoring, fixed some stuff.
---
 plc4j/core/pom.xml                                 |  5 ++
 .../apache/plc4x/java/PlcDriverManagerTest.java    |  6 +-
 .../apache/plc4x/java/mock/DoubleMockDriver.java   | 44 +++++------
 .../org/apache/plc4x/java/mock/MockConnection.java | 85 ++++------------------
 .../org/apache/plc4x/java/mock/MockDriver.java     | 10 +--
 ...{MockDriverTest.java => PlcMockDriverTest.java} |  9 ++-
 .../org/apache/plc4x/java/mock/MockDevice.java     |  2 +-
 .../java/org/apache/plc4x/java/mock/MockField.java |  2 +-
 .../apache/plc4x/java/mock/MockFieldHandler.java   |  0
 .../org/apache/plc4x/java/mock/MockReader.java     |  2 +-
 .../apache/plc4x/java/mock/PlcMockConnection.java} |  4 +-
 .../org/apache/plc4x/java/mock/PlcMockDriver.java} | 10 +--
 .../services/org.apache.plc4x.java.spi.PlcDriver   | 19 +++++
 13 files changed, 82 insertions(+), 116 deletions(-)

diff --git a/plc4j/core/pom.xml b/plc4j/core/pom.xml
index beb9f82..7d1093d 100644
--- a/plc4j/core/pom.xml
+++ b/plc4j/core/pom.xml
@@ -61,6 +61,11 @@
       <artifactId>netty-transport</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>
\ No newline at end of file
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/PlcDriverManagerTest.java b/plc4j/core/src/test/java/org/apache/plc4x/java/PlcDriverManagerTest.java
index c02e2f9..f5f16d1 100644
--- a/plc4j/core/src/test/java/org/apache/plc4x/java/PlcDriverManagerTest.java
+++ b/plc4j/core/src/test/java/org/apache/plc4x/java/PlcDriverManagerTest.java
@@ -47,7 +47,7 @@ public class PlcDriverManagerTest {
     @Test
     @Category(FastTests.class)
     public void getExistingDriverTest() throws PlcException {
-        MockConnection mockConnection = (MockConnection) new PlcDriverManager().getConnection("mock://some-cool-url");
+        MockConnection mockConnection = (MockConnection) new PlcDriverManager().getConnection("spi-mock://some-cool-url");
         assertThat(mockConnection.getAuthentication(), nullValue());
         assertThat(mockConnection.isConnected(), is(true));
     }
@@ -62,7 +62,7 @@ public class PlcDriverManagerTest {
     public void getExistingDriverWithAuthenticationTest() throws PlcException {
         PlcUsernamePasswordAuthentication authentication =
             new PlcUsernamePasswordAuthentication("user", "pass");
-        MockConnection mockConnection = (MockConnection) new PlcDriverManager().getConnection("mock://some-cool-url", authentication);
+        MockConnection mockConnection = (MockConnection) new PlcDriverManager().getConnection("spi-mock://some-cool-url", authentication);
 
         assertThat(mockConnection.getAuthentication(), notNullValue());
         assertThat(mockConnection.getAuthentication(), instanceOf(PlcUsernamePasswordAuthentication.class));
@@ -110,7 +110,7 @@ public class PlcDriverManagerTest {
         ClassLoader fakeClassLoader = new URLClassLoader(urls, originalClassloader);
 
         // expect exception
-        new PlcDriverManager(fakeClassLoader).getConnection("mock://some-cool-url");
+        new PlcDriverManager(fakeClassLoader).getConnection("spi-mock://some-cool-url");
     }
 
 }
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/DoubleMockDriver.java b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/DoubleMockDriver.java
index 05250ab..0a180de 100644
--- a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/DoubleMockDriver.java
+++ b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/DoubleMockDriver.java
@@ -1,21 +1,21 @@
 /*
-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.
-*/
+ * 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.mock;
 
 import org.apache.plc4x.java.spi.PlcDriver;
@@ -27,7 +27,7 @@ public class DoubleMockDriver implements PlcDriver {
 
     @Override
     public String getProtocolCode() {
-        return "mock";
+        return "spi-mock";
     }
 
     @Override
@@ -36,13 +36,13 @@ public class DoubleMockDriver implements PlcDriver {
     }
 
     @Override
-    public PlcConnection connect(String url) throws PlcConnectionException {
-        return new MockConnection(null);
+    public PlcConnection connect(String url) {
+        return new PlcMockConnection(null);
     }
 
     @Override
-    public PlcConnection connect(String url, PlcAuthentication authentication) throws PlcConnectionException {
-        return new MockConnection(authentication);
+    public PlcConnection connect(String url, PlcAuthentication authentication) {
+        return new PlcMockConnection(authentication);
     }
 
 }
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockConnection.java b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockConnection.java
index aaa077d..d44fc5d 100644
--- a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockConnection.java
+++ b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockConnection.java
@@ -18,94 +18,35 @@ under the License.
 */
 package org.apache.plc4x.java.mock;
 
-import org.apache.plc4x.java.api.PlcConnection;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelInitializer;
 import org.apache.plc4x.java.api.authentication.PlcAuthentication;
-import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
-import org.apache.plc4x.java.api.exceptions.PlcUnsupportedOperationException;
-import org.apache.plc4x.java.api.messages.PlcReadRequest;
-import org.apache.plc4x.java.api.messages.PlcSubscriptionRequest;
-import org.apache.plc4x.java.api.messages.PlcUnsubscriptionRequest;
-import org.apache.plc4x.java.api.messages.PlcWriteRequest;
-import org.apache.plc4x.java.api.metadata.PlcConnectionMetadata;
-import org.apache.plc4x.java.base.messages.DefaultPlcReadRequest;
+import org.apache.plc4x.java.base.connection.AbstractPlcConnection;
+import org.apache.plc4x.java.base.connection.TestChannelFactory;
 
-public class MockConnection implements PlcConnection {
+import java.util.concurrent.CompletableFuture;
 
-    private final PlcAuthentication authentication;
+public class MockConnection extends AbstractPlcConnection {
 
-    private boolean isConnected = false;
-    private MockDevice device;
+    private final PlcAuthentication authentication;
 
     MockConnection(PlcAuthentication authentication) {
+        super(new TestChannelFactory());
         this.authentication = authentication;
     }
 
-    public MockDevice getDevice() {
-        return device;
-    }
-
-    public void setDevice(MockDevice device) {
-        this.device = device;
-    }
-
-    @Override
-    public void connect() {
-        // do nothing
-    }
-
     @Override
-    public boolean isConnected() {
-        // is connected if a device is set
-        return device != null;
-    }
-
-    @Override
-    public void close() {
-        // unset device
-        this.device = null;
-    }
-
-    @Override
-    public PlcConnectionMetadata getMetadata() {
-        return new PlcConnectionMetadata() {
-            @Override
-            public boolean canRead() {
-                return true;
-            }
-
+    protected ChannelHandler getChannelHandler(CompletableFuture<Void> sessionSetupCompleteFuture) {
+        return new ChannelInitializer() {
             @Override
-            public boolean canWrite() {
-                return false;
-            }
-
-            @Override
-            public boolean canSubscribe() {
-                return false;
+            protected void initChannel(Channel channel) {
             }
         };
     }
 
-    @Override
-    public PlcReadRequest.Builder readRequestBuilder() {
-        return new DefaultPlcReadRequest.Builder(new MockReader(device), new MockFieldHandler());
-    }
-
-    @Override
-    public PlcWriteRequest.Builder writeRequestBuilder() {
-        throw new PlcUnsupportedOperationException("Write not supported by Mock Driver");
-    }
-
-    @Override
-    public PlcSubscriptionRequest.Builder subscriptionRequestBuilder() {
-        throw new PlcUnsupportedOperationException("Subscription not supported by Mock Driver");
-    }
-
-    @Override
-    public PlcUnsubscriptionRequest.Builder unsubscriptionRequestBuilder() {
-        throw new PlcUnsupportedOperationException("Subscription not supported by Mock Driver");
-    }
-
     public PlcAuthentication getAuthentication() {
         return authentication;
     }
+
 }
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDriver.java b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDriver.java
index 4771550..6ebd36c 100644
--- a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDriver.java
+++ b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDriver.java
@@ -27,7 +27,7 @@ public class MockDriver implements PlcDriver {
 
     @Override
     public String getProtocolCode() {
-        return "mock";
+        return "spi-mock";
     }
 
     @Override
@@ -36,13 +36,13 @@ public class MockDriver implements PlcDriver {
     }
 
     @Override
-    public PlcConnection connect(String url) throws PlcConnectionException {
-        return new MockConnection(null);
+    public PlcConnection connect(String url) {
+        return new PlcMockConnection(null);
     }
 
     @Override
-    public PlcConnection connect(String url, PlcAuthentication authentication) throws PlcConnectionException {
-        return new MockConnection(authentication);
+    public PlcConnection connect(String url, PlcAuthentication authentication) {
+        return new PlcMockConnection(authentication);
     }
 
 }
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDriverTest.java b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/PlcMockDriverTest.java
similarity index 87%
rename from plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDriverTest.java
rename to plc4j/core/src/test/java/org/apache/plc4x/java/mock/PlcMockDriverTest.java
index 05bddcf..532e20f 100644
--- a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDriverTest.java
+++ b/plc4j/core/src/test/java/org/apache/plc4x/java/mock/PlcMockDriverTest.java
@@ -31,11 +31,12 @@ import org.mockito.Mockito;
 import static junit.framework.TestCase.assertFalse;
 import static junit.framework.TestCase.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
-public class MockDriverTest {
+public class PlcMockDriverTest {
 
     private final PlcDriverManager driverManager = new PlcDriverManager();
 
@@ -48,7 +49,7 @@ public class MockDriverTest {
 
     @Test
     public void useMockDriver_deviceSet_isConnected() throws Exception {
-        MockConnection connection = (MockConnection) driverManager.getConnection("mock:dummy");
+        PlcMockConnection connection = (PlcMockConnection) driverManager.getConnection("mock:dummy");
         MockDevice mock = Mockito.mock(MockDevice.class);
         connection.setDevice(mock);
 
@@ -57,7 +58,7 @@ public class MockDriverTest {
 
     @Test
     public void mockDriver_assertSimpleRequest() throws PlcConnectionException {
-        MockConnection connection = (MockConnection) driverManager.getConnection("mock:dummy");
+        PlcMockConnection connection = (PlcMockConnection) driverManager.getConnection("mock:dummy");
         MockDevice mock = Mockito.mock(MockDevice.class);
         when(mock.read(any())).thenReturn(Pair.of(PlcResponseCode.OK, new DefaultLongFieldItem(1L)));
         connection.setDevice(mock);
@@ -68,6 +69,6 @@ public class MockDriverTest {
             .execute();
 
         // Verify the call
-        verify(mock, times(1)).read(any());
+        verify(mock, times(1)).read(eq("myPlcField"));
     }
 }
\ No newline at end of file
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDevice.java b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockDevice.java
similarity index 94%
rename from plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDevice.java
rename to plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockDevice.java
index ed32c3f..def0589 100644
--- a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDevice.java
+++ b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockDevice.java
@@ -29,6 +29,6 @@ import org.apache.plc4x.java.base.messages.items.BaseDefaultFieldItem;
  */
 public interface MockDevice {
 
-    Pair<PlcResponseCode, BaseDefaultFieldItem> read(PlcField field);
+    Pair<PlcResponseCode, BaseDefaultFieldItem> read(String fieldQuery);
 
 }
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockField.java b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockField.java
similarity index 100%
rename from plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockField.java
rename to plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockField.java
index 2762407..e570b2b 100644
--- a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockField.java
+++ b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockField.java
@@ -26,11 +26,11 @@ public class MockField implements PlcField {
     private final String fieldQuery;
 
     public MockField(String fieldQuery) {
-
         this.fieldQuery = fieldQuery;
     }
 
     public String getFieldQuery() {
         return fieldQuery;
     }
+
 }
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockFieldHandler.java b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockFieldHandler.java
similarity index 100%
rename from plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockFieldHandler.java
rename to plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockFieldHandler.java
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockReader.java b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockReader.java
similarity index 96%
rename from plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockReader.java
rename to plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockReader.java
index 79e887f..10febee 100644
--- a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockReader.java
+++ b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/MockReader.java
@@ -44,7 +44,7 @@ public class MockReader implements PlcReader {
     @Override
     public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
         Map<String, Pair<PlcResponseCode, BaseDefaultFieldItem>> response = readRequest.getFieldNames().stream()
-            .collect(Collectors.toMap(Function.identity(), name -> device.read(readRequest.getField(name))));
+            .collect(Collectors.toMap(Function.identity(), name -> device.read(((MockField) readRequest.getField(name)).getFieldQuery())));
         return CompletableFuture.completedFuture(new DefaultPlcReadResponse((DefaultPlcReadRequest)readRequest, response));
     }
 
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockConnection.java b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/PlcMockConnection.java
similarity index 96%
copy from plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockConnection.java
copy to plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/PlcMockConnection.java
index aaa077d..9e834ec 100644
--- a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockConnection.java
+++ b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/PlcMockConnection.java
@@ -29,14 +29,14 @@ import org.apache.plc4x.java.api.messages.PlcWriteRequest;
 import org.apache.plc4x.java.api.metadata.PlcConnectionMetadata;
 import org.apache.plc4x.java.base.messages.DefaultPlcReadRequest;
 
-public class MockConnection implements PlcConnection {
+public class PlcMockConnection implements PlcConnection {
 
     private final PlcAuthentication authentication;
 
     private boolean isConnected = false;
     private MockDevice device;
 
-    MockConnection(PlcAuthentication authentication) {
+    PlcMockConnection(PlcAuthentication authentication) {
         this.authentication = authentication;
     }
 
diff --git a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDriver.java b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/PlcMockDriver.java
similarity index 83%
copy from plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDriver.java
copy to plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/PlcMockDriver.java
index 4771550..c693b84 100644
--- a/plc4j/core/src/test/java/org/apache/plc4x/java/mock/MockDriver.java
+++ b/plc4j/protocols/driver-bases/test/src/main/java/org/apache/plc4x/java/mock/PlcMockDriver.java
@@ -23,7 +23,7 @@ import org.apache.plc4x.java.api.authentication.PlcAuthentication;
 import org.apache.plc4x.java.api.PlcConnection;
 import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
 
-public class MockDriver implements PlcDriver {
+public class PlcMockDriver implements PlcDriver {
 
     @Override
     public String getProtocolCode() {
@@ -36,13 +36,13 @@ public class MockDriver implements PlcDriver {
     }
 
     @Override
-    public PlcConnection connect(String url) throws PlcConnectionException {
-        return new MockConnection(null);
+    public PlcConnection connect(String url) {
+        return new PlcMockConnection(null);
     }
 
     @Override
-    public PlcConnection connect(String url, PlcAuthentication authentication) throws PlcConnectionException {
-        return new MockConnection(authentication);
+    public PlcConnection connect(String url, PlcAuthentication authentication) {
+        return new PlcMockConnection(authentication);
     }
 
 }
diff --git a/plc4j/protocols/driver-bases/test/src/main/resources/META-INF/services/org.apache.plc4x.java.spi.PlcDriver b/plc4j/protocols/driver-bases/test/src/main/resources/META-INF/services/org.apache.plc4x.java.spi.PlcDriver
new file mode 100644
index 0000000..ebff4aa
--- /dev/null
+++ b/plc4j/protocols/driver-bases/test/src/main/resources/META-INF/services/org.apache.plc4x.java.spi.PlcDriver
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+org.apache.plc4x.java.mock.PlcMockDriver