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.Matchers.isEmptyString;
28 import static org.junit.Assert.assertThat;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.List;
34 import org.junit.Test;
35 import org.onap.aai.modelloader.entity.Artifact;
37 public class ModelSorterTest {
40 public void edgeEquality() throws BabelArtifactParsingException {
41 ModelArtifact model = buildTestModel();
42 ModelSorter.Node nodeA = new ModelSorter.Node(model);
43 ModelSorter.Node nodeB = new ModelSorter.Node(model);
45 ModelSorter.Edge edgeA = new ModelSorter.Edge(nodeA, nodeB);
47 assertThat(edgeA, is(equalTo(edgeA)));
48 assertThat(edgeA, is(not(equalTo(null))));
49 assertThat(edgeA, is(not(equalTo(model))));
51 ModelSorter.Edge edgeB = new ModelSorter.Edge(nodeA, nodeB);
52 ModelSorter.Edge edgeC = new ModelSorter.Edge(nodeB, nodeA);
54 ModelSorter.Node nodeC = new ModelSorter.Node(model);
55 ModelSorter.Edge edgeD = new ModelSorter.Edge(nodeA, nodeC);
56 assertThat(edgeA, is(equalTo(edgeB)));
57 assertThat(edgeA, is(not(equalTo(edgeC))));
58 assertThat(edgeA, is(not(equalTo(edgeD))));
62 public void nodeEquality() throws BabelArtifactParsingException {
63 ModelArtifact model = buildTestModel();
64 ModelSorter.Node nodeA = new ModelSorter.Node(model);
66 assertThat(nodeA, is(equalTo(nodeA)));
67 assertThat(nodeA, is(not(equalTo(null))));
68 assertThat(nodeA, is(not(equalTo(model))));
70 ModelSorter.Node nodeB = new ModelSorter.Node(model);
71 assertThat(nodeA, is(equalTo(nodeB)));
72 assertThat(nodeA.toString(), is(equalTo(nodeB.toString())));
73 assertThat(nodeA, is(not(equalTo(new ModelSorter.Node(new ModelArtifact())))));
77 public void testToString() throws BabelArtifactParsingException {
78 ModelArtifact model = buildTestModel();
80 ModelSorter.Node nodeA = new ModelSorter.Node(model);
81 assertThat(nodeA.toString(), not(isEmptyString()));
83 ModelSorter.Node nodeB = new ModelSorter.Node(model);
85 assertThat(nodeA.toString(), not(isEmptyString()));
86 assertThat(nodeB.toString(), not(isEmptyString()));
91 public void noModels() throws BabelArtifactParsingException {
92 assertThat(new ModelSorter().sort(null), is(nullValue()));
93 assertThat(new ModelSorter().sort(Collections.emptyList()).size(), is(0));
97 public void singleModel() throws BabelArtifactParsingException {
98 assertThat(new ModelSorter().sort(Arrays.asList(buildTestModel())).size(), is(1));
102 public void multipleModels() throws BabelArtifactParsingException {
103 Artifact artA = buildTestModel("aaaa", "mvaaaa", "cccc|mvcccc");
104 Artifact artB = buildTestModel("bbbb", "mvbbbb", "aaaa|mvaaaa");
105 Artifact artC = buildTestModel("cccc", "mvcccc");
106 List<Artifact> expected = Arrays.asList(artC, artA, artB);
107 assertThat(new ModelSorter().sort(Arrays.asList(artA, artB, artC)), is(expected));
112 public void multipleModelsWithMultipleIncomingEdges() throws BabelArtifactParsingException {
113 ModelArtifact artA = buildTestModel("aaaa", "mvaaaa", "cccc|mvcccc");
114 Artifact artB = buildTestModel("bbbb", "mvbbbb", "aaaa|mvaaaa");
115 Artifact artC = buildTestModel("cccc", "mvcccc");
116 Artifact artD = buildTestModel("dddd", "mvdddd", "cccc|mvcccc");
117 artA.addDependentModelId("dddd|mvdddd");
118 List<Artifact> expected = Arrays.asList(artC, artD, artA, artB);
119 assertThat(new ModelSorter().sort(Arrays.asList(artA, artB, artC, artD)), is(expected));
123 public void multipleModelsAndNamedQueries() throws BabelArtifactParsingException {
124 Artifact artifact = buildTestModel("aaaa", "1111", "cccc|2222");
125 Artifact nq1 = buildTestNamedQuery("nq1", "aaaa|1111");
126 Artifact nq2 = buildTestNamedQuery("nqw", "existing-model");
127 List<Artifact> expected = Arrays.asList(artifact, nq2, nq1);
128 assertThat(new ModelSorter().sort(Arrays.asList(nq1, nq2, artifact)), is(expected));
131 @Test(expected = BabelArtifactParsingException.class)
132 public void circularDependency() throws BabelArtifactParsingException {
133 List<Artifact> modelList = new ArrayList<Artifact>();
134 modelList.add(buildTestModel("aaaa", "1111", "bbbb|1111"));
135 modelList.add(buildTestModel("bbbb", "1111", "aaaa|1111"));
136 new ModelSorter().sort(modelList);
139 private ModelArtifact buildTestModel() {
140 return buildTestModel("aaa", "111", "xyz|123");
143 private ModelArtifact buildTestModel(String id, String version) {
144 return buildTestModel(id, version, null);
147 private ModelArtifact buildTestModel(String id, String version, String dependentModel) {
148 ModelArtifact modelArtifact = new ModelArtifact();
149 modelArtifact.setModelInvariantId(id);
150 modelArtifact.setModelVerId(version);
151 if (dependentModel != null) {
152 modelArtifact.addDependentModelId(dependentModel);
154 return modelArtifact;
157 private NamedQueryArtifact buildTestNamedQuery(String uuid, String modelId) {
158 NamedQueryArtifact nq = new NamedQueryArtifact();
159 nq.setNamedQueryUuid(uuid);
160 nq.addDependentModelId(modelId);