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.yangtools.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.doReturn;
15 import static org.mockito.Mockito.mock;
17 import com.google.common.collect.Sets;
18 import java.text.DateFormat;
19 import java.text.SimpleDateFormat;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.List;
25 import java.util.Map.Entry;
27 import org.hamcrest.Matcher;
28 import org.junit.Test;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
31 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
32 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort.ModuleNodeImpl;
33 import org.opendaylight.yangtools.yang.parser.util.TopologicalSort.Edge;
35 public class ModuleDependencySortTest {
36 private final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
37 private final ModuleBuilder a = mockModuleBuilder("a", null);
38 private final ModuleBuilder b = mockModuleBuilder("b", null);
39 private final ModuleBuilder c = mockModuleBuilder("c", null);
40 private final ModuleBuilder d = mockModuleBuilder("d", null);
43 public void testValid() throws Exception {
49 ModuleBuilder[] builders = new ModuleBuilder[] { d, b, c, a };
51 List<ModuleBuilder> l = ModuleDependencySort.sort(builders);
53 assertDependencyGraph(ModuleDependencySort.createModuleGraph(ModuleOrModuleBuilder.fromAll(
54 Collections.<Module>emptySet(), Arrays.asList(builders))));
56 @SuppressWarnings("unchecked")
57 Matcher<String> cOrD = anyOf(is(c.getName()), is(d.getName()));
59 assertThat(l.get(0).getName(), cOrD);
60 assertThat(l.get(1).getName(), cOrD);
61 assertThat(l.get(2).getName(), is(b.getName()));
62 assertThat(l.get(3).getName(), is(a.getName()));
66 public void testValidModule() throws Exception {
68 Date rev = new Date();
69 Module a = mockModule("a", rev);
70 Module b = mockModule("b", rev);
71 Module c = mockModule("c", rev);
77 Module[] builders = new Module[] { a, b, c };
79 List<Module> l = ModuleDependencySort.sort(builders);
81 assertThat(l.get(0).getName(), is(c.getName()));
82 assertThat(l.get(1).getName(), is(b.getName()));
83 assertThat(l.get(2).getName(), is(a.getName()));
86 @Test(expected = YangValidationException.class)
87 public void testModuleTwice() throws Exception {
88 ModuleBuilder a2 = mockModuleBuilder("a", null);
90 ModuleBuilder[] builders = new ModuleBuilder[] { a, a2 };
92 ModuleDependencySort.sort(builders);
93 } catch (YangValidationException e) {
94 assertThat(e.getMessage(), containsString("Module:a with revision:default declared twice"));
99 @Test(expected = YangValidationException.class)
100 public void testImportNotExistingModule() throws Exception {
101 mockDependency(a, b);
103 ModuleBuilder[] builders = new ModuleBuilder[] { a };
105 ModuleDependencySort.sort(builders);
106 } catch (YangValidationException e) {
107 assertThat(e.getMessage(), containsString("Not existing module imported:b:default by:a:default"));
113 public void testImportTwice() throws Exception {
114 mockDependency(a, b);
115 mockDependency(c, b);
117 ModuleBuilder[] builders = new ModuleBuilder[] { a, b, c };
118 ModuleDependencySort.sort(builders);
122 public void testModuleTwiceWithDifferentRevs() throws Exception {
123 ModuleBuilder a2 = mockModuleBuilder("a", new Date());
125 ModuleBuilder[] builders = new ModuleBuilder[] { a, a2 };
126 ModuleDependencySort.sort(builders);
129 @Test(expected = YangValidationException.class)
130 public void testModuleTwice2() throws Exception {
131 Date rev = new Date();
132 ModuleBuilder a2 = mockModuleBuilder("a", rev);
133 ModuleBuilder a3 = mockModuleBuilder("a", rev);
135 ModuleBuilder[] builders = new ModuleBuilder[] { a, a2, a3 };
137 ModuleDependencySort.sort(builders);
138 } catch (YangValidationException e) {
139 assertThat(e.getMessage(), containsString("Module:a with revision:" + SIMPLE_DATE_FORMAT.format(rev)
140 + " declared twice"));
145 private void assertDependencyGraph(final Map<String, Map<Date, ModuleNodeImpl>> moduleGraph) {
146 for (Entry<String, Map<Date, ModuleNodeImpl>> node : moduleGraph.entrySet()) {
147 String name = node.getKey();
149 // Expects only one module revision
151 Set<Edge> inEdges = node.getValue().values().iterator().next().getInEdges();
152 Set<Edge> outEdges = node.getValue().values().iterator().next().getOutEdges();
154 if (name.equals("a")) {
155 assertEdgeCount(inEdges, 0, outEdges, 1);
156 } else if (name.equals("b")) {
157 assertEdgeCount(inEdges, 1, outEdges, 2);
159 assertEdgeCount(inEdges, 1, outEdges, 0);
164 private void assertEdgeCount(final Set<Edge> inEdges, final int i, final Set<Edge> outEdges, final int j) {
165 assertThat(inEdges.size(), is(i));
166 assertThat(outEdges.size(), is(j));
169 private void mockDependency(final ModuleBuilder a, final ModuleBuilder b) {
170 ModuleImport imprt = mock(ModuleImport.class);
171 doReturn(b.getName()).when(imprt).getModuleName();
172 doReturn(b.getRevision()).when(imprt).getRevision();
173 a.getModuleImports().add(imprt);
176 private void mockDependency(final Module a, final Module b) {
177 ModuleImport imprt = mock(ModuleImport.class);
178 doReturn(b.getName()).when(imprt).getModuleName();
179 doReturn(b.getRevision()).when(imprt).getRevision();
180 a.getImports().add(imprt);
183 private ModuleBuilder mockModuleBuilder(final String name, final Date rev) {
184 ModuleBuilder a = mock(ModuleBuilder.class);
185 doReturn(name).when(a).getName();
186 Set<ModuleImport> set = Sets.newHashSet();
187 doReturn(set).when(a).getModuleImports();
189 doReturn(rev).when(a).getRevision();
194 private Module mockModule(final String name, final Date rev) {
195 Module a = mock(Module.class);
196 doReturn(name).when(a).getName();
197 Set<ModuleImport> set = Sets.newHashSet();
198 doReturn(set).when(a).getImports();
200 doReturn(rev).when(a).getRevision();