1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2017 azeti Networks AG (<info@azeti.net>)
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7
+ * associated documentation files (the "Software"), to deal in the Software without restriction,
8
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in all copies or
13
+ * substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16
+ * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+ */
21
+ package org .influxdb .impl ;
22
+
23
+ import static org .junit .Assert .assertEquals ;
24
+ import static org .junit .Assert .assertTrue ;
25
+
26
+ import java .time .Instant ;
27
+ import java .util .Arrays ;
28
+ import java .util .Date ;
29
+ import java .util .LinkedList ;
30
+ import java .util .List ;
31
+ import java .util .Random ;
32
+ import java .util .UUID ;
33
+
34
+ import org .influxdb .InfluxDBMapperException ;
35
+ import org .influxdb .annotation .Column ;
36
+ import org .influxdb .annotation .Measurement ;
37
+ import org .influxdb .dto .QueryResult ;
38
+ import org .influxdb .impl .InfluxDBResultMapper ;
39
+ import org .junit .Test ;
40
+
41
+ /**
42
+ * @author fmachado
43
+ */
44
+ public class InfluxDBResultMapperTest {
45
+
46
+ InfluxDBResultMapper mapper = new InfluxDBResultMapper ();
47
+
48
+ @ Test
49
+ public void testToPOJO_HappyPath () {
50
+ // Given...
51
+ List <String > columnList = Arrays .asList ("time" , "uuid" );
52
+ List <Object > firstSeriesResult = Arrays .asList (Instant .now ().toEpochMilli (), UUID .randomUUID ().toString ());
53
+
54
+ QueryResult .Series series = new QueryResult .Series ();
55
+ series .setColumns (columnList );
56
+ series .setName ("CustomMeasurement" );
57
+ series .setValues (Arrays .asList (firstSeriesResult ));
58
+
59
+ QueryResult .Result internalResult = new QueryResult .Result ();
60
+ internalResult .setSeries (Arrays .asList (series ));
61
+
62
+ QueryResult queryResult = new QueryResult ();
63
+ queryResult .setResults (Arrays .asList (internalResult ));
64
+
65
+ //When...
66
+ List <MyCustomMeasurement > myList = mapper .toPOJO (queryResult , MyCustomMeasurement .class );
67
+
68
+ // Then...
69
+ assertEquals ("there must be one entry in the result list" , 1 , myList .size ());
70
+ }
71
+
72
+ @ Test (expected = IllegalArgumentException .class )
73
+ public void testThrowExceptionIfMissingAnnotation () {
74
+ mapper .throwExceptionIfMissingAnnotation (String .class );
75
+ }
76
+
77
+ @ Test (expected = InfluxDBMapperException .class )
78
+ public void testThrowExceptionIfError_InfluxQueryResultHasError () {
79
+ QueryResult queryResult = new QueryResult ();
80
+ queryResult .setError ("main queryresult error" );
81
+
82
+ mapper .throwExceptionIfResultWithError (queryResult );
83
+ }
84
+
85
+ @ Test (expected = InfluxDBMapperException .class )
86
+ public void testThrowExceptionIfError_InfluxQueryResultSeriesHasError () {
87
+ QueryResult queryResult = new QueryResult ();
88
+
89
+ QueryResult .Result seriesResult = new QueryResult .Result ();
90
+ seriesResult .setError ("series error" );
91
+
92
+ queryResult .setResults (Arrays .asList (seriesResult ));
93
+
94
+ mapper .throwExceptionIfResultWithError (queryResult );
95
+ }
96
+
97
+ @ Test
98
+ public void testGetMeasurementName_testStateMeasurement () {
99
+ assertEquals ("CustomMeasurement" , mapper .getMeasurementName (MyCustomMeasurement .class ));
100
+ }
101
+
102
+ @ Test
103
+ public void testParseSeriesAs_testTwoValidSeries () {
104
+ // Given...
105
+ mapper .cacheMeasurementClass (MyCustomMeasurement .class );
106
+
107
+ List <String > columnList = Arrays .asList ("time" , "uuid" );
108
+
109
+ List <Object > firstSeriesResult = Arrays .asList (Instant .now ().toEpochMilli (), UUID .randomUUID ().toString ());
110
+ List <Object > secondSeriesResult = Arrays .asList (Instant .now ().plusSeconds (1 ).toEpochMilli (), UUID .randomUUID ().toString ());
111
+
112
+ QueryResult .Series series = new QueryResult .Series ();
113
+ series .setColumns (columnList );
114
+ series .setValues (Arrays .asList (firstSeriesResult , secondSeriesResult ));
115
+
116
+ //When...
117
+ List <MyCustomMeasurement > result = new LinkedList <>();
118
+ mapper .parseSeriesAs (series , MyCustomMeasurement .class , result );
119
+
120
+ //Then...
121
+ assertTrue ("there must be two series in the result list" , result .size () == 2 );
122
+
123
+ assertEquals ("Field 'time' (1st series) is not valid" , firstSeriesResult .get (0 ), result .get (0 ).time .toEpochMilli ());
124
+ assertEquals ("Field 'uuid' (1st series) is not valid" , firstSeriesResult .get (1 ), result .get (0 ).uuid );
125
+
126
+ assertEquals ("Field 'time' (2nd series) is not valid" , secondSeriesResult .get (0 ), result .get (1 ).time .toEpochMilli ());
127
+ assertEquals ("Field 'uuid' (2nd series) is not valid" , secondSeriesResult .get (1 ), result .get (1 ).uuid );
128
+ }
129
+
130
+ @ Test
131
+ public void testParseSeriesAs_testNonNullAndValidValues () {
132
+ // Given...
133
+ mapper .cacheMeasurementClass (MyCustomMeasurement .class );
134
+
135
+ List <String > columnList = Arrays .asList ("time" , "uuid" ,
136
+ "doubleObject" , "longObject" , "integerObject" ,
137
+ "doublePrimitive" , "longPrimitive" , "integerPrimitive" ,
138
+ "booleanObject" , "booleanPrimitive" );
139
+
140
+ // InfluxDB client returns the time representation as Double.
141
+ Double now = Long .valueOf (System .currentTimeMillis ()).doubleValue ();
142
+ String uuidAsString = UUID .randomUUID ().toString ();
143
+
144
+ // InfluxDB client returns any number as Double.
145
+ // See https://github.com/influxdata/influxdb-java/issues/153#issuecomment-259681987
146
+ // for more information.
147
+ List <Object > seriesResult = Arrays .asList (now , uuidAsString ,
148
+ new Double ("1.01" ), new Double ("2" ), new Double ("3" ),
149
+ new Double ("1.01" ), new Double ("4" ), new Double ("5" ),
150
+ "false" , "true" );
151
+
152
+ QueryResult .Series series = new QueryResult .Series ();
153
+ series .setColumns (columnList );
154
+ series .setValues (Arrays .asList (seriesResult ));
155
+
156
+ //When...
157
+ List <MyCustomMeasurement > result = new LinkedList <>();
158
+ mapper .parseSeriesAs (series , MyCustomMeasurement .class , result );
159
+
160
+ //Then...
161
+ MyCustomMeasurement myObject = result .get (0 );
162
+ assertEquals ("field 'time' does not match" , now .longValue (), myObject .time .toEpochMilli ());
163
+ assertEquals ("field 'uuid' does not match" , uuidAsString , myObject .uuid );
164
+
165
+ assertEquals ("field 'doubleObject' does not match" , asDouble (seriesResult .get (2 )), myObject .doubleObject );
166
+ assertEquals ("field 'longObject' does not match" , new Long (asDouble (seriesResult .get (3 )).longValue ()), myObject .longObject );
167
+ assertEquals ("field 'integerObject' does not match" , new Integer (asDouble (seriesResult .get (4 )).intValue ()), myObject .integerObject );
168
+
169
+ assertTrue ("field 'doublePrimitive' does not match" ,
170
+ Double .compare (asDouble (seriesResult .get (5 )).doubleValue (), myObject .doublePrimitive ) == 0 );
171
+
172
+ assertTrue ("field 'longPrimitive' does not match" ,
173
+ Long .compare (asDouble (seriesResult .get (6 )).longValue (), myObject .longPrimitive ) == 0 );
174
+
175
+ assertTrue ("field 'integerPrimitive' does not match" ,
176
+ Integer .compare (asDouble (seriesResult .get (7 )).intValue (), myObject .integerPrimitive ) == 0 );
177
+
178
+ assertEquals ("booleanObject 'time' does not match" ,
179
+ Boolean .valueOf (String .valueOf (seriesResult .get (8 ))), myObject .booleanObject );
180
+
181
+ assertEquals ("booleanPrimitive 'uuid' does not match" ,
182
+ Boolean .valueOf (String .valueOf (seriesResult .get (9 ))).booleanValue (), myObject .booleanPrimitive );
183
+ }
184
+
185
+ Double asDouble (Object obj ) {
186
+ return (Double ) obj ;
187
+ }
188
+
189
+ @ Test
190
+ public void testFieldValueModified_DateAsISO8601 () {
191
+ // Given...
192
+ mapper .cacheMeasurementClass (MyCustomMeasurement .class );
193
+
194
+ List <String > columnList = Arrays .asList ("time" );
195
+ List <Object > firstSeriesResult = Arrays .asList ("2017-06-19T09:29:45.655123Z" );
196
+
197
+ QueryResult .Series series = new QueryResult .Series ();
198
+ series .setColumns (columnList );
199
+ series .setValues (Arrays .asList (firstSeriesResult ));
200
+
201
+ //When...
202
+ List <MyCustomMeasurement > result = new LinkedList <>();
203
+ mapper .parseSeriesAs (series , MyCustomMeasurement .class , result );
204
+
205
+ //Then...
206
+ assertTrue (result .size () == 1 );
207
+ }
208
+
209
+ @ Test (expected = InfluxDBMapperException .class )
210
+ public void testUnsupportedField () {
211
+ // Given...
212
+ mapper .cacheMeasurementClass (MyPojoWithUnsupportedField .class );
213
+
214
+ List <String > columnList = Arrays .asList ("bar" );
215
+ List <Object > firstSeriesResult = Arrays .asList ("content representing a Date" );
216
+
217
+ QueryResult .Series series = new QueryResult .Series ();
218
+ series .setColumns (columnList );
219
+ series .setValues (Arrays .asList (firstSeriesResult ));
220
+
221
+ //When...
222
+ List <MyPojoWithUnsupportedField > result = new LinkedList <>();
223
+ mapper .parseSeriesAs (series , MyPojoWithUnsupportedField .class , result );
224
+ }
225
+
226
+ @ Measurement (name = "CustomMeasurement" )
227
+ static class MyCustomMeasurement {
228
+
229
+ @ Column (name = "time" )
230
+ private Instant time ;
231
+
232
+ @ Column (name = "uuid" )
233
+ private String uuid ;
234
+
235
+ @ Column (name = "doubleObject" )
236
+ private Double doubleObject ;
237
+
238
+ @ Column (name = "longObject" )
239
+ private Long longObject ;
240
+
241
+ @ Column (name = "integerObject" )
242
+ private Integer integerObject ;
243
+
244
+ @ Column (name = "doublePrimitive" )
245
+ private double doublePrimitive ;
246
+
247
+ @ Column (name = "longPrimitive" )
248
+ private long longPrimitive ;
249
+
250
+ @ Column (name = "integerPrimitive" )
251
+ private int integerPrimitive ;
252
+
253
+ @ Column (name = "booleanObject" )
254
+ private Boolean booleanObject ;
255
+
256
+ @ Column (name = "booleanPrimitive" )
257
+ private boolean booleanPrimitive ;
258
+
259
+ @ SuppressWarnings ("unused" )
260
+ private String nonColumn1 ;
261
+
262
+ @ SuppressWarnings ("unused" )
263
+ private Random rnd ;
264
+
265
+ @ Override
266
+ public String toString () {
267
+ return "MyCustomMeasurement [time=" + time + ", uuid=" + uuid + ", doubleObject=" + doubleObject + ", longObject=" + longObject
268
+ + ", integerObject=" + integerObject + ", doublePrimitive=" + doublePrimitive + ", longPrimitive=" + longPrimitive
269
+ + ", integerPrimitive=" + integerPrimitive + ", booleanObject=" + booleanObject + ", booleanPrimitive=" + booleanPrimitive + "]" ;
270
+ }
271
+ }
272
+
273
+ @ Measurement (name = "foo" )
274
+ static class MyPojoWithUnsupportedField {
275
+
276
+ @ Column (name = "bar" )
277
+ private Date myDate ;
278
+ }
279
+ }
0 commit comments