2 * ============LICENSE_START==========================================
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
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============================================
21 package org.onap.aai.champcore.core;
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;
31 import java.util.Collections;
32 import java.util.Optional;
34 import static org.junit.Assert.assertTrue;
36 public class ChampPartitionTest extends org.onap.aai.champcore.core.BaseChampAPITest {
39 public void runInMemoryTests() {
40 runTests("IN_MEMORY");
43 public void runTests(String apiType) {
44 final ChampAPI api = ChampAPI.Factory.newInstance(apiType);
45 final String graphName = ChampPartitionTest.class.getSimpleName();
47 ChampPartitionTest.testChampPartitionCrud(api.getGraph(graphName));
52 public void testPartitionToString() throws Exception {
53 final ChampObject foo = ChampObject.create()
57 final ChampObject bar = ChampObject.create()
61 final ChampRelationship baz = ChampRelationship.create()
72 final ChampPartition partition = ChampPartition.create()
75 .withRelationship(baz)
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());
85 public void testHashCode() {
87 final ChampObject foo = ChampObject.create()
91 final ChampObject bar = ChampObject.create()
95 final ChampRelationship baz = ChampRelationship.create()
106 final ChampPartition partition = ChampPartition.create()
109 .withRelationship(baz)
112 assertTrue(partition.getChampObjects().contains(foo));
113 assertTrue(partition.getChampObjects().contains(bar));
114 assertTrue(partition.getChampRelationships().contains(baz));
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")
123 final ChampPartition a = new ChampPartition.Builder()
125 .objects(Collections.singleton(bar))
127 .relationships(Collections.singleton(uses))
129 assertTrue(a.getChampObjects().size() == 2);
130 assertTrue(a.getChampObjects().contains(foo));
131 assertTrue(a.getChampObjects().contains(bar));
133 assertTrue(a.getChampRelationships().size() == 1);
134 assertTrue(a.getChampRelationships().contains(uses));
137 public static void testChampPartitionCrud(ChampGraph graph) {
139 final ChampObject foo = ChampObject.create()
142 .withProperty("prop1", "value1")
144 final ChampObject bar = ChampObject.create()
147 .withProperty("prop2", "value2")
150 final ChampRelationship baz = ChampRelationship.create()
159 .withProperty("prop3", "value3")
162 final ChampPartition partition = ChampPartition.create()
165 .withRelationship(baz)
168 assertTrue(partition.getIncidentRelationships(foo).contains(baz));
169 assertTrue(partition.getIncidentRelationships(bar).contains(baz));
170 assertTrue(partition.getIncidentRelationshipsByType(foo).get("baz").contains(baz));
173 final ChampPartition storedPartition = graph.storePartition(partition);
175 ChampPartitionTest.retrievePartitionElements(graph, storedPartition, true);
177 graph.deletePartition(storedPartition);
179 ChampPartitionTest.retrievePartitionElements(graph, storedPartition, false);
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);
194 private static void retrievePartitionElements(ChampGraph graph, ChampPartition partition, boolean expectFound) {
195 for (ChampObject object : partition.getChampObjects()) {
197 final Optional<ChampObject> retrievedObject = graph.retrieveObject(object.getKey().get());
199 if (!expectFound && retrievedObject.isPresent()) {
200 throw new AssertionError("Expected object to not be found, but it was found");
202 if (expectFound && !retrievedObject.isPresent()) {
203 throw new AssertionError("Expected object to be found, but it was not found");
205 } catch (ChampUnmarshallingException | ChampTransactionException e) {
206 throw new AssertionError(e);
210 for (ChampRelationship relationship : partition.getChampRelationships()) {
212 final Optional<ChampRelationship> retrievedRelationship = graph.retrieveRelationship(relationship.getKey().get());
214 if (!expectFound && retrievedRelationship.isPresent()) {
215 throw new AssertionError("Expected relationship to not be found, but it was found");
217 if (expectFound && !retrievedRelationship.isPresent()) {
218 throw new AssertionError("Expected relationship to be found, but it was not found");
220 } catch (ChampUnmarshallingException | ChampTransactionException e) {
221 throw new AssertionError(e);