2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
10 import java.io.BufferedReader;
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;
31 public class ExpressionParserTest {
33 private Collection<File> xmls;
36 public void setup() throws Exception {
37 this.xmls = TestRestconfUtils.loadFiles("/notifications/xml/output/");
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
127 private boolean parser(final String filter, final String fileName) throws Exception {
129 for (final File file : this.xmls) {
130 if (file.getName().equals(fileName)) {
134 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
135 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-14-12", "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);
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")) {
148 if (method == null) {
149 throw new Exception("Methode parseFilterParam doesn't exist in " + superclass.getName());
151 method.setAccessible(true);
152 return (boolean) method.invoke(listener, readFile(xml));
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();
160 while (line != null) {
163 line = br.readLine();
165 return sb.toString();