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.openecomp.aai.champ.core;
24 import static org.junit.Assert.assertTrue;
26 import java.util.Collections;
27 import java.util.Optional;
28 import java.util.stream.Collectors;
30 import org.junit.Test;
31 import org.openecomp.aai.champ.ChampAPI;
32 import org.openecomp.aai.champ.ChampGraph;
33 import org.openecomp.aai.champ.exceptions.ChampMarshallingException;
34 import org.openecomp.aai.champ.exceptions.ChampObjectNotExistsException;
35 import org.openecomp.aai.champ.exceptions.ChampRelationshipNotExistsException;
36 import org.openecomp.aai.champ.exceptions.ChampSchemaViolationException;
37 import org.openecomp.aai.champ.exceptions.ChampUnmarshallingException;
38 import org.openecomp.aai.champ.model.ChampObject;
39 import org.openecomp.aai.champ.model.ChampRelationship;
40 import org.openecomp.aai.champ.model.ChampRelationship.ReservedPropertyKeys;
41 import org.openecomp.aai.champ.model.ChampRelationship.ReservedTypes;
43 public class ChampRelationshipTest extends BaseChampAPITest {
46 public void runTest() {
47 for (ChampGraph.Type apiType : ChampGraph.Type.values()) {
48 final String graphName = ChampRelationshipTest.class.getSimpleName();
60 final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
61 ChampRelationshipTest.testChampRelationshipCrud(api.getGraph(graphName));
66 public static void testChampRelationshipCrud(ChampGraph graph) {
67 final ChampObject source = ChampObject.create()
70 .withProperty("property1", "value1")
73 final ChampObject target = ChampObject.create()
79 final ChampObject storedSource = graph.storeObject(source);
80 final ChampObject storedTarget = graph.storeObject(target);
82 final ChampRelationship relationship = new ChampRelationship.Builder(storedSource, storedTarget, "relationship")
83 .property("property-1", "value-1")
84 .property("property-2", 3)
87 final ChampRelationship storedRelationship = graph.storeRelationship(relationship);
88 final Optional<ChampRelationship> retrievedRelationship = graph.retrieveRelationship(storedRelationship.getKey().get());
90 if (!retrievedRelationship.isPresent()) throw new AssertionError("Failed to retrieve stored relationship " + storedRelationship);
91 if (!storedRelationship.equals(retrievedRelationship.get())) throw new AssertionError("Retrieved relationship does not equal stored object");
93 assertTrue(retrievedRelationship.get().getProperty("property-1").get().equals("value-1"));
94 assertTrue(retrievedRelationship.get().getProperty("property-2").get().equals(3));
96 if (!graph.retrieveRelationships(storedRelationship.getSource()).collect(Collectors.toList()).contains(storedRelationship))
97 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);
106 final Optional<ChampRelationship> retrievedUpdRel = graph.retrieveRelationship(storedUpdRel.getKey().get());
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);
121 final Optional<ChampRelationship> retrievedReplacedRel = graph
122 .retrieveRelationship(replacedRel.getKey().get());
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()) throw new AssertionError("Failed to retrieve stored relationship " + replacedRel);
131 if (!replacedRel.equals(retrievedReplacedRel.get())) throw new AssertionError("Retrieved relationship does not equal stored object");
134 graph.deleteRelationship(retrievedRelationship.get());
136 if (graph.retrieveRelationship(relationship.getKey()).isPresent()) throw new AssertionError("Relationship not successfully deleted");
139 graph.deleteRelationship(retrievedRelationship.get());
140 throw new AssertionError("Failed to throw exception for missing relationship");
141 } catch (ChampRelationshipNotExistsException e) {
145 assertTrue(graph.queryRelationships(Collections.emptyMap()).count() == 0);
146 assertTrue(graph.queryObjects(Collections.emptyMap()).count() == 2);
147 } catch (ChampSchemaViolationException e) {
148 throw new AssertionError("Schema mismatch while storing object", e);
149 } catch (ChampMarshallingException e) {
150 throw new AssertionError("Marshalling exception while storing object", e);
151 } catch (ChampUnmarshallingException e) {
152 throw new AssertionError("Unmarshalling exception while retrieving relationship", e);
153 } catch (ChampRelationshipNotExistsException e) {
154 throw new AssertionError("Attempted to delete non-existent relationship", e);
155 } catch (ChampObjectNotExistsException e) {
156 throw new AssertionError("Object does not exist after storing it", e);
160 graph.retrieveRelationships(ChampObject.create().ofType("").withoutKey().build());
161 throw new AssertionError("Failed to handle missing object while retrieving relationships");
162 } catch (ChampUnmarshallingException e) {
163 throw new AssertionError(e);
164 } catch (ChampObjectNotExistsException e) {
167 //Negative test cases for replace relationship
170 graph.replaceRelationship(new ChampRelationship.Builder(ChampObject.create()
173 .build(), ChampObject.create()
176 .build(), "relationship")
178 .property("property-2", 4)
181 catch (ChampUnmarshallingException e) {
182 throw new AssertionError(e);
183 } catch (ChampMarshallingException e) {
184 throw new AssertionError(e);
185 } catch (ChampSchemaViolationException e) {
186 throw new AssertionError(e);
187 } catch (ChampRelationshipNotExistsException e) {
188 throw new AssertionError(e);
189 } catch(IllegalArgumentException e){
194 graph.replaceRelationship(new ChampRelationship.Builder(ChampObject.create()
197 .build(), ChampObject.create()
200 .build(), "relationship")
201 .property("property-2", 4)
204 catch (ChampUnmarshallingException e) {
205 throw new AssertionError(e);
206 } catch (ChampMarshallingException e) {
207 throw new AssertionError(e);
208 } catch (ChampSchemaViolationException e) {
209 throw new AssertionError(e);
210 } catch (ChampRelationshipNotExistsException e) {
212 } catch(IllegalArgumentException e){
213 throw new AssertionError(e);
220 public void testFluentRelationshipCreation() {
221 final Object value1 = new Object();
222 final String value2 = "value2";
223 final float value3 = 0.0f;
225 final ChampRelationship champRelationship = ChampRelationship.create()
236 .withProperty("key1", value1)
237 .withProperty("key2", value2)
238 .withProperty("key3", value3)
241 assertTrue(champRelationship.getKey().equals(Optional.empty()));
242 assertTrue(champRelationship.getType().equals("foo"));
243 assertTrue(champRelationship.getProperty("key1").get() instanceof Object);
244 assertTrue(champRelationship.getProperty("key1").get().equals(value1));
245 assertTrue(champRelationship.getProperty("key2").get() instanceof String);
246 assertTrue(champRelationship.getProperty("key2").get().equals(value2));
247 assertTrue(champRelationship.getProperty("key3").get() instanceof Float);
248 assertTrue(champRelationship.getProperty("key3").get().equals(value3));
252 public void testChampRelationshipEnums() {
253 for (ReservedPropertyKeys key : ChampRelationship.ReservedPropertyKeys.values()) {
254 assertTrue(ChampRelationship.ReservedPropertyKeys.valueOf(key.name()) == key);
257 for (ReservedTypes type : ChampRelationship.ReservedTypes.values()) {
258 assertTrue(ChampRelationship.ReservedTypes.valueOf(type.name()) == type);