2 * ============LICENSE_START==========================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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============================================
21 package org.onap.aai.champcore.core;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotEquals;
25 import static org.junit.Assert.assertTrue;
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;
45 public class ChampRelationshipIndexTest extends BaseChampAPITest {
48 public void runInMemoryTest() {
52 public void runTest(String apiType) {
53 final String graphName = ChampRelationshipIndexTest.class.getSimpleName();
55 final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
56 testChampRelationshipIndexCrud(api.getGraph(graphName));
60 public void testChampRelationshipIndexCrud(ChampGraph graph) {
62 final ChampField relationshipField = new ChampField.Builder("propertyName").build();
63 final ChampRelationshipIndex relationshipIndex = new ChampRelationshipIndex.Builder("fooEdgeIndex", "foo", relationshipField).build();
65 //Test on an empty graph
66 testChampRelationshipIndexStorage(graph, relationshipIndex);
67 testChampRelationshipIndexDelete(graph, relationshipIndex);
69 //Test with existing data in graph
71 graph.storeRelationship(ChampRelationship.create()
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);
101 private void testChampRelationshipIndexDelete(ChampGraph graph, ChampRelationshipIndex relationshipIndex) {
103 if (!graph.capabilities().canDeleteRelationshipIndices()) {
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) {
109 } catch (ChampIndexNotExistsException e) {
110 throw new AssertionError("Graph claims it doesn't support relationship index delete, but it failed to throw UnsupportedOperationException");
114 graph.deleteRelationshipIndex(relationshipIndex.getName());
116 final Optional<ChampRelationshipIndex> retrieveRelationshipIndex = graph.retrieveRelationshipIndex(relationshipIndex.getName());
118 if (retrieveRelationshipIndex.isPresent()) {
119 throw new AssertionError("Retrieve relationship index after deleting it");
122 final Stream<ChampRelationshipIndex> relationshipIndices = graph.retrieveRelationshipIndices();
123 final Collection<ChampRelationshipIndex> allRelationshipIndices = relationshipIndices.collect(Collectors.toList());
125 if (allRelationshipIndices.contains(relationshipIndex)) {
126 throw new AssertionError("Retrieve all relationship indices contains previously deleted index");
128 if (allRelationshipIndices.size() != 0) {
129 throw new AssertionError("Wrong number of relationship indices returned by retrieve all indices");
131 } catch (ChampIndexNotExistsException e) {
132 throw new AssertionError(e);
136 graph.deleteRelationshipIndex(relationshipIndex.getName());
137 throw new AssertionError("Failed to throw exception on non-existent object index");
138 } catch (ChampIndexNotExistsException e) {
144 private void testChampRelationshipIndexStorage(ChampGraph graph, ChampRelationshipIndex relationshipIndex) {
146 graph.storeRelationshipIndex(relationshipIndex);
147 graph.storeRelationshipIndex(relationshipIndex); //Test storing duplicate relationship index
149 assertTrue(!graph.retrieveObjectIndex(relationshipIndex.getName()).isPresent()); //Make sure this wasn't stored as an object index
151 final Optional<ChampRelationshipIndex> retrieveRelationshipIndex = graph.retrieveRelationshipIndex(relationshipIndex.getName());
153 if (!retrieveRelationshipIndex.isPresent()) {
154 throw new AssertionError("Failed to retrieve relationship index after storing it");
156 if (!relationshipIndex.equals(retrieveRelationshipIndex.get())) {
157 throw new AssertionError("Non-equal relationship index returned from API after storing it");
160 final Stream<ChampRelationshipIndex> relationshipIndices = graph.retrieveRelationshipIndices();
161 final Collection<ChampRelationshipIndex> allRelationshipIndices = relationshipIndices.collect(Collectors.toList());
163 if (!allRelationshipIndices.contains(relationshipIndex)) {
164 throw new AssertionError("Retrieve all relationship indices did not return previously stored relationship index");
166 if (allRelationshipIndices.size() != 1) {
167 throw new AssertionError("Wrong number of relationship indices returned by retrieve all indices");
170 assertTrue(!graph.retrieveRelationshipIndex("nonExistentIndexName").isPresent());
174 public void testFluentRelationshipIndexCreation() {
175 final ChampRelationshipIndex relationshipIndex = ChampRelationshipIndex.create()
176 .ofName("fooNameIndex")
181 assertTrue(relationshipIndex.getName().equals("fooNameIndex"));
182 assertTrue(relationshipIndex.getType().equals("foo"));
183 assertTrue(relationshipIndex.getField().getName().equals("name"));
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();
198 assertEquals(obj1, obj2);
199 assertEquals(obj1.hashCode(), obj2.hashCode());
201 assertEquals(obj1, obj3);
202 assertEquals(obj1.hashCode(), obj3.hashCode());
204 assertEquals(obj2, obj3);
205 assertEquals(obj2.hashCode(), obj3.hashCode());
207 assertNotEquals(obj1, obj4);
208 assertNotEquals(obj1.hashCode(), obj4.hashCode());