fixed tests

This commit is contained in:
YevhenBondarenko 2024-07-25 12:01:42 +02:00
parent a15ec624d7
commit 977da8bc8a
9 changed files with 56 additions and 48 deletions

View File

@ -81,7 +81,7 @@ public class EntityRelationController extends BaseController {
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/relation", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void saveRelation(@Parameter(description = "A JSON value representing the relation.", required = true)
public EntityRelation saveRelation(@Parameter(description = "A JSON value representing the relation.", required = true)
@RequestBody EntityRelation relation) throws ThingsboardException {
checkNotNull(relation);
checkCanCreateRelation(relation.getFrom());
@ -90,7 +90,7 @@ public class EntityRelationController extends BaseController {
relation.setTypeGroup(RelationTypeGroup.COMMON);
}
tbEntityRelationService.save(getTenantId(), getCurrentUser().getCustomerId(), relation, getCurrentUser());
return tbEntityRelationService.save(getTenantId(), getCurrentUser().getCustomerId(), relation, getCurrentUser());
}
@ApiOperation(value = "Delete Relation (deleteRelation)",
@ -98,7 +98,7 @@ public class EntityRelationController extends BaseController {
@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/relation", method = RequestMethod.DELETE, params = {FROM_ID, FROM_TYPE, RELATION_TYPE, TO_ID, TO_TYPE})
@ResponseStatus(value = HttpStatus.OK)
public void deleteRelation(@Parameter(description = ENTITY_ID_PARAM_DESCRIPTION, required = true) @RequestParam(FROM_ID) String strFromId,
public EntityRelation deleteRelation(@Parameter(description = ENTITY_ID_PARAM_DESCRIPTION, required = true) @RequestParam(FROM_ID) String strFromId,
@Parameter(description = ENTITY_TYPE_PARAM_DESCRIPTION, required = true) @RequestParam(FROM_TYPE) String strFromType,
@Parameter(description = RELATION_TYPE_PARAM_DESCRIPTION, required = true) @RequestParam(RELATION_TYPE) String strRelationType,
@Parameter(description = RELATION_TYPE_GROUP_PARAM_DESCRIPTION) @RequestParam(value = "relationTypeGroup", required = false) String strRelationTypeGroup,
@ -116,7 +116,7 @@ public class EntityRelationController extends BaseController {
RelationTypeGroup relationTypeGroup = parseRelationTypeGroup(strRelationTypeGroup, RelationTypeGroup.COMMON);
EntityRelation relation = new EntityRelation(fromId, toId, strRelationType, relationTypeGroup);
tbEntityRelationService.delete(getTenantId(), getCurrentUser().getCustomerId(), relation, getCurrentUser());
return tbEntityRelationService.delete(getTenantId(), getCurrentUser().getCustomerId(), relation, getCurrentUser());
}
@ApiOperation(value = "Delete common relations (deleteCommonRelations)",

View File

@ -39,12 +39,13 @@ public class DefaultTbEntityRelationService extends AbstractTbEntityService impl
private final RelationService relationService;
@Override
public void save(TenantId tenantId, CustomerId customerId, EntityRelation relation, User user) throws ThingsboardException {
public EntityRelation save(TenantId tenantId, CustomerId customerId, EntityRelation relation, User user) throws ThingsboardException {
ActionType actionType = ActionType.RELATION_ADD_OR_UPDATE;
try {
relationService.saveRelation(tenantId, relation);
var savedRelation = relationService.saveRelation(tenantId, relation);
logEntityActionService.logEntityRelationAction(tenantId, customerId,
relation, user, actionType, null, relation);
savedRelation, user, actionType, null, savedRelation);
return savedRelation;
} catch (Exception e) {
logEntityActionService.logEntityRelationAction(tenantId, customerId,
relation, user, actionType, e, relation);
@ -53,14 +54,15 @@ public class DefaultTbEntityRelationService extends AbstractTbEntityService impl
}
@Override
public void delete(TenantId tenantId, CustomerId customerId, EntityRelation relation, User user) throws ThingsboardException {
public EntityRelation delete(TenantId tenantId, CustomerId customerId, EntityRelation relation, User user) throws ThingsboardException {
ActionType actionType = ActionType.RELATION_DELETED;
try {
boolean found = relationService.deleteRelation(tenantId, relation.getFrom(), relation.getTo(), relation.getType(), relation.getTypeGroup());
if (!found) {
var found = relationService.deleteRelation(tenantId, relation.getFrom(), relation.getTo(), relation.getType(), relation.getTypeGroup());
if (found == null) {
throw new ThingsboardException("Requested item wasn't found!", ThingsboardErrorCode.ITEM_NOT_FOUND);
}
logEntityActionService.logEntityRelationAction(tenantId, customerId, relation, user, actionType, null, relation);
logEntityActionService.logEntityRelationAction(tenantId, customerId, found, user, actionType, null, found);
return found;
} catch (Exception e) {
logEntityActionService.logEntityRelationAction(tenantId, customerId,
relation, user, actionType, e, relation);

View File

@ -24,9 +24,9 @@ import org.thingsboard.server.common.data.relation.EntityRelation;
public interface TbEntityRelationService {
void save(TenantId tenantId, CustomerId customerId, EntityRelation entity, User user) throws ThingsboardException;
EntityRelation save(TenantId tenantId, CustomerId customerId, EntityRelation entity, User user) throws ThingsboardException;
void delete(TenantId tenantId, CustomerId customerId, EntityRelation entity, User user) throws ThingsboardException;
EntityRelation delete(TenantId tenantId, CustomerId customerId, EntityRelation entity, User user) throws ThingsboardException;
void deleteCommonRelations(TenantId tenantId, CustomerId customerId, EntityId entityId, User user) throws ThingsboardException;

View File

@ -103,7 +103,7 @@ public class EntityRelationControllerTest extends AbstractControllerTest {
Mockito.reset(tbClusterService, auditLogService);
doPost("/api/relation", relation).andExpect(status().isOk());
relation = doPost("/api/relation", relation, EntityRelation.class);
String url = String.format("/api/relation?fromId=%s&fromType=%s&relationType=%s&toId=%s&toType=%s",
mainDevice.getUuidId(), EntityType.DEVICE,
@ -315,7 +315,7 @@ public class EntityRelationControllerTest extends AbstractControllerTest {
Device device = buildSimpleDevice("Test device 1");
EntityRelation relation = createFromRelation(mainDevice, device, "CONTAINS");
doPost("/api/relation", relation).andExpect(status().isOk());
relation = doPost("/api/relation", relation, EntityRelation.class);
String url = String.format("/api/relation?fromId=%s&fromType=%s&relationType=%s&toId=%s&toType=%s",
mainDevice.getUuidId(), EntityType.DEVICE,
@ -329,11 +329,11 @@ public class EntityRelationControllerTest extends AbstractControllerTest {
Mockito.reset(tbClusterService, auditLogService);
doDelete(url).andExpect(status().isOk());
var deletedRelation = doDelete(url, EntityRelation.class);
testNotifyEntityAllOneTimeRelation(foundRelation,
testNotifyEntityAllOneTimeRelation(deletedRelation,
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(),
ActionType.RELATION_DELETED, foundRelation);
ActionType.RELATION_DELETED, deletedRelation);
doGet(url).andExpect(status().is4xxClientError());
}
@ -523,7 +523,7 @@ public class EntityRelationControllerTest extends AbstractControllerTest {
@Test
public void testCreateRelationFromTenantToDevice() throws Exception {
EntityRelation relation = new EntityRelation(tenantAdmin.getTenantId(), mainDevice.getId(), "CONTAINS");
doPost("/api/relation", relation).andExpect(status().isOk());
relation = doPost("/api/relation", relation, EntityRelation.class);
String url = String.format("/api/relation?fromId=%s&fromType=%s&relationType=%s&toId=%s&toType=%s",
tenantAdmin.getTenantId(), EntityType.TENANT,
@ -539,7 +539,7 @@ public class EntityRelationControllerTest extends AbstractControllerTest {
@Test
public void testCreateRelationFromDeviceToTenant() throws Exception {
EntityRelation relation = new EntityRelation(mainDevice.getId(), tenantAdmin.getTenantId(), "CONTAINS");
doPost("/api/relation", relation).andExpect(status().isOk());
relation = doPost("/api/relation", relation, EntityRelation.class);
String url = String.format("/api/relation?fromId=%s&fromType=%s&relationType=%s&toId=%s&toType=%s",
mainDevice.getUuidId(), EntityType.DEVICE,

View File

@ -48,7 +48,7 @@ public class RelationEdgeTest extends AbstractEdgeTest {
relation.setTo(asset.getId());
relation.setTypeGroup(RelationTypeGroup.COMMON);
edgeImitator.expectMessageAmount(1);
doPost("/api/relation", relation);
relation = doPost("/api/relation", relation, EntityRelation.class);
Assert.assertTrue(edgeImitator.waitForMessages());
AbstractMessage latestMessage = edgeImitator.getLatestMessage();
Assert.assertTrue(latestMessage instanceof RelationUpdateMsg);
@ -60,21 +60,20 @@ public class RelationEdgeTest extends AbstractEdgeTest {
// delete relation
edgeImitator.expectMessageAmount(1);
doDelete("/api/relation?" +
var deletedRelation = doDelete("/api/relation?" +
"fromId=" + relation.getFrom().getId().toString() +
"&fromType=" + relation.getFrom().getEntityType().name() +
"&relationType=" + relation.getType() +
"&relationTypeGroup=" + relation.getTypeGroup().name() +
"&toId=" + relation.getTo().getId().toString() +
"&toType=" + relation.getTo().getEntityType().name())
.andExpect(status().isOk());
"&toType=" + relation.getTo().getEntityType().name(), EntityRelation.class);
Assert.assertTrue(edgeImitator.waitForMessages());
latestMessage = edgeImitator.getLatestMessage();
Assert.assertTrue(latestMessage instanceof RelationUpdateMsg);
relationUpdateMsg = (RelationUpdateMsg) latestMessage;
entityRelation = JacksonUtil.fromString(relationUpdateMsg.getEntity(), EntityRelation.class, true);
Assert.assertNotNull(entityRelation);
Assert.assertEquals(relation, entityRelation);
Assert.assertEquals(deletedRelation, entityRelation);
Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, relationUpdateMsg.getMsgType());
}
@ -119,7 +118,7 @@ public class RelationEdgeTest extends AbstractEdgeTest {
deviceToAssetRelation.setTypeGroup(RelationTypeGroup.COMMON);
edgeImitator.expectMessageAmount(1);
doPost("/api/relation", deviceToAssetRelation);
deviceToAssetRelation = doPost("/api/relation", deviceToAssetRelation, EntityRelation.class);
Assert.assertTrue(edgeImitator.waitForMessages());
EntityRelation assetToTenantRelation = new EntityRelation();

View File

@ -937,8 +937,7 @@ public class VersionControlTest extends AbstractControllerTest {
relation.setType(EntityRelation.MANAGES_TYPE);
relation.setAdditionalInfo(JacksonUtil.newObjectNode().set("a", new TextNode("b")));
relation.setTypeGroup(RelationTypeGroup.COMMON);
doPost("/api/relation", relation).andExpect(status().isOk());
return relation;
return doPost("/api/relation", relation, EntityRelation.class);
}
protected void checkImportedRuleChainData(RuleChain initialRuleChain, RuleChainMetaData initialMetaData, RuleChain importedRuleChain, RuleChainMetaData importedMetaData) {

View File

@ -47,7 +47,7 @@ public interface RelationService {
ListenableFuture<Boolean> deleteRelationAsync(TenantId tenantId, EntityRelation relation);
boolean deleteRelation(TenantId tenantId, EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup);
EntityRelation deleteRelation(TenantId tenantId, EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup);
ListenableFuture<Boolean> deleteRelationAsync(TenantId tenantId, EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup);

View File

@ -18,7 +18,10 @@ package org.thingsboard.server.common.data.relation;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.thingsboard.server.common.data.BaseDataWithAdditionalInfo;
import org.thingsboard.server.common.data.HasVersion;
@ -29,7 +32,8 @@ import java.io.Serializable;
@Slf4j
@Schema
@Data
@EqualsAndHashCode(exclude = "additionalInfoBytes")
@ToString(exclude = {"additionalInfoBytes"})
public class EntityRelation implements HasVersion, Serializable {
private static final long serialVersionUID = 2807343040519543363L;
@ -38,11 +42,17 @@ public class EntityRelation implements HasVersion, Serializable {
public static final String CONTAINS_TYPE = "Contains";
public static final String MANAGES_TYPE = "Manages";
@Setter
private EntityId from;
@Setter
private EntityId to;
@Setter
@Length(fieldName = "type")
private String type;
@Setter
private RelationTypeGroup typeGroup;
@Getter
@Setter
private Long version;
private transient JsonNode additionalInfo;
@JsonIgnore
@ -97,11 +107,6 @@ public class EntityRelation implements HasVersion, Serializable {
return typeGroup;
}
@Override
public Long getVersion() {
return version;
}
@Schema(description = "Additional parameters of the relation",implementation = com.fasterxml.jackson.databind.JsonNode.class)
public JsonNode getAdditionalInfo() {
return BaseDataWithAdditionalInfo.getJson(() -> additionalInfo, () -> additionalInfoBytes);

View File

@ -156,8 +156,8 @@ public class BaseRelationService implements RelationService {
log.trace("Executing saveRelation [{}]", relation);
validate(relation);
var result = relationDao.saveRelation(tenantId, relation);
publishEvictEvent(EntityRelationEvent.from(relation));
eventPublisher.publishEvent(new RelationActionEvent(tenantId, relation, ActionType.RELATION_ADD_OR_UPDATE));
publishEvictEvent(EntityRelationEvent.from(result));
eventPublisher.publishEvent(new RelationActionEvent(tenantId, result, ActionType.RELATION_ADD_OR_UPDATE));
return result;
}
@ -167,10 +167,11 @@ public class BaseRelationService implements RelationService {
for (EntityRelation relation : relations) {
validate(relation);
}
List<EntityRelation> savedRelations = new ArrayList<>(relations.size());
for (List<EntityRelation> partition : Lists.partition(relations, 1024)) {
relationDao.saveRelations(tenantId, partition);
savedRelations.addAll(relationDao.saveRelations(tenantId, partition));
}
for (EntityRelation relation : relations) {
for (EntityRelation relation : savedRelations) {
publishEvictEvent(EntityRelationEvent.from(relation));
eventPublisher.publishEvent(new RelationActionEvent(tenantId, relation, ActionType.RELATION_ADD_OR_UPDATE));
}
@ -182,8 +183,10 @@ public class BaseRelationService implements RelationService {
validate(relation);
var future = relationDao.saveRelationAsync(tenantId, relation);
return Futures.transform(future, savedRelation -> {
handleEvictEvent(EntityRelationEvent.from(relation));
eventPublisher.publishEvent(new RelationActionEvent(tenantId, relation, ActionType.RELATION_ADD_OR_UPDATE));
if (savedRelation != null) {
handleEvictEvent(EntityRelationEvent.from(savedRelation));
eventPublisher.publishEvent(new RelationActionEvent(tenantId, savedRelation, ActionType.RELATION_ADD_OR_UPDATE));
}
return savedRelation != null;
}, MoreExecutors.directExecutor());
}
@ -194,8 +197,8 @@ public class BaseRelationService implements RelationService {
validate(relation);
var result = relationDao.deleteRelation(tenantId, relation);
if (result != null) {
publishEvictEvent(EntityRelationEvent.from(relation));
eventPublisher.publishEvent(new RelationActionEvent(tenantId, relation, ActionType.RELATION_DELETED));
publishEvictEvent(EntityRelationEvent.from(result));
eventPublisher.publishEvent(new RelationActionEvent(tenantId, result, ActionType.RELATION_DELETED));
}
return result != null;
}
@ -207,15 +210,15 @@ public class BaseRelationService implements RelationService {
var future = relationDao.deleteRelationAsync(tenantId, relation);
return Futures.transform(future, deletedRelation -> {
if (deletedRelation != null) {
handleEvictEvent(EntityRelationEvent.from(relation));
eventPublisher.publishEvent(new RelationActionEvent(tenantId, relation, ActionType.RELATION_DELETED));
handleEvictEvent(EntityRelationEvent.from(deletedRelation));
eventPublisher.publishEvent(new RelationActionEvent(tenantId, deletedRelation, ActionType.RELATION_DELETED));
}
return deletedRelation != null;
}, MoreExecutors.directExecutor());
}
@Override
public boolean deleteRelation(TenantId tenantId, EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup) {
public EntityRelation deleteRelation(TenantId tenantId, EntityId from, EntityId to, String relationType, RelationTypeGroup typeGroup) {
log.trace("Executing deleteRelation [{}][{}][{}][{}]", from, to, relationType, typeGroup);
validate(from, to, relationType, typeGroup);
var result = relationDao.deleteRelation(tenantId, from, to, relationType, typeGroup);
@ -223,7 +226,7 @@ public class BaseRelationService implements RelationService {
publishEvictEvent(EntityRelationEvent.from(result));
eventPublisher.publishEvent(new RelationActionEvent(tenantId, result, ActionType.RELATION_DELETED));
}
return result != null;
return result;
}
@Override