Merge "Added @Override annotation above signature"
[aai/champ.git] / src / test / java / org / openecomp / aai / champ / 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.openecomp.aai.champ.core;
23
24 import static org.junit.Assert.assertTrue;
25
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;
32
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;
44
45 public class ChampObjectTest extends BaseChampAPITest {
46
47         @Test
48         public void testHashCode() {
49                 final ChampObject foo1 = ChampObject.create()
50                                         .ofType("foo")
51                                         .withoutKey()
52                                         .withProperty("property", "value")
53                                         .withProperty("prop", 1)
54                                         .build();
55
56                 final ChampObject foo2 = ChampObject.create()
57                                                                                         .ofType("foo")
58                                                                                         .withoutKey()
59                                                                                         .withProperty("property", "value")
60                                                                                         .withProperty("prop", 1)
61                                                                                         .build();
62
63                 final ChampObject foo1Copy = ChampObject.create()
64                                                                                                 .from(foo1)
65                                                                                                 .withoutKey()
66                                                                                                 .build();
67
68                 final ChampObject foo2Copy = ChampObject.create()
69                                                                                                 .from(foo2)
70                                                                                                 .withoutKey()
71                                                                                                 .build();
72
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());
78                 
79                 assertTrue(Collections.singleton(foo1).contains(foo1));
80                 assertTrue(Collections.singleton(foo1).contains(foo1Copy));
81         }
82
83         @Test
84         public void runTest() {
85                 for (ChampGraph.Type apiType : ChampGraph.Type.values()) {
86                         final String graphName = ChampObjectTest.class.getSimpleName();
87                         switch (apiType) {
88                                 case TITAN:
89                                         cleanUp(graphName);
90                                         break;
91                                 default:
92                                 break;
93                         }
94
95                         final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
96                         ChampObjectTest.testChampObjectCrud(api.getGraph(graphName));
97                         testChampObjectReservedProperties(api.getGraph(graphName));
98                         api.shutdown();
99                 }
100         }
101         
102         public static void testChampObjectCrud(ChampGraph graph) {
103                 final ChampObject bookooObject = ChampObject.create()
104                                                                                                         .ofType("foo")
105                                                                                                         .withoutKey()
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"))
115                                                                                                         .build();
116
117                 final ChampObject storedBookooObject;
118
119                 try {
120
121                         graph.storeSchema(ChampSchema.create()
122                                                                                         .withObjectConstraint()
123                                                                                                 .onType("foo")
124                                                                                                 .withPropertyConstraint()
125                                                                                                         .onField("list")
126                                                                                                         .ofType(ChampField.Type.STRING)
127                                                                                                         .cardinality(ChampCardinality.LIST)
128                                                                                                         .optional()
129                                                                                                         .build()
130                                                                                                 .withPropertyConstraint()
131                                                                                                         .onField("set")
132                                                                                                         .ofType(ChampField.Type.STRING)
133                                                                                                         .cardinality(ChampCardinality.SET)
134                                                                                                         .optional()
135                                                                                                         .build()
136                                                                                                 .build()
137                                                                                         .build());
138
139                         storedBookooObject = graph.storeObject(bookooObject);
140
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")));
150
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");
154                                                                                                                                                                 put("long", 2L);
155                                                                                                                                                         }});
156                         
157                         assertTrue(emptyStream.limit(1).count() == 0);
158
159                         final Stream<ChampObject> oneStream = graph.queryObjects(new HashMap<String, Object> () {{
160                                                                                                                                                                 put(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_TYPE.toString(), "foo");
161                                                                                                                                                                 put("long", 1L);
162                                                                                                                                                         }});
163                         final List<ChampObject> oneObject = oneStream.limit(2).collect(Collectors.toList());
164                         assertTrue(oneObject.size() == 1);
165                         assertTrue(oneObject.get(0).equals(storedBookooObject));
166
167                         final List<ChampObject> queryByKey = graph.queryObjects(Collections.singletonMap(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_KEY.toString(), storedBookooObject.getKey().get()))
168                                                                                                                 .limit(2)
169                                                                                                                 .collect(Collectors.toList());
170
171                         assertTrue(queryByKey.size() == 1);
172                         assertTrue(queryByKey.get(0).equals(storedBookooObject));
173
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");
176
177                         final ChampObject updatedBookoo = graph.storeObject(ChampObject.create()
178                                                                                                         .from(storedBookooObject)
179                                                                                                         .withKey(storedBookooObject.getKey().get())
180                                                                                                         .withProperty("long", 2L)
181                                                                                                         .build());
182
183                         final Optional<ChampObject> retrievedUpdBookooObject = graph.retrieveObject(updatedBookoo.getKey().get());
184
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")));
194
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");
197                         
198                         //validate the replaceObject method
199                         final ChampObject replacedBookoo = graph.replaceObject(ChampObject.create()
200                                         .ofType("foo")
201                                         .withKey(storedBookooObject.getKey().get())
202                                         .withProperty("property1", "value2")
203                                         .withProperty("list", Collections.singletonList("list"))
204                                         .withProperty("set", Collections.singleton("set"))
205                                         .build());
206                         
207                         final Optional<ChampObject> retrievedReplacedBookooObject = graph.retrieveObject(replacedBookoo.getKey().get());
208                         
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")));
213                         
214                         
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");
217                         
218
219                         
220                         
221                         
222                         graph.deleteObject(storedBookooObject.getKey().get());
223                         if (graph.retrieveObject(storedBookooObject.getKey().get()).isPresent()) throw new AssertionError("Object not successfully deleted");
224
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);
235                 }
236
237                 try {
238                         graph.deleteObject(storedBookooObject.getKey().get());
239                         throw new AssertionError("Delete succeeded when it should have failed");
240                 } catch (ChampObjectNotExistsException e) {
241                         //Expected
242                 }
243
244                 try {
245                         graph.storeObject(ChampObject.create()
246                                                                                         .ofType("foo")
247                                                                                         .withKey("non-existent object key")
248                                                                                         .build());
249                         throw new AssertionError("Expected ChampObjectNotExistsException but object was successfully stored");
250                 } catch (ChampObjectNotExistsException e) {
251                         //Expected
252                 } catch (ChampMarshallingException e) {
253                         throw new AssertionError(e);
254                 } catch (ChampSchemaViolationException e) {
255                         throw new AssertionError(e);
256                 }
257                 
258                 try {
259                         // validate the replaceObject method when Object key is not passed
260                         graph.replaceObject(
261                                         ChampObject.create().ofType("foo").withoutKey().withProperty("property1", "value2").build());
262                 } catch (ChampObjectNotExistsException e) {
263                         // Expected
264                 } catch (ChampMarshallingException e) {
265                         throw new AssertionError(e);
266                 } catch (ChampSchemaViolationException e) {
267                         throw new AssertionError(e);
268                 }
269                 
270         }
271
272         public void testChampObjectReservedProperties(ChampGraph graph) {
273
274                 for (ChampObject.ReservedPropertyKeys key : ChampObject.ReservedPropertyKeys.values()) {
275                         try {
276                                 ChampObject.create()
277                                                         .ofType(ChampObject.ReservedTypes.ANY.toString())
278                                                         .withoutKey()
279                                                         .withProperty(key.toString(), "")
280                                                         .build();
281                                 throw new AssertionError("Allowed reserved property key to be used during object creation");
282                         } catch (IllegalArgumentException e) {
283                                 //Expected
284                         }
285                 }
286         }
287
288         @Test
289         public void testFluentObjectCreation() {
290                 final Object value1 = new Object();
291                 final String value2 = "value2";
292                 final float value3 = 0.0f;
293
294                 final ChampObject champObject1 = ChampObject.create()
295                                                                                                         .ofType("foo")
296                                                                                                         .withoutKey()
297                                                                                                         .withProperty("key1", value1)
298                                                                                                         .withProperty("key2", value2)
299                                                                                                         .withProperty("key3", value3)
300                                                                                                         .build();
301
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));
311
312                 final ChampObject champObject2 = ChampObject.create()
313                                                                                                         .ofType("foo")
314                                                                                                         .withKey(1)
315                                                                                                         .withProperty("key1", value1)
316                                                                                                         .withProperty("key2", value2)
317                                                                                                         .withProperty("key3", value3)
318                                                                                                         .build();
319
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));
330         }
331 }