Merge "Fix equals and hashCode methods"
[aai/champ.git] / champ-lib / champ-core / src / test / java / org / onap / aai / champcore / core / ChampObjectIndexTest.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 static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotEquals;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.Collection;
28 import java.util.Optional;
29 import java.util.stream.Collectors;
30 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.ChampField;
36 import org.onap.aai.champcore.model.ChampField.Type;
37 import org.onap.aai.champcore.model.ChampObjectIndex;
38 import org.onap.aai.champcore.model.ChampObjectIndex.Builder;
39
40 public class ChampObjectIndexTest extends BaseChampAPITest {
41         @Test
42   public void runTestMemory() {
43     runTest("IN_MEMORY");
44   }
45
46         public void runTest(String apiType) {
47                         final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
48                         final String graphName = api.getClass().getSimpleName();
49
50                         ChampObjectIndexTest.testChampObjectIndexCrud(api.getGraph(graphName));
51
52                         api.shutdown();
53         }
54
55         public static void testChampObjectIndexCrud(ChampGraph graph) {
56                 
57                 final ChampObjectIndex objectIndex = ChampObjectIndex.create()
58                                                                                                                                 .ofName("fooObjectIndex")
59                                                                                                                                 .onType("foo")
60                                                                                                                                 .forField("propertyName")
61                                                                                                                                 .build();
62
63                 testChampObjectIndexStorage(graph, objectIndex);
64                 testChampObjectIndexDelete(graph, objectIndex);
65         }
66
67         private static void testChampObjectIndexDelete(ChampGraph graph, ChampObjectIndex objectIndex) {
68
69                 if (!graph.capabilities().canDeleteObjectIndices()) {
70                         try {
71                                 graph.deleteObjectIndex("someindex");
72                                 throw new AssertionError("Graph claims it does not support object index delete, but failed to throw UnsupportedOperationException");
73                         } catch (UnsupportedOperationException e) {
74                         } catch (ChampIndexNotExistsException e) {
75                                 throw new AssertionError("Graph claims it does not support object index delete, but failed to throw UnsupportedOperationException");
76                         }
77                 } else {
78                         try {
79                                 graph.deleteObjectIndex(objectIndex.getName());
80                                 
81                                 final Optional<ChampObjectIndex> retrievedObjectIndex = graph.retrieveObjectIndex(objectIndex.getName());
82         
83                                 if (retrievedObjectIndex.isPresent()) throw new AssertionError("Retrieved object index after deleting it");
84         
85                                 final Stream<ChampObjectIndex> retrievedObjectIndices = graph.retrieveObjectIndices();
86                                 final Collection<ChampObjectIndex> allObjectIndices = retrievedObjectIndices.collect(Collectors.toList());
87         
88                                 if (allObjectIndices.contains(objectIndex)) throw new AssertionError("Retrieve all indices contained index previously deleted");
89                                 if (allObjectIndices.size() != 0) throw new AssertionError("Wrong number of indices returned by retrieve all indices");
90                         
91                         } catch (ChampIndexNotExistsException e) {
92                                 throw new AssertionError(e);
93                         }
94         
95                         try {
96                                 graph.deleteObjectIndex(objectIndex.getName());
97                                 throw new AssertionError("Failed to throw exception on non-existent object index");
98                         } catch (ChampIndexNotExistsException e) {
99                                 //Expected
100                         }
101                 }
102         }
103
104         private static void testChampObjectIndexStorage(ChampGraph graph, ChampObjectIndex objectIndex) {
105
106                 graph.storeObjectIndex(objectIndex);
107                 graph.storeObjectIndex(objectIndex); //Test storing an already existing object index
108
109                 assertTrue(!graph.retrieveRelationshipIndex(objectIndex.getName()).isPresent()); //Make sure this wasn't stored as an object index
110
111                 final Optional<ChampObjectIndex> retrieveObjectIndex = graph.retrieveObjectIndex(objectIndex.getName());
112                 
113                 if (!retrieveObjectIndex.isPresent()) throw new AssertionError("Failed to retrieve object index after storing it");
114                 if (!objectIndex.equals(retrieveObjectIndex.get())) throw new AssertionError("Non-equal object index returned from API after storing it");
115                 
116                 final Stream<ChampObjectIndex> retrievedObjectIndices = graph.retrieveObjectIndices();
117                 final Collection<ChampObjectIndex> allObjectIndices = retrievedObjectIndices.collect(Collectors.toList());
118
119                 if (!allObjectIndices.contains(objectIndex)) throw new AssertionError("Retrieve all indices did not contained index previously stored");
120                 if (allObjectIndices.size() != 1) throw new AssertionError("Wrong number of indices returned by retrieve all indices");
121
122                 assertTrue(!graph.retrieveObjectIndex("nonExistentIndexName").isPresent());
123         }
124
125         @Test
126         public void testFluentRelationshipCreation() {
127                 final ChampObjectIndex objectIndex = ChampObjectIndex.create()
128                                                                                                                                 .ofName("fooNameIndex")
129                                                                                                                                 .onType("foo")
130                                                                                                                                 .forField("name")
131                                                                                                                                 .build();
132
133                 assertTrue(objectIndex.getName().equals("fooNameIndex"));
134                 assertTrue(objectIndex.getType().equals("foo"));
135                 assertTrue(objectIndex.getField().getName().equals("name"));
136         }
137
138         @Test
139         public void verifyEqualsAndHashCodeMethods() {
140                 ChampField champField1 = new ChampField.Builder("name").type(Type.STRING).build();
141                 ChampField champField2 = new ChampField.Builder("differentName").type(Type.STRING).build();
142
143                 ChampObjectIndex obj1 = new Builder("name", "type", champField1).build();
144                 ChampObjectIndex obj2 = new Builder("name", "type", champField1).build();
145                 ChampObjectIndex obj3 = new Builder("name", "type", champField1).build();
146                 ChampObjectIndex obj4 = new Builder("name", "type", champField2).build();
147                 ChampObjectIndex obj5 = new Builder("differentName", "type", champField1).build();
148
149                 // if
150                 assertEquals(obj1, obj2);
151                 assertEquals(obj1.hashCode(), obj2.hashCode());
152                 //and
153                 assertEquals(obj1, obj3);
154                 assertEquals(obj1.hashCode(), obj3.hashCode());
155                 //then
156                 assertEquals(obj2, obj3);
157                 assertEquals(obj2.hashCode(), obj3.hashCode());
158
159                 assertNotEquals(obj1, obj4);
160                 assertNotEquals(obj1.hashCode(), obj4.hashCode());
161
162                 assertNotEquals(obj1, obj5);
163                 assertNotEquals(obj1.hashCode(), obj5.hashCode());
164         }
165 }