a169e6f752068e7bd650435f271cb47073a17092
[sdc.git] /
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.resolver;
22
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25 import static org.testng.Assert.assertEquals;
26 import static org.testng.Assert.assertTrue;
27
28 import java.util.Arrays;
29 import java.util.Collections;
30 import java.util.List;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.junit.jupiter.MockitoExtension;
37 import org.openecomp.sdc.asdctool.migration.core.DBVersion;
38 import org.openecomp.sdc.asdctool.migration.core.task.IMigrationStage;
39 import org.openecomp.sdc.asdctool.migration.core.task.Migration;
40 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
41 import org.openecomp.sdc.asdctool.migration.dao.MigrationTasksDao;
42 import org.openecomp.sdc.asdctool.migration.service.SdcRepoService;
43 import org.openecomp.sdc.be.dao.cassandra.CassandraClient;
44
45 @ExtendWith(MockitoExtension.class)
46 class SpringBeansMigrationResolverTest {
47
48     @InjectMocks
49     private SpringBeansMigrationResolver testInstance;
50
51     @Mock
52     private SdcRepoService sdcRepoServiceMock;
53
54     private List<Migration> migrations = Arrays.asList(createMigration("1710.1"), createMigration("1710.22"),
55         createMigration("1707.12"), createMigration("1710.3"));
56
57     @BeforeEach
58     public void setUp() {
59         testInstance.setMigrations(migrations);
60     }
61
62     @Test
63     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"));
70     }
71
72     @Test
73     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"));
81     }
82
83     @Test
84     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());
90     }
91
92     private Migration createMigration(String version) {
93         return new Migration() {
94             @Override
95             public String description() {
96                 return null;
97             }
98
99             @Override
100             public DBVersion getVersion() {
101                 return DBVersion.fromString(version);
102             }
103
104             @Override
105             public MigrationResult migrate() {
106                 return null;
107             }
108         };
109     }
110
111     private SpringBeansMigrationResolver createTestSubject() {
112         return new SpringBeansMigrationResolver(null, null, new SdcRepoService(new MigrationTasksDao(mock(CassandraClient.class))));
113     }
114 }