-
Notifications
You must be signed in to change notification settings - Fork 102
/
RollupEventSerializerTest.java
140 lines (129 loc) · 6.06 KB
/
RollupEventSerializerTest.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
/*
* Copyright 2013 Rackspace
*
* Licensed 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 com.rackspacecloud.blueflood.outputs.serializers;
import com.rackspacecloud.blueflood.outputs.serializers.helpers.RollupSerializationHelper;
import com.rackspacecloud.blueflood.types.*;
import junit.framework.Assert;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;
import org.junit.Test;
import java.io.IOError;
import java.io.IOException;
public class RollupEventSerializerTest {
@Test
public void testBasicRollupSerialization() {
BasicRollup rollup = new BasicRollup();
rollup.setCount(20);
rollup.setAverage(10);
rollup.setMax(20);
rollup.setMin(5);
rollup.setVariance(12);
//Get the JSON object node from Rollup
ObjectNode resultNode = RollupSerializationHelper.rollupToJson(rollup);
Assert.assertEquals(resultNode.get("max").asLong(), rollup.getMaxValue().toLong());
Assert.assertEquals(resultNode.get("min").asLong(), rollup.getMinValue().toLong());
Assert.assertEquals(resultNode.get("mean").asLong(), rollup.getAverage().toLong());
Assert.assertEquals(resultNode.get("var").asDouble(), rollup.getVariance().toDouble());
Assert.assertEquals(resultNode.get("count").asLong(), rollup.getCount());
}
@Test
public void testTimerRollupSerialization() {
TimerRollup rollup = new TimerRollup();
rollup.withCount(20);
rollup.withAverage(10);
rollup.withMaxValue(20);
rollup.withMinValue(5);
rollup.withVariance(12);
rollup.withSum(Double.valueOf(10));
rollup.withCountPS(30);
//Get the JSON object node from Rollup
ObjectNode resultNode = RollupSerializationHelper.rollupToJson(rollup);
Assert.assertEquals(resultNode.get("max").asLong(), rollup.getMaxValue().toLong());
Assert.assertEquals(resultNode.get("min").asLong(), rollup.getMinValue().toLong());
Assert.assertEquals(resultNode.get("mean").asLong(), rollup.getAverage().toLong());
Assert.assertEquals(resultNode.get("var").asDouble(), rollup.getVariance().toDouble());
Assert.assertEquals(resultNode.get("count").asLong(), rollup.getCount());
Assert.assertEquals(resultNode.get("sum").asDouble(), rollup.getSum());
Assert.assertEquals(resultNode.get("rate").asDouble(), rollup.getRate());
}
@Test
public void testHistgramRollupSerialization() throws IOException {
Points<SimpleNumber> points = new Points<SimpleNumber>();
long startTime = 12345678L;
//Count = 3.0, Mean = 2.0
points.add(new Points.Point<SimpleNumber>(startTime++, new SimpleNumber(1.0)));
points.add(new Points.Point<SimpleNumber>(startTime++, new SimpleNumber(2.0)));
points.add(new Points.Point<SimpleNumber>(startTime++, new SimpleNumber(3.0)));
HistogramRollup histogramRollup = HistogramRollup.buildRollupFromRawSamples(points);
ObjectNode resultNode = RollupSerializationHelper.rollupToJson(histogramRollup);
ArrayNode node = (ArrayNode)resultNode.get("bins");
Assert.assertEquals(node.get(0).get("count").asDouble(), 3.0);
Assert.assertEquals(node.get(0).get("mean").asDouble(), 2.0);
}
@Test
public void testSetRollupSerialization() {
final SetRollup rollup0 = new SetRollup()
.withObject(10)
.withObject(20)
.withObject(30);
ObjectNode resultNode = RollupSerializationHelper.rollupToJson(rollup0);
Assert.assertEquals(resultNode.get("count").asInt(), 3);
}
@Test
public void testGaugeRollupSerialization() {
final GaugeRollup rollup = new GaugeRollup()
.withLatest(0, 1234);
rollup.setMin(1);
rollup.setMax(2);
rollup.setCount(1);
rollup.setVariance(23);
rollup.setAverage(4);
ObjectNode resultNode = RollupSerializationHelper.rollupToJson(rollup);
Assert.assertEquals(resultNode.get("max").asLong(), rollup.getMaxValue().toLong());
Assert.assertEquals(resultNode.get("min").asLong(), rollup.getMinValue().toLong());
Assert.assertEquals(resultNode.get("mean").asLong(), rollup.getAverage().toLong());
Assert.assertEquals(resultNode.get("var").asDouble(), rollup.getVariance().toDouble());
Assert.assertEquals(resultNode.get("count").asLong(), rollup.getCount());
Assert.assertEquals(resultNode.get("latestVal").asLong(), rollup.getLatestNumericValue().longValue());
}
//Passing an unknown rollup type will throw IOError
@Test(expected = IOError.class)
public void testExceptionOnInvalid() {
class TestRollup implements Rollup{
@Override
public Boolean hasData() {
return null;
}
@Override
public RollupType getRollupType() {
return null;
}
};
RollupSerializationHelper.rollupToJson(new TestRollup());
}
@Test
public void testNullValuesOnZeroCount() {
BasicRollup rollup = new BasicRollup();
rollup.setCount(0);
//Get the JSON object node from Rollup
ObjectNode resultNode = RollupSerializationHelper.rollupToJson(rollup);
Assert.assertTrue(resultNode.get("max").isNull());
Assert.assertTrue(resultNode.get("min").isNull());
Assert.assertTrue(resultNode.get("mean").isNull());
Assert.assertTrue(resultNode.get("var").isNull());
Assert.assertEquals(resultNode.get("count").asLong(), 0);
}
}