summaryrefslogtreecommitdiffstats
path: root/champ-lib/champ-core/src/test/java/org/onap/aai/champcore/core/ChampTransactionTest.java
blob: 30f033c8118247a18107007ed7ae849cfc6ed881 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/**
 * ============LICENSE_START==========================================
 * org.onap.aai
 * ===================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017 Amdocs
 * ===================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
package org.onap.aai.champcore.core;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

import org.apache.commons.configuration.Configuration;
import org.apache.tinkerpop.gremlin.process.computer.GraphComputer;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Transaction;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.util.FeatureDescriptor;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.junit.Before;
import org.junit.Test;
import org.onap.aai.champcore.ChampAPI;
import org.onap.aai.champcore.ChampCapabilities;
import org.onap.aai.champcore.ChampGraph;
import org.onap.aai.champcore.ChampTransaction;
import org.onap.aai.champcore.NoOpTinkerPopTransaction;
import org.onap.aai.champcore.exceptions.ChampIndexNotExistsException;
import org.onap.aai.champcore.exceptions.ChampTransactionException;
import org.onap.aai.champcore.graph.impl.AbstractTinkerpopChampGraph;
import org.onap.aai.champcore.graph.impl.TinkerpopTransaction;
import org.onap.aai.champcore.model.ChampObjectIndex;
import org.onap.aai.champcore.model.ChampRelationshipIndex;
import org.onap.aai.champcore.schema.ChampSchemaEnforcer;


public class ChampTransactionTest {
  
  TestGraph g = null;
  
  @Before
  public void setup() {
    g = new TestGraph();
  }
    
  /**
   * This test validates the behaviour when the underlying graph implementation
   * does not support transactions.
   */
  @Test
  public void testNoTransactionSupport() {
    
    // By default our test graph should be configured to support transactions, but
    // set it explicitly anyway just to be sure.
    g.setTransactionSupport(true);
    
    // Now, try to start a new transaction against our graph - it should succeed.
    TinkerpopTransaction t = new TinkerpopTransaction(g);
    Graph gi = t.getGraphInstance(); // GDF: Making jacoco happy...
    
    // Now, configure our graph to specify that transactions are NOT supported.
    g.setTransactionSupport(false);
    
    try {
      
      // Now, try to start a new transaction against our graph.
      t = new TinkerpopTransaction(g);
      
    } catch (UnsupportedOperationException e) {
      
      // If we're here, it's all good since we expected an exception to be thrown (since our
      // graph does NOT support transactions).
      return;
    }

    // If we're here then we were able to open a transaction even though our graph does 
    // not support that functionality.
    fail("Attempt to open a transaction against a graph with no transaction support should not succeed.");
  }
  
  
  /**
   * This test validates the behaviour when committing a transaction under various
   * circumstances.
   * @throws ChampTransactionException 
   */
  @Test
  public void testCommit() throws ChampTransactionException {
    
    // By default our test graph should simulate successful commits, but set
    // the configuration anyway, just to be sure.
    g.setFailCommits(false);
    
    // Now, start a transaction.
    TinkerpopTransaction t = new TinkerpopTransaction(g);
    
    // Call commit against the transaction - it should complete successfully.
    t.commit();
    
    // Now, configure our test graph to simulate failing to commit.
    g.setFailCommits(true);
    
    // Open another transaction...
    t = new TinkerpopTransaction(g);
    boolean exceptionThrown = false;
    try {
      
      //...and try to commit it.
      t.commit();
      
    } catch (Throwable e) {
      
      // Our commit should have failed and ultimately thrown an exception - so if we
      // are here then it's all good.
      exceptionThrown = true;
    }
    
    assertTrue("Failed commit should have produced an exception.", exceptionThrown);
  }

  
  @Test
  public void testRollback() throws ChampTransactionException {
    
    // By default our test graph should simulate successful commits, but set
    // the configuration anyway, just to be sure.
    g.setFailCommits(false);
    
    // Now, start a transaction.
    TinkerpopTransaction t = new TinkerpopTransaction(g);
    
    // Call rollback against the transaction - it should complete successfully.
    t.rollback();
    
    // Now, configure our test graph to simulate failing to commit.
    g.setFailCommits(true);
    
    // Open another transaction...
    t = new TinkerpopTransaction(g);
    boolean exceptionThrown = false;
    try {
      
      //...and try to commit it.
      t.rollback();
      
    } catch (Throwable e) {
      
      // Our commit should have failed and ultimately thrown an exception - so if we
      // are here then it's all good.
      exceptionThrown = true;
    }
    
    assertTrue("Failed rollback should have produced an exception.", exceptionThrown);
  }

  @Test
  public void test() throws ChampTransactionException {
    
    AbstractTinkerpopChampGraph graph = new AbstractTinkerpopChampGraph(new HashMap<String, Object>()) { 
      
      @Override
      protected Graph getGraph() {
        return TinkerGraph.open();
      }

      @Override
      protected ChampSchemaEnforcer getSchemaEnforcer() {
        return null;
      }

      @Override
      public void executeStoreObjectIndex(ChampObjectIndex index) { }

      @Override
      public Optional<ChampObjectIndex> retrieveObjectIndex(String indexName) {
        return null;
      }

      @Override
      public Stream<ChampObjectIndex> retrieveObjectIndices() {
        return null;
      }

      @Override
      public void executeDeleteObjectIndex(String indexName) throws ChampIndexNotExistsException {}

      @Override
      public void executeStoreRelationshipIndex(ChampRelationshipIndex index) {}

      @Override
      public Optional<ChampRelationshipIndex> retrieveRelationshipIndex(String indexName) {
        return null;
      }

      @Override
      public Stream<ChampRelationshipIndex> retrieveRelationshipIndices() {
        return null;
      }

      @Override
      public void executeDeleteRelationshipIndex(String indexName) throws ChampIndexNotExistsException {}

      @Override
      public ChampCapabilities capabilities() {
        return null;
      }

	@Override
	public GraphTraversal<?, ?> hasLabel(GraphTraversal<?, ?> query, Object type) {
		// TODO Auto-generated method stub
		return null;
	}
    };
    
    TinkerpopTransaction t = new TinkerpopTransaction(g);
    t.id();
    graph.commitTransaction(t);
    graph.rollbackTransaction(t);
    
  }
  
  private class TestGraph implements Graph {

    private boolean supportsTransactions = true;
    private boolean failCommits = false;
    
    
    public void setTransactionSupport(boolean supportsTransactions) {
      this.supportsTransactions = supportsTransactions;
    }
    
    public void setFailCommits(boolean failCommits) {
      this.failCommits = failCommits;
    }
    @Override
    public Vertex addVertex(Object... keyValues) {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public void close() throws Exception {
      // TODO Auto-generated method stub
      
    }

    @Override
    public GraphComputer compute() throws IllegalArgumentException {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public <C extends GraphComputer> C compute(Class<C> graphComputerClass)
        throws IllegalArgumentException {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public Configuration configuration() {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public Iterator<Edge> edges(Object... edgeIds) {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public Transaction tx() {
      return new TestTransaction();
    }

    @Override
    public Variables variables() {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public Iterator<Vertex> vertices(Object... vertexIds) {
      // TODO Auto-generated method stub
      return null;
    }
    
    /**
     * Gets the {@link Features} exposed by the underlying {@code Graph} implementation.
     */
    public Features features() {
      return new TestFeatures() {
      };
    }
    
    public class TestFeatures implements Graph.Features {
      
      /**
       * Gets the features related to "graph" operation.
       */
      public GraphFeatures graph() {
          return new TestGraphFeatures() {
          };
      }
      
      public class TestGraphFeatures implements Graph.Features.GraphFeatures {
        
        /**
         * Determines if the {@code Graph} implementations supports transactions.
         */
        @FeatureDescriptor(name = FEATURE_TRANSACTIONS)
        public boolean supportsTransactions() {
            return supportsTransactions;
        }
      }
    }
  }
  
  private class TestTransaction implements Transaction {

    @Override
    public void addTransactionListener(Consumer<Status> listener) {
      // TODO Auto-generated method stub
      
    }

    @Override
    public void clearTransactionListeners() {
      // TODO Auto-generated method stub
      
    }

    @Override
    public void close() {
      // TODO Auto-generated method stub
      
    }

    @Override
    public void commit() {
      
      if(g.failCommits) {
        throw new UnsupportedOperationException();
      } 
    }

    @Override
    public <G extends Graph> G createThreadedTx() {
     return (G) g;
    }

    @Override
    public boolean isOpen() {
      // TODO Auto-generated method stub
      return false;
    }

    @Override
    public Transaction onClose(Consumer<Transaction> consumer) {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public Transaction onReadWrite(Consumer<Transaction> consumer) {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public void open() {
      // TODO Auto-generated method stub
      
    }

    @Override
    public void readWrite() {
      // TODO Auto-generated method stub
      
    }

    @Override
    public void removeTransactionListener(Consumer<Status> listener) {
      // TODO Auto-generated method stub
      
    }

    @Override
    public void rollback() {
      if(g.failCommits) {
        throw new UnsupportedOperationException();
      } 
    }

    @Override
    public <R> Workload<R> submit(Function<Graph, R> work) {
      // TODO Auto-generated method stub
      return null;
    }
    
  }

  private class TestTinkerpopGraph extends AbstractTinkerpopChampGraph {

    protected TestTinkerpopGraph(Map<String, Object> properties) {
      super(properties);
    }

    @Override
    protected Graph getGraph() {
      return TinkerGraph.open();
    }

    
    @Override
    protected ChampSchemaEnforcer getSchemaEnforcer() {
      return null;
    }

    @Override
    public void executeStoreObjectIndex(ChampObjectIndex index) {      
    }

    @Override
    public Optional<ChampObjectIndex> retrieveObjectIndex(String indexName) {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public Stream<ChampObjectIndex> retrieveObjectIndices() {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public void executeDeleteObjectIndex(String indexName) throws ChampIndexNotExistsException {
      // TODO Auto-generated method stub
      
    }

    @Override
    public void executeStoreRelationshipIndex(ChampRelationshipIndex index) {
      // TODO Auto-generated method stub
      
    }

    @Override
    public Optional<ChampRelationshipIndex> retrieveRelationshipIndex(String indexName) {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public Stream<ChampRelationshipIndex> retrieveRelationshipIndices() {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public void executeDeleteRelationshipIndex(String indexName)
        throws ChampIndexNotExistsException {
      // TODO Auto-generated method stub
      
    }

    @Override
    public ChampCapabilities capabilities() {
      // TODO Auto-generated method stub
      return null;
    }

	@Override
	public GraphTraversal<?, ?> hasLabel(GraphTraversal<?, ?> query, Object type) {
		// TODO Auto-generated method stub
		return null;
	}
    
  }
}