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