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;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.List;
35 import org.junit.Test;
36 import org.onap.aai.modelloader.entity.Artifact;
38 public class ModelSorterTest {
41 public void edgeEquality() throws BabelArtifactParsingException {
42 ModelArtifact model = buildTestModel();
43 ModelSorter.Node nodeA = new ModelSorter.Node(model);
44 ModelSorter.Node nodeB = new ModelSorter.Node(model);
46 ModelSorter.Edge edgeA = new ModelSorter.Edge(nodeA, nodeB);
48 assertThat(edgeA, is(equalTo(edgeA)));
49 assertThat(edgeA, is(not(equalTo(null))));
50 assertThat(edgeA, is(not(equalTo(model))));
52 ModelSorter.Edge edgeB = new ModelSorter.Edge(nodeA, nodeB);
53 ModelSorter.Edge edgeC = new ModelSorter.Edge(nodeB, nodeA);
55 ModelSorter.Node nodeC = new ModelSorter.Node(model);
56 ModelSorter.Edge edgeD = new ModelSorter.Edge(nodeA, nodeC);
57 assertThat(edgeA, is(equalTo(edgeB)));
58 assertThat(edgeA, is(not(equalTo(edgeC))));
59 assertThat(edgeA, is(not(equalTo(edgeD))));
63 public void nodeEquality() throws BabelArtifactParsingException {
64 ModelArtifact model = buildTestModel();
65 ModelSorter.Node nodeA = new ModelSorter.Node(model);
67 assertThat(nodeA, is(equalTo(nodeA)));
68 assertThat(nodeA, is(not(equalTo(null))));
69 assertThat(nodeA, is(not(equalTo(model))));
71 ModelSorter.Node nodeB = new ModelSorter.Node(model);
72 assertThat(nodeA, is(equalTo(nodeB)));
73 assertThat(nodeA.toString(), is(equalTo(nodeB.toString())));
74 assertThat(nodeA, is(not(equalTo(new ModelSorter.Node(new ModelArtifact())))));
78 public void testToString() throws BabelArtifactParsingException {
79 ModelArtifact model = buildTestModel();
81 ModelSorter.Node nodeA = new ModelSorter.Node(model);
82 assertThat(nodeA.toString(), not(isEmptyString()));
84 ModelSorter.Node nodeB = new ModelSorter.Node(model);
86 assertThat(nodeA.toString(), not(isEmptyString()));
87 assertThat(nodeB.toString(), not(isEmptyString()));
92 public void noModels() throws BabelArtifactParsingException {
93 assertThat(new ModelSorter().sort(null), is(nullValue()));
94 assertThat(new ModelSorter().sort(Collections.emptyList()).size(), is(0));
98 public void singleModel() throws BabelArtifactParsingException {
99 assertThat(new ModelSorter().sort(Arrays.asList(buildTestModel())).size(), is(1));
103 public void multipleModels() throws BabelArtifactParsingException {
104 Artifact artA = buildTestModel("aaaa", "mvaaaa", "cccc|mvcccc");
105 Artifact artB = buildTestModel("bbbb", "mvbbbb", "aaaa|mvaaaa");
106 Artifact artC = buildTestModel("cccc", "mvcccc");
107 List<Artifact> expected = Arrays.asList(artC, artA, artB);
108 assertThat(new ModelSorter().sort(Arrays.asList(artA, artB, artC)), is(expected));
113 public void multipleModelsWithMultipleIncomingEdges() throws BabelArtifactParsingException {
114 ModelArtifact artA = buildTestModel("aaaa", "mvaaaa", "cccc|mvcccc");
115 Artifact artB = buildTestModel("bbbb", "mvbbbb", "aaaa|mvaaaa");
116 Artifact artC = buildTestModel("cccc", "mvcccc");
117 Artifact artD = buildTestModel("dddd", "mvdddd", "cccc|mvcccc");
118 artA.addDependentModelId("dddd|mvdddd");
119 List<Artifact> expected = Arrays.asList(artC, artD, artA, artB);
120 assertThat(new ModelSorter().sort(Arrays.asList(artA, artB, artC, artD)), is(expected));
124 public void multipleModelsAndNamedQueries() throws BabelArtifactParsingException {
125 Artifact artifact = buildTestModel("aaaa", "1111", "cccc|2222");
126 Artifact nq1 = buildTestNamedQuery("nq1", "aaaa|1111");
127 Artifact nq2 = buildTestNamedQuery("nqw", "existing-model");
128 List<Artifact> expected = Arrays.asList(artifact, nq2, nq1);
129 assertThat(new ModelSorter().sort(Arrays.asList(nq1, nq2, artifact)), is(expected));
132 @Test(expected = BabelArtifactParsingException.class)
133 public void circularDependency() throws BabelArtifactParsingException {
134 List<Artifact> modelList = new ArrayList<Artifact>();
135 modelList.add(buildTestModel("aaaa", "1111", "bbbb|1111"));
136 modelList.add(buildTestModel("bbbb", "1111", "aaaa|1111"));
137 new ModelSorter().sort(modelList);
140 private ModelArtifact buildTestModel() {
141 return buildTestModel("aaa", "111", "xyz|123");
144 private ModelArtifact buildTestModel(String id, String version) {
145 return buildTestModel(id, version, null);
148 private ModelArtifact buildTestModel(String id, String version, String dependentModel) {
149 ModelArtifact modelArtifact = new ModelArtifact();
150 modelArtifact.setModelInvariantId(id);
151 modelArtifact.setModelVerId(version);
152 if (dependentModel != null) {
153 modelArtifact.addDependentModelId(dependentModel);
155 return modelArtifact;
158 private NamedQueryArtifact buildTestNamedQuery(String uuid, String modelId) {
159 NamedQueryArtifact nq = new NamedQueryArtifact();
160 nq.setNamedQueryUuid(uuid);
161 nq.addDependentModelId(modelId);