2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.yang.parser.util;
10 import static org.hamcrest.core.AnyOf.*;
11 import static org.hamcrest.core.Is.*;
12 import static org.junit.Assert.*;
13 import static org.junit.matchers.JUnitMatchers.*;
14 import static org.mockito.Mockito.*;
16 import java.util.Arrays;
17 import java.util.Date;
18 import java.util.List;
20 import java.util.Map.Entry;
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;
32 import com.google.common.collect.Sets;
34 public class ModuleDependencySortTest {
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);
42 public void testValid() throws Exception {
48 ModuleBuilder[] builders = new ModuleBuilder[] { d, b, c, a };
50 List<ModuleBuilder> l = ModuleDependencySort.sort(builders);
52 assertDependencyGraph(ModuleDependencySort.createModuleGraph(Arrays
55 @SuppressWarnings("unchecked")
56 Matcher<String> cOrD = anyOf(is(c.getName()), is(d.getName()));
58 assertThat(l.get(0).getName(), cOrD);
59 assertThat(l.get(1).getName(), cOrD);
60 assertThat(l.get(2).getName(), is(b.getName()));
61 assertThat(l.get(3).getName(), is(a.getName()));
65 public void testValidModule() throws Exception {
67 Date rev = new Date();
68 Module a = mockModule("a", rev);
69 Module b = mockModule("b", rev);
70 Module c = mockModule("c", rev);
76 Module[] builders = new Module[] { a, b, c };
78 List<Module> l = ModuleDependencySort.sort(builders);
80 assertThat(l.get(0).getName(), is(c.getName()));
81 assertThat(l.get(1).getName(), is(b.getName()));
82 assertThat(l.get(2).getName(), is(a.getName()));
85 @Test(expected = YangValidationException.class)
86 public void testModuleTwice() throws Exception {
87 ModuleBuilder a2 = mockModuleBuilder("a", null);
89 ModuleBuilder[] builders = new ModuleBuilder[] { a, a2 };
91 ModuleDependencySort.sort(builders);
92 } catch (YangValidationException e) {
95 containsString("Module:a with revision:default declared twice"));
100 @Test(expected = YangValidationException.class)
101 public void testImportNotExistingModule() throws Exception {
102 mockDependency(a, b);
104 ModuleBuilder[] builders = new ModuleBuilder[] { a };
106 ModuleDependencySort.sort(builders);
107 } catch (YangValidationException e) {
110 containsString("Not existing module imported:b:default by:a:default"));
116 public void testImportTwice() throws Exception {
117 mockDependency(a, b);
118 mockDependency(c, b);
120 ModuleBuilder[] builders = new ModuleBuilder[] { a, b, c };
121 ModuleDependencySort.sort(builders);
124 @Test(expected = YangValidationException.class)
125 public void testImportTwiceDifferentRevision() throws Exception {
126 Date date = new Date();
127 ModuleBuilder b2 = mockModuleBuilder("b", date);
129 mockDependency(a, b);
130 mockDependency(c, b2);
132 ModuleBuilder[] builders = new ModuleBuilder[] { a, c, b, b2 };
134 ModuleDependencySort.sort(builders);
135 } catch (YangValidationException e) {
138 containsString("Module:b imported twice with different revisions:default, "
139 + YangParserListenerImpl.simpleDateFormat
146 public void testModuleTwiceWithDifferentRevs() throws Exception {
147 ModuleBuilder a2 = mockModuleBuilder("a", new Date());
149 ModuleBuilder[] builders = new ModuleBuilder[] { a, a2 };
150 ModuleDependencySort.sort(builders);
153 @Test(expected = YangValidationException.class)
154 public void testModuleTwice2() throws Exception {
155 Date rev = new Date();
156 ModuleBuilder a2 = mockModuleBuilder("a", rev);
157 ModuleBuilder a3 = mockModuleBuilder("a", rev);
159 ModuleBuilder[] builders = new ModuleBuilder[] { a, a2, a3 };
161 ModuleDependencySort.sort(builders);
162 } catch (YangValidationException e) {
163 assertThat(e.getMessage(), containsString("Module:a with revision:"
164 + YangParserListenerImpl.simpleDateFormat.format(rev)
165 + " declared twice"));
170 private void assertDependencyGraph(
171 Map<String, Map<Date, ModuleNodeImpl>> moduleGraph) {
172 for (Entry<String, Map<Date, ModuleNodeImpl>> node : moduleGraph
174 String name = node.getKey();
176 // Expects only one module revision
178 Set<Edge> inEdges = node.getValue().values().iterator().next()
180 Set<Edge> outEdges = node.getValue().values().iterator().next()
183 if (name.equals("a")) {
184 assertEdgeCount(inEdges, 0, outEdges, 1);
185 } else if (name.equals("b")) {
186 assertEdgeCount(inEdges, 1, outEdges, 2);
188 assertEdgeCount(inEdges, 1, outEdges, 0);
193 private void assertEdgeCount(Set<Edge> inEdges, int i, Set<Edge> outEdges,
195 assertThat(inEdges.size(), is(i));
196 assertThat(outEdges.size(), is(j));
199 private void mockDependency(ModuleBuilder a, ModuleBuilder b) {
200 ModuleImport imprt = mock(ModuleImport.class);
201 doReturn(b.getName()).when(imprt).getModuleName();
202 doReturn(b.getRevision()).when(imprt).getRevision();
203 a.getModuleImports().add(imprt);
206 private void mockDependency(Module a, Module b) {
207 ModuleImport imprt = mock(ModuleImport.class);
208 doReturn(b.getName()).when(imprt).getModuleName();
209 doReturn(b.getRevision()).when(imprt).getRevision();
210 a.getImports().add(imprt);
213 private ModuleBuilder mockModuleBuilder(String name, Date rev) {
214 ModuleBuilder a = mock(ModuleBuilder.class);
215 doReturn(name).when(a).getName();
216 Set<ModuleImport> set = Sets.newHashSet();
217 doReturn(set).when(a).getModuleImports();
219 doReturn(rev).when(a).getRevision();
224 private Module mockModule(String name, Date rev) {
225 Module a = mock(Module.class);
226 doReturn(name).when(a).getName();
227 Set<ModuleImport> set = Sets.newHashSet();
228 doReturn(set).when(a).getImports();
230 doReturn(rev).when(a).getRevision();