dea537f73d835ad34f651e7980ea1e19598d5746
[aai/model-loader.git] / src / test / java / org / openecomp / modelloader / entity / model / ModelSorterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * MODEL LOADER SERVICE
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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
21 package org.openecomp.modelloader.entity.model;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29
30 import org.junit.Test;
31 import org.openecomp.modelloader.entity.Artifact;
32
33 public class ModelSorterTest {
34
35   @Test
36   public void noModels() {
37
38     List<Artifact> emptyList = Collections.emptyList();
39
40     ModelSorter sorter = new ModelSorter();
41     sorter = new ModelSorter();
42
43     List<Artifact> sortedList = sorter.sort(emptyList);
44     assertNotNull(sortedList);
45     assertEquals(0, sortedList.size());
46
47   }
48
49   @Test
50   public void singleModel() {
51
52     List<Artifact> modelList = new ArrayList<Artifact>();
53
54     ModelArtifact model = new ModelArtifact();
55     model.setNameVersionId("aaaaa");
56     model.addDependentModelId("xyz");
57     modelList.add(model);
58
59     ModelSorter sorter = new ModelSorter();
60     sorter = new ModelSorter();
61
62     List<Artifact> sortedList = sorter.sort(modelList);
63     assertNotNull(sortedList);
64     assertEquals(1, sortedList.size());
65
66   }
67
68   /**
69    * 
70    * depends on depends on B ------> A -------> C
71    *
72    *
73    * Input list = a, b, c Sorted list = c, a, b
74    *
75    */
76   @Test
77   public void multipleModels() {
78
79     List<Artifact> modelList = new ArrayList<Artifact>();
80
81     ModelArtifact aaaa = new ModelArtifact();
82     aaaa.setNameVersionId("aaaa");
83     aaaa.addDependentModelId("cccc");
84
85     ModelArtifact bbbb = new ModelArtifact();
86     bbbb.setNameVersionId("bbbb");
87     bbbb.addDependentModelId("aaaa");
88
89     ModelArtifact cccc = new ModelArtifact();
90     cccc.setNameVersionId("cccc");
91
92     modelList.add(aaaa);
93     modelList.add(bbbb);
94     modelList.add(cccc);
95
96     ModelSorter sorter = new ModelSorter();
97     sorter = new ModelSorter();
98
99     List<Artifact> sortedList = sorter.sort(modelList);
100     assertNotNull(sortedList);
101     assertEquals(3, sortedList.size());
102
103     assertEquals(cccc, sortedList.get(0));
104     assertEquals(aaaa, sortedList.get(1));
105     assertEquals(bbbb, sortedList.get(2));
106   }
107
108   @Test(expected = RuntimeException.class)
109   public void circularDependency() {
110
111     List<Artifact> modelList = new ArrayList<Artifact>();
112
113     ModelArtifact aaaa = new ModelArtifact();
114     aaaa.setNameVersionId("aaaa");
115     aaaa.addDependentModelId("bbbb");
116
117     ModelArtifact bbbb = new ModelArtifact();
118     bbbb.setNameVersionId("bbbb");
119     bbbb.addDependentModelId("aaaa");
120
121     modelList.add(aaaa);
122     modelList.add(bbbb);
123
124     ModelSorter sorter = new ModelSorter();
125     sorter = new ModelSorter();
126
127     List<Artifact> sortedList = sorter.sort(modelList);
128     assertNotNull(sortedList);
129     assertEquals(3, sortedList.size());
130
131   }
132
133 }