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