Catalog alignment
[sdc.git] / asdctool / src / test / java / org / openecomp / sdc / asdctool / migration / task / MigrationTasksTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.asdctool.migration.task;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.openecomp.sdc.asdctool.migration.core.DBVersion;
25 import org.openecomp.sdc.asdctool.migration.core.task.Migration;
26 import org.openecomp.sdc.asdctool.migration.scanner.ClassScanner;
27 import org.testng.Assert;
28 import org.testng.annotations.BeforeMethod;
29 import org.testng.annotations.Test;
30
31 import java.util.Collection;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Set;
35 import java.util.stream.Collectors;
36
37
38 public class MigrationTasksTest  {
39
40     public static final String MIGRATIONS_BASE_PACKAGE = "org.openecomp.sdc.asdctool.migration.tasks";
41     private List<Migration> migrations;
42
43     @BeforeMethod
44     public void setUp() throws Exception {
45         ClassScanner classScanner = new ClassScanner();
46         migrations = classScanner.getAllClassesOfType(MIGRATIONS_BASE_PACKAGE, Migration.class);
47     }
48
49     @Test
50     public void testNoTasksWithSameVersion() throws Exception {
51         Map<DBVersion, List<Migration>> migrationsByVersion = migrations.stream().collect(Collectors.groupingBy(Migration::getVersion));
52         migrationsByVersion.forEach((version, migrations) -> {
53             if (migrations.size() > 1) {
54                 System.out.println(String.format("the following migration tasks have the same version %s. versions must be unique", version.toString()));
55                 Assert.fail(String.format("migration tasks %s has same version %s. migration tasks versions must be unique.", getMigrationsNameAsString(migrations), version.toString()));
56             }
57         });
58     }
59
60     @Test
61     public void testNoTaskWithVersionGreaterThanCurrentVersion() throws Exception {
62         Set<Migration> migrationsWithVersionsGreaterThanCurrent = migrations.stream().filter(mig -> mig.getVersion().compareTo(DBVersion.DEFAULT_VERSION) > 0)
63                 .collect(Collectors.toSet());
64
65         if (!migrationsWithVersionsGreaterThanCurrent.isEmpty()) {
66             Assert.fail(String.format("migrations tasks %s have version which is greater than DBVersion.DEFAULT_VERSION %s. did you forget to update current version?",
67                     getMigrationsNameAsString(migrationsWithVersionsGreaterThanCurrent),
68                     DBVersion.DEFAULT_VERSION.toString()));
69         }
70     }
71
72     private String getMigrationsNameAsString(Collection<Migration> migrations) {
73         return StringUtils.join(migrations.stream().map(mig -> mig.getClass().getName()).collect(Collectors.toList()), ",");
74     }
75 }