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;
21 import org.jaxen.Context;
22 import org.jaxen.Function;
23 import org.jaxen.FunctionCallException;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.common.Revision;
28 import org.opendaylight.yangtools.yang.common.XMLNamespace;
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.SystemMapNode;
35 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
36 import org.opendaylight.yangtools.yang.data.jaxen.api.XPathDocument;
37 import org.opendaylight.yangtools.yang.data.jaxen.api.XPathSchemaContext;
38 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
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 =
45 QNameModule.create(XMLNamespace.of("foo-ns"), Revision.of("2017-04-03"));
46 private static final QName MY_CONTAINER = QName.create(FOO_MODULE, "my-container");
47 private static final QName ALARM = QName.create(FOO_MODULE, "alarm");
48 private static final QName SEVERITY = QName.create(FOO_MODULE, "severity");
49 private static final QName ORDINARY_LEAF = QName.create(FOO_MODULE, "ordinary-leaf");
52 public void testEnumValueFunction() throws Exception {
53 final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResources(
54 EnumValueXPathFunctionTest.class, "/yang-xpath-functions-test/enum-value-function/foo.yang");
55 assertNotNull(schemaContext);
57 final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
58 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("major"));
60 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
61 converterBiMap.put("foo-prefix", FOO_MODULE);
63 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
64 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
66 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
67 buildPathToSeverityLeafNode("major"));
69 final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
70 .getFunction(null, null, "enum-value");
71 final int enumValueResult = (int) enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
72 assertEquals(5, enumValueResult);
76 public void testInvalidTypeOfCorrespondingSchemaNode() throws Exception {
77 final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResources(
78 EnumValueXPathFunctionTest.class, "/yang-xpath-functions-test/enum-value-function/foo-invalid.yang");
79 assertNotNull(schemaContext);
81 final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
82 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("major"));
84 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
85 converterBiMap.put("foo-prefix", FOO_MODULE);
87 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
88 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
90 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
91 buildPathToSeverityLeafNode("major"));
93 final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
94 .getFunction(null, null, "enum-value");
95 final Double enumValueResult = (Double) enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
96 assertEquals(Double.NaN, enumValueResult, 0.001);
100 public void testInvalidNormalizedNodeValueType() throws Exception {
101 final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResources(
102 EnumValueXPathFunctionTest.class, "/yang-xpath-functions-test/enum-value-function/foo.yang");
103 assertNotNull(schemaContext);
105 final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
106 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(100));
108 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
109 converterBiMap.put("foo-prefix", FOO_MODULE);
111 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
112 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
114 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
115 buildPathToSeverityLeafNode(100));
117 final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
118 .getFunction(null, null, "enum-value");
119 final Double enumValueResult = (Double) enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
120 assertEquals(Double.NaN, enumValueResult, 0.001);
124 public void shouldFailOnUnknownEnumNodeValue() throws Exception {
125 final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResources(
126 EnumValueXPathFunctionTest.class, "/yang-xpath-functions-test/enum-value-function/foo.yang");
127 assertNotNull(schemaContext);
129 final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
130 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("unknown"));
132 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
133 converterBiMap.put("foo-prefix", FOO_MODULE);
135 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
136 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
138 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
139 buildPathToSeverityLeafNode("unknown"));
141 final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
142 .getFunction(null, null, "enum-value");
144 enumValueFunction.call(normalizedNodeContext, ImmutableList.of());
145 fail("Function call should have failed on unknown enum node value");
146 } catch (final IllegalStateException ex) {
147 assertTrue(ex.getMessage().startsWith("Enum unknown does not belong to enumeration"));
152 public void shouldFailOnInvalidNumberOfArguments() throws Exception {
153 final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
154 final Function enumValueFunction = yangFunctionContext.getFunction(null, null, "enum-value");
156 final Context mockedContext = mock(Context.class);
159 enumValueFunction.call(mockedContext, ImmutableList.of("should not be here"));
160 fail("Function call should have failed on invalid number of arguments.");
161 } catch (final FunctionCallException ex) {
162 assertEquals("enum-value() takes one argument: node-set nodes.", ex.getMessage());
166 private static ContainerNode buildMyContainerNode(final Object keyLeafValue) {
167 final LeafNode<?> ordinaryLeafNode = Builders.leafBuilder()
168 .withNodeIdentifier(new NodeIdentifier(ORDINARY_LEAF)).withValue("test-value").build();
170 final SystemMapNode alarmListNode = Builders.mapBuilder()
171 .withNodeIdentifier(new NodeIdentifier(ALARM))
172 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
173 NodeIdentifierWithPredicates.of(ALARM, SEVERITY, keyLeafValue))
174 .withChild(ordinaryLeafNode).build()).build();
176 final ContainerNode myContainerNode = Builders.containerBuilder().withNodeIdentifier(
177 new NodeIdentifier(MY_CONTAINER)).withChild(alarmListNode).build();
178 return myContainerNode;
181 private static YangInstanceIdentifier buildPathToSeverityLeafNode(final Object keyLeafValue) {
182 final ImmutableMap.Builder<QName, Object> builder = ImmutableMap.builder();
183 final ImmutableMap<QName, Object> keys = builder.put(SEVERITY, keyLeafValue).build();
185 final YangInstanceIdentifier path = YangInstanceIdentifier.of(ALARM)
186 .node(NodeIdentifierWithPredicates.of(ALARM, keys)).node(SEVERITY);