fixes
This commit is contained in:
parent
ac007bea24
commit
a167b39b44
@ -79,8 +79,14 @@ import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
|
||||
|
||||
@ActiveProfiles("test")
|
||||
@ -221,6 +227,7 @@ public abstract class AbstractControllerTest {
|
||||
}
|
||||
|
||||
private Tenant savedDifferentTenant;
|
||||
|
||||
protected void loginDifferentTenant() throws Exception {
|
||||
loginSysAdmin();
|
||||
Tenant tenant = new Tenant();
|
||||
@ -316,6 +323,10 @@ public abstract class AbstractControllerTest {
|
||||
return readResponse(doGet(urlTemplate, urlVariables).andExpect(status().isOk()), responseClass);
|
||||
}
|
||||
|
||||
protected <T> T doGet(String urlTemplate, Class<T> responseClass, ResultMatcher resultMatcher, Object... urlVariables) throws Exception {
|
||||
return readResponse(doGet(urlTemplate, urlVariables).andExpect(resultMatcher), responseClass);
|
||||
}
|
||||
|
||||
protected <T> T doGetAsync(String urlTemplate, Class<T> responseClass, Object... urlVariables) throws Exception {
|
||||
return readResponse(doGetAsync(urlTemplate, urlVariables).andExpect(status().isOk()), responseClass);
|
||||
}
|
||||
|
||||
@ -15,32 +15,37 @@
|
||||
*/
|
||||
package org.thingsboard.server.controller;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import com.datastax.driver.core.utils.UUIDs;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.thingsboard.server.common.data.*;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.thingsboard.server.common.data.Customer;
|
||||
import org.thingsboard.server.common.data.Device;
|
||||
import org.thingsboard.server.common.data.EntitySubtype;
|
||||
import org.thingsboard.server.common.data.Tenant;
|
||||
import org.thingsboard.server.common.data.User;
|
||||
import org.thingsboard.server.common.data.id.CustomerId;
|
||||
import org.thingsboard.server.common.data.id.DeviceCredentialsId;
|
||||
import org.thingsboard.server.common.data.id.DeviceId;
|
||||
import org.thingsboard.server.common.data.page.TextPageData;
|
||||
import org.thingsboard.server.common.data.page.TextPageLink;
|
||||
import org.thingsboard.server.common.data.relation.EntityRelation;
|
||||
import org.thingsboard.server.common.data.relation.RelationTypeGroup;
|
||||
import org.thingsboard.server.common.data.security.Authority;
|
||||
import org.thingsboard.server.common.data.security.DeviceCredentials;
|
||||
import org.thingsboard.server.common.data.security.DeviceCredentialsType;
|
||||
import org.thingsboard.server.dao.model.ModelConstants;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
|
||||
|
||||
public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
|
||||
@ -115,7 +120,7 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
device.setType("default");
|
||||
Device savedDevice = doPost("/api/device", device, Device.class);
|
||||
loginDifferentTenant();
|
||||
doPost("/api/device", savedDevice, Device.class, status().isForbidden());
|
||||
doPost("/api/device", savedDevice, Device.class, status().isNotFound());
|
||||
deleteDifferentTenant();
|
||||
}
|
||||
|
||||
@ -152,7 +157,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
devices.add(doPost("/api/device", device, Device.class));
|
||||
}
|
||||
List<EntitySubtype> deviceTypes = doGetTyped("/api/device/types",
|
||||
new TypeReference<List<EntitySubtype>>(){});
|
||||
new TypeReference<List<EntitySubtype>>() {
|
||||
});
|
||||
|
||||
Assert.assertNotNull(deviceTypes);
|
||||
Assert.assertEquals(3, deviceTypes.size());
|
||||
@ -381,7 +387,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
TextPageData<Device> pageData = null;
|
||||
do {
|
||||
pageData = doGetTypedWithPageLink("/api/tenant/devices?",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink);
|
||||
loadedDevices.addAll(pageData.getData());
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageData.getNextPageLink();
|
||||
@ -424,7 +431,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
TextPageData<Device> pageData = null;
|
||||
do {
|
||||
pageData = doGetTypedWithPageLink("/api/tenant/devices?",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink);
|
||||
loadedDevicesTitle1.addAll(pageData.getData());
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageData.getNextPageLink();
|
||||
@ -440,7 +448,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
pageLink = new TextPageLink(4, title2);
|
||||
do {
|
||||
pageData = doGetTypedWithPageLink("/api/tenant/devices?",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink);
|
||||
loadedDevicesTitle2.addAll(pageData.getData());
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageData.getNextPageLink();
|
||||
@ -459,7 +468,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
|
||||
pageLink = new TextPageLink(4, title1);
|
||||
pageData = doGetTypedWithPageLink("/api/tenant/devices?",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink);
|
||||
Assert.assertFalse(pageData.hasNext());
|
||||
Assert.assertEquals(0, pageData.getData().size());
|
||||
|
||||
@ -470,7 +480,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
|
||||
pageLink = new TextPageLink(4, title2);
|
||||
pageData = doGetTypedWithPageLink("/api/tenant/devices?",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink);
|
||||
Assert.assertFalse(pageData.hasNext());
|
||||
Assert.assertEquals(0, pageData.getData().size());
|
||||
}
|
||||
@ -507,7 +518,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
TextPageData<Device> pageData = null;
|
||||
do {
|
||||
pageData = doGetTypedWithPageLink("/api/tenant/devices?type={type}&",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink, type1);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink, type1);
|
||||
loadedDevicesType1.addAll(pageData.getData());
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageData.getNextPageLink();
|
||||
@ -523,7 +535,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
pageLink = new TextPageLink(4);
|
||||
do {
|
||||
pageData = doGetTypedWithPageLink("/api/tenant/devices?type={type}&",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink, type2);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink, type2);
|
||||
loadedDevicesType2.addAll(pageData.getData());
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageData.getNextPageLink();
|
||||
@ -542,7 +555,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
|
||||
pageLink = new TextPageLink(4);
|
||||
pageData = doGetTypedWithPageLink("/api/tenant/devices?type={type}&",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink, type1);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink, type1);
|
||||
Assert.assertFalse(pageData.hasNext());
|
||||
Assert.assertEquals(0, pageData.getData().size());
|
||||
|
||||
@ -553,7 +567,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
|
||||
pageLink = new TextPageLink(4);
|
||||
pageData = doGetTypedWithPageLink("/api/tenant/devices?type={type}&",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink, type2);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink, type2);
|
||||
Assert.assertFalse(pageData.hasNext());
|
||||
Assert.assertEquals(0, pageData.getData().size());
|
||||
}
|
||||
@ -580,7 +595,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
TextPageData<Device> pageData = null;
|
||||
do {
|
||||
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/devices?",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink);
|
||||
loadedDevices.addAll(pageData.getData());
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageData.getNextPageLink();
|
||||
@ -632,7 +648,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
TextPageData<Device> pageData = null;
|
||||
do {
|
||||
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/devices?",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink);
|
||||
loadedDevicesTitle1.addAll(pageData.getData());
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageData.getNextPageLink();
|
||||
@ -648,7 +665,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
pageLink = new TextPageLink(4, title2);
|
||||
do {
|
||||
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/devices?",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink);
|
||||
loadedDevicesTitle2.addAll(pageData.getData());
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageData.getNextPageLink();
|
||||
@ -667,7 +685,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
|
||||
pageLink = new TextPageLink(4, title1);
|
||||
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/devices?",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink);
|
||||
Assert.assertFalse(pageData.hasNext());
|
||||
Assert.assertEquals(0, pageData.getData().size());
|
||||
|
||||
@ -678,7 +697,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
|
||||
pageLink = new TextPageLink(4, title2);
|
||||
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/devices?",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink);
|
||||
Assert.assertFalse(pageData.hasNext());
|
||||
Assert.assertEquals(0, pageData.getData().size());
|
||||
}
|
||||
@ -724,7 +744,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
TextPageData<Device> pageData = null;
|
||||
do {
|
||||
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/devices?type={type}&",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink, type1);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink, type1);
|
||||
loadedDevicesType1.addAll(pageData.getData());
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageData.getNextPageLink();
|
||||
@ -740,7 +761,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
pageLink = new TextPageLink(4);
|
||||
do {
|
||||
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/devices?type={type}&",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink, type2);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink, type2);
|
||||
loadedDevicesType2.addAll(pageData.getData());
|
||||
if (pageData.hasNext()) {
|
||||
pageLink = pageData.getNextPageLink();
|
||||
@ -759,7 +781,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
|
||||
pageLink = new TextPageLink(4);
|
||||
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/devices?type={type}&",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink, type1);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink, type1);
|
||||
Assert.assertFalse(pageData.hasNext());
|
||||
Assert.assertEquals(0, pageData.getData().size());
|
||||
|
||||
@ -770,9 +793,56 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
|
||||
|
||||
pageLink = new TextPageLink(4);
|
||||
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/devices?type={type}&",
|
||||
new TypeReference<TextPageData<Device>>(){}, pageLink, type2);
|
||||
new TypeReference<TextPageData<Device>>() {
|
||||
}, pageLink, type2);
|
||||
Assert.assertFalse(pageData.hasNext());
|
||||
Assert.assertEquals(0, pageData.getData().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSwapDeviceFromOneTenantToAnother() throws Exception {
|
||||
Device device = new Device();
|
||||
device.setName("My device");
|
||||
device.setType("default");
|
||||
Device savedDevice = doPost("/api/device", device, Device.class);
|
||||
|
||||
Device anotherDevice = new Device();
|
||||
anotherDevice.setName("My device1");
|
||||
anotherDevice.setType("default");
|
||||
Device savedAnotherDevice = doPost("/api/device", anotherDevice, Device.class);
|
||||
|
||||
EntityRelation relation = new EntityRelation();
|
||||
relation.setFrom(savedDevice.getId());
|
||||
relation.setTo(savedAnotherDevice.getId());
|
||||
relation.setTypeGroup(RelationTypeGroup.COMMON);
|
||||
relation.setType("Contains");
|
||||
doPost("/api/relation", relation);
|
||||
|
||||
loginSysAdmin();
|
||||
Tenant tenant = new Tenant();
|
||||
tenant.setTitle("Different tenant");
|
||||
Tenant savedDifferentTenant = doPost("/api/tenant", tenant, Tenant.class);
|
||||
Assert.assertNotNull(savedDifferentTenant);
|
||||
|
||||
User user = new User();
|
||||
user.setAuthority(Authority.TENANT_ADMIN);
|
||||
user.setTenantId(savedDifferentTenant.getId());
|
||||
user.setEmail("tenant9@thingsboard.org");
|
||||
user.setFirstName("Sam");
|
||||
user.setLastName("Downs");
|
||||
|
||||
createUserAndLogin(user, "testPassword1");
|
||||
|
||||
login("tenant2@thingsboard.org", "testPassword1");
|
||||
Device swappedDevice = doPost("/api/tenant/" + savedDifferentTenant.getId().getId() + "/device/" + savedDevice.getId().getId(), Device.class);
|
||||
|
||||
doGet("/api/device/" + swappedDevice.getId().getId().toString(), Device.class, status().isNotFound());
|
||||
|
||||
login("tenant9@thingsboard.org", "testPassword1");
|
||||
|
||||
Device foundDevice1 = doGet("/api/device/" + swappedDevice.getId().getId().toString(), Device.class);
|
||||
Assert.assertNotNull(foundDevice1);
|
||||
|
||||
doGet("/api/relation?fromId=" + savedDevice.getId().getId() + "&fromType=DEVICE&relationType=Contains&toId=" + savedAnotherDevice.getId().getId() + "&toType=DEVICE", EntityRelation.class, status().isNotFound());
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,6 +33,7 @@ import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.common.data.page.TextPageLink;
|
||||
import org.thingsboard.server.dao.DaoUtil;
|
||||
import org.thingsboard.server.dao.model.EntitySubtypeEntity;
|
||||
import org.thingsboard.server.dao.model.ModelConstants;
|
||||
import org.thingsboard.server.dao.model.nosql.DeviceEntity;
|
||||
import org.thingsboard.server.dao.nosql.CassandraAbstractSearchTextDao;
|
||||
import org.thingsboard.server.dao.util.NoSqlDao;
|
||||
@ -191,12 +192,17 @@ public class CassandraDeviceDao extends CassandraAbstractSearchTextDao<DeviceEnt
|
||||
|
||||
@Override
|
||||
public Device findDeviceByTenantIdAndId(TenantId tenantId, UUID id) {
|
||||
return findById(tenantId, id);
|
||||
Select.Where query = select().from(getColumnFamilyName()).where(eq(ModelConstants.TENANT_ID_PROPERTY, tenantId.getId())).and(eq(ModelConstants.ID_PROPERTY, id));
|
||||
log.trace("Execute query {}", query);
|
||||
DeviceEntity entity = findOneByStatement(tenantId, query);
|
||||
return DaoUtil.getData(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListenableFuture<Device> findDeviceByTenantIdAndIdAsync(TenantId tenantId, UUID id) {
|
||||
return findByIdAsync(tenantId, id);
|
||||
Select.Where query = select().from(getColumnFamilyName()).where(eq(ModelConstants.TENANT_ID_PROPERTY, tenantId.getId())).and(eq(ModelConstants.ID_PROPERTY, id));
|
||||
log.trace("Execute query {}", query);
|
||||
return findOneByStatementAsync(tenantId, query);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -355,7 +355,10 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
|
||||
|
||||
relationService.removeRelations(device.getTenantId(), device.getId());
|
||||
|
||||
// TODO: 30/07/2020 implement for Cassandra
|
||||
if (sqlDatabaseUsed) {
|
||||
auditLogService.removeAuditLogs(device.getTenantId(), device.getId());
|
||||
}
|
||||
|
||||
device.setTenantId(tenantId);
|
||||
device.setCustomerId(null);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user