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