Toggle
[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.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
30 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
31 import org.openecomp.sdc.be.dao.impl.HealingPipelineDao;
32 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
33 import org.openecomp.sdc.be.dao.jsongraph.heal.*;
34 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
35 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
36 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
37 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
38
39 import java.util.Map;
40 import java.util.Optional;
41 import org.openecomp.sdc.be.togglz.ToggleableFeature;
42 import org.togglz.testing.TestFeatureManager;
43 import org.togglz.testing.TestFeatureManagerProvider;
44
45 import static org.junit.Assert.*;
46
47 public class HealingPipelineDaoTest {
48
49     @Before
50     public void enableToggleableFeatures(){
51         TestFeatureManager manager = new TestFeatureManager(ToggleableFeature.class);
52         manager.enableAll();
53         TestFeatureManagerProvider.setFeatureManager(manager);
54     }
55
56
57     @Test
58     public void shouldUpgrade() {
59         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
60         healingPipelineDao.setHealVersion(3);
61         healingPipelineDao.initHealVersion();
62         healingPipelineDao.initGraphHealers();
63         final HealVersion<Integer> version3 = HealVersionBuilder.build(3);
64         assertFalse(healingPipelineDao.shouldHeal(HealVersionBuilder.build(2), version3));
65         assertFalse(healingPipelineDao.shouldHeal(version3, version3));
66         assertTrue(healingPipelineDao.shouldHeal(HealVersionBuilder.build(2), HealVersionBuilder.build(1)));
67     }
68
69
70     @Test
71     public void testPipelineFilter3Attributes() {
72         // init data
73         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
74         healingPipelineDao.setHealVersion(7);
75         healingPipelineDao.initHealVersion();
76         healingPipelineDao.initGraphHealers();
77         healingPipelineDao.setHealingPipeline(createPipelineMap());
78
79         assertEquals(2,
80                 healingPipelineDao.getHealersForVertex(EdgeLabelEnum.ATTRIBUTES.name(), HealVersionBuilder.build(5)).size());
81
82         GraphVertex graphVertex = new GraphVertex();
83         final int version = 5;
84         graphVertex.addMetadataProperty(GraphPropertyEnum.HEALING_VERSION, Integer.valueOf(version));
85
86         // perform test
87
88         Optional optional = healingPipelineDao.performGraphReadHealing(graphVertex, EdgeLabelEnum.ATTRIBUTES);
89         assertTrue(optional.isPresent());
90         final GraphVertex changedVertex = (GraphVertex) optional.get();
91
92         //validate result
93         final Object healVersion = changedVertex.getMetadataProperties().get(GraphPropertyEnum.HEALING_VERSION);
94         assertNotNull(healVersion);
95         assertTrue(healVersion instanceof Integer);
96         assertEquals(healingPipelineDao.getCurrentHealVersion().getVersion().intValue(), ((Integer) healVersion).intValue());
97     }
98
99     @Test
100     public void testPipelineFilter3AttributesJanusGraphVertex() {
101         // init data
102         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
103         healingPipelineDao.setHealVersion(7);
104         healingPipelineDao.initHealVersion();
105         healingPipelineDao.initGraphHealers();
106         healingPipelineDao.setHealingPipeline(createPipelineMap());
107
108         assertEquals(2,
109                 healingPipelineDao.getHealersForVertex(GraphEdgeLabels.CAPABILITY.getProperty(), HealVersionBuilder.build(5)).size());
110         JanusGraphVertex janusGraphVertex = Mockito.mock(JanusGraphVertex.class);
111         final int version = 5;
112         StandardVertexProperty vertexProperty = new StandardVertexProperty(1, ImplicitKey.ID, new EmptyVertex(), version, (byte) 1);
113         Mockito.when(janusGraphVertex.property(GraphPropertyEnum.HEALING_VERSION.getProperty())).thenReturn(vertexProperty);
114
115         // perform test
116
117         Optional optional = healingPipelineDao.performGraphReadHealing(janusGraphVertex, GraphEdgeLabels.CAPABILITY);
118         assertTrue(optional.isPresent());
119         final JanusGraphVertex changedVertex = (JanusGraphVertex) optional.get();
120
121         //validate result
122         assertNotNull(changedVertex);
123
124     }
125
126     @Test
127     public void testPipelineFilterGenericJanusGraphDao() {
128         // init data
129         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
130         healingPipelineDao.setHealVersion(7);
131         healingPipelineDao.initHealVersion();
132         healingPipelineDao.initGraphHealers();
133         healingPipelineDao.setHealingPipeline(createPipelineMap());
134         assertEquals(1,
135
136                 healingPipelineDao.getHealersForVertex(GraphEdgeLabels.ATTRIBUTE.getProperty(), HealVersionBuilder.build(6)).size());
137
138         GraphNode mockGraphNode = new MockGraphNode(NodeTypeEnum.Attribute);
139         final int version = 5;
140         mockGraphNode.setHealingVersion(Integer.valueOf(version));
141
142         // perform test
143
144         Optional optional = healingPipelineDao.performGraphReadHealing(mockGraphNode,createGraphEdge(GraphEdgeLabels.ATTRIBUTE));
145         assertTrue(optional.isPresent());
146         final GraphNode changedVertex = (GraphNode) optional.get();
147
148         //validate result
149         final Integer healVersion = changedVertex.getHealingVersion();
150         assertNotNull(healVersion);
151         assertEquals(healingPipelineDao.getCurrentHealVersion().getVersion(), healVersion);
152     }
153
154     @Test
155     public void testPipelineFilterJanusGraph1Attributes() {
156         // init data
157         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
158         healingPipelineDao.setHealVersion(7);
159         healingPipelineDao.initHealVersion();
160         healingPipelineDao.initGraphHealers();
161         healingPipelineDao.setHealingPipeline(createPipelineMap());
162
163         assertEquals(2,
164                 healingPipelineDao.getHealersForVertex(GraphEdgeLabels.ATTRIBUTE.getProperty(), HealVersionBuilder.build(5)).size());
165
166     }
167
168     @Test
169     public void healTest() {
170         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
171         healingPipelineDao.setHealVersion(3);
172         healingPipelineDao.initHealVersion();
173         healingPipelineDao.initGraphHealers();
174         final HealVersion<Integer> version3 = HealVersionBuilder.build(3);
175         assertFalse(healingPipelineDao.shouldHeal(HealVersionBuilder.build(2), version3));
176         assertFalse(healingPipelineDao.shouldHeal(version3, version3));
177         assertTrue(healingPipelineDao.shouldHeal(HealVersionBuilder.build(2), HealVersionBuilder.build(1)));
178     }
179
180
181     @Test
182     public void setCurrentVersion() {
183         //init data
184         GraphVertex graphVertex = new GraphVertex();
185         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
186         final int healVersion = 7;
187         healingPipelineDao.setHealVersion(healVersion);
188         healingPipelineDao.initHealVersion();
189         healingPipelineDao.initGraphHealers();
190
191         //execute code
192         healingPipelineDao.setHealingVersion(graphVertex);
193
194         //validate result
195         final Object currentVersion = graphVertex.getMetadataProperties().get(GraphPropertyEnum.HEALING_VERSION);
196         assertNotNull(currentVersion);
197         assertTrue(currentVersion instanceof Integer);
198         assertEquals(healingPipelineDao.getCurrentHealVersion().getVersion().intValue(), ((Integer) currentVersion).intValue());
199     }
200
201     @Test(expected = IllegalStateException.class)
202     public void testMultilistValidation() {
203         // init data
204         HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
205         healingPipelineDao.setHealVersion(7);
206         healingPipelineDao.initHealVersion();
207         healingPipelineDao.initGraphHealers();
208
209         ImmutableListMultimap<String, Heal> shouldFail = ImmutableListMultimap.<String, Heal>builder().put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(3))
210                 .put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(4))
211                 .put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(5))
212                 .put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(6))
213                 .put(EdgeLabelEnum.CAPABILITIES.name(), new GraphVertexHealTestMock(3))
214                 .put(EdgeLabelEnum.CAPABILITIES.name(), new GraphVertexHealTestMock(3)) // this should cause exception
215                 .put(EdgeLabelEnum.CAPABILITIES.name(), new GraphVertexHealTestMock(69)).build();
216
217         //performTest
218         healingPipelineDao.setHealingPipeline(shouldFail);
219     }
220
221     private ImmutableListMultimap<String, Heal> createPipelineMap() {
222         return ImmutableListMultimap.<String, Heal>builder().put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(3))
223                 .put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(4))
224                 .put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(5))
225                 .put(EdgeLabelEnum.ATTRIBUTES.name(), new GraphVertexHealTestMock(6))
226                 .put(EdgeLabelEnum.CAPABILITIES.name(), new GraphVertexHealTestMock(3))
227                 .put(EdgeLabelEnum.CAPABILITIES.name(), new GraphVertexHealTestMock(6))
228                 .put(EdgeLabelEnum.CAPABILITIES.name(), new GraphVertexHealTestMock(69))
229                 .put(GraphEdgeLabels.ATTRIBUTE.getProperty(), new GraphNodeHealTestMock(4))
230                 .put(GraphEdgeLabels.ATTRIBUTE.getProperty(), new GraphNodeHealTestMock(5))
231                 .put(GraphEdgeLabels.ATTRIBUTE.getProperty(), new GraphNodeHealTestMock(6))
232                 .put(GraphEdgeLabels.CAPABILITY.getProperty(), new JanusGraphVertexHealTestMock(4))
233                 .put(GraphEdgeLabels.CAPABILITY.getProperty(), new JanusGraphVertexHealTestMock(5))
234                 .put(GraphEdgeLabels.CAPABILITY.getProperty(), new JanusGraphVertexHealTestMock(6)).build();
235     }
236
237     public GraphEdge createGraphEdge(GraphEdgeLabels graphEdgeLabels){
238         return new GraphEdge(graphEdgeLabels, new HashMap<>());
239     }
240
241
242     private class GraphVertexHealTestMock extends AbstractGraphVertexHeal {
243
244         private HealVersion healVersion;
245
246         public GraphVertexHealTestMock(int i) {
247             healVersion = HealVersionBuilder.build(i);
248         }
249
250         @Override
251         public HealVersion fromVersion() {
252             return healVersion;
253         }
254
255         @Override
256         public void healData(GraphVertex parentVertex) {
257
258         }
259
260     }
261
262     private class GraphNodeHealTestMock extends AbstractJanusGraphVertexHeal {
263         private HealVersion healVersion;
264
265         public GraphNodeHealTestMock(int i) {
266             healVersion = HealVersionBuilder.build(i);
267         }
268
269         @Override
270         public HealVersion fromVersion() {
271             return healVersion;
272         }
273
274         @Override
275         public void healData(GraphNode parentV) {
276
277         }
278     }
279
280
281     private class JanusGraphVertexHealTestMock implements Heal<JanusGraphVertex> {
282         private HealVersion healVersion;
283
284         public JanusGraphVertexHealTestMock(int i) {
285             healVersion = HealVersionBuilder.build(i);
286         }
287
288         @Override
289         public HealVersion fromVersion() {
290             return healVersion;
291         }
292
293         @Override
294         public void healData(JanusGraphVertex parentV) {
295
296         }
297     }
298
299     private class MockGraphNode extends GraphNode {
300         private int healVersion;
301
302         public MockGraphNode(NodeTypeEnum label) {
303             super(label);
304         }
305
306         @Override
307         public String getUniqueId() {
308             return null;
309         }
310
311         @Override
312         public Map<String, Object> toGraphMap() {
313             return null;
314         }
315
316         @Override
317         public Integer getHealingVersion() {
318             return healVersion;
319         }
320
321         @Override
322         public void setHealingVersion(Integer version) {
323             this.healVersion = version;
324         }
325     }
326
327 }