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

zehnder pushed a commit to branch 3112-opc-ua-multi-node-selection-editor
in repository https://gitbox.apache.org/repos/asf/streampipes.git


The following commit(s) were added to 
refs/heads/3112-opc-ua-multi-node-selection-editor by this push:
     new c724946eae feat(#3112): Add exception when node id is invalid
c724946eae is described below

commit c724946eaef6ec0c25c50811d37c649566117a78
Author: Philipp Zehnder <[email protected]>
AuthorDate: Tue Aug 13 21:57:38 2024 +0200

    feat(#3112): Add exception when node id is invalid
---
 .../connectors/opcua/adapter/OpcUaNodeBrowser.java | 55 ++++++++++++++++-----
 .../connect/opcua/opcAdapterConfiguration.spec.ts  | 56 +++++++++++++---------
 2 files changed, 75 insertions(+), 36 deletions(-)

diff --git 
a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/adapter/OpcUaNodeBrowser.java
 
b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/adapter/OpcUaNodeBrowser.java
index ead655d021..e7dde9d990 100644
--- 
a/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/adapter/OpcUaNodeBrowser.java
+++ 
b/streampipes-extensions/streampipes-connectors-opcua/src/main/java/org/apache/streampipes/extensions/connectors/opcua/adapter/OpcUaNodeBrowser.java
@@ -29,6 +29,7 @@ import org.eclipse.milo.opcua.sdk.client.nodes.UaNode;
 import org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode;
 import org.eclipse.milo.opcua.stack.core.Identifiers;
 import org.eclipse.milo.opcua.stack.core.UaException;
+import org.eclipse.milo.opcua.stack.core.UaRuntimeException;
 import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
 import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode;
 import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
@@ -49,8 +50,10 @@ public class OpcUaNodeBrowser {
 
   private static final Logger LOG = 
LoggerFactory.getLogger(OpcUaNodeBrowser.class);
 
-  public OpcUaNodeBrowser(OpcUaClient client,
-                          OpcUaConfig spOpcUaClientConfig) {
+  public OpcUaNodeBrowser(
+      OpcUaClient client,
+      OpcUaConfig spOpcUaClientConfig
+  ) {
     this.client = client;
     this.spOpcConfig = spOpcUaClientConfig;
   }
@@ -85,20 +88,37 @@ public class OpcUaNodeBrowser {
 
   private OpcNode toOpcNode(String nodeName) throws UaException {
     AddressSpace addressSpace = getAddressSpace();
-    NodeId nodeId = NodeId.parse(nodeName);
+
+    NodeId nodeId;
+    try {
+      nodeId = NodeId.parse(nodeName);
+    } catch (UaRuntimeException e) {
+      throw new UaException(
+          StatusCode.BAD.getValue(), "Node ID " + nodeName + " is not in the 
correct format. "
+          + "The correct format is 
`ns=<namespaceIndex>;<identifierType>=<identifier>`.", e);
+    }
 
     UaNode node;
     try {
       node = addressSpace.getNode(nodeId);
     } catch (UaException e) {
-      throw new UaException(StatusCode.BAD.getValue(), "Node with ID " + 
nodeId + " is not present in the OPC UA server.", e);
+      throw new UaException(
+          StatusCode.BAD.getValue(),
+          "Node with ID " + nodeId + " is not present in the OPC UA server.", e
+      );
     }
 
-    LOG.info("Using node of type {}", node.getNodeClass().toString());
+    LOG.info(
+        "Using node of type {}",
+        node.getNodeClass()
+            .toString()
+    );
 
     if (node instanceof UaVariableNode) {
-      UInteger value = (UInteger) ((UaVariableNode) 
node).getDataType().getIdentifier();
-      return new OpcNode(node.getDisplayName().getText(), 
OpcUaTypes.getType(value), node.getNodeId());
+      UInteger value = (UInteger) ((UaVariableNode) node).getDataType()
+                                                         .getIdentifier();
+      return new OpcNode(node.getDisplayName()
+                             .getText(), OpcUaTypes.getType(value), 
node.getNodeId());
     }
 
     LOG.warn("Node {} not of type UaVariableNode", node.getDisplayName());
@@ -106,16 +126,20 @@ public class OpcUaNodeBrowser {
     throw new UaException(StatusCode.BAD, "Node is not of type 
BaseDataVariableTypeNode");
   }
 
-  private List<TreeInputNode> findChildren(OpcUaClient client,
-                                           NodeId nodeId) throws UaException {
+  private List<TreeInputNode> findChildren(
+      OpcUaClient client,
+      NodeId nodeId
+  ) throws UaException {
     return client
         .getAddressSpace()
         .browseNodes(nodeId)
         .stream()
         .map(node -> {
           TreeInputNode childNode = new TreeInputNode();
-          childNode.setNodeName(node.getDisplayName().getText());
-          childNode.setInternalNodeName(node.getNodeId().toParseableString());
+          childNode.setNodeName(node.getDisplayName()
+                                    .getText());
+          childNode.setInternalNodeName(node.getNodeId()
+                                            .toParseableString());
           childNode.setDataNode(isDataNode(node));
           childNode.setNodeMetadata(new OpcUaNodeMetadataExtractor(client, 
node).extract());
           return childNode;
@@ -124,13 +148,18 @@ public class OpcUaNodeBrowser {
   }
 
 
-
   private AddressSpace getAddressSpace() {
     return client.getAddressSpace();
   }
 
   private boolean isDataNode(UaNode node) {
-    return (node.getNodeClass().equals(NodeClass.Variable) || 
(node.getNodeClass().equals(NodeClass.VariableType)))
+    return (
+        node.getNodeClass()
+            .equals(NodeClass.Variable) || (
+            node.getNodeClass()
+                .equals(NodeClass.VariableType)
+        )
+    )
         && node instanceof UaVariableNode;
   }
 
diff --git a/ui/cypress/tests/connect/opcua/opcAdapterConfiguration.spec.ts 
b/ui/cypress/tests/connect/opcua/opcAdapterConfiguration.spec.ts
index 7fb8215f07..6dc6624e33 100644
--- a/ui/cypress/tests/connect/opcua/opcAdapterConfiguration.spec.ts
+++ b/ui/cypress/tests/connect/opcua/opcAdapterConfiguration.spec.ts
@@ -23,6 +23,7 @@ import { TreeNodeUserInputBuilder } from 
'../../../support/builder/TreeNodeUserI
 import { StaticPropertyUtils } from 
'../../../support/utils/userInput/StaticPropertyUtils';
 import { TreeStaticPropertyUtils } from 
'../../../support/utils/userInput/TreeStaticPropertyUtils';
 import { ErrorMessageUtils } from '../../../support/utils/ErrorMessageUtils';
+import { AdapterInput } from '../../../support/model/AdapterInput';
 
 describe('Test OPC-UA Adapter Configuration', () => {
     beforeEach('Setup Test', () => {
@@ -49,13 +50,8 @@ describe('Test OPC-UA Adapter Configuration', () => {
             ),
         );
 
-        const adapterConfiguration = adapterBuilder.build();
-
-        // Set up initial configuration
-        ConnectUtils.goToConnect();
-        ConnectUtils.goToNewAdapterPage();
-        ConnectUtils.selectAdapter(adapterConfiguration.adapterType);
-        StaticPropertyUtils.input(adapterConfiguration.adapterConfiguration);
+        const adapterInput = adapterBuilder.build();
+        setUpInitialConfiguration(adapterInput);
 
         TreeStaticPropertyUtils.validateAmountOfSelectedNodes(2);
 
@@ -84,13 +80,8 @@ describe('Test OPC-UA Adapter Configuration', () => {
     });
 
     it('Test OPC-UA Text Editor', () => {
-        const adapterConfiguration = getAdapterBuilder().build();
-
-        // Set up initial configuration
-        ConnectUtils.goToConnect();
-        ConnectUtils.goToNewAdapterPage();
-        ConnectUtils.selectAdapter(adapterConfiguration.adapterType);
-        StaticPropertyUtils.input(adapterConfiguration.adapterConfiguration);
+        const adapterInput = getAdapterBuilder().build();
+        setUpInitialConfiguration(adapterInput);
 
         TreeStaticPropertyUtils.treeEditor().should('be.visible');
         TreeStaticPropertyUtils.textEditor().should('not.exist');
@@ -122,19 +113,17 @@ describe('Test OPC-UA Adapter Configuration', () => {
         TreeStaticPropertyUtils.getTextInTextEditor().should(
             'equal',
             '# Provide OPC UA Node IDs below, one per line.# Format: ' +
-            'ns=<namespace>;s=<node_id> (e.g., ns=3;s=SampleNodeId)' +
-            'ns=3;s=StepUpns=3;s=AlternatingBoolean',
+                'ns=<namespace>;s=<node_id> (e.g., ns=3;s=SampleNodeId)' +
+                'ns=3;s=StepUpns=3;s=AlternatingBoolean',
         );
+
+        TreeStaticPropertyUtils.switchToTreeEditor();
+        TreeStaticPropertyUtils.validateAmountOfShownBrowseNodes(3);
     });
 
     it('Test OPC-UA Node does not exist', () => {
-        const adapterConfiguration = getAdapterBuilder().build();
-
-        // Set up initial configuration
-        ConnectUtils.goToConnect();
-        ConnectUtils.goToNewAdapterPage();
-        ConnectUtils.selectAdapter(adapterConfiguration.adapterType);
-        StaticPropertyUtils.input(adapterConfiguration.adapterConfiguration);
+        const adapterInput = getAdapterBuilder().build();
+        setUpInitialConfiguration(adapterInput);
 
         // Switch to text editor
         TreeStaticPropertyUtils.switchToTextEditor();
@@ -145,6 +134,20 @@ describe('Test OPC-UA Adapter Configuration', () => {
         // validate that an error is shown with node id
         ErrorMessageUtils.containsMessage('NodeDoesNotExist');
     });
+
+    it('Test OPC-UA Wrong Node Id Format', () => {
+        const adapterInput = getAdapterBuilder().build();
+        setUpInitialConfiguration(adapterInput);
+
+        // Switch to text editor
+        TreeStaticPropertyUtils.switchToTextEditor();
+        TreeStaticPropertyUtils.typeInTextEditor('NoValidNodeId');
+
+        ConnectUtils.finishAdapterSettings();
+
+        // validate that an error is shown with node id
+        ErrorMessageUtils.containsMessage('NoValidNodeId');
+    });
 });
 
 const getAdapterBuilder = () => {
@@ -166,3 +169,10 @@ const getAdapterBuilder = () => {
 
     return builder;
 };
+
+const setUpInitialConfiguration = (adapterInput: AdapterInput) => {
+    ConnectUtils.goToConnect();
+    ConnectUtils.goToNewAdapterPage();
+    ConnectUtils.selectAdapter(adapterInput.adapterType);
+    StaticPropertyUtils.input(adapterInput.adapterConfiguration);
+};

Reply via email to