Commit 30210c70794067539b9b6ea7c3df229d6c479336

Authored by BohdanSmetanyuk
Committed by GitHub
1 parent fc597f2c

Bug/several credentials for device (#3089)

* fix for device credentials

* fix in validateCreate
@@ -84,7 +84,8 @@ public class DeviceCredentialsServiceImpl extends AbstractEntityService implemen @@ -84,7 +84,8 @@ public class DeviceCredentialsServiceImpl extends AbstractEntityService implemen
84 return deviceCredentialsDao.save(tenantId, deviceCredentials); 84 return deviceCredentialsDao.save(tenantId, deviceCredentials);
85 } catch (Exception t) { 85 } catch (Exception t) {
86 ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); 86 ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
87 - if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_credentials_id_unq_key")) { 87 + if (e != null && e.getConstraintName() != null
  88 + && (e.getConstraintName().equalsIgnoreCase("device_credentials_id_unq_key") || e.getConstraintName().equalsIgnoreCase("device_credentials_device_id_unq_key"))) {
88 throw new DataValidationException("Specified credentials are already registered!"); 89 throw new DataValidationException("Specified credentials are already registered!");
89 } else { 90 } else {
90 throw t; 91 throw t;
@@ -111,14 +112,23 @@ public class DeviceCredentialsServiceImpl extends AbstractEntityService implemen @@ -111,14 +112,23 @@ public class DeviceCredentialsServiceImpl extends AbstractEntityService implemen
111 112
112 @Override 113 @Override
113 protected void validateCreate(TenantId tenantId, DeviceCredentials deviceCredentials) { 114 protected void validateCreate(TenantId tenantId, DeviceCredentials deviceCredentials) {
  115 + if (deviceCredentialsDao.findByDeviceId(tenantId, deviceCredentials.getDeviceId().getId()) != null) {
  116 + throw new DataValidationException("Credentials for this device are already specified!");
  117 + }
  118 + if (deviceCredentialsDao.findByCredentialsId(tenantId, deviceCredentials.getCredentialsId()) != null) {
  119 + throw new DataValidationException("Device credentials are already assigned to another device!");
  120 + }
114 } 121 }
115 122
116 @Override 123 @Override
117 protected void validateUpdate(TenantId tenantId, DeviceCredentials deviceCredentials) { 124 protected void validateUpdate(TenantId tenantId, DeviceCredentials deviceCredentials) {
118 - DeviceCredentials existingCredentials = deviceCredentialsDao.findById(tenantId, deviceCredentials.getUuidId());  
119 - if (existingCredentials == null) { 125 + if (deviceCredentialsDao.findById(tenantId, deviceCredentials.getUuidId()) == null) {
120 throw new DataValidationException("Unable to update non-existent device credentials!"); 126 throw new DataValidationException("Unable to update non-existent device credentials!");
121 } 127 }
  128 + DeviceCredentials existingCredentials = deviceCredentialsDao.findByCredentialsId(tenantId, deviceCredentials.getCredentialsId());
  129 + if (existingCredentials != null && !existingCredentials.getId().equals(deviceCredentials.getId())) {
  130 + throw new DataValidationException("Device credentials are already assigned to another device!");
  131 + }
122 } 132 }
123 133
124 @Override 134 @Override
@@ -142,7 +142,8 @@ CREATE TABLE IF NOT EXISTS device_credentials ( @@ -142,7 +142,8 @@ CREATE TABLE IF NOT EXISTS device_credentials (
142 credentials_type varchar(255), 142 credentials_type varchar(255),
143 credentials_value varchar, 143 credentials_value varchar,
144 device_id uuid, 144 device_id uuid,
145 - CONSTRAINT device_credentials_id_unq_key UNIQUE (credentials_id) 145 + CONSTRAINT device_credentials_id_unq_key UNIQUE (credentials_id),
  146 + CONSTRAINT device_credentials_device_id_unq_key UNIQUE (device_id)
146 ); 147 );
147 148
148 CREATE TABLE IF NOT EXISTS event ( 149 CREATE TABLE IF NOT EXISTS event (
@@ -142,7 +142,8 @@ CREATE TABLE IF NOT EXISTS device_credentials ( @@ -142,7 +142,8 @@ CREATE TABLE IF NOT EXISTS device_credentials (
142 credentials_type varchar(255), 142 credentials_type varchar(255),
143 credentials_value varchar, 143 credentials_value varchar,
144 device_id uuid, 144 device_id uuid,
145 - CONSTRAINT device_credentials_id_unq_key UNIQUE (credentials_id) 145 + CONSTRAINT device_credentials_id_unq_key UNIQUE (credentials_id),
  146 + CONSTRAINT device_credentials_device_id_unq_key UNIQUE (device_id)
146 ); 147 );
147 148
148 CREATE TABLE IF NOT EXISTS event ( 149 CREATE TABLE IF NOT EXISTS event (