Merge "Fixed bug in code generator when handling case defined in grouping as augment...
[yangtools.git] / integration-test / yang-runtime-tests / src / test / java / org / opendaylight / yangtools / it / yang / runtime / tests / BindingReadingTest.java
1 package org.opendaylight.yangtools.it.yang.runtime.tests;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6
7 import java.util.Map;
8
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
12 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
13 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
14 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
15 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.Source;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.binding.util.DataObjectReadingUtil;
27
28 import com.google.common.base.Optional;
29 import com.google.common.collect.ImmutableList;
30
31 public class BindingReadingTest {
32
33     private static final TopologyId TOPOLOGY_FOO_ID = new TopologyId("foo:topo");
34     private static final TopologyId TOPOLOGY_BAR_ID = new TopologyId("bar:topo");
35     private static final TopologyId TOPOLOGY_BAZ_ID = new TopologyId("baz:topo");
36     private static final LinkId LINK_BAR_ID = new LinkId("bar:link:1");
37     private static final NodeId SOURCE_NODE_ID = new NodeId("node:id");
38     private static final TpId SOURCE_TP_ID = new TpId("source:tp");
39     private static final InstanceIdentifier<NetworkTopology> NETWORK_TOPOLOGY_PATH = InstanceIdentifier.builder(
40             NetworkTopology.class).toInstance();
41     private NetworkTopology topologyModel;
42     private Link linkModel;
43
44     private static final InstanceIdentifier<Topology> TOPOLOGY_PATH =InstanceIdentifier.builder(NETWORK_TOPOLOGY_PATH) //
45             .child(Topology.class, new TopologyKey(TOPOLOGY_BAR_ID)) //
46             .build();
47
48     private static final InstanceIdentifier<Source> ABSOLUTE_SOURCE_PATH = InstanceIdentifier.builder(TOPOLOGY_PATH)
49             .child(Link.class, new LinkKey(LINK_BAR_ID)) //
50             .child(Source.class) //
51             .build();
52
53
54     private static final InstanceIdentifier<Source> WILDCARDED_SOURCE_PATH = InstanceIdentifier.builder(NETWORK_TOPOLOGY_PATH)
55             .child(Topology.class) //
56             .child(Link.class, new LinkKey(LINK_BAR_ID)) //
57             .child(Source.class) //
58             .build();
59
60
61     @Before
62     public void createTopology() {
63         linkModel = new LinkBuilder() //
64                 .setLinkId(LINK_BAR_ID) //
65                 .setSource(new SourceBuilder() //
66                         .setSourceNode(SOURCE_NODE_ID) //
67                         .setSourceTp(SOURCE_TP_ID) //
68                         .build()) //
69                 .build();
70         topologyModel = new NetworkTopologyBuilder().setTopology(ImmutableList.<Topology> builder() //
71                 .add(new TopologyBuilder() //
72                         .setTopologyId(TOPOLOGY_FOO_ID) //
73                         .setServerProvided(true) //
74                         .build()) //
75                 .add(new TopologyBuilder() //
76                         .setTopologyId(TOPOLOGY_BAR_ID) //
77                         .setServerProvided(false) //
78                         .setLink(ImmutableList.<Link> builder() //
79                                 .add(linkModel) //
80                                 .build()) //
81                         .build()) //
82                 .add(new TopologyBuilder() //
83                         .build())//
84                 .add(new TopologyBuilder() //
85                         .setTopologyId(TOPOLOGY_BAZ_ID).build()) //
86                 .build()) //
87                 .build(); //
88     }
89
90     @Test
91     public void testContainerRead() {
92         Optional<Source> source = DataObjectReadingUtil.readData(linkModel, Source.class);
93         assertNotNull(source);
94         assertEquals(linkModel.getSource(), source.get());
95     }
96
97     @Test
98     public void testInstanceIdentifierRead() {
99         Map<InstanceIdentifier<Source>, Source> source = DataObjectReadingUtil.readData(topologyModel, NETWORK_TOPOLOGY_PATH, ABSOLUTE_SOURCE_PATH);
100         assertNotNull(source);
101         Source potentialSource = source.get(ABSOLUTE_SOURCE_PATH);
102         assertEquals(linkModel.getSource(), potentialSource);
103     }
104
105     @Test
106     public void testInstanceIdentifierReadWildcarded() {
107         Topology topology = DataObjectReadingUtil.readData(topologyModel, NETWORK_TOPOLOGY_PATH, TOPOLOGY_PATH).get(TOPOLOGY_PATH);
108         Map<InstanceIdentifier<Source>, Source> source = DataObjectReadingUtil.readData(topology, TOPOLOGY_PATH, WILDCARDED_SOURCE_PATH);
109         assertNotNull(source);
110         Source potentialSource = source.get(ABSOLUTE_SOURCE_PATH);
111         assertEquals(linkModel.getSource(), potentialSource);
112     }
113
114     @Test
115     public void testInstanceIdentifierReadNonExistingValue() {
116         InstanceIdentifier<Source> sourcePath = InstanceIdentifier.builder(NETWORK_TOPOLOGY_PATH) //
117                 .child(Topology.class, new TopologyKey(TOPOLOGY_BAZ_ID)) //
118                 .child(Link.class, new LinkKey(LINK_BAR_ID)) //
119                 .child(Source.class) //
120                 .build();
121         Map<InstanceIdentifier<Source>, Source> source = DataObjectReadingUtil.readData(topologyModel, NETWORK_TOPOLOGY_PATH, sourcePath);
122         assertNotNull(source);
123         assertTrue(source.isEmpty());
124     }
125
126 }