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.ReservedPropertyKeys;
31 import org.onap.aai.champcore.model.ChampRelationship.ReservedTypes;
33 import java.util.Collections;
34 import java.util.HashMap;
36 import java.util.Optional;
37 import java.util.stream.Collectors;
39 import static org.junit.Assert.assertTrue;
41 public class ChampRelationshipTest extends BaseChampAPITest {
44 public void runInMemoryTest() {
48 public void runTest(String apiType) {
49 final String graphName = ChampRelationshipTest.class.getSimpleName();
51 final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
52 ChampRelationshipTest.testChampRelationshipCrud(api.getGraph(graphName));
56 public static void testChampRelationshipCrud(ChampGraph graph) {
57 final ChampObject source = ChampObject.create()
60 .withProperty("property1", "value1")
63 final ChampObject target = ChampObject.create()
69 ChampTransaction tx = ChampRelationshipTest.getTransaction();
71 final ChampObject storedSource = graph.storeObject(source, Optional.of(tx));
72 final ChampObject storedTarget = graph.storeObject(target, Optional.of(tx));
74 final ChampRelationship relationship = new ChampRelationship.Builder(storedSource, storedTarget, "relationship")
75 .property("property-1", "value-1")
76 .property("property-2", 3)
79 final ChampRelationship storedRelationship = graph.storeRelationship(relationship, Optional.of(tx));
80 final Optional<ChampRelationship> retrievedRelationship = graph.retrieveRelationship(storedRelationship.getKey().get(), Optional.of(tx));
82 if (!retrievedRelationship.isPresent()) {
83 throw new AssertionError("Failed to retrieve stored relationship " + storedRelationship);
85 if (!storedRelationship.equals(retrievedRelationship.get())) {
86 throw new AssertionError("Retrieved relationship does not equal stored object");
89 assertTrue(retrievedRelationship.get().getProperty("property-1").get().equals("value-1"));
90 assertTrue(retrievedRelationship.get().getProperty("property-2").get().equals(3));
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");
96 final ChampRelationship updatedRelationship = ChampRelationship.create()
97 .from(retrievedRelationship.get())
98 .withKey(retrievedRelationship.get().getKey().get())
99 .withProperty("property-2", 4)
102 final ChampRelationship storedUpdRel = graph.storeRelationship(updatedRelationship, Optional.of(tx));
103 final Optional<ChampRelationship> retrievedUpdRel = graph.retrieveRelationship(storedUpdRel.getKey().get(), Optional.of(tx));
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));
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)
117 final ChampRelationship replacedRel = graph.replaceRelationship(replacedRelationship, Optional.of(tx));
118 final Optional<ChampRelationship> retrievedReplacedRel = graph
119 .retrieveRelationship(replacedRel.getKey().get(), Optional.of(tx));
121 assertTrue(replacedRel.getProperties().size() == 1);
122 assertTrue(replacedRel.getProperty("property-2").get().equals(4));
124 assertTrue(retrievedReplacedRel.get().getProperties().size() == 1);
125 assertTrue(retrievedReplacedRel.get().getProperty("property-2").get().equals(4));
127 if (!retrievedReplacedRel.isPresent()) {
128 throw new AssertionError("Failed to retrieve stored relationship " + replacedRel);
130 if (!replacedRel.equals(retrievedReplacedRel.get())) {
131 throw new AssertionError("Retrieved relationship does not equal stored object");
135 graph.deleteRelationship(retrievedRelationship.get(), Optional.of(tx));
137 if (graph.retrieveRelationship(relationship.getKey(), Optional.of(tx)).isPresent()) {
138 throw new AssertionError("Relationship not successfully deleted");
142 graph.deleteRelationship(retrievedRelationship.get(), Optional.of(tx));
143 throw new AssertionError("Failed to throw exception for missing relationship");
144 } catch (ChampRelationshipNotExistsException e) {
148 Map<String, Object> queryParams = new HashMap<>();
149 queryParams.put(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_TYPE.toString(), "blah");
150 graph.queryRelationships(queryParams);
152 queryParams = new HashMap<>();
153 queryParams.put(ChampRelationship.ReservedPropertyKeys.CHAMP_RELATIONSHIP_TYPE.toString(), "blah");
154 graph.queryRelationships(queryParams);
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);
172 ChampTransaction tx = ChampRelationshipTest.getTransaction();
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) {
181 //Negative test cases for replace relationship
184 graph.replaceRelationship(new ChampRelationship.Builder(ChampObject.create()
187 .build(), ChampObject.create()
190 .build(), "relationship")
192 .property("property-2", 4)
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) {
205 } catch (ChampTransactionException e) {
206 throw new AssertionError(e);
210 graph.replaceRelationship(new ChampRelationship.Builder(ChampObject.create()
213 .build(), ChampObject.create()
216 .build(), "relationship")
217 .property("property-2", 4)
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) {
228 } catch (IllegalArgumentException e) {
229 throw new AssertionError(e);
230 } catch (ChampTransactionException e) {
231 throw new AssertionError(e);
238 public void testFluentRelationshipCreation() {
239 final Object value1 = new Object();
240 final String value2 = "value2";
241 final float value3 = 0.0f;
243 final ChampRelationship champRelationship = ChampRelationship.create()
254 .withProperty("key1", value1)
255 .withProperty("key2", value2)
256 .withProperty("key3", value3)
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));
271 public void testChampRelationshipEnums() {
272 for (ReservedPropertyKeys key : ChampRelationship.ReservedPropertyKeys.values()) {
273 assertTrue(ChampRelationship.ReservedPropertyKeys.valueOf(key.name()) == key);
276 for (ReservedTypes type : ChampRelationship.ReservedTypes.values()) {
277 assertTrue(ChampRelationship.ReservedTypes.valueOf(type.name()) == type);
281 public static ChampTransaction getTransaction() {
282 return new TestTransaction();
285 public static class TestTransaction extends ChampTransaction {
287 public TestTransaction() {
291 public void commit() {
292 // TODO Auto-generated method stub
297 public void rollback() {
298 // TODO Auto-generated method stub
303 // TODO Auto-generated method stub