Added oparent to sdc main
[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 org.janusgraph.core.JanusGraphVertex;
24 import fj.data.Either;
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.junit.MockitoJUnitRunner;
31 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
32 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
33 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
34 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
35 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
36 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
37 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
38 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
39 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
40 import org.openecomp.sdc.be.model.*;
41 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
42 import org.openecomp.sdc.be.resources.data.ESArtifactData;
43 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
44 import org.openecomp.sdc.common.api.Constants;
45
46 import java.util.HashMap;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.ArrayList;
50
51 import static org.junit.Assert.assertEquals;
52 import static org.mockito.ArgumentMatchers.any;
53 import static org.mockito.ArgumentMatchers.anyString;
54 import static org.mockito.Mockito.when;
55
56 @RunWith(MockitoJUnitRunner.class)
57 public class ArtifactUuidFixTest {
58         @InjectMocks
59         private ArtifactUuidFix test;
60
61         @Mock
62         private JanusGraphDao janusGraphDao;
63
64         @Mock
65         private JanusGraphVertex vertex;
66
67         @Mock
68         ToscaOperationFacade toscaOperationFacade;
69
70         @Mock
71         ArtifactCassandraDao artifactCassandraDao;
72
73         @Mock
74         Service service;
75
76         @Test
77         public void testDoFixVf() {
78                 String fixComponent = "";
79                 String runMode = "";
80                 String uniqueId = "uniqueId";
81                 boolean result;
82                 fixComponent = "vf_only";
83                 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
84                 hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
85                 hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
86                 List<GraphVertex> list = new ArrayList<>();
87                 GraphVertex graphVertex = new GraphVertex();
88                 graphVertex.setVertex(vertex);
89                 graphVertex.setUniqueId(uniqueId);
90                 graphVertex.setMetadataProperties(hasProps1);
91                 list.add(graphVertex);
92                 when(janusGraphDao.getByCriteria(VertexTypeEnum.NODE_TYPE, hasProps1)).thenReturn(Either.left(list));
93
94                 Map<GraphPropertyEnum, Object> hasProps2 = new HashMap<>();
95                 hasProps2.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
96                 hasProps2.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF);
97                 hasProps2.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
98                 when(janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps2)).thenReturn(Either.left(list));
99
100                 Map<GraphPropertyEnum, Object> hasProps3 = new HashMap<>();
101                 hasProps3.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
102                 hasProps3.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
103                 when(janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps3)).thenReturn(Either.left(list));
104
105                 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
106                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
107                 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name());
108                 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
109                 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
110                 when(janusGraphDao
111         .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
112                 Resource resource = new Resource();
113                 resource.setName(uniqueId);
114                 Map<String, ArtifactDefinition> deployArtifact = new HashMap<>();
115                 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
116                 artifactDefinition.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.getType());
117                 artifactDefinition.setUniqueId("one.two");
118                 artifactDefinition.setArtifactUUID("one.two");
119                 deployArtifact.put("two", artifactDefinition);
120                 resource.setDeploymentArtifacts(deployArtifact);
121                 List<GroupDefinition> groups = new ArrayList<>();
122                 GroupDefinition groupDefinition = new GroupDefinition();
123                 groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
124                 List<String> artifacts = new ArrayList<>();
125                 artifacts.add("one.two");
126                 groupDefinition.setArtifacts(artifacts);
127                 groupDefinition.setArtifactsUuid(artifacts);
128                 groups.add(groupDefinition);
129                 resource.setGroups(groups);
130                 when(toscaOperationFacade.getToscaElement(graphVertex.getUniqueId())).thenReturn(Either.left(resource));
131                 List<Service> serviceList = new ArrayList<>();
132                 serviceList.add(service);
133
134                 result = test.doFix(fixComponent, runMode);
135                 assertEquals(true, result);
136         }
137
138         @Test
139         public void testDoFixServiceVf()
140         {
141                 String fixComponent = "distributed_only";
142                 String runMode = "service_vf";
143                 String uniqueId = "uniqueId";
144                 boolean result;
145                 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
146                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
147                 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
148                 hasProps.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTED.name());
149                 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
150                 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
151                 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
152                 hasProps1.put(GraphPropertyEnum.NAME, "name");
153                 List<GraphVertex> list = new ArrayList<>();
154                 GraphVertex graphVertex = new GraphVertex();
155                 graphVertex.setUniqueId(uniqueId);
156                 graphVertex.setMetadataProperties(hasProps1);
157                 list.add(graphVertex);
158
159                 Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
160                 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
161                 artifactDefinition.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.name());
162                 artifactDefinition.setEsId("esID");
163                 deploymentArtifacts.put("1",artifactDefinition);
164                 List<GroupInstance> groupInstances = new ArrayList<>();
165                 GroupInstance groupInstance = new GroupInstance();
166                 groupInstances.add(groupInstance);
167
168                 ComponentInstance componentInstance = new ComponentInstance();
169                 componentInstance.setDeploymentArtifacts(deploymentArtifacts);
170                 componentInstance.setGroupInstances(groupInstances);
171
172                 Service service = new Service();
173                 service.setUniqueId(uniqueId);
174                 List<ComponentInstance> componentInstances = new ArrayList<>();
175                 componentInstances.add(componentInstance);
176                 service.setComponentInstances(componentInstances);
177
178                 when(janusGraphDao
179         .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
180                 when(toscaOperationFacade.getToscaElement(ArgumentMatchers.eq(graphVertex.getUniqueId()),any(ComponentParametersView.class)))
181                                 .thenReturn(Either.left(service));
182                 byte[] payload = "value".getBytes();
183
184                 ESArtifactData esArtifactData =new ESArtifactData();
185                 esArtifactData.setDataAsArray(payload);
186                 Either<ESArtifactData, CassandraOperationStatus> artifactfromESres = Either.left(esArtifactData);
187                 when(artifactCassandraDao.getArtifact(anyString())).thenReturn(artifactfromESres);
188                 result = test.doFix(fixComponent, runMode);
189                 assertEquals(false, result);
190         }
191
192         @Test
193         public void testDoFixOnly()
194         {
195                 String runMode = "fix_only_services";
196                 String fixComponent = "";
197                 boolean result;
198                 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
199                 hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
200                 hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
201                 List<GraphVertex> list = new ArrayList<>();
202                 GraphVertex graphVertex = new GraphVertex();
203                 graphVertex.setVertex(vertex);
204                 graphVertex.setUniqueId("uniqueId");
205                 graphVertex.setMetadataProperties(hasProps1);
206                 graphVertex.addMetadataProperty(GraphPropertyEnum.NAME, "name");
207                 list.add(graphVertex);
208                 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
209                 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
210                 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
211                 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
212                 when(janusGraphDao
213         .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
214                 result = test.doFix(fixComponent, runMode);
215                 assertEquals(false,result);
216         }
217 }