Upgrade SDC from Titan to Janus Graph
[sdc.git] / asdctool / src / test / java / org / openecomp / sdc / asdctool / impl / ArtifactUuidFixTest.java
1 package org.openecomp.sdc.asdctool.impl;
2
3 import org.janusgraph.core.JanusGraphVertex;
4 import fj.data.Either;
5 import org.junit.Test;
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;
25
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.ArrayList;
30
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;
35
36 @RunWith(MockitoJUnitRunner.class)
37 public class ArtifactUuidFixTest {
38         @InjectMocks
39         private ArtifactUuidFix test;
40
41         @Mock
42         private JanusGraphDao janusGraphDao;
43
44         @Mock
45         private JanusGraphVertex vertex;
46
47         @Mock
48         ToscaOperationFacade toscaOperationFacade;
49
50         @Mock
51         ArtifactCassandraDao artifactCassandraDao;
52
53         @Mock
54         Service service;
55
56         @Test
57         public void testDoFixVf() {
58                 String fixComponent = "";
59                 String runMode = "";
60                 String uniqueId = "uniqueId";
61                 boolean result;
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));
73
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));
79
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));
84
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);
90                 when(janusGraphDao
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);
113
114                 result = test.doFix(fixComponent, runMode);
115                 assertEquals(true, result);
116         }
117
118         @Test
119         public void testDoFixServiceVf()
120         {
121                 String fixComponent = "distributed_only";
122                 String runMode = "service_vf";
123                 String uniqueId = "uniqueId";
124                 boolean result;
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);
138
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);
147
148                 ComponentInstance componentInstance = new ComponentInstance();
149                 componentInstance.setDeploymentArtifacts(deploymentArtifacts);
150                 componentInstance.setGroupInstances(groupInstances);
151
152                 Service service = new Service();
153                 service.setUniqueId(uniqueId);
154                 List<ComponentInstance> componentInstances = new ArrayList<>();
155                 componentInstances.add(componentInstance);
156                 service.setComponentInstances(componentInstances);
157
158                 when(janusGraphDao
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();
163
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);
170         }
171
172         @Test
173         public void testDoFixOnly()
174         {
175                 String runMode = "fix_only_services";
176                 String fixComponent = "";
177                 boolean result;
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);
192                 when(janusGraphDao
193         .getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, hasProps, hasNotProps, JsonParseFlagEnum.ParseAll)).thenReturn(Either.left(list));
194                 result = test.doFix(fixComponent, runMode);
195                 assertEquals(false,result);
196         }
197 }