Repository: apache/streampipes
Pull request #2495 (2024-02-27 07:40:45)

refactor: Remove unused code and minor refactoring
<!--
  ~ 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.
  ~
  -->
  
  <!--
Thanks for contributing! Here are some tips you can follow to help us incorporate your contribution quickly and easily:
1. If this is your first time, please read our contributor guidelines:
    - https://streampipes.apache.org/getinvolved.html
    - https://cwiki.apache.org/confluence/display/STREAMPIPES/Getting+Started
2. Make sure the PR title is formatted like: `[#<GitHub issue id>] PR title ...`
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., `[WIP][#<GitHub issue id>] PR title ...`.
4. Please write your PR title to summarize what this PR proposes/fixes.
5. Link the PR to the corresponding GitHub issue (if present) in the `Development` section in the right menu bar. 
6. Be sure to keep the PR description updated to reflect all changes.
7. If possible, provide a concise example to reproduce the issue for a faster review.
8. Make sure tests pass via `mvn clean install`.
9. (Optional) If the contribution is large, please file an Apache ICLA
    - http://apache.org/licenses/icla.pdf
-->

### Purpose
<!--
Please clarify what changes you are proposing and describe how those changes will address the issue.
Furthermore, describe potential consequences the changes might have.
-->

### Remarks
<!--
Is there anything left we need to pay attention on?
Are there some references that might be important? E.g. links to Confluence, or discussions
on the mailing list or GitHub.
-->
PR introduces (a) breaking change(s): no

PR introduces (a) deprecation(s): no



Patch:
diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/SpOpcUaConfigExtractor.java b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/SpOpcUaConfigExtractor.java
index 612b3e4905..1145cb3501 100644
--- a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/SpOpcUaConfigExtractor.java
+++ b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/config/SpOpcUaConfigExtractor.java
@@ -84,9 +84,9 @@ public class SpOpcUaConfigExtractor {
     if (useURL) {
       String serverAddress =
           extractor.singleValueParameter(OPC_SERVER_URL.name(), String.class);
-      config.setOpcServerURL(OpcUaUtil.formatServerAddress(serverAddress));
+      config.setOpcServerURL(OpcUaUtil.addOpcPrefixIfNotExists(serverAddress));
     } else {
-      String serverAddress = OpcUaUtil.formatServerAddress(
+      String serverAddress = OpcUaUtil.addOpcPrefixIfNotExists(
           extractor.singleValueParameter(OPC_SERVER_HOST.name(), String.class)
       );
       int port = extractor.singleValueParameter(OPC_SERVER_PORT.name(), int.class);
diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaUtil.java b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaUtil.java
index 201a47b603..f7a3a2029d 100644
--- a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaUtil.java
+++ b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaUtil.java
@@ -60,18 +60,15 @@ import java.util.stream.Collectors;
  */
 public class OpcUaUtil {
 
+  private static final String OPC_TCP_PREFIX = "opc.tcp://";
+
   /***
    * Ensures server address starts with {@code opc.tcp://}
    * @param serverAddress server address as given by user
    * @return correctly formated server address
    */
-  public static String formatServerAddress(String serverAddress) {
-
-    if (!serverAddress.startsWith("opc.tcp://")) {
-      serverAddress = "opc.tcp://" + serverAddress;
-    }
-
-    return serverAddress;
+  public static String addOpcPrefixIfNotExists(String serverAddress) {
+    return serverAddress.startsWith(OPC_TCP_PREFIX) ? serverAddress : OPC_TCP_PREFIX + serverAddress;
   }
 
   /***
@@ -103,21 +100,24 @@ public class OpcUaUtil {
         for (OpcNode opcNode : selectedNodes) {
           if (opcNode.hasUnitId()) {
             allProperties.add(PrimitivePropertyBuilder
-                .create(opcNode.getType(), opcNode.getLabel())
-                .label(opcNode.getLabel())
-                .measurementUnit(new URI(opcNode.getQudtURI()))
-                .build());
+                                  .create(opcNode.getType(), opcNode.getLabel())
+                                  .label(opcNode.getLabel())
+                                  .measurementUnit(new URI(opcNode.getQudtURI()))
+                                  .build());
           } else {
             allProperties.add(PrimitivePropertyBuilder
-                .create(opcNode.getType(), opcNode.getLabel())
-                .label(opcNode.getLabel())
-                .build());
+                                  .create(opcNode.getType(), opcNode.getLabel())
+                                  .label(opcNode.getLabel())
+                                  .build());
           }
         }
       }
 
-      var nodeIds = selectedNodes.stream().map(OpcNode::getNodeId).collect(Collectors.toList());
-      var response = spOpcUaClient.getClient().readValues(0, TimestampsToReturn.Both, nodeIds);
+      var nodeIds = selectedNodes.stream()
+                                 .map(OpcNode::getNodeId)
+                                 .collect(Collectors.toList());
+      var response = spOpcUaClient.getClient()
+                                  .readValues(0, TimestampsToReturn.Both, nodeIds);
 
       var returnValues = response.get();
 
@@ -138,20 +138,25 @@ public class OpcUaUtil {
     return builder.build();
   }
 
-  private static void makeEventPreview(List<OpcNode> selectedNodes,
-                                       Map<String, Object> eventPreview,
-                                       Map<String, FieldStatusInfo> fieldStatusInfos,
-                                       List<DataValue> dataValues) {
+  private static void makeEventPreview(
+      List<OpcNode> selectedNodes,
+      Map<String, Object> eventPreview,
+      Map<String, FieldStatusInfo> fieldStatusInfos,
+      List<DataValue> dataValues
+  ) {
 
     for (int i = 0; i < dataValues.size(); i++) {
       var dv = dataValues.get(i);
-      String label = selectedNodes.get(i).getLabel();
+      String label = selectedNodes.get(i)
+                                  .getLabel();
       if (StatusCode.GOOD.equals(dv.getStatusCode())) {
-        var value = dv.getValue().getValue();
+        var value = dv.getValue()
+                      .getValue();
         eventPreview.put(label, value);
         fieldStatusInfos.put(label, FieldStatusInfo.good());
       } else {
-        String additionalInfo = dv.getStatusCode() != null ? dv.getStatusCode().toString() : "Status code is null";
+        String additionalInfo = dv.getStatusCode() != null ? dv.getStatusCode()
+                                                               .toString() : "Status code is null";
         fieldStatusInfos.put(label, FieldStatusInfo.bad(additionalInfo, false));
       }
     }
@@ -179,7 +184,7 @@ public class OpcUaUtil {
       return config;
     }
 
-    var spOpcUaClient = new SpOpcUaClient(
+    SpOpcUaClient spOpcUaClient = new SpOpcUaClient(
         SpOpcUaConfigExtractor.extractSharedConfig(parameterExtractor, new OpcUaConfig())
     );
 


Test patch:
diff --git a/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaUtilTest.java b/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaUtilTest.java
new file mode 100644
index 0000000000..5dda95b53e
--- /dev/null
+++ b/streampipes-extensions/streampipes-connectors-opcua/src/test/java/org/apache/streampipes/extensions/connectors/opcua/utils/OpcUaUtilTest.java
@@ -0,0 +1,40 @@
+/*
+ * 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.streampipes.extensions.connectors.opcua.utils;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class OpcUaUtilTest {
+
+  private static final String SERVER_ADDRESS_WITH_OPC_PREFIX = "opc.tcp://example.com";
+
+  @Test
+  public void testAddOpcPrefixIfNotExistsWithPrefix() {
+    var result = OpcUaUtil.addOpcPrefixIfNotExists(SERVER_ADDRESS_WITH_OPC_PREFIX);
+    assertEquals(SERVER_ADDRESS_WITH_OPC_PREFIX, result);
+  }
+
+  @Test
+  public void testAddOpcPrefixIfNotExistsNoPrefix() {
+    var result = OpcUaUtil.addOpcPrefixIfNotExists("example.com");
+    assertEquals(SERVER_ADDRESS_WITH_OPC_PREFIX, result);
+  }
+}
\ No newline at end of file