2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.asdctool.migration.task;
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;
31 import java.util.Collection;
32 import java.util.List;
35 import java.util.stream.Collectors;
38 public class MigrationTasksTest {
40 public static final String MIGRATIONS_BASE_PACKAGE = "org.openecomp.sdc.asdctool.migration.tasks";
41 private List<Migration> migrations;
44 public void setUp() throws Exception {
45 ClassScanner classScanner = new ClassScanner();
46 migrations = classScanner.getAllClassesOfType(MIGRATIONS_BASE_PACKAGE, Migration.class);
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()));
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());
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()));
72 private String getMigrationsNameAsString(Collection<Migration> migrations) {
73 return StringUtils.join(migrations.stream().map(mig -> mig.getClass().getName()).collect(Collectors.toList()), ",");