15987f2d45cfde80825612c44949b9da334f4211
[yangtools.git] / yang / yang-data-jaxen / src / test / java / org / opendaylight / yangtools / yang / data / jaxen / TestUtils.java
1 /*
2  * Copyright (c) 2015 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.yangtools.yang.data.jaxen;
10
11 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
12 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
13 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
14
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
34 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
37
38 final class TestUtils {
39
40     private static final QName ROOT_QNAME = QName.create("urn:opendaylight.test2", "2015-08-08", "root");
41     private static final QName LIST_A_QNAME = QName.create(ROOT_QNAME, "list-a");
42     private static final QName LIST_B_QNAME = QName.create(ROOT_QNAME, "list-b");
43     private static final QName LEAF_A_QNAME = QName.create(ROOT_QNAME, "leaf-a");
44     private static final QName LEAF_B_QNAME = QName.create(ROOT_QNAME, "leaf-b");
45     private static final QName LEAF_C_QNAME = QName.create(ROOT_QNAME, "leaf-c");
46     private static final QName LEAF_D_QNAME = QName.create(ROOT_QNAME, "leaf-d");
47     private static final QName CONTAINER_A_QNAME = QName.create(ROOT_QNAME, "container-a");
48     private static final QName CONTAINER_B_QNAME = QName.create(ROOT_QNAME, "container-b");
49     private static final String FOO = "foo";
50     private static final String BAR = "bar";
51     private static final String WAZ = "waz";
52     private static final String ONE = "one";
53     private static final String TWO = "two";
54     private static final String THREE = "three";
55
56     private TestUtils() {
57     }
58
59     static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException,
60             ReactorException {
61         URI path = TestUtils.class.getResource(resourceDirectory).toURI();
62         final File testDir = new File(path);
63         final String[] fileList = testDir.list();
64         final List<File> testFiles = new ArrayList<>();
65         if (fileList == null) {
66             throw new FileNotFoundException(resourceDirectory);
67         }
68         for (String fileName : fileList) {
69             if (!new File(testDir, fileName).isDirectory()) {
70                 testFiles.add(new File(testDir, fileName));
71             }
72         }
73         return parseYangSources(testFiles);
74     }
75
76     public static SchemaContext parseYangSources(StatementStreamSource... sources)
77             throws SourceException, ReactorException {
78
79         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
80                 .newBuild();
81         reactor.addSources(sources);
82
83         return reactor.buildEffective();
84     }
85
86     public static SchemaContext parseYangSources(File... files) throws SourceException, ReactorException, FileNotFoundException {
87
88         StatementStreamSource[] sources = new StatementStreamSource[files.length];
89
90         for (int i = 0; i<files.length; i++) {
91             sources[i] = new YangStatementSourceImpl(new FileInputStream(files[i]));
92         }
93
94         return parseYangSources(sources);
95     }
96
97     public static SchemaContext parseYangSources(Collection<File> files) throws SourceException, ReactorException, FileNotFoundException {
98         return parseYangSources(files.toArray(new File[files.size()]));
99     }
100
101     /**
102      * Returns a test document
103      *
104      * <pre>
105      * root
106      *     leaf-c "waz"
107      *     list-a
108      *          leaf-a "foo"
109      *     list-a
110      *          leaf-a "bar"
111      *          list-b
112      *                  leaf-b "one"
113      *          list-b
114      *                  leaf-b "two"
115      *     container-a
116      *          container-b
117      *                  leaf-d "three"
118      * </pre>
119      *
120      * @return
121      */
122     public static NormalizedNode<?, ?> createNormalizedNodes() {
123         return ImmutableContainerNodeBuilder
124                 .create()
125                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ROOT_QNAME))
126                 .withChild(ImmutableNodes.leafNode(LEAF_C_QNAME, WAZ))
127                 .withChild(mapNodeBuilder(LIST_A_QNAME)
128                         .withChild(mapEntry(LIST_A_QNAME, LEAF_A_QNAME, FOO))
129                         .withChild(mapEntryBuilder(LIST_A_QNAME, LEAF_A_QNAME, BAR)
130                                 .withChild(mapNodeBuilder(LIST_B_QNAME)
131                                         .withChild(mapEntry(LIST_B_QNAME, LEAF_B_QNAME, ONE))
132                                         .withChild(mapEntry(LIST_B_QNAME, LEAF_B_QNAME, TWO))
133                                         .build())
134                                 .build())
135                         .build())
136                 .withChild(ImmutableContainerNodeBuilder.create()
137                         .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONTAINER_A_QNAME))
138                         .withChild(ImmutableContainerNodeBuilder.create().
139                                 withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier
140                                         (CONTAINER_B_QNAME))
141                                 .withChild(ImmutableNodes.leafNode(LEAF_D_QNAME, THREE))
142                                 .build())
143                         .build())
144                 .build();
145     }
146 }