Update license date and text
[aai/champ.git] / champ-lib / champ-core / src / test / java / org / onap / aai / champcore / core / ChampRelationshipIndexTest.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
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
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
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  */
21 package org.onap.aai.champcore.core;
22
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;
30
31 import java.util.Collection;
32 import java.util.Optional;
33 import java.util.stream.Collectors;
34 import java.util.stream.Stream;
35
36 import static org.junit.Assert.assertTrue;
37
38 public class ChampRelationshipIndexTest extends BaseChampAPITest {
39
40   @Test
41   public void runInMemoryTest() {
42     runTest("IN_MEMORY");
43   }
44
45   public void runTest(String apiType) {
46     final String graphName = ChampRelationshipIndexTest.class.getSimpleName();
47
48     final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
49     testChampRelationshipIndexCrud(api.getGraph(graphName));
50     api.shutdown();
51   }
52
53   public void testChampRelationshipIndexCrud(ChampGraph graph) {
54
55     final ChampField relationshipField = new ChampField.Builder("propertyName").build();
56     final ChampRelationshipIndex relationshipIndex = new ChampRelationshipIndex.Builder("fooEdgeIndex", "foo", relationshipField).build();
57
58     //Test on an empty graph
59     testChampRelationshipIndexStorage(graph, relationshipIndex);
60     testChampRelationshipIndexDelete(graph, relationshipIndex);
61
62     //Test with existing data in graph
63     try {
64       graph.storeRelationship(ChampRelationship.create()
65           .ofType("uses")
66           .withoutKey()
67           .withSource()
68           .ofType("foo")
69           .withoutKey()
70           .build()
71           .withTarget()
72           .ofType("bar")
73           .withoutKey()
74           .build()
75           .build()
76         , Optional.empty());
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);
91     }
92   }
93
94   private void testChampRelationshipIndexDelete(ChampGraph graph, ChampRelationshipIndex relationshipIndex) {
95
96     if (!graph.capabilities().canDeleteRelationshipIndices()) {
97       try {
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) {
101         //Expected
102       } catch (ChampIndexNotExistsException e) {
103         throw new AssertionError("Graph claims it doesn't support relationship index delete, but it failed to throw UnsupportedOperationException");
104       }
105     } else {
106       try {
107         graph.deleteRelationshipIndex(relationshipIndex.getName());
108
109         final Optional<ChampRelationshipIndex> retrieveRelationshipIndex = graph.retrieveRelationshipIndex(relationshipIndex.getName());
110
111         if (retrieveRelationshipIndex.isPresent()) {
112           throw new AssertionError("Retrieve relationship index after deleting it");
113         }
114
115         final Stream<ChampRelationshipIndex> relationshipIndices = graph.retrieveRelationshipIndices();
116         final Collection<ChampRelationshipIndex> allRelationshipIndices = relationshipIndices.collect(Collectors.toList());
117
118         if (allRelationshipIndices.contains(relationshipIndex)) {
119           throw new AssertionError("Retrieve all relationship indices contains previously deleted index");
120         }
121         if (allRelationshipIndices.size() != 0) {
122           throw new AssertionError("Wrong number of relationship indices returned by retrieve all indices");
123         }
124       } catch (ChampIndexNotExistsException e) {
125         throw new AssertionError(e);
126       }
127
128       try {
129         graph.deleteRelationshipIndex(relationshipIndex.getName());
130         throw new AssertionError("Failed to throw exception on non-existent object index");
131       } catch (ChampIndexNotExistsException e) {
132         //Expected
133       }
134     }
135   }
136
137   private void testChampRelationshipIndexStorage(ChampGraph graph, ChampRelationshipIndex relationshipIndex) {
138
139     graph.storeRelationshipIndex(relationshipIndex);
140     graph.storeRelationshipIndex(relationshipIndex); //Test storing duplicate relationship index
141
142     assertTrue(!graph.retrieveObjectIndex(relationshipIndex.getName()).isPresent()); //Make sure this wasn't stored as an object index
143
144     final Optional<ChampRelationshipIndex> retrieveRelationshipIndex = graph.retrieveRelationshipIndex(relationshipIndex.getName());
145
146     if (!retrieveRelationshipIndex.isPresent()) {
147       throw new AssertionError("Failed to retrieve relationship index after storing it");
148     }
149     if (!relationshipIndex.equals(retrieveRelationshipIndex.get())) {
150       throw new AssertionError("Non-equal relationship index returned from API after storing it");
151     }
152
153     final Stream<ChampRelationshipIndex> relationshipIndices = graph.retrieveRelationshipIndices();
154     final Collection<ChampRelationshipIndex> allRelationshipIndices = relationshipIndices.collect(Collectors.toList());
155
156     if (!allRelationshipIndices.contains(relationshipIndex)) {
157       throw new AssertionError("Retrieve all relationship indices did not return previously stored relationship index");
158     }
159     if (allRelationshipIndices.size() != 1) {
160       throw new AssertionError("Wrong number of relationship indices returned by retrieve all indices");
161     }
162
163     assertTrue(!graph.retrieveRelationshipIndex("nonExistentIndexName").isPresent());
164   }
165
166   @Test
167   public void testFluentRelationshipIndexCreation() {
168     final ChampRelationshipIndex relationshipIndex = ChampRelationshipIndex.create()
169         .ofName("fooNameIndex")
170         .onType("foo")
171         .forField("name")
172         .build();
173
174     assertTrue(relationshipIndex.getName().equals("fooNameIndex"));
175     assertTrue(relationshipIndex.getType().equals("foo"));
176     assertTrue(relationshipIndex.getField().getName().equals("name"));
177   }
178 }