Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-dao / src / test / java / org / openecomp / sdc / be / dao / cassandra / HealingPipelineDaoTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.be.dao.cassandra;
18
19 import com.google.common.collect.ImmutableListMultimap;
20 import org.janusgraph.core.JanusGraphVertex;
21 import org.janusgraph.graphdb.relations.StandardVertexProperty;
22 import org.janusgraph.graphdb.types.system.EmptyVertex;
23 import org.janusgraph.graphdb.types.system.ImplicitKey;
24 import java.util.HashMap;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
28 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
29 import org.openecomp.sdc.be.dao.impl.HealingPipelineDao;
30 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
31 import org.openecomp.sdc.be.dao.jsongraph.heal.*;
32 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
33 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
34 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
35 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
36
37 import java.util.Map;
38 import java.util.Optional;
39
40 import static org.junit.Assert.*;
41
42 public class HealingPipelineDaoTest {
43
44
45     @Test
46     public void shouldUpgrade() {
47         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
48         healingPipelineDao.setHealVersion(3);
49         healingPipelineDao.initHealVersion();
50         healingPipelineDao.initGraphHealers();
51         final HealVersion<Integer> version3 = HealVersionBuilder.build(3);
52         assertFalse(healingPipelineDao.shouldHeal(HealVersionBuilder.build(2), version3));
53         assertFalse(healingPipelineDao.shouldHeal(version3, version3));
54         assertTrue(healingPipelineDao.shouldHeal(HealVersionBuilder.build(2), HealVersionBuilder.build(1)));
55     }
56
57
58     @Test
59     public void testPipelineFilter3Attributes() {
60         // init data
61         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
62         healingPipelineDao.setHealVersion(7);
63         healingPipelineDao.initHealVersion();
64         healingPipelineDao.initGraphHealers();
65         healingPipelineDao.setHealingPipeline(createPipelineMap());
66
67         assertEquals(2,
68                 healingPipelineDao.getHealersForVertex(EdgeLabelEnum.ATTRIBUTES.name(), HealVersionBuilder.build(5)).size());
69
70         GraphVertex graphVertex = new GraphVertex();
71         final int version = 5;
72         graphVertex.addMetadataProperty(GraphPropertyEnum.HEALING_VERSION, Integer.valueOf(version));
73
74         // perform test
75
76         Optional optional = healingPipelineDao.performGraphReadHealing(graphVertex, EdgeLabelEnum.ATTRIBUTES);
77         assertTrue(optional.isPresent());
78         final GraphVertex changedVertex = (GraphVertex) optional.get();
79
80         //validate result
81         final Object healVersion = changedVertex.getMetadataProperties().get(GraphPropertyEnum.HEALING_VERSION);
82         assertNotNull(healVersion);
83         assertTrue(healVersion instanceof Integer);
84         assertEquals(healingPipelineDao.getCurrentHealVersion().getVersion().intValue(), ((Integer) healVersion).intValue());
85     }
86
87     @Test
88     public void testPipelineFilter3AttributesJanusGraphVertex() {
89         // init data
90         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
91         healingPipelineDao.setHealVersion(7);
92         healingPipelineDao.initHealVersion();
93         healingPipelineDao.initGraphHealers();
94         healingPipelineDao.setHealingPipeline(createPipelineMap());
95
96         assertEquals(2,
97                 healingPipelineDao.getHealersForVertex(GraphEdgeLabels.CAPABILITY.getProperty(), HealVersionBuilder.build(5)).size());
98         JanusGraphVertex janusGraphVertex = Mockito.mock(JanusGraphVertex.class);
99         final int version = 5;
100         StandardVertexProperty vertexProperty = new StandardVertexProperty(1, ImplicitKey.ID, new EmptyVertex(), version, (byte) 1);
101         Mockito.when(janusGraphVertex.property(GraphPropertyEnum.HEALING_VERSION.getProperty())).thenReturn(vertexProperty);
102
103         // perform test
104
105         Optional optional = healingPipelineDao.performGraphReadHealing(janusGraphVertex, GraphEdgeLabels.CAPABILITY);
106         assertTrue(optional.isPresent());
107         final JanusGraphVertex changedVertex = (JanusGraphVertex) optional.get();
108
109         //validate result
110         assertNotNull(changedVertex);
111
112     }
113
114     @Test
115     public void testPipelineFilterGenericJanusGraphDao() {
116         // init data
117         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
118         healingPipelineDao.setHealVersion(7);
119         healingPipelineDao.initHealVersion();
120         healingPipelineDao.initGraphHealers();
121         healingPipelineDao.setHealingPipeline(createPipelineMap());
122         assertEquals(1,
123
124                 healingPipelineDao.getHealersForVertex(GraphEdgeLabels.ATTRIBUTE.getProperty(), HealVersionBuilder.build(6)).size());
125
126         GraphNode mockGraphNode = new MockGraphNode(NodeTypeEnum.Attribute);
127         final int version = 5;
128         mockGraphNode.setHealingVersion(Integer.valueOf(version));
129
130         // perform test
131
132         Optional optional = healingPipelineDao.performGraphReadHealing(mockGraphNode,createGraphEdge(GraphEdgeLabels.ATTRIBUTE));
133         assertTrue(optional.isPresent());
134         final GraphNode changedVertex = (GraphNode) optional.get();
135
136         //validate result
137         final Integer healVersion = changedVertex.getHealingVersion();
138         assertNotNull(healVersion);
139         assertEquals(healingPipelineDao.getCurrentHealVersion().getVersion(), healVersion);
140     }
141
142     @Test
143     public void testPipelineFilterJanusGraph1Attributes() {
144         // init data
145         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
146         healingPipelineDao.setHealVersion(7);
147         healingPipelineDao.initHealVersion();
148         healingPipelineDao.initGraphHealers();
149         healingPipelineDao.setHealingPipeline(createPipelineMap());
150
151         assertEquals(2,
152                 healingPipelineDao.getHealersForVertex(GraphEdgeLabels.ATTRIBUTE.getProperty(), HealVersionBuilder.build(5)).size());
153
154     }
155
156     @Test
157     public void healTest() {
158         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
159         healingPipelineDao.setHealVersion(3);
160         healingPipelineDao.initHealVersion();
161         healingPipelineDao.initGraphHealers();
162         final HealVersion<Integer> version3 = HealVersionBuilder.build(3);
163         assertFalse(healingPipelineDao.shouldHeal(HealVersionBuilder.build(2), version3));
164         assertFalse(healingPipelineDao.shouldHeal(version3, version3));
165         assertTrue(healingPipelineDao.shouldHeal(HealVersionBuilder.build(2), HealVersionBuilder.build(1)));
166     }
167
168
169     @Test
170     public void setCurrentVersion() {
171         //init data
172         GraphVertex graphVertex = new GraphVertex();
173         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
174         final int healVersion = 7;
175         healingPipelineDao.setHealVersion(healVersion);
176         healingPipelineDao.initHealVersion();
177         healingPipelineDao.initGraphHealers();
178
179         //execute code
180         healingPipelineDao.setHealingVersion(graphVertex);
181
182         //validate result
183         final Object currentVersion = graphVertex.getMetadataProperties().get(GraphPropertyEnum.HEALING_VERSION);
184         assertNotNull(currentVersion);
185         assertTrue(currentVersion instanceof Integer);
186         assertEquals(healingPipelineDao.getCurrentHealVersion().getVersion().intValue(), ((Integer) currentVersion).intValue());
187     }
188
189     @Test(expected = IllegalStateException.class)
190     public void testMultilistValidation() {
191         // init data
192         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
193         healingPipelineDao.setHealVersion(7);
194         healingPipelineDao.initHealVersion();
195         healingPipelineDao.initGraphHealers();
196
197         ImmutableListMultimap<String, Heal> shouldFail = ImmutableListMultimap.<String, Heal>builder().put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(3))
198                 .put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(4))
199                 .put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(5))
200                 .put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(6))
201                 .put(EdgeLabelEnum.CAPABILITIES.name(), new GraphVertexHealTestMock(3))
202                 .put(EdgeLabelEnum.CAPABILITIES.name(), new GraphVertexHealTestMock(3)) // this should cause exception
203                 .put(EdgeLabelEnum.CAPABILITIES.name(), new GraphVertexHealTestMock(69)).build();
204
205         //performTest
206         healingPipelineDao.setHealingPipeline(shouldFail);
207     }
208
209     private ImmutableListMultimap<String, Heal> createPipelineMap() {
210         return ImmutableListMultimap.<String, Heal>builder().put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(3))
211                 .put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(4))
212                 .put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(5))
213                 .put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(6))
214                 .put(EdgeLabelEnum.CAPABILITIES.name(), new GraphVertexHealTestMock(3))
215                 .put(EdgeLabelEnum.CAPABILITIES.name(), new GraphVertexHealTestMock(6))
216                 .put(EdgeLabelEnum.CAPABILITIES.name(), new GraphVertexHealTestMock(69))
217                 .put(GraphEdgeLabels.ATTRIBUTE.getProperty(), new GraphNodeHealTestMock(4))
218                 .put(GraphEdgeLabels.ATTRIBUTE.getProperty(), new GraphNodeHealTestMock(5))
219                 .put(GraphEdgeLabels.ATTRIBUTE.getProperty(), new GraphNodeHealTestMock(6))
220                 .put(GraphEdgeLabels.CAPABILITY.getProperty(), new JanusGraphVertexHealTestMock(4))
221                 .put(GraphEdgeLabels.CAPABILITY.getProperty(), new JanusGraphVertexHealTestMock(5))
222                 .put(GraphEdgeLabels.CAPABILITY.getProperty(), new JanusGraphVertexHealTestMock(6)).build();
223     }
224
225     public GraphEdge createGraphEdge(GraphEdgeLabels graphEdgeLabels){
226         return new GraphEdge(graphEdgeLabels, new HashMap<>());
227     }
228
229
230     private class GraphVertexHealTestMock extends AbstractGraphVertexHeal {
231
232         private HealVersion healVersion;
233
234         public GraphVertexHealTestMock(int i) {
235             healVersion = HealVersionBuilder.build(i);
236         }
237
238         @Override
239         public HealVersion fromVersion() {
240             return healVersion;
241         }
242
243         @Override
244         public void healData(GraphVertex parentVertex) {
245
246         }
247
248     }
249
250     private class GraphNodeHealTestMock extends AbstractJanusGraphVertexHeal {
251         private HealVersion healVersion;
252
253         public GraphNodeHealTestMock(int i) {
254             healVersion = HealVersionBuilder.build(i);
255         }
256
257         @Override
258         public HealVersion fromVersion() {
259             return healVersion;
260         }
261
262         @Override
263         public void healData(GraphNode parentV) {
264
265         }
266     }
267
268
269     private class JanusGraphVertexHealTestMock implements Heal<JanusGraphVertex> {
270         private HealVersion healVersion;
271
272         public JanusGraphVertexHealTestMock(int i) {
273             healVersion = HealVersionBuilder.build(i);
274         }
275
276         @Override
277         public HealVersion fromVersion() {
278             return healVersion;
279         }
280
281         @Override
282         public void healData(JanusGraphVertex parentV) {
283
284         }
285     }
286
287     private class MockGraphNode extends GraphNode {
288         private int healVersion;
289
290         public MockGraphNode(NodeTypeEnum label) {
291             super(label);
292         }
293
294         @Override
295         public String getUniqueId() {
296             return null;
297         }
298
299         @Override
300         public Map<String, Object> toGraphMap() {
301             return null;
302         }
303
304         @Override
305         public Integer getHealingVersion() {
306             return healVersion;
307         }
308
309         @Override
310         public void setHealingVersion(Integer version) {
311             this.healVersion = version;
312         }
313     }
314
315 }