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/10/31 21:06:36 UTC
[incubator-plc4x] 01/01: Add Simple Mock Driver.
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 3d10fbc6a1edf517f7e468e3b588754bfdc2a4ad
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;
+ }
+}