f4ed8f9f6de81523572609c4f9e5d47bf294f4be
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / ExpressionParserTest.java
1 /*
2  * Copyright (c) 2016 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.controller.sal.restconf.impl.test;
9
10 import java.io.BufferedReader;
11 import java.io.File;
12 import java.io.FileReader;
13 import java.io.IOException;
14 import java.lang.reflect.Method;
15 import java.nio.charset.StandardCharsets;
16 import java.time.Instant;
17 import java.util.Collection;
18 import java.util.Optional;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mockito;
23 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
24 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
25 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
26 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31
32 public class ExpressionParserTest {
33
34     private Collection<File> xmls;
35
36     @Before
37     public void setup() throws Exception {
38         this.xmls = TestRestconfUtils.loadFiles("/notifications/xml/output/");
39     }
40
41     @Test
42     public void trueDownFilterTest() throws Exception {
43         final boolean parser =
44                 parser("notification/data-changed-notification/data-change-event/data/toasterStatus='down'",
45                         "data_change_notification_toaster_status_DOWN.xml");
46         Assert.assertTrue(parser);
47     }
48
49     @Test
50     public void falseDownFilterTest() throws Exception {
51         final boolean parser =
52                 parser("notification/data-changed-notification/data-change-event/data/toasterStatus='up'",
53                         "data_change_notification_toaster_status_DOWN.xml");
54         Assert.assertFalse(parser);
55     }
56
57     @Test
58     public void trueNumberEqualsFilterTest() throws Exception {
59         final boolean parser = parser(
60                 "notification/data-changed-notification/data-change-event/data/toasterStatus=1",
61                 "data_change_notification_toaster_status_NUMBER.xml");
62         Assert.assertTrue(parser);
63     }
64
65     @Test
66     public void falseNumberEqualsFilterTest() throws Exception {
67         final boolean parser = parser("notification/data-changed-notification/data-change-event/data/toasterStatus=0",
68                 "data_change_notification_toaster_status_NUMBER.xml");
69         Assert.assertFalse(parser);
70     }
71
72     @Test
73     public void trueNumberLessFilterTest() throws Exception {
74         final boolean parser = parser("notification/data-changed-notification/data-change-event/data/toasterStatus<2",
75                 "data_change_notification_toaster_status_NUMBER.xml");
76         Assert.assertTrue(parser);
77     }
78
79     @Test
80     public void falseNumberLessFilterTest() throws Exception {
81         final boolean parser = parser("notification/data-changed-notification/data-change-event/data/toasterStatus<0",
82                 "data_change_notification_toaster_status_NUMBER.xml");
83         Assert.assertFalse(parser);
84     }
85
86     @Test
87     public void trueNumberLessEqualsFilterTest() throws Exception {
88         final boolean parser = parser("notification/data-changed-notification/data-change-event/data/toasterStatus<=2",
89                 "data_change_notification_toaster_status_NUMBER.xml");
90         Assert.assertTrue(parser);
91     }
92
93     @Test
94     public void falseNumberLessEqualsFilterTest() throws Exception {
95         final boolean parser = parser("notification/data-changed-notification/data-change-event/data/toasterStatus<=-1",
96                 "data_change_notification_toaster_status_NUMBER.xml");
97         Assert.assertFalse(parser);
98     }
99
100     @Test
101     public void trueNumberGreaterFilterTest() throws Exception {
102         final boolean parser = parser("notification/data-changed-notification/data-change-event/data/toasterStatus>0",
103                 "data_change_notification_toaster_status_NUMBER.xml");
104         Assert.assertTrue(parser);
105     }
106
107     @Test
108     public void falseNumberGreaterFilterTest() throws Exception {
109         final boolean parser = parser("notification/data-changed-notification/data-change-event/data/toasterStatus>5",
110                 "data_change_notification_toaster_status_NUMBER.xml");
111         Assert.assertFalse(parser);
112     }
113
114     @Test
115     public void trueNumberGreaterEqualsFilterTest() throws Exception {
116         final boolean parser = parser("notification/data-changed-notification/data-change-event/data/toasterStatus>=0",
117                 "data_change_notification_toaster_status_NUMBER.xml");
118         Assert.assertTrue(parser);
119     }
120
121     @Test
122     public void falseNumberGreaterEqualsFilterTest() throws Exception {
123         final boolean parser = parser("notification/data-changed-notification/data-change-event/data/toasterStatus>=5",
124                 "data_change_notification_toaster_status_NUMBER.xml");
125         Assert.assertFalse(parser);
126     }
127
128     private boolean parser(final String filter, final String fileName) throws Exception {
129         File xml = null;
130         for (final File file : this.xmls) {
131             if (file.getName().equals(fileName)) {
132                 xml = file;
133             }
134         }
135         final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
136         final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
137         Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
138         final ListenerAdapter listener = Notificator.createListener(path, "streamName", NotificationOutputType.JSON,
139                 null);
140         listener.setQueryParams(Instant.now(), Optional.empty(), Optional.ofNullable(filter), false, false);
141
142         // FIXME: do not use reflection here
143         final Class<?> superclass = listener.getClass().getSuperclass().getSuperclass();
144         Method method = null;
145         for (final Method met : superclass.getDeclaredMethods()) {
146             if (met.getName().equals("parseFilterParam")) {
147                 method = met;
148             }
149         }
150         if (method == null) {
151             throw new Exception("Methode parseFilterParam doesn't exist in " + superclass.getName());
152         }
153         method.setAccessible(true);
154         return (boolean) method.invoke(listener, readFile(xml));
155     }
156
157     private static String readFile(final File xml) throws IOException {
158         try (BufferedReader br = new BufferedReader(new FileReader(xml, StandardCharsets.UTF_8))) {
159             final StringBuilder sb = new StringBuilder();
160             String line = br.readLine();
161
162             while (line != null) {
163                 sb.append(line);
164                 sb.append("\n");
165                 line = br.readLine();
166             }
167             return sb.toString();
168         }
169     }
170
171 }