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