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.openecomp.aai.champ.core;
24 import static org.junit.Assert.assertTrue;
26 import java.util.Collections;
27 import java.util.Optional;
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;
41 public class ChampPartitionTest extends BaseChampAPITest {
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();
59 ChampPartitionTest.testChampPartitionCrud(api.getGraph(graphName));
65 public void testHashCode() {
67 final ChampObject foo = ChampObject.create()
71 final ChampObject bar = ChampObject.create()
75 final ChampRelationship baz = ChampRelationship.create()
86 final ChampPartition partition = ChampPartition.create()
89 .withRelationship(baz)
92 assertTrue(partition.getChampObjects().contains(foo));
93 assertTrue(partition.getChampObjects().contains(bar));
94 assertTrue(partition.getChampRelationships().contains(baz));
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")
103 final ChampPartition a = new ChampPartition.Builder()
105 .objects(Collections.singleton(bar))
107 .relationships(Collections.singleton(uses))
109 assertTrue(a.getChampObjects().size() == 2);
110 assertTrue(a.getChampObjects().contains(foo));
111 assertTrue(a.getChampObjects().contains(bar));
113 assertTrue(a.getChampRelationships().size() == 1);
114 assertTrue(a.getChampRelationships().contains(uses));
117 public static void testChampPartitionCrud(ChampGraph graph) {
119 final ChampObject foo = ChampObject.create()
122 .withProperty("prop1", "value1")
124 final ChampObject bar = ChampObject.create()
127 .withProperty("prop2", "value2")
130 final ChampRelationship baz = ChampRelationship.create()
139 .withProperty("prop3", "value3")
142 final ChampPartition partition = ChampPartition.create()
145 .withRelationship(baz)
148 assertTrue(partition.getIncidentRelationships(foo).contains(baz));
149 assertTrue(partition.getIncidentRelationships(bar).contains(baz));
150 assertTrue(partition.getIncidentRelationshipsByType(foo).get("baz").contains(baz));
153 final ChampPartition storedPartition = graph.storePartition(partition);
155 ChampPartitionTest.retrievePartitionElements(graph, storedPartition, true);
157 graph.deletePartition(storedPartition);
159 ChampPartitionTest.retrievePartitionElements(graph, storedPartition, false);
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);
172 private static void retrievePartitionElements(ChampGraph graph, ChampPartition partition, boolean expectFound) {
173 for (ChampObject object : partition.getChampObjects()) {
175 final Optional<ChampObject> retrievedObject = graph.retrieveObject(object.getKey().get());
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);
184 for (ChampRelationship relationship : partition.getChampRelationships()) {
186 final Optional<ChampRelationship> retrievedRelationship = graph.retrieveRelationship(relationship.getKey().get());
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);