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.junit.Assert.assertThat;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.List;
33 import org.junit.Test;
34 import org.onap.aai.modelloader.entity.Artifact;
36 public class ModelSorterTest {
39 public void edgeEquality() throws BabelArtifactParsingException {
40 ModelArtifact model = buildTestModel();
41 ModelSorter.Node nodeA = new ModelSorter.Node(model);
42 ModelSorter.Node nodeB = new ModelSorter.Node(model);
43 ModelSorter.Node nodeC = new ModelSorter.Node(model);
45 ModelSorter.Edge edgeA = new ModelSorter.Edge(nodeA, nodeB);
46 ModelSorter.Edge edgeB = new ModelSorter.Edge(nodeA, nodeB);
47 ModelSorter.Edge edgeC = new ModelSorter.Edge(nodeB, nodeA);
48 ModelSorter.Edge edgeD = new ModelSorter.Edge(nodeA, nodeC);
50 assertThat(edgeA, is(equalTo(edgeA)));
51 assertThat(edgeA, is(not(equalTo(null))));
52 assertThat(edgeA, is(not(equalTo(model))));
54 assertThat(edgeA, is(equalTo(edgeB)));
55 assertThat(edgeA, is(not(equalTo(edgeC))));
56 assertThat(edgeA, is(not(equalTo(edgeD))));
60 public void nodeEquality() throws BabelArtifactParsingException {
61 ModelArtifact model = buildTestModel();
62 ModelSorter.Node nodeA = new ModelSorter.Node(model);
63 ModelSorter.Node nodeB = new ModelSorter.Node(model);
65 assertThat(nodeA, is(equalTo(nodeA)));
66 assertThat(nodeA, is(not(equalTo(null))));
67 assertThat(nodeA, is(not(equalTo(model))));
69 assertThat(nodeA, is(equalTo(nodeB)));
70 assertThat(nodeA.toString(), is(equalTo(nodeB.toString())));
71 assertThat(nodeA, is(not(equalTo(new ModelSorter.Node(new ModelArtifact())))));
75 public void noModels() throws BabelArtifactParsingException {
76 assertThat(new ModelSorter().sort(null), is(nullValue()));
77 assertThat(new ModelSorter().sort(Collections.emptyList()).size(), is(0));
81 public void singleModel() throws BabelArtifactParsingException {
82 assertThat(new ModelSorter().sort(Arrays.asList(buildTestModel())).size(), is(1));
86 public void multipleModels() throws BabelArtifactParsingException {
87 Artifact a = buildTestModel("aaaa", "mvaaaa", "cccc|mvcccc");
88 Artifact b = buildTestModel("bbbb", "mvbbbb", "aaaa|mvaaaa");
89 Artifact c = buildTestModel("cccc", "mvcccc");
90 List<Artifact> expected = Arrays.asList(c, a, b);
91 assertThat(new ModelSorter().sort(Arrays.asList(a, b, c)), is(expected));
95 public void multipleModelsAndNamedQueries() throws BabelArtifactParsingException {
96 Artifact a = buildTestModel("aaaa", "1111", "cccc|2222");
97 Artifact nq1 = buildTestNamedQuery("nq1", "aaaa|1111");
98 Artifact nq2 = buildTestNamedQuery("nqw", "existing-model");
99 List<Artifact> expected = Arrays.asList(a, nq2, nq1);
100 assertThat(new ModelSorter().sort(Arrays.asList(nq1, nq2, a)), is(expected));
103 @Test(expected = BabelArtifactParsingException.class)
104 public void circularDependency() throws BabelArtifactParsingException {
105 List<Artifact> modelList = new ArrayList<Artifact>();
106 modelList.add(buildTestModel("aaaa", "1111", "bbbb|1111"));
107 modelList.add(buildTestModel("bbbb", "1111", "aaaa|1111"));
108 new ModelSorter().sort(modelList);
111 private ModelArtifact buildTestModel() {
112 return buildTestModel("aaa", "111", "xyz|123");
115 private ModelArtifact buildTestModel(String id, String version) {
116 return buildTestModel(id, version, null);
119 private ModelArtifact buildTestModel(String id, String version, String dependentModel) {
120 ModelArtifact modelArtifact = new ModelArtifact();
121 modelArtifact.setModelInvariantId(id);
122 modelArtifact.setModelVerId(version);
123 if (dependentModel != null) {
124 modelArtifact.addDependentModelId(dependentModel);
126 return modelArtifact;
129 private NamedQueryArtifact buildTestNamedQuery(String uuid, String modelId) {
130 NamedQueryArtifact nq = new NamedQueryArtifact();
131 nq.setNamedQueryUuid(uuid);
132 nq.addDependentModelId(modelId);