e9131fbd43959f5b000acb538f4fa4c75efabeb5
[yangtools.git] / restconf / restconf-util / src / test / java / org / opendaylight / yangtools / restconf / utils / RestconfUtilsTest.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.yangtools.restconf.utils;
9
10 import static junit.framework.Assert.assertNotNull;
11 import static org.junit.Assert.assertEquals;
12
13 import java.io.InputStream;
14
15 import javassist.ClassPool;
16
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
26 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
27 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31
32 public class RestconfUtilsTest {
33
34     private RuntimeGeneratedMappingServiceImpl mappingService;
35
36     @Before
37     public void setup() {
38         this.mappingService = new RuntimeGeneratedMappingServiceImpl(new ClassPool());
39
40         final ModuleInfoBackedContext moduleInfo = ModuleInfoBackedContext.create();
41         moduleInfo.addModuleInfos(BindingReflections.loadModuleInfos());
42         this.mappingService.onGlobalContextUpdated(moduleInfo.tryToCreateSchemaContext().get());
43     }
44
45     @Test
46     public void firstTest() { // test static state collisions with the other test
47         final InstanceIdentifier<Topology> instanceIdentifier = InstanceIdentifier.builder(NetworkTopology.class)
48                 .child(Topology.class, new TopologyKey(new TopologyId("example-pcep-topology"))).toInstance();
49         final InputStream is = this.getClass().getClassLoader().getResourceAsStream("topology-bug1196-linux.xml");
50         RestconfUtils.toRestconfIdentifier(instanceIdentifier, this.mappingService,
51                 this.mappingService.getSchemaContext()).getValue();
52     }
53
54     @Test
55     public void testToDataObjectMappingWithNestedAugmentations() {
56         final InstanceIdentifier<Topology> topologyIdentifier = InstanceIdentifier.builder(NetworkTopology.class)
57                 .child(Topology.class).build();
58         final InputStream is = this.getClass().getClassLoader().getResourceAsStream("topology.xml");
59         final DataSchemaNode dataSchema = RestconfUtils.toRestconfIdentifier(topologyIdentifier, this.mappingService,
60                 this.mappingService.getSchemaContext()).getValue();
61         final Topology topology = (Topology) RestconfUtils.dataObjectFromInputStream(topologyIdentifier, is,
62                 this.mappingService.getSchemaContext(), this.mappingService, dataSchema);
63
64         assertNotNull(topology);
65         assertEquals(1, topology.getNode().size());
66         final Node node = topology.getNode().get(0);
67         assertEquals("bgpls://IsisLevel2:1/type=node&as=72&domain=673720360&router=0000.0000.0042", node.getNodeId()
68                 .getValue());
69
70         final Node1 node1 = node.getAugmentation(Node1.class);
71         assertNotNull(node1);
72         assertNotNull(node1.getIgpNodeAttributes());
73         assertEquals("Of-9k-02", node1.getIgpNodeAttributes().getName().getValue());
74
75         final IgpNodeAttributes1 igpAttributes1 = node1.getIgpNodeAttributes()
76                 .getAugmentation(IgpNodeAttributes1.class);
77         assertNotNull(igpAttributes1);
78         assertNotNull(igpAttributes1.getIsisNodeAttributes());
79         assertEquals("66", igpAttributes1.getIsisNodeAttributes().getIso().getIsoSystemId().getValue());
80     }
81
82 }