Bug 1131 - yang-parser-impl cleanup
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / util / TopologicalSortTest.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.Is.is;
11 import static org.junit.Assert.assertThat;
12
13 import com.google.common.collect.Sets;
14 import java.util.List;
15 import java.util.Set;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.parser.util.TopologicalSort.Node;
18 import org.opendaylight.yangtools.yang.parser.util.TopologicalSort.NodeImpl;
19
20 public class TopologicalSortTest {
21
22     @Test(expected = IllegalStateException.class)
23     public void test() throws Exception {
24         Set<Node> nodes = Sets.newHashSet();
25
26         NodeImpl node1 = new NodeImpl();
27         nodes.add(node1);
28         NodeImpl node2 = new NodeImpl();
29         nodes.add(node2);
30         NodeImpl node3 = new NodeImpl();
31         nodes.add(node3);
32
33         node1.addEdge(node2);
34         node2.addEdge(node3);
35         node3.addEdge(node1);
36
37         try {
38             TopologicalSort.sort(nodes);
39         } catch (IllegalStateException e) {
40             throw e;
41         }
42     }
43
44     @Test
45     public void testValidSimple() throws Exception {
46         Set<Node> nodes = Sets.newHashSet();
47
48         Node node1 = new NodeImpl();
49         nodes.add(node1);
50         Node node2 = new NodeImpl();
51         nodes.add(node2);
52         Node node3 = new NodeImpl();
53         nodes.add(node3);
54         Node node4 = new NodeImpl();
55         nodes.add(node4);
56
57         ((NodeImpl) node1).addEdge(node2);
58         ((NodeImpl) node1).addEdge(node3);
59         ((NodeImpl) node2).addEdge(node4);
60         ((NodeImpl) node3).addEdge(node2);
61
62         List<Node> sorted = TopologicalSort.sort(nodes);
63
64         assertThat(sorted.get(0), is(node4));
65         assertThat(sorted.get(1), is(node2));
66         assertThat(sorted.get(2), is(node3));
67         assertThat(sorted.get(3), is(node1));
68     }
69
70 }