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.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;
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;
45 import static org.junit.Assert.*;
47 public class HealingPipelineDaoTest {
50 public void enableToggleableFeatures(){
51 TestFeatureManager manager = new TestFeatureManager(ToggleableFeature.class);
53 TestFeatureManagerProvider.setFeatureManager(manager);
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)));
71 public void testPipelineFilter3Attributes() {
73 HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
74 healingPipelineDao.setHealVersion(7);
75 healingPipelineDao.initHealVersion();
76 healingPipelineDao.initGraphHealers();
77 healingPipelineDao.setHealingPipeline(createPipelineMap());
80 healingPipelineDao.getHealersForVertex(EdgeLabelEnum.ATTRIBUTES.name(), HealVersionBuilder.build(5)).size());
82 GraphVertex graphVertex = new GraphVertex();
83 final int version = 5;
84 graphVertex.addMetadataProperty(GraphPropertyEnum.HEALING_VERSION, Integer.valueOf(version));
88 Optional optional = healingPipelineDao.performGraphReadHealing(graphVertex, EdgeLabelEnum.ATTRIBUTES);
89 assertTrue(optional.isPresent());
90 final GraphVertex changedVertex = (GraphVertex) optional.get();
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());
100 public void testPipelineFilter3AttributesJanusGraphVertex() {
102 HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
103 healingPipelineDao.setHealVersion(7);
104 healingPipelineDao.initHealVersion();
105 healingPipelineDao.initGraphHealers();
106 healingPipelineDao.setHealingPipeline(createPipelineMap());
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);
117 Optional optional = healingPipelineDao.performGraphReadHealing(janusGraphVertex, GraphEdgeLabels.CAPABILITY);
118 assertTrue(optional.isPresent());
119 final JanusGraphVertex changedVertex = (JanusGraphVertex) optional.get();
122 assertNotNull(changedVertex);
127 public void testPipelineFilterGenericJanusGraphDao() {
129 HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
130 healingPipelineDao.setHealVersion(7);
131 healingPipelineDao.initHealVersion();
132 healingPipelineDao.initGraphHealers();
133 healingPipelineDao.setHealingPipeline(createPipelineMap());
136 healingPipelineDao.getHealersForVertex(GraphEdgeLabels.ATTRIBUTE.getProperty(), HealVersionBuilder.build(6)).size());
138 GraphNode mockGraphNode = new MockGraphNode(NodeTypeEnum.Attribute);
139 final int version = 5;
140 mockGraphNode.setHealingVersion(Integer.valueOf(version));
144 Optional optional = healingPipelineDao.performGraphReadHealing(mockGraphNode,createGraphEdge(GraphEdgeLabels.ATTRIBUTE));
145 assertTrue(optional.isPresent());
146 final GraphNode changedVertex = (GraphNode) optional.get();
149 final Integer healVersion = changedVertex.getHealingVersion();
150 assertNotNull(healVersion);
151 assertEquals(healingPipelineDao.getCurrentHealVersion().getVersion(), healVersion);
155 public void testPipelineFilterJanusGraph1Attributes() {
157 HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
158 healingPipelineDao.setHealVersion(7);
159 healingPipelineDao.initHealVersion();
160 healingPipelineDao.initGraphHealers();
161 healingPipelineDao.setHealingPipeline(createPipelineMap());
164 healingPipelineDao.getHealersForVertex(GraphEdgeLabels.ATTRIBUTE.getProperty(), HealVersionBuilder.build(5)).size());
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)));
182 public void setCurrentVersion() {
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();
192 healingPipelineDao.setHealingVersion(graphVertex);
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());
201 @Test(expected = IllegalStateException.class)
202 public void testMultilistValidation() {
204 HealingPipelineDao healingPipelineDao = new HealingPipelineDao();
205 healingPipelineDao.setHealVersion(7);
206 healingPipelineDao.initHealVersion();
207 healingPipelineDao.initGraphHealers();
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();
218 healingPipelineDao.setHealingPipeline(shouldFail);
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();
237 public GraphEdge createGraphEdge(GraphEdgeLabels graphEdgeLabels){
238 return new GraphEdge(graphEdgeLabels, new HashMap<>());
242 private class GraphVertexHealTestMock extends AbstractGraphVertexHeal {
244 private HealVersion healVersion;
246 public GraphVertexHealTestMock(int i) {
247 healVersion = HealVersionBuilder.build(i);
251 public HealVersion fromVersion() {
256 public void healData(GraphVertex parentVertex) {
262 private class GraphNodeHealTestMock extends AbstractJanusGraphVertexHeal {
263 private HealVersion healVersion;
265 public GraphNodeHealTestMock(int i) {
266 healVersion = HealVersionBuilder.build(i);
270 public HealVersion fromVersion() {
275 public void healData(GraphNode parentV) {
281 private class JanusGraphVertexHealTestMock implements Heal<JanusGraphVertex> {
282 private HealVersion healVersion;
284 public JanusGraphVertexHealTestMock(int i) {
285 healVersion = HealVersionBuilder.build(i);
289 public HealVersion fromVersion() {
294 public void healData(JanusGraphVertex parentV) {
299 private class MockGraphNode extends GraphNode {
300 private int healVersion;
302 public MockGraphNode(NodeTypeEnum label) {
307 public String getUniqueId() {
312 public Map<String, Object> toGraphMap() {
317 public Integer getHealingVersion() {
322 public void setHealingVersion(Integer version) {
323 this.healVersion = version;