BUG-4688: switch revisions from Date to Revision
[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
9 package org.opendaylight.yangtools.yang.data.jaxen;
10
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;
16
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;
22 import java.net.URI;
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;
43
44 public class EnumValueXPathFunctionTest {
45
46     private static JaxenSchemaContextFactory jaxenSchemaContextFactory;
47
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;
53
54     @BeforeClass
55     public static void setup() throws ParseException {
56         jaxenSchemaContextFactory = new JaxenSchemaContextFactory();
57
58         fooModule = QNameModule.create(URI.create("foo-ns"), Revision.valueOf("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");
63     }
64
65     @Test
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);
70
71         final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
72         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("major"));
73
74         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
75         converterBiMap.put("foo-prefix", fooModule);
76
77         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
78                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
79
80         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
81                 buildPathToSeverityLeafNode("major"));
82
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);
87     }
88
89     @Test
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);
94
95         final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
96         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("major"));
97
98         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
99         converterBiMap.put("foo-prefix", fooModule);
100
101         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
102                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
103
104         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
105                 buildPathToSeverityLeafNode("major"));
106
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);
111     }
112
113     @Test
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);
118
119         final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
120         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(100));
121
122         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
123         converterBiMap.put("foo-prefix", fooModule);
124
125         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
126                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
127
128         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
129                 buildPathToSeverityLeafNode(100));
130
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);
135     }
136
137     @Test
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);
142
143         final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
144         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode("unknown"));
145
146         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
147         converterBiMap.put("foo-prefix", fooModule);
148
149         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
150                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
151
152         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
153                 buildPathToSeverityLeafNode("unknown"));
154
155         final Function enumValueFunction = normalizedNodeContextSupport.getFunctionContext()
156                 .getFunction(null, null, "enum-value");
157         try {
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"));
162         }
163     }
164
165     @Test
166     public void shouldFailOnInvalidNumberOfArguments() throws Exception {
167         final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
168         final Function enumValueFunction = yangFunctionContext.getFunction(null, null, "enum-value");
169
170         final Context mockedContext = mock(Context.class);
171
172         try {
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());
177         }
178     }
179
180     private static ContainerNode buildMyContainerNode(final Object keyLeafValue) {
181         final LeafNode<?> ordinaryLeafNode = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(ordinaryLeaf))
182                 .withValue("test-value").build();
183
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();
188
189         final ContainerNode myContainerNode = Builders.containerBuilder().withNodeIdentifier(
190                 new NodeIdentifier(myContainer)).withChild(alarmListNode).build();
191         return myContainerNode;
192     }
193
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();
197
198         final YangInstanceIdentifier path = YangInstanceIdentifier.of(alarm)
199                 .node(new NodeIdentifierWithPredicates(alarm, keys)).node(severity);
200         return path;
201     }
202 }