Merge "Added @Override annotation above signature"
[aai/champ.git] / src / test / java / org / openecomp / aai / champ / core / ChampPartitionTest.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
29 import org.junit.Test;
30 import org.openecomp.aai.champ.ChampAPI;
31 import org.openecomp.aai.champ.ChampGraph;
32 import org.openecomp.aai.champ.exceptions.ChampMarshallingException;
33 import org.openecomp.aai.champ.exceptions.ChampObjectNotExistsException;
34 import org.openecomp.aai.champ.exceptions.ChampRelationshipNotExistsException;
35 import org.openecomp.aai.champ.exceptions.ChampSchemaViolationException;
36 import org.openecomp.aai.champ.exceptions.ChampUnmarshallingException;
37 import org.openecomp.aai.champ.model.ChampObject;
38 import org.openecomp.aai.champ.model.ChampPartition;
39 import org.openecomp.aai.champ.model.ChampRelationship;
40
41 public class ChampPartitionTest extends BaseChampAPITest {
42
43         @Test
44         public void runTests() {
45                 for (ChampGraph.Type apiType : ChampGraph.Type.values()) {
46                         final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
47                         final String graphName = ChampPartitionTest.class.getSimpleName();
48
49                         switch (apiType) {
50                         case IN_MEMORY:
51                         break;
52                         case TITAN:
53                                 cleanUp(graphName);
54                         break;
55                         default:
56                         break;
57                         }
58
59                         ChampPartitionTest.testChampPartitionCrud(api.getGraph(graphName));
60                         api.shutdown();
61                 }
62         }
63
64         @Test
65         public void testHashCode() {
66
67                 final ChampObject foo = ChampObject.create()
68                                                                                                 .ofType("foo")
69                                                                                                 .withoutKey()
70                                                                                                 .build();
71                 final ChampObject bar = ChampObject.create()
72                                                                                                 .ofType("bar")
73                                                                                                 .withoutKey()
74                                                                                                 .build();
75                 final ChampRelationship baz = ChampRelationship.create()
76                                                                                                                 .ofType("baz")
77                                                                                                                 .withoutKey()
78                                                                                                                 .withSource()
79                                                                                                                         .from(foo)
80                                                                                                                         .build()
81                                                                                                                 .withTarget()
82                                                                                                                         .from(bar)
83                                                                                                                         .build()
84                                                                                                                 .build();
85
86                 final ChampPartition partition = ChampPartition.create()
87                                                                                                                 .withObject(foo)
88                                                                                                                 .withObject(bar)
89                                                                                                                 .withRelationship(baz)
90                                                                                                                 .build();
91
92                 assertTrue(partition.getChampObjects().contains(foo));
93                 assertTrue(partition.getChampObjects().contains(bar));
94                 assertTrue(partition.getChampRelationships().contains(baz));
95         }
96
97         @Test
98         public void testBuilder() {
99                 final ChampObject foo = new ChampObject.Builder("foo").build();
100                 final ChampObject bar = new ChampObject.Builder("bar").build();
101                 final ChampRelationship uses = new ChampRelationship.Builder(foo, bar, "uses")
102                                                                                                                                 .build();
103                 final ChampPartition a = new ChampPartition.Builder()
104                                                                                                         .object(foo)
105                                                                                                         .objects(Collections.singleton(bar))
106                                                                                                         .relationship(uses)
107                                                                                                         .relationships(Collections.singleton(uses))
108                                                                                                         .build();
109                 assertTrue(a.getChampObjects().size() == 2);
110                 assertTrue(a.getChampObjects().contains(foo));
111                 assertTrue(a.getChampObjects().contains(bar));
112
113                 assertTrue(a.getChampRelationships().size() == 1);
114                 assertTrue(a.getChampRelationships().contains(uses));
115         }
116
117         public static void testChampPartitionCrud(ChampGraph graph) {
118
119                 final ChampObject foo = ChampObject.create()
120                                                                                         .ofType("foo")
121                                                                                         .withoutKey()
122                                                                                         .withProperty("prop1", "value1")
123                                                                                         .build();
124                 final ChampObject bar = ChampObject.create()
125                                                                                         .ofType("bar")
126                                                                                         .withoutKey()
127                                                                                         .withProperty("prop2", "value2")
128                                                                                         .build();
129
130                 final ChampRelationship baz = ChampRelationship.create()
131                                                                                                                 .ofType("baz")
132                                                                                                                 .withoutKey()
133                                                                                                                 .withSource()
134                                                                                                                         .from(foo)
135                                                                                                                         .build()
136                                                                                                                 .withTarget()
137                                                                                                                         .from(bar)
138                                                                                                                         .build()
139                                                                                                                 .withProperty("prop3", "value3")
140                                                                                                                 .build();
141
142                 final ChampPartition partition = ChampPartition.create()
143                                                                                                                 .withObject(foo)
144                                                                                                                 .withObject(bar)
145                                                                                                                 .withRelationship(baz)
146                                                                                                                 .build();
147
148                 assertTrue(partition.getIncidentRelationships(foo).contains(baz));
149                 assertTrue(partition.getIncidentRelationships(bar).contains(baz));
150                 assertTrue(partition.getIncidentRelationshipsByType(foo).get("baz").contains(baz));
151
152                 try {
153                         final ChampPartition storedPartition = graph.storePartition(partition);
154
155                         ChampPartitionTest.retrievePartitionElements(graph, storedPartition, true);
156
157                         graph.deletePartition(storedPartition);
158
159                         ChampPartitionTest.retrievePartitionElements(graph, storedPartition, false);
160
161                 } catch (ChampMarshallingException e) {
162                         throw new AssertionError(e);
163                 } catch (ChampObjectNotExistsException e) {
164                         throw new AssertionError(e);
165                 } catch (ChampSchemaViolationException e) {
166                         throw new AssertionError(e);
167                 } catch (ChampRelationshipNotExistsException e) {
168                         throw new AssertionError(e);
169                 }
170         }
171
172         private static void retrievePartitionElements(ChampGraph graph, ChampPartition partition, boolean expectFound) {
173                 for (ChampObject object : partition.getChampObjects()) {
174                         try {
175                                 final Optional<ChampObject> retrievedObject = graph.retrieveObject(object.getKey().get());
176                                 
177                                 if (!expectFound && retrievedObject.isPresent()) throw new AssertionError("Expected object to not be found, but it was found");
178                                 if (expectFound && !retrievedObject.isPresent()) throw new AssertionError("Expected object to be found, but it was not found");
179                         } catch (ChampUnmarshallingException e) {
180                                 throw new AssertionError(e);
181                         }
182                 }
183
184                 for (ChampRelationship relationship : partition.getChampRelationships()) {
185                         try {
186                                 final Optional<ChampRelationship> retrievedRelationship = graph.retrieveRelationship(relationship.getKey().get());
187                                 
188                                 if (!expectFound && retrievedRelationship.isPresent()) throw new AssertionError("Expected relationship to not be found, but it was found");
189                                 if (expectFound && !retrievedRelationship.isPresent()) throw new AssertionError("Expected relationship to be found, but it was not found");
190                         } catch (ChampUnmarshallingException e) {
191                                 throw new AssertionError(e);
192                         }
193                 }
194         }
195 }