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 15:25:48 UTC

[incubator-plc4x] branch develop updated: [plc4j-opm] fixed 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 d1bcb67  [plc4j-opm] fixed tests
d1bcb67 is described below

commit d1bcb670498820ac002104a01b4cba06617669c9
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Sat Nov 24 16:17:47 2018 +0100

    [plc4j-opm] fixed tests
---
 .../plc4x/java/opm/PlcEntityInterceptorTest.java   | 23 +++++++++++++++++++
 .../plc4x/java/opm/PlcEntityManagerTest.java       | 26 +++++++++-------------
 2 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityInterceptorTest.java b/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityInterceptorTest.java
index 7ec939b..fadf6f4 100644
--- a/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityInterceptorTest.java
+++ b/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityInterceptorTest.java
@@ -25,12 +25,17 @@ import org.apache.plc4x.java.api.messages.PlcReadRequest;
 import org.apache.plc4x.java.api.types.PlcResponseCode;
 import org.apache.plc4x.java.base.messages.DefaultPlcReadResponse;
 import org.assertj.core.api.WithAssertions;
+import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
 import org.mockito.stubbing.Answer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.Collections;
+import java.util.concurrent.Callable;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeoutException;
@@ -42,6 +47,7 @@ import static org.mockito.ArgumentMatchers.anyLong;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+@ExtendWith(MockitoExtension.class)
 public class PlcEntityInterceptorTest implements WithAssertions {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(PlcEntityInterceptorTest.class);
@@ -102,6 +108,23 @@ public class PlcEntityInterceptorTest implements WithAssertions {
             .hasMessage("Unable to identify field with name 'field1' for call to 'getField1'");
     }
 
+    @Nested
+    class Misc {
+
+        @Mock
+        Callable callable;
+
+        @Test
+        void missingCases() throws Exception {
+            when(callable.call()).then(invocation -> {
+                throw new PlcRuntimeException("broken");
+            });
+            assertThatThrownBy(() -> PlcEntityInterceptor.interceptGetter(null, this.getClass().getDeclaredMethod("missingCases"), callable, null, null, null, null, null))
+                .isInstanceOf(OPMException.class)
+                .hasMessage("Exception during forwarding call");
+        }
+    }
+
     private void runGetPlcResponseWIthException(Answer a) throws InterruptedException, ExecutionException, TimeoutException, OPMException {
         PlcReadRequest request = mock(PlcReadRequest.class);
         CompletableFuture future = mock(CompletableFuture.class);
diff --git a/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityManagerTest.java b/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityManagerTest.java
index 40ee6ef..95f672f 100644
--- a/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityManagerTest.java
+++ b/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityManagerTest.java
@@ -21,11 +21,8 @@ package org.apache.plc4x.java.opm;
 
 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.exceptions.PlcInvalidFieldException;
-import org.apache.plc4x.java.api.messages.PlcReadRequest;
-import org.apache.plc4x.java.api.metadata.PlcConnectionMetadata;
 import org.apache.plc4x.java.api.types.PlcResponseCode;
 import org.apache.plc4x.java.base.messages.items.DefaultStringFieldItem;
 import org.apache.plc4x.java.mock.MockDevice;
@@ -34,6 +31,8 @@ import org.assertj.core.api.WithAssertions;
 import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.junit.jupiter.MockitoExtension;
 
@@ -48,18 +47,15 @@ public class PlcEntityManagerTest implements WithAssertions {
 
     @Nested
     class Read {
+
+        @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+        PlcDriverManager driverManager;
+
         @Test
         public void throwsInvalidFieldException_rethrows() throws PlcConnectionException {
             // Prepare the Mock
-            PlcDriverManager driverManager = Mockito.mock(PlcDriverManager.class);
-            PlcConnection connection = Mockito.mock(PlcConnection.class);
-            PlcConnectionMetadata metadata = Mockito.mock(PlcConnectionMetadata.class);
-            PlcReadRequest.Builder builder = Mockito.mock(PlcReadRequest.Builder.class);
-            when(metadata.canRead()).thenReturn(true);
-            when(connection.readRequestBuilder()).thenReturn(builder);
-            when(connection.getMetadata()).thenReturn(metadata);
-            when(builder.build()).thenThrow(new PlcInvalidFieldException("field1"));
-            when(driverManager.getConnection(any())).thenReturn(connection);
+            when(driverManager.getConnection(any()).readRequestBuilder().build())
+                .thenThrow(new PlcInvalidFieldException("field1"));
 
             // Create Entity Manager
             PlcEntityManager entityManager = new PlcEntityManager(driverManager);
@@ -73,8 +69,8 @@ public class PlcEntityManagerTest implements WithAssertions {
         @Test
         public void unableToConnect_rethrows() throws PlcConnectionException {
             // Prepare the Mock
-            PlcDriverManager driverManager = Mockito.mock(PlcDriverManager.class);
-            when(driverManager.getConnection(any())).thenThrow(new PlcConnectionException(""));
+            when(driverManager.getConnection(any()))
+                .thenThrow(new PlcConnectionException(""));
 
             // Create Entity Manager
             PlcEntityManager entityManager = new PlcEntityManager(driverManager);
@@ -162,6 +158,7 @@ public class PlcEntityManagerTest implements WithAssertions {
 
     @Nested
     class Write {
+
         @Test
         void simpleWrite() throws Exception {
             SimpleAliasRegistry registry = new SimpleAliasRegistry();
@@ -201,7 +198,6 @@ public class PlcEntityManagerTest implements WithAssertions {
             PlcDriverManager driverManager = new PlcDriverManager();
             PlcMockConnection connection = (PlcMockConnection) driverManager.getConnection("mock:test");
             MockDevice mockDevice = Mockito.mock(MockDevice.class);
-            when(mockDevice.write(anyString(), any())).thenReturn(PlcResponseCode.OK);
             when(mockDevice.read(any())).thenReturn(Pair.of(PlcResponseCode.OK, new DefaultStringFieldItem("value")));
             connection.setDevice(mockDevice);