2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.data.jaxen;
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;
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;
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.Revision;
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;
44 public class EnumValueXPathFunctionTest {
46 private static JaxenSchemaContextFactory jaxenSchemaContextFactory;
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;
55 public static void setup() throws ParseException {
56 jaxenSchemaContextFactory = new JaxenSchemaContextFactory();
58 fooModule = QNameModule.create(URI.create("foo-ns"), Revision.of("2017-04-03"));
59 myContainer = QName.create(fooModule, "my-container");
60 alarm = QName.create(fooModule, "alarm");
61 severity = QName.create(fooModule, "severity");
62 ordinaryLeaf = QName.create(fooModule, "ordinary-leaf");
66 public void testEnumValueFunction() throws Exception {
67 final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(EnumValueXPathFunctionTest.class,
68 "/yang-xpath-functions-test/enum-value-function/foo.yang");
69 assertNotNull(schemaContext);
71 final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
72 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("major"));
74 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
75 converterBiMap.put("foo-prefix", fooModule);
77 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
78 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
80 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
81 buildPathToSeverityLeafNode("major"));
83 final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
84 .getFunction(null, null, "enum-value");
85 final int enumValueResult = (int) enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
86 assertEquals(5, enumValueResult);
90 public void testInvalidTypeOfCorrespondingSchemaNode() throws Exception {
91 final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(EnumValueXPathFunctionTest.class,
92 "/yang-xpath-functions-test/enum-value-function/foo-invalid.yang");
93 assertNotNull(schemaContext);
95 final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
96 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("major"));
98 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
99 converterBiMap.put("foo-prefix", fooModule);
101 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
102 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
104 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
105 buildPathToSeverityLeafNode("major"));
107 final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
108 .getFunction(null, null, "enum-value");
109 final Double enumValueResult = (Double) enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
110 assertEquals(Double.NaN, enumValueResult, 0.001);
114 public void testInvalidNormalizedNodeValueType() throws Exception {
115 final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(EnumValueXPathFunctionTest.class,
116 "/yang-xpath-functions-test/enum-value-function/foo.yang");
117 assertNotNull(schemaContext);
119 final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
120 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(100));
122 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
123 converterBiMap.put("foo-prefix", fooModule);
125 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
126 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
128 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
129 buildPathToSeverityLeafNode(100));
131 final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
132 .getFunction(null, null, "enum-value");
133 final Double enumValueResult = (Double) enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
134 assertEquals(Double.NaN, enumValueResult, 0.001);
138 public void shouldFailOnUnknownEnumNodeValue() throws Exception {
139 final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(EnumValueXPathFunctionTest.class,
140 "/yang-xpath-functions-test/enum-value-function/foo.yang");
141 assertNotNull(schemaContext);
143 final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
144 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("unknown"));
146 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
147 converterBiMap.put("foo-prefix", fooModule);
149 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
150 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
152 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
153 buildPathToSeverityLeafNode("unknown"));
155 final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
156 .getFunction(null, null, "enum-value");
158 enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
159 fail("Function call should have failed on unknown enum node value");
160 } catch (final IllegalStateException ex) {
161 assertTrue(ex.getMessage().startsWith("Enum unknown does not belong to enumeration"));
166 public void shouldFailOnInvalidNumberOfArguments() throws Exception {
167 final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
168 final Function enumValueFunction = yangFunctionContext.getFunction(null, null, "enum-value");
170 final Context mockedContext = mock(Context.class);
173 enumValueFunction.call(mockedContext, ImmutableList.of("should not be here"));
174 fail("Function call should have failed on invalid number of arguments.");
175 } catch (final FunctionCallException ex) {
176 assertEquals("enum-value() takes one argument: node-set nodes.", ex.getMessage());
180 private static ContainerNode buildMyContainerNode(final Object keyLeafValue) {
181 final LeafNode<?> ordinaryLeafNode = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(ordinaryLeaf))
182 .withValue("test-value").build();
184 final MapNode alarmListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(alarm))
185 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
186 new NodeIdentifierWithPredicates(alarm, severity, keyLeafValue))
187 .withChild(ordinaryLeafNode).build()).build();
189 final ContainerNode myContainerNode = Builders.containerBuilder().withNodeIdentifier(
190 new NodeIdentifier(myContainer)).withChild(alarmListNode).build();
191 return myContainerNode;
194 private static YangInstanceIdentifier buildPathToSeverityLeafNode(final Object keyLeafValue) {
195 final ImmutableMap.Builder<QName, Object> builder = ImmutableMap.builder();
196 final ImmutableMap<QName, Object> keys = builder.put(severity, keyLeafValue).build();
198 final YangInstanceIdentifier path = YangInstanceIdentifier.of(alarm)
199 .node(new NodeIdentifierWithPredicates(alarm, keys)).node(severity);