You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by bo...@apache.org on 2023/10/23 13:04:35 UTC

[streampipes] branch 2045-implement-migration-for-adapter-and-pipeline-element-configurations updated: add tests

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

bossenti pushed a commit to branch 2045-implement-migration-for-adapter-and-pipeline-element-configurations
in repository https://gitbox.apache.org/repos/asf/streampipes.git


The following commit(s) were added to refs/heads/2045-implement-migration-for-adapter-and-pipeline-element-configurations by this push:
     new 7277ddb50 add tests
7277ddb50 is described below

commit 7277ddb50698b2482907308b9889f8d0d7350b84
Author: bossenti <bo...@posteo.de>
AuthorDate: Mon Oct 23 15:04:26 2023 +0200

    add tests
---
 .../api/migration/AdapterMigratorTest.java         | 42 +++++++++++++++++
 .../model/SpServiceDefinitionBuilderTest.java      | 52 ++++++++++++++++++++++
 .../model/migration/ModelMigratorConfig.java       |  4 --
 .../model/migration/ModelMigratorConfigTest.java   | 49 ++++++++++++++++++++
 4 files changed, 143 insertions(+), 4 deletions(-)

diff --git a/streampipes-extensions-api/src/test/java/org/apache/streampipes/extensions/api/migration/AdapterMigratorTest.java b/streampipes-extensions-api/src/test/java/org/apache/streampipes/extensions/api/migration/AdapterMigratorTest.java
new file mode 100644
index 000000000..6061f1bc4
--- /dev/null
+++ b/streampipes-extensions-api/src/test/java/org/apache/streampipes/extensions/api/migration/AdapterMigratorTest.java
@@ -0,0 +1,42 @@
+/*
+ * 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.api.migration;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
+public class AdapterMigratorTest {
+
+  @Test
+  public void migrationOrdering(){
+    var migratorA1 = new MigratorTestClass<AdapterMigrator>("app-id", 1, 2);
+    var migratorA2 = new MigratorTestClass<AdapterMigrator>("app-id", 2, 3);
+    var migratorB = new MigratorTestClass<AdapterMigrator>("other-id", 1, 2);
+
+    List<MigratorTestClass<AdapterMigrator>> migrators = new ArrayList<>(List.of(migratorA2, migratorA1, migratorB));
+    Collections.sort(migrators);
+
+    assertEquals(List.of(migratorA1, migratorA2, migratorB), migrators);
+  }
+}
diff --git a/streampipes-extensions-management/src/test/java/org/apache/streampipes/extensions/management/model/SpServiceDefinitionBuilderTest.java b/streampipes-extensions-management/src/test/java/org/apache/streampipes/extensions/management/model/SpServiceDefinitionBuilderTest.java
index 523d9c1f6..e36a624a9 100644
--- a/streampipes-extensions-management/src/test/java/org/apache/streampipes/extensions/management/model/SpServiceDefinitionBuilderTest.java
+++ b/streampipes-extensions-management/src/test/java/org/apache/streampipes/extensions/management/model/SpServiceDefinitionBuilderTest.java
@@ -24,7 +24,13 @@ import org.apache.streampipes.extensions.api.connect.StreamPipesAdapter;
 import org.apache.streampipes.extensions.api.connect.context.IAdapterGuessSchemaContext;
 import org.apache.streampipes.extensions.api.connect.context.IAdapterRuntimeContext;
 import org.apache.streampipes.extensions.api.extractor.IAdapterParameterExtractor;
+import org.apache.streampipes.extensions.api.extractor.IStaticPropertyExtractor;
+import org.apache.streampipes.extensions.api.migration.AdapterMigrator;
+import org.apache.streampipes.extensions.api.migration.MigrationResult;
+import org.apache.streampipes.model.connect.adapter.AdapterDescription;
 import org.apache.streampipes.model.connect.guess.GuessSchema;
+import org.apache.streampipes.model.extensions.svcdiscovery.SpServiceTagPrefix;
+import org.apache.streampipes.model.migration.ModelMigratorConfig;
 
 import org.junit.Test;
 
@@ -43,6 +49,27 @@ public class SpServiceDefinitionBuilderTest {
     assertEquals(expected, result.getAdapters().get(0));
   }
 
+  @Test
+  public void registerMigration() {
+
+    var migration1 = new TestMigration("app-id", 1, 2);
+    var migration2 = new TestMigration("app-id", 0, 1);
+    var migration3 = new TestMigration("other-id", 0, 1);
+    var migration4 = new TestMigration("app-id", 0, 1);
+
+    var result = SpServiceDefinitionBuilder.create("", "", "", 1)
+            .registerMigrators(migration1, migration2, migration3, migration4)
+            .build();
+
+    // assert de-duplication (last migration should not be registered)
+    assertEquals(3, result.getMigrators().size());
+
+    // assert ordering
+    assertEquals(migration2, result.getMigrators().get(0));
+    assertEquals(migration1, result.getMigrators().get(1));
+    assertEquals(migration3, result.getMigrators().get(2));
+  }
+
   private static class TestAdapter implements StreamPipesAdapter {
 
     @Override
@@ -67,4 +94,29 @@ public class SpServiceDefinitionBuilderTest {
       return null;
     }
   }
+
+  private static class TestMigration extends AdapterMigrator {
+
+    String appId;
+    int fromVersion;
+    int toVersion;
+
+    public TestMigration(String appId, int fromVersion, int toVersion) {
+      this.appId = appId;
+      this.fromVersion = fromVersion;
+      this.toVersion = toVersion;
+    }
+
+    @Override
+    public ModelMigratorConfig config() {
+      return new ModelMigratorConfig(appId, SpServiceTagPrefix.ADAPTER, fromVersion, toVersion);
+    }
+
+    @Override
+    public MigrationResult<AdapterDescription> migrate(
+            AdapterDescription element,
+            IStaticPropertyExtractor extractor) throws RuntimeException {
+      return null;
+    }
+  }
 }
diff --git a/streampipes-model/src/main/java/org/apache/streampipes/model/migration/ModelMigratorConfig.java b/streampipes-model/src/main/java/org/apache/streampipes/model/migration/ModelMigratorConfig.java
index 65713d905..23356e750 100644
--- a/streampipes-model/src/main/java/org/apache/streampipes/model/migration/ModelMigratorConfig.java
+++ b/streampipes-model/src/main/java/org/apache/streampipes/model/migration/ModelMigratorConfig.java
@@ -45,10 +45,6 @@ public record ModelMigratorConfig(String targetAppId, SpServiceTagPrefix modelTy
     }
 
     if (targetAppId.equals(((ModelMigratorConfig) o).targetAppId())) {
-
-      if (fromVersion != ((ModelMigratorConfig) o).fromVersion()) {
-        return this.toVersion() - ((ModelMigratorConfig) o).toVersion();
-      }
       return this.fromVersion() - ((ModelMigratorConfig) o).fromVersion();
     }
 
diff --git a/streampipes-model/src/test/java/org/apache/streampipes/model/migration/ModelMigratorConfigTest.java b/streampipes-model/src/test/java/org/apache/streampipes/model/migration/ModelMigratorConfigTest.java
new file mode 100644
index 000000000..914e1ed4e
--- /dev/null
+++ b/streampipes-model/src/test/java/org/apache/streampipes/model/migration/ModelMigratorConfigTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.model.migration;
+
+import org.apache.streampipes.model.extensions.svcdiscovery.SpServiceTagPrefix;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+
+public class ModelMigratorConfigTest {
+
+  static ModelMigratorConfig config = new ModelMigratorConfig("app-id", SpServiceTagPrefix.ADAPTER, 0, 1);
+
+  @Test
+  public void compareToWithNullPointer() {
+    assertThrows(NullPointerException.class, () -> config.compareTo(null));
+  }
+
+  @Test
+  public void compareToWithClassCastException() {
+    assertThrows(ClassCastException.class, () -> config.compareTo(5));
+  }
+
+  @Test
+  public void compareTo() {
+    assertEquals(0, config.compareTo(new ModelMigratorConfig("other-app-id", SpServiceTagPrefix.ADAPTER, 0, 1)));
+    assertEquals(0, config.compareTo(new ModelMigratorConfig("app-id", SpServiceTagPrefix.ADAPTER, 0, 1)));
+    assertEquals(-1, config.compareTo(new ModelMigratorConfig("app-id", SpServiceTagPrefix.ADAPTER, 1, 2)));
+    assertEquals(0, config.compareTo(new ModelMigratorConfig("app-id", SpServiceTagPrefix.ADAPTER, 0, 3)));
+  }
+}