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.onap.aai.champcore.core;
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;
32 import java.util.Collections;
33 import java.util.Optional;
35 import static org.junit.Assert.assertTrue;
37 public class ChampPartitionTest extends org.onap.aai.champcore.core.BaseChampAPITest {
40 public void runInMemoryTests() {
41 runTests("IN_MEMORY");
44 public void runTests(String apiType) {
45 final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
46 final String graphName = ChampPartitionTest.class.getSimpleName();
48 ChampPartitionTest.testChampPartitionCrud(api.getGraph(graphName));
53 public void testPartitionToString() throws Exception {
54 final ChampObject foo = ChampObject.create()
58 final ChampObject bar = ChampObject.create()
62 final ChampRelationship baz = ChampRelationship.create()
73 final ChampPartition partition = ChampPartition.create()
76 .withRelationship(baz)
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());
86 public void testHashCode() {
88 final ChampObject foo = ChampObject.create()
92 final ChampObject bar = ChampObject.create()
96 final ChampRelationship baz = ChampRelationship.create()
107 final ChampPartition partition = ChampPartition.create()
110 .withRelationship(baz)
113 assertTrue(partition.getChampObjects().contains(foo));
114 assertTrue(partition.getChampObjects().contains(bar));
115 assertTrue(partition.getChampRelationships().contains(baz));
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")
124 final ChampPartition a = new ChampPartition.Builder()
126 .objects(Collections.singleton(bar))
128 .relationships(Collections.singleton(uses))
130 assertTrue(a.getChampObjects().size() == 2);
131 assertTrue(a.getChampObjects().contains(foo));
132 assertTrue(a.getChampObjects().contains(bar));
134 assertTrue(a.getChampRelationships().size() == 1);
135 assertTrue(a.getChampRelationships().contains(uses));
138 public static void testChampPartitionCrud(ChampGraph graph) {
140 final ChampObject foo = ChampObject.create()
143 .withProperty("prop1", "value1")
145 final ChampObject bar = ChampObject.create()
148 .withProperty("prop2", "value2")
151 final ChampRelationship baz = ChampRelationship.create()
160 .withProperty("prop3", "value3")
163 final ChampPartition partition = ChampPartition.create()
166 .withRelationship(baz)
169 assertTrue(partition.getIncidentRelationships(foo).contains(baz));
170 assertTrue(partition.getIncidentRelationships(bar).contains(baz));
171 assertTrue(partition.getIncidentRelationshipsByType(foo).get("baz").contains(baz));
174 final ChampPartition storedPartition = graph.storePartition(partition);
176 ChampPartitionTest.retrievePartitionElements(graph, storedPartition, true);
178 graph.deletePartition(storedPartition);
180 ChampPartitionTest.retrievePartitionElements(graph, storedPartition, false);
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);
195 private static void retrievePartitionElements(ChampGraph graph, ChampPartition partition, boolean expectFound) {
196 for (ChampObject object : partition.getChampObjects()) {
198 final Optional<ChampObject> retrievedObject = graph.retrieveObject(object.getKey().get());
200 if (!expectFound && retrievedObject.isPresent()) {
201 throw new AssertionError("Expected object to not be found, but it was found");
203 if (expectFound && !retrievedObject.isPresent()) {
204 throw new AssertionError("Expected object to be found, but it was not found");
206 } catch (ChampUnmarshallingException | ChampTransactionException e) {
207 throw new AssertionError(e);
211 for (ChampRelationship relationship : partition.getChampRelationships()) {
213 final Optional<ChampRelationship> retrievedRelationship = graph.retrieveRelationship(relationship.getKey().get());
215 if (!expectFound && retrievedRelationship.isPresent()) {
216 throw new AssertionError("Expected relationship to not be found, but it was found");
218 if (expectFound && !retrievedRelationship.isPresent()) {
219 throw new AssertionError("Expected relationship to be found, but it was not found");
221 } catch (ChampUnmarshallingException | ChampTransactionException e) {
222 throw new AssertionError(e);