updated GET /resources api to filter resources by type

This commit is contained in:
dashevchenko 2023-05-17 15:10:57 +03:00
parent 07ff1ab44a
commit b083ab98e0
11 changed files with 221 additions and 3 deletions

View File

@ -140,6 +140,9 @@ public class ControllerConstants {
protected static final String RESOURCE_TEXT_SEARCH_DESCRIPTION = "The case insensitive 'substring' filter based on the resource title."; protected static final String RESOURCE_TEXT_SEARCH_DESCRIPTION = "The case insensitive 'substring' filter based on the resource title.";
protected static final String RESOURCE_SORT_PROPERTY_ALLOWABLE_VALUES = "createdTime, title, resourceType, tenantId"; protected static final String RESOURCE_SORT_PROPERTY_ALLOWABLE_VALUES = "createdTime, title, resourceType, tenantId";
protected static final String RESOURCE_TYPE_PROPERTY_ALLOWABLE_VALUES = "LWM2M_MODEL, JKS, PKCS_12, JS_MODULE";
protected static final String RESOURCE_TYPE = "A string value representing the resource type.";
protected static final String LWM2M_OBJECT_DESCRIPTION = "LwM2M Object is a object that includes information about the LwM2M model which can be used in transport configuration for the LwM2M device profile. "; protected static final String LWM2M_OBJECT_DESCRIPTION = "LwM2M Object is a object that includes information about the LwM2M model which can be used in transport configuration for the LwM2M device profile. ";
protected static final String LWM2M_OBJECT_SORT_PROPERTY_ALLOWABLE_VALUES = "id, name"; protected static final String LWM2M_OBJECT_SORT_PROPERTY_ALLOWABLE_VALUES = "id, name";

View File

@ -31,6 +31,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.thingsboard.server.common.data.ResourceType;
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.TbResourceInfo; import org.thingsboard.server.common.data.TbResourceInfo;
import org.thingsboard.server.common.data.exception.ThingsboardException; import org.thingsboard.server.common.data.exception.ThingsboardException;
@ -57,6 +59,8 @@ import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_ID_
import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_INFO_DESCRIPTION; import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_INFO_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_SORT_PROPERTY_ALLOWABLE_VALUES; import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_SORT_PROPERTY_ALLOWABLE_VALUES;
import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_TEXT_SEARCH_DESCRIPTION; import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_TEXT_SEARCH_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_TYPE;
import static org.thingsboard.server.controller.ControllerConstants.RESOURCE_TYPE_PROPERTY_ALLOWABLE_VALUES;
import static org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_ALLOWABLE_VALUES; import static org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_ALLOWABLE_VALUES;
import static org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_DESCRIPTION; import static org.thingsboard.server.controller.ControllerConstants.SORT_ORDER_DESCRIPTION;
import static org.thingsboard.server.controller.ControllerConstants.SORT_PROPERTY_DESCRIPTION; import static org.thingsboard.server.controller.ControllerConstants.SORT_PROPERTY_DESCRIPTION;
@ -153,6 +157,8 @@ public class TbResourceController extends BaseController {
@RequestParam int pageSize, @RequestParam int pageSize,
@ApiParam(value = PAGE_NUMBER_DESCRIPTION, required = true) @ApiParam(value = PAGE_NUMBER_DESCRIPTION, required = true)
@RequestParam int page, @RequestParam int page,
@ApiParam(value = RESOURCE_TYPE, allowableValues = RESOURCE_TYPE_PROPERTY_ALLOWABLE_VALUES)
@RequestParam(required = false) String resourceType,
@ApiParam(value = RESOURCE_TEXT_SEARCH_DESCRIPTION) @ApiParam(value = RESOURCE_TEXT_SEARCH_DESCRIPTION)
@RequestParam(required = false) String textSearch, @RequestParam(required = false) String textSearch,
@ApiParam(value = SORT_PROPERTY_DESCRIPTION, allowableValues = RESOURCE_SORT_PROPERTY_ALLOWABLE_VALUES) @ApiParam(value = SORT_PROPERTY_DESCRIPTION, allowableValues = RESOURCE_SORT_PROPERTY_ALLOWABLE_VALUES)
@ -161,9 +167,17 @@ public class TbResourceController extends BaseController {
@RequestParam(required = false) String sortOrder) throws ThingsboardException { @RequestParam(required = false) String sortOrder) throws ThingsboardException {
PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder); PageLink pageLink = createPageLink(pageSize, page, textSearch, sortProperty, sortOrder);
if (Authority.SYS_ADMIN.equals(getCurrentUser().getAuthority())) { if (Authority.SYS_ADMIN.equals(getCurrentUser().getAuthority())) {
return checkNotNull(resourceService.findTenantResourcesByTenantId(getTenantId(), pageLink)); if (StringUtils.isNotEmpty(resourceType)){
return checkNotNull(resourceService.findTenantResourcesByType(getTenantId(), ResourceType.valueOf(resourceType), pageLink));
} else {
return checkNotNull(resourceService.findTenantResourcesByTenantId(getTenantId(), pageLink));
}
} else { } else {
return checkNotNull(resourceService.findAllTenantResourcesByTenantId(getTenantId(), pageLink)); if (StringUtils.isNotEmpty(resourceType)){
return checkNotNull(resourceService.findAllTenantResourcesByType(getTenantId(), ResourceType.valueOf(resourceType), pageLink));
} else {
return checkNotNull(resourceService.findAllTenantResourcesByTenantId(getTenantId(), pageLink));
}
} }
} }

View File

@ -85,6 +85,16 @@ public class DefaultTbResourceService extends AbstractTbEntityService implements
return resourceService.findTenantResourcesByTenantId(tenantId, pageLink); return resourceService.findTenantResourcesByTenantId(tenantId, pageLink);
} }
@Override
public PageData<TbResourceInfo> findAllTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink) {
return resourceService.findAllTenantResourcesByType(tenantId, resourceType, pageLink);
}
@Override
public PageData<TbResourceInfo> findTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink) {
return resourceService.findTenantResourcesByType(tenantId, resourceType, pageLink);
}
@Override @Override
public List<LwM2mObject> findLwM2mObject(TenantId tenantId, String sortOrder, String sortProperty, String[] objectIds) { public List<LwM2mObject> findLwM2mObject(TenantId tenantId, String sortOrder, String sortProperty, String[] objectIds) {
log.trace("Executing findByTenantId [{}]", tenantId); log.trace("Executing findByTenantId [{}]", tenantId);

View File

@ -39,6 +39,10 @@ public interface TbResourceService extends SimpleTbEntityService<TbResource> {
PageData<TbResourceInfo> findTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink); PageData<TbResourceInfo> findTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink);
PageData<TbResourceInfo> findAllTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink);
PageData<TbResourceInfo> findTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink);
List<LwM2mObject> findLwM2mObject(TenantId tenantId, List<LwM2mObject> findLwM2mObject(TenantId tenantId,
String sortOrder, String sortOrder,
String sortProperty, String sortProperty,

View File

@ -47,6 +47,7 @@ public class TbResourceControllerTest extends AbstractControllerTest {
private IdComparator<TbResourceInfo> idComparator = new IdComparator<>(); private IdComparator<TbResourceInfo> idComparator = new IdComparator<>();
private static final String DEFAULT_FILE_NAME = "test.jks"; private static final String DEFAULT_FILE_NAME = "test.jks";
private static final String DEFAULT_FILE_NAME_2 = "test2.jks";
private Tenant savedTenant; private Tenant savedTenant;
private User tenantAdmin; private User tenantAdmin;
@ -242,6 +243,54 @@ public class TbResourceControllerTest extends AbstractControllerTest {
Assert.assertEquals(resources, loadedResources); Assert.assertEquals(resources, loadedResources);
} }
@Test
public void testFindTenantTbResourcesByType() throws Exception {
Mockito.reset(tbClusterService, auditLogService);
List<TbResourceInfo> resources = new ArrayList<>();
int jksCntEntity = 17;
for (int i = 0; i < jksCntEntity; i++) {
TbResource resource = new TbResource();
resource.setTitle("JKS Resource" + i);
resource.setResourceType(ResourceType.JKS);
resource.setFileName(i + DEFAULT_FILE_NAME);
resource.setData("Test Data");
resources.add(new TbResourceInfo(save(resource)));
}
int lwm2mCntEntity = 19;
for (int i = 0; i < lwm2mCntEntity; i++) {
TbResource resource = new TbResource();
resource.setTitle("LWM2M Resource" + i);
resource.setResourceType(ResourceType.PKCS_12);
resource.setFileName(i + DEFAULT_FILE_NAME_2);
resource.setData("Test Data");
save(resource);
}
List<TbResourceInfo> loadedResources = new ArrayList<>();
PageLink pageLink = new PageLink(5);
PageData<TbResourceInfo> pageData;
do {
pageData = doGetTypedWithPageLink("/api/resource?resourceType=" + ResourceType.JKS.name() + "&",
new TypeReference<>() {
}, pageLink);
loadedResources.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink();
}
} while (pageData.hasNext());
testNotifyManyEntityManyTimeMsgToEdgeServiceNever(new TbResource(), new TbResource(),
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(),
ActionType.ADDED, jksCntEntity + lwm2mCntEntity);
Collections.sort(resources, idComparator);
Collections.sort(loadedResources, idComparator);
Assert.assertEquals(resources, loadedResources);
}
@Test @Test
public void testFindSystemTbResources() throws Exception { public void testFindSystemTbResources() throws Exception {
loginSysAdmin(); loginSysAdmin();
@ -300,6 +349,76 @@ public class TbResourceControllerTest extends AbstractControllerTest {
Assert.assertTrue(loadedResources.isEmpty()); Assert.assertTrue(loadedResources.isEmpty());
} }
@Test
public void testFindSystemTbResourcesByType() throws Exception {
loginSysAdmin();
List<TbResourceInfo> resources = new ArrayList<>();
int jksCntEntity = 17;
for (int i = 0; i < jksCntEntity; i++) {
TbResource resource = new TbResource();
resource.setTitle("JKS Resource" + i);
resource.setResourceType(ResourceType.JKS);
resource.setFileName(i + DEFAULT_FILE_NAME);
resource.setData("Test Data");
resources.add(new TbResourceInfo(save(resource)));
}
int lwm2mCntEntity = 19;
for (int i = 0; i < lwm2mCntEntity; i++) {
TbResource resource = new TbResource();
resource.setTitle("LWM2M Resource" + i);
resource.setResourceType(ResourceType.PKCS_12);
resource.setFileName(i + DEFAULT_FILE_NAME_2);
resource.setData("Test Data");
save(resource);
}
List<TbResourceInfo> loadedResources = new ArrayList<>();
PageLink pageLink = new PageLink(30);
PageData<TbResourceInfo> pageData;
do {
pageData = doGetTypedWithPageLink("/api/resource?resourceType=" + ResourceType.JKS + "&",
new TypeReference<>() {
}, pageLink);
loadedResources.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink();
}
} while (pageData.hasNext());
Collections.sort(resources, idComparator);
Collections.sort(loadedResources, idComparator);
Assert.assertEquals(resources, loadedResources);
Mockito.reset(tbClusterService, auditLogService);
int cntEntity = resources.size();
for (TbResourceInfo resource : resources) {
doDelete("/api/resource/" + resource.getId().getId().toString())
.andExpect(status().isOk());
}
testNotifyManyEntityManyTimeMsgToEdgeServiceNeverAdditionalInfoAny(new TbResource(), new TbResource(),
resources.get(0).getTenantId(), null, null, SYS_ADMIN_EMAIL,
ActionType.DELETED, cntEntity, 1);
pageLink = new PageLink(27);
loadedResources.clear();
do {
pageData = doGetTypedWithPageLink("/api/resource?resourceType=" + ResourceType.JKS + "&",
new TypeReference<>() {
}, pageLink);
loadedResources.addAll(pageData.getData());
if (pageData.hasNext()) {
pageLink = pageLink.nextPageLink();
}
} while (pageData.hasNext());
Assert.assertTrue(loadedResources.isEmpty());
}
@Test @Test
public void testFindSystemAndTenantTbResources() throws Exception { public void testFindSystemAndTenantTbResources() throws Exception {
List<TbResourceInfo> systemResources = new ArrayList<>(); List<TbResourceInfo> systemResources = new ArrayList<>();

View File

@ -43,6 +43,10 @@ public interface ResourceService extends EntityDaoService {
PageData<TbResourceInfo> findTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink); PageData<TbResourceInfo> findTenantResourcesByTenantId(TenantId tenantId, PageLink pageLink);
PageData<TbResourceInfo> findAllTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink);
PageData<TbResourceInfo> findTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink);
List<TbResource> findTenantResourcesByResourceTypeAndObjectIds(TenantId tenantId, ResourceType lwm2mModel, String[] objectIds); List<TbResource> findTenantResourcesByResourceTypeAndObjectIds(TenantId tenantId, ResourceType lwm2mModel, String[] objectIds);
PageData<TbResource> findTenantResourcesByResourceTypeAndPageLink(TenantId tenantId, ResourceType lwm2mModel, PageLink pageLink); PageData<TbResource> findTenantResourcesByResourceTypeAndPageLink(TenantId tenantId, ResourceType lwm2mModel, PageLink pageLink);

View File

@ -16,5 +16,5 @@
package org.thingsboard.server.common.data; package org.thingsboard.server.common.data;
public enum ResourceType { public enum ResourceType {
LWM2M_MODEL, JKS, PKCS_12 LWM2M_MODEL, JKS, PKCS_12, JS_MODULE
} }

View File

@ -116,6 +116,20 @@ public class BaseResourceService implements ResourceService {
return resourceInfoDao.findTenantResourcesByTenantId(tenantId.getId(), pageLink); return resourceInfoDao.findTenantResourcesByTenantId(tenantId.getId(), pageLink);
} }
@Override
public PageData<TbResourceInfo> findAllTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink) {
log.trace("Executing findAllTenantResourcesByType [{}]", tenantId);
validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
return resourceInfoDao.findAllTenantResourcesByType(tenantId.getId(), resourceType.name(), pageLink);
}
@Override
public PageData<TbResourceInfo> findTenantResourcesByType(TenantId tenantId, ResourceType resourceType, PageLink pageLink) {
log.trace("Executing findTenantResourcesByType [{}]", tenantId);
validateId(tenantId, INCORRECT_TENANT_ID + tenantId);
return resourceInfoDao.findTenantResourcesByType(tenantId.getId(), resourceType.name(), pageLink);
}
@Override @Override
public List<TbResource> findTenantResourcesByResourceTypeAndObjectIds(TenantId tenantId, ResourceType resourceType, String[] objectIds) { public List<TbResource> findTenantResourcesByResourceTypeAndObjectIds(TenantId tenantId, ResourceType resourceType, String[] objectIds) {
log.trace("Executing findTenantResourcesByResourceTypeAndObjectIds [{}][{}][{}]", tenantId, resourceType, objectIds); log.trace("Executing findTenantResourcesByResourceTypeAndObjectIds [{}][{}][{}]", tenantId, resourceType, objectIds);

View File

@ -28,4 +28,8 @@ public interface TbResourceInfoDao extends Dao<TbResourceInfo> {
PageData<TbResourceInfo> findTenantResourcesByTenantId(UUID tenantId, PageLink pageLink); PageData<TbResourceInfo> findTenantResourcesByTenantId(UUID tenantId, PageLink pageLink);
PageData<TbResourceInfo> findAllTenantResourcesByType(UUID tenantId, String resourceType, PageLink pageLink);
PageData<TbResourceInfo> findTenantResourcesByType(UUID tenantId, String resourceType, PageLink pageLink);
} }

View File

@ -68,4 +68,25 @@ public class JpaTbResourceInfoDao extends JpaAbstractSearchTextDao<TbResourceInf
Objects.toString(pageLink.getTextSearch(), ""), Objects.toString(pageLink.getTextSearch(), ""),
DaoUtil.toPageable(pageLink))); DaoUtil.toPageable(pageLink)));
} }
@Override
public PageData<TbResourceInfo> findAllTenantResourcesByType(UUID tenantId, String resourceType, PageLink pageLink) {
return DaoUtil.toPageData(resourceInfoRepository
.findAllTenantResourcesByType(
tenantId,
TenantId.NULL_UUID,
resourceType,
Objects.toString(pageLink.getTextSearch(), ""),
DaoUtil.toPageable(pageLink)));
}
@Override
public PageData<TbResourceInfo> findTenantResourcesByType(UUID tenantId, String resourceType, PageLink pageLink) {
return DaoUtil.toPageData(resourceInfoRepository
.findTenantResourcesByType(
tenantId,
resourceType,
Objects.toString(pageLink.getTextSearch(), ""),
DaoUtil.toPageable(pageLink)));
}
} }

View File

@ -46,4 +46,29 @@ public interface TbResourceInfoRepository extends JpaRepository<TbResourceInfoEn
Page<TbResourceInfoEntity> findTenantResourcesByTenantId(@Param("tenantId") UUID tenantId, Page<TbResourceInfoEntity> findTenantResourcesByTenantId(@Param("tenantId") UUID tenantId,
@Param("searchText") String searchText, @Param("searchText") String searchText,
Pageable pageable); Pageable pageable);
@Query("SELECT tr FROM TbResourceInfoEntity tr WHERE " +
"LOWER(tr.title) LIKE LOWER(CONCAT('%', :searchText, '%'))" +
"AND (tr.tenantId = :tenantId " +
"OR (tr.tenantId = :systemAdminId " +
"AND NOT EXISTS " +
"(SELECT sr FROM TbResourceEntity sr " +
"WHERE sr.tenantId = :tenantId " +
"AND tr.resourceType = sr.resourceType " +
"AND tr.resourceKey = sr.resourceKey)))" +
"AND tr.resourceType = :resourceType")
Page<TbResourceInfoEntity> findAllTenantResourcesByType(@Param("tenantId") UUID tenantId,
@Param("systemAdminId") UUID sysadminId,
@Param("resourceType") String resourceType,
@Param("searchText") String searchText,
Pageable pageable);
@Query("SELECT ri FROM TbResourceInfoEntity ri WHERE " +
"ri.tenantId = :tenantId " +
"AND ri.resourceType = :resourceType " +
"AND LOWER(ri.title) LIKE LOWER(CONCAT('%', :searchText, '%'))")
Page<TbResourceInfoEntity> findTenantResourcesByType(@Param("tenantId") UUID tenantId,
@Param("resourceType") String resourceType,
@Param("searchText") String searchText,
Pageable pageable);
} }