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 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.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.model.ChampObjectIndex;
37 public class ChampObjectIndexTest extends BaseChampAPITest {
39 public void runTestMemory() {
43 public void runTest(String apiType) {
44 final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
45 final String graphName = api.getClass().getSimpleName();
47 ChampObjectIndexTest.testChampObjectIndexCrud(api.getGraph(graphName));
52 public static void testChampObjectIndexCrud(ChampGraph graph) {
54 final ChampObjectIndex objectIndex = ChampObjectIndex.create()
55 .ofName("fooObjectIndex")
57 .forField("propertyName")
60 testChampObjectIndexStorage(graph, objectIndex);
61 testChampObjectIndexDelete(graph, objectIndex);
64 private static void testChampObjectIndexDelete(ChampGraph graph, ChampObjectIndex objectIndex) {
66 if (!graph.capabilities().canDeleteObjectIndices()) {
68 graph.deleteObjectIndex("someindex");
69 throw new AssertionError("Graph claims it does not support object index delete, but failed to throw UnsupportedOperationException");
70 } catch (UnsupportedOperationException e) {
71 } catch (ChampIndexNotExistsException e) {
72 throw new AssertionError("Graph claims it does not support object index delete, but failed to throw UnsupportedOperationException");
76 graph.deleteObjectIndex(objectIndex.getName());
78 final Optional<ChampObjectIndex> retrievedObjectIndex = graph.retrieveObjectIndex(objectIndex.getName());
80 if (retrievedObjectIndex.isPresent()) throw new AssertionError("Retrieved object index after deleting it");
82 final Stream<ChampObjectIndex> retrievedObjectIndices = graph.retrieveObjectIndices();
83 final Collection<ChampObjectIndex> allObjectIndices = retrievedObjectIndices.collect(Collectors.toList());
85 if (allObjectIndices.contains(objectIndex)) throw new AssertionError("Retrieve all indices contained index previously deleted");
86 if (allObjectIndices.size() != 0) throw new AssertionError("Wrong number of indices returned by retrieve all indices");
88 } catch (ChampIndexNotExistsException e) {
89 throw new AssertionError(e);
93 graph.deleteObjectIndex(objectIndex.getName());
94 throw new AssertionError("Failed to throw exception on non-existent object index");
95 } catch (ChampIndexNotExistsException e) {
101 private static void testChampObjectIndexStorage(ChampGraph graph, ChampObjectIndex objectIndex) {
103 graph.storeObjectIndex(objectIndex);
104 graph.storeObjectIndex(objectIndex); //Test storing an already existing object index
106 assertTrue(!graph.retrieveRelationshipIndex(objectIndex.getName()).isPresent()); //Make sure this wasn't stored as an object index
108 final Optional<ChampObjectIndex> retrieveObjectIndex = graph.retrieveObjectIndex(objectIndex.getName());
110 if (!retrieveObjectIndex.isPresent()) throw new AssertionError("Failed to retrieve object index after storing it");
111 if (!objectIndex.equals(retrieveObjectIndex.get())) throw new AssertionError("Non-equal object index returned from API after storing it");
113 final Stream<ChampObjectIndex> retrievedObjectIndices = graph.retrieveObjectIndices();
114 final Collection<ChampObjectIndex> allObjectIndices = retrievedObjectIndices.collect(Collectors.toList());
116 if (!allObjectIndices.contains(objectIndex)) throw new AssertionError("Retrieve all indices did not contained index previously stored");
117 if (allObjectIndices.size() != 1) throw new AssertionError("Wrong number of indices returned by retrieve all indices");
119 assertTrue(!graph.retrieveObjectIndex("nonExistentIndexName").isPresent());
123 public void testFluentRelationshipCreation() {
124 final ChampObjectIndex objectIndex = ChampObjectIndex.create()
125 .ofName("fooNameIndex")
130 assertTrue(objectIndex.getName().equals("fooNameIndex"));
131 assertTrue(objectIndex.getType().equals("foo"));
132 assertTrue(objectIndex.getField().getName().equals("name"));