Improve error logging
[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
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.List;
34
35 import org.junit.Test;
36 import org.onap.aai.modelloader.entity.Artifact;
37
38 public class ModelSorterTest {
39
40     @Test
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);
45
46         ModelSorter.Edge edgeA = new ModelSorter.Edge(nodeA, nodeB);
47
48         assertThat(edgeA, is(equalTo(edgeA)));
49         assertThat(edgeA, is(not(equalTo(null))));
50         assertThat(edgeA, is(not(equalTo(model))));
51
52         ModelSorter.Edge edgeB = new ModelSorter.Edge(nodeA, nodeB);
53         ModelSorter.Edge edgeC = new ModelSorter.Edge(nodeB, nodeA);
54
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))));
60     }
61
62     @Test
63     public void nodeEquality() throws BabelArtifactParsingException {
64         ModelArtifact model = buildTestModel();
65         ModelSorter.Node nodeA = new ModelSorter.Node(model);
66
67         assertThat(nodeA, is(equalTo(nodeA)));
68         assertThat(nodeA, is(not(equalTo(null))));
69         assertThat(nodeA, is(not(equalTo(model))));
70
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())))));
75     }
76
77     @Test
78     public void testToString() throws BabelArtifactParsingException {
79         ModelArtifact model = buildTestModel();
80
81         ModelSorter.Node nodeA = new ModelSorter.Node(model);
82         assertThat(nodeA.toString(), not(isEmptyString()));
83
84         ModelSorter.Node nodeB = new ModelSorter.Node(model);
85         nodeA.addEdge(nodeB);
86         assertThat(nodeA.toString(), not(isEmptyString()));
87         assertThat(nodeB.toString(), not(isEmptyString()));
88
89     }
90
91     @Test
92     public void noModels() throws BabelArtifactParsingException {
93         assertThat(new ModelSorter().sort(null), is(nullValue()));
94         assertThat(new ModelSorter().sort(Collections.emptyList()).size(), is(0));
95     }
96
97     @Test
98     public void singleModel() throws BabelArtifactParsingException {
99         assertThat(new ModelSorter().sort(Arrays.asList(buildTestModel())).size(), is(1));
100     }
101
102     @Test
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));
109     }
110
111
112     @Test
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));
121     }
122
123     @Test
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));
130     }
131
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);
138     }
139
140     private ModelArtifact buildTestModel() {
141         return buildTestModel("aaa", "111", "xyz|123");
142     }
143
144     private ModelArtifact buildTestModel(String id, String version) {
145         return buildTestModel(id, version, null);
146     }
147
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);
154         }
155         return modelArtifact;
156     }
157
158     private NamedQueryArtifact buildTestNamedQuery(String uuid, String modelId) {
159         NamedQueryArtifact nq = new NamedQueryArtifact();
160         nq.setNamedQueryUuid(uuid);
161         nq.addDependentModelId(modelId);
162         return nq;
163     }
164
165 }