1 package org.openecomp.sdc.asdctool.impl;
3 import org.janusgraph.core.JanusGraphVertex;
6 import org.junit.runner.RunWith;
7 import org.mockito.ArgumentMatchers;
8 import org.mockito.InjectMocks;
9 import org.mockito.Mock;
10 import org.mockito.junit.MockitoJUnitRunner;
11 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
12 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
13 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
14 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
15 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
16 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
17 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
18 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
19 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
20 import org.openecomp.sdc.be.model.*;
21 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
22 import org.openecomp.sdc.be.resources.data.ESArtifactData;
23 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
24 import org.openecomp.sdc.common.api.Constants;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.ArrayList;
31 import static org.junit.Assert.assertEquals;
32 import static org.mockito.ArgumentMatchers.any;
33 import static org.mockito.ArgumentMatchers.anyString;
34 import static org.mockito.Mockito.when;
36 @RunWith(MockitoJUnitRunner.class)
37 public class ArtifactUuidFixTest {
39 private ArtifactUuidFix test;
42 private JanusGraphDao janusGraphDao;
45 private JanusGraphVertex vertex;
48 ToscaOperationFacade toscaOperationFacade;
51 ArtifactCassandraDao artifactCassandraDao;
57 public void testDoFixVf() {
58 String fixComponent = "";
60 String uniqueId = "uniqueId";
62 fixComponent = "vf_only";
63 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
64 hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
65 hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
66 List<GraphVertex> list = new ArrayList<>();
67 GraphVertex graphVertex = new GraphVertex();
68 graphVertex.setVertex(vertex);
69 graphVertex.setUniqueId(uniqueId);
70 graphVertex.setMetadataProperties(hasProps1);
71 list.add(graphVertex);
72 when(janusGraphDao.getByCriteria(VertexTypeEnum.NODE_TYPE, hasProps1)).thenReturn(Either.left(list));
74 Map<GraphPropertyEnum, Object> hasProps2 = new HashMap<>();
75 hasProps2.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
76 hasProps2.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF);
77 hasProps2.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
78 when(janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps2)).thenReturn(Either.left(list));
80 Map<GraphPropertyEnum, Object> hasProps3 = new HashMap<>();
81 hasProps3.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
82 hasProps3.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
83 when(janusGraphDao.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps3)).thenReturn(Either.left(list));
85 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
86 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
87 hasProps.put(GraphPropertyEnum.RESOURCE_TYPE, ResourceTypeEnum.VF.name());
88 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
89 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
91 .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
92 Resource resource = new Resource();
93 resource.setName(uniqueId);
94 Map<String, ArtifactDefinition> deployArtifact = new HashMap<>();
95 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
96 artifactDefinition.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.getType());
97 artifactDefinition.setUniqueId("one.two");
98 artifactDefinition.setArtifactUUID("one.two");
99 deployArtifact.put("two", artifactDefinition);
100 resource.setDeploymentArtifacts(deployArtifact);
101 List<GroupDefinition> groups = new ArrayList<>();
102 GroupDefinition groupDefinition = new GroupDefinition();
103 groupDefinition.setType(Constants.DEFAULT_GROUP_VF_MODULE);
104 List<String> artifacts = new ArrayList<>();
105 artifacts.add("one.two");
106 groupDefinition.setArtifacts(artifacts);
107 groupDefinition.setArtifactsUuid(artifacts);
108 groups.add(groupDefinition);
109 resource.setGroups(groups);
110 when(toscaOperationFacade.getToscaElement(graphVertex.getUniqueId())).thenReturn(Either.left(resource));
111 List<Service> serviceList = new ArrayList<>();
112 serviceList.add(service);
114 result = test.doFix(fixComponent, runMode);
115 assertEquals(true, result);
119 public void testDoFixServiceVf()
121 String fixComponent = "distributed_only";
122 String runMode = "service_vf";
123 String uniqueId = "uniqueId";
125 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
126 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
127 hasProps.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
128 hasProps.put(GraphPropertyEnum.DISTRIBUTION_STATUS, DistributionStatusEnum.DISTRIBUTED.name());
129 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
130 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
131 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
132 hasProps1.put(GraphPropertyEnum.NAME, "name");
133 List<GraphVertex> list = new ArrayList<>();
134 GraphVertex graphVertex = new GraphVertex();
135 graphVertex.setUniqueId(uniqueId);
136 graphVertex.setMetadataProperties(hasProps1);
137 list.add(graphVertex);
139 Map<String, ArtifactDefinition> deploymentArtifacts = new HashMap<>();
140 ArtifactDefinition artifactDefinition = new ArtifactDefinition();
141 artifactDefinition.setArtifactType(ArtifactTypeEnum.VF_MODULES_METADATA.name());
142 artifactDefinition.setEsId("esID");
143 deploymentArtifacts.put("1",artifactDefinition);
144 List<GroupInstance> groupInstances = new ArrayList<>();
145 GroupInstance groupInstance = new GroupInstance();
146 groupInstances.add(groupInstance);
148 ComponentInstance componentInstance = new ComponentInstance();
149 componentInstance.setDeploymentArtifacts(deploymentArtifacts);
150 componentInstance.setGroupInstances(groupInstances);
152 Service service = new Service();
153 service.setUniqueId(uniqueId);
154 List<ComponentInstance> componentInstances = new ArrayList<>();
155 componentInstances.add(componentInstance);
156 service.setComponentInstances(componentInstances);
159 .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
160 when(toscaOperationFacade.getToscaElement(ArgumentMatchers.eq(graphVertex.getUniqueId()),any(ComponentParametersView.class)))
161 .thenReturn(Either.left(service));
162 byte[] payload = "value".getBytes();
164 ESArtifactData esArtifactData =new ESArtifactData();
165 esArtifactData.setDataAsArray(payload);
166 Either<ESArtifactData, CassandraOperationStatus> artifactfromESres = Either.left(esArtifactData);
167 when(artifactCassandraDao.getArtifact(anyString())).thenReturn(artifactfromESres);
168 result = test.doFix(fixComponent, runMode);
169 assertEquals(false, result);
173 public void testDoFixOnly()
175 String runMode = "fix_only_services";
176 String fixComponent = "";
178 Map<GraphPropertyEnum, Object> hasProps1 = new HashMap<>();
179 hasProps1.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.RESOURCE.name());
180 hasProps1.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
181 List<GraphVertex> list = new ArrayList<>();
182 GraphVertex graphVertex = new GraphVertex();
183 graphVertex.setVertex(vertex);
184 graphVertex.setUniqueId("uniqueId");
185 graphVertex.setMetadataProperties(hasProps1);
186 graphVertex.addMetadataProperty(GraphPropertyEnum.NAME, "name");
187 list.add(graphVertex);
188 Map<GraphPropertyEnum, Object> hasProps = new HashMap<>();
189 hasProps.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
190 Map<GraphPropertyEnum, Object> hasNotProps = new HashMap<>();
191 hasNotProps.put(GraphPropertyEnum.IS_DELETED, true);
193 .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
194 result = test.doFix(fixComponent, runMode);
195 assertEquals(false,result);