Remove deprecated Yin/YangStatementSourceImpl
[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.parseYangResources(BitIsSetXPathFunctionTest.class,
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.parseYangResources(BitIsSetXPathFunctionTest.class,
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.parseYangResources(BitIsSetXPathFunctionTest.class,
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(
135                     invalidNodeValueType));
136
137         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
138         converterBiMap.put("foo-prefix", fooModule);
139
140         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
141                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
142
143         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
144                 buildPathToFlagsLeafNode(invalidNodeValueType));
145
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);
150     }
151
152     @Test
153     public void shouldFailOnUnknownBitArgument() throws Exception {
154         final Set<String> setOfBits = ImmutableSet.of("UP", "PROMISCUOUS");
155
156         final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(BitIsSetXPathFunctionTest.class,
157                 "/yang-xpath-functions-test/bit-is-set-function/foo.yang");
158         assertNotNull(schemaContext);
159
160         final XPathSchemaContext jaxenSchemaContext = jaxenSchemaContextFactory.createContext(schemaContext);
161         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(setOfBits));
162
163         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
164         converterBiMap.put("foo-prefix", fooModule);
165
166         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
167                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
168
169         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
170                 buildPathToFlagsLeafNode(setOfBits));
171
172         final Function bitIsSetFunction = normalizedNodeContextSupport.getFunctionContext()
173                 .getFunction(null, null, "bit-is-set");
174         try {
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"));
179         }
180     }
181
182     @Test
183     public void shouldFailOnInvalidNumberOfArguments() throws Exception {
184         final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
185         final Function bitIsSetFunction = yangFunctionContext.getFunction(null, null, "bit-is-set");
186
187         final Context mockedContext = mock(Context.class);
188
189         try {
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());
194         }
195     }
196
197     @Test
198     public void shouldFailOnInvalidTypeOfArgument() throws Exception {
199         final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
200         final Function bitIsSetFunction = yangFunctionContext.getFunction(null, null, "bit-is-set");
201
202         final Context mockedContext = mock(Context.class);
203
204         try {
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());
209         }
210     }
211
212     private static ContainerNode buildMyContainerNode(final Object keyLeafValue) {
213         final LeafNode<?> ordinaryLeafNode = Builders.leafBuilder().withNodeIdentifier(new NodeIdentifier(ordinaryLeaf))
214                 .withValue("test-value").build();
215
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();
220
221         final ContainerNode myContainerNode = Builders.containerBuilder().withNodeIdentifier(
222                 new NodeIdentifier(myContainer)).withChild(myListNode).build();
223
224         return myContainerNode;
225     }
226
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();
230
231         final YangInstanceIdentifier path = YangInstanceIdentifier.of(myList)
232                 .node(new NodeIdentifierWithPredicates(myList, keys)).node(flags);
233         return path;
234     }
235 }