Catalog alignment
[sdc.git] / asdctool / src / test / java / org / openecomp / sdc / asdctool / migration / tasks / mig1902 / SdcResourceIconMigrationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 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.tasks.mig1902;
22
23 import fj.data.Either;
24 import org.assertj.core.util.Lists;
25 import org.assertj.core.util.Maps;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.mockito.junit.MockitoJUnitRunner;
32 import org.openecomp.sdc.asdctool.migration.core.task.MigrationResult;
33 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
34 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
35 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
36 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
37 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
38 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
39 import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition;
40 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
41 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
42 import org.openecomp.sdc.be.model.jsonjanusgraph.enums.JsonConstantKeysEnum;
43
44 import java.util.HashMap;
45 import java.util.List;
46
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertFalse;
49 import static org.junit.Assert.assertTrue;
50 import static org.mockito.ArgumentMatchers.anyMap;
51 import static org.mockito.ArgumentMatchers.eq;
52 import static org.mockito.Mockito.any;
53 import static org.mockito.Mockito.doReturn;
54 import static org.mockito.Mockito.times;
55 import static org.mockito.Mockito.verify;
56 import static org.mockito.Mockito.when;
57
58 @RunWith(MockitoJUnitRunner.class)
59 public class SdcResourceIconMigrationTest {
60     @Mock
61     private JanusGraphDao janusGraphDao;
62
63     @Mock
64     private GraphVertex graphVertex;
65
66     @Mock
67     private GraphVertex topologyTemplateVertex;
68
69     @Mock
70     private CompositionDataDefinition compositionDataDefinition;
71
72     @Mock
73     private ComponentInstanceDataDefinition  componentInstanceDataDefinition;
74
75     @InjectMocks
76     private SdcResourceIconMigration iconMigration;
77
78     @Before
79     public void setUp() {
80         iconMigration = new SdcResourceIconMigration(janusGraphDao);
81         when(janusGraphDao.getVertexById(any())).thenReturn(Either.left(topologyTemplateVertex));
82         when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
83     }
84
85
86     @Test
87     public void migrationFailedWhenNoNodeTypeDefined() {
88         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.NODE_TYPE), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseAll)))
89                 .thenReturn(Either.left(Lists.emptyList()));
90         assertEquals(MigrationResult.MigrationStatus.FAILED, iconMigration.migrate().getMigrationStatus());
91     }
92
93     @Test
94     public void resourceIsNotUpdatedIfNotVL() {
95         //iconMigration.handleOneContainer(graphVertex);
96         mockInstancesNotFoundFlow();
97
98         iconMigration.updateNodeTypeIconAndStoreInMap(ResourceTypeEnum.VL);
99         assertFalse(iconMigration.updateIconInsideInstance(componentInstanceDataDefinition));
100     }
101
102
103
104     @Test
105     public void resourceIsUpdatedIfCP() {
106         //iconMigration.handleOneContainer(graphVertex);
107         mockInstancesFoundFlow();
108         when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.left(graphVertex));
109         iconMigration.updateNodeTypeIconAndStoreInMap(ResourceTypeEnum.CP);
110         assertTrue(iconMigration.updateIconInsideInstance(componentInstanceDataDefinition));
111     }
112
113     @Test
114     public void migrateWhenIconsAreUpdated() {
115         mockInstancesFoundFlow();
116         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.TOPOLOGY_TEMPLATE), eq(null), anyMap(), eq(JsonParseFlagEnum.ParseAll)))
117                 .thenReturn(Either.left(Lists.newArrayList(topologyTemplateVertex)));
118         when(compositionDataDefinition.getComponentInstances()).thenReturn(Maps.newHashMap("a", componentInstanceDataDefinition));
119         doReturn(Maps.newHashMap(JsonConstantKeysEnum.COMPOSITION.getValue(), compositionDataDefinition)).when(topologyTemplateVertex).getJson();
120         when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.left(graphVertex));
121
122         assertEquals(MigrationResult.MigrationStatus.COMPLETED, iconMigration.migrate().getMigrationStatus());
123         verify(janusGraphDao, times(3)).commit();
124     }
125
126     @Test
127     public void migrateWhenIconsNotUpdated() {
128         mockInstancesNotFoundFlow();
129         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.TOPOLOGY_TEMPLATE), eq(null), anyMap(), eq(JsonParseFlagEnum.ParseAll)))
130                 .thenReturn(Either.left(Lists.newArrayList(topologyTemplateVertex)));
131         when(compositionDataDefinition.getComponentInstances()).thenReturn(Maps.newHashMap("a", componentInstanceDataDefinition));
132         doReturn(Maps.newHashMap(JsonConstantKeysEnum.COMPOSITION.getValue(), compositionDataDefinition)).when(topologyTemplateVertex).getJson();
133
134         assertEquals(MigrationResult.MigrationStatus.COMPLETED, iconMigration.migrate().getMigrationStatus());
135         verify(janusGraphDao, times(2)).commit();
136     }
137
138     @Test
139     public void migrateWhenNoInstancesFound() {
140         mockInstancesNotFoundFlow();
141         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.TOPOLOGY_TEMPLATE), eq(null), anyMap(), eq(JsonParseFlagEnum.ParseAll)))
142                 .thenReturn(Either.left(Lists.newArrayList(topologyTemplateVertex)));
143
144         assertEquals(MigrationResult.MigrationStatus.COMPLETED, iconMigration.migrate().getMigrationStatus());
145         verify(janusGraphDao, times(2)).commit();
146     }
147
148     // A temp remark for this test - following Commit hash:    08595ad21b0c409c69e3902232f5575963199e3e [ASDC-641] – Migration workaround for deployment artifact timeout. Reviewer: Lior.
149 //    @Test
150 //    public void migrationFailedWhenInstanceVertexUpdateFailed() {
151 //        mockInstancesFoundFlow();
152 //        when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.TOPOLOGY_TEMPLATE), eq(null), anyMap(), eq(JsonParseFlagEnum.ParseAll)))
153 //                .thenReturn(Either.left(Lists.newArrayList(topologyTemplateVertex)));
154 //        when(compositionDataDefinition.getComponentInstances()).thenReturn(Maps.newHashMap("a", componentInstanceDataDefinition));
155 //        doReturn(Maps.newHashMap(JsonConstantKeysEnum.COMPOSITION.getValue(), compositionDataDefinition)).when(topologyTemplateVertex).getJson();
156 //        when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.left(graphVertex))
157 //                .thenReturn(Either.left(graphVertex))
158 //                .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
159 //
160 //        assertEquals(MigrationResult.MigrationStatus.FAILED, iconMigration.migrate().getMigrationStatus());
161 //        verify(janusGraphDao, times(2)).commit();
162 //    }
163
164     @Test
165     public void migrationCompletedWhenVertexJsonIsEmpty() {
166         mockInstancesFoundFlow();
167         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.TOPOLOGY_TEMPLATE), eq(null), anyMap(), eq(JsonParseFlagEnum.ParseAll)))
168                 .thenReturn(Either.left(Lists.newArrayList(topologyTemplateVertex)));
169         doReturn(new HashMap<>()).when(topologyTemplateVertex).getJson();
170         when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.left(graphVertex));
171
172         assertEquals(MigrationResult.MigrationStatus.COMPLETED, iconMigration.migrate().getMigrationStatus());
173         verify(janusGraphDao, times(2)).commit();
174     }
175
176     @Test
177     public void migrationCompletedWhenVertexJsonIsNull() {
178         mockInstancesFoundFlow();
179         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.TOPOLOGY_TEMPLATE), eq(null), anyMap(), eq(JsonParseFlagEnum.ParseAll)))
180                 .thenReturn(Either.left(Lists.newArrayList(topologyTemplateVertex)));
181         doReturn(null).when(topologyTemplateVertex).getJson();
182         when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.left(graphVertex));
183
184         assertEquals(MigrationResult.MigrationStatus.COMPLETED, iconMigration.migrate().getMigrationStatus());
185         verify(janusGraphDao, times(2)).commit();
186     }
187
188     @Test
189     public void migrationFailedWhenTypeUpdateFailed() {
190         mockInstancesFoundFlow();
191         when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR));
192         assertEquals(MigrationResult.MigrationStatus.FAILED, iconMigration.migrate().getMigrationStatus());
193         verify(janusGraphDao, times(0)).commit();
194     }
195
196     private void mockInstancesNotFoundFlow() {
197         List<GraphVertex> nodeTypeVertexList = Lists.newArrayList(graphVertex);
198         when(graphVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn("vl1");
199         when(componentInstanceDataDefinition.getComponentName()).thenReturn("other");
200         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.NODE_TYPE), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseAll)))
201                 .thenReturn(Either.left(nodeTypeVertexList));
202         when(janusGraphDao.updateVertex(any(GraphVertex.class))).thenReturn(Either.left(graphVertex));
203     }
204
205     private void mockInstancesFoundFlow() {
206         when(graphVertex.getMetadataProperty(GraphPropertyEnum.NAME)).thenReturn(String.valueOf("cp1"));
207         when(componentInstanceDataDefinition.getComponentName()).thenReturn("cp1");
208         when(janusGraphDao.getByCriteria(eq(VertexTypeEnum.NODE_TYPE), anyMap(), anyMap(), eq(JsonParseFlagEnum.ParseAll)))
209                 .thenReturn(Either.left(Lists.newArrayList(graphVertex)));
210     }
211
212
213 }