2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.be.dao.cassandra;
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;
38 import java.util.Optional;
40 import static org.junit.Assert.*;
42 public class HealingPipelineDaoTest {
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)));
59 public void testPipelineFilter3Attributes() {
61 HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
62 healingPipelineDao.setHealVersion(7);
63 healingPipelineDao.initHealVersion();
64 healingPipelineDao.initGraphHealers();
65 healingPipelineDao.setHealingPipeline(createPipelineMap());
68 healingPipelineDao.getHealersForVertex(EdgeLabelEnum.ATTRIBUTES.name(), HealVersionBuilder.build(5)).size());
70 GraphVertex graphVertex = new GraphVertex();
71 final int version = 5;
72 graphVertex.addMetadataProperty(GraphPropertyEnum.HEALING_VERSION, Integer.valueOf(version));
76 Optional optional = healingPipelineDao.performGraphReadHealing(graphVertex, EdgeLabelEnum.ATTRIBUTES);
77 assertTrue(optional.isPresent());
78 final GraphVertex changedVertex = (GraphVertex) optional.get();
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());
88 public void testPipelineFilter3AttributesJanusGraphVertex() {
90 HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
91 healingPipelineDao.setHealVersion(7);
92 healingPipelineDao.initHealVersion();
93 healingPipelineDao.initGraphHealers();
94 healingPipelineDao.setHealingPipeline(createPipelineMap());
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);
105 Optional optional = healingPipelineDao.performGraphReadHealing(janusGraphVertex, GraphEdgeLabels.CAPABILITY);
106 assertTrue(optional.isPresent());
107 final JanusGraphVertex changedVertex = (JanusGraphVertex) optional.get();
110 assertNotNull(changedVertex);
115 public void testPipelineFilterGenericJanusGraphDao() {
117 HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
118 healingPipelineDao.setHealVersion(7);
119 healingPipelineDao.initHealVersion();
120 healingPipelineDao.initGraphHealers();
121 healingPipelineDao.setHealingPipeline(createPipelineMap());
124 healingPipelineDao.getHealersForVertex(GraphEdgeLabels.ATTRIBUTE.getProperty(), HealVersionBuilder.build(6)).size());
126 GraphNode mockGraphNode = new MockGraphNode(NodeTypeEnum.Attribute);
127 final int version = 5;
128 mockGraphNode.setHealingVersion(Integer.valueOf(version));
132 Optional optional = healingPipelineDao.performGraphReadHealing(mockGraphNode,createGraphEdge(GraphEdgeLabels.ATTRIBUTE));
133 assertTrue(optional.isPresent());
134 final GraphNode changedVertex = (GraphNode) optional.get();
137 final Integer healVersion = changedVertex.getHealingVersion();
138 assertNotNull(healVersion);
139 assertEquals(healingPipelineDao.getCurrentHealVersion().getVersion(), healVersion);
143 public void testPipelineFilterJanusGraph1Attributes() {
145 HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
146 healingPipelineDao.setHealVersion(7);
147 healingPipelineDao.initHealVersion();
148 healingPipelineDao.initGraphHealers();
149 healingPipelineDao.setHealingPipeline(createPipelineMap());
152 healingPipelineDao.getHealersForVertex(GraphEdgeLabels.ATTRIBUTE.getProperty(), HealVersionBuilder.build(5)).size());
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)));
170 public void setCurrentVersion() {
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();
180 healingPipelineDao.setHealingVersion(graphVertex);
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());
189 @Test(expected = IllegalStateException.class)
190 public void testMultilistValidation() {
192 HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
193 healingPipelineDao.setHealVersion(7);
194 healingPipelineDao.initHealVersion();
195 healingPipelineDao.initGraphHealers();
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();
206 healingPipelineDao.setHealingPipeline(shouldFail);
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();
225 public GraphEdge createGraphEdge(GraphEdgeLabels graphEdgeLabels){
226 return new GraphEdge(graphEdgeLabels, new HashMap<>());
230 private class GraphVertexHealTestMock extends AbstractGraphVertexHeal {
232 private HealVersion healVersion;
234 public GraphVertexHealTestMock(int i) {
235 healVersion = HealVersionBuilder.build(i);
239 public HealVersion fromVersion() {
244 public void healData(GraphVertex parentVertex) {
250 private class GraphNodeHealTestMock extends AbstractJanusGraphVertexHeal {
251 private HealVersion healVersion;
253 public GraphNodeHealTestMock(int i) {
254 healVersion = HealVersionBuilder.build(i);
258 public HealVersion fromVersion() {
263 public void healData(GraphNode parentV) {
269 private class JanusGraphVertexHealTestMock implements Heal<JanusGraphVertex> {
270 private HealVersion healVersion;
272 public JanusGraphVertexHealTestMock(int i) {
273 healVersion = HealVersionBuilder.build(i);
277 public HealVersion fromVersion() {
282 public void healData(JanusGraphVertex parentV) {
287 private class MockGraphNode extends GraphNode {
288 private int healVersion;
290 public MockGraphNode(NodeTypeEnum label) {
295 public String getUniqueId() {
300 public Map<String, Object> toGraphMap() {
305 public Integer getHealingVersion() {
310 public void setHealingVersion(Integer version) {
311 this.healVersion = version;