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
9 package org.opendaylight.yangtools.yang.data.jaxen;
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;
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;
25 import java.text.ParseException;
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;
47 public class BitIsSetXPathFunctionTest {
49 private static JaxenSchemaContextFactory jaxenSchemaContextFactory;
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;
58 public static void setup() throws ParseException {
59 jaxenSchemaContextFactory = new JaxenSchemaContextFactory();
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");
70 public void testBitIsSetFunction() throws Exception {
71 final Set<String> setOfBits = ImmutableSet.of("UP", "PROMISCUOUS");
73 final SchemaContext schemaContext = YangParserTestUtils.parseYangSource(
74 "/yang-xpath-functions-test/bit-is-set-function/foo.yang");
75 assertNotNull(schemaContext);
77 final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
78 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(setOfBits));
80 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
81 converterBiMap.put("foo-prefix", fooModule);
83 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
84 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
86 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
87 buildPathToFlagsLeafNode(setOfBits));
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);
100 public void testInvalidTypeOfCorrespondingSchemaNode() throws Exception {
101 final Set<String> setOfBits = ImmutableSet.of("UP", "PROMISCUOUS");
103 final SchemaContext schemaContext = YangParserTestUtils.parseYangSource(
104 "/yang-xpath-functions-test/bit-is-set-function/foo-invalid.yang");
105 assertNotNull(schemaContext);
107 final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
108 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(setOfBits));
110 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
111 converterBiMap.put("foo-prefix", fooModule);
113 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
114 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
116 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
117 buildPathToFlagsLeafNode(setOfBits));
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);
126 public void testInvalidNormalizedNodeValueType() throws Exception {
127 final String invalidNodeValueType = "value of invalid type";
129 final SchemaContext schemaContext = YangParserTestUtils.parseYangSource(
130 "/yang-xpath-functions-test/bit-is-set-function/foo.yang");
131 assertNotNull(schemaContext);
133 final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
134 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(
135 invalidNodeValueType));
137 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
138 converterBiMap.put("foo-prefix", fooModule);
140 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
141 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
143 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
144 buildPathToFlagsLeafNode(invalidNodeValueType));
146 final Function bitIsSetFunction = normalizedNodeContextSupport.getFunctionContext()
147 .getFunction(null, null, "bit-is-set");
148 boolean bitIsSetResult = (boolean) bitIsSetFunction.call(normalizedNodeContext, ImmutableList.of("UP"));
149 assertFalse(bitIsSetResult);
153 public void shouldFailOnUnknownBitArgument() throws Exception {
154 final Set<String> setOfBits = ImmutableSet.of("UP", "PROMISCUOUS");
156 final SchemaContext schemaContext = YangParserTestUtils.parseYangSource(
157 "/yang-xpath-functions-test/bit-is-set-function/foo.yang");
158 assertNotNull(schemaContext);
160 final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
161 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(setOfBits));
163 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
164 converterBiMap.put("foo-prefix", fooModule);
166 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
167 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
169 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
170 buildPathToFlagsLeafNode(setOfBits));
172 final Function bitIsSetFunction = normalizedNodeContextSupport.getFunctionContext()
173 .getFunction(null, null, "bit-is-set");
175 bitIsSetFunction.call(normalizedNodeContext, ImmutableList.of("UNKNOWN"));
176 fail("Function call should have failed on unknown bit-name argument");
177 } catch (final IllegalStateException ex) {
178 assertTrue(ex.getMessage().startsWith("Bit UNKNOWN does not belong to bits"));
183 public void shouldFailOnInvalidNumberOfArguments() throws Exception {
184 final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
185 final Function bitIsSetFunction = yangFunctionContext.getFunction(null, null, "bit-is-set");
187 final Context mockedContext = mock(Context.class);
190 bitIsSetFunction.call(mockedContext, ImmutableList.of("bit-a", "bit-b"));
191 fail("Function call should have failed on invalid number of arguments.");
192 } catch (final FunctionCallException ex) {
193 assertEquals("bit-is-set() takes two arguments: node-set nodes, string bit-name", ex.getMessage());
198 public void shouldFailOnInvalidTypeOfArgument() throws Exception {
199 final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
200 final Function bitIsSetFunction = yangFunctionContext.getFunction(null, null, "bit-is-set");
202 final Context mockedContext = mock(Context.class);
205 bitIsSetFunction.call(mockedContext, ImmutableList.of(100));
206 fail("Function call should have failed on invalid type of the bit-name argument.");
207 } catch (final FunctionCallException ex) {
208 assertEquals("Argument bit-name of bit-is-set() function should be a String", ex.getMessage());
212 private static ContainerNode buildMyContainerNode(final Object keyLeafValue) {
213 final LeafNode<?> ordinaryLeafNode = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(ordinaryLeaf))
214 .withValue("test-value").build();
216 final MapNode myListNode = Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(myList))
217 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
218 new NodeIdentifierWithPredicates(myList, flags, keyLeafValue))
219 .withChild(ordinaryLeafNode).build()).build();
221 final ContainerNode myContainerNode = Builders.containerBuilder().withNodeIdentifier(
222 new NodeIdentifier(myContainer)).withChild(myListNode).build();
224 return myContainerNode;
227 private static YangInstanceIdentifier buildPathToFlagsLeafNode(final Object keyLeafValue) {
228 final ImmutableMap.Builder<QName, Object> builder = ImmutableMap.builder();
229 final ImmutableMap<QName, Object> keys = builder.put(flags, keyLeafValue).build();
231 final YangInstanceIdentifier path = YangInstanceIdentifier.of(myList)
232 .node(new NodeIdentifierWithPredicates(myList, keys)).node(flags);