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 84 return deviceCredentialsDao.save(tenantId, deviceCredentials);
85 85 } catch (Exception t) {
86 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 89 throw new DataValidationException("Specified credentials are already registered!");
89 90 } else {
90 91 throw t;
... ... @@ -111,14 +112,23 @@ public class DeviceCredentialsServiceImpl extends AbstractEntityService implemen
111 112
112 113 @Override
113 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 123 @Override
117 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 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 134 @Override
... ...
... ... @@ -142,7 +142,8 @@ CREATE TABLE IF NOT EXISTS device_credentials (
142 142 credentials_type varchar(255),
143 143 credentials_value varchar,
144 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 149 CREATE TABLE IF NOT EXISTS event (
... ...
... ... @@ -142,7 +142,8 @@ CREATE TABLE IF NOT EXISTS device_credentials (
142 142 credentials_type varchar(255),
143 143 credentials_value varchar,
144 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 149 CREATE TABLE IF NOT EXISTS event (
... ...