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.assertFalse;
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;
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.ImmutableSet;
22 import com.google.common.collect.Maps;
24 import org.jaxen.Context;
25 import org.jaxen.Function;
26 import org.jaxen.FunctionCallException;
27 import org.junit.Test;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.QNameModule;
30 import org.opendaylight.yangtools.yang.common.Revision;
31 import org.opendaylight.yangtools.yang.common.XMLNamespace;
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.SystemMapNode;
38 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
39 import org.opendaylight.yangtools.yang.data.jaxen.api.XPathDocument;
40 import org.opendaylight.yangtools.yang.data.jaxen.api.XPathSchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
42 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44 public class BitIsSetXPathFunctionTest {
46 private static final JaxenSchemaContextFactory SCHEMA_CONTEXT_FACTORY = new JaxenSchemaContextFactory();
47 private static final QNameModule FOO_MODULE =
48 QNameModule.create(XMLNamespace.of("foo-ns"), Revision.of("2017-04-03"));
49 private static final QName MY_CONTAINER = QName.create(FOO_MODULE, "my-container");
50 private static final QName MY_LIST = QName.create(FOO_MODULE, "my-list");
51 private static final QName FLAGS = QName.create(FOO_MODULE, "flags");
52 private static final QName ORDINARY_LEAF = QName.create(FOO_MODULE, "ordinary-leaf");
55 public void testBitIsSetFunction() throws Exception {
56 final Set<String> setOfBits = ImmutableSet.of("UP", "PROMISCUOUS");
58 final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResources(
59 BitIsSetXPathFunctionTest.class, "/yang-xpath-functions-test/bit-is-set-function/foo.yang");
60 assertNotNull(schemaContext);
62 final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
63 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(setOfBits));
65 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
66 converterBiMap.put("foo-prefix", FOO_MODULE);
68 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
69 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
71 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
72 buildPathToFlagsLeafNode(setOfBits));
74 final Function bitIsSetFunction = normalizedNodeContextSupport.getFunctionContext()
75 .getFunction(null, null, "bit-is-set");
76 boolean bitIsSetResult = (boolean) bitIsSetFunction.call(normalizedNodeContext, ImmutableList.of("UP"));
77 assertTrue(bitIsSetResult);
78 bitIsSetResult = (boolean) bitIsSetFunction.call(normalizedNodeContext, ImmutableList.of("PROMISCUOUS"));
79 assertTrue(bitIsSetResult);
80 bitIsSetResult = (boolean) bitIsSetFunction.call(normalizedNodeContext, ImmutableList.of("DISABLED"));
81 assertFalse(bitIsSetResult);
85 public void testInvalidTypeOfCorrespondingSchemaNode() throws Exception {
86 final Set<String> setOfBits = ImmutableSet.of("UP", "PROMISCUOUS");
88 final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResources(
89 BitIsSetXPathFunctionTest.class, "/yang-xpath-functions-test/bit-is-set-function/foo-invalid.yang");
90 assertNotNull(schemaContext);
92 final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
93 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(setOfBits));
95 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
96 converterBiMap.put("foo-prefix", FOO_MODULE);
98 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
99 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
101 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
102 buildPathToFlagsLeafNode(setOfBits));
104 final Function bitIsSetFunction = normalizedNodeContextSupport.getFunctionContext()
105 .getFunction(null, null, "bit-is-set");
106 boolean bitIsSetResult = (boolean) bitIsSetFunction.call(normalizedNodeContext, ImmutableList.of("UP"));
107 assertFalse(bitIsSetResult);
111 public void testInvalidNormalizedNodeValueType() throws Exception {
112 final String invalidNodeValueType = "value of invalid type";
114 final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResources(
115 BitIsSetXPathFunctionTest.class, "/yang-xpath-functions-test/bit-is-set-function/foo.yang");
116 assertNotNull(schemaContext);
118 final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
119 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(
120 invalidNodeValueType));
122 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
123 converterBiMap.put("foo-prefix", FOO_MODULE);
125 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
126 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
128 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
129 buildPathToFlagsLeafNode(invalidNodeValueType));
131 final Function bitIsSetFunction = normalizedNodeContextSupport.getFunctionContext()
132 .getFunction(null, null, "bit-is-set");
133 boolean bitIsSetResult = (boolean) bitIsSetFunction.call(normalizedNodeContext, ImmutableList.of("UP"));
134 assertFalse(bitIsSetResult);
138 public void shouldFailOnUnknownBitArgument() throws Exception {
139 final Set<String> setOfBits = ImmutableSet.of("UP", "PROMISCUOUS");
141 final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResources(
142 BitIsSetXPathFunctionTest.class, "/yang-xpath-functions-test/bit-is-set-function/foo.yang");
143 assertNotNull(schemaContext);
145 final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
146 final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(setOfBits));
148 final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
149 converterBiMap.put("foo-prefix", FOO_MODULE);
151 final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
152 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
154 final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
155 buildPathToFlagsLeafNode(setOfBits));
157 final Function bitIsSetFunction = normalizedNodeContextSupport.getFunctionContext()
158 .getFunction(null, null, "bit-is-set");
160 bitIsSetFunction.call(normalizedNodeContext, ImmutableList.of("UNKNOWN"));
161 fail("Function call should have failed on unknown bit-name argument");
162 } catch (final IllegalStateException ex) {
163 assertTrue(ex.getMessage().startsWith("Bit UNKNOWN does not belong to bits"));
168 public void shouldFailOnInvalidNumberOfArguments() throws Exception {
169 final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
170 final Function bitIsSetFunction = yangFunctionContext.getFunction(null, null, "bit-is-set");
172 final Context mockedContext = mock(Context.class);
175 bitIsSetFunction.call(mockedContext, ImmutableList.of("bit-a", "bit-b"));
176 fail("Function call should have failed on invalid number of arguments.");
177 } catch (final FunctionCallException ex) {
178 assertEquals("bit-is-set() takes two arguments: node-set nodes, string bit-name", ex.getMessage());
183 public void shouldFailOnInvalidTypeOfArgument() 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(100));
191 fail("Function call should have failed on invalid type of the bit-name argument.");
192 } catch (final FunctionCallException ex) {
193 assertEquals("Argument bit-name of bit-is-set() function should be a String", ex.getMessage());
197 private static ContainerNode buildMyContainerNode(final Object keyLeafValue) {
198 final LeafNode<?> ordinaryLeafNode = Builders.leafBuilder()
199 .withNodeIdentifier(new NodeIdentifier(ORDINARY_LEAF)).withValue("test-value").build();
201 final SystemMapNode myListNode = Builders.mapBuilder()
202 .withNodeIdentifier(new NodeIdentifier(MY_LIST))
203 .withChild(Builders.mapEntryBuilder().withNodeIdentifier(
204 NodeIdentifierWithPredicates.of(MY_LIST, FLAGS, keyLeafValue))
205 .withChild(ordinaryLeafNode).build()).build();
207 final ContainerNode myContainerNode = Builders.containerBuilder().withNodeIdentifier(
208 new NodeIdentifier(MY_CONTAINER)).withChild(myListNode).build();
210 return myContainerNode;
213 private static YangInstanceIdentifier buildPathToFlagsLeafNode(final Object keyLeafValue) {
214 final ImmutableMap.Builder<QName, Object> builder = ImmutableMap.builder();
215 final ImmutableMap<QName, Object> keys = builder.put(FLAGS, keyLeafValue).build();
217 final YangInstanceIdentifier path = YangInstanceIdentifier.of(MY_LIST)
218 .node(NodeIdentifierWithPredicates.of(MY_LIST, keys)).node(FLAGS);