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.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;
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"),
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");
67 public void testEnumValueFunction() throws Exception {
68 final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(EnumValueXPathFunctionTest.class,
69 "/yang-xpath-functions-test/enum-value-function/foo.yang");
70 assertNotNull(schemaContext);
72 final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
73 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("major"));
75 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
76 converterBiMap.put("foo-prefix", fooModule);
78 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
79 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
81 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
82 buildPathToSeverityLeafNode("major"));
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);
91 public void testInvalidTypeOfCorrespondingSchemaNode() throws Exception {
92 final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(EnumValueXPathFunctionTest.class,
93 "/yang-xpath-functions-test/enum-value-function/foo-invalid.yang");
94 assertNotNull(schemaContext);
96 final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
97 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("major"));
99 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
100 converterBiMap.put("foo-prefix", fooModule);
102 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
103 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
105 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
106 buildPathToSeverityLeafNode("major"));
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);
115 public void testInvalidNormalizedNodeValueType() throws Exception {
116 final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(EnumValueXPathFunctionTest.class,
117 "/yang-xpath-functions-test/enum-value-function/foo.yang");
118 assertNotNull(schemaContext);
120 final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
121 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(100));
123 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
124 converterBiMap.put("foo-prefix", fooModule);
126 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
127 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
129 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
130 buildPathToSeverityLeafNode(100));
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);
139 public void shouldFailOnUnknownEnumNodeValue() throws Exception {
140 final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(EnumValueXPathFunctionTest.class,
141 "/yang-xpath-functions-test/enum-value-function/foo.yang");
142 assertNotNull(schemaContext);
144 final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
145 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("unknown"));
147 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
148 converterBiMap.put("foo-prefix", fooModule);
150 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
151 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
153 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
154 buildPathToSeverityLeafNode("unknown"));
156 final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
157 .getFunction(null, null, "enum-value");
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"));
167 public void shouldFailOnInvalidNumberOfArguments() throws Exception {
168 final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
169 final Function enumValueFunction = yangFunctionContext.getFunction(null, null, "enum-value");
171 final Context mockedContext = mock(Context.class);
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());
181 private static ContainerNode buildMyContainerNode(final Object keyLeafValue) {
182 final LeafNode<?> ordinaryLeafNode = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(ordinaryLeaf))
183 .withValue("test-value").build();
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();
190 final ContainerNode myContainerNode = Builders.containerBuilder().withNodeIdentifier(
191 new NodeIdentifier(myContainer)).withChild(alarmListNode).build();
192 return myContainerNode;
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();
199 final YangInstanceIdentifier path = YangInstanceIdentifier.of(alarm)
200 .node(new NodeIdentifierWithPredicates(alarm, keys)).node(severity);