[AAI-2175] Change aai champ container processes to run as non-root on the host
[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.assertEquals;
24 import static org.junit.Assert.assertNotEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.List;
30 import java.util.Optional;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
33 import org.junit.Test;
34 import org.onap.aai.champcore.ChampAPI;
35 import org.onap.aai.champcore.ChampGraph;
36 import org.onap.aai.champcore.exceptions.ChampIndexNotExistsException;
37 import org.onap.aai.champcore.model.ChampField;
38 import org.onap.aai.champcore.model.ChampField.Type;
39 import org.onap.aai.champcore.model.ChampObjectIndex;
40 import org.onap.aai.champcore.model.ChampObjectIndex.Builder;
41
42 public class ChampObjectIndexTest extends BaseChampAPITest {
43         @Test
44   public void runTestMemory() {
45     runTest("IN_MEMORY");
46   }
47
48         public void runTest(String apiType) {
49                         final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
50                         final String graphName = api.getClass().getSimpleName();
51
52                         ChampObjectIndexTest.testChampObjectIndexCrud(api.getGraph(graphName));
53
54                         api.shutdown();
55         }
56
57         public static void testChampObjectIndexCrud(ChampGraph graph) {
58         List<String> fields = new ArrayList<String>();
59         fields.add("propertyName");             
60                 final ChampObjectIndex objectIndex = ChampObjectIndex.create()
61                                                                                                                                 .ofName("fooObjectIndex")
62                                                                                                                                 .onType("foo")
63                                                                                                                                 .forFields(fields)
64                                                                                                                                 .build();
65
66                 testChampObjectIndexStorage(graph, objectIndex);
67                 testChampObjectIndexDelete(graph, objectIndex);
68         }
69
70         private static void testChampObjectIndexDelete(ChampGraph graph, ChampObjectIndex objectIndex) {
71
72                 if (!graph.capabilities().canDeleteObjectIndices()) {
73                         try {
74                                 graph.deleteObjectIndex("someindex");
75                                 throw new AssertionError("Graph claims it does not support object index delete, but failed to throw UnsupportedOperationException");
76                         } catch (UnsupportedOperationException e) {
77                         } catch (ChampIndexNotExistsException e) {
78                                 throw new AssertionError("Graph claims it does not support object index delete, but failed to throw UnsupportedOperationException");
79                         }
80                 } else {
81                         try {
82                                 graph.deleteObjectIndex(objectIndex.getName());
83                                 
84                                 final Optional<ChampObjectIndex> retrievedObjectIndex = graph.retrieveObjectIndex(objectIndex.getName());
85         
86                                 if (retrievedObjectIndex.isPresent()) throw new AssertionError("Retrieved object index after deleting it");
87         
88                                 final Stream<ChampObjectIndex> retrievedObjectIndices = graph.retrieveObjectIndices();
89                                 final Collection<ChampObjectIndex> allObjectIndices = retrievedObjectIndices.collect(Collectors.toList());
90         
91                                 if (allObjectIndices.contains(objectIndex)) throw new AssertionError("Retrieve all indices contained index previously deleted");
92                                 if (allObjectIndices.size() != 0) throw new AssertionError("Wrong number of indices returned by retrieve all indices");
93                         
94                         } catch (ChampIndexNotExistsException e) {
95                                 throw new AssertionError(e);
96                         }
97         
98                         try {
99                                 graph.deleteObjectIndex(objectIndex.getName());
100                                 throw new AssertionError("Failed to throw exception on non-existent object index");
101                         } catch (ChampIndexNotExistsException e) {
102                                 //Expected
103                         }
104                 }
105         }
106
107         private static void testChampObjectIndexStorage(ChampGraph graph, ChampObjectIndex objectIndex) {
108
109                 graph.storeObjectIndex(objectIndex);
110                 graph.storeObjectIndex(objectIndex); //Test storing an already existing object index
111
112                 assertTrue(!graph.retrieveRelationshipIndex(objectIndex.getName()).isPresent()); //Make sure this wasn't stored as an object index
113
114                 final Optional<ChampObjectIndex> retrieveObjectIndex = graph.retrieveObjectIndex(objectIndex.getName());
115                 
116                 if (!retrieveObjectIndex.isPresent()) throw new AssertionError("Failed to retrieve object index after storing it");
117                 if (!objectIndex.equals(retrieveObjectIndex.get())) throw new AssertionError("Non-equal object index returned from API after storing it");
118                 
119                 final Stream<ChampObjectIndex> retrievedObjectIndices = graph.retrieveObjectIndices();
120                 final Collection<ChampObjectIndex> allObjectIndices = retrievedObjectIndices.collect(Collectors.toList());
121
122                 if (!allObjectIndices.contains(objectIndex)) throw new AssertionError("Retrieve all indices did not contained index previously stored");
123                 //if (allObjectIndices.size() != 1) throw new AssertionError("Wrong number of indices returned by retrieve all indices");
124
125                 assertTrue(!graph.retrieveObjectIndex("nonExistentIndexName").isPresent());
126         }
127
128         @Test
129         public void testFluentRelationshipCreation() {
130             List<String> fields = new ArrayList<String>();
131             fields.add("name");
132                 final ChampObjectIndex objectIndex = ChampObjectIndex.create()
133                                                                                                                                 .ofName("fooNameIndex")
134                                                                                                                                 .onType("foo")
135                                                                                                                                 .forFields(fields)
136                                                                                                                                 .build();
137
138                 assertTrue(objectIndex.getName().equals("fooNameIndex"));
139                 assertTrue(objectIndex.getType().equals("foo"));
140                 assertTrue(objectIndex.getFields().get(0).getName().equals("name"));
141         }
142
143         @Test
144         public void verifyEqualsAndHashCodeMethods() {
145                 ChampField champField1 = new ChampField.Builder("name").type(Type.STRING).build();
146                 ChampField champField2 = new ChampField.Builder("differentName").type(Type.STRING).build();
147                 List<ChampField> champFields1 = new ArrayList<ChampField>();
148                 champFields1.add(champField1);
149                 List<ChampField> champFields2 = new ArrayList<ChampField>();
150         champFields2.add(champField2);
151
152                 ChampObjectIndex obj1 = new Builder("name", "type", champFields1).build();
153                 ChampObjectIndex obj2 = new Builder("name", "type", champFields1).build();
154                 ChampObjectIndex obj3 = new Builder("name", "type", champFields1).build();
155                 ChampObjectIndex obj4 = new Builder("name", "type", champFields2).build();
156                 ChampObjectIndex obj5 = new Builder("differentName", "type", champFields1).build();
157
158                 // if
159                 assertEquals(obj1, obj2);
160                 assertEquals(obj1.hashCode(), obj2.hashCode());
161                 //and
162                 assertEquals(obj1, obj3);
163                 assertEquals(obj1.hashCode(), obj3.hashCode());
164                 //then
165                 assertEquals(obj2, obj3);
166                 assertEquals(obj2.hashCode(), obj3.hashCode());
167
168                 assertNotEquals(obj1, obj4);
169                 assertNotEquals(obj1.hashCode(), obj4.hashCode());
170
171                 assertNotEquals(obj1, obj5);
172                 assertNotEquals(obj1.hashCode(), obj5.hashCode());
173         }
174 }