Search in sources :

Example 86 with TenantId

use of org.thingsboard.server.common.data.id.TenantId in project thingsboard by thingsboard.

the class BaseDeviceServiceTest method testFindDevicesByTenantId.

@Test
public void testFindDevicesByTenantId() {
    Tenant tenant = new Tenant();
    tenant.setTitle("Test tenant");
    tenant = tenantService.saveTenant(tenant);
    TenantId tenantId = tenant.getId();
    List<Device> devices = new ArrayList<>();
    for (int i = 0; i < 178; i++) {
        Device device = new Device();
        device.setTenantId(tenantId);
        device.setName("Device" + i);
        device.setType("default");
        devices.add(deviceService.saveDevice(device));
    }
    List<Device> loadedDevices = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(23);
    TextPageData<Device> pageData = null;
    do {
        pageData = deviceService.findDevicesByTenantId(tenantId, pageLink);
        loadedDevices.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devices, idComparator);
    Collections.sort(loadedDevices, idComparator);
    Assert.assertEquals(devices, loadedDevices);
    deviceService.deleteDevicesByTenantId(tenantId);
    pageLink = new TextPageLink(33);
    pageData = deviceService.findDevicesByTenantId(tenantId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertTrue(pageData.getData().isEmpty());
    tenantService.deleteTenant(tenantId);
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 87 with TenantId

use of org.thingsboard.server.common.data.id.TenantId in project thingsboard by thingsboard.

the class BaseUserServiceTest method testFindCustomerUsersByEmail.

@Test
public void testFindCustomerUsersByEmail() {
    Tenant tenant = new Tenant();
    tenant.setTitle("Test tenant");
    tenant = tenantService.saveTenant(tenant);
    TenantId tenantId = tenant.getId();
    Customer customer = new Customer();
    customer.setTitle("Test customer");
    customer.setTenantId(tenantId);
    customer = customerService.saveCustomer(customer);
    CustomerId customerId = customer.getId();
    String email1 = "testEmail1";
    List<User> customerUsersEmail1 = new ArrayList<>();
    for (int i = 0; i < 124; i++) {
        User user = new User();
        user.setAuthority(Authority.CUSTOMER_USER);
        user.setTenantId(tenantId);
        user.setCustomerId(customerId);
        String suffix = RandomStringUtils.randomAlphanumeric((int) (5 + Math.random() * 10));
        String email = email1 + suffix + "@thingsboard.org";
        email = i % 2 == 0 ? email.toLowerCase() : email.toUpperCase();
        user.setEmail(email);
        customerUsersEmail1.add(userService.saveUser(user));
    }
    String email2 = "testEmail2";
    List<User> customerUsersEmail2 = new ArrayList<>();
    for (int i = 0; i < 132; i++) {
        User user = new User();
        user.setAuthority(Authority.CUSTOMER_USER);
        user.setTenantId(tenantId);
        user.setCustomerId(customerId);
        String suffix = RandomStringUtils.randomAlphanumeric((int) (5 + Math.random() * 10));
        String email = email2 + suffix + "@thingsboard.org";
        email = i % 2 == 0 ? email.toLowerCase() : email.toUpperCase();
        user.setEmail(email);
        customerUsersEmail2.add(userService.saveUser(user));
    }
    List<User> loadedCustomerUsersEmail1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(33, email1);
    TextPageData<User> pageData = null;
    do {
        pageData = userService.findCustomerUsers(tenantId, customerId, pageLink);
        loadedCustomerUsersEmail1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(customerUsersEmail1, idComparator);
    Collections.sort(loadedCustomerUsersEmail1, idComparator);
    Assert.assertEquals(customerUsersEmail1, loadedCustomerUsersEmail1);
    List<User> loadedCustomerUsersEmail2 = new ArrayList<>();
    pageLink = new TextPageLink(16, email2);
    do {
        pageData = userService.findCustomerUsers(tenantId, customerId, pageLink);
        loadedCustomerUsersEmail2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(customerUsersEmail2, idComparator);
    Collections.sort(loadedCustomerUsersEmail2, idComparator);
    Assert.assertEquals(customerUsersEmail2, loadedCustomerUsersEmail2);
    for (User user : loadedCustomerUsersEmail1) {
        userService.deleteUser(user.getId());
    }
    pageLink = new TextPageLink(4, email1);
    pageData = userService.findCustomerUsers(tenantId, customerId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (User user : loadedCustomerUsersEmail2) {
        userService.deleteUser(user.getId());
    }
    pageLink = new TextPageLink(4, email2);
    pageData = userService.findCustomerUsers(tenantId, customerId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    tenantService.deleteTenant(tenantId);
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) User(org.thingsboard.server.common.data.User) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Test(org.junit.Test)

Example 88 with TenantId

use of org.thingsboard.server.common.data.id.TenantId in project thingsboard by thingsboard.

the class BaseUserServiceTest method testFindTenantAdmins.

@Test
public void testFindTenantAdmins() {
    User tenantAdminUser = userService.findUserByEmail("tenant@thingsboard.org");
    TextPageData<User> pageData = userService.findTenantAdmins(tenantAdminUser.getTenantId(), new TextPageLink(10));
    Assert.assertFalse(pageData.hasNext());
    List<User> users = pageData.getData();
    Assert.assertEquals(1, users.size());
    Assert.assertEquals(tenantAdminUser, users.get(0));
    Tenant tenant = new Tenant();
    tenant.setTitle("Test tenant");
    tenant = tenantService.saveTenant(tenant);
    TenantId tenantId = tenant.getId();
    List<User> tenantAdmins = new ArrayList<>();
    for (int i = 0; i < 124; i++) {
        User user = new User();
        user.setAuthority(Authority.TENANT_ADMIN);
        user.setTenantId(tenantId);
        user.setEmail("testTenant" + i + "@thingsboard.org");
        tenantAdmins.add(userService.saveUser(user));
    }
    List<User> loadedTenantAdmins = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(33);
    do {
        pageData = userService.findTenantAdmins(tenantId, pageLink);
        loadedTenantAdmins.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(tenantAdmins, idComparator);
    Collections.sort(loadedTenantAdmins, idComparator);
    Assert.assertEquals(tenantAdmins, loadedTenantAdmins);
    tenantService.deleteTenant(tenantId);
    pageLink = new TextPageLink(33);
    pageData = userService.findTenantAdmins(tenantId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertTrue(pageData.getData().isEmpty());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) User(org.thingsboard.server.common.data.User) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Tenant(org.thingsboard.server.common.data.Tenant) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 89 with TenantId

use of org.thingsboard.server.common.data.id.TenantId in project thingsboard by thingsboard.

the class BaseUserServiceTest method testFindTenantAdminsByEmail.

@Test
public void testFindTenantAdminsByEmail() {
    Tenant tenant = new Tenant();
    tenant.setTitle("Test tenant");
    tenant = tenantService.saveTenant(tenant);
    TenantId tenantId = tenant.getId();
    String email1 = "testEmail1";
    List<User> tenantAdminsEmail1 = new ArrayList<>();
    for (int i = 0; i < 94; i++) {
        User user = new User();
        user.setAuthority(Authority.TENANT_ADMIN);
        user.setTenantId(tenantId);
        String suffix = RandomStringUtils.randomAlphanumeric((int) (5 + Math.random() * 10));
        String email = email1 + suffix + "@thingsboard.org";
        email = i % 2 == 0 ? email.toLowerCase() : email.toUpperCase();
        user.setEmail(email);
        tenantAdminsEmail1.add(userService.saveUser(user));
    }
    String email2 = "testEmail2";
    List<User> tenantAdminsEmail2 = new ArrayList<>();
    for (int i = 0; i < 132; i++) {
        User user = new User();
        user.setAuthority(Authority.TENANT_ADMIN);
        user.setTenantId(tenantId);
        String suffix = RandomStringUtils.randomAlphanumeric((int) (5 + Math.random() * 10));
        String email = email2 + suffix + "@thingsboard.org";
        email = i % 2 == 0 ? email.toLowerCase() : email.toUpperCase();
        user.setEmail(email);
        tenantAdminsEmail2.add(userService.saveUser(user));
    }
    List<User> loadedTenantAdminsEmail1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(33, email1);
    TextPageData<User> pageData = null;
    do {
        pageData = userService.findTenantAdmins(tenantId, pageLink);
        loadedTenantAdminsEmail1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(tenantAdminsEmail1, idComparator);
    Collections.sort(loadedTenantAdminsEmail1, idComparator);
    Assert.assertEquals(tenantAdminsEmail1, loadedTenantAdminsEmail1);
    List<User> loadedTenantAdminsEmail2 = new ArrayList<>();
    pageLink = new TextPageLink(16, email2);
    do {
        pageData = userService.findTenantAdmins(tenantId, pageLink);
        loadedTenantAdminsEmail2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(tenantAdminsEmail2, idComparator);
    Collections.sort(loadedTenantAdminsEmail2, idComparator);
    Assert.assertEquals(tenantAdminsEmail2, loadedTenantAdminsEmail2);
    for (User user : loadedTenantAdminsEmail1) {
        userService.deleteUser(user.getId());
    }
    pageLink = new TextPageLink(4, email1);
    pageData = userService.findTenantAdmins(tenantId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (User user : loadedTenantAdminsEmail2) {
        userService.deleteUser(user.getId());
    }
    pageLink = new TextPageLink(4, email2);
    pageData = userService.findTenantAdmins(tenantId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    tenantService.deleteTenant(tenantId);
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) User(org.thingsboard.server.common.data.User) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 90 with TenantId

use of org.thingsboard.server.common.data.id.TenantId in project thingsboard by thingsboard.

the class BaseWidgetTypeServiceTest method testSaveWidgetTypeWithInvalidTenant.

@Test(expected = DataValidationException.class)
public void testSaveWidgetTypeWithInvalidTenant() throws IOException {
    WidgetsBundle widgetsBundle = new WidgetsBundle();
    widgetsBundle.setTenantId(tenantId);
    widgetsBundle.setTitle("Widgets bundle");
    WidgetsBundle savedWidgetsBundle = widgetsBundleService.saveWidgetsBundle(widgetsBundle);
    WidgetType widgetType = new WidgetType();
    widgetType.setTenantId(new TenantId(UUIDs.timeBased()));
    widgetType.setBundleAlias(savedWidgetsBundle.getAlias());
    widgetType.setName("Widget Type");
    widgetType.setDescriptor(new ObjectMapper().readValue("{ \"someKey\": \"someValue\" }", JsonNode.class));
    try {
        widgetTypeService.saveWidgetType(widgetType);
    } finally {
        widgetsBundleService.deleteWidgetsBundle(savedWidgetsBundle.getId());
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) JsonNode(com.fasterxml.jackson.databind.JsonNode) WidgetType(org.thingsboard.server.common.data.widget.WidgetType) WidgetsBundle(org.thingsboard.server.common.data.widget.WidgetsBundle) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

TenantId (org.thingsboard.server.common.data.id.TenantId)119 Test (org.junit.Test)44 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)38 CustomerId (org.thingsboard.server.common.data.id.CustomerId)30 ArrayList (java.util.ArrayList)26 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)24 Tenant (org.thingsboard.server.common.data.Tenant)23 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)23 PluginMetaData (org.thingsboard.server.common.data.plugin.PluginMetaData)16 Customer (org.thingsboard.server.common.data.Customer)14 User (org.thingsboard.server.common.data.User)14 WidgetsBundle (org.thingsboard.server.common.data.widget.WidgetsBundle)13 DeviceId (org.thingsboard.server.common.data.id.DeviceId)10 RuleMetaData (org.thingsboard.server.common.data.rule.RuleMetaData)10 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)10 AbstractServiceTest (org.thingsboard.server.dao.service.AbstractServiceTest)10 UserId (org.thingsboard.server.common.data.id.UserId)8 TimePageLink (org.thingsboard.server.common.data.page.TimePageLink)8 IOException (java.io.IOException)7 PluginId (org.thingsboard.server.common.data.id.PluginId)7