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