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.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.*;
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.asList(builders)));
54 @SuppressWarnings("unchecked")
55 Matcher<String> cOrD = anyOf(is(c.getName()), is(d.getName()));
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()));
64 public void testValidModule() throws Exception {
66 Date rev = new Date();
67 Module a = mockModule("a", rev);
68 Module b = mockModule("b", rev);
69 Module c = mockModule("c", rev);
75 Module[] builders = new Module[] { a, b, c };
77 List<Module> l = ModuleDependencySort.sort(builders);
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()));
84 @Test(expected = YangValidationException.class)
85 public void testModuleTwice() throws Exception {
86 ModuleBuilder a2 = mockModuleBuilder("a", null);
88 ModuleBuilder[] builders = new ModuleBuilder[] { a, a2 };
90 ModuleDependencySort.sort(builders);
91 } catch (YangValidationException e) {
92 assertThat(e.getMessage(), containsString("Module:a with revision:default declared twice"));
97 @Test(expected = YangValidationException.class)
98 public void testImportNotExistingModule() throws Exception {
101 ModuleBuilder[] builders = new ModuleBuilder[] { a };
103 ModuleDependencySort.sort(builders);
104 } catch (YangValidationException e) {
105 assertThat(e.getMessage(), containsString("Not existing module imported:b:default by:a:default"));
111 public void testImportTwice() throws Exception {
112 mockDependency(a, b);
113 mockDependency(c, b);
115 ModuleBuilder[] builders = new ModuleBuilder[] { a, b, c };
116 ModuleDependencySort.sort(builders);
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);
126 mockDependency(a, b);
127 mockDependency(c, b2);
129 ModuleBuilder[] builders = new ModuleBuilder[] { a, c, b, b2 };
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)));
141 public void testModuleTwiceWithDifferentRevs() throws Exception {
142 ModuleBuilder a2 = mockModuleBuilder("a", new Date());
144 ModuleBuilder[] builders = new ModuleBuilder[] { a, a2 };
145 ModuleDependencySort.sort(builders);
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);
154 ModuleBuilder[] builders = new ModuleBuilder[] { a, a2, a3 };
156 ModuleDependencySort.sort(builders);
157 } catch (YangValidationException e) {
158 assertThat(e.getMessage(), containsString("Module:a with revision:"
159 + YangParserListenerImpl.simpleDateFormat.format(rev) + " declared twice"));
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();
168 // Expects only one module revision
170 Set<Edge> inEdges = node.getValue().values().iterator().next().getInEdges();
171 Set<Edge> outEdges = node.getValue().values().iterator().next().getOutEdges();
173 if (name.equals("a")) {
174 assertEdgeCount(inEdges, 0, outEdges, 1);
175 } else if (name.equals("b")) {
176 assertEdgeCount(inEdges, 1, outEdges, 2);
178 assertEdgeCount(inEdges, 1, outEdges, 0);
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));
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);
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);
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();
208 doReturn(rev).when(a).getRevision();
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();
219 doReturn(rev).when(a).getRevision();