277 changed files with 11664 additions and 4092 deletions
@ -0,0 +1,129 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.controller; |
||||
|
|
||||
|
import com.google.common.util.concurrent.ListenableFuture; |
||||
|
import org.apache.commons.lang3.StringUtils; |
||||
|
import org.springframework.http.HttpStatus; |
||||
|
import org.springframework.security.access.prepost.PreAuthorize; |
||||
|
import org.springframework.web.bind.annotation.*; |
||||
|
import org.thingsboard.server.common.data.Customer; |
||||
|
import org.thingsboard.server.common.data.Event; |
||||
|
import org.thingsboard.server.common.data.alarm.Alarm; |
||||
|
import org.thingsboard.server.common.data.alarm.AlarmId; |
||||
|
import org.thingsboard.server.common.data.alarm.AlarmQuery; |
||||
|
import org.thingsboard.server.common.data.alarm.AlarmStatus; |
||||
|
import org.thingsboard.server.common.data.asset.Asset; |
||||
|
import org.thingsboard.server.common.data.id.*; |
||||
|
import org.thingsboard.server.common.data.page.TextPageData; |
||||
|
import org.thingsboard.server.common.data.page.TextPageLink; |
||||
|
import org.thingsboard.server.common.data.page.TimePageData; |
||||
|
import org.thingsboard.server.common.data.page.TimePageLink; |
||||
|
import org.thingsboard.server.dao.asset.AssetSearchQuery; |
||||
|
import org.thingsboard.server.dao.exception.IncorrectParameterException; |
||||
|
import org.thingsboard.server.dao.model.ModelConstants; |
||||
|
import org.thingsboard.server.exception.ThingsboardErrorCode; |
||||
|
import org.thingsboard.server.exception.ThingsboardException; |
||||
|
import org.thingsboard.server.service.security.model.SecurityUser; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
@RestController |
||||
|
@RequestMapping("/api") |
||||
|
public class AlarmController extends BaseController { |
||||
|
|
||||
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") |
||||
|
@RequestMapping(value = "/alarm/{alarmId}", method = RequestMethod.GET) |
||||
|
@ResponseBody |
||||
|
public Alarm getAlarmById(@PathVariable("alarmId") String strAlarmId) throws ThingsboardException { |
||||
|
checkParameter("alarmId", strAlarmId); |
||||
|
try { |
||||
|
AlarmId alarmId = new AlarmId(toUUID(strAlarmId)); |
||||
|
return checkAlarmId(alarmId); |
||||
|
} catch (Exception e) { |
||||
|
throw handleException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") |
||||
|
@RequestMapping(value = "/alarm", method = RequestMethod.POST) |
||||
|
@ResponseBody |
||||
|
public Alarm saveAlarm(@RequestBody Alarm alarm) throws ThingsboardException { |
||||
|
try { |
||||
|
alarm.setTenantId(getCurrentUser().getTenantId()); |
||||
|
return checkNotNull(alarmService.createOrUpdateAlarm(alarm)); |
||||
|
} catch (Exception e) { |
||||
|
throw handleException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
||||
|
@RequestMapping(value = "/alarm/{alarmId}/ack", method = RequestMethod.POST) |
||||
|
@ResponseStatus(value = HttpStatus.OK) |
||||
|
public void ackAlarm(@PathVariable("alarmId") String strAlarmId) throws ThingsboardException { |
||||
|
checkParameter("alarmId", strAlarmId); |
||||
|
try { |
||||
|
AlarmId alarmId = new AlarmId(toUUID(strAlarmId)); |
||||
|
checkAlarmId(alarmId); |
||||
|
alarmService.ackAlarm(alarmId, System.currentTimeMillis()).get(); |
||||
|
} catch (Exception e) { |
||||
|
throw handleException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@PreAuthorize("hasAuthority('TENANT_ADMIN')") |
||||
|
@RequestMapping(value = "/alarm/{alarmId}/clear", method = RequestMethod.POST) |
||||
|
@ResponseStatus(value = HttpStatus.OK) |
||||
|
public void clearAlarm(@PathVariable("alarmId") String strAlarmId) throws ThingsboardException { |
||||
|
checkParameter("alarmId", strAlarmId); |
||||
|
try { |
||||
|
AlarmId alarmId = new AlarmId(toUUID(strAlarmId)); |
||||
|
checkAlarmId(alarmId); |
||||
|
alarmService.clearAlarm(alarmId, System.currentTimeMillis()).get(); |
||||
|
} catch (Exception e) { |
||||
|
throw handleException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") |
||||
|
@RequestMapping(value = "/alarm/{entityType}/{entityId}", method = RequestMethod.GET) |
||||
|
@ResponseBody |
||||
|
public TimePageData<Alarm> getAlarms( |
||||
|
@PathVariable("entityType") String strEntityType, |
||||
|
@PathVariable("entityId") String strEntityId, |
||||
|
@RequestParam(required = false) String status, |
||||
|
@RequestParam int limit, |
||||
|
@RequestParam(required = false) Long startTime, |
||||
|
@RequestParam(required = false) Long endTime, |
||||
|
@RequestParam(required = false, defaultValue = "false") boolean ascOrder, |
||||
|
@RequestParam(required = false) String offset |
||||
|
) throws ThingsboardException { |
||||
|
checkParameter("EntityId", strEntityId); |
||||
|
checkParameter("EntityType", strEntityType); |
||||
|
EntityId entityId = EntityIdFactory.getByTypeAndId(strEntityType, strEntityId); |
||||
|
AlarmStatus alarmStatus = StringUtils.isEmpty(status) ? null : AlarmStatus.valueOf(status); |
||||
|
checkEntityId(entityId); |
||||
|
try { |
||||
|
TimePageLink pageLink = createPageLink(limit, startTime, endTime, ascOrder, offset); |
||||
|
return checkNotNull(alarmService.findAlarms(new AlarmQuery(entityId, pageLink, alarmStatus)).get()); |
||||
|
} catch (Exception e) { |
||||
|
throw handleException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,659 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
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 org.apache.commons.lang3.RandomStringUtils; |
||||
|
import org.thingsboard.server.common.data.*; |
||||
|
import org.thingsboard.server.common.data.asset.Asset; |
||||
|
import org.thingsboard.server.common.data.asset.TenantAssetType; |
||||
|
import org.thingsboard.server.common.data.id.CustomerId; |
||||
|
import org.thingsboard.server.common.data.id.AssetId; |
||||
|
import org.thingsboard.server.common.data.page.TextPageData; |
||||
|
import org.thingsboard.server.common.data.page.TextPageLink; |
||||
|
import org.thingsboard.server.common.data.security.Authority; |
||||
|
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.datastax.driver.core.utils.UUIDs; |
||||
|
import com.fasterxml.jackson.core.type.TypeReference; |
||||
|
|
||||
|
public class AssetControllerTest extends AbstractControllerTest { |
||||
|
|
||||
|
private IdComparator<Asset> idComparator = new IdComparator<>(); |
||||
|
|
||||
|
private Tenant savedTenant; |
||||
|
private User tenantAdmin; |
||||
|
|
||||
|
@Before |
||||
|
public void beforeTest() throws Exception { |
||||
|
loginSysAdmin(); |
||||
|
|
||||
|
Tenant tenant = new Tenant(); |
||||
|
tenant.setTitle("My tenant"); |
||||
|
savedTenant = doPost("/api/tenant", tenant, Tenant.class); |
||||
|
Assert.assertNotNull(savedTenant); |
||||
|
|
||||
|
tenantAdmin = new User(); |
||||
|
tenantAdmin.setAuthority(Authority.TENANT_ADMIN); |
||||
|
tenantAdmin.setTenantId(savedTenant.getId()); |
||||
|
tenantAdmin.setEmail("tenant2@thingsboard.org"); |
||||
|
tenantAdmin.setFirstName("Joe"); |
||||
|
tenantAdmin.setLastName("Downs"); |
||||
|
|
||||
|
tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1"); |
||||
|
} |
||||
|
|
||||
|
@After |
||||
|
public void afterTest() throws Exception { |
||||
|
loginSysAdmin(); |
||||
|
|
||||
|
doDelete("/api/tenant/"+savedTenant.getId().getId().toString()) |
||||
|
.andExpect(status().isOk()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testSaveAsset() throws Exception { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("My asset"); |
||||
|
asset.setType("default"); |
||||
|
Asset savedAsset = doPost("/api/asset", asset, Asset.class); |
||||
|
|
||||
|
Assert.assertNotNull(savedAsset); |
||||
|
Assert.assertNotNull(savedAsset.getId()); |
||||
|
Assert.assertTrue(savedAsset.getCreatedTime() > 0); |
||||
|
Assert.assertEquals(savedTenant.getId(), savedAsset.getTenantId()); |
||||
|
Assert.assertNotNull(savedAsset.getCustomerId()); |
||||
|
Assert.assertEquals(NULL_UUID, savedAsset.getCustomerId().getId()); |
||||
|
Assert.assertEquals(asset.getName(), savedAsset.getName()); |
||||
|
|
||||
|
savedAsset.setName("My new asset"); |
||||
|
doPost("/api/asset", savedAsset, Asset.class); |
||||
|
|
||||
|
Asset foundAsset = doGet("/api/asset/" + savedAsset.getId().getId().toString(), Asset.class); |
||||
|
Assert.assertEquals(foundAsset.getName(), savedAsset.getName()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindAssetById() throws Exception { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("My asset"); |
||||
|
asset.setType("default"); |
||||
|
Asset savedAsset = doPost("/api/asset", asset, Asset.class); |
||||
|
Asset foundAsset = doGet("/api/asset/" + savedAsset.getId().getId().toString(), Asset.class); |
||||
|
Assert.assertNotNull(foundAsset); |
||||
|
Assert.assertEquals(savedAsset, foundAsset); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindAssetTypesByTenantId() throws Exception { |
||||
|
List<Asset> assets = new ArrayList<>(); |
||||
|
for (int i=0;i<3;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("My asset B"+i); |
||||
|
asset.setType("typeB"); |
||||
|
assets.add(doPost("/api/asset", asset, Asset.class)); |
||||
|
} |
||||
|
for (int i=0;i<7;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("My asset C"+i); |
||||
|
asset.setType("typeC"); |
||||
|
assets.add(doPost("/api/asset", asset, Asset.class)); |
||||
|
} |
||||
|
for (int i=0;i<9;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("My asset A"+i); |
||||
|
asset.setType("typeA"); |
||||
|
assets.add(doPost("/api/asset", asset, Asset.class)); |
||||
|
} |
||||
|
List<TenantAssetType> assetTypes = doGetTyped("/api/asset/types", |
||||
|
new TypeReference<List<TenantAssetType>>(){}); |
||||
|
|
||||
|
Assert.assertNotNull(assetTypes); |
||||
|
Assert.assertEquals(3, assetTypes.size()); |
||||
|
Assert.assertEquals("typeA", assetTypes.get(0).getType()); |
||||
|
Assert.assertEquals("typeB", assetTypes.get(1).getType()); |
||||
|
Assert.assertEquals("typeC", assetTypes.get(2).getType()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testDeleteAsset() throws Exception { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("My asset"); |
||||
|
asset.setType("default"); |
||||
|
Asset savedAsset = doPost("/api/asset", asset, Asset.class); |
||||
|
|
||||
|
doDelete("/api/asset/"+savedAsset.getId().getId().toString()) |
||||
|
.andExpect(status().isOk()); |
||||
|
|
||||
|
doGet("/api/asset/"+savedAsset.getId().getId().toString()) |
||||
|
.andExpect(status().isNotFound()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testSaveAssetWithEmptyType() throws Exception { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("My asset"); |
||||
|
doPost("/api/asset", asset) |
||||
|
.andExpect(status().isBadRequest()) |
||||
|
.andExpect(statusReason(containsString("Asset type should be specified"))); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testSaveAssetWithEmptyName() throws Exception { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setType("default"); |
||||
|
doPost("/api/asset", asset) |
||||
|
.andExpect(status().isBadRequest()) |
||||
|
.andExpect(statusReason(containsString("Asset name should be specified"))); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testAssignUnassignAssetToCustomer() throws Exception { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("My asset"); |
||||
|
asset.setType("default"); |
||||
|
Asset savedAsset = doPost("/api/asset", asset, Asset.class); |
||||
|
|
||||
|
Customer customer = new Customer(); |
||||
|
customer.setTitle("My customer"); |
||||
|
Customer savedCustomer = doPost("/api/customer", customer, Customer.class); |
||||
|
|
||||
|
Asset assignedAsset = doPost("/api/customer/" + savedCustomer.getId().getId().toString() |
||||
|
+ "/asset/" + savedAsset.getId().getId().toString(), Asset.class); |
||||
|
Assert.assertEquals(savedCustomer.getId(), assignedAsset.getCustomerId()); |
||||
|
|
||||
|
Asset foundAsset = doGet("/api/asset/" + savedAsset.getId().getId().toString(), Asset.class); |
||||
|
Assert.assertEquals(savedCustomer.getId(), foundAsset.getCustomerId()); |
||||
|
|
||||
|
Asset unassignedAsset = |
||||
|
doDelete("/api/customer/asset/" + savedAsset.getId().getId().toString(), Asset.class); |
||||
|
Assert.assertEquals(ModelConstants.NULL_UUID, unassignedAsset.getCustomerId().getId()); |
||||
|
|
||||
|
foundAsset = doGet("/api/asset/" + savedAsset.getId().getId().toString(), Asset.class); |
||||
|
Assert.assertEquals(ModelConstants.NULL_UUID, foundAsset.getCustomerId().getId()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testAssignAssetToNonExistentCustomer() throws Exception { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("My asset"); |
||||
|
asset.setType("default"); |
||||
|
Asset savedAsset = doPost("/api/asset", asset, Asset.class); |
||||
|
|
||||
|
doPost("/api/customer/" + UUIDs.timeBased().toString() |
||||
|
+ "/asset/" + savedAsset.getId().getId().toString()) |
||||
|
.andExpect(status().isNotFound()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testAssignAssetToCustomerFromDifferentTenant() throws Exception { |
||||
|
loginSysAdmin(); |
||||
|
|
||||
|
Tenant tenant2 = new Tenant(); |
||||
|
tenant2.setTitle("Different tenant"); |
||||
|
Tenant savedTenant2 = doPost("/api/tenant", tenant2, Tenant.class); |
||||
|
Assert.assertNotNull(savedTenant2); |
||||
|
|
||||
|
User tenantAdmin2 = new User(); |
||||
|
tenantAdmin2.setAuthority(Authority.TENANT_ADMIN); |
||||
|
tenantAdmin2.setTenantId(savedTenant2.getId()); |
||||
|
tenantAdmin2.setEmail("tenant3@thingsboard.org"); |
||||
|
tenantAdmin2.setFirstName("Joe"); |
||||
|
tenantAdmin2.setLastName("Downs"); |
||||
|
|
||||
|
tenantAdmin2 = createUserAndLogin(tenantAdmin2, "testPassword1"); |
||||
|
|
||||
|
Customer customer = new Customer(); |
||||
|
customer.setTitle("Different customer"); |
||||
|
Customer savedCustomer = doPost("/api/customer", customer, Customer.class); |
||||
|
|
||||
|
login(tenantAdmin.getEmail(), "testPassword1"); |
||||
|
|
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("My asset"); |
||||
|
asset.setType("default"); |
||||
|
Asset savedAsset = doPost("/api/asset", asset, Asset.class); |
||||
|
|
||||
|
doPost("/api/customer/" + savedCustomer.getId().getId().toString() |
||||
|
+ "/asset/" + savedAsset.getId().getId().toString()) |
||||
|
.andExpect(status().isForbidden()); |
||||
|
|
||||
|
loginSysAdmin(); |
||||
|
|
||||
|
doDelete("/api/tenant/"+savedTenant2.getId().getId().toString()) |
||||
|
.andExpect(status().isOk()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindTenantAssets() throws Exception { |
||||
|
List<Asset> assets = new ArrayList<>(); |
||||
|
for (int i=0;i<178;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("Asset"+i); |
||||
|
asset.setType("default"); |
||||
|
assets.add(doPost("/api/asset", asset, Asset.class)); |
||||
|
} |
||||
|
List<Asset> loadedAssets = new ArrayList<>(); |
||||
|
TextPageLink pageLink = new TextPageLink(23); |
||||
|
TextPageData<Asset> pageData = null; |
||||
|
do { |
||||
|
pageData = doGetTypedWithPageLink("/api/tenant/assets?", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink); |
||||
|
loadedAssets.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assets, idComparator); |
||||
|
Collections.sort(loadedAssets, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assets, loadedAssets); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindTenantAssetsByName() throws Exception { |
||||
|
String title1 = "Asset title 1"; |
||||
|
List<Asset> assetsTitle1 = new ArrayList<>(); |
||||
|
for (int i=0;i<143;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title1+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType("default"); |
||||
|
assetsTitle1.add(doPost("/api/asset", asset, Asset.class)); |
||||
|
} |
||||
|
String title2 = "Asset title 2"; |
||||
|
List<Asset> assetsTitle2 = new ArrayList<>(); |
||||
|
for (int i=0;i<75;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title2+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType("default"); |
||||
|
assetsTitle2.add(doPost("/api/asset", asset, Asset.class)); |
||||
|
} |
||||
|
|
||||
|
List<Asset> loadedAssetsTitle1 = new ArrayList<>(); |
||||
|
TextPageLink pageLink = new TextPageLink(15, title1); |
||||
|
TextPageData<Asset> pageData = null; |
||||
|
do { |
||||
|
pageData = doGetTypedWithPageLink("/api/tenant/assets?", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink); |
||||
|
loadedAssetsTitle1.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsTitle1, idComparator); |
||||
|
Collections.sort(loadedAssetsTitle1, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsTitle1, loadedAssetsTitle1); |
||||
|
|
||||
|
List<Asset> loadedAssetsTitle2 = new ArrayList<>(); |
||||
|
pageLink = new TextPageLink(4, title2); |
||||
|
do { |
||||
|
pageData = doGetTypedWithPageLink("/api/tenant/assets?", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink); |
||||
|
loadedAssetsTitle2.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsTitle2, idComparator); |
||||
|
Collections.sort(loadedAssetsTitle2, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsTitle2, loadedAssetsTitle2); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsTitle1) { |
||||
|
doDelete("/api/asset/"+asset.getId().getId().toString()) |
||||
|
.andExpect(status().isOk()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4, title1); |
||||
|
pageData = doGetTypedWithPageLink("/api/tenant/assets?", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsTitle2) { |
||||
|
doDelete("/api/asset/"+asset.getId().getId().toString()) |
||||
|
.andExpect(status().isOk()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4, title2); |
||||
|
pageData = doGetTypedWithPageLink("/api/tenant/assets?", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindTenantAssetsByType() throws Exception { |
||||
|
String title1 = "Asset title 1"; |
||||
|
String type1 = "typeA"; |
||||
|
List<Asset> assetsType1 = new ArrayList<>(); |
||||
|
for (int i=0;i<143;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title1+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType(type1); |
||||
|
assetsType1.add(doPost("/api/asset", asset, Asset.class)); |
||||
|
} |
||||
|
String title2 = "Asset title 2"; |
||||
|
String type2 = "typeB"; |
||||
|
List<Asset> assetsType2 = new ArrayList<>(); |
||||
|
for (int i=0;i<75;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title2+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType(type2); |
||||
|
assetsType2.add(doPost("/api/asset", asset, Asset.class)); |
||||
|
} |
||||
|
|
||||
|
List<Asset> loadedAssetsType1 = new ArrayList<>(); |
||||
|
TextPageLink pageLink = new TextPageLink(15); |
||||
|
TextPageData<Asset> pageData = null; |
||||
|
do { |
||||
|
pageData = doGetTypedWithPageLink("/api/tenant/assets?type={type}&", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink, type1); |
||||
|
loadedAssetsType1.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsType1, idComparator); |
||||
|
Collections.sort(loadedAssetsType1, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsType1, loadedAssetsType1); |
||||
|
|
||||
|
List<Asset> loadedAssetsType2 = new ArrayList<>(); |
||||
|
pageLink = new TextPageLink(4); |
||||
|
do { |
||||
|
pageData = doGetTypedWithPageLink("/api/tenant/assets?type={type}&", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink, type2); |
||||
|
loadedAssetsType2.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsType2, idComparator); |
||||
|
Collections.sort(loadedAssetsType2, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsType2, loadedAssetsType2); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsType1) { |
||||
|
doDelete("/api/asset/"+asset.getId().getId().toString()) |
||||
|
.andExpect(status().isOk()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4); |
||||
|
pageData = doGetTypedWithPageLink("/api/tenant/assets?type={type}&", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink, type1); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsType2) { |
||||
|
doDelete("/api/asset/"+asset.getId().getId().toString()) |
||||
|
.andExpect(status().isOk()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4); |
||||
|
pageData = doGetTypedWithPageLink("/api/tenant/assets?type={type}&", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink, type2); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindCustomerAssets() throws Exception { |
||||
|
Customer customer = new Customer(); |
||||
|
customer.setTitle("Test customer"); |
||||
|
customer = doPost("/api/customer", customer, Customer.class); |
||||
|
CustomerId customerId = customer.getId(); |
||||
|
|
||||
|
List<Asset> assets = new ArrayList<>(); |
||||
|
for (int i=0;i<128;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("Asset"+i); |
||||
|
asset.setType("default"); |
||||
|
asset = doPost("/api/asset", asset, Asset.class); |
||||
|
assets.add(doPost("/api/customer/" + customerId.getId().toString() |
||||
|
+ "/asset/" + asset.getId().getId().toString(), Asset.class)); |
||||
|
} |
||||
|
|
||||
|
List<Asset> loadedAssets = new ArrayList<>(); |
||||
|
TextPageLink pageLink = new TextPageLink(23); |
||||
|
TextPageData<Asset> pageData = null; |
||||
|
do { |
||||
|
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink); |
||||
|
loadedAssets.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assets, idComparator); |
||||
|
Collections.sort(loadedAssets, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assets, loadedAssets); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindCustomerAssetsByName() throws Exception { |
||||
|
Customer customer = new Customer(); |
||||
|
customer.setTitle("Test customer"); |
||||
|
customer = doPost("/api/customer", customer, Customer.class); |
||||
|
CustomerId customerId = customer.getId(); |
||||
|
|
||||
|
String title1 = "Asset title 1"; |
||||
|
List<Asset> assetsTitle1 = new ArrayList<>(); |
||||
|
for (int i=0;i<125;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title1+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType("default"); |
||||
|
asset = doPost("/api/asset", asset, Asset.class); |
||||
|
assetsTitle1.add(doPost("/api/customer/" + customerId.getId().toString() |
||||
|
+ "/asset/" + asset.getId().getId().toString(), Asset.class)); |
||||
|
} |
||||
|
String title2 = "Asset title 2"; |
||||
|
List<Asset> assetsTitle2 = new ArrayList<>(); |
||||
|
for (int i=0;i<143;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title2+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType("default"); |
||||
|
asset = doPost("/api/asset", asset, Asset.class); |
||||
|
assetsTitle2.add(doPost("/api/customer/" + customerId.getId().toString() |
||||
|
+ "/asset/" + asset.getId().getId().toString(), Asset.class)); |
||||
|
} |
||||
|
|
||||
|
List<Asset> loadedAssetsTitle1 = new ArrayList<>(); |
||||
|
TextPageLink pageLink = new TextPageLink(15, title1); |
||||
|
TextPageData<Asset> pageData = null; |
||||
|
do { |
||||
|
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink); |
||||
|
loadedAssetsTitle1.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsTitle1, idComparator); |
||||
|
Collections.sort(loadedAssetsTitle1, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsTitle1, loadedAssetsTitle1); |
||||
|
|
||||
|
List<Asset> loadedAssetsTitle2 = new ArrayList<>(); |
||||
|
pageLink = new TextPageLink(4, title2); |
||||
|
do { |
||||
|
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink); |
||||
|
loadedAssetsTitle2.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsTitle2, idComparator); |
||||
|
Collections.sort(loadedAssetsTitle2, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsTitle2, loadedAssetsTitle2); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsTitle1) { |
||||
|
doDelete("/api/customer/asset/" + asset.getId().getId().toString()) |
||||
|
.andExpect(status().isOk()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4, title1); |
||||
|
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsTitle2) { |
||||
|
doDelete("/api/customer/asset/" + asset.getId().getId().toString()) |
||||
|
.andExpect(status().isOk()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4, title2); |
||||
|
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindCustomerAssetsByType() throws Exception { |
||||
|
Customer customer = new Customer(); |
||||
|
customer.setTitle("Test customer"); |
||||
|
customer = doPost("/api/customer", customer, Customer.class); |
||||
|
CustomerId customerId = customer.getId(); |
||||
|
|
||||
|
String title1 = "Asset title 1"; |
||||
|
String type1 = "typeC"; |
||||
|
List<Asset> assetsType1 = new ArrayList<>(); |
||||
|
for (int i=0;i<125;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title1+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType(type1); |
||||
|
asset = doPost("/api/asset", asset, Asset.class); |
||||
|
assetsType1.add(doPost("/api/customer/" + customerId.getId().toString() |
||||
|
+ "/asset/" + asset.getId().getId().toString(), Asset.class)); |
||||
|
} |
||||
|
String title2 = "Asset title 2"; |
||||
|
String type2 = "typeD"; |
||||
|
List<Asset> assetsType2 = new ArrayList<>(); |
||||
|
for (int i=0;i<143;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title2+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType(type2); |
||||
|
asset = doPost("/api/asset", asset, Asset.class); |
||||
|
assetsType2.add(doPost("/api/customer/" + customerId.getId().toString() |
||||
|
+ "/asset/" + asset.getId().getId().toString(), Asset.class)); |
||||
|
} |
||||
|
|
||||
|
List<Asset> loadedAssetsType1 = new ArrayList<>(); |
||||
|
TextPageLink pageLink = new TextPageLink(15); |
||||
|
TextPageData<Asset> pageData = null; |
||||
|
do { |
||||
|
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?type={type}&", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink, type1); |
||||
|
loadedAssetsType1.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsType1, idComparator); |
||||
|
Collections.sort(loadedAssetsType1, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsType1, loadedAssetsType1); |
||||
|
|
||||
|
List<Asset> loadedAssetsType2 = new ArrayList<>(); |
||||
|
pageLink = new TextPageLink(4); |
||||
|
do { |
||||
|
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?type={type}&", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink, type2); |
||||
|
loadedAssetsType2.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsType2, idComparator); |
||||
|
Collections.sort(loadedAssetsType2, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsType2, loadedAssetsType2); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsType1) { |
||||
|
doDelete("/api/customer/asset/" + asset.getId().getId().toString()) |
||||
|
.andExpect(status().isOk()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4); |
||||
|
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?type={type}&", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink, type1); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsType2) { |
||||
|
doDelete("/api/customer/asset/" + asset.getId().getId().toString()) |
||||
|
.andExpect(status().isOk()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4); |
||||
|
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/assets?type={type}&", |
||||
|
new TypeReference<TextPageData<Asset>>(){}, pageLink, type2); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,79 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data; |
||||
|
|
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
|
||||
|
public class TenantDeviceType { |
||||
|
|
||||
|
private static final long serialVersionUID = 8057240243859922101L; |
||||
|
|
||||
|
private String type; |
||||
|
private TenantId tenantId; |
||||
|
|
||||
|
public TenantDeviceType() { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
public TenantDeviceType(String type, TenantId tenantId) { |
||||
|
this.type = type; |
||||
|
this.tenantId = tenantId; |
||||
|
} |
||||
|
|
||||
|
public String getType() { |
||||
|
return type; |
||||
|
} |
||||
|
|
||||
|
public void setType(String type) { |
||||
|
this.type = type; |
||||
|
} |
||||
|
|
||||
|
public TenantId getTenantId() { |
||||
|
return tenantId; |
||||
|
} |
||||
|
|
||||
|
public void setTenantId(TenantId tenantId) { |
||||
|
this.tenantId = tenantId; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int hashCode() { |
||||
|
int result = type != null ? type.hashCode() : 0; |
||||
|
result = 31 * result + (tenantId != null ? tenantId.hashCode() : 0); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean equals(Object o) { |
||||
|
if (this == o) return true; |
||||
|
if (o == null || getClass() != o.getClass()) return false; |
||||
|
|
||||
|
TenantDeviceType that = (TenantDeviceType) o; |
||||
|
|
||||
|
if (type != null ? !type.equals(that.type) : that.type != null) return false; |
||||
|
return tenantId != null ? tenantId.equals(that.tenantId) : that.tenantId == null; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
final StringBuilder sb = new StringBuilder("TenantDeviceType{"); |
||||
|
sb.append("type='").append(type).append('\''); |
||||
|
sb.append(", tenantId=").append(tenantId); |
||||
|
sb.append('}'); |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,79 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.asset; |
||||
|
|
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
|
||||
|
public class TenantAssetType { |
||||
|
|
||||
|
private static final long serialVersionUID = 8057290243855622101L; |
||||
|
|
||||
|
private String type; |
||||
|
private TenantId tenantId; |
||||
|
|
||||
|
public TenantAssetType() { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
public TenantAssetType(String type, TenantId tenantId) { |
||||
|
this.type = type; |
||||
|
this.tenantId = tenantId; |
||||
|
} |
||||
|
|
||||
|
public String getType() { |
||||
|
return type; |
||||
|
} |
||||
|
|
||||
|
public void setType(String type) { |
||||
|
this.type = type; |
||||
|
} |
||||
|
|
||||
|
public TenantId getTenantId() { |
||||
|
return tenantId; |
||||
|
} |
||||
|
|
||||
|
public void setTenantId(TenantId tenantId) { |
||||
|
this.tenantId = tenantId; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int hashCode() { |
||||
|
int result = type != null ? type.hashCode() : 0; |
||||
|
result = 31 * result + (tenantId != null ? tenantId.hashCode() : 0); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean equals(Object o) { |
||||
|
if (this == o) return true; |
||||
|
if (o == null || getClass() != o.getClass()) return false; |
||||
|
|
||||
|
TenantAssetType that = (TenantAssetType) o; |
||||
|
|
||||
|
if (type != null ? !type.equals(that.type) : that.type != null) return false; |
||||
|
return tenantId != null ? tenantId.equals(that.tenantId) : that.tenantId == null; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
final StringBuilder sb = new StringBuilder("TenantAssetType{"); |
||||
|
sb.append("type='").append(type).append('\''); |
||||
|
sb.append(", tenantId=").append(tenantId); |
||||
|
sb.append('}'); |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,59 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
|
||||
|
package org.thingsboard.server.common.data.relation; |
||||
|
|
||||
|
public class EntityRelationInfo extends EntityRelation { |
||||
|
|
||||
|
private static final long serialVersionUID = 2807343097519543363L; |
||||
|
|
||||
|
private String toName; |
||||
|
|
||||
|
public EntityRelationInfo() { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
public EntityRelationInfo(EntityRelation entityRelation) { |
||||
|
super(entityRelation); |
||||
|
} |
||||
|
|
||||
|
public String getToName() { |
||||
|
return toName; |
||||
|
} |
||||
|
|
||||
|
public void setToName(String toName) { |
||||
|
this.toName = toName; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean equals(Object o) { |
||||
|
if (this == o) return true; |
||||
|
if (o == null || getClass() != o.getClass()) return false; |
||||
|
if (!super.equals(o)) return false; |
||||
|
|
||||
|
EntityRelationInfo that = (EntityRelationInfo) o; |
||||
|
|
||||
|
return toName != null ? toName.equals(that.toName) : that.toName == null; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int hashCode() { |
||||
|
int result = super.hashCode(); |
||||
|
result = 31 * result + (toName != null ? toName.hashCode() : 0); |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.common.data.relation; |
||||
|
|
||||
|
public enum RelationTypeGroup { |
||||
|
|
||||
|
COMMON, |
||||
|
ALARM |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,46 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.dao.device; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import org.thingsboard.server.common.data.EntityType; |
||||
|
import org.thingsboard.server.common.data.relation.EntityRelation; |
||||
|
import org.thingsboard.server.dao.relation.RelationsSearchParameters; |
||||
|
import org.thingsboard.server.dao.relation.EntityRelationsQuery; |
||||
|
import org.thingsboard.server.dao.relation.EntityTypeFilter; |
||||
|
|
||||
|
import javax.annotation.Nullable; |
||||
|
import java.util.Collections; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Data |
||||
|
public class DeviceSearchQuery { |
||||
|
|
||||
|
private RelationsSearchParameters parameters; |
||||
|
@Nullable |
||||
|
private String relationType; |
||||
|
@Nullable |
||||
|
private List<String> deviceTypes; |
||||
|
|
||||
|
public EntityRelationsQuery toEntitySearchQuery() { |
||||
|
EntityRelationsQuery query = new EntityRelationsQuery(); |
||||
|
query.setParameters(parameters); |
||||
|
query.setFilters( |
||||
|
Collections.singletonList(new EntityTypeFilter(relationType == null ? EntityRelation.CONTAINS_TYPE : relationType, |
||||
|
Collections.singletonList(EntityType.DEVICE)))); |
||||
|
return query; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
|
||||
|
package org.thingsboard.server.dao.entity; |
||||
|
|
||||
|
import com.google.common.util.concurrent.ListenableFuture; |
||||
|
import org.thingsboard.server.common.data.id.EntityId; |
||||
|
|
||||
|
public interface EntityService { |
||||
|
|
||||
|
ListenableFuture<String> fetchEntityNameAsync(EntityId entityId); |
||||
|
|
||||
|
void deleteEntityRelations(EntityId entityId); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,107 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
|
||||
|
package org.thingsboard.server.dao.model; |
||||
|
|
||||
|
import com.datastax.driver.mapping.annotations.Column; |
||||
|
import com.datastax.driver.mapping.annotations.PartitionKey; |
||||
|
import com.datastax.driver.mapping.annotations.Table; |
||||
|
import com.datastax.driver.mapping.annotations.Transient; |
||||
|
import org.thingsboard.server.common.data.asset.TenantAssetType; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
|
||||
|
import java.util.UUID; |
||||
|
|
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.*; |
||||
|
|
||||
|
@Table(name = ASSET_TYPES_BY_TENANT_VIEW_NAME) |
||||
|
public class TenantAssetTypeEntity { |
||||
|
|
||||
|
@Transient |
||||
|
private static final long serialVersionUID = -1268181161886910152L; |
||||
|
|
||||
|
@PartitionKey(value = 0) |
||||
|
@Column(name = ASSET_TYPE_PROPERTY) |
||||
|
private String type; |
||||
|
|
||||
|
@PartitionKey(value = 1) |
||||
|
@Column(name = ASSET_TENANT_ID_PROPERTY) |
||||
|
private UUID tenantId; |
||||
|
|
||||
|
public TenantAssetTypeEntity() { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
public TenantAssetTypeEntity(TenantAssetType tenantAssetType) { |
||||
|
this.type = tenantAssetType.getType(); |
||||
|
if (tenantAssetType.getTenantId() != null) { |
||||
|
this.tenantId = tenantAssetType.getTenantId().getId(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public String getType() { |
||||
|
return type; |
||||
|
} |
||||
|
|
||||
|
public void setType(String type) { |
||||
|
this.type = type; |
||||
|
} |
||||
|
|
||||
|
public UUID getTenantId() { |
||||
|
return tenantId; |
||||
|
} |
||||
|
|
||||
|
public void setTenantId(UUID tenantId) { |
||||
|
this.tenantId = tenantId; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int hashCode() { |
||||
|
int result = type != null ? type.hashCode() : 0; |
||||
|
result = 31 * result + (tenantId != null ? tenantId.hashCode() : 0); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean equals(Object o) { |
||||
|
if (this == o) return true; |
||||
|
if (o == null || getClass() != o.getClass()) return false; |
||||
|
|
||||
|
TenantAssetTypeEntity that = (TenantAssetTypeEntity) o; |
||||
|
|
||||
|
if (type != null ? !type.equals(that.type) : that.type != null) return false; |
||||
|
return tenantId != null ? tenantId.equals(that.tenantId) : that.tenantId == null; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
final StringBuilder sb = new StringBuilder("TenantAssetTypeEntity{"); |
||||
|
sb.append("type='").append(type).append('\''); |
||||
|
sb.append(", tenantId=").append(tenantId); |
||||
|
sb.append('}'); |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public TenantAssetType toTenantAssetType() { |
||||
|
TenantAssetType tenantAssetType = new TenantAssetType(); |
||||
|
tenantAssetType.setType(type); |
||||
|
if (tenantId != null) { |
||||
|
tenantAssetType.setTenantId(new TenantId(tenantId)); |
||||
|
} |
||||
|
return tenantAssetType; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,107 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
|
||||
|
package org.thingsboard.server.dao.model; |
||||
|
|
||||
|
import com.datastax.driver.mapping.annotations.Column; |
||||
|
import com.datastax.driver.mapping.annotations.PartitionKey; |
||||
|
import com.datastax.driver.mapping.annotations.Table; |
||||
|
import com.datastax.driver.mapping.annotations.Transient; |
||||
|
import org.thingsboard.server.common.data.TenantDeviceType; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
|
||||
|
import java.util.UUID; |
||||
|
|
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.*; |
||||
|
|
||||
|
@Table(name = DEVICE_TYPES_BY_TENANT_VIEW_NAME) |
||||
|
public class TenantDeviceTypeEntity { |
||||
|
|
||||
|
@Transient |
||||
|
private static final long serialVersionUID = -1268181166886910152L; |
||||
|
|
||||
|
@PartitionKey(value = 0) |
||||
|
@Column(name = DEVICE_TYPE_PROPERTY) |
||||
|
private String type; |
||||
|
|
||||
|
@PartitionKey(value = 1) |
||||
|
@Column(name = DEVICE_TENANT_ID_PROPERTY) |
||||
|
private UUID tenantId; |
||||
|
|
||||
|
public TenantDeviceTypeEntity() { |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
public TenantDeviceTypeEntity(TenantDeviceType tenantDeviceType) { |
||||
|
this.type = tenantDeviceType.getType(); |
||||
|
if (tenantDeviceType.getTenantId() != null) { |
||||
|
this.tenantId = tenantDeviceType.getTenantId().getId(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public String getType() { |
||||
|
return type; |
||||
|
} |
||||
|
|
||||
|
public void setType(String type) { |
||||
|
this.type = type; |
||||
|
} |
||||
|
|
||||
|
public UUID getTenantId() { |
||||
|
return tenantId; |
||||
|
} |
||||
|
|
||||
|
public void setTenantId(UUID tenantId) { |
||||
|
this.tenantId = tenantId; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int hashCode() { |
||||
|
int result = type != null ? type.hashCode() : 0; |
||||
|
result = 31 * result + (tenantId != null ? tenantId.hashCode() : 0); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean equals(Object o) { |
||||
|
if (this == o) return true; |
||||
|
if (o == null || getClass() != o.getClass()) return false; |
||||
|
|
||||
|
TenantDeviceTypeEntity that = (TenantDeviceTypeEntity) o; |
||||
|
|
||||
|
if (type != null ? !type.equals(that.type) : that.type != null) return false; |
||||
|
return tenantId != null ? tenantId.equals(that.tenantId) : that.tenantId == null; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString() { |
||||
|
final StringBuilder sb = new StringBuilder("TenantDeviceTypeEntity{"); |
||||
|
sb.append("type='").append(type).append('\''); |
||||
|
sb.append(", tenantId=").append(tenantId); |
||||
|
sb.append('}'); |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public TenantDeviceType toTenantDeviceType() { |
||||
|
TenantDeviceType tenantDeviceType = new TenantDeviceType(); |
||||
|
tenantDeviceType.setType(type); |
||||
|
if (tenantId != null) { |
||||
|
tenantDeviceType.setTenantId(new TenantId(tenantId)); |
||||
|
} |
||||
|
return tenantDeviceType; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.dao.model.type; |
||||
|
|
||||
|
import org.thingsboard.server.common.data.relation.RelationTypeGroup; |
||||
|
|
||||
|
import com.datastax.driver.extras.codecs.enums.EnumNameCodec; |
||||
|
|
||||
|
public class RelationTypeGroupCodec extends EnumNameCodec<RelationTypeGroup> { |
||||
|
|
||||
|
public RelationTypeGroupCodec() { |
||||
|
super(RelationTypeGroup.class); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,634 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.dao.service; |
||||
|
|
||||
|
import com.datastax.driver.core.utils.UUIDs; |
||||
|
import org.apache.commons.lang3.RandomStringUtils; |
||||
|
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.Tenant; |
||||
|
import org.thingsboard.server.common.data.asset.Asset; |
||||
|
import org.thingsboard.server.common.data.asset.TenantAssetType; |
||||
|
import org.thingsboard.server.common.data.id.CustomerId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.page.TextPageData; |
||||
|
import org.thingsboard.server.common.data.page.TextPageLink; |
||||
|
import org.thingsboard.server.dao.exception.DataValidationException; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Collections; |
||||
|
import java.util.List; |
||||
|
|
||||
|
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; |
||||
|
|
||||
|
public class BaseAssetServiceTest extends AbstractServiceTest { |
||||
|
|
||||
|
private IdComparator<Asset> idComparator = new IdComparator<>(); |
||||
|
|
||||
|
private TenantId tenantId; |
||||
|
|
||||
|
@Before |
||||
|
public void before() { |
||||
|
Tenant tenant = new Tenant(); |
||||
|
tenant.setTitle("My tenant"); |
||||
|
Tenant savedTenant = tenantService.saveTenant(tenant); |
||||
|
Assert.assertNotNull(savedTenant); |
||||
|
tenantId = savedTenant.getId(); |
||||
|
} |
||||
|
|
||||
|
@After |
||||
|
public void after() { |
||||
|
tenantService.deleteTenant(tenantId); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testSaveAsset() { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
asset.setName("My asset"); |
||||
|
asset.setType("default"); |
||||
|
Asset savedAsset = assetService.saveAsset(asset); |
||||
|
|
||||
|
Assert.assertNotNull(savedAsset); |
||||
|
Assert.assertNotNull(savedAsset.getId()); |
||||
|
Assert.assertTrue(savedAsset.getCreatedTime() > 0); |
||||
|
Assert.assertEquals(asset.getTenantId(), savedAsset.getTenantId()); |
||||
|
Assert.assertNotNull(savedAsset.getCustomerId()); |
||||
|
Assert.assertEquals(NULL_UUID, savedAsset.getCustomerId().getId()); |
||||
|
Assert.assertEquals(asset.getName(), savedAsset.getName()); |
||||
|
|
||||
|
savedAsset.setName("My new asset"); |
||||
|
|
||||
|
assetService.saveAsset(savedAsset); |
||||
|
Asset foundAsset = assetService.findAssetById(savedAsset.getId()); |
||||
|
Assert.assertEquals(foundAsset.getName(), savedAsset.getName()); |
||||
|
|
||||
|
assetService.deleteAsset(savedAsset.getId()); |
||||
|
} |
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testSaveAssetWithEmptyName() { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
asset.setType("default"); |
||||
|
assetService.saveAsset(asset); |
||||
|
} |
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testSaveAssetWithEmptyTenant() { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("My asset"); |
||||
|
asset.setType("default"); |
||||
|
assetService.saveAsset(asset); |
||||
|
} |
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testSaveAssetWithInvalidTenant() { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("My asset"); |
||||
|
asset.setType("default"); |
||||
|
asset.setTenantId(new TenantId(UUIDs.timeBased())); |
||||
|
assetService.saveAsset(asset); |
||||
|
} |
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testAssignAssetToNonExistentCustomer() { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("My asset"); |
||||
|
asset.setType("default"); |
||||
|
asset.setTenantId(tenantId); |
||||
|
asset = assetService.saveAsset(asset); |
||||
|
try { |
||||
|
assetService.assignAssetToCustomer(asset.getId(), new CustomerId(UUIDs.timeBased())); |
||||
|
} finally { |
||||
|
assetService.deleteAsset(asset.getId()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testAssignAssetToCustomerFromDifferentTenant() { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setName("My asset"); |
||||
|
asset.setType("default"); |
||||
|
asset.setTenantId(tenantId); |
||||
|
asset = assetService.saveAsset(asset); |
||||
|
Tenant tenant = new Tenant(); |
||||
|
tenant.setTitle("Test different tenant"); |
||||
|
tenant = tenantService.saveTenant(tenant); |
||||
|
Customer customer = new Customer(); |
||||
|
customer.setTenantId(tenant.getId()); |
||||
|
customer.setTitle("Test different customer"); |
||||
|
customer = customerService.saveCustomer(customer); |
||||
|
try { |
||||
|
assetService.assignAssetToCustomer(asset.getId(), customer.getId()); |
||||
|
} finally { |
||||
|
assetService.deleteAsset(asset.getId()); |
||||
|
tenantService.deleteTenant(tenant.getId()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindAssetById() { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
asset.setName("My asset"); |
||||
|
asset.setType("default"); |
||||
|
Asset savedAsset = assetService.saveAsset(asset); |
||||
|
Asset foundAsset = assetService.findAssetById(savedAsset.getId()); |
||||
|
Assert.assertNotNull(foundAsset); |
||||
|
Assert.assertEquals(savedAsset, foundAsset); |
||||
|
assetService.deleteAsset(savedAsset.getId()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindAssetTypesByTenantId() throws Exception { |
||||
|
List<Asset> assets = new ArrayList<>(); |
||||
|
try { |
||||
|
for (int i=0;i<3;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
asset.setName("My asset B"+i); |
||||
|
asset.setType("typeB"); |
||||
|
assets.add(assetService.saveAsset(asset)); |
||||
|
} |
||||
|
for (int i=0;i<7;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
asset.setName("My asset C"+i); |
||||
|
asset.setType("typeC"); |
||||
|
assets.add(assetService.saveAsset(asset)); |
||||
|
} |
||||
|
for (int i=0;i<9;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
asset.setName("My asset A"+i); |
||||
|
asset.setType("typeA"); |
||||
|
assets.add(assetService.saveAsset(asset)); |
||||
|
} |
||||
|
List<TenantAssetType> assetTypes = assetService.findAssetTypesByTenantId(tenantId).get(); |
||||
|
Assert.assertNotNull(assetTypes); |
||||
|
Assert.assertEquals(3, assetTypes.size()); |
||||
|
Assert.assertEquals("typeA", assetTypes.get(0).getType()); |
||||
|
Assert.assertEquals("typeB", assetTypes.get(1).getType()); |
||||
|
Assert.assertEquals("typeC", assetTypes.get(2).getType()); |
||||
|
} finally { |
||||
|
assets.forEach((asset) -> { assetService.deleteAsset(asset.getId()); }); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testDeleteAsset() { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
asset.setName("My asset"); |
||||
|
asset.setType("default"); |
||||
|
Asset savedAsset = assetService.saveAsset(asset); |
||||
|
Asset foundAsset = assetService.findAssetById(savedAsset.getId()); |
||||
|
Assert.assertNotNull(foundAsset); |
||||
|
assetService.deleteAsset(savedAsset.getId()); |
||||
|
foundAsset = assetService.findAssetById(savedAsset.getId()); |
||||
|
Assert.assertNull(foundAsset); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindAssetsByTenantId() { |
||||
|
Tenant tenant = new Tenant(); |
||||
|
tenant.setTitle("Test tenant"); |
||||
|
tenant = tenantService.saveTenant(tenant); |
||||
|
|
||||
|
TenantId tenantId = tenant.getId(); |
||||
|
|
||||
|
List<Asset> assets = new ArrayList<>(); |
||||
|
for (int i=0;i<178;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
asset.setName("Asset"+i); |
||||
|
asset.setType("default"); |
||||
|
assets.add(assetService.saveAsset(asset)); |
||||
|
} |
||||
|
|
||||
|
List<Asset> loadedAssets = new ArrayList<>(); |
||||
|
TextPageLink pageLink = new TextPageLink(23); |
||||
|
TextPageData<Asset> pageData = null; |
||||
|
do { |
||||
|
pageData = assetService.findAssetsByTenantId(tenantId, pageLink); |
||||
|
loadedAssets.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assets, idComparator); |
||||
|
Collections.sort(loadedAssets, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assets, loadedAssets); |
||||
|
|
||||
|
assetService.deleteAssetsByTenantId(tenantId); |
||||
|
|
||||
|
pageLink = new TextPageLink(33); |
||||
|
pageData = assetService.findAssetsByTenantId(tenantId, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertTrue(pageData.getData().isEmpty()); |
||||
|
|
||||
|
tenantService.deleteTenant(tenantId); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindAssetsByTenantIdAndName() { |
||||
|
String title1 = "Asset title 1"; |
||||
|
List<Asset> assetsTitle1 = new ArrayList<>(); |
||||
|
for (int i=0;i<143;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title1+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType("default"); |
||||
|
assetsTitle1.add(assetService.saveAsset(asset)); |
||||
|
} |
||||
|
String title2 = "Asset title 2"; |
||||
|
List<Asset> assetsTitle2 = new ArrayList<>(); |
||||
|
for (int i=0;i<175;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title2+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType("default"); |
||||
|
assetsTitle2.add(assetService.saveAsset(asset)); |
||||
|
} |
||||
|
|
||||
|
List<Asset> loadedAssetsTitle1 = new ArrayList<>(); |
||||
|
TextPageLink pageLink = new TextPageLink(15, title1); |
||||
|
TextPageData<Asset> pageData = null; |
||||
|
do { |
||||
|
pageData = assetService.findAssetsByTenantId(tenantId, pageLink); |
||||
|
loadedAssetsTitle1.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsTitle1, idComparator); |
||||
|
Collections.sort(loadedAssetsTitle1, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsTitle1, loadedAssetsTitle1); |
||||
|
|
||||
|
List<Asset> loadedAssetsTitle2 = new ArrayList<>(); |
||||
|
pageLink = new TextPageLink(4, title2); |
||||
|
do { |
||||
|
pageData = assetService.findAssetsByTenantId(tenantId, pageLink); |
||||
|
loadedAssetsTitle2.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsTitle2, idComparator); |
||||
|
Collections.sort(loadedAssetsTitle2, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsTitle2, loadedAssetsTitle2); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsTitle1) { |
||||
|
assetService.deleteAsset(asset.getId()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4, title1); |
||||
|
pageData = assetService.findAssetsByTenantId(tenantId, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsTitle2) { |
||||
|
assetService.deleteAsset(asset.getId()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4, title2); |
||||
|
pageData = assetService.findAssetsByTenantId(tenantId, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindAssetsByTenantIdAndType() { |
||||
|
String title1 = "Asset title 1"; |
||||
|
String type1 = "typeA"; |
||||
|
List<Asset> assetsType1 = new ArrayList<>(); |
||||
|
for (int i=0;i<143;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title1+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType(type1); |
||||
|
assetsType1.add(assetService.saveAsset(asset)); |
||||
|
} |
||||
|
String title2 = "Asset title 2"; |
||||
|
String type2 = "typeB"; |
||||
|
List<Asset> assetsType2 = new ArrayList<>(); |
||||
|
for (int i=0;i<175;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title2+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType(type2); |
||||
|
assetsType2.add(assetService.saveAsset(asset)); |
||||
|
} |
||||
|
|
||||
|
List<Asset> loadedAssetsType1 = new ArrayList<>(); |
||||
|
TextPageLink pageLink = new TextPageLink(15); |
||||
|
TextPageData<Asset> pageData = null; |
||||
|
do { |
||||
|
pageData = assetService.findAssetsByTenantIdAndType(tenantId, type1, pageLink); |
||||
|
loadedAssetsType1.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsType1, idComparator); |
||||
|
Collections.sort(loadedAssetsType1, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsType1, loadedAssetsType1); |
||||
|
|
||||
|
List<Asset> loadedAssetsType2 = new ArrayList<>(); |
||||
|
pageLink = new TextPageLink(4); |
||||
|
do { |
||||
|
pageData = assetService.findAssetsByTenantIdAndType(tenantId, type2, pageLink); |
||||
|
loadedAssetsType2.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsType2, idComparator); |
||||
|
Collections.sort(loadedAssetsType2, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsType2, loadedAssetsType2); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsType1) { |
||||
|
assetService.deleteAsset(asset.getId()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4); |
||||
|
pageData = assetService.findAssetsByTenantIdAndType(tenantId, type1, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsType2) { |
||||
|
assetService.deleteAsset(asset.getId()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4); |
||||
|
pageData = assetService.findAssetsByTenantIdAndType(tenantId, type2, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindAssetsByTenantIdAndCustomerId() { |
||||
|
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(); |
||||
|
|
||||
|
List<Asset> assets = new ArrayList<>(); |
||||
|
for (int i=0;i<278;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
asset.setName("Asset"+i); |
||||
|
asset.setType("default"); |
||||
|
asset = assetService.saveAsset(asset); |
||||
|
assets.add(assetService.assignAssetToCustomer(asset.getId(), customerId)); |
||||
|
} |
||||
|
|
||||
|
List<Asset> loadedAssets = new ArrayList<>(); |
||||
|
TextPageLink pageLink = new TextPageLink(23); |
||||
|
TextPageData<Asset> pageData = null; |
||||
|
do { |
||||
|
pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink); |
||||
|
loadedAssets.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assets, idComparator); |
||||
|
Collections.sort(loadedAssets, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assets, loadedAssets); |
||||
|
|
||||
|
assetService.unassignCustomerAssets(tenantId, customerId); |
||||
|
|
||||
|
pageLink = new TextPageLink(33); |
||||
|
pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertTrue(pageData.getData().isEmpty()); |
||||
|
|
||||
|
tenantService.deleteTenant(tenantId); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindAssetsByTenantIdCustomerIdAndName() { |
||||
|
|
||||
|
Customer customer = new Customer(); |
||||
|
customer.setTitle("Test customer"); |
||||
|
customer.setTenantId(tenantId); |
||||
|
customer = customerService.saveCustomer(customer); |
||||
|
CustomerId customerId = customer.getId(); |
||||
|
|
||||
|
String title1 = "Asset title 1"; |
||||
|
List<Asset> assetsTitle1 = new ArrayList<>(); |
||||
|
for (int i=0;i<175;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title1+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType("default"); |
||||
|
asset = assetService.saveAsset(asset); |
||||
|
assetsTitle1.add(assetService.assignAssetToCustomer(asset.getId(), customerId)); |
||||
|
} |
||||
|
String title2 = "Asset title 2"; |
||||
|
List<Asset> assetsTitle2 = new ArrayList<>(); |
||||
|
for (int i=0;i<143;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title2+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType("default"); |
||||
|
asset = assetService.saveAsset(asset); |
||||
|
assetsTitle2.add(assetService.assignAssetToCustomer(asset.getId(), customerId)); |
||||
|
} |
||||
|
|
||||
|
List<Asset> loadedAssetsTitle1 = new ArrayList<>(); |
||||
|
TextPageLink pageLink = new TextPageLink(15, title1); |
||||
|
TextPageData<Asset> pageData = null; |
||||
|
do { |
||||
|
pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink); |
||||
|
loadedAssetsTitle1.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsTitle1, idComparator); |
||||
|
Collections.sort(loadedAssetsTitle1, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsTitle1, loadedAssetsTitle1); |
||||
|
|
||||
|
List<Asset> loadedAssetsTitle2 = new ArrayList<>(); |
||||
|
pageLink = new TextPageLink(4, title2); |
||||
|
do { |
||||
|
pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink); |
||||
|
loadedAssetsTitle2.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsTitle2, idComparator); |
||||
|
Collections.sort(loadedAssetsTitle2, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsTitle2, loadedAssetsTitle2); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsTitle1) { |
||||
|
assetService.deleteAsset(asset.getId()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4, title1); |
||||
|
pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsTitle2) { |
||||
|
assetService.deleteAsset(asset.getId()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4, title2); |
||||
|
pageData = assetService.findAssetsByTenantIdAndCustomerId(tenantId, customerId, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
customerService.deleteCustomer(customerId); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindAssetsByTenantIdCustomerIdAndType() { |
||||
|
|
||||
|
Customer customer = new Customer(); |
||||
|
customer.setTitle("Test customer"); |
||||
|
customer.setTenantId(tenantId); |
||||
|
customer = customerService.saveCustomer(customer); |
||||
|
CustomerId customerId = customer.getId(); |
||||
|
|
||||
|
String title1 = "Asset title 1"; |
||||
|
String type1 = "typeC"; |
||||
|
List<Asset> assetsType1 = new ArrayList<>(); |
||||
|
for (int i=0;i<175;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title1+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType(type1); |
||||
|
asset = assetService.saveAsset(asset); |
||||
|
assetsType1.add(assetService.assignAssetToCustomer(asset.getId(), customerId)); |
||||
|
} |
||||
|
String title2 = "Asset title 2"; |
||||
|
String type2 = "typeD"; |
||||
|
List<Asset> assetsType2 = new ArrayList<>(); |
||||
|
for (int i=0;i<143;i++) { |
||||
|
Asset asset = new Asset(); |
||||
|
asset.setTenantId(tenantId); |
||||
|
String suffix = RandomStringUtils.randomAlphanumeric(15); |
||||
|
String name = title2+suffix; |
||||
|
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); |
||||
|
asset.setName(name); |
||||
|
asset.setType(type2); |
||||
|
asset = assetService.saveAsset(asset); |
||||
|
assetsType2.add(assetService.assignAssetToCustomer(asset.getId(), customerId)); |
||||
|
} |
||||
|
|
||||
|
List<Asset> loadedAssetsType1 = new ArrayList<>(); |
||||
|
TextPageLink pageLink = new TextPageLink(15); |
||||
|
TextPageData<Asset> pageData = null; |
||||
|
do { |
||||
|
pageData = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type1, pageLink); |
||||
|
loadedAssetsType1.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsType1, idComparator); |
||||
|
Collections.sort(loadedAssetsType1, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsType1, loadedAssetsType1); |
||||
|
|
||||
|
List<Asset> loadedAssetsType2 = new ArrayList<>(); |
||||
|
pageLink = new TextPageLink(4); |
||||
|
do { |
||||
|
pageData = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type2, pageLink); |
||||
|
loadedAssetsType2.addAll(pageData.getData()); |
||||
|
if (pageData.hasNext()) { |
||||
|
pageLink = pageData.getNextPageLink(); |
||||
|
} |
||||
|
} while (pageData.hasNext()); |
||||
|
|
||||
|
Collections.sort(assetsType2, idComparator); |
||||
|
Collections.sort(loadedAssetsType2, idComparator); |
||||
|
|
||||
|
Assert.assertEquals(assetsType2, loadedAssetsType2); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsType1) { |
||||
|
assetService.deleteAsset(asset.getId()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4); |
||||
|
pageData = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type1, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
|
||||
|
for (Asset asset : loadedAssetsType2) { |
||||
|
assetService.deleteAsset(asset.getId()); |
||||
|
} |
||||
|
|
||||
|
pageLink = new TextPageLink(4); |
||||
|
pageData = assetService.findAssetsByTenantIdAndCustomerIdAndType(tenantId, customerId, type2, pageLink); |
||||
|
Assert.assertFalse(pageData.hasNext()); |
||||
|
Assert.assertEquals(0, pageData.getData().size()); |
||||
|
customerService.deleteCustomer(customerId); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,284 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
package org.thingsboard.server.dao.service; |
||||
|
|
||||
|
import com.datastax.driver.core.utils.UUIDs; |
||||
|
import com.google.common.util.concurrent.ListenableFuture; |
||||
|
import org.junit.After; |
||||
|
import org.junit.Assert; |
||||
|
import org.junit.Before; |
||||
|
import org.junit.Test; |
||||
|
import org.thingsboard.server.common.data.EntityType; |
||||
|
import org.thingsboard.server.common.data.asset.Asset; |
||||
|
import org.thingsboard.server.common.data.id.AssetId; |
||||
|
import org.thingsboard.server.common.data.id.DeviceId; |
||||
|
import org.thingsboard.server.common.data.relation.EntityRelation; |
||||
|
import org.thingsboard.server.common.data.relation.RelationTypeGroup; |
||||
|
import org.thingsboard.server.dao.exception.DataValidationException; |
||||
|
import org.thingsboard.server.dao.relation.EntityRelationsQuery; |
||||
|
import org.thingsboard.server.dao.relation.EntitySearchDirection; |
||||
|
import org.thingsboard.server.dao.relation.EntityTypeFilter; |
||||
|
import org.thingsboard.server.dao.relation.RelationsSearchParameters; |
||||
|
|
||||
|
import java.util.Collections; |
||||
|
import java.util.List; |
||||
|
import java.util.concurrent.ExecutionException; |
||||
|
|
||||
|
public class RelationServiceImplTest extends AbstractServiceTest { |
||||
|
|
||||
|
@Before |
||||
|
public void before() { |
||||
|
} |
||||
|
|
||||
|
@After |
||||
|
public void after() { |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testSaveRelation() throws ExecutionException, InterruptedException { |
||||
|
AssetId parentId = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId childId = new AssetId(UUIDs.timeBased()); |
||||
|
|
||||
|
EntityRelation relation = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE); |
||||
|
|
||||
|
Assert.assertTrue(saveRelation(relation)); |
||||
|
|
||||
|
Assert.assertTrue(relationService.checkRelation(parentId, childId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get()); |
||||
|
|
||||
|
Assert.assertFalse(relationService.checkRelation(parentId, childId, "NOT_EXISTING_TYPE", RelationTypeGroup.COMMON).get()); |
||||
|
|
||||
|
Assert.assertFalse(relationService.checkRelation(childId, parentId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get()); |
||||
|
|
||||
|
Assert.assertFalse(relationService.checkRelation(childId, parentId, "NOT_EXISTING_TYPE", RelationTypeGroup.COMMON).get()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testDeleteRelation() throws ExecutionException, InterruptedException { |
||||
|
AssetId parentId = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId childId = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId subChildId = new AssetId(UUIDs.timeBased()); |
||||
|
|
||||
|
EntityRelation relationA = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE); |
||||
|
EntityRelation relationB = new EntityRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE); |
||||
|
|
||||
|
saveRelation(relationA); |
||||
|
saveRelation(relationB); |
||||
|
|
||||
|
Assert.assertTrue(relationService.deleteRelation(relationA).get()); |
||||
|
|
||||
|
Assert.assertFalse(relationService.checkRelation(parentId, childId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get()); |
||||
|
|
||||
|
Assert.assertTrue(relationService.checkRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get()); |
||||
|
|
||||
|
Assert.assertTrue(relationService.deleteRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testDeleteEntityRelations() throws ExecutionException, InterruptedException { |
||||
|
AssetId parentId = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId childId = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId subChildId = new AssetId(UUIDs.timeBased()); |
||||
|
|
||||
|
EntityRelation relationA = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE); |
||||
|
EntityRelation relationB = new EntityRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE); |
||||
|
|
||||
|
saveRelation(relationA); |
||||
|
saveRelation(relationB); |
||||
|
|
||||
|
Assert.assertTrue(relationService.deleteEntityRelations(childId).get()); |
||||
|
|
||||
|
Assert.assertFalse(relationService.checkRelation(parentId, childId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get()); |
||||
|
|
||||
|
Assert.assertFalse(relationService.checkRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindFrom() throws ExecutionException, InterruptedException { |
||||
|
AssetId parentA = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId parentB = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId childA = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId childB = new AssetId(UUIDs.timeBased()); |
||||
|
|
||||
|
EntityRelation relationA1 = new EntityRelation(parentA, childA, EntityRelation.CONTAINS_TYPE); |
||||
|
EntityRelation relationA2 = new EntityRelation(parentA, childB, EntityRelation.CONTAINS_TYPE); |
||||
|
|
||||
|
EntityRelation relationB1 = new EntityRelation(parentB, childA, EntityRelation.MANAGES_TYPE); |
||||
|
EntityRelation relationB2 = new EntityRelation(parentB, childB, EntityRelation.MANAGES_TYPE); |
||||
|
|
||||
|
saveRelation(relationA1); |
||||
|
saveRelation(relationA2); |
||||
|
|
||||
|
saveRelation(relationB1); |
||||
|
saveRelation(relationB2); |
||||
|
|
||||
|
List<EntityRelation> relations = relationService.findByFrom(parentA, RelationTypeGroup.COMMON).get(); |
||||
|
Assert.assertEquals(2, relations.size()); |
||||
|
for (EntityRelation relation : relations) { |
||||
|
Assert.assertEquals(EntityRelation.CONTAINS_TYPE, relation.getType()); |
||||
|
Assert.assertEquals(parentA, relation.getFrom()); |
||||
|
Assert.assertTrue(childA.equals(relation.getTo()) || childB.equals(relation.getTo())); |
||||
|
} |
||||
|
|
||||
|
relations = relationService.findByFromAndType(parentA, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get(); |
||||
|
Assert.assertEquals(2, relations.size()); |
||||
|
|
||||
|
relations = relationService.findByFromAndType(parentA, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON).get(); |
||||
|
Assert.assertEquals(0, relations.size()); |
||||
|
|
||||
|
relations = relationService.findByFrom(parentB, RelationTypeGroup.COMMON).get(); |
||||
|
Assert.assertEquals(2, relations.size()); |
||||
|
for (EntityRelation relation : relations) { |
||||
|
Assert.assertEquals(EntityRelation.MANAGES_TYPE, relation.getType()); |
||||
|
Assert.assertEquals(parentB, relation.getFrom()); |
||||
|
Assert.assertTrue(childA.equals(relation.getTo()) || childB.equals(relation.getTo())); |
||||
|
} |
||||
|
|
||||
|
relations = relationService.findByFromAndType(parentB, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get(); |
||||
|
Assert.assertEquals(0, relations.size()); |
||||
|
|
||||
|
relations = relationService.findByFromAndType(parentB, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get(); |
||||
|
Assert.assertEquals(0, relations.size()); |
||||
|
} |
||||
|
|
||||
|
private Boolean saveRelation(EntityRelation relationA1) throws ExecutionException, InterruptedException { |
||||
|
return relationService.saveRelation(relationA1).get(); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testFindTo() throws ExecutionException, InterruptedException { |
||||
|
AssetId parentA = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId parentB = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId childA = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId childB = new AssetId(UUIDs.timeBased()); |
||||
|
|
||||
|
EntityRelation relationA1 = new EntityRelation(parentA, childA, EntityRelation.CONTAINS_TYPE); |
||||
|
EntityRelation relationA2 = new EntityRelation(parentA, childB, EntityRelation.CONTAINS_TYPE); |
||||
|
|
||||
|
EntityRelation relationB1 = new EntityRelation(parentB, childA, EntityRelation.MANAGES_TYPE); |
||||
|
EntityRelation relationB2 = new EntityRelation(parentB, childB, EntityRelation.MANAGES_TYPE); |
||||
|
|
||||
|
saveRelation(relationA1); |
||||
|
saveRelation(relationA2); |
||||
|
|
||||
|
saveRelation(relationB1); |
||||
|
saveRelation(relationB2); |
||||
|
|
||||
|
// Data propagation to views is async
|
||||
|
Thread.sleep(3000); |
||||
|
|
||||
|
List<EntityRelation> relations = relationService.findByTo(childA, RelationTypeGroup.COMMON).get(); |
||||
|
Assert.assertEquals(2, relations.size()); |
||||
|
for (EntityRelation relation : relations) { |
||||
|
Assert.assertEquals(childA, relation.getTo()); |
||||
|
Assert.assertTrue(parentA.equals(relation.getFrom()) || parentB.equals(relation.getFrom())); |
||||
|
} |
||||
|
|
||||
|
relations = relationService.findByToAndType(childA, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get(); |
||||
|
Assert.assertEquals(1, relations.size()); |
||||
|
|
||||
|
relations = relationService.findByToAndType(childB, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON).get(); |
||||
|
Assert.assertEquals(1, relations.size()); |
||||
|
|
||||
|
relations = relationService.findByToAndType(parentA, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON).get(); |
||||
|
Assert.assertEquals(0, relations.size()); |
||||
|
|
||||
|
relations = relationService.findByToAndType(parentB, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON).get(); |
||||
|
Assert.assertEquals(0, relations.size()); |
||||
|
|
||||
|
relations = relationService.findByTo(childB, RelationTypeGroup.COMMON).get(); |
||||
|
Assert.assertEquals(2, relations.size()); |
||||
|
for (EntityRelation relation : relations) { |
||||
|
Assert.assertEquals(childB, relation.getTo()); |
||||
|
Assert.assertTrue(parentA.equals(relation.getFrom()) || parentB.equals(relation.getFrom())); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testCyclicRecursiveRelation() throws ExecutionException, InterruptedException { |
||||
|
// A -> B -> C -> A
|
||||
|
AssetId assetA = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId assetB = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId assetC = new AssetId(UUIDs.timeBased()); |
||||
|
|
||||
|
EntityRelation relationA = new EntityRelation(assetA, assetB, EntityRelation.CONTAINS_TYPE); |
||||
|
EntityRelation relationB = new EntityRelation(assetB, assetC, EntityRelation.CONTAINS_TYPE); |
||||
|
EntityRelation relationC = new EntityRelation(assetC, assetA, EntityRelation.CONTAINS_TYPE); |
||||
|
|
||||
|
saveRelation(relationA); |
||||
|
saveRelation(relationB); |
||||
|
saveRelation(relationC); |
||||
|
|
||||
|
EntityRelationsQuery query = new EntityRelationsQuery(); |
||||
|
query.setParameters(new RelationsSearchParameters(assetA, EntitySearchDirection.FROM, -1)); |
||||
|
query.setFilters(Collections.singletonList(new EntityTypeFilter(EntityRelation.CONTAINS_TYPE, Collections.singletonList(EntityType.ASSET)))); |
||||
|
List<EntityRelation> relations = relationService.findByQuery(query).get(); |
||||
|
Assert.assertEquals(3, relations.size()); |
||||
|
Assert.assertTrue(relations.contains(relationA)); |
||||
|
Assert.assertTrue(relations.contains(relationB)); |
||||
|
Assert.assertTrue(relations.contains(relationC)); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void testRecursiveRelation() throws ExecutionException, InterruptedException { |
||||
|
// A -> B -> [C,D]
|
||||
|
AssetId assetA = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId assetB = new AssetId(UUIDs.timeBased()); |
||||
|
AssetId assetC = new AssetId(UUIDs.timeBased()); |
||||
|
DeviceId deviceD = new DeviceId(UUIDs.timeBased()); |
||||
|
|
||||
|
EntityRelation relationAB = new EntityRelation(assetA, assetB, EntityRelation.CONTAINS_TYPE); |
||||
|
EntityRelation relationBC = new EntityRelation(assetB, assetC, EntityRelation.CONTAINS_TYPE); |
||||
|
EntityRelation relationBD = new EntityRelation(assetB, deviceD, EntityRelation.CONTAINS_TYPE); |
||||
|
|
||||
|
|
||||
|
saveRelation(relationAB); |
||||
|
saveRelation(relationBC); |
||||
|
saveRelation(relationBD); |
||||
|
|
||||
|
EntityRelationsQuery query = new EntityRelationsQuery(); |
||||
|
query.setParameters(new RelationsSearchParameters(assetA, EntitySearchDirection.FROM, -1)); |
||||
|
query.setFilters(Collections.singletonList(new EntityTypeFilter(EntityRelation.CONTAINS_TYPE, Collections.singletonList(EntityType.ASSET)))); |
||||
|
List<EntityRelation> relations = relationService.findByQuery(query).get(); |
||||
|
Assert.assertEquals(2, relations.size()); |
||||
|
Assert.assertTrue(relations.contains(relationAB)); |
||||
|
Assert.assertTrue(relations.contains(relationBC)); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testSaveRelationWithEmptyFrom() throws ExecutionException, InterruptedException { |
||||
|
EntityRelation relation = new EntityRelation(); |
||||
|
relation.setTo(new AssetId(UUIDs.timeBased())); |
||||
|
relation.setType(EntityRelation.CONTAINS_TYPE); |
||||
|
Assert.assertTrue(saveRelation(relation)); |
||||
|
} |
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testSaveRelationWithEmptyTo() throws ExecutionException, InterruptedException { |
||||
|
EntityRelation relation = new EntityRelation(); |
||||
|
relation.setFrom(new AssetId(UUIDs.timeBased())); |
||||
|
relation.setType(EntityRelation.CONTAINS_TYPE); |
||||
|
Assert.assertTrue(saveRelation(relation)); |
||||
|
} |
||||
|
|
||||
|
@Test(expected = DataValidationException.class) |
||||
|
public void testSaveRelationWithEmptyType() throws ExecutionException, InterruptedException { |
||||
|
EntityRelation relation = new EntityRelation(); |
||||
|
relation.setFrom(new AssetId(UUIDs.timeBased())); |
||||
|
relation.setTo(new AssetId(UUIDs.timeBased())); |
||||
|
Assert.assertTrue(saveRelation(relation)); |
||||
|
} |
||||
|
} |
||||
@ -1 +1,7 @@ |
|||||
CASSANDRA_DATA_DIR=/home/docker/cassandra_volume |
CASSANDRA_DATA_DIR=/home/docker/cassandra_volume |
||||
|
|
||||
|
# cassandra schema container environment variables |
||||
|
CREATE_SCHEMA=true |
||||
|
ADD_SYSTEM_DATA=false |
||||
|
ADD_DEMO_DATA=false |
||||
|
CASSANDRA_URL=cassandra |
||||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue