YANG XPath functions - unit tests and bugfix
[yangtools.git] / yang / yang-data-jaxen / src / test / java / org / opendaylight / yangtools / yang / data / jaxen / BitIsSetXPathFunctionTest.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.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16 import static org.mockito.Mockito.mock;
17
18 import com.google.common.collect.BiMap;
19 import com.google.common.collect.HashBiMap;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.ImmutableMap;
22 import com.google.common.collect.ImmutableSet;
23 import com.google.common.collect.Maps;
24 import java.net.URI;
25 import java.text.ParseException;
26 import java.util.Set;
27 import org.jaxen.Context;
28 import org.jaxen.Function;
29 import org.jaxen.FunctionCallException;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.QNameModule;
34 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathDocument;
42 import org.opendaylight.yangtools.yang.data.api.schema.xpath.XPathSchemaContext;
43 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
46
47 public class BitIsSetXPathFunctionTest {
48
49     private static JaxenSchemaContextFactory jaxenSchemaContextFactory;
50
51     private static QNameModule fooModule;
52     private static QName myContainer; 
53     private static QName myList;
54     private static QName flags;
55     private static QName ordinaryLeaf;
56
57     @BeforeClass
58     public static void setup() throws ParseException {
59         jaxenSchemaContextFactory = new JaxenSchemaContextFactory();
60
61         fooModule = QNameModule.create(URI.create("foo-ns"),
62                 SimpleDateFormatUtil.getRevisionFormat().parse("2017-04-03"));
63         myContainer = QName.create(fooModule, "my-container");
64         myList = QName.create(fooModule, "my-list");
65         flags = QName.create(fooModule, "flags");
66         ordinaryLeaf = QName.create(fooModule, "ordinary-leaf");
67     }
68
69     @Test
70     public void testBitIsSetFunction() throws Exception {
71         final Set<String> setOfBits = ImmutableSet.of("UP", "PROMISCUOUS");
72
73         final SchemaContext schemaContext = YangParserTestUtils.parseYangSource(
74                 "/yang-xpath-functions-test/bit-is-set-function/foo.yang");
75         assertNotNull(schemaContext);
76
77         final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
78         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(setOfBits));
79
80         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
81         converterBiMap.put("foo-prefix", fooModule);
82
83         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
84                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
85
86         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
87                 buildPathToFlagsLeafNode(setOfBits));
88
89         final Function bitIsSetFunction = normalizedNodeContextSupport.getFunctionContext()
90                 .getFunction(null, null, "bit-is-set");
91         boolean bitIsSetResult = (boolean) bitIsSetFunction.call(normalizedNodeContext, ImmutableList.of("UP"));
92         assertTrue(bitIsSetResult);
93         bitIsSetResult = (boolean) bitIsSetFunction.call(normalizedNodeContext, ImmutableList.of("PROMISCUOUS"));
94         assertTrue(bitIsSetResult);
95         bitIsSetResult = (boolean) bitIsSetFunction.call(normalizedNodeContext, ImmutableList.of("DISABLED"));
96         assertFalse(bitIsSetResult);
97     }
98
99     @Test
100     public void testInvalidTypeOfCorrespondingSchemaNode() throws Exception {
101         final Set<String> setOfBits = ImmutableSet.of("UP", "PROMISCUOUS");
102
103         final SchemaContext schemaContext = YangParserTestUtils.parseYangSource(
104                 "/yang-xpath-functions-test/bit-is-set-function/foo-invalid.yang");
105         assertNotNull(schemaContext);
106
107         final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
108         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(setOfBits));
109
110         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
111         converterBiMap.put("foo-prefix", fooModule);
112
113         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
114                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
115
116         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
117                 buildPathToFlagsLeafNode(setOfBits));
118
119         final Function bitIsSetFunction = normalizedNodeContextSupport.getFunctionContext()
120                 .getFunction(null, null, "bit-is-set");
121         boolean bitIsSetResult = (boolean) bitIsSetFunction.call(normalizedNodeContext, ImmutableList.of("UP"));
122         assertFalse(bitIsSetResult);
123     }
124
125     @Test
126     public void testInvalidNormalizedNodeValueType() throws Exception {
127         final String invalidNodeValueType = "value of invalid type";
128
129         final SchemaContext schemaContext = YangParserTestUtils.parseYangSource(
130                 "/yang-xpath-functions-test/bit-is-set-function/foo.yang");
131         assertNotNull(schemaContext);
132
133         final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
134         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(invalidNodeValueType));
135
136         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
137         converterBiMap.put("foo-prefix", fooModule);
138
139         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
140                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
141
142         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
143                 buildPathToFlagsLeafNode(invalidNodeValueType));
144
145         final Function bitIsSetFunction = normalizedNodeContextSupport.getFunctionContext()
146                 .getFunction(null, null, "bit-is-set");
147         boolean bitIsSetResult = (boolean) bitIsSetFunction.call(normalizedNodeContext, ImmutableList.of("UP"));
148         assertFalse(bitIsSetResult);
149     }
150
151     @Test
152     public void shouldFailOnUnknownBitArgument() throws Exception {
153         final Set<String> setOfBits = ImmutableSet.of("UP", "PROMISCUOUS");
154
155         final SchemaContext schemaContext = YangParserTestUtils.parseYangSource(
156                 "/yang-xpath-functions-test/bit-is-set-function/foo.yang");
157         assertNotNull(schemaContext);
158
159         final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
160         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(setOfBits));
161
162         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
163         converterBiMap.put("foo-prefix", fooModule);
164
165         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
166                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
167
168         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
169                 buildPathToFlagsLeafNode(setOfBits));
170
171         final Function bitIsSetFunction = normalizedNodeContextSupport.getFunctionContext()
172                 .getFunction(null, null, "bit-is-set");
173         try {
174             bitIsSetFunction.call(normalizedNodeContext, ImmutableList.of("UNKNOWN"));
175             fail("Function call should have failed on unknown bit-name argument");
176         } catch (final IllegalStateException ex) {
177             assertTrue(ex.getMessage().startsWith("Bit UNKNOWN does not belong to bits"));
178         }
179     }
180
181     @Test
182     public void shouldFailOnInvalidNumberOfArguments() throws Exception {
183         final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
184         final Function bitIsSetFunction = yangFunctionContext.getFunction(null, null, "bit-is-set");
185
186         final Context mockedContext = mock(Context.class);
187
188         try {
189             bitIsSetFunction.call(mockedContext, ImmutableList.of("bit-a", "bit-b"));
190             fail("Function call should have failed on invalid number of arguments.");
191         } catch (final FunctionCallException ex) {
192             assertEquals("bit-is-set() takes two arguments: node-set nodes, string bit-name", ex.getMessage());
193         }
194     }
195
196     @Test
197     public void shouldFailOnInvalidTypeOfArgument() throws Exception {
198         final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
199         final Function bitIsSetFunction = yangFunctionContext.getFunction(null, null, "bit-is-set");
200
201         final Context mockedContext = mock(Context.class);
202
203         try {
204             bitIsSetFunction.call(mockedContext, ImmutableList.of(100));
205             fail("Function call should have failed on invalid type of the bit-name argument.");
206         } catch (final FunctionCallException ex) {
207             assertEquals("Argument bit-name of bit-is-set() function should be a String", ex.getMessage());
208         }
209     }
210
211     private static ContainerNode buildMyContainerNode(final Object keyLeafValue) {
212         final LeafNode<?> ordinaryLeafNode = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(ordinaryLeaf))
213                 .withValue("test-value").build();
214
215         final MapNode myListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(myList))
216                 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
217                         new NodeIdentifierWithPredicates(myList, flags, keyLeafValue))
218                         .withChild(ordinaryLeafNode).build()).build();
219
220         final ContainerNode myContainerNode = Builders.containerBuilder().withNodeIdentifier(
221                 new NodeIdentifier(myContainer)).withChild(myListNode).build();
222
223         return myContainerNode;
224     }
225
226     private static YangInstanceIdentifier buildPathToFlagsLeafNode(final Object keyLeafValue) {
227         final ImmutableMap.Builder<QName, Object> builder = ImmutableMap.builder();
228         final ImmutableMap<QName, Object> keys = builder.put(flags, keyLeafValue).build();
229
230         final YangInstanceIdentifier path = YangInstanceIdentifier.of(myList)
231                 .node(new NodeIdentifierWithPredicates(myList, keys)).node(flags);
232         return path;
233     }
234 }