From 38e628643047227484aeabcf90061c4627cb4cb6 Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Wed, 1 Jun 2022 00:32:16 +0300 Subject: [PATCH] refactoring: 09_TbResourceController: fix bug test save resource --- .../controller/TbResourceController.java | 4 +- .../entitiy/AbstractTbEntityService.java | 3 + .../DefaultTbResourceNotifyService.java | 71 +++++++++++++++++++ .../resource/TbResourceNotifyService.java | 22 ++++++ .../resource/DefaultTbResourceService.java | 40 +---------- .../service/resource/TbResourceService.java | 3 +- 6 files changed, 100 insertions(+), 43 deletions(-) create mode 100644 application/src/main/java/org/thingsboard/server/service/entitiy/resource/DefaultTbResourceNotifyService.java create mode 100644 application/src/main/java/org/thingsboard/server/service/entitiy/resource/TbResourceNotifyService.java diff --git a/application/src/main/java/org/thingsboard/server/controller/TbResourceController.java b/application/src/main/java/org/thingsboard/server/controller/TbResourceController.java index 120449c137..21d7eadb69 100644 --- a/application/src/main/java/org/thingsboard/server/controller/TbResourceController.java +++ b/application/src/main/java/org/thingsboard/server/controller/TbResourceController.java @@ -40,7 +40,7 @@ import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.security.Authority; import org.thingsboard.server.queue.util.TbCoreComponent; -import org.thingsboard.server.service.resource.TbResourceService; +import org.thingsboard.server.service.entitiy.resource.TbResourceNotifyService; import org.thingsboard.server.service.security.permission.Operation; import org.thingsboard.server.service.security.permission.Resource; @@ -71,7 +71,7 @@ import static org.thingsboard.server.controller.ControllerConstants.UUID_WIKI_LI @RequiredArgsConstructor public class TbResourceController extends BaseController { - private final TbResourceService tbResourceService; + private final TbResourceNotifyService tbResourceService; public static final String RESOURCE_ID = "resourceId"; diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/AbstractTbEntityService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/AbstractTbEntityService.java index 7694f3f8f4..f3e33076c7 100644 --- a/application/src/main/java/org/thingsboard/server/service/entitiy/AbstractTbEntityService.java +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/AbstractTbEntityService.java @@ -65,6 +65,7 @@ import org.thingsboard.server.service.edge.EdgeNotificationService; import org.thingsboard.server.service.executors.DbCallbackExecutorService; import org.thingsboard.server.service.install.InstallScripts; import org.thingsboard.server.service.ota.OtaPackageStateService; +import org.thingsboard.server.service.resource.TbResourceService; import org.thingsboard.server.service.rule.TbRuleChainService; import org.thingsboard.server.service.security.permission.AccessControlService; import org.thingsboard.server.service.telemetry.TelemetrySubscriptionService; @@ -144,6 +145,8 @@ public abstract class AbstractTbEntityService { protected InstallScripts installScripts; @Autowired protected UserService userService; + @Autowired + protected TbResourceService resourceService; protected ListenableFuture removeAlarmsByEntityId(TenantId tenantId, EntityId entityId) { ListenableFuture> alarmsFuture = diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/resource/DefaultTbResourceNotifyService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/resource/DefaultTbResourceNotifyService.java new file mode 100644 index 0000000000..beb1707214 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/resource/DefaultTbResourceNotifyService.java @@ -0,0 +1,71 @@ +/** + * Copyright © 2016-2022 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.service.entitiy.resource; + + +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.thingsboard.server.common.data.EntityType; +import org.thingsboard.server.common.data.TbResource; +import org.thingsboard.server.common.data.audit.ActionType; +import org.thingsboard.server.common.data.exception.ThingsboardException; +import org.thingsboard.server.common.data.id.TbResourceId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.queue.util.TbCoreComponent; +import org.thingsboard.server.service.entitiy.AbstractTbEntityService; +import org.thingsboard.server.service.security.model.SecurityUser; + +@Service +@TbCoreComponent +@AllArgsConstructor +@Slf4j +public class DefaultTbResourceNotifyService extends AbstractTbEntityService implements TbResourceNotifyService { + + @Override + public TbResource save(TbResource tbResource, SecurityUser user) throws ThingsboardException { + ActionType actionType = tbResource.getId() == null ? ActionType.ADDED : ActionType.UPDATED; + TenantId tenantId = tbResource.getTenantId(); + try { + + TbResource savedResource = checkNotNull(resourceService.saveResource(tbResource)); + tbClusterService.onResourceChange(savedResource, null); + notificationEntityService.notifyCreateOrUpdateOrDelete(tenantId, null, savedResource.getId(), + savedResource, user, actionType, false, null); + return savedResource; + } catch (Exception e) { + notificationEntityService.notifyCreateOrUpdateOrDelete(tenantId, null, emptyId(EntityType.TB_RESOURCE), + tbResource, user, actionType, false, e); + throw handleException(e); + } + } + + @Override + public void delete(TbResource tbResource, SecurityUser user) throws ThingsboardException { + TbResourceId resourceId = tbResource.getId(); + TenantId tenantId = tbResource.getTenantId(); + try { + resourceService.deleteResource(tenantId, resourceId); + tbClusterService.onResourceDeleted(tbResource, null); + notificationEntityService.notifyCreateOrUpdateOrDelete(tenantId, null, resourceId, tbResource, user, ActionType.DELETED, + false, null, resourceId.toString()); + } catch (Exception e) { + notificationEntityService.notifyCreateOrUpdateOrDelete(tenantId, null, emptyId(EntityType.TB_RESOURCE), null, user, ActionType.DELETED, + false, e, resourceId.toString()); + throw handleException(e); + } + } +} diff --git a/application/src/main/java/org/thingsboard/server/service/entitiy/resource/TbResourceNotifyService.java b/application/src/main/java/org/thingsboard/server/service/entitiy/resource/TbResourceNotifyService.java new file mode 100644 index 0000000000..1605d745c2 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/entitiy/resource/TbResourceNotifyService.java @@ -0,0 +1,22 @@ +/** + * Copyright © 2016-2022 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.service.entitiy.resource; + +import org.thingsboard.server.common.data.TbResource; +import org.thingsboard.server.service.entitiy.SimpleTbEntityService; + +public interface TbResourceNotifyService extends SimpleTbEntityService { +} diff --git a/application/src/main/java/org/thingsboard/server/service/resource/DefaultTbResourceService.java b/application/src/main/java/org/thingsboard/server/service/resource/DefaultTbResourceService.java index 811eaf1774..fab78afc70 100644 --- a/application/src/main/java/org/thingsboard/server/service/resource/DefaultTbResourceService.java +++ b/application/src/main/java/org/thingsboard/server/service/resource/DefaultTbResourceService.java @@ -22,11 +22,9 @@ import org.eclipse.leshan.core.model.DefaultDDFFileValidator; import org.eclipse.leshan.core.model.InvalidDDFFileException; import org.eclipse.leshan.core.model.ObjectModel; import org.springframework.stereotype.Service; -import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.ResourceType; import org.thingsboard.server.common.data.TbResource; import org.thingsboard.server.common.data.TbResourceInfo; -import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; import org.thingsboard.server.common.data.exception.ThingsboardException; import org.thingsboard.server.common.data.id.TbResourceId; @@ -38,8 +36,6 @@ import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.dao.exception.DataValidationException; import org.thingsboard.server.dao.resource.ResourceService; -import org.thingsboard.server.service.entitiy.AbstractTbEntityService; -import org.thingsboard.server.service.security.model.SecurityUser; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -57,7 +53,7 @@ import static org.thingsboard.server.dao.service.Validator.validateId; @Slf4j @Service -public class DefaultTbResourceService extends AbstractTbEntityService implements TbResourceService { +public class DefaultTbResourceService implements TbResourceService { private final ResourceService resourceService; private final DDFFileParser ddfFileParser; @@ -219,38 +215,4 @@ public class DefaultTbResourceService extends AbstractTbEntityService implements return null; } } - - @Override - public TbResource save(TbResource tbResource, SecurityUser user) throws ThingsboardException { - ActionType actionType = tbResource.getId() == null ? ActionType.ADDED : ActionType.UPDATED; - TenantId tenantId = tbResource.getTenantId(); - try { - - TbResource savedResource = checkNotNull(resourceService.saveResource(tbResource)); - tbClusterService.onResourceChange(savedResource, null); - notificationEntityService.notifyCreateOrUpdateOrDelete(tenantId, null, savedResource.getId(), - savedResource, user, actionType, false, null); - return savedResource; - } catch (Exception e) { - notificationEntityService.notifyCreateOrUpdateOrDelete(tenantId, null, emptyId(EntityType.TB_RESOURCE), - tbResource, user, actionType, false, e); - throw handleException(e); - } - } - - @Override - public void delete(TbResource tbResource, SecurityUser user) throws ThingsboardException { - TbResourceId resourceId = tbResource.getId(); - TenantId tenantId = tbResource.getTenantId(); - try { - resourceService.deleteResource(tenantId, resourceId); - tbClusterService.onResourceDeleted(tbResource, null); - notificationEntityService.notifyCreateOrUpdateOrDelete(tenantId, null, resourceId, tbResource, user, ActionType.DELETED, - false, null, resourceId.toString()); - } catch (Exception e) { - notificationEntityService.notifyCreateOrUpdateOrDelete(tenantId, null, emptyId(EntityType.TB_RESOURCE), null, user, ActionType.DELETED, - false, e, resourceId.toString()); - throw handleException(e); - } - } } diff --git a/application/src/main/java/org/thingsboard/server/service/resource/TbResourceService.java b/application/src/main/java/org/thingsboard/server/service/resource/TbResourceService.java index 06120f3b49..bfa04d4cd6 100644 --- a/application/src/main/java/org/thingsboard/server/service/resource/TbResourceService.java +++ b/application/src/main/java/org/thingsboard/server/service/resource/TbResourceService.java @@ -24,11 +24,10 @@ import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.lwm2m.LwM2mObject; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; -import org.thingsboard.server.service.entitiy.SimpleTbEntityService; import java.util.List; -public interface TbResourceService extends SimpleTbEntityService { +public interface TbResourceService { TbResource saveResource(TbResource resource) throws ThingsboardException;