Merge "Added @Override annotation above signature"
[aai/champ.git] / src / test / java / org / openecomp / aai / champ / 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.openecomp.aai.champ.core;
23
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.Collections;
27 import java.util.Optional;
28 import java.util.stream.Collectors;
29
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;
42
43 public class ChampRelationshipTest extends BaseChampAPITest {
44
45         @Test
46         public void runTest() {
47                 for (ChampGraph.Type apiType : ChampGraph.Type.values()) {
48                         final String graphName = ChampRelationshipTest.class.getSimpleName();
49
50                         switch (apiType) {
51                                 case IN_MEMORY:
52                                 break;
53                                 case TITAN:
54                                         cleanUp(graphName);
55                                 break;
56                                 default:
57                                 break;
58                         }
59
60                         final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
61                         ChampRelationshipTest.testChampRelationshipCrud(api.getGraph(graphName));
62                         api.shutdown();
63                 }
64         }
65         
66         public static void testChampRelationshipCrud(ChampGraph graph) {
67                 final ChampObject source = ChampObject.create()
68                                                                                                 .ofType("foo")
69                                                                                                 .withoutKey()
70                                                                                                 .withProperty("property1", "value1")
71                                                                                                 .build();
72
73                 final ChampObject target = ChampObject.create()
74                                                                                                 .ofType("foo")
75                                                                                                 .withoutKey()
76                                                                                                 .build();
77
78                 try {
79                         final ChampObject storedSource = graph.storeObject(source);
80                         final ChampObject storedTarget = graph.storeObject(target);
81
82                         final ChampRelationship relationship = new ChampRelationship.Builder(storedSource, storedTarget, "relationship")
83                                                                                                                                         .property("property-1", "value-1")
84                                                                                                                                         .property("property-2", 3)
85                                                                                                                                         .build();
86
87                         final ChampRelationship storedRelationship = graph.storeRelationship(relationship);
88                         final Optional<ChampRelationship> retrievedRelationship = graph.retrieveRelationship(storedRelationship.getKey().get());
89
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");
92
93                         assertTrue(retrievedRelationship.get().getProperty("property-1").get().equals("value-1"));
94                         assertTrue(retrievedRelationship.get().getProperty("property-2").get().equals(3));
95
96                         if (!graph.retrieveRelationships(storedRelationship.getSource()).collect(Collectors.toList()).contains(storedRelationship))
97                                 throw new AssertionError("Failed to retrieve relationships for source object");
98
99                         final ChampRelationship updatedRelationship = ChampRelationship.create()
100                                                                                                                                                         .from(retrievedRelationship.get())
101                                                                                                                                                         .withKey(retrievedRelationship.get().getKey().get())
102                                                                                                                                                         .withProperty("property-2", 4)
103                                                                                                                                                         .build();
104
105                         final ChampRelationship storedUpdRel = graph.storeRelationship(updatedRelationship);
106                         final Optional<ChampRelationship> retrievedUpdRel = graph.retrieveRelationship(storedUpdRel.getKey().get());
107
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));
112                         
113                         
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)
118                                         .build();
119
120                         final ChampRelationship replacedRel = graph.replaceRelationship(replacedRelationship);
121                         final Optional<ChampRelationship> retrievedReplacedRel = graph
122                                         .retrieveRelationship(replacedRel.getKey().get());
123                         
124                         assertTrue(replacedRel.getProperties().size()==1);
125                         assertTrue(replacedRel.getProperty("property-2").get().equals(4));
126                         
127                         assertTrue(retrievedReplacedRel.get().getProperties().size()==1);
128                         assertTrue(retrievedReplacedRel.get().getProperty("property-2").get().equals(4));
129                         
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");
132                         
133
134                         graph.deleteRelationship(retrievedRelationship.get());
135
136                         if (graph.retrieveRelationship(relationship.getKey()).isPresent()) throw new AssertionError("Relationship not successfully deleted");
137
138                         try {
139                                 graph.deleteRelationship(retrievedRelationship.get());
140                                 throw new AssertionError("Failed to throw exception for missing relationship");
141                         } catch (ChampRelationshipNotExistsException e) {
142                                 //Expected
143                         }
144
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);
157                 }
158
159                 try {
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) {
165                         //Expected
166                 }
167                 //Negative test cases for replace relationship
168                 
169                 try{
170                         graph.replaceRelationship(new ChampRelationship.Builder(ChampObject.create()
171                                         .ofType("foo")
172                                         .withoutKey()
173                                         .build(), ChampObject.create()
174                                         .ofType("foo")
175                                         .withoutKey()
176                                         .build(), "relationship")
177                         .key("1234")
178                         .property("property-2", 4)
179                         .build());
180                 }
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){
190                 //expected      
191                 }
192                 
193                 try{
194                         graph.replaceRelationship(new ChampRelationship.Builder(ChampObject.create()
195                                         .ofType("foo")
196                                         .withKey("123")
197                                         .build(), ChampObject.create()
198                                         .ofType("foo")
199                                         .withKey("456")
200                                         .build(), "relationship")                       
201                         .property("property-2", 4)
202                         .build());
203                 }
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) {
211                         //expected
212                 } catch(IllegalArgumentException e){
213                         throw new AssertionError(e);
214                 }
215                 
216                 
217         }
218         
219         @Test
220         public void testFluentRelationshipCreation() {
221                 final Object value1 = new Object();
222                 final String value2 = "value2";
223                 final float value3 = 0.0f;
224
225                 final ChampRelationship champRelationship = ChampRelationship.create()
226                                                                                                         .ofType("foo")
227                                                                                                         .withoutKey()
228                                                                                                         .withSource()
229                                                                                                                 .ofType("bar")
230                                                                                                                 .withoutKey()
231                                                                                                                 .build()
232                                                                                                         .withTarget()
233                                                                                                                 .ofType("baz")
234                                                                                                                 .withKey(1)
235                                                                                                                 .build()
236                                                                                                         .withProperty("key1", value1)
237                                                                                                         .withProperty("key2", value2)
238                                                                                                         .withProperty("key3", value3)
239                                                                                                         .build();
240
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));
249         }
250
251         @Test
252         public void testChampRelationshipEnums() {
253                 for (ReservedPropertyKeys key : ChampRelationship.ReservedPropertyKeys.values()) {
254                         assertTrue(ChampRelationship.ReservedPropertyKeys.valueOf(key.name()) == key);
255                 }
256
257                 for (ReservedTypes type : ChampRelationship.ReservedTypes.values()) {
258                         assertTrue(ChampRelationship.ReservedTypes.valueOf(type.name()) == type);
259                 }
260         }
261 }