aboutsummaryrefslogtreecommitdiffstats
path: root/champ-lib/champ-titan/src/test/java/org/onap/aai/champtitan/core/ChampTransactionTest.java
blob: aafc05307f46f30779f854917e8452a045740326 (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
/**
 * ============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.champtitan.core;

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

import java.util.Optional;
import java.util.concurrent.CountDownLatch;

import org.junit.Before;
import org.junit.Test;
import org.onap.aai.champcore.ChampTransaction;
import org.onap.aai.champcore.exceptions.ChampMarshallingException;
import org.onap.aai.champcore.exceptions.ChampObjectNotExistsException;
import org.onap.aai.champcore.exceptions.ChampRelationshipNotExistsException;
import org.onap.aai.champcore.exceptions.ChampSchemaViolationException;
import org.onap.aai.champcore.exceptions.ChampTransactionException;
import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
import org.onap.aai.champcore.model.ChampObject;
import org.onap.aai.champcore.model.ChampRelationship;
import org.onap.aai.champtitan.graph.impl.TitanChampGraphImpl;

public class ChampTransactionTest {

  public TitanChampGraphImpl graph = null;
  public CountDownLatch latch = new CountDownLatch(2);
  public ChampObject[] storedVertices = new ChampObject[2];

  
  @Before
  public void setup() {
    graph = new TitanChampGraphImpl.Builder("TransactionTestGraph")
      .property("storage.backend", "inmemory")
      .build();
  }
  
  
  /**
   * This test validates that multiple CRUD operations can be performed against vertices within
   * the same transactional context and that, once the transaction is committed, the final 
   * state of the vertices reflects the sum of all of the operations performed within the
   * transaction.
   * 
   * @throws ChampMarshallingException
   * @throws ChampSchemaViolationException
   * @throws ChampObjectNotExistsException
   * @throws ChampUnmarshallingException
   * @throws ChampTransactionException 
   */
  @Test
  public void champObjectSingleTxTest() throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException, ChampUnmarshallingException, ChampTransactionException {

    ChampObject v1 = ChampObject.create()
        .ofType("foo")
        .withoutKey()
        .withProperty("property1", "value1")
        .withProperty("property2", "value2")
        .build();
    
    ChampObject v2 = ChampObject.create()
        .ofType("foo")
        .withoutKey()
        .withProperty("property3", "value3")
        .withProperty("property4", "value4")
        .build();
    
    ChampObject v3 = ChampObject.create()
        .ofType("foo")
        .withoutKey()
        .withProperty("property5", "value5")
        .withProperty("property6", "value6")
        .build();
    
    ChampObject v4 = ChampObject.create()
        .ofType("foo")
        .withoutKey()
        .withProperty("property7", "value7")
        .withProperty("property8", "value8")
        .build();
    
    
    // Open a transaction with the graph data store.
    ChampTransaction tx = graph.openTransaction();
    
      // Create all of our vertices.
      ChampObject storedV1 = graph.storeObject(v1, Optional.of(tx));
      ChampObject storedV2 = graph.storeObject(v2, Optional.of(tx));
      ChampObject storedV3 = graph.storeObject(v3, Optional.of(tx));
      ChampObject storedV4 = graph.storeObject(v4, Optional.of(tx));
    
      // Now, within the same transactional context, do a replacement against one of the 
      // vertices that we just created.
      ChampObject replacedV2 = graph.replaceObject(ChampObject.create()
                                                       .ofType("foo")
                                                       .withKey(storedV2.getKey().get())
                                                       .withProperty("replacedProperty3", "replacedValue3")
                                                       .withProperty("replacedProperty4", "replacedValue4")
                                                       .build(), 
                                                   Optional.of(tx));
      
      // Within the same transactional context, do an update against one of the vertices
      // that we just created.
      final ChampObject updatedV3 = graph.storeObject(ChampObject.create()
                                                        .from(storedV3)
                                                        .withKey(storedV3.getKey().get())
                                                        .withProperty("updatedProperty", "updatedValue")
                                                        .build(), Optional.of(tx));
      
      // Within the same transactional context, delete one of the vertices that we just
      // created.
      graph.deleteObject(storedV4.getKey().get(), Optional.of(tx));
      
    // Finally, commit our transaction.
    tx.commit();
    
    tx = graph.openTransaction();
    
      Optional<ChampObject> retrievedV1 = graph.retrieveObject(storedV1.getKey().get(), Optional.of(tx));
      assertTrue(retrievedV1.isPresent());
      assertTrue(retrievedV1.get().getProperty("property1").get().equals("value1"));
      assertTrue(retrievedV1.get().getProperty("property2").get().equals("value2"));

      
      Optional<ChampObject> retrievedV2 = graph.retrieveObject(storedV2.getKey().get(), Optional.of(tx));
      assertTrue(retrievedV2.isPresent());
      assertTrue(retrievedV2.get().getProperty("replacedProperty3").get().equals("replacedValue3"));
      assertTrue(retrievedV2.get().getProperty("replacedProperty4").get().equals("replacedValue4"));
      assertFalse(retrievedV2.get().getProperty("value3").isPresent());
      assertFalse(retrievedV2.get().getProperty("value4").isPresent());
      
      Optional<ChampObject> retrievedV3 = graph.retrieveObject(storedV3.getKey().get(), Optional.of(tx));
      assertTrue(retrievedV3.isPresent());
      assertTrue(retrievedV3.get().getProperty("property5").get().equals("value5"));
      assertTrue(retrievedV3.get().getProperty("property6").get().equals("value6"));
      assertTrue(retrievedV3.get().getProperty("updatedProperty").get().equals("updatedValue"));

      
      Optional<ChampObject> retrievedV4 = graph.retrieveObject(storedV4.getKey().get(), Optional.of(tx));
      assertFalse("Deleted vertex should not be present in graph", retrievedV4.isPresent());
      
    tx.commit();
  }
  

  /**
   * This test validates that multiple threads can each open their own transactions with the 
   * graph data store, and that there is no leakage between each thread's transactions.
   * 
   * @throws ChampMarshallingException
   * @throws ChampSchemaViolationException
   * @throws ChampObjectNotExistsException
   * @throws ChampUnmarshallingException
   * @throws ChampTransactionException 
   */
  @Test
  public void multipleTransactionTest() throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException, ChampUnmarshallingException, ChampTransactionException {
    
    ChampObject v1 = ChampObject.create()
        .ofType("foo")
        .withoutKey()
        .withProperty("property1", "value1")
        .withProperty("property2", "value2")
        .build();
    
    ChampObject v2 = ChampObject.create()
        .ofType("bar")
        .withoutKey()
        .withProperty("property3", "value3")
        .withProperty("property4", "value4")
        .build();
       
    // Instantiate and start our two transactional worker threads...
    Thread thread1 = new Thread(new VertexWriter(v1, 0));
    Thread thread2 = new Thread(new VertexWriter(v2, 1));
    thread1.start();
    thread2.start();
    
    // and wait for the threads to complete.
    try {
      thread1.join();
      thread2.join();
      
    } catch (InterruptedException e) { }
        
    // Now that all of our data has been committed, let's open a new transaction
    // and verify that all of our vertices can be retrieved.
    ChampTransaction tx3 = graph.openTransaction();

    Optional<ChampObject> retrievedV1 = graph.retrieveObject(storedVertices[0].getKey().get(), Optional.of(tx3));
    assertTrue(retrievedV1.isPresent());
    Optional<ChampObject> retrievedV2 = graph.retrieveObject(storedVertices[1].getKey().get(), Optional.of(tx3));
    assertTrue(retrievedV2.isPresent());
    
    tx3.commit();
  }

  
  /**
   * This method validates that edges can be successfully created within a single transaction.
   * 
   * @throws ChampMarshallingException
   * @throws ChampSchemaViolationException
   * @throws ChampObjectNotExistsException
   * @throws ChampUnmarshallingException
   * @throws ChampRelationshipNotExistsException
   * @throws ChampTransactionException 
   */
  @Test
  public void edgeTest() throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException, ChampUnmarshallingException, ChampRelationshipNotExistsException, ChampTransactionException {
    
    // Create the source and target vertices for our edge.
    final ChampObject source = ChampObject.create()
        .ofType("foo")
        .withoutKey()
        .withProperty("property1", "value1")
        .build();

    final ChampObject target = ChampObject.create()
        .ofType("foo")
        .withoutKey()
        .build();

    // Open a transaction with the graph data store.
    ChampTransaction tx = graph.openTransaction();
    
      // Now, create our vertices.
      ChampObject storedSource = graph.storeObject(source, Optional.of(tx));
      ChampObject storedTarget = graph.storeObject(target, Optional.of(tx));
      
      // Create the edge between the vertices.
      ChampRelationship relationship = new ChampRelationship.Builder(storedSource, storedTarget, "relationship")
          .property("property-1", "value-1")
          .property("property-2", 3)
          .build();
      ChampRelationship storedRelationship = graph.storeRelationship(relationship, Optional.of(tx));
      
      // Validate that we can read back the edge within the transactional context.
      Optional<ChampRelationship> retrievedRelationship = graph.retrieveRelationship(storedRelationship.getKey().get(), Optional.of(tx));
      assertTrue("Failed to retrieve stored relationship", retrievedRelationship.isPresent());
      
    // Commit our transaction.
    graph.commitTransaction(tx);
    
    // Now, open a new transaction.
    tx = graph.openTransaction();
    
      // Now, read back the edge that we just created again, validating that it was
      // successfully committed to the graph.
      retrievedRelationship = graph.retrieveRelationship(storedRelationship.getKey().get(), Optional.of(tx));
      assertTrue("Failed to retrieve stored relationship", retrievedRelationship.isPresent());

    graph.commitTransaction(tx);
  }
  
  private class VertexWriter implements Runnable {
    
    ChampObject vertex;
    int         index;
    
    public VertexWriter(ChampObject vertex, int index) {
      this.vertex = vertex;
      this.index  = index;
    }
    
    public void run() {
      
      ChampTransaction tx=null;
      try {
        
        // Open a threaded transaction to do some work in.
        tx = graph.openTransaction();
        
        // Now store one of our two vertices within the context of this transaction.
        storedVertices[index] = graph.storeObject(vertex, Optional.of(tx));
        
        // Use our latch to indicate that we are done creating vertices, and wait for
        // the other thread to do the same.          
        latch.countDown();
        latch.await();
        
        // Validate that the vertex we created is visible to us in the graph, but the
        // one that the other thread created is not.          
        Optional<ChampObject> retrievedV2 = graph.retrieveObject(storedVertices[index].getKey().get(), Optional.of(tx));
        assertTrue(retrievedV2.isPresent());
        Optional<ChampObject> retrievedV1 = graph.retrieveObject(storedVertices[(index+1)%2].getKey().get(), Optional.of(tx));
        assertFalse(retrievedV1.isPresent());
                
      } catch (InterruptedException | 
               ChampUnmarshallingException | 
               ChampMarshallingException | 
               ChampSchemaViolationException | 
               ChampObjectNotExistsException | ChampTransactionException e) {
        
        fail("Thread failed to interact with graph due to " + e.getMessage());
      
      } finally {
        
        try {
          Thread.sleep(index * 500);
        } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        // Now, commit our transaction and bail...          
        try {
          graph.commitTransaction(tx);
        } catch (ChampTransactionException e) {

        }
      }
    }
  };
}