3ef8f8c7d808742595ac69527d50e7d5aab30a10
[aai/champ.git] / champ-lib / champ-core / src / test / java / org / onap / aai / champcore / core / ChampObjectTest.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.ChampCapabilities;
27 import org.onap.aai.champcore.ChampGraph;
28 import org.onap.aai.champcore.ChampTransaction;
29 import org.onap.aai.champcore.core.ChampRelationshipTest.TestTransaction;
30 import org.onap.aai.champcore.exceptions.ChampMarshallingException;
31 import org.onap.aai.champcore.exceptions.ChampObjectNotExistsException;
32 import org.onap.aai.champcore.exceptions.ChampSchemaViolationException;
33 import org.onap.aai.champcore.exceptions.ChampTransactionException;
34 import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
35 import org.onap.aai.champcore.graph.impl.InMemoryChampGraphImpl;
36 import org.onap.aai.champcore.model.ChampCardinality;
37 import org.onap.aai.champcore.model.ChampField;
38 import org.onap.aai.champcore.model.ChampObject;
39 import org.onap.aai.champcore.model.ChampSchema;
40
41 import java.util.Collections;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Optional;
45 import java.util.stream.Collectors;
46 import java.util.stream.Stream;
47
48 import static org.junit.Assert.assertTrue;
49
50 public class ChampObjectTest extends BaseChampAPITest {
51
52   @Test
53   public void testHashCode() {
54     final ChampObject foo1 = ChampObject.create()
55         .ofType("foo")
56         .withoutKey()
57         .withProperty("property", "value")
58         .withProperty("prop", 1)
59         .build();
60
61     final ChampObject foo2 = ChampObject.create()
62         .ofType("foo")
63         .withoutKey()
64         .withProperty("property", "value")
65         .withProperty("prop", 1)
66         .build();
67
68     final ChampObject foo1Copy = ChampObject.create()
69         .from(foo1)
70         .withoutKey()
71         .build();
72
73     final ChampObject foo2Copy = ChampObject.create()
74         .from(foo2)
75         .withoutKey()
76         .build();
77
78     assertTrue(foo1.hashCode() == foo2.hashCode());
79     assertTrue(foo1.hashCode() == foo1.hashCode());
80     assertTrue(foo2.hashCode() == foo2.hashCode());
81     assertTrue(foo1.hashCode() == foo1Copy.hashCode());
82     assertTrue(foo2.hashCode() == foo2Copy.hashCode());
83
84     assertTrue(Collections.singleton(foo1).contains(foo1));
85     assertTrue(Collections.singleton(foo1).contains(foo1Copy));
86   }
87
88   @Test
89   public void runInMemoryTest() {
90     runTest("IN_MEMORY");
91   }
92
93   public void runTest(String apiType) {
94     final String graphName = ChampObjectTest.class.getSimpleName();
95
96     final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
97     ChampObjectTest.testChampObjectCrud(api.getGraph(graphName));
98     testChampObjectReservedProperties(api.getGraph(graphName));
99     api.shutdown();
100   }
101
102   public static void testChampObjectCrud(ChampGraph graph) {
103     
104     // Create a dummy object...
105     final ChampObject bookooObject = ChampObject.create()
106         .ofType("foo")
107         .withoutKey()
108         .withProperty("property1", "value1")
109         .withProperty("integer", 1)
110         .withProperty("long", 1L)
111         .withProperty("double", 1.2)
112         .withProperty("float", 2.3F)
113         .withProperty("string", "foo")
114         .withProperty("boolean", true)
115         .withProperty("list", Collections.singletonList("list"))
116         .withProperty("set", Collections.singleton("set"))
117         .build();
118
119     final ChampObject storedBookooObject;
120    
121     try {
122
123       // Create a schema for the graph.
124       graph.storeSchema(ChampSchema.create()
125           .withObjectConstraint()
126           .onType("foo")
127           .withPropertyConstraint()
128           .onField("list")
129           .ofType(ChampField.Type.STRING)
130           .cardinality(ChampCardinality.LIST)
131           .optional()
132           .build()
133           .withPropertyConstraint()
134           .onField("set")
135           .ofType(ChampField.Type.STRING)
136           .cardinality(ChampCardinality.SET)
137           .optional()
138           .build()
139           .build()
140           .build());
141
142
143       
144       // Store the object in the graph.
145       storedBookooObject = graph.storeObject(bookooObject, Optional.empty());
146
147       // Check that the object we got back from the store call matches what we sent.
148       assertTrue(storedBookooObject.getProperty("property1").get().equals("value1"));
149       assertTrue(storedBookooObject.getProperty("integer").get().equals(1));
150       assertTrue(storedBookooObject.getProperty("long").get().equals(1L));
151       assertTrue(storedBookooObject.getProperty("double").get().equals(1.2));
152       assertTrue(storedBookooObject.getProperty("float").get().equals(2.3F));
153       assertTrue(storedBookooObject.getProperty("string").get().equals("foo"));
154       assertTrue(storedBookooObject.getProperty("boolean").get().equals(true));
155       assertTrue(storedBookooObject.getProperty("list").get().equals(Collections.singletonList("list")));
156       assertTrue(storedBookooObject.getProperty("set").get().equals(Collections.singleton("set")));
157
158       final Optional<ChampObject> retrievedBookooObject = graph.retrieveObject(storedBookooObject.getKey().get(), Optional.empty());
159         
160       final Stream<ChampObject> emptyStream = graph.queryObjects(new HashMap<String, Object>() {{
161         put(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_TYPE.toString(), "foo");
162         put("long", 2L);
163       }}, Optional.empty());
164
165       assertTrue(emptyStream.limit(1).count() == 0);
166
167       final Stream<ChampObject> oneStream = graph.queryObjects(new HashMap<String, Object>() {{
168         put(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_TYPE.toString(), "foo");
169         put("long", 1L);
170       }}, Optional.empty());
171       final List<ChampObject> oneObject = oneStream.limit(2).collect(Collectors.toList());
172       assertTrue(oneObject.size() == 1);
173       assertTrue(oneObject.get(0).equals(storedBookooObject));
174
175       final List<ChampObject> queryByKey = graph.queryObjects(Collections.singletonMap(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_KEY.toString(), storedBookooObject.getKey().get()), Optional.empty())
176           .limit(2)
177           .collect(Collectors.toList());
178
179       assertTrue(queryByKey.size() == 1);
180       assertTrue(queryByKey.get(0).equals(storedBookooObject));
181
182       if (!retrievedBookooObject.isPresent()) {
183         throw new AssertionError("Failed to retrieve stored object " + bookooObject);
184       }
185       if (!storedBookooObject.equals(retrievedBookooObject.get())) {
186         throw new AssertionError("Retrieved object does not equal stored object");
187       }
188       
189       final ChampObject updatedBookoo = graph.storeObject(ChampObject.create()
190           .from(storedBookooObject)
191           .withKey(storedBookooObject.getKey().get())
192           .withProperty("long", 2L)
193           .build(), Optional.empty());
194
195       final Optional<ChampObject> retrievedUpdBookooObject = graph.retrieveObject(updatedBookoo.getKey().get(), Optional.empty());
196
197       assertTrue(updatedBookoo.getProperty("property1").get().equals("value1"));
198       assertTrue(updatedBookoo.getProperty("integer").get().equals(1));
199       assertTrue(updatedBookoo.getProperty("long").get().equals(2L));
200       assertTrue(updatedBookoo.getProperty("double").get().equals(1.2));
201       assertTrue(updatedBookoo.getProperty("float").get().equals(2.3F));
202       assertTrue(updatedBookoo.getProperty("string").get().equals("foo"));
203       assertTrue(updatedBookoo.getProperty("boolean").get().equals(true));
204       assertTrue(updatedBookoo.getProperty("list").get().equals(Collections.singletonList("list")));
205       assertTrue(updatedBookoo.getProperty("set").get().equals(Collections.singleton("set")));
206
207       if (!retrievedUpdBookooObject.isPresent()) {
208         throw new AssertionError("Failed to retrieve stored object " + bookooObject);
209       }
210       if (!updatedBookoo.equals(retrievedUpdBookooObject.get())) {
211         throw new AssertionError("Retrieved object does not equal stored object");
212       }
213       
214       //validate the replaceObject method
215       final ChampObject replacedBookoo = graph.replaceObject(ChampObject.create()
216           .ofType("foo")
217           .withKey(storedBookooObject.getKey().get())
218           .withProperty("property1", "value2")
219           .withProperty("list", Collections.singletonList("list"))
220           .withProperty("set", Collections.singleton("set"))
221           .build(), Optional.empty());
222
223       final Optional<ChampObject> retrievedReplacedBookooObject = graph.retrieveObject(replacedBookoo.getKey().get(), Optional.empty());
224
225       assertTrue(replacedBookoo.getProperties().size() == 3);
226       assertTrue(replacedBookoo.getProperty("property1").get().equals("value2"));
227       assertTrue(replacedBookoo.getProperty("list").get().equals(Collections.singletonList("list")));
228       assertTrue(replacedBookoo.getProperty("set").get().equals(Collections.singleton("set")));
229
230
231       if (!retrievedReplacedBookooObject.isPresent()) {
232         throw new AssertionError("Failed to retrieve stored object " + replacedBookoo);
233       }
234       if (!replacedBookoo.equals(retrievedReplacedBookooObject.get())) {
235         throw new AssertionError("Retrieved object does not equal stored object");
236       }
237       
238       graph.deleteObject(storedBookooObject.getKey().get(), Optional.empty());
239       
240       if (graph.retrieveObject(storedBookooObject.getKey().get(), Optional.empty()).isPresent()) {
241         throw new AssertionError("Object not successfully deleted");
242       }
243
244       assertTrue(graph.queryObjects(Collections.emptyMap(), Optional.empty()).count() == 0);
245       assertTrue(graph.queryRelationships(Collections.emptyMap(), Optional.empty()).count() == 0);
246       
247       
248     } catch (ChampSchemaViolationException e) {
249       throw new AssertionError("Schema mismatch while storing object", e);
250     } catch (ChampMarshallingException e) {
251       throw new AssertionError("Marshalling exception while storing object", e);
252     } catch (ChampUnmarshallingException e) {
253       throw new AssertionError("Unmarshalling exception while retrieving object", e);
254     } catch (ChampObjectNotExistsException e) {
255       throw new AssertionError("Missing object on delete/update", e);
256     } catch (ChampTransactionException e) {
257       throw new AssertionError("Transaction exception occurred", e);
258     }
259     
260     try {
261       graph.deleteObject(storedBookooObject.getKey().get(), Optional.empty());
262       throw new AssertionError("Delete succeeded when it should have failed");
263     } catch (ChampObjectNotExistsException e) {
264       //Expected
265     } catch (ChampTransactionException e) {
266       throw new AssertionError("Transaction exception occurred", e);
267     } 
268     
269     try {
270       graph.storeObject(ChampObject.create()
271           .ofType("foo")
272           .withKey("non-existent object key")
273           .build(), Optional.empty());
274       throw new AssertionError("Expected ChampObjectNotExistsException but object was successfully stored");
275     } catch (ChampObjectNotExistsException e) {
276       //Expected
277     } catch (ChampMarshallingException e) {
278       throw new AssertionError(e);
279     } catch (ChampSchemaViolationException e) {
280       throw new AssertionError(e);
281     } catch (ChampTransactionException e) {
282       throw new AssertionError(e);
283     }
284
285     try {
286       // validate the replaceObject method when Object key is not passed
287       graph.replaceObject(
288           ChampObject.create().ofType("foo").withoutKey().withProperty("property1", "value2").build(), Optional.empty());
289     } catch (ChampObjectNotExistsException e) {
290       // Expected
291     } catch (ChampMarshallingException e) {
292       throw new AssertionError(e);
293     } catch (ChampSchemaViolationException e) {
294       throw new AssertionError(e);
295     } catch (ChampTransactionException e) {
296       throw new AssertionError(e);
297     }
298   }
299
300   public void testChampObjectReservedProperties(ChampGraph graph) {
301
302     for (ChampObject.ReservedPropertyKeys key : ChampObject.ReservedPropertyKeys.values()) {
303       try {
304         ChampObject.create()
305             .ofType(ChampObject.ReservedTypes.ANY.toString())
306             .withoutKey()
307             .withProperty(key.toString(), "")
308             .build();
309         throw new AssertionError("Allowed reserved property key to be used during object creation");
310       } catch (IllegalArgumentException e) {
311         //Expected
312       }
313     }
314   }
315
316   @Test
317   public void testFluentObjectCreation() {
318     final Object value1 = new Object();
319     final String value2 = "value2";
320     final float value3 = 0.0f;
321
322     final ChampObject champObject1 = ChampObject.create()
323         .ofType("foo")
324         .withoutKey()
325         .withProperty("key1", value1)
326         .withProperty("key2", value2)
327         .withProperty("key3", value3)
328         .build();
329
330     assertTrue(champObject1.getKey().equals(Optional.empty()));
331     assertTrue(champObject1.getKey().isPresent() == false);
332     assertTrue(champObject1.getType().equals("foo"));
333     assertTrue(champObject1.getProperty("key1").get() instanceof Object);
334     assertTrue(champObject1.getProperty("key1").get().equals(value1));
335     assertTrue(champObject1.getProperty("key2").get() instanceof String);
336     assertTrue(champObject1.getProperty("key2").get().equals(value2));
337     assertTrue(champObject1.getProperty("key3").get() instanceof Float);
338     assertTrue(champObject1.getProperty("key3").get().equals(value3));
339
340     final ChampObject champObject2 = ChampObject.create()
341         .ofType("foo")
342         .withKey(1)
343         .withProperty("key1", value1)
344         .withProperty("key2", value2)
345         .withProperty("key3", value3)
346         .build();
347
348     assertTrue(champObject2.getType().equals("foo"));
349     assertTrue(champObject2.getKey().isPresent() == true);
350     assertTrue(champObject2.getKey().get() instanceof Integer);
351     assertTrue(champObject2.getKey().get().equals(1));
352     assertTrue(champObject2.getProperty("key1").get() instanceof Object);
353     assertTrue(champObject2.getProperty("key1").get().equals(value1));
354     assertTrue(champObject2.getProperty("key2").get() instanceof String);
355     assertTrue(champObject2.getProperty("key2").get().equals(value2));
356     assertTrue(champObject2.getProperty("key3").get() instanceof Float);
357     assertTrue(champObject2.getProperty("key3").get().equals(value3));
358   }
359 }