2 * ============LICENSE_START==========================================
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
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============================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.openecomp.aai.champ.core;
24 import static org.junit.Assert.assertTrue;
26 import java.util.Collection;
27 import java.util.Optional;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
31 import org.junit.Test;
32 import org.openecomp.aai.champ.ChampAPI;
33 import org.openecomp.aai.champ.ChampGraph;
34 import org.openecomp.aai.champ.exceptions.ChampIndexNotExistsException;
35 import org.openecomp.aai.champ.exceptions.ChampMarshallingException;
36 import org.openecomp.aai.champ.exceptions.ChampObjectNotExistsException;
37 import org.openecomp.aai.champ.exceptions.ChampRelationshipNotExistsException;
38 import org.openecomp.aai.champ.exceptions.ChampSchemaViolationException;
39 import org.openecomp.aai.champ.exceptions.ChampUnmarshallingException;
40 import org.openecomp.aai.champ.model.ChampField;
41 import org.openecomp.aai.champ.model.ChampRelationship;
42 import org.openecomp.aai.champ.model.ChampRelationshipIndex;
44 public class ChampRelationshipIndexTest extends BaseChampAPITest {
47 public void runTest() {
48 for (ChampGraph.Type apiType : ChampGraph.Type.values()) {
49 final String graphName = ChampRelationshipIndexTest.class.getSimpleName();
61 final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
62 testChampRelationshipIndexCrud(api.getGraph(graphName));
67 private void testChampRelationshipIndexCrud(ChampGraph graph) {
69 final ChampField relationshipField = new ChampField.Builder("propertyName").build();
70 final ChampRelationshipIndex relationshipIndex = new ChampRelationshipIndex.Builder("fooEdgeIndex", "foo", relationshipField).build();
72 //Test on an empty graph
73 testChampRelationshipIndexStorage(graph, relationshipIndex);
74 testChampRelationshipIndexDelete(graph, relationshipIndex);
76 //Test with existing data in graph
78 graph.storeRelationship(ChampRelationship.create()
90 testChampRelationshipIndexStorage(graph, relationshipIndex);
91 testChampRelationshipIndexDelete(graph, relationshipIndex);
92 } catch (ChampMarshallingException e) {
93 throw new AssertionError(e);
94 } catch (ChampSchemaViolationException e) {
95 throw new AssertionError(e);
96 } catch (ChampObjectNotExistsException e) {
97 throw new AssertionError(e);
98 } catch (ChampRelationshipNotExistsException e) {
99 throw new AssertionError(e);
100 } catch (ChampUnmarshallingException e) {
101 throw new AssertionError(e);
105 private void testChampRelationshipIndexDelete(ChampGraph graph, ChampRelationshipIndex relationshipIndex) {
107 if (!graph.capabilities().canDeleteRelationshipIndices()) {
109 graph.deleteRelationshipIndex("someindex");
110 throw new AssertionError("Graph claims it doesn't support relationship index delete, but it failed to throw UnsupportedOperationException");
111 } catch (UnsupportedOperationException e) {
113 } catch (ChampIndexNotExistsException e) {
114 throw new AssertionError("Graph claims it doesn't support relationship index delete, but it failed to throw UnsupportedOperationException");
118 graph.deleteRelationshipIndex(relationshipIndex.getName());
120 final Optional<ChampRelationshipIndex> retrieveRelationshipIndex = graph.retrieveRelationshipIndex(relationshipIndex.getName());
122 if (retrieveRelationshipIndex.isPresent()) throw new AssertionError("Retrieve relationship index after deleting it");
124 final Stream<ChampRelationshipIndex> relationshipIndices = graph.retrieveRelationshipIndices();
125 final Collection<ChampRelationshipIndex> allRelationshipIndices = relationshipIndices.collect(Collectors.toList());
127 if (allRelationshipIndices.contains(relationshipIndex)) throw new AssertionError("Retrieve all relationship indices contains previously deleted index");
128 if (allRelationshipIndices.size() != 0) throw new AssertionError("Wrong number of relationship indices returned by retrieve all indices");
129 } catch (ChampIndexNotExistsException e) {
130 throw new AssertionError(e);
134 graph.deleteRelationshipIndex(relationshipIndex.getName());
135 throw new AssertionError("Failed to throw exception on non-existent object index");
136 } catch (ChampIndexNotExistsException e) {
142 private void testChampRelationshipIndexStorage(ChampGraph graph, ChampRelationshipIndex relationshipIndex) {
144 graph.storeRelationshipIndex(relationshipIndex);
145 graph.storeRelationshipIndex(relationshipIndex); //Test storing duplicate relationship index
147 assertTrue(!graph.retrieveObjectIndex(relationshipIndex.getName()).isPresent()); //Make sure this wasn't stored as an object index
149 final Optional<ChampRelationshipIndex> retrieveRelationshipIndex = graph.retrieveRelationshipIndex(relationshipIndex.getName());
151 if (!retrieveRelationshipIndex.isPresent()) throw new AssertionError("Failed to retrieve relationship index after storing it");
152 if (!relationshipIndex.equals(retrieveRelationshipIndex.get())) throw new AssertionError("Non-equal relationship index returned from API after storing it");
154 final Stream<ChampRelationshipIndex> relationshipIndices = graph.retrieveRelationshipIndices();
155 final Collection<ChampRelationshipIndex> allRelationshipIndices = relationshipIndices.collect(Collectors.toList());
157 if (!allRelationshipIndices.contains(relationshipIndex)) throw new AssertionError("Retrieve all relationship indices did not return previously stored relationship index");
158 if (allRelationshipIndices.size() != 1) throw new AssertionError("Wrong number of relationship indices returned by retrieve all indices");
160 assertTrue(!graph.retrieveRelationshipIndex("nonExistentIndexName").isPresent());
164 public void testFluentRelationshipIndexCreation() {
165 final ChampRelationshipIndex relationshipIndex = ChampRelationshipIndex.create()
166 .ofName("fooNameIndex")
171 assertTrue(relationshipIndex.getName().equals("fooNameIndex"));
172 assertTrue(relationshipIndex.getType().equals("foo"));
173 assertTrue(relationshipIndex.getField().getName().equals("name"));