summaryrefslogtreecommitdiffstats
path: root/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/task/MigrationTasksTest.java
blob: fa61b7e19edd4c28abed106b2d941f948bbbbe1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.openecomp.sdc.asdctool.migration.task;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.openecomp.sdc.asdctool.migration.core.DBVersion;
import org.openecomp.sdc.asdctool.migration.core.task.Migration;
import org.openecomp.sdc.asdctool.migration.scanner.ClassScanner;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;


public class MigrationTasksTest  {

    public static final String MIGRATIONS_BASE_PACKAGE = "org.openecomp.sdc.asdctool.migration.tasks";
    private List<Migration> migrations;

    @BeforeMethod
    public void setUp() throws Exception {
        ClassScanner classScanner = new ClassScanner();
        migrations = classScanner.getAllClassesOfType(MIGRATIONS_BASE_PACKAGE, Migration.class);
    }

    @Test
    public void testNoTasksWithSameVersion() throws Exception {
        Map<DBVersion, List<Migration>> migrationsByVersion = migrations.stream().collect(Collectors.groupingBy(Migration::getVersion));
        migrationsByVersion.forEach((version, migrations) -> {
            if (migrations.size() > 1) {
                System.out.println(String.format("the following migration tasks have the same version %s. versions must be unique", version.toString()));
                Assert.fail(String.format("migration tasks %s has same version %s. migration tasks versions must be unique.", getMigrationsNameAsString(migrations), version.toString()));
            }
        });
    }

    @Test
    public void testNoTaskWithVersionGreaterThanCurrentVersion() throws Exception {
        Set<Migration> migrationsWithVersionsGreaterThanCurrent = migrations.stream().filter(mig -> mig.getVersion().compareTo(DBVersion.CURRENT_VERSION) > 0)
                .collect(Collectors.toSet());

        if (!migrationsWithVersionsGreaterThanCurrent.isEmpty()) {
            Assert.fail(String.format("migrations tasks %s have version which is greater than DBVersion.CURRENT_VERSION %s. did you forget to update current version?",
                               getMigrationsNameAsString(migrationsWithVersionsGreaterThanCurrent),
                               DBVersion.CURRENT_VERSION.toString()));
        }
    }

    private String getMigrationsNameAsString(Collection<Migration> migrations) {
        return StringUtils.join(migrations.stream().map(mig -> mig.getClass().getName()).collect(Collectors.toList()), ",");
    }
}