2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 European Software Marketing Ltd.
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.modelloader.entity.model;
23 import static org.hamcrest.CoreMatchers.equalTo;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.CoreMatchers.not;
26 import static org.hamcrest.CoreMatchers.nullValue;
27 import static org.hamcrest.MatcherAssert.assertThat;
28 import static org.hamcrest.Matchers.isEmptyString;
29 import static org.junit.jupiter.api.Assertions.assertThrows;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
36 import org.junit.jupiter.api.Test;
37 import org.onap.aai.modelloader.entity.Artifact;
39 public class ModelSorterTest {
42 public void edgeEquality() throws BabelArtifactParsingException {
43 ModelArtifact model = buildTestModel();
44 ModelSorter.Node nodeA = new ModelSorter.Node(model);
45 ModelSorter.Node nodeB = new ModelSorter.Node(model);
47 ModelSorter.Edge edgeA = new ModelSorter.Edge(nodeA, nodeB);
49 assertThat(edgeA, is(equalTo(edgeA)));
50 assertThat(edgeA, is(not(equalTo(null))));
51 assertThat(edgeA, is(not(equalTo(model))));
53 ModelSorter.Edge edgeB = new ModelSorter.Edge(nodeA, nodeB);
54 ModelSorter.Edge edgeC = new ModelSorter.Edge(nodeB, nodeA);
56 ModelSorter.Node nodeC = new ModelSorter.Node(model);
57 ModelSorter.Edge edgeD = new ModelSorter.Edge(nodeA, nodeC);
58 assertThat(edgeA, is(equalTo(edgeB)));
59 assertThat(edgeA, is(not(equalTo(edgeC))));
60 assertThat(edgeA, is(not(equalTo(edgeD))));
64 public void nodeEquality() throws BabelArtifactParsingException {
65 ModelArtifact model = buildTestModel();
66 ModelSorter.Node nodeA = new ModelSorter.Node(model);
68 assertThat(nodeA, is(equalTo(nodeA)));
69 assertThat(nodeA, is(not(equalTo(null))));
70 assertThat(nodeA, is(not(equalTo(model))));
72 ModelSorter.Node nodeB = new ModelSorter.Node(model);
73 assertThat(nodeA, is(equalTo(nodeB)));
74 assertThat(nodeA.toString(), is(equalTo(nodeB.toString())));
75 assertThat(nodeA, is(not(equalTo(new ModelSorter.Node(new ModelArtifact())))));
79 public void testToString() throws BabelArtifactParsingException {
80 ModelArtifact model = buildTestModel();
82 ModelSorter.Node nodeA = new ModelSorter.Node(model);
83 assertThat(nodeA.toString(), not(isEmptyString()));
85 ModelSorter.Node nodeB = new ModelSorter.Node(model);
87 assertThat(nodeA.toString(), not(isEmptyString()));
88 assertThat(nodeB.toString(), not(isEmptyString()));
93 public void noModels() throws BabelArtifactParsingException {
94 assertThat(new ModelSorter().sort(null), is(nullValue()));
95 assertThat(new ModelSorter().sort(Collections.emptyList()).size(), is(0));
99 public void singleModel() throws BabelArtifactParsingException {
100 assertThat(new ModelSorter().sort(Arrays.asList(buildTestModel())).size(), is(1));
104 public void multipleModels() throws BabelArtifactParsingException {
105 Artifact artA = buildTestModel("aaaa", "mvaaaa", "cccc|mvcccc");
106 Artifact artB = buildTestModel("bbbb", "mvbbbb", "aaaa|mvaaaa");
107 Artifact artC = buildTestModel("cccc", "mvcccc");
108 List<Artifact> expected = Arrays.asList(artC, artA, artB);
109 assertThat(new ModelSorter().sort(Arrays.asList(artA, artB, artC)), is(expected));
114 public void multipleModelsWithMultipleIncomingEdges() throws BabelArtifactParsingException {
115 ModelArtifact artA = buildTestModel("aaaa", "mvaaaa", "cccc|mvcccc");
116 Artifact artB = buildTestModel("bbbb", "mvbbbb", "aaaa|mvaaaa");
117 Artifact artC = buildTestModel("cccc", "mvcccc");
118 Artifact artD = buildTestModel("dddd", "mvdddd", "cccc|mvcccc");
119 artA.addDependentModelId("dddd|mvdddd");
120 List<Artifact> expected = Arrays.asList(artC, artD, artA, artB);
121 assertThat(new ModelSorter().sort(Arrays.asList(artA, artB, artC, artD)), is(expected));
125 public void multipleModelsAndNamedQueries() throws BabelArtifactParsingException {
126 Artifact artifact = buildTestModel("aaaa", "1111", "cccc|2222");
127 Artifact nq1 = buildTestNamedQuery("nq1", "aaaa|1111");
128 Artifact nq2 = buildTestNamedQuery("nqw", "existing-model");
129 List<Artifact> expected = Arrays.asList(artifact, nq2, nq1);
130 assertThat(new ModelSorter().sort(Arrays.asList(nq1, nq2, artifact)), is(expected));
134 public void circularDependency() throws BabelArtifactParsingException {
135 assertThrows(BabelArtifactParsingException.class, () -> {
136 List<Artifact> modelList = new ArrayList<Artifact>();
137 modelList.add(buildTestModel("aaaa", "1111", "bbbb|1111"));
138 modelList.add(buildTestModel("bbbb", "1111", "aaaa|1111"));
139 new ModelSorter().sort(modelList);
143 private ModelArtifact buildTestModel() {
144 return buildTestModel("aaa", "111", "xyz|123");
147 private ModelArtifact buildTestModel(String id, String version) {
148 return buildTestModel(id, version, null);
151 private ModelArtifact buildTestModel(String id, String version, String dependentModel) {
152 ModelArtifact modelArtifact = new ModelArtifact();
153 modelArtifact.setModelInvariantId(id);
154 modelArtifact.setModelVerId(version);
155 if (dependentModel != null) {
156 modelArtifact.addDependentModelId(dependentModel);
158 return modelArtifact;
161 private NamedQueryArtifact buildTestNamedQuery(String uuid, String modelId) {
162 NamedQueryArtifact nq = new NamedQueryArtifact();
163 nq.setNamedQueryUuid(uuid);
164 nq.addDependentModelId(modelId);