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 org.junit.Test;
24 import org.onap.aai.champcore.ChampAPI;
25 import org.onap.aai.champcore.ChampGraph;
26 import org.onap.aai.champcore.exceptions.*;
27 import org.onap.aai.champcore.model.ChampField;
28 import org.onap.aai.champcore.model.ChampRelationship;
29 import org.onap.aai.champcore.model.ChampRelationshipIndex;
31 import java.util.Collection;
32 import java.util.Optional;
33 import java.util.stream.Collectors;
34 import java.util.stream.Stream;
36 import static org.junit.Assert.assertTrue;
38 public class ChampRelationshipIndexTest extends BaseChampAPITest {
41 public void runInMemoryTest() {
45 public void runTest(String apiType) {
46 final String graphName = ChampRelationshipIndexTest.class.getSimpleName();
48 final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
49 testChampRelationshipIndexCrud(api.getGraph(graphName));
53 public void testChampRelationshipIndexCrud(ChampGraph graph) {
55 final ChampField relationshipField = new ChampField.Builder("propertyName").build();
56 final ChampRelationshipIndex relationshipIndex = new ChampRelationshipIndex.Builder("fooEdgeIndex", "foo", relationshipField).build();
58 //Test on an empty graph
59 testChampRelationshipIndexStorage(graph, relationshipIndex);
60 testChampRelationshipIndexDelete(graph, relationshipIndex);
62 //Test with existing data in graph
64 graph.storeRelationship(ChampRelationship.create()
77 testChampRelationshipIndexStorage(graph, relationshipIndex);
78 testChampRelationshipIndexDelete(graph, relationshipIndex);
79 } catch (ChampMarshallingException e) {
80 throw new AssertionError(e);
81 } catch (ChampSchemaViolationException e) {
82 throw new AssertionError(e);
83 } catch (ChampObjectNotExistsException e) {
84 throw new AssertionError(e);
85 } catch (ChampRelationshipNotExistsException e) {
86 throw new AssertionError(e);
87 } catch (ChampUnmarshallingException e) {
88 throw new AssertionError(e);
89 } catch (ChampTransactionException e) {
90 throw new AssertionError(e);
94 private void testChampRelationshipIndexDelete(ChampGraph graph, ChampRelationshipIndex relationshipIndex) {
96 if (!graph.capabilities().canDeleteRelationshipIndices()) {
98 graph.deleteRelationshipIndex("someindex");
99 throw new AssertionError("Graph claims it doesn't support relationship index delete, but it failed to throw UnsupportedOperationException");
100 } catch (UnsupportedOperationException e) {
102 } catch (ChampIndexNotExistsException e) {
103 throw new AssertionError("Graph claims it doesn't support relationship index delete, but it failed to throw UnsupportedOperationException");
107 graph.deleteRelationshipIndex(relationshipIndex.getName());
109 final Optional<ChampRelationshipIndex> retrieveRelationshipIndex = graph.retrieveRelationshipIndex(relationshipIndex.getName());
111 if (retrieveRelationshipIndex.isPresent()) {
112 throw new AssertionError("Retrieve relationship index after deleting it");
115 final Stream<ChampRelationshipIndex> relationshipIndices = graph.retrieveRelationshipIndices();
116 final Collection<ChampRelationshipIndex> allRelationshipIndices = relationshipIndices.collect(Collectors.toList());
118 if (allRelationshipIndices.contains(relationshipIndex)) {
119 throw new AssertionError("Retrieve all relationship indices contains previously deleted index");
121 if (allRelationshipIndices.size() != 0) {
122 throw new AssertionError("Wrong number of relationship indices returned by retrieve all indices");
124 } catch (ChampIndexNotExistsException e) {
125 throw new AssertionError(e);
129 graph.deleteRelationshipIndex(relationshipIndex.getName());
130 throw new AssertionError("Failed to throw exception on non-existent object index");
131 } catch (ChampIndexNotExistsException e) {
137 private void testChampRelationshipIndexStorage(ChampGraph graph, ChampRelationshipIndex relationshipIndex) {
139 graph.storeRelationshipIndex(relationshipIndex);
140 graph.storeRelationshipIndex(relationshipIndex); //Test storing duplicate relationship index
142 assertTrue(!graph.retrieveObjectIndex(relationshipIndex.getName()).isPresent()); //Make sure this wasn't stored as an object index
144 final Optional<ChampRelationshipIndex> retrieveRelationshipIndex = graph.retrieveRelationshipIndex(relationshipIndex.getName());
146 if (!retrieveRelationshipIndex.isPresent()) {
147 throw new AssertionError("Failed to retrieve relationship index after storing it");
149 if (!relationshipIndex.equals(retrieveRelationshipIndex.get())) {
150 throw new AssertionError("Non-equal relationship index returned from API after storing it");
153 final Stream<ChampRelationshipIndex> relationshipIndices = graph.retrieveRelationshipIndices();
154 final Collection<ChampRelationshipIndex> allRelationshipIndices = relationshipIndices.collect(Collectors.toList());
156 if (!allRelationshipIndices.contains(relationshipIndex)) {
157 throw new AssertionError("Retrieve all relationship indices did not return previously stored relationship index");
159 if (allRelationshipIndices.size() != 1) {
160 throw new AssertionError("Wrong number of relationship indices returned by retrieve all indices");
163 assertTrue(!graph.retrieveRelationshipIndex("nonExistentIndexName").isPresent());
167 public void testFluentRelationshipIndexCreation() {
168 final ChampRelationshipIndex relationshipIndex = ChampRelationshipIndex.create()
169 .ofName("fooNameIndex")
174 assertTrue(relationshipIndex.getName().equals("fooNameIndex"));
175 assertTrue(relationshipIndex.getType().equals("foo"));
176 assertTrue(relationshipIndex.getField().getName().equals("name"));