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/11/24 18:18:31 UTC

[incubator-plc4x] branch develop updated: [plc4j-driver-bases-test] added some tests.

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

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


The following commit(s) were added to refs/heads/develop by this push:
     new b7c907f  [plc4j-driver-bases-test] added some tests.
b7c907f is described below

commit b7c907f6bef786fb9d155e643195836b8fed0e20
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Sat Nov 24 19:10:30 2018 +0100

    [plc4j-driver-bases-test] added some tests.
---
 .../plc4x/java/mock/MockFieldHandlerTest.java      | 111 +++++++++++++++
 .../apache/plc4x/java/mock/MockFieldItemTest.java  |  31 ++++
 .../org/apache/plc4x/java/mock/MockFieldTest.java  |  48 +++++++
 .../plc4x/java/mock/PlcMockConnectionTest.java     | 156 +++++++++++++++++++++
 .../apache/plc4x/java/mock/PlcMockDriverTest.java  |  53 +++----
 5 files changed, 375 insertions(+), 24 deletions(-)

diff --git a/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/MockFieldHandlerTest.java b/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/MockFieldHandlerTest.java
new file mode 100644
index 0000000..fb3d786
--- /dev/null
+++ b/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/MockFieldHandlerTest.java
@@ -0,0 +1,111 @@
+/*
+ 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;
+import org.assertj.core.api.WithAssertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+class MockFieldHandlerTest implements WithAssertions {
+
+    MockFieldHandler SUT = new MockFieldHandler();
+
+    @Mock
+    PlcField plcField;
+
+    @Test
+    void createField() {
+        assertThat(SUT.createField("")).isNotNull();
+    }
+
+    @Test
+    void encodeBoolean() {
+        assertThat(SUT.encodeBoolean(plcField, new Object[0])).isNotNull();
+    }
+
+    @Test
+    void encodeByte() {
+        assertThat(SUT.encodeByte(plcField, new Object[0])).isNotNull();
+    }
+
+    @Test
+    void encodeShort() {
+        assertThat(SUT.encodeShort(plcField, new Object[0])).isNotNull();
+    }
+
+    @Test
+    void encodeInteger() {
+        assertThat(SUT.encodeInteger(plcField, new Object[0])).isNotNull();
+    }
+
+    @Test
+    void encodeBigInteger() {
+        assertThat(SUT.encodeBigInteger(plcField, new Object[0])).isNotNull();
+    }
+
+    @Test
+    void encodeLong() {
+        assertThat(SUT.encodeLong(plcField, new Object[0])).isNotNull();
+    }
+
+    @Test
+    void encodeFloat() {
+        assertThat(SUT.encodeFloat(plcField, new Object[0])).isNotNull();
+    }
+
+    @Test
+    void encodeBigDecimal() {
+        assertThat(SUT.encodeBigDecimal(plcField, new Object[0])).isNotNull();
+    }
+
+    @Test
+    void encodeDouble() {
+        assertThat(SUT.encodeDouble(plcField, new Object[0])).isNotNull();
+    }
+
+    @Test
+    void encodeString() {
+        assertThat(SUT.encodeString(plcField, new Object[0])).isNotNull();
+    }
+
+    @Test
+    void encodeTime() {
+        assertThat(SUT.encodeTime(plcField, new Object[0])).isNotNull();
+    }
+
+    @Test
+    void encodeDate() {
+        assertThat(SUT.encodeDate(plcField, new Object[0])).isNotNull();
+    }
+
+    @Test
+    void encodeDateTime() {
+        assertThat(SUT.encodeDateTime(plcField, new Object[0])).isNotNull();
+    }
+
+    @Test
+    void encodeByteArray() {
+        assertThat(SUT.encodeByteArray(plcField, new Object[0])).isNotNull();
+    }
+}
\ No newline at end of file
diff --git a/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/MockFieldItemTest.java b/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/MockFieldItemTest.java
new file mode 100644
index 0000000..066f87c
--- /dev/null
+++ b/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/MockFieldItemTest.java
@@ -0,0 +1,31 @@
+/*
+ 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.assertj.core.api.WithAssertions;
+import org.junit.jupiter.api.Test;
+
+class MockFieldItemTest implements WithAssertions {
+
+    @Test
+    void getObject() {
+        assertThat(new MockFieldItem("").getObject(0)).isNotNull();
+    }
+}
\ No newline at end of file
diff --git a/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/MockFieldTest.java b/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/MockFieldTest.java
new file mode 100644
index 0000000..5864e59
--- /dev/null
+++ b/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/MockFieldTest.java
@@ -0,0 +1,48 @@
+/*
+ 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.assertj.core.api.WithAssertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+class MockFieldTest implements WithAssertions {
+
+    @Test
+    void constructor1() {
+        assertThat(new MockField("")).isNotNull();
+    }
+
+    @Test
+    void constructor2() {
+        assertThat(new MockField("", Collections.emptyList())).isNotNull();
+    }
+
+    @Test
+    void getFieldQuery() {
+        new MockField("", Collections.emptyList()).getFieldQuery();
+    }
+
+    @Test
+    void getValues() {
+        new MockField("", Collections.emptyList()).getValues();
+    }
+}
\ No newline at end of file
diff --git a/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/PlcMockConnectionTest.java b/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/PlcMockConnectionTest.java
new file mode 100644
index 0000000..37bcb1a
--- /dev/null
+++ b/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/PlcMockConnectionTest.java
@@ -0,0 +1,156 @@
+/*
+ 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.messages.*;
+import org.apache.plc4x.java.api.metadata.PlcConnectionMetadata;
+import org.assertj.core.api.WithAssertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collections;
+import java.util.concurrent.CompletableFuture;
+
+@ExtendWith(MockitoExtension.class)
+class PlcMockConnectionTest implements WithAssertions {
+
+    PlcMockConnection SUT = new PlcMockConnection(null);
+
+    @Mock
+    MockDevice mockDevice;
+
+    @BeforeEach
+    void setUp() {
+        SUT.setDevice(mockDevice);
+    }
+
+    @Test
+    void getDevice() {
+        assertThat(SUT.getDevice()).isNotNull();
+    }
+
+    @Test
+    void setDevice() {
+        SUT.setDevice(null);
+    }
+
+    @Test
+    void connect() {
+        SUT.connect();
+    }
+
+    @Test
+    void isConnected() {
+        SUT.setDevice(mockDevice);
+        assertThat(SUT.isConnected()).isTrue();
+        SUT.setDevice(null);
+        assertThat(SUT.isConnected()).isFalse();
+    }
+
+    @Test
+    void close() {
+        SUT.close();
+    }
+
+    @Test
+    void getMetadata() {
+        PlcConnectionMetadata metadata = SUT.getMetadata();
+        assertThat(metadata).isNotNull();
+        assertThat(metadata.canRead()).isTrue();
+        assertThat(metadata.canWrite()).isTrue();
+        assertThat(metadata.canSubscribe()).isTrue();
+    }
+
+    @Test
+    void readRequestBuilder() {
+        assertThat(SUT.readRequestBuilder()).isNotNull();
+    }
+
+    @Test
+    void read() {
+        PlcReadRequest plcReadRequest = SUT.readRequestBuilder().build();
+        assertThat(SUT.read(plcReadRequest)).isNotNull();
+    }
+
+    @Test
+    @Disabled("due to mysterious NPE")
+    void write() throws Exception {
+        PlcWriteRequest plcWriteRequest = SUT.writeRequestBuilder()
+            .addItem("asd", "asd", "asd")
+            .build();
+        CompletableFuture<PlcWriteResponse> write = SUT.write(plcWriteRequest);
+        assertThat(write).isNotNull();
+        PlcWriteResponse plcWriteResponse = write.get();
+        assertThat(plcWriteResponse).isNotNull();
+    }
+
+    @Test
+    @Disabled("due to mysterious NPE")
+    void subscribe() throws Exception {
+        PlcSubscriptionRequest plcSubscriptionRequest = SUT.subscriptionRequestBuilder()
+            .addChangeOfStateField("asd", "asd")
+            .build();
+        CompletableFuture<PlcSubscriptionResponse> subscribe = SUT.subscribe(plcSubscriptionRequest);
+        assertThat(subscribe).isNotNull();
+        PlcSubscriptionResponse plcSubscriptionResponse = subscribe.get();
+        assertThat(plcSubscriptionResponse).isNotNull();
+    }
+
+    @Test
+    void unsubscribe() {
+        PlcUnsubscriptionRequest unsubscriptionRequest = SUT.unsubscriptionRequestBuilder().build();
+        assertThat(SUT.unsubscribe(unsubscriptionRequest)).isNotNull();
+    }
+
+    @Test
+    void register() {
+        SUT.register(plcSubscriptionEvent -> {
+        }, Collections.emptyList());
+    }
+
+    @Test
+    void unregister() {
+        SUT.unregister(null);
+    }
+
+    @Test
+    void writeRequestBuilder() {
+        assertThat(SUT.writeRequestBuilder()).isNotNull();
+    }
+
+    @Test
+    void subscriptionRequestBuilder() {
+        assertThat(SUT.subscriptionRequestBuilder()).isNotNull();
+    }
+
+    @Test
+    void unsubscriptionRequestBuilder() {
+        assertThat(SUT.unsubscriptionRequestBuilder()).isNotNull();
+    }
+
+    @Test
+    void getAuthentication() {
+        assertThat(SUT.getAuthentication()).isNull();
+    }
+}
\ No newline at end of file
diff --git a/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/PlcMockDriverTest.java b/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/PlcMockDriverTest.java
index 1da4853..b13ccbc 100644
--- a/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/PlcMockDriverTest.java
+++ b/plc4j/protocols/driver-bases/test/src/test/java/org/apache/plc4x/java/mock/PlcMockDriverTest.java
@@ -1,20 +1,20 @@
 /*
- * 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;
@@ -25,20 +25,18 @@ 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.assertj.core.api.WithAssertions;
+import org.junit.jupiter.api.Test;
 import org.mockito.Mockito;
 
 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;
+import static org.mockito.Mockito.*;
 
 
-public class PlcMockDriverTest {
+public class PlcMockDriverTest implements WithAssertions {
 
     private final PlcMockDriver driver = new PlcMockDriver();
 
@@ -53,10 +51,10 @@ public class PlcMockDriverTest {
     /**
      * 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.
-     *
+     * <p>
      * 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.
-     *
+     * <p>
      * 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.
      */
@@ -74,8 +72,15 @@ public class PlcMockDriverTest {
         verify(mock, times(1)).read("DB2.DBD17:INT");
     }
 
+    @Test
+    public void wrongDevice() {
+        assertThatThrownBy(() -> driver.connect("mock:"))
+            .isInstanceOf(PlcConnectionException.class);
+    }
+
     /**
      * 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 {