0bd928d1a848b4abbdf35faf37a669fa014b28b3
[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 package org.opendaylight.yangtools.yang.data.jaxen;
9
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;
15
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 java.net.URI;
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.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.SchemaContext;
39 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
40
41 public class EnumValueXPathFunctionTest {
42
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");
49
50     @Test
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);
55
56         final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
57         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("major"));
58
59         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
60         converterBiMap.put("foo-prefix", FOO_MODULE);
61
62         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
63                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
64
65         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
66                 buildPathToSeverityLeafNode("major"));
67
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);
72     }
73
74     @Test
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);
79
80         final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
81         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("major"));
82
83         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
84         converterBiMap.put("foo-prefix", FOO_MODULE);
85
86         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
87                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
88
89         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
90                 buildPathToSeverityLeafNode("major"));
91
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);
96     }
97
98     @Test
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);
103
104         final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
105         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(100));
106
107         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
108         converterBiMap.put("foo-prefix", FOO_MODULE);
109
110         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
111                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
112
113         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
114                 buildPathToSeverityLeafNode(100));
115
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);
120     }
121
122     @Test
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);
127
128         final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
129         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("unknown"));
130
131         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
132         converterBiMap.put("foo-prefix", FOO_MODULE);
133
134         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
135                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
136
137         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
138                 buildPathToSeverityLeafNode("unknown"));
139
140         final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
141                 .getFunction(null, null, "enum-value");
142         try {
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"));
147         }
148     }
149
150     @Test
151     public void shouldFailOnInvalidNumberOfArguments() throws Exception {
152         final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
153         final Function enumValueFunction = yangFunctionContext.getFunction(null, null, "enum-value");
154
155         final Context mockedContext = mock(Context.class);
156
157         try {
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());
162         }
163     }
164
165     private static ContainerNode buildMyContainerNode(final Object keyLeafValue) {
166         final LeafNode<?> ordinaryLeafNode = Builders.leafBuilder()
167                 .withNodeIdentifier(new NodeIdentifier(ORDINARY_LEAF)).withValue("test-value").build();
168
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();
173
174         final ContainerNode myContainerNode = Builders.containerBuilder().withNodeIdentifier(
175                 new NodeIdentifier(MY_CONTAINER)).withChild(alarmListNode).build();
176         return myContainerNode;
177     }
178
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();
182
183         final YangInstanceIdentifier path = YangInstanceIdentifier.of(ALARM)
184                 .node(NodeIdentifierWithPredicates.of(ALARM, keys)).node(SEVERITY);
185         return path;
186     }
187 }