Added oparent to sdc main
[sdc.git] / asdctool / src / test / java / org / openecomp / sdc / asdctool / migration / resolver / SpringBeansMigrationResolverTest.java
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 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;
35
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.List;
39
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;
44
45 public class SpringBeansMigrationResolverTest {
46
47         @InjectMocks
48         private SpringBeansMigrationResolver testInstance;
49
50         @Mock
51         private SdcRepoService sdcRepoServiceMock;
52
53         private List<Migration> migrations = Arrays.asList(createMigration("1710.1"), createMigration("1710.22"),
54                         createMigration("1707.12"), createMigration("1710.3"));
55
56         @BeforeMethod
57     public void setUp() {
58                 MockitoAnnotations.initMocks(this);
59                 testInstance.setMigrations(migrations);
60         }
61
62         @Test
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"));
70         }
71
72         @Test
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"));
81         }
82
83         @Test
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());
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 }