Catalog alignment
[sdc.git] / asdctool / src / test / java / org / openecomp / sdc / asdctool / impl / ArtifactUuidFixTest.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.impl;
22
23 import fj.data.Either;
24 import org.janusgraph.core.JanusGraphVertex;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.ArgumentMatchers;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.junit.MockitoJUnitRunner;
32 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
33 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
34 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
35 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
36 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
37 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
38 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
39 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
40 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
41 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
42 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
43 import org.openecomp.sdc.be.model.ArtifactDefinition;
44 import org.openecomp.sdc.be.model.Component;
45 import org.openecomp.sdc.be.model.ComponentInstance;
46 import org.openecomp.sdc.be.model.ComponentParametersView;
47 import org.openecomp.sdc.be.model.DistributionStatusEnum;
48 import org.openecomp.sdc.be.model.GroupDefinition;
49 import org.openecomp.sdc.be.model.GroupInstance;
50 import org.openecomp.sdc.be.model.LifecycleStateEnum;
51 import org.openecomp.sdc.be.model.Resource;
52 import org.openecomp.sdc.be.model.Service;
53 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
54 import org.openecomp.sdc.be.resources.data.DAOArtifactData;
55 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
56 import org.openecomp.sdc.common.api.Constants;
57
58 import java.nio.ByteBuffer;
59 import java.util.ArrayList;
60 import java.util.HashMap;
61 import java.util.List;
62 import java.util.Map;
63
64 import static org.junit.Assert.assertEquals;
65 import static org.mockito.ArgumentMatchers.any;
66 import static org.mockito.ArgumentMatchers.anyString;
67 import static org.mockito.Mockito.when;
68
69 @RunWith(MockitoJUnitRunner.class)
70 public class ArtifactUuidFixTest {
71         @InjectMocks
72         private ArtifactUuidFix test;
73
74         @Mock
75         private Component component;
76
77         @Mock
78         private JanusGraphDao janusGraphDao;
79
80         @Mock
81         private JanusGraphVertex vertex;
82
83         @Mock
84         ToscaOperationFacade toscaOperationFacade;
85
86         @Mock
87         ArtifactCassandraDao artifactCassandraDao;
88
89         @Mock
90         Service service;
91
92         @Test
93         public void testDoFixVf() {
94                 String fixComponent = "";
95                 String runMode = "";
96                 String uniqueId = "uniqueId";
97                 boolean result;
98                 fixComponent = "vf_only";
99                 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
100                 hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
101                 hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
102                 List<GraphVertex> list = new ArrayList<>();
103                 GraphVertex graphVertex = new GraphVertex();
104                 graphVertex.setVertex(vertex);
105                 graphVertex.setUniqueId(uniqueId);
106                 graphVertex.setMetadataProperties(hasProps1);
107                 list.add(graphVertex);
108                 when(janusGraphDao.getByCriteria(VertexTypeEnum.NODE_TYPE, hasProps1)).thenReturn(Either.left(list));
109
110                 Map<GraphPropertyEnum, Object> hasProps2 = new HashMap<>();
111                 hasProps2.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
112                 hasProps2.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF);
113                 hasProps2.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
114                 when(janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps2)).thenReturn(Either.left(list));
115
116                 Map<GraphPropertyEnum, Object> hasProps3 = new HashMap<>();
117                 hasProps3.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
118                 hasProps3.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
119                 when(janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps3)).thenReturn(Either.left(list));
120
121                 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
122                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
123                 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name());
124                 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
125                 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
126                 when(janusGraphDao
127         .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
128                 Resource resource = new Resource();
129                 resource.setName(uniqueId);
130                 Map<String, ArtifactDefinition> deployArtifact = new HashMap<>();
131                 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
132                 artifactDefinition.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.getType());
133                 artifactDefinition.setUniqueId("one.two");
134                 artifactDefinition.setArtifactUUID("one.two");
135                 deployArtifact.put("two", artifactDefinition);
136                 resource.setDeploymentArtifacts(deployArtifact);
137                 List<GroupDefinition> groups = new ArrayList<>();
138                 GroupDefinition groupDefinition = new GroupDefinition();
139                 groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
140                 List<String> artifacts = new ArrayList<>();
141                 artifacts.add("one.two");
142                 groupDefinition.setArtifacts(artifacts);
143                 groupDefinition.setArtifactsUuid(artifacts);
144                 groups.add(groupDefinition);
145                 resource.setGroups(groups);
146                 when(toscaOperationFacade.getToscaElement(graphVertex.getUniqueId())).thenReturn(Either.left(resource));
147                 List<Service> serviceList = new ArrayList<>();
148                 serviceList.add(service);
149
150                 result = test.doFix(fixComponent, runMode);
151                 assertEquals(true, result);
152         }
153
154         @Test
155         public void testDoFixServiceVf()
156         {
157                 String fixComponent = "distributed_only";
158                 String runMode = "service_vf";
159                 String uniqueId = "uniqueId";
160                 boolean result;
161                 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
162                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
163                 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
164                 hasProps.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTED.name());
165                 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
166                 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
167                 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
168                 hasProps1.put(GraphPropertyEnum.NAME, "name");
169                 List<GraphVertex> list = new ArrayList<>();
170                 GraphVertex graphVertex = new GraphVertex();
171                 graphVertex.setUniqueId(uniqueId);
172                 graphVertex.setMetadataProperties(hasProps1);
173                 list.add(graphVertex);
174
175                 Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
176                 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
177                 artifactDefinition.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.name());
178                 artifactDefinition.setEsId("esID");
179                 deploymentArtifacts.put("1",artifactDefinition);
180                 List<GroupInstance> groupInstances = new ArrayList<>();
181                 GroupInstance groupInstance = new GroupInstance();
182                 groupInstances.add(groupInstance);
183
184                 ComponentInstance componentInstance = new ComponentInstance();
185                 componentInstance.setDeploymentArtifacts(deploymentArtifacts);
186                 componentInstance.setGroupInstances(groupInstances);
187
188                 Service service = new Service();
189                 service.setUniqueId(uniqueId);
190                 List<ComponentInstance> componentInstances = new ArrayList<>();
191                 componentInstances.add(componentInstance);
192                 service.setComponentInstances(componentInstances);
193
194                 when(janusGraphDao
195         .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
196                 when(toscaOperationFacade.getToscaElement(ArgumentMatchers.eq(graphVertex.getUniqueId()),any(ComponentParametersView.class)))
197                                 .thenReturn(Either.left(service));
198
199                 DAOArtifactData artifactData = new DAOArtifactData();
200                 byte[] data = "value".getBytes();
201                 ByteBuffer bufferData = ByteBuffer.wrap(data);
202                 artifactData.setData(bufferData);
203
204                 Either<DAOArtifactData, CassandraOperationStatus> artifactfromESres = Either.left(artifactData);
205                 when(artifactCassandraDao.getArtifact(anyString())).thenReturn(artifactfromESres);
206                 result = test.doFix(fixComponent, runMode);
207                 assertEquals(false, result);
208         }
209
210         @Test
211         public void testDoFixOnly()
212         {
213                 String runMode = "fix_only_services";
214                 String fixComponent = "";
215                 boolean result;
216                 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
217                 hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
218                 hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
219                 List<GraphVertex> list = new ArrayList<>();
220                 GraphVertex graphVertex = new GraphVertex();
221                 graphVertex.setVertex(vertex);
222                 graphVertex.setUniqueId("uniqueId");
223                 graphVertex.setMetadataProperties(hasProps1);
224                 graphVertex.addMetadataProperty(GraphPropertyEnum.NAME, "name");
225                 list.add(graphVertex);
226                 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
227                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
228                 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
229                 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
230                 when(janusGraphDao
231         .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
232                 result = test.doFix(fixComponent, runMode);
233                 assertEquals(false,result);
234         }
235
236
237         @Test
238         public void testDoFixVfWithFixMode() {
239                 String fixComponent = "";
240                 String runMode = "fix";
241                 String uniqueId = "uniqueId";
242                 boolean result;
243                 fixComponent = "vf_only";
244                 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
245                 hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
246                 hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
247                 List<GraphVertex> list = new ArrayList<>();
248                 GraphVertex graphVertex = new GraphVertex();
249                 graphVertex.setVertex(vertex);
250                 graphVertex.setUniqueId(uniqueId);
251                 graphVertex.setMetadataProperties(hasProps1);
252                 list.add(graphVertex);
253                 when(janusGraphDao.getByCriteria(VertexTypeEnum.NODE_TYPE, hasProps1)).thenReturn(Either.left(list));
254
255                 Map<GraphPropertyEnum, Object> hasProps2 = new HashMap<>();
256                 hasProps2.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
257                 hasProps2.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF);
258                 hasProps2.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
259                 when(janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps2)).thenReturn(Either.left(list));
260
261                 Map<GraphPropertyEnum, Object> hasProps3 = new HashMap<>();
262                 hasProps3.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
263                 hasProps3.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
264                 when(janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps3)).thenReturn(Either.left(list));
265
266                 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
267                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
268                 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name());
269                 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
270                 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
271                 when(janusGraphDao
272                         .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
273                 Resource resource = new Resource();
274                 resource.setName(uniqueId);
275                 Map<String, ArtifactDefinition> deployArtifact = new HashMap<>();
276                 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
277                 artifactDefinition.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.getType());
278                 artifactDefinition.setUniqueId("one.two");
279                 artifactDefinition.setArtifactUUID("one.two");
280                 deployArtifact.put("two", artifactDefinition);
281                 resource.setDeploymentArtifacts(deployArtifact);
282                 List<GroupDefinition> groups = new ArrayList<>();
283                 GroupDefinition groupDefinition = new GroupDefinition();
284                 groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
285                 List<String> artifacts = new ArrayList<>();
286                 artifacts.add("one.two");
287                 groupDefinition.setArtifacts(artifacts);
288                 groupDefinition.setArtifactsUuid(artifacts);
289                 groups.add(groupDefinition);
290                 resource.setGroups(groups);
291                 resource.setUniqueId(uniqueId);
292                 resource.setComponentType(ComponentTypeEnum.SERVICE_INSTANCE);
293
294                 when(toscaOperationFacade.getToscaElement(graphVertex.getUniqueId())).thenReturn(Either.left(resource));
295                 when(toscaOperationFacade.getToscaFullElement(Mockito.anyString())).thenReturn(Either.left(component));
296                 when(component.getUniqueId()).thenReturn(uniqueId);
297                 when(toscaOperationFacade.getToscaElement(Mockito.anyString(), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(component));
298                 when(janusGraphDao.getVertexById(uniqueId, JsonParseFlagEnum.NoParse)).thenReturn(Either.left(graphVertex));
299                 when(janusGraphDao.getChildVertex(graphVertex, EdgeLabelEnum.GROUPS, JsonParseFlagEnum.ParseJson)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
300                 when(janusGraphDao.getChildVertex(graphVertex, EdgeLabelEnum.DEPLOYMENT_ARTIFACTS, JsonParseFlagEnum.ParseJson)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
301                 when(janusGraphDao.getChildVertex(graphVertex, EdgeLabelEnum.TOSCA_ARTIFACTS, JsonParseFlagEnum.ParseJson)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
302
303                 List<Service> serviceList = new ArrayList<>();
304                 serviceList.add(service);
305
306                 result = test.doFix(fixComponent, runMode);
307                 assertEquals(true, result);
308         }
309
310 }