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