bb2c65ec69bad964b879ab1c5acd4f230fb79a14
[aai/champ.git] / champ-lib / champ-core / src / test / java / org / onap / aai / champcore / core / ChampObjectIndexTest.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.champcore.core;
23
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.Collection;
27 import java.util.Optional;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
30
31 import org.junit.Test;
32 import org.onap.aai.champcore.ChampAPI;
33 import org.onap.aai.champcore.ChampGraph;
34 import org.onap.aai.champcore.exceptions.ChampIndexNotExistsException;
35 import org.onap.aai.champcore.model.ChampObjectIndex;
36
37 public class ChampObjectIndexTest extends BaseChampAPITest {
38         @Test
39   public void runTestMemory() {
40     runTest("IN_MEMORY");
41   }
42
43         public void runTest(String apiType) {
44                         final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
45                         final String graphName = api.getClass().getSimpleName();
46
47                         ChampObjectIndexTest.testChampObjectIndexCrud(api.getGraph(graphName));
48
49                         api.shutdown();
50         }
51
52         public static void testChampObjectIndexCrud(ChampGraph graph) {
53                 
54                 final ChampObjectIndex objectIndex = ChampObjectIndex.create()
55                                                                                                                                 .ofName("fooObjectIndex")
56                                                                                                                                 .onType("foo")
57                                                                                                                                 .forField("propertyName")
58                                                                                                                                 .build();
59
60                 testChampObjectIndexStorage(graph, objectIndex);
61                 testChampObjectIndexDelete(graph, objectIndex);
62         }
63
64         private static void testChampObjectIndexDelete(ChampGraph graph, ChampObjectIndex objectIndex) {
65
66                 if (!graph.capabilities().canDeleteObjectIndices()) {
67                         try {
68                                 graph.deleteObjectIndex("someindex");
69                                 throw new AssertionError("Graph claims it does not support object index delete, but failed to throw UnsupportedOperationException");
70                         } catch (UnsupportedOperationException e) {
71                         } catch (ChampIndexNotExistsException e) {
72                                 throw new AssertionError("Graph claims it does not support object index delete, but failed to throw UnsupportedOperationException");
73                         }
74                 } else {
75                         try {
76                                 graph.deleteObjectIndex(objectIndex.getName());
77                                 
78                                 final Optional<ChampObjectIndex> retrievedObjectIndex = graph.retrieveObjectIndex(objectIndex.getName());
79         
80                                 if (retrievedObjectIndex.isPresent()) throw new AssertionError("Retrieved object index after deleting it");
81         
82                                 final Stream<ChampObjectIndex> retrievedObjectIndices = graph.retrieveObjectIndices();
83                                 final Collection<ChampObjectIndex> allObjectIndices = retrievedObjectIndices.collect(Collectors.toList());
84         
85                                 if (allObjectIndices.contains(objectIndex)) throw new AssertionError("Retrieve all indices contained index previously deleted");
86                                 if (allObjectIndices.size() != 0) throw new AssertionError("Wrong number of indices returned by retrieve all indices");
87                         
88                         } catch (ChampIndexNotExistsException e) {
89                                 throw new AssertionError(e);
90                         }
91         
92                         try {
93                                 graph.deleteObjectIndex(objectIndex.getName());
94                                 throw new AssertionError("Failed to throw exception on non-existent object index");
95                         } catch (ChampIndexNotExistsException e) {
96                                 //Expected
97                         }
98                 }
99         }
100
101         private static void testChampObjectIndexStorage(ChampGraph graph, ChampObjectIndex objectIndex) {
102
103                 graph.storeObjectIndex(objectIndex);
104                 graph.storeObjectIndex(objectIndex); //Test storing an already existing object index
105
106                 assertTrue(!graph.retrieveRelationshipIndex(objectIndex.getName()).isPresent()); //Make sure this wasn't stored as an object index
107
108                 final Optional<ChampObjectIndex> retrieveObjectIndex = graph.retrieveObjectIndex(objectIndex.getName());
109                 
110                 if (!retrieveObjectIndex.isPresent()) throw new AssertionError("Failed to retrieve object index after storing it");
111                 if (!objectIndex.equals(retrieveObjectIndex.get())) throw new AssertionError("Non-equal object index returned from API after storing it");
112                 
113                 final Stream<ChampObjectIndex> retrievedObjectIndices = graph.retrieveObjectIndices();
114                 final Collection<ChampObjectIndex> allObjectIndices = retrievedObjectIndices.collect(Collectors.toList());
115
116                 if (!allObjectIndices.contains(objectIndex)) throw new AssertionError("Retrieve all indices did not contained index previously stored");
117                 if (allObjectIndices.size() != 1) throw new AssertionError("Wrong number of indices returned by retrieve all indices");
118
119                 assertTrue(!graph.retrieveObjectIndex("nonExistentIndexName").isPresent());
120         }
121
122         @Test
123         public void testFluentRelationshipCreation() {
124                 final ChampObjectIndex objectIndex = ChampObjectIndex.create()
125                                                                                                                                 .ofName("fooNameIndex")
126                                                                                                                                 .onType("foo")
127                                                                                                                                 .forField("name")
128                                                                                                                                 .build();
129
130                 assertTrue(objectIndex.getName().equals("fooNameIndex"));
131                 assertTrue(objectIndex.getType().equals("foo"));
132                 assertTrue(objectIndex.getField().getName().equals("name"));
133         }
134 }