Update license date and text
[aai/champ.git] / champ-lib / champ-core / src / test / java / org / onap / aai / champcore / core / ChampTransactionTest.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ===================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  */
21 package org.onap.aai.champcore.core;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.util.*;
28 import java.util.function.Consumer;
29 import java.util.function.Function;
30 import java.util.stream.Stream;
31
32 import org.apache.commons.configuration.Configuration;
33 import org.apache.tinkerpop.gremlin.process.computer.GraphComputer;
34 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
35 import org.apache.tinkerpop.gremlin.structure.Edge;
36 import org.apache.tinkerpop.gremlin.structure.Graph;
37 import org.apache.tinkerpop.gremlin.structure.Transaction;
38 import org.apache.tinkerpop.gremlin.structure.Vertex;
39 import org.apache.tinkerpop.gremlin.structure.util.FeatureDescriptor;
40 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.onap.aai.champcore.ChampAPI;
44 import org.onap.aai.champcore.ChampCapabilities;
45 import org.onap.aai.champcore.ChampGraph;
46 import org.onap.aai.champcore.ChampTransaction;
47 import org.onap.aai.champcore.NoOpTinkerPopTransaction;
48 import org.onap.aai.champcore.exceptions.ChampIndexNotExistsException;
49 import org.onap.aai.champcore.exceptions.ChampTransactionException;
50 import org.onap.aai.champcore.graph.impl.AbstractTinkerpopChampGraph;
51 import org.onap.aai.champcore.graph.impl.TinkerpopTransaction;
52 import org.onap.aai.champcore.model.ChampObjectIndex;
53 import org.onap.aai.champcore.model.ChampRelationshipIndex;
54 import org.onap.aai.champcore.schema.ChampSchemaEnforcer;
55
56
57 public class ChampTransactionTest {
58   
59   TestGraph g = null;
60   
61   @Before
62   public void setup() {
63     g = new TestGraph();
64   }
65     
66   /**
67    * This test validates the behaviour when the underlying graph implementation
68    * does not support transactions.
69    */
70   @Test
71   public void testNoTransactionSupport() {
72     
73     // By default our test graph should be configured to support transactions, but
74     // set it explicitly anyway just to be sure.
75     g.setTransactionSupport(true);
76     
77     // Now, try to start a new transaction against our graph - it should succeed.
78     TinkerpopTransaction t = new TinkerpopTransaction(g);
79     Graph gi = t.getGraphInstance(); // GDF: Making jacoco happy...
80     
81     // Now, configure our graph to specify that transactions are NOT supported.
82     g.setTransactionSupport(false);
83     
84     try {
85       
86       // Now, try to start a new transaction against our graph.
87       t = new TinkerpopTransaction(g);
88       
89     } catch (UnsupportedOperationException e) {
90       
91       // If we're here, it's all good since we expected an exception to be thrown (since our
92       // graph does NOT support transactions).
93       return;
94     }
95
96     // If we're here then we were able to open a transaction even though our graph does 
97     // not support that functionality.
98     fail("Attempt to open a transaction against a graph with no transaction support should not succeed.");
99   }
100   
101   
102   /**
103    * This test validates the behaviour when committing a transaction under various
104    * circumstances.
105    * @throws ChampTransactionException 
106    */
107   @Test
108   public void testCommit() throws ChampTransactionException {
109     
110     // By default our test graph should simulate successful commits, but set
111     // the configuration anyway, just to be sure.
112     g.setFailCommits(false);
113     
114     // Now, start a transaction.
115     TinkerpopTransaction t = new TinkerpopTransaction(g);
116     
117     // Call commit against the transaction - it should complete successfully.
118     t.commit();
119     
120     // Now, configure our test graph to simulate failing to commit.
121     g.setFailCommits(true);
122     
123     // Open another transaction...
124     t = new TinkerpopTransaction(g);
125     boolean exceptionThrown = false;
126     try {
127       
128       //...and try to commit it.
129       t.commit();
130       
131     } catch (Throwable e) {
132       
133       // Our commit should have failed and ultimately thrown an exception - so if we
134       // are here then it's all good.
135       exceptionThrown = true;
136     }
137     
138     assertTrue("Failed commit should have produced an exception.", exceptionThrown);
139   }
140
141   
142   @Test
143   public void testRollback() throws ChampTransactionException {
144     
145     // By default our test graph should simulate successful commits, but set
146     // the configuration anyway, just to be sure.
147     g.setFailCommits(false);
148     
149     // Now, start a transaction.
150     TinkerpopTransaction t = new TinkerpopTransaction(g);
151     
152     // Call rollback against the transaction - it should complete successfully.
153     t.rollback();
154     
155     // Now, configure our test graph to simulate failing to commit.
156     g.setFailCommits(true);
157     
158     // Open another transaction...
159     t = new TinkerpopTransaction(g);
160     boolean exceptionThrown = false;
161     try {
162       
163       //...and try to commit it.
164       t.rollback();
165       
166     } catch (Throwable e) {
167       
168       // Our commit should have failed and ultimately thrown an exception - so if we
169       // are here then it's all good.
170       exceptionThrown = true;
171     }
172     
173     assertTrue("Failed rollback should have produced an exception.", exceptionThrown);
174   }
175
176   @Test
177   public void test() throws ChampTransactionException {
178     
179     AbstractTinkerpopChampGraph graph = new AbstractTinkerpopChampGraph(new HashMap<String, Object>()) { 
180       
181       @Override
182       protected Graph getGraph() {
183         return TinkerGraph.open();
184       }
185
186       @Override
187       protected ChampSchemaEnforcer getSchemaEnforcer() {
188         return null;
189       }
190
191       @Override
192       public void executeStoreObjectIndex(ChampObjectIndex index) { }
193
194       @Override
195       public Optional<ChampObjectIndex> retrieveObjectIndex(String indexName) {
196         return null;
197       }
198
199       @Override
200       public Stream<ChampObjectIndex> retrieveObjectIndices() {
201         return null;
202       }
203
204       @Override
205       public void executeDeleteObjectIndex(String indexName) throws ChampIndexNotExistsException {}
206
207       @Override
208       public void executeStoreRelationshipIndex(ChampRelationshipIndex index) {}
209
210       @Override
211       public Optional<ChampRelationshipIndex> retrieveRelationshipIndex(String indexName) {
212         return null;
213       }
214
215       @Override
216       public Stream<ChampRelationshipIndex> retrieveRelationshipIndices() {
217         return null;
218       }
219
220       @Override
221       public void executeDeleteRelationshipIndex(String indexName) throws ChampIndexNotExistsException {}
222
223       @Override
224       public ChampCapabilities capabilities() {
225         return null;
226       }
227
228         @Override
229         public GraphTraversal<?, ?> hasLabel(GraphTraversal<?, ?> query, Object type) {
230                 // TODO Auto-generated method stub
231                 return null;
232         }
233     };
234     
235     TinkerpopTransaction t = new TinkerpopTransaction(g);
236     t.id();
237     graph.commitTransaction(t);
238     graph.rollbackTransaction(t);
239     
240   }
241   
242   private class TestGraph implements Graph {
243
244     private boolean supportsTransactions = true;
245     private boolean failCommits = false;
246     
247     
248     public void setTransactionSupport(boolean supportsTransactions) {
249       this.supportsTransactions = supportsTransactions;
250     }
251     
252     public void setFailCommits(boolean failCommits) {
253       this.failCommits = failCommits;
254     }
255     @Override
256     public Vertex addVertex(Object... keyValues) {
257       // TODO Auto-generated method stub
258       return null;
259     }
260
261     @Override
262     public void close() throws Exception {
263       // TODO Auto-generated method stub
264       
265     }
266
267     @Override
268     public GraphComputer compute() throws IllegalArgumentException {
269       // TODO Auto-generated method stub
270       return null;
271     }
272
273     @Override
274     public <C extends GraphComputer> C compute(Class<C> graphComputerClass)
275         throws IllegalArgumentException {
276       // TODO Auto-generated method stub
277       return null;
278     }
279
280     @Override
281     public Configuration configuration() {
282       // TODO Auto-generated method stub
283       return null;
284     }
285
286     @Override
287     public Iterator<Edge> edges(Object... edgeIds) {
288       // TODO Auto-generated method stub
289       return null;
290     }
291
292     @Override
293     public Transaction tx() {
294       return new TestTransaction();
295     }
296
297     @Override
298     public Variables variables() {
299       // TODO Auto-generated method stub
300       return null;
301     }
302
303     @Override
304     public Iterator<Vertex> vertices(Object... vertexIds) {
305       // TODO Auto-generated method stub
306       return null;
307     }
308     
309     /**
310      * Gets the {@link Features} exposed by the underlying {@code Graph} implementation.
311      */
312     public Features features() {
313       return new TestFeatures() {
314       };
315     }
316     
317     public class TestFeatures implements Graph.Features {
318       
319       /**
320        * Gets the features related to "graph" operation.
321        */
322       public GraphFeatures graph() {
323           return new TestGraphFeatures() {
324           };
325       }
326       
327       public class TestGraphFeatures implements Graph.Features.GraphFeatures {
328         
329         /**
330          * Determines if the {@code Graph} implementations supports transactions.
331          */
332         @FeatureDescriptor(name = FEATURE_TRANSACTIONS)
333         public boolean supportsTransactions() {
334             return supportsTransactions;
335         }
336       }
337     }
338   }
339   
340   private class TestTransaction implements Transaction {
341
342     @Override
343     public void addTransactionListener(Consumer<Status> listener) {
344       // TODO Auto-generated method stub
345       
346     }
347
348     @Override
349     public void clearTransactionListeners() {
350       // TODO Auto-generated method stub
351       
352     }
353
354     @Override
355     public void close() {
356       // TODO Auto-generated method stub
357       
358     }
359
360     @Override
361     public void commit() {
362       
363       if(g.failCommits) {
364         throw new UnsupportedOperationException();
365       } 
366     }
367
368     @Override
369     public <G extends Graph> G createThreadedTx() {
370      return (G) g;
371     }
372
373     @Override
374     public boolean isOpen() {
375       // TODO Auto-generated method stub
376       return false;
377     }
378
379     @Override
380     public Transaction onClose(Consumer<Transaction> consumer) {
381       // TODO Auto-generated method stub
382       return null;
383     }
384
385     @Override
386     public Transaction onReadWrite(Consumer<Transaction> consumer) {
387       // TODO Auto-generated method stub
388       return null;
389     }
390
391     @Override
392     public void open() {
393       // TODO Auto-generated method stub
394       
395     }
396
397     @Override
398     public void readWrite() {
399       // TODO Auto-generated method stub
400       
401     }
402
403     @Override
404     public void removeTransactionListener(Consumer<Status> listener) {
405       // TODO Auto-generated method stub
406       
407     }
408
409     @Override
410     public void rollback() {
411       if(g.failCommits) {
412         throw new UnsupportedOperationException();
413       } 
414     }
415
416     @Override
417     public <R> Workload<R> submit(Function<Graph, R> work) {
418       // TODO Auto-generated method stub
419       return null;
420     }
421     
422   }
423
424   private class TestTinkerpopGraph extends AbstractTinkerpopChampGraph {
425
426     protected TestTinkerpopGraph(Map<String, Object> properties) {
427       super(properties);
428     }
429
430     @Override
431     protected Graph getGraph() {
432       return TinkerGraph.open();
433     }
434
435     
436     @Override
437     protected ChampSchemaEnforcer getSchemaEnforcer() {
438       return null;
439     }
440
441     @Override
442     public void executeStoreObjectIndex(ChampObjectIndex index) {      
443     }
444
445     @Override
446     public Optional<ChampObjectIndex> retrieveObjectIndex(String indexName) {
447       // TODO Auto-generated method stub
448       return null;
449     }
450
451     @Override
452     public Stream<ChampObjectIndex> retrieveObjectIndices() {
453       // TODO Auto-generated method stub
454       return null;
455     }
456
457     @Override
458     public void executeDeleteObjectIndex(String indexName) throws ChampIndexNotExistsException {
459       // TODO Auto-generated method stub
460       
461     }
462
463     @Override
464     public void executeStoreRelationshipIndex(ChampRelationshipIndex index) {
465       // TODO Auto-generated method stub
466       
467     }
468
469     @Override
470     public Optional<ChampRelationshipIndex> retrieveRelationshipIndex(String indexName) {
471       // TODO Auto-generated method stub
472       return null;
473     }
474
475     @Override
476     public Stream<ChampRelationshipIndex> retrieveRelationshipIndices() {
477       // TODO Auto-generated method stub
478       return null;
479     }
480
481     @Override
482     public void executeDeleteRelationshipIndex(String indexName)
483         throws ChampIndexNotExistsException {
484       // TODO Auto-generated method stub
485       
486     }
487
488     @Override
489     public ChampCapabilities capabilities() {
490       // TODO Auto-generated method stub
491       return null;
492     }
493
494         @Override
495         public GraphTraversal<?, ?> hasLabel(GraphTraversal<?, ?> query, Object type) {
496                 // TODO Auto-generated method stub
497                 return null;
498         }
499     
500   }
501 }