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.onap.aai.champcore.core;
24 import org.junit.Test;
25 import org.onap.aai.champcore.ChampAPI;
26 import org.onap.aai.champcore.ChampGraph;
27 import org.onap.aai.champcore.exceptions.*;
28 import org.onap.aai.champcore.model.ChampField;
29 import org.onap.aai.champcore.model.ChampRelationship;
30 import org.onap.aai.champcore.model.ChampRelationshipIndex;
32 import java.util.Collection;
33 import java.util.Optional;
34 import java.util.stream.Collectors;
35 import java.util.stream.Stream;
37 import static org.junit.Assert.assertTrue;
39 public class ChampRelationshipIndexTest extends BaseChampAPITest {
42 public void runInMemoryTest() {
46 public void runTest(String apiType) {
47 final String graphName = ChampRelationshipIndexTest.class.getSimpleName();
49 final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
50 testChampRelationshipIndexCrud(api.getGraph(graphName));
54 public void testChampRelationshipIndexCrud(ChampGraph graph) {
56 final ChampField relationshipField = new ChampField.Builder("propertyName").build();
57 final ChampRelationshipIndex relationshipIndex = new ChampRelationshipIndex.Builder("fooEdgeIndex", "foo", relationshipField).build();
59 //Test on an empty graph
60 testChampRelationshipIndexStorage(graph, relationshipIndex);
61 testChampRelationshipIndexDelete(graph, relationshipIndex);
63 //Test with existing data in graph
65 graph.storeRelationship(ChampRelationship.create()
78 testChampRelationshipIndexStorage(graph, relationshipIndex);
79 testChampRelationshipIndexDelete(graph, relationshipIndex);
80 } catch (ChampMarshallingException e) {
81 throw new AssertionError(e);
82 } catch (ChampSchemaViolationException e) {
83 throw new AssertionError(e);
84 } catch (ChampObjectNotExistsException e) {
85 throw new AssertionError(e);
86 } catch (ChampRelationshipNotExistsException e) {
87 throw new AssertionError(e);
88 } catch (ChampUnmarshallingException e) {
89 throw new AssertionError(e);
90 } catch (ChampTransactionException e) {
91 throw new AssertionError(e);
95 private void testChampRelationshipIndexDelete(ChampGraph graph, ChampRelationshipIndex relationshipIndex) {
97 if (!graph.capabilities().canDeleteRelationshipIndices()) {
99 graph.deleteRelationshipIndex("someindex");
100 throw new AssertionError("Graph claims it doesn't support relationship index delete, but it failed to throw UnsupportedOperationException");
101 } catch (UnsupportedOperationException e) {
103 } catch (ChampIndexNotExistsException e) {
104 throw new AssertionError("Graph claims it doesn't support relationship index delete, but it failed to throw UnsupportedOperationException");
108 graph.deleteRelationshipIndex(relationshipIndex.getName());
110 final Optional<ChampRelationshipIndex> retrieveRelationshipIndex = graph.retrieveRelationshipIndex(relationshipIndex.getName());
112 if (retrieveRelationshipIndex.isPresent()) {
113 throw new AssertionError("Retrieve relationship index after deleting it");
116 final Stream<ChampRelationshipIndex> relationshipIndices = graph.retrieveRelationshipIndices();
117 final Collection<ChampRelationshipIndex> allRelationshipIndices = relationshipIndices.collect(Collectors.toList());
119 if (allRelationshipIndices.contains(relationshipIndex)) {
120 throw new AssertionError("Retrieve all relationship indices contains previously deleted index");
122 if (allRelationshipIndices.size() != 0) {
123 throw new AssertionError("Wrong number of relationship indices returned by retrieve all indices");
125 } catch (ChampIndexNotExistsException e) {
126 throw new AssertionError(e);
130 graph.deleteRelationshipIndex(relationshipIndex.getName());
131 throw new AssertionError("Failed to throw exception on non-existent object index");
132 } catch (ChampIndexNotExistsException e) {
138 private void testChampRelationshipIndexStorage(ChampGraph graph, ChampRelationshipIndex relationshipIndex) {
140 graph.storeRelationshipIndex(relationshipIndex);
141 graph.storeRelationshipIndex(relationshipIndex); //Test storing duplicate relationship index
143 assertTrue(!graph.retrieveObjectIndex(relationshipIndex.getName()).isPresent()); //Make sure this wasn't stored as an object index
145 final Optional<ChampRelationshipIndex> retrieveRelationshipIndex = graph.retrieveRelationshipIndex(relationshipIndex.getName());
147 if (!retrieveRelationshipIndex.isPresent()) {
148 throw new AssertionError("Failed to retrieve relationship index after storing it");
150 if (!relationshipIndex.equals(retrieveRelationshipIndex.get())) {
151 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)) {
158 throw new AssertionError("Retrieve all relationship indices did not return previously stored relationship index");
160 if (allRelationshipIndices.size() != 1) {
161 throw new AssertionError("Wrong number of relationship indices returned by retrieve all indices");
164 assertTrue(!graph.retrieveRelationshipIndex("nonExistentIndexName").isPresent());
168 public void testFluentRelationshipIndexCreation() {
169 final ChampRelationshipIndex relationshipIndex = ChampRelationshipIndex.create()
170 .ofName("fooNameIndex")
175 assertTrue(relationshipIndex.getName().equals("fooNameIndex"));
176 assertTrue(relationshipIndex.getType().equals("foo"));
177 assertTrue(relationshipIndex.getField().getName().equals("name"));