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 static org.junit.jupiter.api.Assertions.fail;
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;
32 import java.util.Collection;
33 import java.util.List;
36 import java.util.stream.Collectors;
39 public class MigrationTasksTest {
41 public static final String MIGRATIONS_BASE_PACKAGE = "org.openecomp.sdc.asdctool.migration.tasks";
42 private List<Migration> migrations;
45 public void setUp() throws Exception {
46 ClassScanner classScanner = new ClassScanner();
47 migrations = classScanner.getAllClassesOfType(MIGRATIONS_BASE_PACKAGE, Migration.class);
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()));
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());
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()));
73 private String getMigrationsNameAsString(Collection<Migration> migrations) {
74 return StringUtils.join(migrations.stream().map(mig -> mig.getClass().getName()).collect(Collectors.toList()), ",");