2 * ============LICENSE_START==========================================
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
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============================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.champcore.core;
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;
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.assertTrue;
42 public class ChampRelationshipTest extends BaseChampAPITest {
45 public void runInMemoryTest() {
49 public void runTest(String apiType) {
50 final String graphName = ChampRelationshipTest.class.getSimpleName();
52 final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
53 ChampRelationshipTest.testChampRelationshipCrud(api.getGraph(graphName));
57 public static void testChampRelationshipCrud(ChampGraph graph) {
58 final ChampObject source = ChampObject.create()
61 .withProperty("property1", "value1")
64 final ChampObject target = ChampObject.create()
70 ChampTransaction tx = ChampRelationshipTest.getTransaction();
72 final ChampObject storedSource = graph.storeObject(source, Optional.of(tx));
73 final ChampObject storedTarget = graph.storeObject(target, Optional.of(tx));
75 final ChampRelationship relationship = new ChampRelationship.Builder(storedSource, storedTarget, "relationship")
76 .property("property-1", "value-1")
77 .property("property-2", 3)
80 final ChampRelationship storedRelationship = graph.storeRelationship(relationship, Optional.of(tx));
81 final Optional<ChampRelationship> retrievedRelationship = graph.retrieveRelationship(storedRelationship.getKey().get(), Optional.of(tx));
83 if (!retrievedRelationship.isPresent()) {
84 throw new AssertionError("Failed to retrieve stored relationship " + storedRelationship);
86 if (!storedRelationship.equals(retrievedRelationship.get())) {
87 throw new AssertionError("Retrieved relationship does not equal stored object");
90 assertTrue(retrievedRelationship.get().getProperty("property-1").get().equals("value-1"));
91 assertTrue(retrievedRelationship.get().getProperty("property-2").get().equals(3));
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");
97 final ChampRelationship updatedRelationship = ChampRelationship.create()
98 .from(retrievedRelationship.get())
99 .withKey(retrievedRelationship.get().getKey().get())
100 .withProperty("property-2", 4)
103 final ChampRelationship storedUpdRel = graph.storeRelationship(updatedRelationship, Optional.of(tx));
104 final Optional<ChampRelationship> retrievedUpdRel = graph.retrieveRelationship(storedUpdRel.getKey().get(), Optional.of(tx));
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));
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)
118 final ChampRelationship replacedRel = graph.replaceRelationship(replacedRelationship, Optional.of(tx));
119 final Optional<ChampRelationship> retrievedReplacedRel = graph
120 .retrieveRelationship(replacedRel.getKey().get(), Optional.of(tx));
122 assertTrue(replacedRel.getProperties().size() == 1);
123 assertTrue(replacedRel.getProperty("property-2").get().equals(4));
125 assertTrue(retrievedReplacedRel.get().getProperties().size() == 1);
126 assertTrue(retrievedReplacedRel.get().getProperty("property-2").get().equals(4));
128 if (!retrievedReplacedRel.isPresent()) {
129 throw new AssertionError("Failed to retrieve stored relationship " + replacedRel);
131 if (!replacedRel.equals(retrievedReplacedRel.get())) {
132 throw new AssertionError("Retrieved relationship does not equal stored object");
136 graph.deleteRelationship(retrievedRelationship.get(), Optional.of(tx));
138 if (graph.retrieveRelationship(relationship.getKey(), Optional.of(tx)).isPresent()) {
139 throw new AssertionError("Relationship not successfully deleted");
143 graph.deleteRelationship(retrievedRelationship.get(), Optional.of(tx));
144 throw new AssertionError("Failed to throw exception for missing relationship");
145 } catch (ChampRelationshipNotExistsException e) {
149 Map<String, Object> queryParams = new HashMap<>();
150 queryParams.put(ChampObject.ReservedPropertyKeys.CHAMP_OBJECT_TYPE.toString(), "blah");
151 graph.queryRelationships(queryParams);
153 queryParams = new HashMap<>();
154 queryParams.put(ChampRelationship.ReservedPropertyKeys.CHAMP_RELATIONSHIP_TYPE.toString(), "blah");
155 graph.queryRelationships(queryParams);
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);
173 ChampTransaction tx = ChampRelationshipTest.getTransaction();
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) {
182 //Negative test cases for replace relationship
185 graph.replaceRelationship(new ChampRelationship.Builder(ChampObject.create()
188 .build(), ChampObject.create()
191 .build(), "relationship")
193 .property("property-2", 4)
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) {
206 } catch (ChampTransactionException e) {
207 throw new AssertionError(e);
211 graph.replaceRelationship(new ChampRelationship.Builder(ChampObject.create()
214 .build(), ChampObject.create()
217 .build(), "relationship")
218 .property("property-2", 4)
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) {
229 } catch (IllegalArgumentException e) {
230 throw new AssertionError(e);
231 } catch (ChampTransactionException e) {
232 throw new AssertionError(e);
239 public void testFluentRelationshipCreation() {
240 final Object value1 = new Object();
241 final String value2 = "value2";
242 final float value3 = 0.0f;
244 final ChampRelationship champRelationship = ChampRelationship.create()
255 .withProperty("key1", value1)
256 .withProperty("key2", value2)
257 .withProperty("key3", value3)
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));
272 public void testChampRelationshipEnums() {
273 for (ReservedPropertyKeys key : ChampRelationship.ReservedPropertyKeys.values()) {
274 assertTrue(ChampRelationship.ReservedPropertyKeys.valueOf(key.name()) == key);
277 for (ReservedTypes type : ChampRelationship.ReservedTypes.values()) {
278 assertTrue(ChampRelationship.ReservedTypes.valueOf(type.name()) == type);
282 public static ChampTransaction getTransaction() {
283 return new TestTransaction();
286 public static class TestTransaction extends ChampTransaction {
288 public TestTransaction() {
292 public void commit() {
293 // TODO Auto-generated method stub
298 public void rollback() {
299 // TODO Auto-generated method stub
304 // TODO Auto-generated method stub