Get rid of old Yang Parser in Controller
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / xml / codec / XmlUtilsTest.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
9 package org.opendaylight.controller.xml.codec;
10
11 import com.google.common.collect.Lists;
12 import com.google.common.io.ByteSource;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.ArrayList;
16 import javax.xml.parsers.DocumentBuilderFactory;
17 import org.junit.Before;
18 import org.opendaylight.yangtools.yang.model.api.Module;
19 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
22 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
24
25 // FIXME : CompositeNode is not avaliable anymore so fix the test to use NormalizedNodeContainer ASAP
26 public class XmlUtilsTest {
27
28   private static final DocumentBuilderFactory BUILDERFACTORY;
29
30   static {
31     final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
32     factory.setNamespaceAware(true);
33     factory.setCoalescing(true);
34     factory.setIgnoringElementContentWhitespace(true);
35     factory.setIgnoringComments(true);
36     BUILDERFACTORY = factory;
37   }
38
39   private SchemaContext schemaContext;
40   private RpcDefinition testRpc;
41
42   public static final String XML_CONTENT = "<add-flow xmlns=\"urn:opendaylight:controller:rpc:test\"><input xmlns=\"urn:opendaylight:controller:rpc:test\">" +
43       "<id>flowid</id>" +
44       "<flow xmlns:ltha=\"urn:opendaylight:controller:rpc:test\">/ltha:node/ltha:node1[ltha:id='3@java.lang.Short']</flow>" +
45       "</input></add-flow>";
46
47   @Before
48   public void setUp() throws Exception {
49     final ByteSource byteSource = new ByteSource() {
50       @Override
51       public InputStream openStream() throws IOException {
52         return XmlUtilsTest.this.getClass().getResourceAsStream("rpcTest.yang");
53       }
54     };
55
56     final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
57     final ArrayList<ByteSource> sources = Lists.newArrayList(byteSource);
58
59     try {
60
61       schemaContext = reactor.buildEffective(sources);
62     } catch (ReactorException e) {
63       throw new RuntimeException("Unable to build schema context from " + sources, e);
64     }
65
66     final Module rpcTestModule = schemaContext.getModules().iterator().next();
67     testRpc = rpcTestModule.getRpcs().iterator().next();
68   }
69
70 }