Commit 6bf2b322af0fe996f95a3bdaa1f0e7137b641778
Committed by
Andrew Shvayka
1 parent
e93c975a
Test and some code cleaning
Showing
2 changed files
with
154 additions
and
5 deletions
@@ -16,12 +16,10 @@ | @@ -16,12 +16,10 @@ | ||
16 | package org.thingsboard.server.common.data.query; | 16 | package org.thingsboard.server.common.data.query; |
17 | 17 | ||
18 | import com.fasterxml.jackson.annotation.JsonIgnore; | 18 | import com.fasterxml.jackson.annotation.JsonIgnore; |
19 | -import lombok.AllArgsConstructor; | ||
20 | import lombok.Data; | 19 | import lombok.Data; |
21 | import lombok.Getter; | 20 | import lombok.Getter; |
22 | 21 | ||
23 | @Data | 22 | @Data |
24 | -@AllArgsConstructor | ||
25 | public class DynamicValue<T> { | 23 | public class DynamicValue<T> { |
26 | 24 | ||
27 | @JsonIgnore | 25 | @JsonIgnore |
@@ -33,6 +31,12 @@ public class DynamicValue<T> { | @@ -33,6 +31,12 @@ public class DynamicValue<T> { | ||
33 | this.inherit = false; | 31 | this.inherit = false; |
34 | } | 32 | } |
35 | 33 | ||
34 | + public DynamicValue(DynamicValueSourceType sourceType, String sourceAttribute, boolean inherit) { | ||
35 | + this.sourceAttribute = sourceAttribute; | ||
36 | + this.sourceType = sourceType; | ||
37 | + this.inherit = inherit; | ||
38 | + } | ||
39 | + | ||
36 | @Getter | 40 | @Getter |
37 | private final DynamicValueSourceType sourceType; | 41 | private final DynamicValueSourceType sourceType; |
38 | @Getter | 42 | @Getter |
@@ -434,11 +434,156 @@ public class TbDeviceProfileNodeTest { | @@ -434,11 +434,156 @@ public class TbDeviceProfileNodeTest { | ||
434 | verify(ctx, Mockito.never()).tellFailure(Mockito.any(), Mockito.any()); | 434 | verify(ctx, Mockito.never()).tellFailure(Mockito.any(), Mockito.any()); |
435 | } | 435 | } |
436 | 436 | ||
437 | - private void init() throws TbNodeException { | 437 | + @Test |
438 | + public void testTenantInheritModeForDynamicValues() throws Exception { | ||
439 | + init(); | ||
440 | + | ||
441 | + DeviceProfile deviceProfile = new DeviceProfile(); | ||
442 | + DeviceProfileData deviceProfileData = new DeviceProfileData(); | ||
443 | + | ||
444 | + AttributeKvCompositeKey compositeKey = new AttributeKvCompositeKey( | ||
445 | + EntityType.TENANT, deviceId.getId(), "SERVER_SCOPE", "tenantAttribute" | ||
446 | + ); | ||
447 | + | ||
448 | + AttributeKvEntity attributeKvEntity = new AttributeKvEntity(); | ||
449 | + attributeKvEntity.setId(compositeKey); | ||
450 | + attributeKvEntity.setLongValue(100L); | ||
451 | + attributeKvEntity.setLastUpdateTs(0L); | ||
452 | + | ||
453 | + AttributeKvEntry entry = attributeKvEntity.toData(); | ||
454 | + ListenableFuture<List<AttributeKvEntry>> listListenableFutureWithLess = | ||
455 | + Futures.immediateFuture(Collections.singletonList(entry)); | ||
456 | + ListenableFuture<Optional<AttributeKvEntry>> optionalListenableFutureWithLess = | ||
457 | + Futures.immediateFuture(Optional.of(entry)); | ||
458 | + | ||
459 | + KeyFilter lowTempFilter = new KeyFilter(); | ||
460 | + lowTempFilter.setKey(new EntityKey(EntityKeyType.TIME_SERIES, "temperature")); | ||
461 | + lowTempFilter.setValueType(EntityKeyValueType.NUMERIC); | ||
462 | + NumericFilterPredicate lowTempPredicate = new NumericFilterPredicate(); | ||
463 | + lowTempPredicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER); | ||
464 | + lowTempPredicate.setValue( | ||
465 | + new FilterPredicateValue<>( | ||
466 | + 0.0, | ||
467 | + null, | ||
468 | + new DynamicValue<>(DynamicValueSourceType.CURRENT_DEVICE, "tenantAttribute", true)) | ||
469 | + ); | ||
470 | + lowTempFilter.setPredicate(lowTempPredicate); | ||
471 | + AlarmCondition alarmCondition = new AlarmCondition(); | ||
472 | + alarmCondition.setCondition(Collections.singletonList(lowTempFilter)); | ||
473 | + AlarmRule alarmRule = new AlarmRule(); | ||
474 | + alarmRule.setCondition(alarmCondition); | ||
475 | + DeviceProfileAlarm dpa = new DeviceProfileAlarm(); | ||
476 | + dpa.setId("lesstempID"); | ||
477 | + dpa.setAlarmType("lessTemperatureAlarm"); | ||
478 | + dpa.setCreateRules(new TreeMap<>(Collections.singletonMap(AlarmSeverity.CRITICAL, alarmRule))); | ||
479 | + | ||
480 | + deviceProfileData.setAlarms(Collections.singletonList(dpa)); | ||
481 | + deviceProfile.setProfileData(deviceProfileData); | ||
482 | + | ||
483 | + Mockito.when(cache.get(tenantId, deviceId)).thenReturn(deviceProfile); | ||
484 | + Mockito.when(timeseriesService.findLatest(tenantId, deviceId, Collections.singleton("temperature"))) | ||
485 | + .thenReturn(Futures.immediateFuture(Collections.emptyList())); | ||
486 | + Mockito.when(alarmService.findLatestByOriginatorAndType(tenantId, deviceId, "lessTemperatureAlarm")) | ||
487 | + .thenReturn(Futures.immediateFuture(null)); | ||
488 | + Mockito.when(alarmService.createOrUpdateAlarm(Mockito.any())) | ||
489 | + .thenAnswer(AdditionalAnswers.returnsFirstArg()); | ||
490 | + Mockito.when(ctx.getAttributesService()).thenReturn(attributesService); | ||
491 | + Mockito.when(attributesService.find(eq(tenantId), eq(deviceId), Mockito.anyString(), Mockito.anySet())) | ||
492 | + .thenReturn(listListenableFutureWithLess); | ||
493 | + Mockito.when(attributesService.find(eq(tenantId), eq(tenantId), eq(DataConstants.SERVER_SCOPE), Mockito.anyString())) | ||
494 | + .thenReturn(optionalListenableFutureWithLess); | ||
438 | 495 | ||
439 | - UUID uuid = new UUID(6041557255264276971L, -9019477126543226049L); | ||
440 | - System.out.println(uuid); | 496 | + TbMsg theMsg = TbMsg.newMsg("ALARM", deviceId, new TbMsgMetaData(), ""); |
497 | + Mockito.when(ctx.newMsg(Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.anyString())) | ||
498 | + .thenReturn(theMsg); | ||
499 | + | ||
500 | + ObjectNode data = mapper.createObjectNode(); | ||
501 | + data.put("temperature", 150L); | ||
502 | + TbMsg msg = TbMsg.newMsg(SessionMsgType.POST_TELEMETRY_REQUEST.name(), deviceId, new TbMsgMetaData(), | ||
503 | + TbMsgDataType.JSON, mapper.writeValueAsString(data), null, null); | ||
441 | 504 | ||
505 | + node.onMsg(ctx, msg); | ||
506 | + verify(ctx).tellSuccess(msg); | ||
507 | + verify(ctx).tellNext(theMsg, "Alarm Created"); | ||
508 | + verify(ctx, Mockito.never()).tellFailure(Mockito.any(), Mockito.any()); | ||
509 | + | ||
510 | + } | ||
511 | + | ||
512 | + @Test | ||
513 | + public void testCustomerInheritModeForDynamicValues() throws Exception { | ||
514 | + init(); | ||
515 | + | ||
516 | + DeviceProfile deviceProfile = new DeviceProfile(); | ||
517 | + DeviceProfileData deviceProfileData = new DeviceProfileData(); | ||
518 | + | ||
519 | + AttributeKvCompositeKey compositeKey = new AttributeKvCompositeKey( | ||
520 | + EntityType.TENANT, deviceId.getId(), "SERVER_SCOPE", "customerAttribute" | ||
521 | + ); | ||
522 | + | ||
523 | + AttributeKvEntity attributeKvEntity = new AttributeKvEntity(); | ||
524 | + attributeKvEntity.setId(compositeKey); | ||
525 | + attributeKvEntity.setLongValue(100L); | ||
526 | + attributeKvEntity.setLastUpdateTs(0L); | ||
527 | + | ||
528 | + AttributeKvEntry entry = attributeKvEntity.toData(); | ||
529 | + ListenableFuture<List<AttributeKvEntry>> listListenableFutureWithLess = | ||
530 | + Futures.immediateFuture(Collections.singletonList(entry)); | ||
531 | + ListenableFuture<Optional<AttributeKvEntry>> optionalListenableFutureWithLess = | ||
532 | + Futures.immediateFuture(Optional.of(entry)); | ||
533 | + | ||
534 | + KeyFilter lowTempFilter = new KeyFilter(); | ||
535 | + lowTempFilter.setKey(new EntityKey(EntityKeyType.TIME_SERIES, "temperature")); | ||
536 | + lowTempFilter.setValueType(EntityKeyValueType.NUMERIC); | ||
537 | + NumericFilterPredicate lowTempPredicate = new NumericFilterPredicate(); | ||
538 | + lowTempPredicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER); | ||
539 | + lowTempPredicate.setValue( | ||
540 | + new FilterPredicateValue<>( | ||
541 | + 0.0, | ||
542 | + null, | ||
543 | + new DynamicValue<>(DynamicValueSourceType.CURRENT_CUSTOMER, "customerAttribute", true)) | ||
544 | + ); | ||
545 | + lowTempFilter.setPredicate(lowTempPredicate); | ||
546 | + AlarmCondition alarmCondition = new AlarmCondition(); | ||
547 | + alarmCondition.setCondition(Collections.singletonList(lowTempFilter)); | ||
548 | + AlarmRule alarmRule = new AlarmRule(); | ||
549 | + alarmRule.setCondition(alarmCondition); | ||
550 | + DeviceProfileAlarm dpa = new DeviceProfileAlarm(); | ||
551 | + dpa.setId("lesstempID"); | ||
552 | + dpa.setAlarmType("lessTemperatureAlarm"); | ||
553 | + dpa.setCreateRules(new TreeMap<>(Collections.singletonMap(AlarmSeverity.CRITICAL, alarmRule))); | ||
554 | + | ||
555 | + deviceProfileData.setAlarms(Collections.singletonList(dpa)); | ||
556 | + deviceProfile.setProfileData(deviceProfileData); | ||
557 | + | ||
558 | + Mockito.when(cache.get(tenantId, deviceId)).thenReturn(deviceProfile); | ||
559 | + Mockito.when(timeseriesService.findLatest(tenantId, deviceId, Collections.singleton("temperature"))) | ||
560 | + .thenReturn(Futures.immediateFuture(Collections.emptyList())); | ||
561 | + Mockito.when(alarmService.findLatestByOriginatorAndType(tenantId, deviceId, "lessTemperatureAlarm")) | ||
562 | + .thenReturn(Futures.immediateFuture(null)); | ||
563 | + Mockito.when(alarmService.createOrUpdateAlarm(Mockito.any())) | ||
564 | + .thenAnswer(AdditionalAnswers.returnsFirstArg()); | ||
565 | + Mockito.when(ctx.getAttributesService()).thenReturn(attributesService); | ||
566 | + Mockito.when(attributesService.find(eq(tenantId), eq(deviceId), Mockito.anyString(), Mockito.anySet())) | ||
567 | + .thenReturn(listListenableFutureWithLess); | ||
568 | + Mockito.when(attributesService.find(eq(tenantId), eq(tenantId), eq(DataConstants.SERVER_SCOPE), Mockito.anyString())) | ||
569 | + .thenReturn(optionalListenableFutureWithLess); | ||
570 | + | ||
571 | + TbMsg theMsg = TbMsg.newMsg("ALARM", deviceId, new TbMsgMetaData(), ""); | ||
572 | + Mockito.when(ctx.newMsg(Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.anyString())) | ||
573 | + .thenReturn(theMsg); | ||
574 | + | ||
575 | + ObjectNode data = mapper.createObjectNode(); | ||
576 | + data.put("temperature", 150L); | ||
577 | + TbMsg msg = TbMsg.newMsg(SessionMsgType.POST_TELEMETRY_REQUEST.name(), deviceId, new TbMsgMetaData(), | ||
578 | + TbMsgDataType.JSON, mapper.writeValueAsString(data), null, null); | ||
579 | + | ||
580 | + node.onMsg(ctx, msg); | ||
581 | + verify(ctx).tellSuccess(msg); | ||
582 | + verify(ctx).tellNext(theMsg, "Alarm Created"); | ||
583 | + verify(ctx, Mockito.never()).tellFailure(Mockito.any(), Mockito.any()); | ||
584 | + } | ||
585 | + | ||
586 | + private void init() throws TbNodeException { | ||
442 | Mockito.when(ctx.getTenantId()).thenReturn(tenantId); | 587 | Mockito.when(ctx.getTenantId()).thenReturn(tenantId); |
443 | Mockito.when(ctx.getDeviceProfileCache()).thenReturn(cache); | 588 | Mockito.when(ctx.getDeviceProfileCache()).thenReturn(cache); |
444 | Mockito.when(ctx.getTimeseriesService()).thenReturn(timeseriesService); | 589 | Mockito.when(ctx.getTimeseriesService()).thenReturn(timeseriesService); |