YANG XPath functions - unit tests and bugfix
[yangtools.git] / yang / yang-data-jaxen / src / test / java / org / opendaylight / yangtools / yang / data / jaxen / ReMatchXPathFunctionTest.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.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Mockito.mock;
16
17 import com.google.common.collect.ImmutableList;
18 import org.jaxen.Context;
19 import org.jaxen.Function;
20 import org.jaxen.FunctionCallException;
21 import org.junit.Test;
22
23 public class ReMatchXPathFunctionTest {
24
25     @Test
26     public void testRematchFunction() throws Exception {
27         // re-match() uses regex processing from yang-parser-impl which has been thoroughly tested within
28         // the Bug5410Test unit test class, so here is just a basic test
29         final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
30         final Function rematchFunction = yangFunctionContext.getFunction(null, null, "re-match");
31
32         final Context mockedContext = mock(Context.class);
33
34         boolean rematchResult = (boolean) rematchFunction.call(mockedContext, ImmutableList.of("abbc", "[abc]{1,4}"));
35         assertTrue(rematchResult);
36         rematchResult = (boolean) rematchFunction.call(mockedContext, ImmutableList.of("abbcc", "[abc]{1,4}"));
37         assertFalse(rematchResult);
38     }
39
40     @Test
41     public void shouldFailOnInvalidNumberOfArguments() throws Exception {
42         final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
43         final Function rematchFunction = yangFunctionContext.getFunction(null, null, "re-match");
44
45         final Context mockedContext = mock(Context.class);
46
47         try {
48             rematchFunction.call(mockedContext, ImmutableList.of("abbc", "[abc]{1,4}", "should not be here"));
49             fail("Function call should have failed on invalid number of arguments.");
50         } catch (final FunctionCallException ex) {
51             assertEquals("re-match() takes two arguments: string subject, string pattern.", ex.getMessage());
52         }
53     }
54
55     @Test
56     public void shouldFailOnInvalidTypeOfArgument() throws Exception {
57         final YangFunctionContext yangFunctionContext = YangFunctionContext.getInstance();
58         final Function rematchFunction = yangFunctionContext.getFunction(null, null, "re-match");
59
60         final Context mockedContext = mock(Context.class);
61
62         try {
63             rematchFunction.call(mockedContext, ImmutableList.of(100, "[abc]{1,4}"));
64             fail("Function call should have failed on invalid type of the subject argument.");
65         } catch (final FunctionCallException ex) {
66             assertEquals("First argument of re-match() should be a String.", ex.getMessage());
67         }
68
69         try {
70             rematchFunction.call(mockedContext, ImmutableList.of("abbc", 100));
71             fail("Function call should have failed on invalid type of the pattern argument.");
72         } catch (final FunctionCallException ex) {
73             assertEquals("Second argument of re-match() should be a String.", ex.getMessage());
74         }
75     }
76 }