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.model.ChampObjectIndex;
37 public class ChampObjectIndexTest extends BaseChampAPITest {
39 public void runTest() {
40 for (ChampGraph.Type apiType : ChampGraph.Type.values()) {
41 final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
42 final String graphName = api.getClass().getSimpleName();
54 ChampObjectIndexTest.testChampObjectIndexCrud(api.getGraph(graphName));
60 private static void testChampObjectIndexCrud(ChampGraph graph) {
62 final ChampObjectIndex objectIndex = ChampObjectIndex.create()
63 .ofName("fooObjectIndex")
65 .forField("propertyName")
68 testChampObjectIndexStorage(graph, objectIndex);
69 testChampObjectIndexDelete(graph, objectIndex);
72 private static void testChampObjectIndexDelete(ChampGraph graph, ChampObjectIndex objectIndex) {
74 if (!graph.capabilities().canDeleteObjectIndices()) {
76 graph.deleteObjectIndex("someindex");
77 throw new AssertionError("Graph claims it does not support object index delete, but failed to throw UnsupportedOperationException");
78 } catch (UnsupportedOperationException e) {
79 } catch (ChampIndexNotExistsException e) {
80 throw new AssertionError("Graph claims it does not support object index delete, but failed to throw UnsupportedOperationException");
84 graph.deleteObjectIndex(objectIndex.getName());
86 final Optional<ChampObjectIndex> retrievedObjectIndex = graph.retrieveObjectIndex(objectIndex.getName());
88 if (retrievedObjectIndex.isPresent()) throw new AssertionError("Retrieved object index after deleting it");
90 final Stream<ChampObjectIndex> retrievedObjectIndices = graph.retrieveObjectIndices();
91 final Collection<ChampObjectIndex> allObjectIndices = retrievedObjectIndices.collect(Collectors.toList());
93 if (allObjectIndices.contains(objectIndex)) throw new AssertionError("Retrieve all indices contained index previously deleted");
94 if (allObjectIndices.size() != 0) throw new AssertionError("Wrong number of indices returned by retrieve all indices");
96 } catch (ChampIndexNotExistsException e) {
97 throw new AssertionError(e);
101 graph.deleteObjectIndex(objectIndex.getName());
102 throw new AssertionError("Failed to throw exception on non-existent object index");
103 } catch (ChampIndexNotExistsException e) {
109 private static void testChampObjectIndexStorage(ChampGraph graph, ChampObjectIndex objectIndex) {
111 graph.storeObjectIndex(objectIndex);
112 graph.storeObjectIndex(objectIndex); //Test storing an already existing object index
114 assertTrue(!graph.retrieveRelationshipIndex(objectIndex.getName()).isPresent()); //Make sure this wasn't stored as an object index
116 final Optional<ChampObjectIndex> retrieveObjectIndex = graph.retrieveObjectIndex(objectIndex.getName());
118 if (!retrieveObjectIndex.isPresent()) throw new AssertionError("Failed to retrieve object index after storing it");
119 if (!objectIndex.equals(retrieveObjectIndex.get())) throw new AssertionError("Non-equal object index returned from API after storing it");
121 final Stream<ChampObjectIndex> retrievedObjectIndices = graph.retrieveObjectIndices();
122 final Collection<ChampObjectIndex> allObjectIndices = retrievedObjectIndices.collect(Collectors.toList());
124 if (!allObjectIndices.contains(objectIndex)) throw new AssertionError("Retrieve all indices did not contained index previously stored");
125 if (allObjectIndices.size() != 1) throw new AssertionError("Wrong number of indices returned by retrieve all indices");
127 assertTrue(!graph.retrieveObjectIndex("nonExistentIndexName").isPresent());
131 public void testFluentRelationshipCreation() {
132 final ChampObjectIndex objectIndex = ChampObjectIndex.create()
133 .ofName("fooNameIndex")
138 assertTrue(objectIndex.getName().equals("fooNameIndex"));
139 assertTrue(objectIndex.getType().equals("foo"));
140 assertTrue(objectIndex.getField().getName().equals("name"));