Drop unneeded generic type specifiers
[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.CoreMatchers.containsString;
11 import static org.hamcrest.core.AnyOf.anyOf;
12 import static org.hamcrest.core.IsEqual.equalTo;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertThat;
15 import static org.mockito.Mockito.doReturn;
16 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.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.Set;
28 import org.hamcrest.Matcher;
29 import org.junit.Test;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
32 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
33 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort.ModuleNodeImpl;
34 import org.opendaylight.yangtools.yang.parser.util.TopologicalSort.Edge;
35
36 public class ModuleDependencySortTest {
37     private final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
38     private final ModuleBuilder a = mockModuleBuilder("a", null);
39     private final ModuleBuilder b = mockModuleBuilder("b", null);
40     private final ModuleBuilder c = mockModuleBuilder("c", null);
41     private final ModuleBuilder d = mockModuleBuilder("d", null);
42
43     @Test
44     public void testValid() throws Exception {
45
46         mockDependency(a, b);
47         mockDependency(b, c);
48         mockDependency(b, d);
49
50         ModuleBuilder[] builders = new ModuleBuilder[] { d, b, c, a };
51
52         List<ModuleBuilder> l = ModuleDependencySort.sort(builders);
53
54         assertDependencyGraph(ModuleDependencySort.createModuleGraph(ModuleOrModuleBuilder.fromAll(
55                 Collections.emptySet(), Arrays.asList(builders))));
56
57         Matcher<String> cOrD = anyOf(equalTo(c.getName()), equalTo(d.getName()));
58
59         assertThat(l.get(0).getName(), cOrD);
60         assertThat(l.get(1).getName(), cOrD);
61         assertEquals(b.getName(), l.get(2).getName());
62         assertEquals(a.getName(), l.get(3).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         assertEquals(c.getName(), l.get(0).getName());
82         assertEquals(b.getName(), l.get(1).getName());
83         assertEquals(a.getName(), l.get(2).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 static 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 static void assertEdgeCount(final Set<Edge> inEdges, final int i, final Set<Edge> outEdges, final int j) {
165         assertEquals(i, inEdges.size());
166         assertEquals(j, outEdges.size());
167     }
168
169     private static void mockDependency(final ModuleBuilder a, final ModuleBuilder b) {
170         ModuleImport imprt = mock(ModuleImport.class);
171         doReturn(b.getName()).when(imprt).getModuleName();
172         doReturn(b.getName()).when(imprt).getPrefix();
173         doReturn(b.getRevision()).when(imprt).getRevision();
174         a.getImports().put(b.getName(), imprt);
175     }
176
177     private static void mockDependency(final Module a, final Module b) {
178         ModuleImport imprt = mock(ModuleImport.class);
179         doReturn(b.getName()).when(imprt).getModuleName();
180         doReturn(b.getRevision()).when(imprt).getRevision();
181         a.getImports().add(imprt);
182     }
183
184     private static ModuleBuilder mockModuleBuilder(final String name, final Date rev) {
185         ModuleBuilder a = mock(ModuleBuilder.class);
186         doReturn(name).when(a).getName();
187         Map<String, ModuleImport> map = new HashMap<>();
188         doReturn(map).when(a).getImports();
189         if (rev != null) {
190             doReturn(rev).when(a).getRevision();
191         }
192         return a;
193     }
194
195     private static Module mockModule(final String name, final Date rev) {
196         Module a = mock(Module.class);
197         doReturn(name).when(a).getName();
198         Set<ModuleImport> set = Sets.newHashSet();
199         doReturn(set).when(a).getImports();
200         if (rev != null) {
201             doReturn(rev).when(a).getRevision();
202         }
203         return a;
204     }
205 }