2 * ============LICENSE_START==========================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 package org.onap.aai.champcore.core;
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;
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;
48 import static org.junit.Assert.assertTrue;
50 public class ChampObjectTest extends BaseChampAPITest {
53 public void testHashCode() {
54 final ChampObject foo1 = ChampObject.create()
57 .withProperty("property", "value")
58 .withProperty("prop", 1)
61 final ChampObject foo2 = ChampObject.create()
64 .withProperty("property", "value")
65 .withProperty("prop", 1)
68 final ChampObject foo1Copy = ChampObject.create()
73 final ChampObject foo2Copy = ChampObject.create()
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());
84 assertTrue(Collections.singleton(foo1).contains(foo1));
85 assertTrue(Collections.singleton(foo1).contains(foo1Copy));
89 public void runInMemoryTest() {
93 public void runTest(String apiType) {
94 final String graphName = ChampObjectTest.class.getSimpleName();
96 final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
97 ChampObjectTest.testChampObjectCrud(api.getGraph(graphName));
98 testChampObjectReservedProperties(api.getGraph(graphName));
102 public static void testChampObjectCrud(ChampGraph graph) {
104 // Create a dummy object...
105 final ChampObject bookooObject = ChampObject.create()
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"))
119 final ChampObject storedBookooObject;
123 // Create a schema for the graph.
124 graph.storeSchema(ChampSchema.create()
125 .withObjectConstraint()
127 .withPropertyConstraint()
129 .ofType(ChampField.Type.STRING)
130 .cardinality(ChampCardinality.LIST)
133 .withPropertyConstraint()
135 .ofType(ChampField.Type.STRING)
136 .cardinality(ChampCardinality.SET)
144 // Store the object in the graph.
145 storedBookooObject = graph.storeObject(bookooObject, Optional.empty());
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")));
158 final Optional<ChampObject> retrievedBookooObject = graph.retrieveObject(storedBookooObject.getKey().get(), Optional.empty());
160 final Stream<ChampObject> emptyStream = graph.queryObjects(new HashMap<String, Object>() {{
161 put(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_TYPE.toString(), "foo");
163 }}, Optional.empty());
165 assertTrue(emptyStream.limit(1).count() == 0);
167 final Stream<ChampObject> oneStream = graph.queryObjects(new HashMap<String, Object>() {{
168 put(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_TYPE.toString(), "foo");
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));
175 final List<ChampObject> queryByKey = graph.queryObjects(Collections.singletonMap(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_KEY.toString(), storedBookooObject.getKey().get()), Optional.empty())
177 .collect(Collectors.toList());
179 assertTrue(queryByKey.size() == 1);
180 assertTrue(queryByKey.get(0).equals(storedBookooObject));
182 if (!retrievedBookooObject.isPresent()) {
183 throw new AssertionError("Failed to retrieve stored object " + bookooObject);
185 if (!storedBookooObject.equals(retrievedBookooObject.get())) {
186 throw new AssertionError("Retrieved object does not equal stored object");
189 final ChampObject updatedBookoo = graph.storeObject(ChampObject.create()
190 .from(storedBookooObject)
191 .withKey(storedBookooObject.getKey().get())
192 .withProperty("long", 2L)
193 .build(), Optional.empty());
195 final Optional<ChampObject> retrievedUpdBookooObject = graph.retrieveObject(updatedBookoo.getKey().get(), Optional.empty());
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")));
207 if (!retrievedUpdBookooObject.isPresent()) {
208 throw new AssertionError("Failed to retrieve stored object " + bookooObject);
210 if (!updatedBookoo.equals(retrievedUpdBookooObject.get())) {
211 throw new AssertionError("Retrieved object does not equal stored object");
214 //validate the replaceObject method
215 final ChampObject replacedBookoo = graph.replaceObject(ChampObject.create()
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());
223 final Optional<ChampObject> retrievedReplacedBookooObject = graph.retrieveObject(replacedBookoo.getKey().get(), Optional.empty());
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")));
231 if (!retrievedReplacedBookooObject.isPresent()) {
232 throw new AssertionError("Failed to retrieve stored object " + replacedBookoo);
234 if (!replacedBookoo.equals(retrievedReplacedBookooObject.get())) {
235 throw new AssertionError("Retrieved object does not equal stored object");
238 graph.deleteObject(storedBookooObject.getKey().get(), Optional.empty());
240 if (graph.retrieveObject(storedBookooObject.getKey().get(), Optional.empty()).isPresent()) {
241 throw new AssertionError("Object not successfully deleted");
244 assertTrue(graph.queryObjects(Collections.emptyMap(), Optional.empty()).count() == 0);
245 assertTrue(graph.queryRelationships(Collections.emptyMap(), Optional.empty()).count() == 0);
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);
261 graph.deleteObject(storedBookooObject.getKey().get(), Optional.empty());
262 throw new AssertionError("Delete succeeded when it should have failed");
263 } catch (ChampObjectNotExistsException e) {
265 } catch (ChampTransactionException e) {
266 throw new AssertionError("Transaction exception occurred", e);
270 graph.storeObject(ChampObject.create()
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) {
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);
286 // validate the replaceObject method when Object key is not passed
288 ChampObject.create().ofType("foo").withoutKey().withProperty("property1", "value2").build(), Optional.empty());
289 } catch (ChampObjectNotExistsException e) {
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);
300 public void testChampObjectReservedProperties(ChampGraph graph) {
302 for (ChampObject.ReservedPropertyKeys key : ChampObject.ReservedPropertyKeys.values()) {
305 .ofType(ChampObject.ReservedTypes.ANY.toString())
307 .withProperty(key.toString(), "")
309 throw new AssertionError("Allowed reserved property key to be used during object creation");
310 } catch (IllegalArgumentException e) {
317 public void testFluentObjectCreation() {
318 final Object value1 = new Object();
319 final String value2 = "value2";
320 final float value3 = 0.0f;
322 final ChampObject champObject1 = ChampObject.create()
325 .withProperty("key1", value1)
326 .withProperty("key2", value2)
327 .withProperty("key3", value3)
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));
340 final ChampObject champObject2 = ChampObject.create()
343 .withProperty("key1", value1)
344 .withProperty("key2", value2)
345 .withProperty("key3", value3)
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));