[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 / ChampRelationshipIndexTest.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.Collection;
28 import java.util.Optional;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
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.exceptions.ChampMarshallingException;
36 import org.onap.aai.champcore.exceptions.ChampObjectNotExistsException;
37 import org.onap.aai.champcore.exceptions.ChampRelationshipNotExistsException;
38 import org.onap.aai.champcore.exceptions.ChampSchemaViolationException;
39 import org.onap.aai.champcore.exceptions.ChampTransactionException;
40 import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
41 import org.onap.aai.champcore.model.ChampField;
42 import org.onap.aai.champcore.model.ChampRelationship;
43 import org.onap.aai.champcore.model.ChampRelationshipIndex;
44
45 public class ChampRelationshipIndexTest extends BaseChampAPITest {
46
47   @Test
48   public void runInMemoryTest() {
49     runTest("IN_MEMORY");
50   }
51
52   public void runTest(String apiType) {
53     final String graphName = ChampRelationshipIndexTest.class.getSimpleName();
54
55     final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
56     testChampRelationshipIndexCrud(api.getGraph(graphName));
57     api.shutdown();
58   }
59
60   public void testChampRelationshipIndexCrud(ChampGraph graph) {
61
62     final ChampField relationshipField = new ChampField.Builder("propertyName").build();
63     final ChampRelationshipIndex relationshipIndex = new ChampRelationshipIndex.Builder("fooEdgeIndex", "foo", relationshipField).build();
64
65     //Test on an empty graph
66     testChampRelationshipIndexStorage(graph, relationshipIndex);
67     testChampRelationshipIndexDelete(graph, relationshipIndex);
68
69     //Test with existing data in graph
70     try {
71       graph.storeRelationship(ChampRelationship.create()
72           .ofType("uses")
73           .withoutKey()
74           .withSource()
75           .ofType("foo")
76           .withoutKey()
77           .build()
78           .withTarget()
79           .ofType("bar")
80           .withoutKey()
81           .build()
82           .build()
83         , Optional.empty());
84       testChampRelationshipIndexStorage(graph, relationshipIndex);
85       testChampRelationshipIndexDelete(graph, relationshipIndex);
86     } catch (ChampMarshallingException e) {
87       throw new AssertionError(e);
88     } catch (ChampSchemaViolationException e) {
89       throw new AssertionError(e);
90     } catch (ChampObjectNotExistsException e) {
91       throw new AssertionError(e);
92     } catch (ChampRelationshipNotExistsException e) {
93       throw new AssertionError(e);
94     } catch (ChampUnmarshallingException e) {
95       throw new AssertionError(e);
96     } catch (ChampTransactionException e) {
97       throw new AssertionError(e);
98     }
99   }
100
101   private void testChampRelationshipIndexDelete(ChampGraph graph, ChampRelationshipIndex relationshipIndex) {
102
103     if (!graph.capabilities().canDeleteRelationshipIndices()) {
104       try {
105         graph.deleteRelationshipIndex("someindex");
106         throw new AssertionError("Graph claims it doesn't support relationship index delete, but it failed to throw UnsupportedOperationException");
107       } catch (UnsupportedOperationException e) {
108         //Expected
109       } catch (ChampIndexNotExistsException e) {
110         throw new AssertionError("Graph claims it doesn't support relationship index delete, but it failed to throw UnsupportedOperationException");
111       }
112     } else {
113       try {
114         graph.deleteRelationshipIndex(relationshipIndex.getName());
115
116         final Optional<ChampRelationshipIndex> retrieveRelationshipIndex = graph.retrieveRelationshipIndex(relationshipIndex.getName());
117
118         if (retrieveRelationshipIndex.isPresent()) {
119           throw new AssertionError("Retrieve relationship index after deleting it");
120         }
121
122         final Stream<ChampRelationshipIndex> relationshipIndices = graph.retrieveRelationshipIndices();
123         final Collection<ChampRelationshipIndex> allRelationshipIndices = relationshipIndices.collect(Collectors.toList());
124
125         if (allRelationshipIndices.contains(relationshipIndex)) {
126           throw new AssertionError("Retrieve all relationship indices contains previously deleted index");
127         }
128         if (allRelationshipIndices.size() != 0) {
129           throw new AssertionError("Wrong number of relationship indices returned by retrieve all indices");
130         }
131       } catch (ChampIndexNotExistsException e) {
132         throw new AssertionError(e);
133       }
134
135       try {
136         graph.deleteRelationshipIndex(relationshipIndex.getName());
137         throw new AssertionError("Failed to throw exception on non-existent object index");
138       } catch (ChampIndexNotExistsException e) {
139         //Expected
140       }
141     }
142   }
143
144   private void testChampRelationshipIndexStorage(ChampGraph graph, ChampRelationshipIndex relationshipIndex) {
145
146     graph.storeRelationshipIndex(relationshipIndex);
147     graph.storeRelationshipIndex(relationshipIndex); //Test storing duplicate relationship index
148
149     assertTrue(!graph.retrieveObjectIndex(relationshipIndex.getName()).isPresent()); //Make sure this wasn't stored as an object index
150
151     final Optional<ChampRelationshipIndex> retrieveRelationshipIndex = graph.retrieveRelationshipIndex(relationshipIndex.getName());
152
153     if (!retrieveRelationshipIndex.isPresent()) {
154       throw new AssertionError("Failed to retrieve relationship index after storing it");
155     }
156     if (!relationshipIndex.equals(retrieveRelationshipIndex.get())) {
157       throw new AssertionError("Non-equal relationship index returned from API after storing it");
158     }
159
160     final Stream<ChampRelationshipIndex> relationshipIndices = graph.retrieveRelationshipIndices();
161     final Collection<ChampRelationshipIndex> allRelationshipIndices = relationshipIndices.collect(Collectors.toList());
162
163     if (!allRelationshipIndices.contains(relationshipIndex)) {
164       throw new AssertionError("Retrieve all relationship indices did not return previously stored relationship index");
165     }
166     if (allRelationshipIndices.size() != 1) {
167       throw new AssertionError("Wrong number of relationship indices returned by retrieve all indices");
168     }
169
170     assertTrue(!graph.retrieveRelationshipIndex("nonExistentIndexName").isPresent());
171   }
172
173   @Test
174   public void testFluentRelationshipIndexCreation() {
175     final ChampRelationshipIndex relationshipIndex = ChampRelationshipIndex.create()
176         .ofName("fooNameIndex")
177         .onType("foo")
178         .forField("name")
179         .build();
180
181     assertTrue(relationshipIndex.getName().equals("fooNameIndex"));
182     assertTrue(relationshipIndex.getType().equals("foo"));
183     assertTrue(relationshipIndex.getField().getName().equals("name"));
184   }
185
186
187       @Test
188     public void verifyEqualsAndHashCodeMethods(){
189         ChampField champField1 = new ChampField.Builder("name").build();
190         ChampField champField2 = new ChampField.Builder("differentName").build();
191         ChampRelationshipIndex obj1 = new ChampRelationshipIndex.Builder("name","type",champField1).build();
192         ChampRelationshipIndex obj2 = new ChampRelationshipIndex.Builder("name","type",champField1).build();
193         ChampRelationshipIndex obj3 = new ChampRelationshipIndex.Builder("name","type",champField1).build();
194         ChampRelationshipIndex obj4 = new ChampRelationshipIndex.Builder("name","type",champField2).build();
195
196
197         // if
198         assertEquals(obj1, obj2);
199         assertEquals(obj1.hashCode(), obj2.hashCode());
200         //and
201         assertEquals(obj1, obj3);
202         assertEquals(obj1.hashCode(), obj3.hashCode());
203         //then
204         assertEquals(obj2, obj3);
205         assertEquals(obj2.hashCode(), obj3.hashCode());
206
207         assertNotEquals(obj1, obj4);
208         assertNotEquals(obj1.hashCode(), obj4.hashCode());
209     }
210 }