2 * ============LICENSE_START==========================================
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
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============================================
21 package org.onap.aai.champcore.core;
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.Builder;
31 import org.onap.aai.champcore.model.ChampRelationship.ReservedPropertyKeys;
32 import org.onap.aai.champcore.model.ChampRelationship.ReservedTypes;
34 import java.util.Collections;
35 import java.util.HashMap;
37 import java.util.Optional;
38 import java.util.stream.Collectors;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNotEquals;
42 import static org.junit.Assert.assertTrue;
44 public class ChampRelationshipTest extends BaseChampAPITest {
47 public void runInMemoryTest() {
51 public void runTest(String apiType) {
52 final String graphName = ChampRelationshipTest.class.getSimpleName();
54 final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
55 ChampRelationshipTest.testChampRelationshipCrud(api.getGraph(graphName));
59 public static void testChampRelationshipCrud(ChampGraph graph) {
60 final ChampObject source = ChampObject.create()
63 .withProperty("property1", "value1")
66 final ChampObject target = ChampObject.create()
72 ChampTransaction tx = ChampRelationshipTest.getTransaction();
74 final ChampObject storedSource = graph.storeObject(source, Optional.of(tx));
75 final ChampObject storedTarget = graph.storeObject(target, Optional.of(tx));
77 final ChampRelationship relationship = new ChampRelationship.Builder(storedSource, storedTarget, "relationship")
78 .property("property-1", "value-1")
79 .property("property-2", 3)
82 final ChampRelationship storedRelationship = graph.storeRelationship(relationship, Optional.of(tx));
83 final Optional<ChampRelationship> retrievedRelationship = graph.retrieveRelationship(storedRelationship.getKey().get(), Optional.of(tx));
85 if (!retrievedRelationship.isPresent()) {
86 throw new AssertionError("Failed to retrieve stored relationship " + storedRelationship);
88 if (!storedRelationship.equals(retrievedRelationship.get())) {
89 throw new AssertionError("Retrieved relationship does not equal stored object");
92 assertTrue(retrievedRelationship.get().getProperty("property-1").get().equals("value-1"));
93 assertTrue(retrievedRelationship.get().getProperty("property-2").get().equals(3));
95 if (!graph.retrieveRelationships(storedRelationship.getSource(), Optional.of(tx)).collect(Collectors.toList()).contains(storedRelationship)) {
96 throw new AssertionError("Failed to retrieve relationships for source object");
99 final ChampRelationship updatedRelationship = ChampRelationship.create()
100 .from(retrievedRelationship.get())
101 .withKey(retrievedRelationship.get().getKey().get())
102 .withProperty("property-2", 4)
105 final ChampRelationship storedUpdRel = graph.storeRelationship(updatedRelationship, Optional.of(tx));
106 final Optional<ChampRelationship> retrievedUpdRel = graph.retrieveRelationship(storedUpdRel.getKey().get(), Optional.of(tx));
108 assertTrue(retrievedUpdRel.isPresent());
109 assertTrue(retrievedUpdRel.get().equals(storedUpdRel));
110 assertTrue(retrievedUpdRel.get().getProperty("property-1").get().equals("value-1"));
111 assertTrue(retrievedUpdRel.get().getProperty("property-2").get().equals(4));
114 // validate the replaceRelationship method
115 final ChampRelationship replacedRelationship = new ChampRelationship.Builder(storedSource, storedTarget, "relationship")
116 .key(retrievedRelationship.get().getKey().get())
117 .property("property-2", 4)
120 final ChampRelationship replacedRel = graph.replaceRelationship(replacedRelationship, Optional.of(tx));
121 final Optional<ChampRelationship> retrievedReplacedRel = graph
122 .retrieveRelationship(replacedRel.getKey().get(), Optional.of(tx));
124 assertTrue(replacedRel.getProperties().size() == 1);
125 assertTrue(replacedRel.getProperty("property-2").get().equals(4));
127 assertTrue(retrievedReplacedRel.get().getProperties().size() == 1);
128 assertTrue(retrievedReplacedRel.get().getProperty("property-2").get().equals(4));
130 if (!retrievedReplacedRel.isPresent()) {
131 throw new AssertionError("Failed to retrieve stored relationship " + replacedRel);
133 if (!replacedRel.equals(retrievedReplacedRel.get())) {
134 throw new AssertionError("Retrieved relationship does not equal stored object");
138 graph.deleteRelationship(retrievedRelationship.get(), Optional.of(tx));
140 if (graph.retrieveRelationship(relationship.getKey(), Optional.of(tx)).isPresent()) {
141 throw new AssertionError("Relationship not successfully deleted");
145 graph.deleteRelationship(retrievedRelationship.get(), Optional.of(tx));
146 throw new AssertionError("Failed to throw exception for missing relationship");
147 } catch (ChampRelationshipNotExistsException e) {
151 Map<String, Object> queryParams = new HashMap<>();
152 queryParams.put(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_TYPE.toString(), "blah");
153 graph.queryRelationships(queryParams);
155 queryParams = new HashMap<>();
156 queryParams.put(ChampRelationship.ReservedPropertyKeys.CHAMP_RELATIONSHIP_TYPE.toString(), "blah");
157 graph.queryRelationships(queryParams);
159 assertTrue(graph.queryRelationships(Collections.emptyMap(), Optional.of(tx)).count() == 0);
160 assertTrue(graph.queryObjects(Collections.emptyMap(), Optional.of(tx)).count() == 2);
161 } catch (ChampSchemaViolationException e) {
162 throw new AssertionError("Schema mismatch while storing object", e);
163 } catch (ChampMarshallingException e) {
164 throw new AssertionError("Marshalling exception while storing object", e);
165 } catch (ChampUnmarshallingException e) {
166 throw new AssertionError("Unmarshalling exception while retrieving relationship", e);
167 } catch (ChampRelationshipNotExistsException e) {
168 throw new AssertionError("Attempted to delete non-existent relationship", e);
169 } catch (ChampObjectNotExistsException e) {
170 throw new AssertionError("Object does not exist after storing it", e);
171 } catch (ChampTransactionException e) {
172 throw new AssertionError("Transaction failure", e);
175 ChampTransaction tx = ChampRelationshipTest.getTransaction();
177 graph.retrieveRelationships(ChampObject.create().ofType("").withoutKey().build(), Optional.of(tx));
178 throw new AssertionError("Failed to handle missing object while retrieving relationships");
179 } catch (ChampUnmarshallingException | ChampTransactionException e) {
180 throw new AssertionError(e);
181 } catch (ChampObjectNotExistsException e) {
184 //Negative test cases for replace relationship
187 graph.replaceRelationship(new ChampRelationship.Builder(ChampObject.create()
190 .build(), ChampObject.create()
193 .build(), "relationship")
195 .property("property-2", 4)
198 } catch (ChampUnmarshallingException e) {
199 throw new AssertionError(e);
200 } catch (ChampMarshallingException e) {
201 throw new AssertionError(e);
202 } catch (ChampSchemaViolationException e) {
203 throw new AssertionError(e);
204 } catch (ChampRelationshipNotExistsException e) {
205 throw new AssertionError(e);
206 } catch (IllegalArgumentException e) {
208 } catch (ChampTransactionException e) {
209 throw new AssertionError(e);
213 graph.replaceRelationship(new ChampRelationship.Builder(ChampObject.create()
216 .build(), ChampObject.create()
219 .build(), "relationship")
220 .property("property-2", 4)
223 } catch (ChampUnmarshallingException e) {
224 throw new AssertionError(e);
225 } catch (ChampMarshallingException e) {
226 throw new AssertionError(e);
227 } catch (ChampSchemaViolationException e) {
228 throw new AssertionError(e);
229 } catch (ChampRelationshipNotExistsException e) {
231 } catch (IllegalArgumentException e) {
232 throw new AssertionError(e);
233 } catch (ChampTransactionException e) {
234 throw new AssertionError(e);
241 public void testFluentRelationshipCreation() {
242 final Object value1 = new Object();
243 final String value2 = "value2";
244 final float value3 = 0.0f;
246 final ChampRelationship champRelationship = ChampRelationship.create()
257 .withProperty("key1", value1)
258 .withProperty("key2", value2)
259 .withProperty("key3", value3)
262 ChampTransaction tx = ChampRelationshipTest.getTransaction();
263 assertTrue(champRelationship.getKey().equals(Optional.empty()));
264 assertTrue(champRelationship.getType().equals("foo"));
265 assertTrue(champRelationship.getProperty("key1").get() instanceof Object);
266 assertTrue(champRelationship.getProperty("key1").get().equals(value1));
267 assertTrue(champRelationship.getProperty("key2").get() instanceof String);
268 assertTrue(champRelationship.getProperty("key2").get().equals(value2));
269 assertTrue(champRelationship.getProperty("key3").get() instanceof Float);
270 assertTrue(champRelationship.getProperty("key3").get().equals(value3));
274 public void testChampRelationshipEnums() {
275 for (ReservedPropertyKeys key : ChampRelationship.ReservedPropertyKeys.values()) {
276 assertTrue(ChampRelationship.ReservedPropertyKeys.valueOf(key.name()) == key);
279 for (ReservedTypes type : ChampRelationship.ReservedTypes.values()) {
280 assertTrue(ChampRelationship.ReservedTypes.valueOf(type.name()) == type);
285 public void verifyEqualsAndHashCodeMethods() {
286 ChampObject source = new ChampObject.Builder("type").build();
287 ChampObject target = new ChampObject.Builder("type").build();
288 ChampRelationship champRelationship = new Builder(source, target, "type").key("someKey").build();
289 ChampRelationship obj1 = new Builder(champRelationship).key("someKey").build();
290 ChampRelationship obj2 = new Builder(champRelationship).key("someKey").build();
291 ChampRelationship obj3 = new Builder(champRelationship).key("someKey").build();
292 ChampRelationship obj4 = new Builder(champRelationship).key("differentKey").build();
295 assertEquals(obj1, obj2);
296 assertEquals(obj1.hashCode(), obj2.hashCode());
298 assertEquals(obj1, obj3);
299 assertEquals(obj1.hashCode(), obj3.hashCode());
301 assertEquals(obj2, obj3);
302 assertEquals(obj2.hashCode(), obj3.hashCode());
304 assertNotEquals(obj1, obj4);
305 assertNotEquals(obj1.hashCode(), obj4.hashCode());
308 public static ChampTransaction getTransaction() {
309 return new TestTransaction();
312 public static class TestTransaction extends ChampTransaction {
314 public TestTransaction() {
318 public void commit() {
319 // TODO Auto-generated method stub
324 public void rollback() {
325 // TODO Auto-generated method stub
330 // TODO Auto-generated method stub