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.openecomp.aai.champ.core;
24 import static org.junit.Assert.assertTrue;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Optional;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
33 import org.junit.Test;
34 import org.openecomp.aai.champ.ChampAPI;
35 import org.openecomp.aai.champ.ChampGraph;
36 import org.openecomp.aai.champ.exceptions.ChampMarshallingException;
37 import org.openecomp.aai.champ.exceptions.ChampObjectNotExistsException;
38 import org.openecomp.aai.champ.exceptions.ChampSchemaViolationException;
39 import org.openecomp.aai.champ.exceptions.ChampUnmarshallingException;
40 import org.openecomp.aai.champ.model.ChampCardinality;
41 import org.openecomp.aai.champ.model.ChampField;
42 import org.openecomp.aai.champ.model.ChampObject;
43 import org.openecomp.aai.champ.model.ChampSchema;
45 public class ChampObjectTest extends BaseChampAPITest {
48 public void testHashCode() {
49 final ChampObject foo1 = ChampObject.create()
52 .withProperty("property", "value")
53 .withProperty("prop", 1)
56 final ChampObject foo2 = ChampObject.create()
59 .withProperty("property", "value")
60 .withProperty("prop", 1)
63 final ChampObject foo1Copy = ChampObject.create()
68 final ChampObject foo2Copy = ChampObject.create()
73 assertTrue(foo1.hashCode() == foo2.hashCode());
74 assertTrue(foo1.hashCode() == foo1.hashCode());
75 assertTrue(foo2.hashCode() == foo2.hashCode());
76 assertTrue(foo1.hashCode() == foo1Copy.hashCode());
77 assertTrue(foo2.hashCode() == foo2Copy.hashCode());
79 assertTrue(Collections.singleton(foo1).contains(foo1));
80 assertTrue(Collections.singleton(foo1).contains(foo1Copy));
84 public void runTest() {
85 for (ChampGraph.Type apiType : ChampGraph.Type.values()) {
86 final String graphName = ChampObjectTest.class.getSimpleName();
95 final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
96 ChampObjectTest.testChampObjectCrud(api.getGraph(graphName));
97 testChampObjectReservedProperties(api.getGraph(graphName));
102 public static void testChampObjectCrud(ChampGraph graph) {
103 final ChampObject bookooObject = ChampObject.create()
106 .withProperty("property1", "value1")
107 .withProperty("integer", 1)
108 .withProperty("long", 1L)
109 .withProperty("double", 1.2)
110 .withProperty("float", 2.3F)
111 .withProperty("string", "foo")
112 .withProperty("boolean", true)
113 .withProperty("list", Collections.singletonList("list"))
114 .withProperty("set", Collections.singleton("set"))
117 final ChampObject storedBookooObject;
121 graph.storeSchema(ChampSchema.create()
122 .withObjectConstraint()
124 .withPropertyConstraint()
126 .ofType(ChampField.Type.STRING)
127 .cardinality(ChampCardinality.LIST)
130 .withPropertyConstraint()
132 .ofType(ChampField.Type.STRING)
133 .cardinality(ChampCardinality.SET)
139 storedBookooObject = graph.storeObject(bookooObject);
141 assertTrue(storedBookooObject.getProperty("property1").get().equals("value1"));
142 assertTrue(storedBookooObject.getProperty("integer").get().equals(1));
143 assertTrue(storedBookooObject.getProperty("long").get().equals(1L));
144 assertTrue(storedBookooObject.getProperty("double").get().equals(1.2));
145 assertTrue(storedBookooObject.getProperty("float").get().equals(2.3F));
146 assertTrue(storedBookooObject.getProperty("string").get().equals("foo"));
147 assertTrue(storedBookooObject.getProperty("boolean").get().equals(true));
148 assertTrue(storedBookooObject.getProperty("list").get().equals(Collections.singletonList("list")));
149 assertTrue(storedBookooObject.getProperty("set").get().equals(Collections.singleton("set")));
151 final Optional<ChampObject> retrievedBookooObject = graph.retrieveObject(storedBookooObject.getKey().get());
152 final Stream<ChampObject> emptyStream = graph.queryObjects(new HashMap<String, Object> () {{
153 put(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_TYPE.toString(), "foo");
157 assertTrue(emptyStream.limit(1).count() == 0);
159 final Stream<ChampObject> oneStream = graph.queryObjects(new HashMap<String, Object> () {{
160 put(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_TYPE.toString(), "foo");
163 final List<ChampObject> oneObject = oneStream.limit(2).collect(Collectors.toList());
164 assertTrue(oneObject.size() == 1);
165 assertTrue(oneObject.get(0).equals(storedBookooObject));
167 final List<ChampObject> queryByKey = graph.queryObjects(Collections.singletonMap(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_KEY.toString(), storedBookooObject.getKey().get()))
169 .collect(Collectors.toList());
171 assertTrue(queryByKey.size() == 1);
172 assertTrue(queryByKey.get(0).equals(storedBookooObject));
174 if (!retrievedBookooObject.isPresent()) throw new AssertionError("Failed to retrieve stored object " + bookooObject);
175 if (!storedBookooObject.equals(retrievedBookooObject.get())) throw new AssertionError("Retrieved object does not equal stored object");
177 final ChampObject updatedBookoo = graph.storeObject(ChampObject.create()
178 .from(storedBookooObject)
179 .withKey(storedBookooObject.getKey().get())
180 .withProperty("long", 2L)
183 final Optional<ChampObject> retrievedUpdBookooObject = graph.retrieveObject(updatedBookoo.getKey().get());
185 assertTrue(updatedBookoo.getProperty("property1").get().equals("value1"));
186 assertTrue(updatedBookoo.getProperty("integer").get().equals(1));
187 assertTrue(updatedBookoo.getProperty("long").get().equals(2L));
188 assertTrue(updatedBookoo.getProperty("double").get().equals(1.2));
189 assertTrue(updatedBookoo.getProperty("float").get().equals(2.3F));
190 assertTrue(updatedBookoo.getProperty("string").get().equals("foo"));
191 assertTrue(updatedBookoo.getProperty("boolean").get().equals(true));
192 assertTrue(updatedBookoo.getProperty("list").get().equals(Collections.singletonList("list")));
193 assertTrue(updatedBookoo.getProperty("set").get().equals(Collections.singleton("set")));
195 if (!retrievedUpdBookooObject.isPresent()) throw new AssertionError("Failed to retrieve stored object " + bookooObject);
196 if (!updatedBookoo.equals(retrievedUpdBookooObject.get())) throw new AssertionError("Retrieved object does not equal stored object");
198 //validate the replaceObject method
199 final ChampObject replacedBookoo = graph.replaceObject(ChampObject.create()
201 .withKey(storedBookooObject.getKey().get())
202 .withProperty("property1", "value2")
203 .withProperty("list", Collections.singletonList("list"))
204 .withProperty("set", Collections.singleton("set"))
207 final Optional<ChampObject> retrievedReplacedBookooObject = graph.retrieveObject(replacedBookoo.getKey().get());
209 assertTrue(replacedBookoo.getProperties().size()==3);
210 assertTrue(replacedBookoo.getProperty("property1").get().equals("value2"));
211 assertTrue(replacedBookoo.getProperty("list").get().equals(Collections.singletonList("list")));
212 assertTrue(replacedBookoo.getProperty("set").get().equals(Collections.singleton("set")));
215 if (!retrievedReplacedBookooObject.isPresent()) throw new AssertionError("Failed to retrieve stored object " + replacedBookoo);
216 if (!replacedBookoo.equals(retrievedReplacedBookooObject.get())) throw new AssertionError("Retrieved object does not equal stored object");
222 graph.deleteObject(storedBookooObject.getKey().get());
223 if (graph.retrieveObject(storedBookooObject.getKey().get()).isPresent()) throw new AssertionError("Object not successfully deleted");
225 assertTrue(graph.queryObjects(Collections.emptyMap()).count() == 0);
226 assertTrue(graph.queryRelationships(Collections.emptyMap()).count() == 0);
227 } catch (ChampSchemaViolationException e) {
228 throw new AssertionError("Schema mismatch while storing object", e);
229 } catch (ChampMarshallingException e) {
230 throw new AssertionError("Marshalling exception while storing object", e);
231 } catch (ChampUnmarshallingException e) {
232 throw new AssertionError("Unmarshalling exception while retrieving object", e);
233 }catch (ChampObjectNotExistsException e) {
234 throw new AssertionError("Missing object on delete/update", e);
238 graph.deleteObject(storedBookooObject.getKey().get());
239 throw new AssertionError("Delete succeeded when it should have failed");
240 } catch (ChampObjectNotExistsException e) {
245 graph.storeObject(ChampObject.create()
247 .withKey("non-existent object key")
249 throw new AssertionError("Expected ChampObjectNotExistsException but object was successfully stored");
250 } catch (ChampObjectNotExistsException e) {
252 } catch (ChampMarshallingException e) {
253 throw new AssertionError(e);
254 } catch (ChampSchemaViolationException e) {
255 throw new AssertionError(e);
259 // validate the replaceObject method when Object key is not passed
261 ChampObject.create().ofType("foo").withoutKey().withProperty("property1", "value2").build());
262 } catch (ChampObjectNotExistsException e) {
264 } catch (ChampMarshallingException e) {
265 throw new AssertionError(e);
266 } catch (ChampSchemaViolationException e) {
267 throw new AssertionError(e);
272 public void testChampObjectReservedProperties(ChampGraph graph) {
274 for (ChampObject.ReservedPropertyKeys key : ChampObject.ReservedPropertyKeys.values()) {
277 .ofType(ChampObject.ReservedTypes.ANY.toString())
279 .withProperty(key.toString(), "")
281 throw new AssertionError("Allowed reserved property key to be used during object creation");
282 } catch (IllegalArgumentException e) {
289 public void testFluentObjectCreation() {
290 final Object value1 = new Object();
291 final String value2 = "value2";
292 final float value3 = 0.0f;
294 final ChampObject champObject1 = ChampObject.create()
297 .withProperty("key1", value1)
298 .withProperty("key2", value2)
299 .withProperty("key3", value3)
302 assertTrue(champObject1.getKey().equals(Optional.empty()));
303 assertTrue(champObject1.getKey().isPresent() == false);
304 assertTrue(champObject1.getType().equals("foo"));
305 assertTrue(champObject1.getProperty("key1").get() instanceof Object);
306 assertTrue(champObject1.getProperty("key1").get().equals(value1));
307 assertTrue(champObject1.getProperty("key2").get() instanceof String);
308 assertTrue(champObject1.getProperty("key2").get().equals(value2));
309 assertTrue(champObject1.getProperty("key3").get() instanceof Float);
310 assertTrue(champObject1.getProperty("key3").get().equals(value3));
312 final ChampObject champObject2 = ChampObject.create()
315 .withProperty("key1", value1)
316 .withProperty("key2", value2)
317 .withProperty("key3", value3)
320 assertTrue(champObject2.getType().equals("foo"));
321 assertTrue(champObject2.getKey().isPresent() == true);
322 assertTrue(champObject2.getKey().get() instanceof Integer);
323 assertTrue(champObject2.getKey().get().equals(1));
324 assertTrue(champObject2.getProperty("key1").get() instanceof Object);
325 assertTrue(champObject2.getProperty("key1").get().equals(value1));
326 assertTrue(champObject2.getProperty("key2").get() instanceof String);
327 assertTrue(champObject2.getProperty("key2").get().equals(value2));
328 assertTrue(champObject2.getProperty("key3").get() instanceof Float);
329 assertTrue(champObject2.getProperty("key3").get().equals(value3));