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/07 17:40:19 UTC
[incubator-plc4x] branch master updated: [OPM] Added
PlcEntityInterceptor tests.
This is an automated email from the ASF dual-hosted git repository.
jfeinauer pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git
The following commit(s) were added to refs/heads/master by this push:
new ae11433 [OPM] Added PlcEntityInterceptor tests.
ae11433 is described below
commit ae1143333095769efca183cf3d90cdc12fb1b34f
Author: Julian Feinauer <j....@pragmaticminds.de>
AuthorDate: Wed Nov 7 18:38:06 2018 +0100
[OPM] Added PlcEntityInterceptor tests.
---
plc4j/utils/opm/pom.xml | 6 ++
.../plc4x/java/opm/PlcEntityInterceptor.java | 8 +-
.../plc4x/java/opm/PlcEntityInterceptorTest.java | 117 +++++++++++++++++++++
.../plc4x/java/opm/PlcEntityManagerTest.java | 2 +
4 files changed, 129 insertions(+), 4 deletions(-)
diff --git a/plc4j/utils/opm/pom.xml b/plc4j/utils/opm/pom.xml
index 383d5ef..263dfdb 100644
--- a/plc4j/utils/opm/pom.xml
+++ b/plc4j/utils/opm/pom.xml
@@ -80,6 +80,12 @@
<version>0.2.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.plc4x</groupId>
+ <artifactId>plc4j-protocol-test</artifactId>
+ <version>0.2.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
diff --git a/plc4j/utils/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityInterceptor.java b/plc4j/utils/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityInterceptor.java
index 857f944..00c0fe0 100644
--- a/plc4j/utils/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityInterceptor.java
+++ b/plc4j/utils/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityInterceptor.java
@@ -201,7 +201,7 @@ public class PlcEntityInterceptor {
field = m.getDeclaringClass().getDeclaredField(variable);
annotation = field.getDeclaredAnnotation(PlcField.class);
} catch (NoSuchFieldException e) {
- throw new OPMException("Unable to identify field annotated field for call to " + m.getName(), e);
+ throw new OPMException("Unable to identify field with name '" + variable + "' for call to '" + m.getName() + "'", e);
}
try (PlcConnection connection = driverManager.getConnection(address)) {
// Catch the exception, if no reader present (see below)
@@ -248,11 +248,11 @@ public class PlcEntityInterceptor {
}
@SuppressWarnings("squid:S3776") // Cognitive Complexity not too high, as highly structured
- private static Object getTyped(Class<?> clazz, PlcReadResponse response, String sourceFieldName) {
+ static Object getTyped(Class<?> clazz, PlcReadResponse response, String sourceFieldName) {
LOGGER.debug("getTyped clazz: {}, response: {}, fieldName: {}", clazz, response, sourceFieldName);
if (response.getResponseCode(sourceFieldName) != PlcResponseCode.OK) {
- throw new PlcRuntimeException(String.format("Unable to read specified field %s, response code was %s",
- sourceFieldName, response));
+ throw new PlcRuntimeException(String.format("Unable to read specified field '%s', response code was '%s'",
+ sourceFieldName, response.getResponseCode(sourceFieldName)));
}
if (clazz.isPrimitive()) {
if (clazz == boolean.class) {
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
new file mode 100644
index 0000000..7108474
--- /dev/null
+++ b/plc4j/utils/opm/src/test/java/org/apache/plc4x/java/opm/PlcEntityInterceptorTest.java
@@ -0,0 +1,117 @@
+/*
+ * 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.opm;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
+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.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.stubbing.Answer;
+
+import java.util.Collections;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeoutException;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.when;
+
+public class PlcEntityInterceptorTest {
+
+ private void runGetPlcResponseWIthException(Answer a) throws InterruptedException, ExecutionException, TimeoutException, OPMException {
+ PlcReadRequest request = Mockito.mock(PlcReadRequest.class);
+ CompletableFuture future = Mockito.mock(CompletableFuture.class);
+ when(future.get(anyLong(), any())).then(a);
+ when(request.execute()).thenReturn(future);
+
+ PlcEntityInterceptor.getPlcReadResponse(request);
+ }
+
+ @Test(expected = OPMException.class)
+ public void getPlcReadResponse_catchesInterruptedException_rethrows() throws OPMException, InterruptedException, ExecutionException, TimeoutException {
+ runGetPlcResponseWIthException(invocation -> {
+ throw new InterruptedException();
+ });
+ return;
+ }
+
+ @Test(expected = OPMException.class)
+ public void getPlcReadResponse_catchesExecutionException_rethrows() throws OPMException, InterruptedException, ExecutionException, TimeoutException {
+ runGetPlcResponseWIthException(invocation -> {
+ throw new ExecutionException(new Exception());
+ });
+ }
+
+ @Test(expected = OPMException.class)
+ public void getPlcReadResponse_timeoutOnGet_rethrows() throws OPMException {
+ PlcReadRequest request = Mockito.mock(PlcReadRequest.class);
+ CompletableFuture future = new CompletableFuture<>();
+ when(request.execute()).thenReturn(future);
+
+ PlcEntityInterceptor.getPlcReadResponse(request);
+ }
+
+ @Test
+ public void getTyped_notOkResponse_throws() {
+ DefaultPlcReadResponse response = new DefaultPlcReadResponse(null, Collections.singletonMap("field", Pair.of(PlcResponseCode.NOT_FOUND, null)));
+ String message = null;
+ try {
+ PlcEntityInterceptor.getTyped(Long.class, response, "field");
+ } catch (PlcRuntimeException e) {
+ message = e.getMessage();
+ }
+ assertEquals("Unable to read specified field 'field', response code was 'NOT_FOUND'", message);
+ }
+
+ @Test
+ public void getterWithNoField() throws OPMException {
+ PlcEntityManager entityManager = new PlcEntityManager();
+ BadEntity entity = entityManager.connect(BadEntity.class, "mock:test");
+
+ String message = null;
+ try {
+ entity.getField1();
+ } catch (Exception e) {
+ message = e.getMessage();
+ }
+ assertEquals("Unable to identify field with name 'field1' for call to 'getField1'", message);
+ }
+
+ @PlcEntity
+ public static class BadEntity {
+
+ public BadEntity() {
+ // For OPM
+ }
+
+ // Getter with no field
+ @PlcField("field1")
+ public String getField1() {
+ return "";
+ }
+
+ }
+
+}
\ No newline at end of file
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 7270ccf..14e748c 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
@@ -25,6 +25,7 @@ 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.connection.PlcFieldHandler;
@@ -446,4 +447,5 @@ public class PlcEntityManagerTest {
'}';
}
}
+
}
\ No newline at end of file