[AAI-3] Remove dependency on SDC
[aai/model-loader.git] / src / test / java / org / openecomp / modelloader / entity / model / ModelSorterTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Model Loader
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  *
20  * ECOMP and OpenECOMP are trademarks
21  * and service marks of AT&T Intellectual Property.
22  */
23 package org.openecomp.modelloader.entity.model;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31
32 import org.junit.Test;
33 import org.openecomp.modelloader.entity.Artifact;
34
35 public class ModelSorterTest {
36
37   @Test
38   public void noModels() {
39
40     List<Artifact> emptyList = Collections.emptyList();
41
42     ModelSorter sorter = new ModelSorter();
43     sorter = new ModelSorter();
44
45     List<Artifact> sortedList = sorter.sort(emptyList);
46     assertNotNull(sortedList);
47     assertEquals(0, sortedList.size());
48
49   }
50
51   @Test
52   public void singleModel() {
53
54     List<Artifact> modelList = new ArrayList<Artifact>();
55
56     ModelArtifact model = new ModelArtifact();
57     model.setNameVersionId("aaaaa");
58     model.addDependentModelId("xyz");
59     modelList.add(model);
60
61     ModelSorter sorter = new ModelSorter();
62     sorter = new ModelSorter();
63
64     List<Artifact> sortedList = sorter.sort(modelList);
65     assertNotNull(sortedList);
66     assertEquals(1, sortedList.size());
67
68   }
69
70   /**
71    * 
72    * depends on depends on B ------> A -------> C
73    *
74    *
75    * Input list = a, b, c Sorted list = c, a, b
76    *
77    */
78   @Test
79   public void multipleModels() {
80
81     List<Artifact> modelList = new ArrayList<Artifact>();
82
83     ModelArtifact aaaa = new ModelArtifact();
84     aaaa.setModelInvariantId("aaaa");
85         aaaa.setModelVerId("mvaaaa");
86         aaaa.addDependentModelId("cccc|mvcccc");
87
88     ModelArtifact bbbb = new ModelArtifact();
89     bbbb.setModelInvariantId("bbbb");
90         bbbb.setModelVerId("mvbbbb");
91         bbbb.addDependentModelId("aaaa|mvaaaa");
92
93     ModelArtifact cccc = new ModelArtifact();
94     cccc.setModelInvariantId("cccc");
95         cccc.setModelVerId("mvcccc");
96
97     modelList.add(aaaa);
98     modelList.add(bbbb);
99     modelList.add(cccc);
100
101     ModelSorter sorter = new ModelSorter();
102     sorter = new ModelSorter();
103
104     List<Artifact> sortedList = sorter.sort(modelList);
105     assertNotNull(sortedList);
106     assertEquals(3, sortedList.size());
107
108     assertEquals(cccc, sortedList.get(0));
109     assertEquals(aaaa, sortedList.get(1));
110     assertEquals(bbbb, sortedList.get(2));
111   }
112
113   @Test(expected = RuntimeException.class)
114   public void circularDependency() {
115
116     List<Artifact> modelList = new ArrayList<Artifact>();
117
118     ModelArtifact aaaa = new ModelArtifact();
119     aaaa.setNameVersionId("aaaa");
120     aaaa.addDependentModelId("bbbb");
121
122     ModelArtifact bbbb = new ModelArtifact();
123     bbbb.setNameVersionId("bbbb");
124     bbbb.addDependentModelId("aaaa");
125
126     modelList.add(aaaa);
127     modelList.add(bbbb);
128
129     ModelSorter sorter = new ModelSorter();
130     sorter = new ModelSorter();
131
132     List<Artifact> sortedList = sorter.sort(modelList);
133     assertNotNull(sortedList);
134     assertEquals(2, sortedList.size());
135
136   }
137
138 }