/
StreamFunctionTestCase.java
198 lines (159 loc) · 7.51 KB
/
StreamFunctionTestCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.siddhi.core.query.streamfunction;
import org.apache.log4j.Logger;
import org.testng.AssertJUnit;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.wso2.siddhi.core.SiddhiAppRuntime;
import org.wso2.siddhi.core.SiddhiManager;
import org.wso2.siddhi.core.event.Event;
import org.wso2.siddhi.core.query.output.callback.QueryCallback;
import org.wso2.siddhi.core.stream.input.InputHandler;
import org.wso2.siddhi.core.util.EventPrinter;
public class StreamFunctionTestCase {
private static final Logger log = Logger.getLogger(StreamFunctionTestCase.class);
private int inEventCount;
private int removeEventCount;
private boolean eventArrived;
@BeforeMethod
public void init() {
inEventCount = 0;
removeEventCount = 0;
eventArrived = false;
}
@Test
public void pol2CartFunctionTest() throws InterruptedException {
SiddhiManager siddhiManager = new SiddhiManager();
String polarStream = "define stream PolarStream (theta double, rho double);";
String query = "@info(name = 'query1') " +
"from PolarStream#pol2Cart(theta, rho) " +
"select x, y " +
"insert into outputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(polarStream + query);
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
if (inEvents != null) {
inEventCount = inEventCount + inEvents.length;
AssertJUnit.assertEquals(12, Math.round((Double) inEvents[0].getData(0)));
AssertJUnit.assertEquals(5, Math.round((Double) inEvents[0].getData(1)));
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("PolarStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[]{22.6, 13.0});
Thread.sleep(100);
AssertJUnit.assertEquals(1, inEventCount);
AssertJUnit.assertTrue(eventArrived);
siddhiAppRuntime.shutdown();
}
@Test
public void pol2CartFunctionTest2() throws InterruptedException {
SiddhiManager siddhiManager = new SiddhiManager();
String polarStream = "define stream PolarStream (theta double, rho double, elevation double);";
String query = "@info(name = 'query1') " +
"from PolarStream#pol2Cart(theta, rho, elevation) " +
"select x, z " +
"insert into outputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(polarStream + query);
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
if (inEvents != null) {
inEventCount = inEventCount + inEvents.length;
AssertJUnit.assertEquals(12, Math.round((Double) inEvents[0].getData(0)));
AssertJUnit.assertEquals(7, Math.round((Double) inEvents[0].getData(1)));
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("PolarStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[]{22.6, 13.0, 7.0});
Thread.sleep(100);
AssertJUnit.assertEquals(1, inEventCount);
AssertJUnit.assertTrue(eventArrived);
siddhiAppRuntime.shutdown();
}
@Test
public void pol2CartFunctionTest3() throws InterruptedException {
SiddhiManager siddhiManager = new SiddhiManager();
String polarStream = "define stream PolarStream (theta double, rho double);";
String query = "@info(name = 'query1') " +
"from PolarStream#pol2Cart(*) " +
"select x, y " +
"insert into outputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(polarStream + query);
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
if (inEvents != null) {
inEventCount = inEventCount + inEvents.length;
AssertJUnit.assertEquals(12, Math.round((Double) inEvents[0].getData(0)));
AssertJUnit.assertEquals(5, Math.round((Double) inEvents[0].getData(1)));
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("PolarStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[]{22.6, 13.0});
Thread.sleep(100);
AssertJUnit.assertEquals(1, inEventCount);
AssertJUnit.assertTrue(eventArrived);
siddhiAppRuntime.shutdown();
}
@Test
public void nonStandardAttribute() throws InterruptedException {
SiddhiManager siddhiManager = new SiddhiManager();
siddhiManager.setExtension("custom:get", AttributeStreamFunction.class);
String siddhiApp = "" +
"define stream `$InputStream` (`56$2theta` double, rho double); " +
"@info(name = 'query1') " +
"from `$InputStream`#custom:get('test(0)') " +
"select `56$2theta`, rho, `test(0)` as foo " +
"insert into OutputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
if (inEvents != null) {
inEventCount = inEventCount + inEvents.length;
AssertJUnit.assertEquals(22.6, inEvents[0].getData(0));
AssertJUnit.assertEquals(13.0,inEvents[0].getData(1));
AssertJUnit.assertEquals("test",inEvents[0].getData(2));
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("$InputStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[]{22.6, 13.0});
Thread.sleep(100);
AssertJUnit.assertEquals(1, inEventCount);
AssertJUnit.assertTrue(eventArrived);
siddhiAppRuntime.shutdown();
}
}