edacfc55727f3d48d42eb0790d21a6c4f39374c0
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / 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.yangtools.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.doReturn;
15 import static org.mockito.Mockito.mock;
16
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;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.Set;
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;
34
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);
41
42     @Test
43     public void testValid() throws Exception {
44
45         mockDependency(a, b);
46         mockDependency(b, c);
47         mockDependency(b, d);
48
49         ModuleBuilder[] builders = new ModuleBuilder[] { d, b, c, a };
50
51         List<ModuleBuilder> l = ModuleDependencySort.sort(builders);
52
53         assertDependencyGraph(ModuleDependencySort.createModuleGraph(ModuleOrModuleBuilder.fromAll(
54                 Collections.<Module>emptySet(), Arrays.asList(builders))));
55
56         @SuppressWarnings("unchecked")
57         Matcher<String> cOrD = anyOf(is(c.getName()), is(d.getName()));
58
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()));
63     }
64
65     @Test
66     public void testValidModule() throws Exception {
67
68         Date rev = new Date();
69         Module a = mockModule("a", rev);
70         Module b = mockModule("b", rev);
71         Module c = mockModule("c", rev);
72
73         mockDependency(a, b);
74         mockDependency(b, c);
75         mockDependency(a, c);
76
77         Module[] builders = new Module[] { a, b, c };
78
79         List<Module> l = ModuleDependencySort.sort(builders);
80
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()));
84     }
85
86     @Test(expected = YangValidationException.class)
87     public void testModuleTwice() throws Exception {
88         ModuleBuilder a2 = mockModuleBuilder("a", null);
89
90         ModuleBuilder[] builders = new ModuleBuilder[] { a, a2 };
91         try {
92             ModuleDependencySort.sort(builders);
93         } catch (YangValidationException e) {
94             assertThat(e.getMessage(), containsString("Module:a with revision:default declared twice"));
95             throw e;
96         }
97     }
98
99     @Test(expected = YangValidationException.class)
100     public void testImportNotExistingModule() throws Exception {
101         mockDependency(a, b);
102
103         ModuleBuilder[] builders = new ModuleBuilder[] { a };
104         try {
105             ModuleDependencySort.sort(builders);
106         } catch (YangValidationException e) {
107             assertThat(e.getMessage(), containsString("Not existing module imported:b:default by:a:default"));
108             throw e;
109         }
110     }
111
112     @Test
113     public void testImportTwice() throws Exception {
114         mockDependency(a, b);
115         mockDependency(c, b);
116
117         ModuleBuilder[] builders = new ModuleBuilder[] { a, b, c };
118         ModuleDependencySort.sort(builders);
119     }
120
121     @Test
122     public void testModuleTwiceWithDifferentRevs() throws Exception {
123         ModuleBuilder a2 = mockModuleBuilder("a", new Date());
124
125         ModuleBuilder[] builders = new ModuleBuilder[] { a, a2 };
126         ModuleDependencySort.sort(builders);
127     }
128
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);
134
135         ModuleBuilder[] builders = new ModuleBuilder[] { a, a2, a3 };
136         try {
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"));
141             throw e;
142         }
143     }
144
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();
148
149             // Expects only one module revision
150
151             Set<Edge> inEdges = node.getValue().values().iterator().next().getInEdges();
152             Set<Edge> outEdges = node.getValue().values().iterator().next().getOutEdges();
153
154             if (name.equals("a")) {
155                 assertEdgeCount(inEdges, 0, outEdges, 1);
156             } else if (name.equals("b")) {
157                 assertEdgeCount(inEdges, 1, outEdges, 2);
158             } else {
159                 assertEdgeCount(inEdges, 1, outEdges, 0);
160             }
161         }
162     }
163
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));
167     }
168
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);
174     }
175
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);
181     }
182
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();
188         if (rev != null) {
189             doReturn(rev).when(a).getRevision();
190         }
191         return a;
192     }
193
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();
199         if (rev != null) {
200             doReturn(rev).when(a).getRevision();
201         }
202         return a;
203     }
204 }