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
8 package org.opendaylight.yangtools.yang.data.jaxen;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Mockito.mock;
16 import com.google.common.collect.BiMap;
17 import com.google.common.collect.HashBiMap;
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableMap;
20 import com.google.common.collect.Maps;
22 import org.jaxen.Context;
23 import org.jaxen.Function;
24 import org.jaxen.FunctionCallException;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.common.Revision;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathDocument;
36 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathSchemaContext;
37 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
41 public class EnumValueXPathFunctionTest {
43 private static final JaxenSchemaContextFactory SCHEMA_CONTEXT_FACTORY = new JaxenSchemaContextFactory();
44 private static final QNameModule FOO_MODULE = QNameModule.create(URI.create("foo-ns"), Revision.of("2017-04-03"));
45 private static final QName MY_CONTAINER = QName.create(FOO_MODULE, "my-container");
46 private static final QName ALARM = QName.create(FOO_MODULE, "alarm");
47 private static final QName SEVERITY = QName.create(FOO_MODULE, "severity");
48 private static final QName ORDINARY_LEAF = QName.create(FOO_MODULE, "ordinary-leaf");
51 public void testEnumValueFunction() throws Exception {
52 final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(EnumValueXPathFunctionTest.class,
53 "/yang-xpath-functions-test/enum-value-function/foo.yang");
54 assertNotNull(schemaContext);
56 final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
57 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("major"));
59 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
60 converterBiMap.put("foo-prefix", FOO_MODULE);
62 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
63 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
65 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
66 buildPathToSeverityLeafNode("major"));
68 final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
69 .getFunction(null, null, "enum-value");
70 final int enumValueResult = (int) enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
71 assertEquals(5, enumValueResult);
75 public void testInvalidTypeOfCorrespondingSchemaNode() throws Exception {
76 final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(EnumValueXPathFunctionTest.class,
77 "/yang-xpath-functions-test/enum-value-function/foo-invalid.yang");
78 assertNotNull(schemaContext);
80 final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
81 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("major"));
83 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
84 converterBiMap.put("foo-prefix", FOO_MODULE);
86 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
87 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
89 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
90 buildPathToSeverityLeafNode("major"));
92 final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
93 .getFunction(null, null, "enum-value");
94 final Double enumValueResult = (Double) enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
95 assertEquals(Double.NaN, enumValueResult, 0.001);
99 public void testInvalidNormalizedNodeValueType() throws Exception {
100 final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(EnumValueXPathFunctionTest.class,
101 "/yang-xpath-functions-test/enum-value-function/foo.yang");
102 assertNotNull(schemaContext);
104 final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
105 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(100));
107 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
108 converterBiMap.put("foo-prefix", FOO_MODULE);
110 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
111 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
113 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
114 buildPathToSeverityLeafNode(100));
116 final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
117 .getFunction(null, null, "enum-value");
118 final Double enumValueResult = (Double) enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
119 assertEquals(Double.NaN, enumValueResult, 0.001);
123 public void shouldFailOnUnknownEnumNodeValue() throws Exception {
124 final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(EnumValueXPathFunctionTest.class,
125 "/yang-xpath-functions-test/enum-value-function/foo.yang");
126 assertNotNull(schemaContext);
128 final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
129 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("unknown"));
131 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
132 converterBiMap.put("foo-prefix", FOO_MODULE);
134 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
135 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
137 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
138 buildPathToSeverityLeafNode("unknown"));
140 final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
141 .getFunction(null, null, "enum-value");
143 enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
144 fail("Function call should have failed on unknown enum node value");
145 } catch (final IllegalStateException ex) {
146 assertTrue(ex.getMessage().startsWith("Enum unknown does not belong to enumeration"));
151 public void shouldFailOnInvalidNumberOfArguments() throws Exception {
152 final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
153 final Function enumValueFunction = yangFunctionContext.getFunction(null, null, "enum-value");
155 final Context mockedContext = mock(Context.class);
158 enumValueFunction.call(mockedContext, ImmutableList.of("should not be here"));
159 fail("Function call should have failed on invalid number of arguments.");
160 } catch (final FunctionCallException ex) {
161 assertEquals("enum-value() takes one argument: node-set nodes.", ex.getMessage());
165 private static ContainerNode buildMyContainerNode(final Object keyLeafValue) {
166 final LeafNode<?> ordinaryLeafNode = Builders.leafBuilder()
167 .withNodeIdentifier(new NodeIdentifier(ORDINARY_LEAF)).withValue("test-value").build();
169 final MapNode alarmListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(ALARM))
170 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
171 NodeIdentifierWithPredicates.of(ALARM, SEVERITY, keyLeafValue))
172 .withChild(ordinaryLeafNode).build()).build();
174 final ContainerNode myContainerNode = Builders.containerBuilder().withNodeIdentifier(
175 new NodeIdentifier(MY_CONTAINER)).withChild(alarmListNode).build();
176 return myContainerNode;
179 private static YangInstanceIdentifier buildPathToSeverityLeafNode(final Object keyLeafValue) {
180 final ImmutableMap.Builder<QName, Object> builder = ImmutableMap.builder();
181 final ImmutableMap<QName, Object> keys = builder.put(SEVERITY, keyLeafValue).build();
183 final YangInstanceIdentifier path = YangInstanceIdentifier.of(ALARM)
184 .node(NodeIdentifierWithPredicates.of(ALARM, keys)).node(SEVERITY);