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