Bug 868: Replaced use of toInstance() for build().
[l2switch.git] / loopremover / implementation / src / test / java / org / opendaylight / l2switch / loopremover / util / InstanceIdentifierUtilsTest.java
1 /*
2  * Copyright (c) 2014 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.l2switch.loopremover.util;
9
10 import org.junit.Test;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
28 public class InstanceIdentifierUtilsTest {
29
30   private final Short NUM_ID_1 = 1;
31   private final String STR_ID_1 = "id1";
32   private final String STR_ID_2 = "id2";
33
34   @Test
35   public void testCreateNodePath() throws Exception {
36     InstanceIdentifier<Node> insId = InstanceIdentifierUtils.createNodePath(new NodeId(STR_ID_1));
37     assertNotNull(insId);
38     assertNotNull(insId.firstIdentifierOf(Nodes.class));
39     assertEquals(STR_ID_1, insId.firstKeyOf(Node.class, NodeKey.class).getId().getValue());
40   }
41
42   @Test
43   public void testGetNodePath() throws Exception {
44     InstanceIdentifier<NodeConnector> ncInsId = InstanceIdentifier.builder(Nodes.class).child(Node.class).child(NodeConnector.class).build();
45     assertNotNull(InstanceIdentifierUtils.getNodePath(ncInsId));
46   }
47
48   @Test
49   public void testCreateTablePath() throws Exception {
50     InstanceIdentifier<Table> tableInsId =
51       InstanceIdentifierUtils.createTablePath(
52         InstanceIdentifier.builder(Nodes.class).child(Node.class).build(),
53         new TableKey(NUM_ID_1));
54     assertNotNull(tableInsId);
55     assertEquals(NUM_ID_1.shortValue(), tableInsId.firstKeyOf(Table.class, TableKey.class).getId().shortValue());
56     assertNotNull(tableInsId.firstIdentifierOf(FlowCapableNode.class));
57   }
58
59   @Test
60   public void testCreateNodeConnectorIdentifier() throws Exception {
61     InstanceIdentifier<NodeConnector> ncInsId = InstanceIdentifierUtils.createNodeConnectorIdentifier(STR_ID_1, STR_ID_2);
62     assertNotNull(ncInsId);
63     assertEquals(STR_ID_1, ncInsId.firstKeyOf(Node.class, NodeKey.class).getId().getValue());
64     assertEquals(STR_ID_2, ncInsId.firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId().getValue());
65   }
66
67   @Test
68   public void testGenerateNodeInstanceIdentifier() throws Exception {
69     NodeConnectorRef ncRef = new NodeConnectorRef(InstanceIdentifier.builder(Nodes.class).child(Node.class).child(NodeConnector.class).build());
70     assertNotNull(InstanceIdentifierUtils.generateNodeInstanceIdentifier(ncRef));
71   }
72
73   @Test
74   public void testGenerateFlowTableInstanceIdentifier() throws Exception {
75     NodeConnectorRef ncRef = new NodeConnectorRef(InstanceIdentifier.builder(Nodes.class).child(Node.class).child(NodeConnector.class).build());
76     InstanceIdentifier<Table> tableInsId = InstanceIdentifierUtils.generateFlowTableInstanceIdentifier(ncRef, new TableKey(NUM_ID_1));
77     assertNotNull(tableInsId);
78     assertEquals(NUM_ID_1, tableInsId.firstKeyOf(Table.class, TableKey.class).getId());
79   }
80
81   @Test
82   public void testGenerateTopologyInstanceIdentifier() throws Exception {
83     InstanceIdentifier<Topology> topologyInsId = InstanceIdentifierUtils.generateTopologyInstanceIdentifier(STR_ID_1);
84     assertNotNull(topologyInsId);
85     assertEquals(STR_ID_1, topologyInsId.firstKeyOf(Topology.class, TopologyKey.class).getTopologyId().getValue());
86   }
87 }