X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fyang-model-parser-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fyang%2Fparser%2Futil%2FModuleDependencySortTest.java;fp=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fyang-model-parser-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fyang%2Fparser%2Futil%2FModuleDependencySortTest.java;h=484e34d4101c20ffbe7fd6ffaf659a4a321c8007;hp=195347aac04c64b6a6660e8b4cf27713bd34df41;hb=8d0760923824b72f8279ea26b97377ae26a49bee;hpb=e3e571dbfd9bb37364502101d482b7796d6b5cea diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/util/ModuleDependencySortTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/util/ModuleDependencySortTest.java index 195347aac0..484e34d410 100644 --- a/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/util/ModuleDependencySortTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/yang-model-parser-impl/src/test/java/org/opendaylight/controller/yang/parser/util/ModuleDependencySortTest.java @@ -7,12 +7,13 @@ */ package org.opendaylight.controller.yang.parser.util; -import static org.hamcrest.core.AnyOf.anyOf; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; -import static org.junit.matchers.JUnitMatchers.containsString; +import static org.hamcrest.core.AnyOf.*; +import static org.hamcrest.core.Is.*; +import static org.junit.Assert.*; +import static org.junit.matchers.JUnitMatchers.*; import static org.mockito.Mockito.*; +import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map; @@ -21,21 +22,21 @@ import java.util.Set; import org.hamcrest.Matcher; import org.junit.Test; +import org.opendaylight.controller.yang.model.api.Module; import org.opendaylight.controller.yang.model.api.ModuleImport; import org.opendaylight.controller.yang.parser.builder.impl.ModuleBuilder; import org.opendaylight.controller.yang.parser.impl.YangParserListenerImpl; import org.opendaylight.controller.yang.parser.util.ModuleDependencySort.ModuleNodeImpl; -import org.opendaylight.controller.yang.parser.util.ModuleDependencySort.ModuleSimple; import org.opendaylight.controller.yang.parser.util.TopologicalSort.Edge; import com.google.common.collect.Sets; public class ModuleDependencySortTest { - private ModuleBuilder a = mockModule("a", null); - private ModuleBuilder b = mockModule("b", null); - private ModuleBuilder c = mockModule("c", null); - private ModuleBuilder d = mockModule("d", null); + private ModuleBuilder a = mockModuleBuilder("a", null); + private ModuleBuilder b = mockModuleBuilder("b", null); + private ModuleBuilder c = mockModuleBuilder("c", null); + private ModuleBuilder d = mockModuleBuilder("d", null); @Test public void testValid() throws Exception { @@ -45,11 +46,11 @@ public class ModuleDependencySortTest { mockDependency(b, d); ModuleBuilder[] builders = new ModuleBuilder[] { d, b, c, a }; - ModuleDependencySort sort = new ModuleDependencySort(builders); - assertDependencyGraph(sort.getModuleGraph()); + List l = ModuleDependencySort.sort(builders); - List l = sort.sort(); + assertDependencyGraph(ModuleDependencySort.createModuleGraph(Arrays + .asList(builders))); @SuppressWarnings("unchecked") Matcher cOrD = anyOf(is(c.getName()), is(d.getName())); @@ -60,15 +61,37 @@ public class ModuleDependencySortTest { assertThat(l.get(3).getName(), is(a.getName())); } + @Test + public void testValidModule() throws Exception { + + Date rev = new Date(); + Module a = mockModule("a", rev); + Module b = mockModule("b", rev); + Module c = mockModule("c", rev); + + mockDependency(a, b); + mockDependency(b, c); + mockDependency(a, c); + + Module[] builders = new Module[] { a, b, c }; + + List l = ModuleDependencySort.sort(builders); + + assertThat(l.get(0).getName(), is(c.getName())); + assertThat(l.get(1).getName(), is(b.getName())); + assertThat(l.get(2).getName(), is(a.getName())); + } + @Test(expected = YangValidationException.class) public void testModuleTwice() throws Exception { - ModuleBuilder a2 = mockModule("a", null); + ModuleBuilder a2 = mockModuleBuilder("a", null); ModuleBuilder[] builders = new ModuleBuilder[] { a, a2 }; try { - new ModuleDependencySort(builders); + ModuleDependencySort.sort(builders); } catch (YangValidationException e) { - assertThat(e.getMessage(), + assertThat( + e.getMessage(), containsString("Module:a with revision:default declared twice")); throw e; } @@ -80,9 +103,10 @@ public class ModuleDependencySortTest { ModuleBuilder[] builders = new ModuleBuilder[] { a }; try { - new ModuleDependencySort(builders); + ModuleDependencySort.sort(builders); } catch (YangValidationException e) { - assertThat(e.getMessage(), + assertThat( + e.getMessage(), containsString("Not existing module imported:b:default by:a:default")); throw e; } @@ -94,23 +118,23 @@ public class ModuleDependencySortTest { mockDependency(c, b); ModuleBuilder[] builders = new ModuleBuilder[] { a, b, c }; - new ModuleDependencySort(builders); + ModuleDependencySort.sort(builders); } @Test(expected = YangValidationException.class) public void testImportTwiceDifferentRevision() throws Exception { Date date = new Date(); - ModuleBuilder b2 = mockModule("b", date); + ModuleBuilder b2 = mockModuleBuilder("b", date); mockDependency(a, b); mockDependency(c, b2); ModuleBuilder[] builders = new ModuleBuilder[] { a, c, b, b2 }; try { - ModuleDependencySort aaa = new ModuleDependencySort(builders); - System.out.println(aaa.getModuleGraph()); + ModuleDependencySort.sort(builders); } catch (YangValidationException e) { - assertThat(e.getMessage(), + assertThat( + e.getMessage(), containsString("Module:b imported twice with different revisions:default, " + YangParserListenerImpl.simpleDateFormat .format(date))); @@ -120,21 +144,21 @@ public class ModuleDependencySortTest { @Test public void testModuleTwiceWithDifferentRevs() throws Exception { - ModuleBuilder a2 = mockModule("a", new Date()); + ModuleBuilder a2 = mockModuleBuilder("a", new Date()); ModuleBuilder[] builders = new ModuleBuilder[] { a, a2 }; - new ModuleDependencySort(builders); + ModuleDependencySort.sort(builders); } @Test(expected = YangValidationException.class) public void testModuleTwice2() throws Exception { Date rev = new Date(); - ModuleBuilder a2 = mockModule("a", rev); - ModuleBuilder a3 = mockModule("a", rev); + ModuleBuilder a2 = mockModuleBuilder("a", rev); + ModuleBuilder a3 = mockModuleBuilder("a", rev); ModuleBuilder[] builders = new ModuleBuilder[] { a, a2, a3 }; try { - new ModuleDependencySort(builders); + ModuleDependencySort.sort(builders); } catch (YangValidationException e) { assertThat(e.getMessage(), containsString("Module:a with revision:" + YangParserListenerImpl.simpleDateFormat.format(rev) @@ -179,7 +203,14 @@ public class ModuleDependencySortTest { a.getModuleImports().add(imprt); } - private ModuleBuilder mockModule(String name, Date rev) { + private void mockDependency(Module a, Module b) { + ModuleImport imprt = mock(ModuleImport.class); + doReturn(b.getName()).when(imprt).getModuleName(); + doReturn(b.getRevision()).when(imprt).getRevision(); + a.getImports().add(imprt); + } + + private ModuleBuilder mockModuleBuilder(String name, Date rev) { ModuleBuilder a = mock(ModuleBuilder.class); doReturn(name).when(a).getName(); Set set = Sets.newHashSet(); @@ -189,4 +220,15 @@ public class ModuleDependencySortTest { } return a; } + + private Module mockModule(String name, Date rev) { + Module a = mock(Module.class); + doReturn(name).when(a).getName(); + Set set = Sets.newHashSet(); + doReturn(set).when(a).getImports(); + if (rev != null) { + doReturn(rev).when(a).getRevision(); + } + return a; + } }