Merge "Added @Override annotation above signature"
[aai/champ.git] / src / test / java / org / onap / aai / champ / 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.champ.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.champ.ChampAPI;
33 import org.onap.aai.champ.ChampGraph;
34 import org.onap.aai.champ.exceptions.ChampIndexNotExistsException;
35 import org.onap.aai.champ.model.ChampObjectIndex;
36
37 public class ChampObjectIndexTest extends BaseChampAPITest {
38         @Test
39         public void runTest() {
40                 for (ChampGraph.Type apiType : ChampGraph.Type.values()) {
41                         final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
42                         final String graphName = api.getClass().getSimpleName();
43
44                         switch (apiType) {
45                         case IN_MEMORY:
46                         break;
47                         case TITAN:
48                                 cleanUp(graphName);
49                         break;
50                         default:
51                         break;
52                         }
53
54                         ChampObjectIndexTest.testChampObjectIndexCrud(api.getGraph(graphName));
55
56                         api.shutdown();
57                 }
58         }
59
60         private static void testChampObjectIndexCrud(ChampGraph graph) {
61                 
62                 final ChampObjectIndex objectIndex = ChampObjectIndex.create()
63                                                                                                                                 .ofName("fooObjectIndex")
64                                                                                                                                 .onType("foo")
65                                                                                                                                 .forField("propertyName")
66                                                                                                                                 .build();
67
68                 testChampObjectIndexStorage(graph, objectIndex);
69                 testChampObjectIndexDelete(graph, objectIndex);
70         }
71
72         private static void testChampObjectIndexDelete(ChampGraph graph, ChampObjectIndex objectIndex) {
73
74                 if (!graph.capabilities().canDeleteObjectIndices()) {
75                         try {
76                                 graph.deleteObjectIndex("someindex");
77                                 throw new AssertionError("Graph claims it does not support object index delete, but failed to throw UnsupportedOperationException");
78                         } catch (UnsupportedOperationException e) {
79                         } catch (ChampIndexNotExistsException e) {
80                                 throw new AssertionError("Graph claims it does not support object index delete, but failed to throw UnsupportedOperationException");
81                         }
82                 } else {
83                         try {
84                                 graph.deleteObjectIndex(objectIndex.getName());
85                                 
86                                 final Optional<ChampObjectIndex> retrievedObjectIndex = graph.retrieveObjectIndex(objectIndex.getName());
87         
88                                 if (retrievedObjectIndex.isPresent()) throw new AssertionError("Retrieved object index after deleting it");
89         
90                                 final Stream<ChampObjectIndex> retrievedObjectIndices = graph.retrieveObjectIndices();
91                                 final Collection<ChampObjectIndex> allObjectIndices = retrievedObjectIndices.collect(Collectors.toList());
92         
93                                 if (allObjectIndices.contains(objectIndex)) throw new AssertionError("Retrieve all indices contained index previously deleted");
94                                 if (allObjectIndices.size() != 0) throw new AssertionError("Wrong number of indices returned by retrieve all indices");
95                         
96                         } catch (ChampIndexNotExistsException e) {
97                                 throw new AssertionError(e);
98                         }
99         
100                         try {
101                                 graph.deleteObjectIndex(objectIndex.getName());
102                                 throw new AssertionError("Failed to throw exception on non-existent object index");
103                         } catch (ChampIndexNotExistsException e) {
104                                 //Expected
105                         }
106                 }
107         }
108
109         private static void testChampObjectIndexStorage(ChampGraph graph, ChampObjectIndex objectIndex) {
110
111                 graph.storeObjectIndex(objectIndex);
112                 graph.storeObjectIndex(objectIndex); //Test storing an already existing object index
113
114                 assertTrue(!graph.retrieveRelationshipIndex(objectIndex.getName()).isPresent()); //Make sure this wasn't stored as an object index
115
116                 final Optional<ChampObjectIndex> retrieveObjectIndex = graph.retrieveObjectIndex(objectIndex.getName());
117                 
118                 if (!retrieveObjectIndex.isPresent()) throw new AssertionError("Failed to retrieve object index after storing it");
119                 if (!objectIndex.equals(retrieveObjectIndex.get())) throw new AssertionError("Non-equal object index returned from API after storing it");
120                 
121                 final Stream<ChampObjectIndex> retrievedObjectIndices = graph.retrieveObjectIndices();
122                 final Collection<ChampObjectIndex> allObjectIndices = retrievedObjectIndices.collect(Collectors.toList());
123
124                 if (!allObjectIndices.contains(objectIndex)) throw new AssertionError("Retrieve all indices did not contained index previously stored");
125                 if (allObjectIndices.size() != 1) throw new AssertionError("Wrong number of indices returned by retrieve all indices");
126
127                 assertTrue(!graph.retrieveObjectIndex("nonExistentIndexName").isPresent());
128         }
129
130         @Test
131         public void testFluentRelationshipCreation() {
132                 final ChampObjectIndex objectIndex = ChampObjectIndex.create()
133                                                                                                                                 .ofName("fooNameIndex")
134                                                                                                                                 .onType("foo")
135                                                                                                                                 .forField("name")
136                                                                                                                                 .build();
137
138                 assertTrue(objectIndex.getName().equals("fooNameIndex"));
139                 assertTrue(objectIndex.getType().equals("foo"));
140                 assertTrue(objectIndex.getField().getName().equals("name"));
141         }
142 }