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