-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathPointTest.java
More file actions
451 lines (341 loc) · 15.7 KB
/
PointTest.java
File metadata and controls
451 lines (341 loc) · 15.7 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.influxdb.client.write;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.util.HashMap;
import com.influxdb.client.domain.WritePrecision;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* @author Jakub Bednar (bednar@github) (11/10/2018 12:57)
*/
class PointTest {
@Test
void measurementEscape() {
Point point = Point.measurement("h2 o")
.addTag("location", "europe")
.addTag("", "warn")
.addField("level", 2);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2\\ o,location=europe level=2i");
point = Point.measurement("h2=o")
.addTag("location", "europe")
.addTag("", "warn")
.addField("level", 2);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2=o,location=europe level=2i");
point = Point.measurement("h2,o")
.addTag("location", "europe")
.addTag("", "warn")
.addField("level", 2);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2\\,o,location=europe level=2i");
}
@Test
public void createByConstructor() {
Point point = new Point("h2o")
.addTag("location", "europe")
.addField("level", 2);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i");
}
@SuppressWarnings("ConstantConditions")
@Test
public void createByConstructorMeasurementRequired() {
Assertions.assertThatThrownBy(() -> new Point(null))
.isInstanceOf(NullPointerException.class)
.hasMessage("Expecting a not null reference for measurement");
}
@Test
void tagEmptyKey() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addTag("", "warn")
.addField("level", 2);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i");
}
@Test
void tagEmptyValue() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addTag("log", "")
.addField("level", 2);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i");
}
@Test
void tagNullValue() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addTag("log", null)
.addField("level", 2);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i");
}
@Test
public void tagEscapingKeyAndValue() {
Point point = Point.measurement("h\n2\ro\t_data")
.addTag("new\nline", "new\nline")
.addTag("carriage\rreturn", "carriage\rreturn")
.addTag("t\tab", "t\tab")
.addField("level", 2);
Assertions.assertThat(point.toLineProtocol())
.isEqualTo("h\\n2\\ro\\t_data,carriage\\rreturn=carriage\\rreturn,new\\nline=new\\nline,t\\tab=t\\tab level=2i");
}
@Test
public void equalSignEscaping() {
Point point = Point.measurement("h=2o")
.addTag("l=ocation", "e=urope")
.addField("l=evel", 2);
Assertions.assertThat(point.toLineProtocol())
.isEqualTo("h=2o,l\\=ocation=e\\=urope l\\=evel=2i");
}
@Test
void fieldTypes() {
Point point = Point.measurement("h2o").addTag("location", "europe")
.addField("long", 1L)
.addField("double", 2D)
.addField("float", 3F)
.addField("longObject", Long.valueOf("4"))
.addField("doubleObject", Double.valueOf("5"))
.addField("floatObject", Float.valueOf("6"))
.addField("bigDecimal", new BigDecimal("33.45"))
.addField("integer", 7)
.addField("integerObject", Integer.valueOf("8"))
.addField("boolean", false)
.addField("booleanObject", Boolean.TRUE)
.addField("string", "string value");
String expected = "h2o,location=europe bigDecimal=33.45,boolean=false,booleanObject=true,double=2.0,doubleObject=5.0,"
+ "float=3.0,floatObject=6.0,integer=7i,integerObject=8i,long=1i,longObject=4i,string=\"string value\"";
Assertions.assertThat(point.toLineProtocol()).isEqualTo(expected);
}
@Test
void fieldNullValue() {
Point point = Point.measurement("h2o").addTag("location", "europe").addField("level", 2)
.addField("warning", (String) null);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i");
}
@Test
void fieldEscape() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", "string esc\\ape value");
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=\"string esc\\\\ape value\"");
point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", "string esc\"ape value");
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=\"string esc\\\"ape value\"");
}
@Test
void tagEscape() {
Point point = Point.measurement("h2o")
.addTag("location", "\\")
.addTag("zone", "europe")
.addField("level", "dummy value");
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=\\\\,zone=europe level=\"dummy value\"");
}
@Test
void time() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", 2)
.time(123L, WritePrecision.S);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i 123");
}
@Test
void timeBigInteger() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", 2)
.time(new BigInteger("123"), WritePrecision.S);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i 123");
// Friday, June 22, 3353
point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", 2)
.time(new BigInteger("43658216763800123456"), WritePrecision.NS);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i 43658216763800123456");
}
@Test
void timeBigDecimal() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", 2)
.time(new BigDecimal("123"), WritePrecision.S);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i 123");
point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", 2)
.time(new BigDecimal("1.23E+02"), WritePrecision.NS);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i 123");
// Friday, June 22, 3353
point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", 2)
.time(new BigDecimal("43658216763800123456"), WritePrecision.NS);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i 43658216763800123456");
}
@Test
void timeFloat() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", 2)
.time(new Float("123"), WritePrecision.S);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i 123");
point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", 2)
.time(new Float("1.23"), WritePrecision.NS);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i 1");
}
@Test
void timePrecisionDefault() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", 2);
Assertions.assertThat(point.getPrecision()).isEqualTo(WritePrecision.NS);
}
@Test
void timeInstantOver2262() {
Instant time = Instant.parse("3353-06-22T10:26:03.800123456Z");
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", 2)
.time(time, WritePrecision.NS);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i 43658216763800123456");
}
@Test
void timeInstantNull() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", 2)
.time((Instant) null, WritePrecision.S);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i");
}
@Test
void timeGetTime() {
Instant time = Instant.parse("2022-06-12T10:26:03.800123456Z");
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", 2)
.time(time, WritePrecision.NS);
Assertions.assertThat(point.getTime()).isEqualTo(BigInteger.valueOf(1655029563800123456L));
}
@Test
void defaultTags() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", 2);
PointSettings defaults = new PointSettings().addDefaultTag("expensive", "true");
Assertions.assertThat(point.toLineProtocol(defaults)).isEqualTo("h2o,expensive=true,location=europe level=2i");
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i");
}
@Test
void defaultTagsOverride() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addTag("expensive", "")
.addField("level", 2);
PointSettings defaults = new PointSettings().addDefaultTag("expensive", "true");
Assertions.assertThat(point.toLineProtocol(defaults)).isEqualTo("h2o,expensive=true,location=europe level=2i");
}
@Test
void defaultTagsOverrideNull() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addTag("expensive", null)
.addField("level", 2);
PointSettings defaults = new PointSettings().addDefaultTag("expensive", "true");
Assertions.assertThat(point.toLineProtocol(defaults)).isEqualTo("h2o,expensive=true,location=europe level=2i");
}
@Test
void defaultTagsNotOverride() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addTag("expensive", "false")
.addField("level", 2);
PointSettings defaults = new PointSettings().addDefaultTag("expensive", "true");
Assertions.assertThat(point.toLineProtocol(defaults)).isEqualTo("h2o,expensive=false,location=europe level=2i");
}
@Test
void defaultTagsSorted() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("level", 2);
PointSettings defaults = new PointSettings()
.addDefaultTag("a-expensive", "true")
.addDefaultTag("z-expensive", "false");
Assertions.assertThat(point.toLineProtocol(defaults)).isEqualTo("h2o,a-expensive=true,location=europe,z-expensive=false level=2i");
}
@Test
public void infinityValues() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("double-infinity-positive", Double.POSITIVE_INFINITY)
.addField("double-infinity-negative", Double.NEGATIVE_INFINITY)
.addField("double-nan", Double.NaN)
.addField("flout-infinity-positive", Float.POSITIVE_INFINITY)
.addField("flout-infinity-negative", Float.NEGATIVE_INFINITY)
.addField("flout-nan", Float.NaN)
.addField("level", 2);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe level=2i");
}
@Test
public void onlyInfinityValues() {
Point point = Point.measurement("h2o")
.addTag("location", "europe")
.addField("double-infinity-positive", Double.POSITIVE_INFINITY)
.addField("double-infinity-negative", Double.NEGATIVE_INFINITY)
.addField("double-nan", Double.NaN)
.addField("flout-infinity-positive", Float.POSITIVE_INFINITY)
.addField("flout-infinity-negative", Float.NEGATIVE_INFINITY)
.addField("flout-nan", Float.NaN);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("");
}
@Test
void hasFields() {
Assertions.assertThat(Point.measurement("h2o").hasFields()).isFalse();
Assertions.assertThat(Point.measurement("h2o").addTag("location", "europe").hasFields()).isFalse();
Assertions.assertThat(Point.measurement("h2o").addField("level", 2).hasFields()).isTrue();
Assertions.assertThat(Point.measurement("h2o").addTag("location", "europe").addField("level", 3).hasFields()).isTrue();
}
@Test
void addTags() {
HashMap<String, String> tags = new HashMap<>();
tags.put("type", "production");
tags.put("location", "europe");
tags.put("expensive", "");
Point point = Point.measurement("h2o")
.addField("level", 2)
.addTags(tags);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe,type=production level=2i");
}
@Test
void addFields() {
HashMap<String, Object> fields = new HashMap<>();
fields.put("level", 2);
fields.put("accepted", true);
fields.put("power", 2.56);
fields.put("clean", null);
Point point = Point
.measurement("h2o")
.addTag("location", "europe")
.addFields(fields);
Assertions.assertThat(point.toLineProtocol()).isEqualTo("h2o,location=europe accepted=true,level=2i,power=2.56");
}
}