Merge "Added @Override annotation above signature"
[aai/champ.git] / src / test / java / org / openecomp / aai / champ / 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.openecomp.aai.champ.core;
23
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.Collection;
27 import java.util.Optional;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
30
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.exceptions.ChampMarshallingException;
36 import org.openecomp.aai.champ.exceptions.ChampObjectNotExistsException;
37 import org.openecomp.aai.champ.exceptions.ChampRelationshipNotExistsException;
38 import org.openecomp.aai.champ.exceptions.ChampSchemaViolationException;
39 import org.openecomp.aai.champ.exceptions.ChampUnmarshallingException;
40 import org.openecomp.aai.champ.model.ChampField;
41 import org.openecomp.aai.champ.model.ChampRelationship;
42 import org.openecomp.aai.champ.model.ChampRelationshipIndex;
43
44 public class ChampRelationshipIndexTest extends BaseChampAPITest {
45
46         @Test
47         public void runTest() {
48                 for (ChampGraph.Type apiType : ChampGraph.Type.values()) {
49                         final String graphName = ChampRelationshipIndexTest.class.getSimpleName();
50
51                         switch (apiType) {
52                         case IN_MEMORY:
53                         break;
54                         case TITAN:
55                                 cleanUp(graphName);
56                         break;
57                         default:
58                         break;
59                         }
60
61                         final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
62                         testChampRelationshipIndexCrud(api.getGraph(graphName));
63                         api.shutdown();
64                 }
65         }
66         
67         private void testChampRelationshipIndexCrud(ChampGraph graph) {
68         
69                 final ChampField relationshipField = new ChampField.Builder("propertyName").build();
70                 final ChampRelationshipIndex relationshipIndex = new ChampRelationshipIndex.Builder("fooEdgeIndex", "foo", relationshipField).build();
71
72                 //Test on an empty graph
73                 testChampRelationshipIndexStorage(graph, relationshipIndex);
74                 testChampRelationshipIndexDelete(graph, relationshipIndex);
75
76                 //Test with existing data in graph
77                 try {
78                         graph.storeRelationship(ChampRelationship.create()
79                                                                                                         .ofType("uses")
80                                                                                                         .withoutKey()
81                                                                                                         .withSource()
82                                                                                                                 .ofType("foo")
83                                                                                                                 .withoutKey()
84                                                                                                                 .build()
85                                                                                                         .withTarget()
86                                                                                                                 .ofType("bar")
87                                                                                                                 .withoutKey()
88                                                                                                                 .build()
89                                                                                                         .build());
90                         testChampRelationshipIndexStorage(graph, relationshipIndex);
91                         testChampRelationshipIndexDelete(graph, relationshipIndex);
92                 } catch (ChampMarshallingException e) {
93                         throw new AssertionError(e);
94                 } catch (ChampSchemaViolationException e) {
95                         throw new AssertionError(e);
96                 } catch (ChampObjectNotExistsException e) {
97                         throw new AssertionError(e);
98                 } catch (ChampRelationshipNotExistsException e) {
99                         throw new AssertionError(e);
100                 } catch (ChampUnmarshallingException e) {
101                         throw new AssertionError(e);
102                 }
103         }
104
105         private void testChampRelationshipIndexDelete(ChampGraph graph, ChampRelationshipIndex relationshipIndex) {
106
107                 if (!graph.capabilities().canDeleteRelationshipIndices()) {
108                         try {
109                                 graph.deleteRelationshipIndex("someindex");
110                                 throw new AssertionError("Graph claims it doesn't support relationship index delete, but it failed to throw UnsupportedOperationException");
111                         } catch (UnsupportedOperationException e) {
112                                 //Expected
113                         } catch (ChampIndexNotExistsException e) {
114                                 throw new AssertionError("Graph claims it doesn't support relationship index delete, but it failed to throw UnsupportedOperationException");
115                         }
116                 } else {
117                         try {
118                                 graph.deleteRelationshipIndex(relationshipIndex.getName());
119         
120                                 final Optional<ChampRelationshipIndex> retrieveRelationshipIndex = graph.retrieveRelationshipIndex(relationshipIndex.getName());
121                                 
122                                 if (retrieveRelationshipIndex.isPresent()) throw new AssertionError("Retrieve relationship index after deleting it");
123         
124                                 final Stream<ChampRelationshipIndex> relationshipIndices = graph.retrieveRelationshipIndices();
125                                 final Collection<ChampRelationshipIndex> allRelationshipIndices = relationshipIndices.collect(Collectors.toList());
126         
127                                 if (allRelationshipIndices.contains(relationshipIndex)) throw new AssertionError("Retrieve all relationship indices contains previously deleted index");
128                                 if (allRelationshipIndices.size() != 0) throw new AssertionError("Wrong number of relationship indices returned by retrieve all indices");
129                         } catch (ChampIndexNotExistsException e) {
130                                 throw new AssertionError(e);
131                         }
132         
133                         try {
134                                 graph.deleteRelationshipIndex(relationshipIndex.getName());
135                                 throw new AssertionError("Failed to throw exception on non-existent object index");
136                         } catch (ChampIndexNotExistsException e) {
137                                 //Expected
138                         }
139                 }
140         }
141
142         private void testChampRelationshipIndexStorage(ChampGraph graph, ChampRelationshipIndex relationshipIndex) {
143
144                 graph.storeRelationshipIndex(relationshipIndex);
145                 graph.storeRelationshipIndex(relationshipIndex); //Test storing duplicate relationship index
146
147                 assertTrue(!graph.retrieveObjectIndex(relationshipIndex.getName()).isPresent()); //Make sure this wasn't stored as an object index
148
149                 final Optional<ChampRelationshipIndex> retrieveRelationshipIndex = graph.retrieveRelationshipIndex(relationshipIndex.getName());
150                 
151                 if (!retrieveRelationshipIndex.isPresent()) throw new AssertionError("Failed to retrieve relationship index after storing it");
152                 if (!relationshipIndex.equals(retrieveRelationshipIndex.get())) throw new AssertionError("Non-equal relationship index returned from API after storing it");
153
154                 final Stream<ChampRelationshipIndex> relationshipIndices = graph.retrieveRelationshipIndices();
155                 final Collection<ChampRelationshipIndex> allRelationshipIndices = relationshipIndices.collect(Collectors.toList());
156
157                 if (!allRelationshipIndices.contains(relationshipIndex)) throw new AssertionError("Retrieve all relationship indices did not return previously stored relationship index");
158                 if (allRelationshipIndices.size() != 1) throw new AssertionError("Wrong number of relationship indices returned by retrieve all indices");
159
160                 assertTrue(!graph.retrieveRelationshipIndex("nonExistentIndexName").isPresent());
161         }
162
163         @Test
164         public void testFluentRelationshipIndexCreation() {
165                 final ChampRelationshipIndex relationshipIndex = ChampRelationshipIndex.create()
166                                                                                                                                 .ofName("fooNameIndex")
167                                                                                                                                 .onType("foo")
168                                                                                                                                 .forField("name")
169                                                                                                                                 .build();
170
171                 assertTrue(relationshipIndex.getName().equals("fooNameIndex"));
172                 assertTrue(relationshipIndex.getType().equals("foo"));
173                 assertTrue(relationshipIndex.getField().getName().equals("name"));
174         }
175 }