Cleanup more tests
[yangtools.git] / attic / 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 package org.opendaylight.yangtools.yang.data.jaxen;
9
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;
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.ImmutableSet;
22 import com.google.common.collect.Maps;
23 import java.util.Set;
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;
43
44 public class BitIsSetXPathFunctionTest {
45
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");
53
54     @Test
55     public void testBitIsSetFunction() throws Exception {
56         final Set<String> setOfBits = ImmutableSet.of("UP", "PROMISCUOUS");
57
58         final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResources(
59             BitIsSetXPathFunctionTest.class, "/yang-xpath-functions-test/bit-is-set-function/foo.yang");
60         assertNotNull(schemaContext);
61
62         final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
63         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(setOfBits));
64
65         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
66         converterBiMap.put("foo-prefix", FOO_MODULE);
67
68         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
69                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
70
71         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
72                 buildPathToFlagsLeafNode(setOfBits));
73
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);
82     }
83
84     @Test
85     public void testInvalidTypeOfCorrespondingSchemaNode() throws Exception {
86         final Set<String> setOfBits = ImmutableSet.of("UP", "PROMISCUOUS");
87
88         final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResources(
89             BitIsSetXPathFunctionTest.class, "/yang-xpath-functions-test/bit-is-set-function/foo-invalid.yang");
90         assertNotNull(schemaContext);
91
92         final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
93         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(setOfBits));
94
95         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
96         converterBiMap.put("foo-prefix", FOO_MODULE);
97
98         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
99                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
100
101         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
102                 buildPathToFlagsLeafNode(setOfBits));
103
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);
108     }
109
110     @Test
111     public void testInvalidNormalizedNodeValueType() throws Exception {
112         final String invalidNodeValueType = "value of invalid type";
113
114         final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResources(
115             BitIsSetXPathFunctionTest.class, "/yang-xpath-functions-test/bit-is-set-function/foo.yang");
116         assertNotNull(schemaContext);
117
118         final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
119         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(
120                     invalidNodeValueType));
121
122         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
123         converterBiMap.put("foo-prefix", FOO_MODULE);
124
125         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
126                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
127
128         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
129                 buildPathToFlagsLeafNode(invalidNodeValueType));
130
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);
135     }
136
137     @Test
138     public void shouldFailOnUnknownBitArgument() throws Exception {
139         final Set<String> setOfBits = ImmutableSet.of("UP", "PROMISCUOUS");
140
141         final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResources(
142             BitIsSetXPathFunctionTest.class, "/yang-xpath-functions-test/bit-is-set-function/foo.yang");
143         assertNotNull(schemaContext);
144
145         final XPathSchemaContext jaxenSchemaContext = SCHEMA_CONTEXT_FACTORY.createContext(schemaContext);
146         final XPathDocument jaxenDocument = jaxenSchemaContext.createDocument(buildMyContainerNode(setOfBits));
147
148         final BiMap<String, QNameModule> converterBiMap = HashBiMap.create();
149         converterBiMap.put("foo-prefix", FOO_MODULE);
150
151         final NormalizedNodeContextSupport normalizedNodeContextSupport = NormalizedNodeContextSupport.create(
152                 (JaxenDocument) jaxenDocument, Maps.asConverter(converterBiMap));
153
154         final NormalizedNodeContext normalizedNodeContext = normalizedNodeContextSupport.createContext(
155                 buildPathToFlagsLeafNode(setOfBits));
156
157         final Function bitIsSetFunction = normalizedNodeContextSupport.getFunctionContext()
158                 .getFunction(null, null, "bit-is-set");
159         try {
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"));
164         }
165     }
166
167     @Test
168     public void shouldFailOnInvalidNumberOfArguments() throws Exception {
169         final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
170         final Function bitIsSetFunction = yangFunctionContext.getFunction(null, null, "bit-is-set");
171
172         final Context mockedContext = mock(Context.class);
173
174         try {
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());
179         }
180     }
181
182     @Test
183     public void shouldFailOnInvalidTypeOfArgument() 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(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());
194         }
195     }
196
197     private static ContainerNode buildMyContainerNode(final Object keyLeafValue) {
198         final LeafNode<?> ordinaryLeafNode = Builders.leafBuilder()
199                 .withNodeIdentifier(new NodeIdentifier(ORDINARY_LEAF)).withValue("test-value").build();
200
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();
206
207         final ContainerNode myContainerNode = Builders.containerBuilder().withNodeIdentifier(
208                 new NodeIdentifier(MY_CONTAINER)).withChild(myListNode).build();
209
210         return myContainerNode;
211     }
212
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();
216
217         final YangInstanceIdentifier path = YangInstanceIdentifier.of(MY_LIST)
218                 .node(NodeIdentifierWithPredicates.of(MY_LIST, keys)).node(FLAGS);
219         return path;
220     }
221 }