Add ModuleDependencyInfoTest 78/70878/9
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 12 Apr 2018 22:31:26 +0000 (00:31 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 16 Apr 2018 13:26:10 +0000 (15:26 +0200)
We are missing an explicit test suite, start one.

Change-Id: I93e17df5c178d134a5d5a3bd586b398f93b0c4f3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/ModuleDependencySort.java
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/ModuleDependencySortTest.java [new file with mode: 0644]

index 6a6b7ab648e0af2a8d5c16d92c50c3fb4377986a..dee1a18f1ae7aee9547cc529b440cd1afa517d5f 100644 (file)
@@ -12,6 +12,7 @@ import com.google.common.collect.HashBasedTable;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Table;
 import java.net.URI;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -45,6 +46,17 @@ public final class ModuleDependencySort {
         throw new UnsupportedOperationException();
     }
 
+    /**
+     * Topological sort of module dependency graph.
+     *
+     * @param modules YANG modules
+     * @return Sorted list of Modules. Modules can be further processed in returned order.
+     * @throws IllegalArgumentException when provided modules are not consistent.
+     */
+    public static List<Module> sort(final Module... modules) {
+        return sort(Arrays.asList(modules));
+    }
+
     /**
      * Topological sort of module dependency graph.
      *
diff --git a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/ModuleDependencySortTest.java b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/ModuleDependencySortTest.java
new file mode 100644 (file)
index 0000000..a310b06
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.model.util;
+
+import static com.google.common.collect.ImmutableList.of;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.doReturn;
+
+import com.google.common.collect.ImmutableSet;
+import java.net.URI;
+import java.util.List;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.model.api.Module;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ModuleDependencySortTest {
+    private static final QNameModule TEST = QNameModule.create(URI.create("foo"));
+
+    @Mock
+    private Module fooNoRev;
+
+    @Before
+    public void before() {
+        doReturn(ImmutableSet.of()).when(fooNoRev).getImports();
+        doReturn("foo").when(fooNoRev).getName();
+        doReturn(TEST).when(fooNoRev).getQNameModule();
+        doReturn(TEST.getNamespace()).when(fooNoRev).getNamespace();
+        doReturn(TEST.getRevision()).when(fooNoRev).getRevision();
+    }
+
+    @Test
+    public void testSimpleModules() {
+        assertSortedTo(of(fooNoRev), fooNoRev);
+    }
+
+    private static void assertSortedTo(final List<Module> expected, final Module... modules) {
+        assertEquals(expected, ModuleDependencySort.sort(modules));
+    }
+}