[AAI-2175] Change aai champ container processes to run as non-root on the host
[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   @Override
235   public void createDefaultIndexes() {
236     // TODO Auto-generated method stub
237     
238   }
239     };
240     
241     TinkerpopTransaction t = new TinkerpopTransaction(g);
242     t.id();
243     graph.commitTransaction(t);
244     graph.rollbackTransaction(t);
245     
246   }
247   
248   private class TestGraph implements Graph {
249
250     private boolean supportsTransactions = true;
251     private boolean failCommits = false;
252     
253     
254     public void setTransactionSupport(boolean supportsTransactions) {
255       this.supportsTransactions = supportsTransactions;
256     }
257     
258     public void setFailCommits(boolean failCommits) {
259       this.failCommits = failCommits;
260     }
261     @Override
262     public Vertex addVertex(Object... keyValues) {
263       // TODO Auto-generated method stub
264       return null;
265     }
266
267     @Override
268     public void close() throws Exception {
269       // TODO Auto-generated method stub
270       
271     }
272
273     @Override
274     public GraphComputer compute() throws IllegalArgumentException {
275       // TODO Auto-generated method stub
276       return null;
277     }
278
279     @Override
280     public <C extends GraphComputer> C compute(Class<C> graphComputerClass)
281         throws IllegalArgumentException {
282       // TODO Auto-generated method stub
283       return null;
284     }
285
286     @Override
287     public Configuration configuration() {
288       // TODO Auto-generated method stub
289       return null;
290     }
291
292     @Override
293     public Iterator<Edge> edges(Object... edgeIds) {
294       // TODO Auto-generated method stub
295       return null;
296     }
297
298     @Override
299     public Transaction tx() {
300       return new TestTransaction();
301     }
302
303     @Override
304     public Variables variables() {
305       // TODO Auto-generated method stub
306       return null;
307     }
308
309     @Override
310     public Iterator<Vertex> vertices(Object... vertexIds) {
311       // TODO Auto-generated method stub
312       return null;
313     }
314     
315     /**
316      * Gets the {@link Features} exposed by the underlying {@code Graph} implementation.
317      */
318     public Features features() {
319       return new TestFeatures() {
320       };
321     }
322     
323     public class TestFeatures implements Graph.Features {
324       
325       /**
326        * Gets the features related to "graph" operation.
327        */
328       public GraphFeatures graph() {
329           return new TestGraphFeatures() {
330           };
331       }
332       
333       public class TestGraphFeatures implements Graph.Features.GraphFeatures {
334         
335         /**
336          * Determines if the {@code Graph} implementations supports transactions.
337          */
338         @FeatureDescriptor(name = FEATURE_TRANSACTIONS)
339         public boolean supportsTransactions() {
340             return supportsTransactions;
341         }
342       }
343     }
344   }
345   
346   private class TestTransaction implements Transaction {
347
348     @Override
349     public void addTransactionListener(Consumer<Status> listener) {
350       // TODO Auto-generated method stub
351       
352     }
353
354     @Override
355     public void clearTransactionListeners() {
356       // TODO Auto-generated method stub
357       
358     }
359
360     @Override
361     public void close() {
362       // TODO Auto-generated method stub
363       
364     }
365
366     @Override
367     public void commit() {
368       
369       if(g.failCommits) {
370         throw new UnsupportedOperationException();
371       } 
372     }
373
374     @Override
375     public <G extends Graph> G createThreadedTx() {
376      return (G) g;
377     }
378
379     @Override
380     public boolean isOpen() {
381       // TODO Auto-generated method stub
382       return false;
383     }
384
385     @Override
386     public Transaction onClose(Consumer<Transaction> consumer) {
387       // TODO Auto-generated method stub
388       return null;
389     }
390
391     @Override
392     public Transaction onReadWrite(Consumer<Transaction> consumer) {
393       // TODO Auto-generated method stub
394       return null;
395     }
396
397     @Override
398     public void open() {
399       // TODO Auto-generated method stub
400       
401     }
402
403     @Override
404     public void readWrite() {
405       // TODO Auto-generated method stub
406       
407     }
408
409     @Override
410     public void removeTransactionListener(Consumer<Status> listener) {
411       // TODO Auto-generated method stub
412       
413     }
414
415     @Override
416     public void rollback() {
417       if(g.failCommits) {
418         throw new UnsupportedOperationException();
419       } 
420     }
421
422     @Override
423     public <R> Workload<R> submit(Function<Graph, R> work) {
424       // TODO Auto-generated method stub
425       return null;
426     }
427     
428   }
429
430   private class TestTinkerpopGraph extends AbstractTinkerpopChampGraph {
431
432     protected TestTinkerpopGraph(Map<String, Object> properties) {
433       super(properties);
434     }
435
436     @Override
437     protected Graph getGraph() {
438       return TinkerGraph.open();
439     }
440
441     
442     @Override
443     protected ChampSchemaEnforcer getSchemaEnforcer() {
444       return null;
445     }
446
447     @Override
448     public void executeStoreObjectIndex(ChampObjectIndex index) {      
449     }
450
451     @Override
452     public Optional<ChampObjectIndex> retrieveObjectIndex(String indexName) {
453       // TODO Auto-generated method stub
454       return null;
455     }
456
457     @Override
458     public Stream<ChampObjectIndex> retrieveObjectIndices() {
459       // TODO Auto-generated method stub
460       return null;
461     }
462
463     @Override
464     public void executeDeleteObjectIndex(String indexName) throws ChampIndexNotExistsException {
465       // TODO Auto-generated method stub
466       
467     }
468
469     @Override
470     public void executeStoreRelationshipIndex(ChampRelationshipIndex index) {
471       // TODO Auto-generated method stub
472       
473     }
474
475     @Override
476     public Optional<ChampRelationshipIndex> retrieveRelationshipIndex(String indexName) {
477       // TODO Auto-generated method stub
478       return null;
479     }
480
481     @Override
482     public Stream<ChampRelationshipIndex> retrieveRelationshipIndices() {
483       // TODO Auto-generated method stub
484       return null;
485     }
486
487     @Override
488     public void executeDeleteRelationshipIndex(String indexName)
489         throws ChampIndexNotExistsException {
490       // TODO Auto-generated method stub
491       
492     }
493
494     @Override
495     public ChampCapabilities capabilities() {
496       // TODO Auto-generated method stub
497       return null;
498     }
499
500         @Override
501         public GraphTraversal<?, ?> hasLabel(GraphTraversal<?, ?> query, Object type) {
502                 // TODO Auto-generated method stub
503                 return null;
504         }
505
506   @Override
507   public void createDefaultIndexes() {
508     // TODO Auto-generated method stub
509     
510   }
511     
512   }
513 }