0e42f875e5bf43e7084ef8e26e4ace5a438898a2
[yangtools.git] / yang / yang-data-jaxen / src / test / java / org / opendaylight / yangtools / yang / data / jaxen / EnumValueXPathFunctionTest.java
1 /*
2  * Copyright (c) 2017 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.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Mockito.mock;
16
17 import com.google.common.collect.BiMap;
18 import com.google.common.collect.HashBiMap;
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.ImmutableMap;
21 import com.google.common.collect.Maps;
22 import java.net.URI;
23 import java.text.ParseException;
24 import org.jaxen.Context;
25 import org.jaxen.Function;
26 import org.jaxen.FunctionCallException;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.QNameModule;
31 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathDocument;
39 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathSchemaContext;
40 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
43
44 public class EnumValueXPathFunctionTest {
45
46     private static JaxenSchemaContextFactory jaxenSchemaContextFactory;
47
48     private static QNameModule fooModule;
49     private static QName myContainer;
50     private static QName alarm;
51     private static QName severity;
52     private static QName ordinaryLeaf;
53
54     @BeforeClass
55     public static void setup() throws ParseException {
56         jaxenSchemaContextFactory = new JaxenSchemaContextFactory();
57
58         fooModule = QNameModule.create(URI.create("foo-ns"),
59                 SimpleDateFormatUtil.getRevisionFormat().parse("2017-04-03"));
60         myContainer = QName.create(fooModule, "my-container");
61         alarm = QName.create(fooModule, "alarm");
62         severity = QName.create(fooModule, "severity");
63         ordinaryLeaf = QName.create(fooModule, "ordinary-leaf");
64     }
65
66     @Test
67     public void testEnumValueFunction() throws Exception {
68         final SchemaContext schemaContext = YangParserTestUtils.parseYangSource(
69                 "/yang-xpath-functions-test/enum-value-function/foo.yang");
70         assertNotNull(schemaContext);
71
72         final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
73         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("major"));
74
75         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
76         converterBiMap.put("foo-prefix", fooModule);
77
78         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
79                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
80
81         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
82                 buildPathToSeverityLeafNode("major"));
83
84         final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
85                 .getFunction(null, null, "enum-value");
86         final int enumValueResult = (int) enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
87         assertEquals(5, enumValueResult);
88     }
89
90     @Test
91     public void testInvalidTypeOfCorrespondingSchemaNode() throws Exception {
92         final SchemaContext schemaContext = YangParserTestUtils.parseYangSource(
93                 "/yang-xpath-functions-test/enum-value-function/foo-invalid.yang");
94         assertNotNull(schemaContext);
95
96         final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
97         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("major"));
98
99         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
100         converterBiMap.put("foo-prefix", fooModule);
101
102         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
103                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
104
105         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
106                 buildPathToSeverityLeafNode("major"));
107
108         final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
109                 .getFunction(null, null, "enum-value");
110         final Double enumValueResult = (Double) enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
111         assertEquals(Double.NaN, enumValueResult, 0.001);
112     }
113
114     @Test
115     public void testInvalidNormalizedNodeValueType() throws Exception {
116         final SchemaContext schemaContext = YangParserTestUtils.parseYangSource(
117                 "/yang-xpath-functions-test/enum-value-function/foo.yang");
118         assertNotNull(schemaContext);
119
120         final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
121         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(100));
122
123         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
124         converterBiMap.put("foo-prefix", fooModule);
125
126         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
127                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
128
129         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
130                 buildPathToSeverityLeafNode(100));
131
132         final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
133                 .getFunction(null, null, "enum-value");
134         final Double enumValueResult = (Double) enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
135         assertEquals(Double.NaN, enumValueResult, 0.001);
136     }
137
138     @Test
139     public void shouldFailOnUnknownEnumNodeValue() throws Exception {
140         final SchemaContext schemaContext = YangParserTestUtils.parseYangSource(
141                 "/yang-xpath-functions-test/enum-value-function/foo.yang");
142         assertNotNull(schemaContext);
143
144         final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
145         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("unknown"));
146
147         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
148         converterBiMap.put("foo-prefix", fooModule);
149
150         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
151                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
152
153         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
154                 buildPathToSeverityLeafNode("unknown"));
155
156         final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
157                 .getFunction(null, null, "enum-value");
158         try {
159             enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
160             fail("Function call should have failed on unknown enum node value");
161         } catch (final IllegalStateException ex) {
162             assertTrue(ex.getMessage().startsWith("Enum unknown does not belong to enumeration"));
163         }
164     }
165
166     @Test
167     public void shouldFailOnInvalidNumberOfArguments() throws Exception {
168         final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
169         final Function enumValueFunction = yangFunctionContext.getFunction(null, null, "enum-value");
170
171         final Context mockedContext = mock(Context.class);
172
173         try {
174             enumValueFunction.call(mockedContext, ImmutableList.of("should not be here"));
175             fail("Function call should have failed on invalid number of arguments.");
176         } catch (final FunctionCallException ex) {
177             assertEquals("enum-value() takes one argument: node-set nodes.", ex.getMessage());
178         }
179     }
180
181     private static ContainerNode buildMyContainerNode(final Object keyLeafValue) {
182         final LeafNode<?> ordinaryLeafNode = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(ordinaryLeaf))
183                 .withValue("test-value").build();
184
185         final MapNode alarmListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(alarm))
186                 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
187                         new NodeIdentifierWithPredicates(alarm, severity, keyLeafValue))
188                         .withChild(ordinaryLeafNode).build()).build();
189
190         final ContainerNode myContainerNode = Builders.containerBuilder().withNodeIdentifier(
191                 new NodeIdentifier(myContainer)).withChild(alarmListNode).build();
192         return myContainerNode;
193     }
194
195     private static YangInstanceIdentifier buildPathToSeverityLeafNode(final Object keyLeafValue) {
196         final ImmutableMap.Builder<QName, Object> builder = ImmutableMap.builder();
197         final ImmutableMap<QName, Object> keys = builder.put(severity, keyLeafValue).build();
198
199         final YangInstanceIdentifier path = YangInstanceIdentifier.of(alarm)
200                 .node(new NodeIdentifierWithPredicates(alarm, keys)).node(severity);
201         return path;
202     }
203 }