Update vulnerable package dependencies
[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 static org.junit.jupiter.api.Assertions.fail;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.openecomp.sdc.asdctool.migration.core.DBVersion;
29 import org.openecomp.sdc.asdctool.migration.core.task.Migration;
30 import org.openecomp.sdc.asdctool.migration.scanner.ClassScanner;
31
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.stream.Collectors;
37
38
39 public class MigrationTasksTest  {
40
41     public static final String MIGRATIONS_BASE_PACKAGE = "org.openecomp.sdc.asdctool.migration.tasks";
42     private List<Migration> migrations;
43
44     @BeforeEach
45     public void setUp() throws Exception {
46         ClassScanner classScanner = new ClassScanner();
47         migrations = classScanner.getAllClassesOfType(MIGRATIONS_BASE_PACKAGE, Migration.class);
48     }
49
50     @Test
51     public void testNoTasksWithSameVersion() throws Exception {
52         Map<DBVersion, List<Migration>> migrationsByVersion = migrations.stream().collect(Collectors.groupingBy(Migration::getVersion));
53         migrationsByVersion.forEach((version, migrations) -> {
54             if (migrations.size() > 1) {
55                 System.out.println(String.format("the following migration tasks have the same version %s. versions must be unique", version.toString()));
56                 fail(String.format("migration tasks %s has same version %s. migration tasks versions must be unique.", getMigrationsNameAsString(migrations), version.toString()));
57             }
58         });
59     }
60
61     @Test
62     public void testNoTaskWithVersionGreaterThanCurrentVersion() throws Exception {
63         Set<Migration> migrationsWithVersionsGreaterThanCurrent = migrations.stream().filter(mig -> mig.getVersion().compareTo(DBVersion.DEFAULT_VERSION) > 0)
64                 .collect(Collectors.toSet());
65
66         if (!migrationsWithVersionsGreaterThanCurrent.isEmpty()) {
67             fail(String.format("migrations tasks %s have version which is greater than DBVersion.DEFAULT_VERSION %s. did you forget to update current version?",
68                     getMigrationsNameAsString(migrationsWithVersionsGreaterThanCurrent),
69                     DBVersion.DEFAULT_VERSION.toString()));
70         }
71     }
72
73     private String getMigrationsNameAsString(Collection<Migration> migrations) {
74         return StringUtils.join(migrations.stream().map(mig -> mig.getClass().getName()).collect(Collectors.toList()), ",");
75     }
76 }