fixed save method to not put into cache
This commit is contained in:
parent
c021c64eab
commit
7d1e12d4b1
@ -126,8 +126,8 @@ public class AssetProfileServiceImpl extends AbstractCachedEntityService<AssetPr
|
|||||||
AssetProfile oldAssetProfile = null;
|
AssetProfile oldAssetProfile = null;
|
||||||
if (doValidate) {
|
if (doValidate) {
|
||||||
oldAssetProfile = assetProfileValidator.validate(assetProfile, AssetProfile::getTenantId);
|
oldAssetProfile = assetProfileValidator.validate(assetProfile, AssetProfile::getTenantId);
|
||||||
} else if (assetProfile.getId() != null) {
|
} else if (assetProfile.getId() != null && assetProfile.getId().getId() != null) {
|
||||||
oldAssetProfile = findAssetProfileById(assetProfile.getTenantId(), assetProfile.getId());
|
oldAssetProfile = assetProfileDao.findById(assetProfile.getTenantId(), assetProfile.getId().getId());
|
||||||
}
|
}
|
||||||
AssetProfile savedAssetProfile;
|
AssetProfile savedAssetProfile;
|
||||||
try {
|
try {
|
||||||
@ -208,13 +208,14 @@ public class AssetProfileServiceImpl extends AbstractCachedEntityService<AssetPr
|
|||||||
@Override
|
@Override
|
||||||
public AssetProfile findOrCreateAssetProfile(TenantId tenantId, String name) {
|
public AssetProfile findOrCreateAssetProfile(TenantId tenantId, String name) {
|
||||||
log.trace("Executing findOrCreateAssetProfile");
|
log.trace("Executing findOrCreateAssetProfile");
|
||||||
AssetProfile assetProfile = findAssetProfileByName(tenantId, name);
|
Validator.validateString(name, INCORRECT_ASSET_PROFILE_NAME + name);
|
||||||
|
AssetProfile assetProfile = assetProfileDao.findByName(tenantId, name);
|
||||||
if (assetProfile == null) {
|
if (assetProfile == null) {
|
||||||
try {
|
try {
|
||||||
assetProfile = this.doCreateDefaultAssetProfile(tenantId, name, name.equals("default"));
|
assetProfile = this.doCreateDefaultAssetProfile(tenantId, name, name.equals("default"));
|
||||||
} catch (DataValidationException e) {
|
} catch (DataValidationException e) {
|
||||||
if (ASSET_PROFILE_WITH_SUCH_NAME_ALREADY_EXISTS.equals(e.getMessage())) {
|
if (ASSET_PROFILE_WITH_SUCH_NAME_ALREADY_EXISTS.equals(e.getMessage())) {
|
||||||
assetProfile = findAssetProfileByName(tenantId, name);
|
assetProfile = assetProfileDao.findByName(tenantId, name);
|
||||||
} else {
|
} else {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -163,8 +163,8 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device
|
|||||||
DeviceProfile oldDeviceProfile = null;
|
DeviceProfile oldDeviceProfile = null;
|
||||||
if (doValidate) {
|
if (doValidate) {
|
||||||
oldDeviceProfile = deviceProfileValidator.validate(deviceProfile, DeviceProfile::getTenantId);
|
oldDeviceProfile = deviceProfileValidator.validate(deviceProfile, DeviceProfile::getTenantId);
|
||||||
} else if (deviceProfile.getId() != null) {
|
} else if (deviceProfile.getId() != null && deviceProfile.getId().getId() != null) {
|
||||||
oldDeviceProfile = findDeviceProfileById(deviceProfile.getTenantId(), deviceProfile.getId());
|
oldDeviceProfile = deviceProfileDao.findById(deviceProfile.getTenantId(), deviceProfile.getId().getId());
|
||||||
}
|
}
|
||||||
DeviceProfile savedDeviceProfile;
|
DeviceProfile savedDeviceProfile;
|
||||||
try {
|
try {
|
||||||
@ -252,13 +252,13 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device
|
|||||||
@Override
|
@Override
|
||||||
public DeviceProfile findOrCreateDeviceProfile(TenantId tenantId, String name) {
|
public DeviceProfile findOrCreateDeviceProfile(TenantId tenantId, String name) {
|
||||||
log.trace("Executing findOrCreateDefaultDeviceProfile");
|
log.trace("Executing findOrCreateDefaultDeviceProfile");
|
||||||
DeviceProfile deviceProfile = findDeviceProfileByName(tenantId, name);
|
DeviceProfile deviceProfile = deviceProfileDao.findByName(tenantId, name);
|
||||||
if (deviceProfile == null) {
|
if (deviceProfile == null) {
|
||||||
try {
|
try {
|
||||||
deviceProfile = this.doCreateDefaultDeviceProfile(tenantId, name, name.equals("default"));
|
deviceProfile = this.doCreateDefaultDeviceProfile(tenantId, name, name.equals("default"));
|
||||||
} catch (DataValidationException e) {
|
} catch (DataValidationException e) {
|
||||||
if (DEVICE_PROFILE_WITH_SUCH_NAME_ALREADY_EXISTS.equals(e.getMessage())) {
|
if (DEVICE_PROFILE_WITH_SUCH_NAME_ALREADY_EXISTS.equals(e.getMessage())) {
|
||||||
deviceProfile = findDeviceProfileByName(tenantId, name);
|
deviceProfile = deviceProfileDao.findByName(tenantId, name);
|
||||||
} else {
|
} else {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -109,7 +109,8 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private DeviceProfileService deviceProfileService;
|
private DeviceProfileService deviceProfileService;
|
||||||
|
@Autowired
|
||||||
|
private DeviceProfileDao deviceProfileDao;
|
||||||
@Autowired
|
@Autowired
|
||||||
private EventService eventService;
|
private EventService eventService;
|
||||||
|
|
||||||
@ -223,7 +224,8 @@ public class DeviceServiceImpl extends AbstractCachedEntityService<DeviceCacheKe
|
|||||||
}
|
}
|
||||||
device.setDeviceProfileId(new DeviceProfileId(deviceProfile.getId().getId()));
|
device.setDeviceProfileId(new DeviceProfileId(deviceProfile.getId().getId()));
|
||||||
} else {
|
} else {
|
||||||
deviceProfile = this.deviceProfileService.findDeviceProfileById(device.getTenantId(), device.getDeviceProfileId());
|
validateId(device.getDeviceProfileId(), INCORRECT_DEVICE_PROFILE_ID + device.getDeviceProfileId());
|
||||||
|
deviceProfile = this.deviceProfileDao.findById(device.getTenantId(), device.getDeviceProfileId().getId());
|
||||||
if (deviceProfile == null) {
|
if (deviceProfile == null) {
|
||||||
throw new DataValidationException("Device is referencing non existing device profile!");
|
throw new DataValidationException("Device is referencing non existing device profile!");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -116,8 +116,8 @@ public class EntityViewServiceImpl extends AbstractCachedEntityService<EntityVie
|
|||||||
EntityView old = null;
|
EntityView old = null;
|
||||||
if (doValidate) {
|
if (doValidate) {
|
||||||
old = entityViewValidator.validate(entityView, EntityView::getTenantId);
|
old = entityViewValidator.validate(entityView, EntityView::getTenantId);
|
||||||
} else if (entityView.getId() != null) {
|
} else if (entityView.getId() != null && entityView.getId().getId() != null) {
|
||||||
old = findEntityViewById(entityView.getTenantId(), entityView.getId());
|
old = entityViewDao.findById(entityView.getTenantId(), entityView.getId().getId());
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
EntityView saved = entityViewDao.save(entityView.getTenantId(), entityView);
|
EntityView saved = entityViewDao.save(entityView.getTenantId(), entityView);
|
||||||
|
|||||||
@ -20,16 +20,25 @@ import org.junit.Assert;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
import org.springframework.transaction.TransactionStatus;
|
||||||
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||||
|
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
|
||||||
|
import org.springframework.transaction.support.TransactionTemplate;
|
||||||
import org.thingsboard.server.common.data.Customer;
|
import org.thingsboard.server.common.data.Customer;
|
||||||
import org.thingsboard.server.common.data.EntitySubtype;
|
import org.thingsboard.server.common.data.EntitySubtype;
|
||||||
import org.thingsboard.server.common.data.StringUtils;
|
import org.thingsboard.server.common.data.StringUtils;
|
||||||
import org.thingsboard.server.common.data.Tenant;
|
import org.thingsboard.server.common.data.Tenant;
|
||||||
import org.thingsboard.server.common.data.asset.Asset;
|
import org.thingsboard.server.common.data.asset.Asset;
|
||||||
import org.thingsboard.server.common.data.asset.AssetInfo;
|
import org.thingsboard.server.common.data.asset.AssetInfo;
|
||||||
|
import org.thingsboard.server.common.data.asset.AssetProfile;
|
||||||
|
import org.thingsboard.server.common.data.id.AssetProfileId;
|
||||||
import org.thingsboard.server.common.data.id.CustomerId;
|
import org.thingsboard.server.common.data.id.CustomerId;
|
||||||
import org.thingsboard.server.common.data.id.TenantId;
|
import org.thingsboard.server.common.data.id.TenantId;
|
||||||
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.dao.asset.AssetDao;
|
||||||
|
import org.thingsboard.server.dao.asset.AssetProfileService;
|
||||||
import org.thingsboard.server.dao.asset.AssetService;
|
import org.thingsboard.server.dao.asset.AssetService;
|
||||||
import org.thingsboard.server.dao.customer.CustomerService;
|
import org.thingsboard.server.dao.customer.CustomerService;
|
||||||
import org.thingsboard.server.dao.exception.DataValidationException;
|
import org.thingsboard.server.dao.exception.DataValidationException;
|
||||||
@ -37,6 +46,7 @@ import org.thingsboard.server.dao.exception.DataValidationException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
|
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
|
||||||
|
|
||||||
@ -46,7 +56,13 @@ public class AssetServiceTest extends AbstractServiceTest {
|
|||||||
@Autowired
|
@Autowired
|
||||||
AssetService assetService;
|
AssetService assetService;
|
||||||
@Autowired
|
@Autowired
|
||||||
|
AssetDao assetDao;
|
||||||
|
@Autowired
|
||||||
CustomerService customerService;
|
CustomerService customerService;
|
||||||
|
@Autowired
|
||||||
|
private AssetProfileService assetProfileService;
|
||||||
|
@Autowired
|
||||||
|
private PlatformTransactionManager platformTransactionManager;
|
||||||
|
|
||||||
private IdComparator<Asset> idComparator = new IdComparator<>();
|
private IdComparator<Asset> idComparator = new IdComparator<>();
|
||||||
|
|
||||||
@ -75,6 +91,29 @@ public class AssetServiceTest extends AbstractServiceTest {
|
|||||||
assetService.deleteAsset(tenantId, savedAsset.getId());
|
assetService.deleteAsset(tenantId, savedAsset.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testShouldNotPutInCacheRolledbackAssetProfile() {
|
||||||
|
AssetProfile assetProfile = new AssetProfile();
|
||||||
|
assetProfile.setName(StringUtils.randomAlphabetic(10));
|
||||||
|
assetProfile.setTenantId(tenantId);
|
||||||
|
|
||||||
|
Asset asset = new Asset();
|
||||||
|
asset.setName("My asset" + StringUtils.randomAlphabetic(15));
|
||||||
|
asset.setType(assetProfile.getName());
|
||||||
|
asset.setTenantId(tenantId);
|
||||||
|
|
||||||
|
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||||
|
TransactionStatus status = platformTransactionManager.getTransaction(def);
|
||||||
|
try {
|
||||||
|
assetProfileService.saveAssetProfile(assetProfile);
|
||||||
|
assetService.saveAsset(asset);
|
||||||
|
} finally {
|
||||||
|
platformTransactionManager.rollback(status);
|
||||||
|
}
|
||||||
|
AssetProfile assetProfileByName = assetProfileService.findAssetProfileByName(tenantId, assetProfile.getName());
|
||||||
|
Assert.assertNull(assetProfileByName);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSaveAssetWithEmptyName() {
|
public void testSaveAssetWithEmptyName() {
|
||||||
Asset asset = new Asset();
|
Asset asset = new Asset();
|
||||||
|
|||||||
@ -22,6 +22,9 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
import org.springframework.transaction.TransactionStatus;
|
||||||
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||||
import org.thingsboard.server.common.data.Customer;
|
import org.thingsboard.server.common.data.Customer;
|
||||||
import org.thingsboard.server.common.data.Device;
|
import org.thingsboard.server.common.data.Device;
|
||||||
import org.thingsboard.server.common.data.DeviceInfo;
|
import org.thingsboard.server.common.data.DeviceInfo;
|
||||||
@ -32,6 +35,8 @@ import org.thingsboard.server.common.data.OtaPackage;
|
|||||||
import org.thingsboard.server.common.data.StringUtils;
|
import org.thingsboard.server.common.data.StringUtils;
|
||||||
import org.thingsboard.server.common.data.Tenant;
|
import org.thingsboard.server.common.data.Tenant;
|
||||||
import org.thingsboard.server.common.data.TenantProfile;
|
import org.thingsboard.server.common.data.TenantProfile;
|
||||||
|
import org.thingsboard.server.common.data.asset.Asset;
|
||||||
|
import org.thingsboard.server.common.data.asset.AssetProfile;
|
||||||
import org.thingsboard.server.common.data.id.CustomerId;
|
import org.thingsboard.server.common.data.id.CustomerId;
|
||||||
import org.thingsboard.server.common.data.id.TenantId;
|
import org.thingsboard.server.common.data.id.TenantId;
|
||||||
import org.thingsboard.server.common.data.ota.ChecksumAlgorithm;
|
import org.thingsboard.server.common.data.ota.ChecksumAlgorithm;
|
||||||
@ -72,6 +77,8 @@ public class DeviceServiceTest extends AbstractServiceTest {
|
|||||||
OtaPackageService otaPackageService;
|
OtaPackageService otaPackageService;
|
||||||
@Autowired
|
@Autowired
|
||||||
TenantProfileService tenantProfileService;
|
TenantProfileService tenantProfileService;
|
||||||
|
@Autowired
|
||||||
|
private PlatformTransactionManager platformTransactionManager;
|
||||||
|
|
||||||
private IdComparator<Device> idComparator = new IdComparator<>();
|
private IdComparator<Device> idComparator = new IdComparator<>();
|
||||||
private TenantId anotherTenantId;
|
private TenantId anotherTenantId;
|
||||||
@ -305,6 +312,28 @@ public class DeviceServiceTest extends AbstractServiceTest {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testShouldNotPutInCacheRolledbackDeviceProfile() {
|
||||||
|
DeviceProfile deviceProfile = createDeviceProfile(tenantId, "New device Profile" + StringUtils.randomAlphabetic(5));
|
||||||
|
|
||||||
|
|
||||||
|
Device device = new Device();
|
||||||
|
device.setType(deviceProfile.getName());
|
||||||
|
device.setTenantId(tenantId);
|
||||||
|
device.setName("My device"+ StringUtils.randomAlphabetic(5));
|
||||||
|
|
||||||
|
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
|
||||||
|
TransactionStatus status = platformTransactionManager.getTransaction(def);
|
||||||
|
try {
|
||||||
|
deviceProfileService.saveDeviceProfile(deviceProfile);
|
||||||
|
deviceService.saveDevice(device);
|
||||||
|
} finally {
|
||||||
|
platformTransactionManager.rollback(status);
|
||||||
|
}
|
||||||
|
DeviceProfile deviceProfileByName = deviceProfileService.findDeviceProfileByName(tenantId, deviceProfile.getName());
|
||||||
|
Assert.assertNull(deviceProfileByName);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAssignDeviceToNonExistentCustomer() {
|
public void testAssignDeviceToNonExistentCustomer() {
|
||||||
Device device = new Device();
|
Device device = new Device();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user