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.resolver;
23 import org.mockito.InjectMocks;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.openecomp.sdc.asdctool.migration.core.DBVersion;
27 import org.openecomp.sdc.asdctool.migration.core.task.IMigrationStage;
28 import org.openecomp.sdc.asdctool.migration.core.task.Migration;
29 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
30 import org.openecomp.sdc.asdctool.migration.dao.MigrationTasksDao;
31 import org.openecomp.sdc.asdctool.migration.service.SdcRepoService;
32 import org.openecomp.sdc.be.dao.cassandra.CassandraClient;
33 import org.testng.annotations.BeforeMethod;
34 import org.testng.annotations.Test;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.List;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42 import static org.testng.Assert.assertEquals;
43 import static org.testng.Assert.assertTrue;
45 public class SpringBeansMigrationResolverTest {
48 private SpringBeansMigrationResolver testInstance;
51 private SdcRepoService sdcRepoServiceMock;
53 private List<Migration> migrations = Arrays.asList(createMigration("1710.1"), createMigration("1710.22"),
54 createMigration("1707.12"), createMigration("1710.3"));
58 MockitoAnnotations.initMocks(this);
59 testInstance.setMigrations(migrations);
63 public void testResolveMigrations_getMigrationsWithVersionGreaterThanLatest() {
64 when(sdcRepoServiceMock.getLatestDBVersion()).thenReturn(DBVersion.fromString("1710.2"));
65 testInstance.setPostMigrations(Collections.emptyList());
66 List<IMigrationStage> resolvedMigrations = testInstance.resolveMigrations();
67 assertEquals(resolvedMigrations.size(), 2);
68 assertEquals(resolvedMigrations.get(0).getVersion(), DBVersion.fromString("1710.3"));
69 assertEquals(resolvedMigrations.get(1).getVersion(), DBVersion.fromString("1710.22"));
73 public void testResolveMigration_noLatestVersionForCurrentMajorVersion() {
74 when(sdcRepoServiceMock.getLatestDBVersion()).thenReturn(DBVersion.fromString("1710.-1"));
75 testInstance.setPostMigrations(Collections.emptyList());
76 List<IMigrationStage> resolvedMigrations = testInstance.resolveMigrations();
77 assertEquals(resolvedMigrations.size(), 3);
78 assertEquals(resolvedMigrations.get(0).getVersion(), DBVersion.fromString("1710.1"));
79 assertEquals(resolvedMigrations.get(1).getVersion(), DBVersion.fromString("1710.3"));
80 assertEquals(resolvedMigrations.get(2).getVersion(), DBVersion.fromString("1710.22"));
84 public void testResolveMigrations_emptyMigrationsList() {
85 testInstance.setMigrations(Collections.emptyList());
86 testInstance.setPostMigrations(Collections.emptyList());
87 when(sdcRepoServiceMock.getLatestDBVersion()).thenReturn(DBVersion.fromString("1710.-1"));
88 List<IMigrationStage> resolvedMigrations = testInstance.resolveMigrations();
89 assertTrue(resolvedMigrations.isEmpty());
92 private Migration createMigration(String version) {
93 return new Migration() {
95 public String description() {
100 public DBVersion getVersion() {
101 return DBVersion.fromString(version);
105 public MigrationResult migrate() {
111 private SpringBeansMigrationResolver createTestSubject() {
112 return new SpringBeansMigrationResolver(null, null, new SdcRepoService(new MigrationTasksDao(mock(CassandraClient.class))));