added test
This commit is contained in:
parent
9ddc5e5b8d
commit
cce56b9547
@ -26,6 +26,6 @@ public interface SimpleTbEntityService<T> {
|
|||||||
|
|
||||||
T save(T entity, User user) throws Exception;
|
T save(T entity, User user) throws Exception;
|
||||||
|
|
||||||
void delete(T entity, User user) throws ThingsboardException;
|
void delete(T entity, User user);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -151,15 +151,10 @@ public class DefaultTbResourceService extends AbstractTbEntityService implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(TbResource tbResource, User user) throws ThingsboardException {
|
public void delete(TbResource tbResource, User user) {
|
||||||
TbResourceId resourceId = tbResource.getId();
|
TbResourceId resourceId = tbResource.getId();
|
||||||
TenantId tenantId = tbResource.getTenantId();
|
TenantId tenantId = tbResource.getTenantId();
|
||||||
try {
|
try {
|
||||||
List<WidgetTypeDetails> widgets = widgetTypeService.findWidgetTypesInfosByTenantIdAndResourceId(tenantId, resourceId);
|
|
||||||
if (!widgets.isEmpty()) {
|
|
||||||
List<String> widgetNames = widgets.stream().map(BaseWidgetType::getName).collect(Collectors.toList());
|
|
||||||
throw new ThingsboardException(String.format("Following widget types uses current resource: %s", widgetNames), ThingsboardErrorCode.GENERAL);
|
|
||||||
}
|
|
||||||
resourceService.deleteResource(tenantId, resourceId);
|
resourceService.deleteResource(tenantId, resourceId);
|
||||||
tbClusterService.onResourceDeleted(tbResource, null);
|
tbClusterService.onResourceDeleted(tbResource, null);
|
||||||
notificationEntityService.logEntityAction(tenantId, resourceId, tbResource, ActionType.DELETED, user, resourceId.toString());
|
notificationEntityService.logEntityAction(tenantId, resourceId, tbResource, ActionType.DELETED, user, resourceId.toString());
|
||||||
|
|||||||
@ -38,6 +38,8 @@ import org.thingsboard.server.common.data.audit.ActionType;
|
|||||||
import org.thingsboard.server.common.data.page.PageData;
|
import org.thingsboard.server.common.data.page.PageData;
|
||||||
import org.thingsboard.server.common.data.page.PageLink;
|
import org.thingsboard.server.common.data.page.PageLink;
|
||||||
import org.thingsboard.server.common.data.security.Authority;
|
import org.thingsboard.server.common.data.security.Authority;
|
||||||
|
import org.thingsboard.server.common.data.widget.WidgetTypeDetails;
|
||||||
|
import org.thingsboard.server.common.data.widget.WidgetsBundle;
|
||||||
import org.thingsboard.server.dao.exception.DataValidationException;
|
import org.thingsboard.server.dao.exception.DataValidationException;
|
||||||
import org.thingsboard.server.dao.service.DaoSqlTest;
|
import org.thingsboard.server.dao.service.DaoSqlTest;
|
||||||
|
|
||||||
@ -216,6 +218,36 @@ public class TbResourceControllerTest extends AbstractControllerTest {
|
|||||||
.andExpect(statusReason(containsString(msgErrorNoFound("Resource", resourceIdStr))));
|
.andExpect(statusReason(containsString(msgErrorNoFound("Resource", resourceIdStr))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testShoudNotDeleteTbResourceIfAssignedToWidgetType() throws Exception {
|
||||||
|
TbResource resource = new TbResource();
|
||||||
|
resource.setResourceType(ResourceType.JKS);
|
||||||
|
resource.setTitle("My first resource");
|
||||||
|
resource.setFileName(DEFAULT_FILE_NAME);
|
||||||
|
resource.setData(TEST_DATA);
|
||||||
|
|
||||||
|
TbResource savedResource = save(resource);
|
||||||
|
|
||||||
|
Mockito.reset(tbClusterService, auditLogService);
|
||||||
|
String resourceIdStr = savedResource.getId().getId().toString();
|
||||||
|
|
||||||
|
//create widget type
|
||||||
|
WidgetsBundle widgetsBundle = new WidgetsBundle();
|
||||||
|
widgetsBundle.setTitle("My widgets bundle");
|
||||||
|
WidgetsBundle savedWidgetsBundle = doPost("/api/widgetsBundle", widgetsBundle, WidgetsBundle.class);
|
||||||
|
|
||||||
|
WidgetTypeDetails widgetType = new WidgetTypeDetails();
|
||||||
|
widgetType.setBundleAlias(savedWidgetsBundle.getAlias());
|
||||||
|
widgetType.setName("Widget Type");
|
||||||
|
widgetType.setDescriptor(JacksonUtil.fromString(String.format("{ \"resources\": [{\"url\":{\"entityType\":\"TB_RESOURCE\",\"id\":\"%s\"},\"isModule\":true}]}", savedResource.getId()), JsonNode.class));
|
||||||
|
doPost("/api/widgetType", widgetType, WidgetTypeDetails.class);
|
||||||
|
|
||||||
|
doDelete("/api/resource/" + resourceIdStr)
|
||||||
|
.andExpect(status().isBadRequest())
|
||||||
|
.andExpect(statusReason(containsString("Following widget types uses current resource: ["
|
||||||
|
+ widgetType .getName()+ "]")));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFindTenantTbResources() throws Exception {
|
public void testFindTenantTbResources() throws Exception {
|
||||||
|
|
||||||
|
|||||||
@ -109,6 +109,7 @@ public class BaseResourceService extends AbstractCachedEntityService<ResourceInf
|
|||||||
public void deleteResource(TenantId tenantId, TbResourceId resourceId) {
|
public void deleteResource(TenantId tenantId, TbResourceId resourceId) {
|
||||||
log.trace("Executing deleteResource [{}] [{}]", tenantId, resourceId);
|
log.trace("Executing deleteResource [{}] [{}]", tenantId, resourceId);
|
||||||
Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);
|
Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId);
|
||||||
|
resourceValidator.validateDelete(tenantId, resourceId);
|
||||||
resourceDao.removeById(tenantId, resourceId.getId());
|
resourceDao.removeById(tenantId, resourceId.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -22,6 +22,7 @@ import org.springframework.context.annotation.Lazy;
|
|||||||
import org.thingsboard.server.common.data.BaseData;
|
import org.thingsboard.server.common.data.BaseData;
|
||||||
import org.thingsboard.server.common.data.EntityType;
|
import org.thingsboard.server.common.data.EntityType;
|
||||||
import org.thingsboard.server.common.data.StringUtils;
|
import org.thingsboard.server.common.data.StringUtils;
|
||||||
|
import org.thingsboard.server.common.data.id.EntityId;
|
||||||
import org.thingsboard.server.common.data.id.TenantId;
|
import org.thingsboard.server.common.data.id.TenantId;
|
||||||
import org.thingsboard.server.dao.TenantEntityWithDataDao;
|
import org.thingsboard.server.dao.TenantEntityWithDataDao;
|
||||||
import org.thingsboard.server.dao.exception.DataValidationException;
|
import org.thingsboard.server.dao.exception.DataValidationException;
|
||||||
@ -82,6 +83,9 @@ public abstract class DataValidator<D extends BaseData<?>> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void validateDelete(TenantId tenantId, EntityId entityId) {
|
||||||
|
}
|
||||||
|
|
||||||
protected boolean isSameData(D existentData, D actualData) {
|
protected boolean isSameData(D existentData, D actualData) {
|
||||||
return actualData.getId() != null && existentData.getId().equals(actualData.getId());
|
return actualData.getId() != null && existentData.getId().equals(actualData.getId());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,14 +20,21 @@ import org.springframework.context.annotation.Lazy;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.thingsboard.server.common.data.StringUtils;
|
import org.thingsboard.server.common.data.StringUtils;
|
||||||
import org.thingsboard.server.common.data.TbResource;
|
import org.thingsboard.server.common.data.TbResource;
|
||||||
|
import org.thingsboard.server.common.data.id.EntityId;
|
||||||
import org.thingsboard.server.common.data.id.TenantId;
|
import org.thingsboard.server.common.data.id.TenantId;
|
||||||
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
|
import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration;
|
||||||
|
import org.thingsboard.server.common.data.widget.BaseWidgetType;
|
||||||
|
import org.thingsboard.server.common.data.widget.WidgetTypeDetails;
|
||||||
import org.thingsboard.server.dao.exception.DataValidationException;
|
import org.thingsboard.server.dao.exception.DataValidationException;
|
||||||
import org.thingsboard.server.dao.model.ModelConstants;
|
import org.thingsboard.server.dao.model.ModelConstants;
|
||||||
import org.thingsboard.server.dao.resource.TbResourceDao;
|
import org.thingsboard.server.dao.resource.TbResourceDao;
|
||||||
import org.thingsboard.server.dao.service.DataValidator;
|
import org.thingsboard.server.dao.service.DataValidator;
|
||||||
import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
|
import org.thingsboard.server.dao.tenant.TbTenantProfileCache;
|
||||||
import org.thingsboard.server.dao.tenant.TenantService;
|
import org.thingsboard.server.dao.tenant.TenantService;
|
||||||
|
import org.thingsboard.server.dao.widget.WidgetTypeDao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static org.thingsboard.server.common.data.EntityType.TB_RESOURCE;
|
import static org.thingsboard.server.common.data.EntityType.TB_RESOURCE;
|
||||||
|
|
||||||
@ -37,6 +44,9 @@ public class ResourceDataValidator extends DataValidator<TbResource> {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private TbResourceDao resourceDao;
|
private TbResourceDao resourceDao;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private WidgetTypeDao widgetTypeDao;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private TenantService tenantService;
|
private TenantService tenantService;
|
||||||
|
|
||||||
@ -77,4 +87,15 @@ public class ResourceDataValidator extends DataValidator<TbResource> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validateDelete(TenantId tenantId, EntityId resourceId) {
|
||||||
|
List<WidgetTypeDetails> widgets = widgetTypeDao.findWidgetTypesInfosByTenantIdAndResourceId(tenantId.getId(),
|
||||||
|
resourceId.getId());
|
||||||
|
if (!widgets.isEmpty()) {
|
||||||
|
List<String> widgetNames = widgets.stream().map(BaseWidgetType::getName).collect(Collectors.toList());
|
||||||
|
throw new DataValidationException(String.format("Following widget types uses current resource: %s", widgetNames));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user