Fix equals and hashCode methods
[aai/champ.git] / champ-lib / champ-core / src / test / java / org / onap / aai / champcore / core / ChampRelationshipTest.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.ChampTransaction;
27 import org.onap.aai.champcore.exceptions.*;
28 import org.onap.aai.champcore.model.ChampObject;
29 import org.onap.aai.champcore.model.ChampRelationship;
30 import org.onap.aai.champcore.model.ChampRelationship.Builder;
31 import org.onap.aai.champcore.model.ChampRelationship.ReservedPropertyKeys;
32 import org.onap.aai.champcore.model.ChampRelationship.ReservedTypes;
33
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.Optional;
38 import java.util.stream.Collectors;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNotEquals;
42 import static org.junit.Assert.assertTrue;
43
44 public class ChampRelationshipTest extends BaseChampAPITest {
45
46   @Test
47   public void runInMemoryTest() {
48     runTest("IN_MEMORY");
49   }
50
51   public void runTest(String apiType) {
52     final String graphName = ChampRelationshipTest.class.getSimpleName();
53
54     final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
55     ChampRelationshipTest.testChampRelationshipCrud(api.getGraph(graphName));
56     api.shutdown();
57   }
58
59   public static void testChampRelationshipCrud(ChampGraph graph) {
60     final ChampObject source = ChampObject.create()
61         .ofType("foo")
62         .withoutKey()
63         .withProperty("property1", "value1")
64         .build();
65
66     final ChampObject target = ChampObject.create()
67         .ofType("foo")
68         .withoutKey()
69         .build();
70
71     try {
72       ChampTransaction tx = ChampRelationshipTest.getTransaction();
73       
74       final ChampObject storedSource = graph.storeObject(source, Optional.of(tx));
75       final ChampObject storedTarget = graph.storeObject(target, Optional.of(tx));
76
77       final ChampRelationship relationship = new ChampRelationship.Builder(storedSource, storedTarget, "relationship")
78           .property("property-1", "value-1")
79           .property("property-2", 3)
80           .build();
81
82       final ChampRelationship storedRelationship = graph.storeRelationship(relationship, Optional.of(tx));
83       final Optional<ChampRelationship> retrievedRelationship = graph.retrieveRelationship(storedRelationship.getKey().get(), Optional.of(tx));
84
85       if (!retrievedRelationship.isPresent()) {
86         throw new AssertionError("Failed to retrieve stored relationship " + storedRelationship);
87       }
88       if (!storedRelationship.equals(retrievedRelationship.get())) {
89         throw new AssertionError("Retrieved relationship does not equal stored object");
90       }
91
92       assertTrue(retrievedRelationship.get().getProperty("property-1").get().equals("value-1"));
93       assertTrue(retrievedRelationship.get().getProperty("property-2").get().equals(3));
94
95       if (!graph.retrieveRelationships(storedRelationship.getSource(), Optional.of(tx)).collect(Collectors.toList()).contains(storedRelationship)) {
96         throw new AssertionError("Failed to retrieve relationships for source object");
97       }
98
99       final ChampRelationship updatedRelationship = ChampRelationship.create()
100           .from(retrievedRelationship.get())
101           .withKey(retrievedRelationship.get().getKey().get())
102           .withProperty("property-2", 4)
103           .build();
104
105       final ChampRelationship storedUpdRel = graph.storeRelationship(updatedRelationship, Optional.of(tx));
106       final Optional<ChampRelationship> retrievedUpdRel = graph.retrieveRelationship(storedUpdRel.getKey().get(), Optional.of(tx));
107
108       assertTrue(retrievedUpdRel.isPresent());
109       assertTrue(retrievedUpdRel.get().equals(storedUpdRel));
110       assertTrue(retrievedUpdRel.get().getProperty("property-1").get().equals("value-1"));
111       assertTrue(retrievedUpdRel.get().getProperty("property-2").get().equals(4));
112
113
114       // validate the replaceRelationship method
115       final ChampRelationship replacedRelationship = new ChampRelationship.Builder(storedSource, storedTarget, "relationship")
116           .key(retrievedRelationship.get().getKey().get())
117           .property("property-2", 4)
118           .build();
119
120       final ChampRelationship replacedRel = graph.replaceRelationship(replacedRelationship, Optional.of(tx));
121       final Optional<ChampRelationship> retrievedReplacedRel = graph
122           .retrieveRelationship(replacedRel.getKey().get(), Optional.of(tx));
123
124       assertTrue(replacedRel.getProperties().size() == 1);
125       assertTrue(replacedRel.getProperty("property-2").get().equals(4));
126
127       assertTrue(retrievedReplacedRel.get().getProperties().size() == 1);
128       assertTrue(retrievedReplacedRel.get().getProperty("property-2").get().equals(4));
129
130       if (!retrievedReplacedRel.isPresent()) {
131         throw new AssertionError("Failed to retrieve stored relationship " + replacedRel);
132       }
133       if (!replacedRel.equals(retrievedReplacedRel.get())) {
134         throw new AssertionError("Retrieved relationship does not equal stored object");
135       }
136
137
138       graph.deleteRelationship(retrievedRelationship.get(), Optional.of(tx));
139
140       if (graph.retrieveRelationship(relationship.getKey(), Optional.of(tx)).isPresent()) {
141         throw new AssertionError("Relationship not successfully deleted");
142       }
143
144       try {
145         graph.deleteRelationship(retrievedRelationship.get(), Optional.of(tx));
146         throw new AssertionError("Failed to throw exception for missing relationship");
147       } catch (ChampRelationshipNotExistsException e) {
148         //Expected
149       }
150       
151       Map<String, Object> queryParams = new HashMap<>();
152       queryParams.put(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_TYPE.toString(), "blah");
153       graph.queryRelationships(queryParams);
154       
155       queryParams = new HashMap<>();
156       queryParams.put(ChampRelationship.ReservedPropertyKeys.CHAMP_RELATIONSHIP_TYPE.toString(), "blah");
157       graph.queryRelationships(queryParams);
158
159       assertTrue(graph.queryRelationships(Collections.emptyMap(), Optional.of(tx)).count() == 0);
160       assertTrue(graph.queryObjects(Collections.emptyMap(), Optional.of(tx)).count() == 2);
161     } catch (ChampSchemaViolationException e) {
162       throw new AssertionError("Schema mismatch while storing object", e);
163     } catch (ChampMarshallingException e) {
164       throw new AssertionError("Marshalling exception while storing object", e);
165     } catch (ChampUnmarshallingException e) {
166       throw new AssertionError("Unmarshalling exception while retrieving relationship", e);
167     } catch (ChampRelationshipNotExistsException e) {
168       throw new AssertionError("Attempted to delete non-existent relationship", e);
169     } catch (ChampObjectNotExistsException e) {
170       throw new AssertionError("Object does not exist after storing it", e);
171     } catch (ChampTransactionException e) {
172       throw new AssertionError("Transaction failure", e);
173     }
174
175     ChampTransaction tx = ChampRelationshipTest.getTransaction();
176     try {
177       graph.retrieveRelationships(ChampObject.create().ofType("").withoutKey().build(), Optional.of(tx));
178       throw new AssertionError("Failed to handle missing object while retrieving relationships");
179     } catch (ChampUnmarshallingException | ChampTransactionException e) {
180       throw new AssertionError(e);
181     } catch (ChampObjectNotExistsException e) {
182       //Expected
183     }
184     //Negative test cases for replace relationship
185
186     try {
187       graph.replaceRelationship(new ChampRelationship.Builder(ChampObject.create()
188           .ofType("foo")
189           .withoutKey()
190           .build(), ChampObject.create()
191           .ofType("foo")
192           .withoutKey()
193           .build(), "relationship")
194           .key("1234")
195           .property("property-2", 4)
196           .build(),
197           Optional.of(tx));
198     } catch (ChampUnmarshallingException e) {
199       throw new AssertionError(e);
200     } catch (ChampMarshallingException e) {
201       throw new AssertionError(e);
202     } catch (ChampSchemaViolationException e) {
203       throw new AssertionError(e);
204     } catch (ChampRelationshipNotExistsException e) {
205       throw new AssertionError(e);
206     } catch (IllegalArgumentException e) {
207       //expected
208     } catch (ChampTransactionException e) {
209       throw new AssertionError(e);
210     }
211
212     try {
213       graph.replaceRelationship(new ChampRelationship.Builder(ChampObject.create()
214           .ofType("foo")
215           .withKey("123")
216           .build(), ChampObject.create()
217           .ofType("foo")
218           .withKey("456")
219           .build(), "relationship")
220           .property("property-2", 4)
221           .build(), 
222           Optional.of(tx));
223     } catch (ChampUnmarshallingException e) {
224       throw new AssertionError(e);
225     } catch (ChampMarshallingException e) {
226       throw new AssertionError(e);
227     } catch (ChampSchemaViolationException e) {
228       throw new AssertionError(e);
229     } catch (ChampRelationshipNotExistsException e) {
230       //expected
231     } catch (IllegalArgumentException e) {
232       throw new AssertionError(e);
233     } catch (ChampTransactionException e) {
234       throw new AssertionError(e);
235     }
236
237
238   }
239
240   @Test
241   public void testFluentRelationshipCreation() {
242     final Object value1 = new Object();
243     final String value2 = "value2";
244     final float value3 = 0.0f;
245
246     final ChampRelationship champRelationship = ChampRelationship.create()
247         .ofType("foo")
248         .withoutKey()
249         .withSource()
250         .ofType("bar")
251         .withoutKey()
252         .build()
253         .withTarget()
254         .ofType("baz")
255         .withKey(1)
256         .build()
257         .withProperty("key1", value1)
258         .withProperty("key2", value2)
259         .withProperty("key3", value3)
260         .build();
261
262     ChampTransaction tx = ChampRelationshipTest.getTransaction();
263     assertTrue(champRelationship.getKey().equals(Optional.empty()));
264     assertTrue(champRelationship.getType().equals("foo"));
265     assertTrue(champRelationship.getProperty("key1").get() instanceof Object);
266     assertTrue(champRelationship.getProperty("key1").get().equals(value1));
267     assertTrue(champRelationship.getProperty("key2").get() instanceof String);
268     assertTrue(champRelationship.getProperty("key2").get().equals(value2));
269     assertTrue(champRelationship.getProperty("key3").get() instanceof Float);
270     assertTrue(champRelationship.getProperty("key3").get().equals(value3));
271   }
272
273   @Test
274   public void testChampRelationshipEnums() {
275     for (ReservedPropertyKeys key : ChampRelationship.ReservedPropertyKeys.values()) {
276       assertTrue(ChampRelationship.ReservedPropertyKeys.valueOf(key.name()) == key);
277     }
278
279     for (ReservedTypes type : ChampRelationship.ReservedTypes.values()) {
280       assertTrue(ChampRelationship.ReservedTypes.valueOf(type.name()) == type);
281     }
282   }
283
284   @Test
285   public void verifyEqualsAndHashCodeMethods() {
286     ChampObject source = new ChampObject.Builder("type").build();
287     ChampObject target = new ChampObject.Builder("type").build();
288     ChampRelationship champRelationship = new Builder(source, target, "type").key("someKey").build();
289     ChampRelationship obj1 = new Builder(champRelationship).key("someKey").build();
290     ChampRelationship obj2 = new Builder(champRelationship).key("someKey").build();
291     ChampRelationship obj3 = new Builder(champRelationship).key("someKey").build();
292     ChampRelationship obj4 = new Builder(champRelationship).key("differentKey").build();
293
294     // if
295     assertEquals(obj1, obj2);
296     assertEquals(obj1.hashCode(), obj2.hashCode());
297     //and
298     assertEquals(obj1, obj3);
299     assertEquals(obj1.hashCode(), obj3.hashCode());
300     //then
301     assertEquals(obj2, obj3);
302     assertEquals(obj2.hashCode(), obj3.hashCode());
303
304     assertNotEquals(obj1, obj4);
305     assertNotEquals(obj1.hashCode(), obj4.hashCode());
306   }
307   
308   public static ChampTransaction getTransaction() {
309     return new TestTransaction();
310   }
311   
312   public static class TestTransaction extends ChampTransaction {
313
314     public TestTransaction() {
315       
316     }
317     @Override
318     public void commit() {
319       // TODO Auto-generated method stub
320       
321     }
322
323     @Override
324     public void rollback() {
325       // TODO Auto-generated method stub
326       
327     }
328     @Override
329     public String id() {
330       // TODO Auto-generated method stub
331       return null;
332     }
333     
334   }
335
336 }