8f4bf8df6c260bec3f6b31362e67600a5931342a
[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.Matchers.isEmptyString;
28 import static org.junit.Assert.assertThat;
29
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;
36
37 public class ModelSorterTest {
38
39     @Test
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);
44
45         ModelSorter.Edge edgeA = new ModelSorter.Edge(nodeA, nodeB);
46
47         assertThat(edgeA, is(equalTo(edgeA)));
48         assertThat(edgeA, is(not(equalTo(null))));
49         assertThat(edgeA, is(not(equalTo(model))));
50
51         ModelSorter.Edge edgeB = new ModelSorter.Edge(nodeA, nodeB);
52         ModelSorter.Edge edgeC = new ModelSorter.Edge(nodeB, nodeA);
53
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))));
59     }
60
61     @Test
62     public void nodeEquality() throws BabelArtifactParsingException {
63         ModelArtifact model = buildTestModel();
64         ModelSorter.Node nodeA = new ModelSorter.Node(model);
65
66         assertThat(nodeA, is(equalTo(nodeA)));
67         assertThat(nodeA, is(not(equalTo(null))));
68         assertThat(nodeA, is(not(equalTo(model))));
69
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())))));
74     }
75
76     @Test
77     public void testToString() throws BabelArtifactParsingException {
78         ModelArtifact model = buildTestModel();
79
80         ModelSorter.Node nodeA = new ModelSorter.Node(model);
81         assertThat(nodeA.toString(), not(isEmptyString()));
82
83         ModelSorter.Node nodeB = new ModelSorter.Node(model);
84         nodeA.addEdge(nodeB);
85         assertThat(nodeA.toString(), not(isEmptyString()));
86         assertThat(nodeB.toString(), not(isEmptyString()));
87
88     }
89
90     @Test
91     public void noModels() throws BabelArtifactParsingException {
92         assertThat(new ModelSorter().sort(null), is(nullValue()));
93         assertThat(new ModelSorter().sort(Collections.emptyList()).size(), is(0));
94     }
95
96     @Test
97     public void singleModel() throws BabelArtifactParsingException {
98         assertThat(new ModelSorter().sort(Arrays.asList(buildTestModel())).size(), is(1));
99     }
100
101     @Test
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));
108     }
109
110
111     @Test
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));
120     }
121
122     @Test
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));
129     }
130
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);
137     }
138
139     private ModelArtifact buildTestModel() {
140         return buildTestModel("aaa", "111", "xyz|123");
141     }
142
143     private ModelArtifact buildTestModel(String id, String version) {
144         return buildTestModel(id, version, null);
145     }
146
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);
153         }
154         return modelArtifact;
155     }
156
157     private NamedQueryArtifact buildTestNamedQuery(String uuid, String modelId) {
158         NamedQueryArtifact nq = new NamedQueryArtifact();
159         nq.setNamedQueryUuid(uuid);
160         nq.addDependentModelId(modelId);
161         return nq;
162     }
163
164 }