Fix minor bug in FRM proactive flow code path
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / parser / util / ModuleDependencySortTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.yang.parser.util;
9
10 import static org.hamcrest.core.AnyOf.anyOf;
11 import static org.hamcrest.core.Is.is;
12 import static org.junit.Assert.assertThat;
13 import static org.junit.matchers.JUnitMatchers.containsString;
14 import static org.mockito.Mockito.*;
15
16 import java.util.Arrays;
17 import java.util.Date;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Map.Entry;
21 import java.util.Set;
22
23 import org.hamcrest.Matcher;
24 import org.junit.Test;
25 import org.opendaylight.controller.yang.model.api.Module;
26 import org.opendaylight.controller.yang.model.api.ModuleImport;
27 import org.opendaylight.controller.yang.parser.builder.impl.ModuleBuilder;
28 import org.opendaylight.controller.yang.parser.impl.YangParserListenerImpl;
29 import org.opendaylight.controller.yang.parser.util.ModuleDependencySort.ModuleNodeImpl;
30 import org.opendaylight.controller.yang.parser.util.TopologicalSort.Edge;
31
32 import com.google.common.collect.Sets;
33
34 public class ModuleDependencySortTest {
35
36     private ModuleBuilder a = mockModuleBuilder("a", null);
37     private ModuleBuilder b = mockModuleBuilder("b", null);
38     private ModuleBuilder c = mockModuleBuilder("c", null);
39     private ModuleBuilder d = mockModuleBuilder("d", null);
40
41     @Test
42     public void testValid() throws Exception {
43
44         mockDependency(a, b);
45         mockDependency(b, c);
46         mockDependency(b, d);
47
48         ModuleBuilder[] builders = new ModuleBuilder[] { d, b, c, a };
49
50         List<ModuleBuilder> l = ModuleDependencySort.sort(builders);
51
52         assertDependencyGraph(ModuleDependencySort.createModuleGraph(Arrays.asList(builders)));
53
54         @SuppressWarnings("unchecked")
55         Matcher<String> cOrD = anyOf(is(c.getName()), is(d.getName()));
56
57         assertThat(l.get(0).getName(), cOrD);
58         assertThat(l.get(1).getName(), cOrD);
59         assertThat(l.get(2).getName(), is(b.getName()));
60         assertThat(l.get(3).getName(), is(a.getName()));
61     }
62
63     @Test
64     public void testValidModule() throws Exception {
65
66         Date rev = new Date();
67         Module a = mockModule("a", rev);
68         Module b = mockModule("b", rev);
69         Module c = mockModule("c", rev);
70
71         mockDependency(a, b);
72         mockDependency(b, c);
73         mockDependency(a, c);
74
75         Module[] builders = new Module[] { a, b, c };
76
77         List<Module> l = ModuleDependencySort.sort(builders);
78
79         assertThat(l.get(0).getName(), is(c.getName()));
80         assertThat(l.get(1).getName(), is(b.getName()));
81         assertThat(l.get(2).getName(), is(a.getName()));
82     }
83
84     @Test(expected = YangValidationException.class)
85     public void testModuleTwice() throws Exception {
86         ModuleBuilder a2 = mockModuleBuilder("a", null);
87
88         ModuleBuilder[] builders = new ModuleBuilder[] { a, a2 };
89         try {
90             ModuleDependencySort.sort(builders);
91         } catch (YangValidationException e) {
92             assertThat(e.getMessage(), containsString("Module:a with revision:default declared twice"));
93             throw e;
94         }
95     }
96
97     @Test(expected = YangValidationException.class)
98     public void testImportNotExistingModule() throws Exception {
99         mockDependency(a, b);
100
101         ModuleBuilder[] builders = new ModuleBuilder[] { a };
102         try {
103             ModuleDependencySort.sort(builders);
104         } catch (YangValidationException e) {
105             assertThat(e.getMessage(), containsString("Not existing module imported:b:default by:a:default"));
106             throw e;
107         }
108     }
109
110     @Test
111     public void testImportTwice() throws Exception {
112         mockDependency(a, b);
113         mockDependency(c, b);
114
115         ModuleBuilder[] builders = new ModuleBuilder[] { a, b, c };
116         ModuleDependencySort.sort(builders);
117     }
118
119     @Test(expected = YangValidationException.class)
120     public void testImportTwiceDifferentRevision() throws Exception {
121         Date date1 = new Date(463846463486L);
122         Date date2 = new Date(364896446683L);
123         b = mockModuleBuilder("b", date1);
124         ModuleBuilder b2 = mockModuleBuilder("b", date2);
125
126         mockDependency(a, b);
127         mockDependency(c, b2);
128
129         ModuleBuilder[] builders = new ModuleBuilder[] { a, c, b, b2 };
130         try {
131             ModuleDependencySort.sort(builders);
132         } catch (YangValidationException e) {
133             assertThat(e.getMessage(), containsString("Module:b imported twice with different revisions:"
134                     + YangParserListenerImpl.simpleDateFormat.format(date1) + ", "
135                     + YangParserListenerImpl.simpleDateFormat.format(date2)));
136             throw e;
137         }
138     }
139
140     @Test
141     public void testModuleTwiceWithDifferentRevs() throws Exception {
142         ModuleBuilder a2 = mockModuleBuilder("a", new Date());
143
144         ModuleBuilder[] builders = new ModuleBuilder[] { a, a2 };
145         ModuleDependencySort.sort(builders);
146     }
147
148     @Test(expected = YangValidationException.class)
149     public void testModuleTwice2() throws Exception {
150         Date rev = new Date();
151         ModuleBuilder a2 = mockModuleBuilder("a", rev);
152         ModuleBuilder a3 = mockModuleBuilder("a", rev);
153
154         ModuleBuilder[] builders = new ModuleBuilder[] { a, a2, a3 };
155         try {
156             ModuleDependencySort.sort(builders);
157         } catch (YangValidationException e) {
158             assertThat(e.getMessage(), containsString("Module:a with revision:"
159                     + YangParserListenerImpl.simpleDateFormat.format(rev) + " declared twice"));
160             throw e;
161         }
162     }
163
164     private void assertDependencyGraph(Map<String, Map<Date, ModuleNodeImpl>> moduleGraph) {
165         for (Entry<String, Map<Date, ModuleNodeImpl>> node : moduleGraph.entrySet()) {
166             String name = node.getKey();
167
168             // Expects only one module revision
169
170             Set<Edge> inEdges = node.getValue().values().iterator().next().getInEdges();
171             Set<Edge> outEdges = node.getValue().values().iterator().next().getOutEdges();
172
173             if (name.equals("a")) {
174                 assertEdgeCount(inEdges, 0, outEdges, 1);
175             } else if (name.equals("b")) {
176                 assertEdgeCount(inEdges, 1, outEdges, 2);
177             } else {
178                 assertEdgeCount(inEdges, 1, outEdges, 0);
179             }
180         }
181     }
182
183     private void assertEdgeCount(Set<Edge> inEdges, int i, Set<Edge> outEdges, int j) {
184         assertThat(inEdges.size(), is(i));
185         assertThat(outEdges.size(), is(j));
186     }
187
188     private void mockDependency(ModuleBuilder a, ModuleBuilder b) {
189         ModuleImport imprt = mock(ModuleImport.class);
190         doReturn(b.getName()).when(imprt).getModuleName();
191         doReturn(b.getRevision()).when(imprt).getRevision();
192         a.getModuleImports().add(imprt);
193     }
194
195     private void mockDependency(Module a, Module b) {
196         ModuleImport imprt = mock(ModuleImport.class);
197         doReturn(b.getName()).when(imprt).getModuleName();
198         doReturn(b.getRevision()).when(imprt).getRevision();
199         a.getImports().add(imprt);
200     }
201
202     private ModuleBuilder mockModuleBuilder(String name, Date rev) {
203         ModuleBuilder a = mock(ModuleBuilder.class);
204         doReturn(name).when(a).getName();
205         Set<ModuleImport> set = Sets.newHashSet();
206         doReturn(set).when(a).getModuleImports();
207         if (rev != null) {
208             doReturn(rev).when(a).getRevision();
209         }
210         return a;
211     }
212
213     private Module mockModule(String name, Date rev) {
214         Module a = mock(Module.class);
215         doReturn(name).when(a).getName();
216         Set<ModuleImport> set = Sets.newHashSet();
217         doReturn(set).when(a).getImports();
218         if (rev != null) {
219             doReturn(rev).when(a).getRevision();
220         }
221         return a;
222     }
223 }