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

zehnder pushed a commit to branch 
3191-remove-latestfetchednodes-and-nodes-from-opc-ua-adapters-in-couchdb
in repository https://gitbox.apache.org/repos/asf/streampipes.git


The following commit(s) were added to 
refs/heads/3191-remove-latestfetchednodes-and-nodes-from-opc-ua-adapters-in-couchdb
 by this push:
     new cf87aa473c fix(#3191): Add migration to remove nodes fields from opc 
ua adapters
cf87aa473c is described below

commit cf87aa473ce14f2085b1f0763b0851dc9df6fab7
Author: Philipp Zehnder <[email protected]>
AuthorDate: Thu Aug 29 08:19:46 2024 +0200

    fix(#3191): Add migration to remove nodes fields from opc ua adapters
---
 streampipes-service-core/pom.xml                   |  5 ++
 .../core/migrations/AvailableMigrations.java       |  4 +-
 .../RemoveNodesFromOpcUaAdaptersMigration.java     | 98 ++++++++++++++++++++++
 .../RemoveNodesFromOpcUaAdaptersMigrationTest.java | 85 +++++++++++++++++++
 4 files changed, 191 insertions(+), 1 deletion(-)

diff --git a/streampipes-service-core/pom.xml b/streampipes-service-core/pom.xml
index 9d34826cc2..ba2a1333c7 100644
--- a/streampipes-service-core/pom.xml
+++ b/streampipes-service-core/pom.xml
@@ -119,6 +119,11 @@
             <artifactId>junit-jupiter-api</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
diff --git 
a/streampipes-service-core/src/main/java/org/apache/streampipes/service/core/migrations/AvailableMigrations.java
 
b/streampipes-service-core/src/main/java/org/apache/streampipes/service/core/migrations/AvailableMigrations.java
index 6ae71c253d..010c16d55e 100644
--- 
a/streampipes-service-core/src/main/java/org/apache/streampipes/service/core/migrations/AvailableMigrations.java
+++ 
b/streampipes-service-core/src/main/java/org/apache/streampipes/service/core/migrations/AvailableMigrations.java
@@ -29,6 +29,7 @@ import 
org.apache.streampipes.service.core.migrations.v095.MergeFilenamesAndRena
 import 
org.apache.streampipes.service.core.migrations.v970.AddLinkSettingsMigration;
 import 
org.apache.streampipes.service.core.migrations.v970.DataExplorerDataViewMigration;
 import 
org.apache.streampipes.service.core.migrations.v970.ModifyAssetLinkTypeMigration;
+import 
org.apache.streampipes.service.core.migrations.v970.RemoveNodesFromOpcUaAdaptersMigration;
 
 import java.util.Arrays;
 import java.util.List;
@@ -46,7 +47,8 @@ public class AvailableMigrations {
         new MergeFilenamesAndRenameDuplicatesMigration(),
         new AddLinkSettingsMigration(),
         new DataExplorerDataViewMigration(),
-        new ModifyAssetLinkTypeMigration()
+        new ModifyAssetLinkTypeMigration(),
+        new RemoveNodesFromOpcUaAdaptersMigration()
     );
   }
 }
diff --git 
a/streampipes-service-core/src/main/java/org/apache/streampipes/service/core/migrations/v970/RemoveNodesFromOpcUaAdaptersMigration.java
 
b/streampipes-service-core/src/main/java/org/apache/streampipes/service/core/migrations/v970/RemoveNodesFromOpcUaAdaptersMigration.java
new file mode 100644
index 0000000000..e21c14866e
--- /dev/null
+++ 
b/streampipes-service-core/src/main/java/org/apache/streampipes/service/core/migrations/v970/RemoveNodesFromOpcUaAdaptersMigration.java
@@ -0,0 +1,98 @@
+/*
+ * 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.service.core.migrations.v970;
+
+import org.apache.streampipes.model.connect.adapter.AdapterDescription;
+import 
org.apache.streampipes.model.staticproperty.RuntimeResolvableTreeInputStaticProperty;
+import org.apache.streampipes.service.core.migrations.Migration;
+import org.apache.streampipes.storage.api.IAdapterStorage;
+import org.apache.streampipes.storage.management.StorageDispatcher;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+
+public class RemoveNodesFromOpcUaAdaptersMigration implements Migration {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(RemoveNodesFromOpcUaAdaptersMigration.class);
+
+  private final IAdapterStorage adapterStorage;
+
+  public RemoveNodesFromOpcUaAdaptersMigration() {
+    adapterStorage = StorageDispatcher.INSTANCE.getNoSqlStore()
+                                               .getAdapterInstanceStorage();
+  }
+
+  public RemoveNodesFromOpcUaAdaptersMigration(IAdapterStorage adapterStorage) 
{
+    this.adapterStorage = adapterStorage;
+  }
+
+  @Override
+  public boolean shouldExecute() {
+    return true;
+  }
+
+  @Override
+  public String getDescription() {
+    return "Remove latestFetchedNodes and nodes from OPC UA adapter instances 
and updates the adapters in CouchDB.";
+  }
+
+  @Override
+  public void executeMigration() throws IOException {
+    var adapters = adapterStorage.findAll();
+
+    var opcUaAdapters = filterForOpcUaAdapters(adapters);
+
+    opcUaAdapters.forEach(adapter -> {
+      removeTreeNodesFromConfigurationAndUpdateAdapter(adapter);
+      LOG.info("The nodes and latest fetched nodes of the adapter {} have been 
removed.", adapter.getElementId());
+    });
+
+  }
+
+  private void 
removeTreeNodesFromConfigurationAndUpdateAdapter(AdapterDescription adapter) {
+    removeTreeNodesFromConfiguration(adapter);
+    adapterStorage.updateElement(adapter);
+  }
+
+  /**
+   * This method sets the nodes and latest fetched nodes of the adapter 
configuration to an empty list.
+   */
+  private static void removeTreeNodesFromConfiguration(AdapterDescription 
adapter) {
+    adapter.getConfig()
+           .stream()
+           .filter(RuntimeResolvableTreeInputStaticProperty.class::isInstance)
+           .map(RuntimeResolvableTreeInputStaticProperty.class::cast)
+           .forEach(treeInputProperty -> {
+             treeInputProperty.setNodes(Collections.emptyList());
+             treeInputProperty.setLatestFetchedNodes(Collections.emptyList());
+           });
+  }
+
+  private static List<AdapterDescription> 
filterForOpcUaAdapters(List<AdapterDescription> adapters) {
+    return adapters.stream()
+                   .filter(adapter -> adapter.getAppId()
+                                             
.equals("org.apache.streampipes.connect.iiot.adapters.opcua"))
+                   .toList();
+  }
+
+}
diff --git 
a/streampipes-service-core/src/test/java/org/apache/streampipes/service/core/migrations/v970/RemoveNodesFromOpcUaAdaptersMigrationTest.java
 
b/streampipes-service-core/src/test/java/org/apache/streampipes/service/core/migrations/v970/RemoveNodesFromOpcUaAdaptersMigrationTest.java
new file mode 100644
index 0000000000..d6b9d23877
--- /dev/null
+++ 
b/streampipes-service-core/src/test/java/org/apache/streampipes/service/core/migrations/v970/RemoveNodesFromOpcUaAdaptersMigrationTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.service.core.migrations.v970;
+
+import org.apache.streampipes.model.connect.adapter.AdapterDescription;
+import 
org.apache.streampipes.model.staticproperty.RuntimeResolvableTreeInputStaticProperty;
+import org.apache.streampipes.model.staticproperty.TreeInputNode;
+import org.apache.streampipes.storage.api.IAdapterStorage;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+
+import java.io.IOException;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+
+class RemoveNodesFromOpcUaAdaptersMigrationTest {
+
+  private RemoveNodesFromOpcUaAdaptersMigration migration;
+  private IAdapterStorage adapterStorage;
+
+  @BeforeEach
+  void setUp() {
+    adapterStorage = mock(IAdapterStorage.class);
+    migration = new RemoveNodesFromOpcUaAdaptersMigration(adapterStorage);
+  }
+
+  @Test
+  void executeMigration_RemoveConfigsFromOpcUaAdapter() throws IOException {
+    var opcUaAdapter = prepareOpcUaAdapterWithNodesAndLatestFetchedNodes();
+    when(adapterStorage.findAll()).thenReturn(List.of(opcUaAdapter));
+
+    migration.executeMigration();
+
+    // Ensure that the nodes and latestFetchedNodes are removed from the 
adapter description
+    var captor = ArgumentCaptor.forClass(AdapterDescription.class);
+    verify(adapterStorage).updateElement(captor.capture());
+    var updatedAdapter = captor.getValue();
+    var treeProperty = getFirstTreeInputProperty(updatedAdapter);
+    assertTrue(treeProperty.getNodes().isEmpty());
+    assertTrue(treeProperty.getLatestFetchedNodes()
+                           .isEmpty());
+  }
+
+  private RuntimeResolvableTreeInputStaticProperty 
getFirstTreeInputProperty(AdapterDescription adapterDescription) {
+    return adapterDescription.getConfig()
+                             .stream()
+                             .filter(config -> config instanceof 
RuntimeResolvableTreeInputStaticProperty)
+                             .map(config -> 
(RuntimeResolvableTreeInputStaticProperty) config)
+                             .findFirst()
+                             .orElse(null);
+  }
+
+  private AdapterDescription 
prepareOpcUaAdapterWithNodesAndLatestFetchedNodes() {
+    var opcUaAdapter = new AdapterDescription();
+    
opcUaAdapter.setAppId("org.apache.streampipes.connect.iiot.adapters.opcua");
+    var property = new RuntimeResolvableTreeInputStaticProperty();
+    property.setNodes(List.of(new TreeInputNode()));
+    property.setLatestFetchedNodes(List.of(new TreeInputNode()));
+    opcUaAdapter.setConfig(List.of(property));
+    return opcUaAdapter;
+  }
+}
\ No newline at end of file

Reply via email to