diff options
author | Michael Lando <ml636r@att.com> | 2017-07-17 21:12:03 +0300 |
---|---|---|
committer | Michael Lando <ml636r@att.com> | 2017-07-17 21:12:03 +0300 |
commit | 75aacbbe1acf78fa53378f07f0a8c7769449a17e (patch) | |
tree | 68a9799eb8f4704dd9a3d513401df9bb11af7739 /asdctool/src/test | |
parent | dec02e7cc74e1c401be51bd9d266575e1e008f5f (diff) |
[SDC] rebase 1710 code
Change-Id: I532ed68979fee7840ea8a5395e7e965b155fb9f9
Signed-off-by: Michael Lando <ml636r@att.com>
Diffstat (limited to 'asdctool/src/test')
8 files changed, 553 insertions, 0 deletions
diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/DummyMigrationFactory.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/DummyMigrationFactory.java new file mode 100644 index 0000000000..b16951ef02 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/DummyMigrationFactory.java @@ -0,0 +1,72 @@ +package org.openecomp.sdc.asdctool.migration; + + +import org.openecomp.sdc.asdctool.migration.core.DBVersion; +import org.openecomp.sdc.asdctool.migration.core.task.Migration; +import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult; + +public class DummyMigrationFactory { + + public static Migration SUCCESSFUL_MIGRATION = new Migration() { + @Override + public String description() { + return "success mig"; + } + + @Override + public DBVersion getVersion() { + return DBVersion.fromString("1710.22"); + } + + @Override + public MigrationResult migrate() { + MigrationResult migrationResult = new MigrationResult(); + migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.COMPLETED); + migrationResult.setMsg("myMsg"); + return migrationResult; + } + }; + + public static Migration FAILED_MIGRATION = new Migration() { + @Override + public String description() { + return "failed mig"; + } + + @Override + public DBVersion getVersion() { + return DBVersion.fromString("1710.22"); + } + + @Override + public MigrationResult migrate() { + MigrationResult migrationResult = new MigrationResult(); + migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.FAILED); + migrationResult.setMsg("myMsg"); + return migrationResult; + } + }; + + public static Migration getMigration(String version, MigrationResult.MigrationStatus status) { + return new Migration() { + @Override + public String description() { + return "success mig"; + } + + @Override + public DBVersion getVersion() { + return DBVersion.fromString(version); + } + + @Override + public MigrationResult migrate() { + MigrationResult migrationResult = new MigrationResult(); + migrationResult.setMigrationStatus(status); + migrationResult.setMsg("myMsg"); + return migrationResult; + } + }; + } + +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/DBVersionTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/DBVersionTest.java new file mode 100644 index 0000000000..4e5fd3ab31 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/DBVersionTest.java @@ -0,0 +1,57 @@ +package org.openecomp.sdc.asdctool.migration.core; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; + +public class DBVersionTest { + + + @DataProvider(name = "invalidVersionStringsProvider") + private Object[][] invalidVersionStringsProvider() { + return new Object[][] { + {"1.1.1"}, + {"1.a"}, + {"a.1"}, + {"1"} + }; + } + + @Test(dataProvider = "invalidVersionStringsProvider", expectedExceptions = MigrationException.class) + public void testFromString_invalidVersionString(String invalidVersion) { + DBVersion.fromString(invalidVersion); + } + + @DataProvider(name = "validVersionStringsProvider") + private Object[][] validVersionStringsProvider() { + return new Object[][] { + {"1.1", "1.1"}, + {"10100.0001", "10100.1"}, + {"000.1", "0.1"}, + {"01.00001000", "1.1000"}, + }; + } + + @Test(dataProvider = "validVersionStringsProvider") + public void testFromString(String validString, String expectedVersionString) { + assertEquals(expectedVersionString, DBVersion.fromString(validString).toString()); + } + + @DataProvider(name = "versionComparisionProvider") + public static Object[][] versionComparisionProvider() { + return new Object[][] { + {"1.1", "001.00001", 0}, + {"10.1", "0010.00001", 0}, + {"1.1", "001.000010", -1}, + {"1.1", "0010.00001", -1}, + {"10.10", "0010.00001", 1}, + {"1.1", "001.00", 1}, + }; + } + + @Test(dataProvider = "versionComparisionProvider") + public void testVersionCompareTo2(String firstVersion, String otherVersion, int expectedComparisionResult) throws Exception { + assertEquals(DBVersion.fromString(firstVersion).compareTo(DBVersion.fromString(otherVersion)), expectedComparisionResult); + } +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/SdcMigrationToolTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/SdcMigrationToolTest.java new file mode 100644 index 0000000000..b1813227e2 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/SdcMigrationToolTest.java @@ -0,0 +1,101 @@ +package org.openecomp.sdc.asdctool.migration.core; + +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.internal.verification.Times; +import org.openecomp.sdc.asdctool.migration.core.task.Migration; +import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult; +import org.openecomp.sdc.asdctool.migration.resolver.MigrationResolver; +import org.openecomp.sdc.asdctool.migration.service.SdcRepoService; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.Arrays; +import java.util.Collections; + +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class SdcMigrationToolTest { + + @InjectMocks + private SdcMigrationTool testInstance = spy(SdcMigrationTool.class); + + @Mock + private MigrationResolver migrationResolverMock; + + @Mock + private SdcRepoService sdcRepoServiceMock; + + @BeforeMethod + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testMigrate_noMigrations() throws Exception { + when(migrationResolverMock.resolveMigrations()).thenReturn(Collections.emptyList()); + testInstance.migrate(false); + verify(sdcRepoServiceMock, new Times(0)).clearTasksForCurrentMajor(); + verify(sdcRepoServiceMock, new Times(0)).createMigrationTask(Mockito.any()); + } + + @Test + public void testMigrate_enforceFlag_removeAllMigrationTasksForCurrentVersion() throws Exception { + when(migrationResolverMock.resolveMigrations()).thenReturn(Collections.emptyList()); + testInstance.migrate(true); + verify(sdcRepoServiceMock, new Times(1)).clearTasksForCurrentMajor(); + } + + @Test + public void testMigrate_stopAfterFirstFailure() throws Exception { + when(migrationResolverMock.resolveMigrations()).thenReturn(Arrays.asList(new SuccessfulMigration(), new FailedMigration(), new SuccessfulMigration())); + testInstance.migrate(false); + verify(sdcRepoServiceMock, new Times(0)).clearTasksForCurrentMajor(); + verify(sdcRepoServiceMock, new Times(1)).createMigrationTask(Mockito.any()); + + } + + private class FailedMigration implements Migration { + + @Override + public String description() { + return null; + } + + @Override + public DBVersion getVersion() { + return DBVersion.fromString("1710.22"); + } + + @Override + public MigrationResult migrate() { + MigrationResult migrationResult = new MigrationResult(); + migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.FAILED); + return migrationResult; + } + } + + private class SuccessfulMigration implements Migration { + + @Override + public String description() { + return null; + } + + @Override + public DBVersion getVersion() { + return DBVersion.fromString("1710.22"); + } + + @Override + public MigrationResult migrate() { + MigrationResult migrationResult = new MigrationResult(); + migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.COMPLETED); + return migrationResult; + } + } +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutorImplTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutorImplTest.java new file mode 100644 index 0000000000..763d6c83d2 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/core/execution/MigrationExecutorImplTest.java @@ -0,0 +1,47 @@ +package org.openecomp.sdc.asdctool.migration.core.execution; + +import org.openecomp.sdc.asdctool.migration.core.DBVersion; +import org.openecomp.sdc.asdctool.migration.core.task.Migration; +import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; + +public class MigrationExecutorImplTest { + + @Test + public void testExecuteMigration() throws Exception { + MigrationExecutionResult execute = new MigrationExecutorImpl().execute(new DummyMigration()); + + } + + private void assertMigrationTaskEntryByMigrationExecutionResult(MigrationExecutionResult executionResult, Migration migration, MigrationResult result) { + assertEquals(executionResult.getMsg(), result.getMsg()); + assertEquals(executionResult.getMigrationStatus().name(), result.getMigrationStatus()); + assertEquals(executionResult.getTaskName(), migration.getClass().getName()); + assertEquals(executionResult.getVersion(), migration.getVersion()); + assertNotNull(executionResult.getExecutionTime()); + } + + private class DummyMigration implements Migration { + + @Override + public String description() { + return null; + } + + @Override + public DBVersion getVersion() { + return DBVersion.fromString("1710.22"); + } + + @Override + public MigrationResult migrate() { + MigrationResult migrationResult = new MigrationResult(); + migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.COMPLETED); + migrationResult.setMsg("myMsg"); + return migrationResult; + } + } +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/resolver/SpringBeansMigrationResolverTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/resolver/SpringBeansMigrationResolverTest.java new file mode 100644 index 0000000000..6084608f0e --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/resolver/SpringBeansMigrationResolverTest.java @@ -0,0 +1,84 @@ +package org.openecomp.sdc.asdctool.migration.resolver; + +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.asdctool.migration.core.DBVersion; +import org.openecomp.sdc.asdctool.migration.core.task.Migration; +import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult; +import org.openecomp.sdc.asdctool.migration.service.SdcRepoService; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +public class SpringBeansMigrationResolverTest { + + @InjectMocks + private SpringBeansMigrationResolver testInstance; + + @Mock + private SdcRepoService sdcRepoServiceMock; + + private List<Migration> migrations = Arrays.asList(createMigration("1710.1"), createMigration("1710.22"), createMigration("1707.12"), createMigration("1710.3")); + + + @BeforeMethod + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + testInstance.setMigrations(migrations); + } + + @Test + public void testResolveMigrations_getMigrationsWithVersionGreaterThanLatest() throws Exception { + when(sdcRepoServiceMock.getLatestDBVersion()).thenReturn(DBVersion.fromString("1710.2")); + List<Migration> resolvedMigrations = testInstance.resolveMigrations(); + assertEquals(resolvedMigrations.size(), 2); + assertEquals(resolvedMigrations.get(0).getVersion(), DBVersion.fromString("1710.3")); + assertEquals(resolvedMigrations.get(1).getVersion(), DBVersion.fromString("1710.22")); + } + + @Test + public void testResolveMigration_noLatestVersionForCurrentMajorVersion() throws Exception { + when(sdcRepoServiceMock.getLatestDBVersion()).thenReturn(DBVersion.fromString("1710.-1")); + List<Migration> resolvedMigrations = testInstance.resolveMigrations(); + assertEquals(resolvedMigrations.size(), 3); + assertEquals(resolvedMigrations.get(0).getVersion(), DBVersion.fromString("1710.1")); + assertEquals(resolvedMigrations.get(1).getVersion(), DBVersion.fromString("1710.3")); + assertEquals(resolvedMigrations.get(2).getVersion(), DBVersion.fromString("1710.22")); + } + + @Test + public void testResolveMigrations_emptyMigrationsList() throws Exception { + testInstance.setMigrations(Collections.emptyList()); + when(sdcRepoServiceMock.getLatestDBVersion()).thenReturn(DBVersion.fromString("1710.-1")); + List<Migration> resolvedMigrations = testInstance.resolveMigrations(); + assertTrue(resolvedMigrations.isEmpty()); + } + + private Migration createMigration(String version) { + return new Migration() { + @Override + public String description() { + return null; + } + + @Override + public DBVersion getVersion() { + return DBVersion.fromString(version); + } + + @Override + public MigrationResult migrate() { + return null; + } + }; + } + +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/scanner/ClassScanner.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/scanner/ClassScanner.java new file mode 100644 index 0000000000..a2bf623b83 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/scanner/ClassScanner.java @@ -0,0 +1,81 @@ +package org.openecomp.sdc.asdctool.migration.scanner; + +import org.apache.commons.io.FileUtils; +import org.openecomp.sdc.asdctool.migration.core.MigrationException; + +import java.io.File; +import java.lang.reflect.Modifier; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +/** + * scan and instantiate classes of given type in the class path + */ +public class ClassScanner { + + + private ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + + public <T> List<T> getAllClassesOfType(String basePackage, Class<T> ofType) { + Collection<File> allClassesInPackage = getAllClassesInPackage(basePackage); + List<T> loadedClasses = new ArrayList<>(); + for (File clazzFile : allClassesInPackage) { + Optional<T> instance = loadAndInstantiateClass(getClassReference(clazzFile), ofType); + instance.ifPresent(loadedClasses::add); + } + return loadedClasses; + } + + private <T> Optional<T> loadAndInstantiateClass(String classReference, Class<T> ofType) { + try { + return instantiateClassOfType(classReference, ofType); + }catch (ClassNotFoundException e) { + //log + throw new MigrationException(String.format("could not find class %s of type %s. cause: %s", classReference, ofType.toGenericString(), e.getMessage())); + } catch (IllegalAccessException e1) { + //log + throw new MigrationException(String.format("could not instantiate class %s of type %s. class is not accessible. cause: %s", classReference, ofType.toGenericString(), e1.getMessage())); + } catch (InstantiationException e2) { + //log + throw new MigrationException(String.format("could not instantiate class %s of type %s. cause: %s", classReference, ofType.toGenericString(), e2.getMessage())); + } + } + + private <T> Optional<T> instantiateClassOfType(String classReference, Class<T> ofType) throws ClassNotFoundException, IllegalAccessException, InstantiationException { + String className = classReference.replaceAll(".class$", "").replaceAll(".class", ""); + Class<?> aClass = classLoader.loadClass(className); + if (ofType.isAssignableFrom(aClass) && isInstantiateAbleClass(aClass)){ + return Optional.of((T) aClass.newInstance()); + } + return Optional.empty(); + } + + private boolean isInstantiateAbleClass(Class<?> clazz) { + return !Modifier.isAbstract(clazz.getModifiers()) && !clazz.isEnum() && !clazz.isAnonymousClass() && !clazz.isInterface(); + } + + private Collection<File> getAllClassesInPackage(String fromPackage) { + String path = fromPackage.replace(".", "/"); + URL resource = classLoader.getResource(path); + if (noMigrationTasks(resource)) { + return Collections.emptyList(); + } + return FileUtils.listFiles(new File(resource.getFile()), new String[]{"class"}, true); + } + + private boolean noMigrationTasks(URL resource) { + return resource == null; + } + + private String getClassReference(File classFile) { + String asPackage = classFile.getPath().replace(File.separator, "."); + String classes = "classes."; + return asPackage.substring(asPackage.indexOf(classes) + classes.length()); + } + + +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/service/SdcRepoServiceTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/service/SdcRepoServiceTest.java new file mode 100644 index 0000000000..f8e9abe2c9 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/service/SdcRepoServiceTest.java @@ -0,0 +1,55 @@ +package org.openecomp.sdc.asdctool.migration.service; + +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.internal.verification.Times; +import org.openecomp.sdc.asdctool.migration.core.DBVersion; +import org.openecomp.sdc.be.dao.cassandra.MigrationTasksDao; +import org.openecomp.sdc.be.resources.data.MigrationTaskEntry; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.math.BigInteger; + +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertEquals; + +public class SdcRepoServiceTest { + + @InjectMocks + private SdcRepoService testInstance; + + @Mock + private MigrationTasksDao migrationTasksDaoMock; + + @BeforeMethod + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testGetLatestVersion_noMinorVersionForCurrentVersion() throws Exception { + when(migrationTasksDaoMock.getLatestMinorVersion(DBVersion.CURRENT_VERSION.getMajor())).thenReturn(null); + DBVersion latestDBVersion = testInstance.getLatestDBVersion(); + assertEquals(latestDBVersion.getMajor(), DBVersion.CURRENT_VERSION.getMajor()); + assertEquals(latestDBVersion.getMinor(), BigInteger.valueOf(Integer.MIN_VALUE)); + } + + @Test + public void testGetLatestVersion() throws Exception { + when(migrationTasksDaoMock.getLatestMinorVersion(DBVersion.CURRENT_VERSION.getMajor())).thenReturn(BigInteger.TEN); + DBVersion latestDBVersion = testInstance.getLatestDBVersion(); + assertEquals(latestDBVersion.getMajor(), DBVersion.CURRENT_VERSION.getMajor()); + assertEquals(latestDBVersion.getMinor(), BigInteger.TEN); + } + + @Test + public void testCreateMigrationTask() throws Exception { + MigrationTaskEntry taskEntry = new MigrationTaskEntry(); + testInstance.createMigrationTask(taskEntry); + verify(migrationTasksDaoMock, new Times(1)).createMigrationTask(taskEntry); + } + +} diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/task/MigrationTasksTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/task/MigrationTasksTest.java new file mode 100644 index 0000000000..037d51ba12 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/migration/task/MigrationTasksTest.java @@ -0,0 +1,56 @@ +package org.openecomp.sdc.asdctool.migration.task; + +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; + + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + + +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()), ","); + } +} |