Added oparent to sdc main
[sdc.git] / asdctool / src / test / java / org / openecomp / sdc / asdctool / migration / core / SdcMigrationToolTest.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.core;
22
23 import org.mockito.InjectMocks;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.MockitoAnnotations;
27 import org.mockito.internal.verification.Times;
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.resolver.MigrationResolver;
31 import org.openecomp.sdc.asdctool.migration.service.SdcRepoService;
32 import org.testng.annotations.BeforeMethod;
33 import org.testng.annotations.Test;
34
35 import java.util.Arrays;
36 import java.util.Collections;
37
38 import static org.mockito.Mockito.*;
39
40 public class SdcMigrationToolTest {
41
42         @InjectMocks
43         private SdcMigrationTool testInstance = spy(SdcMigrationTool.class);
44
45         @Mock
46         private MigrationResolver migrationResolverMock;
47
48         @Mock
49         private SdcRepoService sdcRepoServiceMock;
50
51         @BeforeMethod
52     public void setUp() {
53                 MockitoAnnotations.initMocks(this);
54         }
55
56         @Test
57     public void testMigrate_noMigrations() {
58                 when(migrationResolverMock.resolveMigrations()).thenReturn(Collections.emptyList());
59                 testInstance.migrate(false);
60                 verify(sdcRepoServiceMock, new Times(0)).clearTasksForCurrentMajor();
61                 verify(sdcRepoServiceMock, new Times(0)).createMigrationTask(Mockito.any());
62         }
63
64         @Test
65     public void testMigrate_enforceFlag_removeAllMigrationTasksForCurrentVersion() {
66                 when(migrationResolverMock.resolveMigrations()).thenReturn(Collections.emptyList());
67                 testInstance.migrate(true);
68                 verify(sdcRepoServiceMock, new Times(1)).clearTasksForCurrentMajor();
69         }
70
71         @Test
72     public void testMigrate_stopAfterFirstFailure() {
73                 when(migrationResolverMock.resolveMigrations())
74                                 .thenReturn(Arrays.asList(new SuccessfulMigration(), new FailedMigration(), new SuccessfulMigration()));
75                 testInstance.migrate(false);
76                 verify(sdcRepoServiceMock, new Times(0)).clearTasksForCurrentMajor();
77                 verify(sdcRepoServiceMock, new Times(1)).createMigrationTask(Mockito.any());
78
79         }
80
81         private class FailedMigration implements Migration {
82
83                 @Override
84                 public String description() {
85                         return null;
86                 }
87
88                 @Override
89                 public DBVersion getVersion() {
90                         return DBVersion.fromString("1710.22");
91                 }
92
93                 @Override
94                 public MigrationResult migrate() {
95                         MigrationResult migrationResult = new MigrationResult();
96                         migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.FAILED);
97                         return migrationResult;
98                 }
99         }
100
101         private class SuccessfulMigration implements Migration {
102
103                 @Override
104                 public String description() {
105                         return null;
106                 }
107
108                 @Override
109                 public DBVersion getVersion() {
110                         return DBVersion.fromString("1710.22");
111                 }
112
113                 @Override
114                 public MigrationResult migrate() {
115                         MigrationResult migrationResult = new MigrationResult();
116                         migrationResult.setMigrationStatus(MigrationResult.MigrationStatus.COMPLETED);
117                         return migrationResult;
118                 }
119         }
120 }