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