Update model-loader to spring-boot 2.4
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / entity / model / ModelSorterTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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  */
21 package org.onap.aai.modelloader.entity.model;
22
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;
30
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35
36 import org.junit.jupiter.api.Test;
37 import org.onap.aai.modelloader.entity.Artifact;
38
39 public class ModelSorterTest {
40
41     @Test
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);
46
47         ModelSorter.Edge edgeA = new ModelSorter.Edge(nodeA, nodeB);
48
49         assertThat(edgeA, is(equalTo(edgeA)));
50         assertThat(edgeA, is(not(equalTo(null))));
51         assertThat(edgeA, is(not(equalTo(model))));
52
53         ModelSorter.Edge edgeB = new ModelSorter.Edge(nodeA, nodeB);
54         ModelSorter.Edge edgeC = new ModelSorter.Edge(nodeB, nodeA);
55
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))));
61     }
62
63     @Test
64     public void nodeEquality() throws BabelArtifactParsingException {
65         ModelArtifact model = buildTestModel();
66         ModelSorter.Node nodeA = new ModelSorter.Node(model);
67
68         assertThat(nodeA, is(equalTo(nodeA)));
69         assertThat(nodeA, is(not(equalTo(null))));
70         assertThat(nodeA, is(not(equalTo(model))));
71
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())))));
76     }
77
78     @Test
79     public void testToString() throws BabelArtifactParsingException {
80         ModelArtifact model = buildTestModel();
81
82         ModelSorter.Node nodeA = new ModelSorter.Node(model);
83         assertThat(nodeA.toString(), not(isEmptyString()));
84
85         ModelSorter.Node nodeB = new ModelSorter.Node(model);
86         nodeA.addEdge(nodeB);
87         assertThat(nodeA.toString(), not(isEmptyString()));
88         assertThat(nodeB.toString(), not(isEmptyString()));
89
90     }
91
92     @Test
93     public void noModels() throws BabelArtifactParsingException {
94         assertThat(new ModelSorter().sort(null), is(nullValue()));
95         assertThat(new ModelSorter().sort(Collections.emptyList()).size(), is(0));
96     }
97
98     @Test
99     public void singleModel() throws BabelArtifactParsingException {
100         assertThat(new ModelSorter().sort(Arrays.asList(buildTestModel())).size(), is(1));
101     }
102
103     @Test
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));
110     }
111
112
113     @Test
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));
122     }
123
124     @Test
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));
131     }
132
133     @Test
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);
140         });
141     }
142
143     private ModelArtifact buildTestModel() {
144         return buildTestModel("aaa", "111", "xyz|123");
145     }
146
147     private ModelArtifact buildTestModel(String id, String version) {
148         return buildTestModel(id, version, null);
149     }
150
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);
157         }
158         return modelArtifact;
159     }
160
161     private NamedQueryArtifact buildTestNamedQuery(String uuid, String modelId) {
162         NamedQueryArtifact nq = new NamedQueryArtifact();
163         nq.setNamedQueryUuid(uuid);
164         nq.addDependentModelId(modelId);
165         return nq;
166     }
167
168 }