tests: relation query - added test cases with many edges between two nodes, horizontal ring (loop) on the same level, loop Tenant-Asset-Device-Tenant. This commit will fail 4 hierarchy tests. Reformat code

This commit is contained in:
Sergey Matvienko 2021-08-30 11:58:29 +03:00
parent 7f3d63512e
commit a5d446914c

View File

@ -19,6 +19,7 @@ import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.RandomUtils;
import org.hamcrest.Matchers;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
@ -82,6 +83,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
public abstract class BaseEntityServiceTest extends AbstractServiceTest { public abstract class BaseEntityServiceTest extends AbstractServiceTest {
@ -159,7 +161,7 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
public void testCountHierarchicalEntitiesByQuery() throws InterruptedException { public void testCountHierarchicalEntitiesByQuery() throws InterruptedException {
List<Asset> assets = new ArrayList<>(); List<Asset> assets = new ArrayList<>();
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
createTestHierarchy(assets, devices, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>()); createTestHierarchy(tenantId, assets, devices, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
RelationsQueryFilter filter = new RelationsQueryFilter(); RelationsQueryFilter filter = new RelationsQueryFilter();
filter.setRootEntity(tenantId); filter.setRootEntity(tenantId);
@ -308,7 +310,7 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
List<Long> temperatures = new ArrayList<>(); List<Long> temperatures = new ArrayList<>();
List<Long> highTemperatures = new ArrayList<>(); List<Long> highTemperatures = new ArrayList<>();
createTestHierarchy(assets, devices, new ArrayList<>(), new ArrayList<>(), temperatures, highTemperatures); createTestHierarchy(tenantId, assets, devices, new ArrayList<>(), new ArrayList<>(), temperatures, highTemperatures);
List<ListenableFuture<List<Void>>> attributeFutures = new ArrayList<>(); List<ListenableFuture<List<Void>>> attributeFutures = new ArrayList<>();
for (int i = 0; i < devices.size(); i++) { for (int i = 0; i < devices.size(); i++) {
@ -380,7 +382,7 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
List<Long> temperatures = new ArrayList<>(); List<Long> temperatures = new ArrayList<>();
List<Long> highTemperatures = new ArrayList<>(); List<Long> highTemperatures = new ArrayList<>();
createTestHierarchy(assets, devices, new ArrayList<>(), new ArrayList<>(), temperatures, highTemperatures); createTestHierarchy(tenantId, assets, devices, new ArrayList<>(), new ArrayList<>(), temperatures, highTemperatures);
List<ListenableFuture<List<Void>>> attributeFutures = new ArrayList<>(); List<ListenableFuture<List<Void>>> attributeFutures = new ArrayList<>();
for (int i = 0; i < devices.size(); i++) { for (int i = 0; i < devices.size(); i++) {
@ -453,7 +455,7 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
List<Long> consumptions = new ArrayList<>(); List<Long> consumptions = new ArrayList<>();
List<Long> highConsumptions = new ArrayList<>(); List<Long> highConsumptions = new ArrayList<>();
createTestHierarchy(assets, devices, consumptions, highConsumptions, new ArrayList<>(), new ArrayList<>()); createTestHierarchy(tenantId, assets, devices, consumptions, highConsumptions, new ArrayList<>(), new ArrayList<>());
List<ListenableFuture<List<Void>>> attributeFutures = new ArrayList<>(); List<ListenableFuture<List<Void>>> attributeFutures = new ArrayList<>();
for (int i = 0; i < assets.size(); i++) { for (int i = 0; i < assets.size(); i++) {
@ -518,7 +520,7 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
deviceService.deleteDevicesByTenantId(tenantId); deviceService.deleteDevicesByTenantId(tenantId);
} }
private void createTestHierarchy(List<Asset> assets, List<Device> devices, List<Long> consumptions, List<Long> highConsumptions, List<Long> temperatures, List<Long> highTemperatures) throws InterruptedException { private void createTestHierarchy(TenantId tenantId, List<Asset> assets, List<Device> devices, List<Long> consumptions, List<Long> highConsumptions, List<Long> temperatures, List<Long> highTemperatures) throws InterruptedException {
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
Asset asset = new Asset(); Asset asset = new Asset();
asset.setTenantId(tenantId); asset.setTenantId(tenantId);
@ -563,8 +565,40 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
} }
} }
} }
createManyCustomRelationsBetweenTwoNodes(tenantId, "UseCase", assets, devices);
createHorizontalRingRelations(tenantId, "Ring(Loop)-Ast", assets);
createLoopRelations(tenantId, "Loop-Tnt-Ast-Dev", tenantId, assets.get(0).getId(), devices.get(0).getId());
createLoopRelations(tenantId, "Loop-Tnt-Ast", tenantId, assets.get(1).getId());
createLoopRelations(tenantId, "Loop-Ast-Tnt-Ast", assets.get(2).getId(), tenantId, assets.get(3).getId());
} }
void createLoopRelations(TenantId tenantId, String type, EntityId... ids) {
assertThat("ids lenght", ids.length, Matchers.greaterThanOrEqualTo(1));
//chain all from the head to the tail
for (int i = 1; i < ids.length; i++) {
relationService.saveRelation(tenantId, new EntityRelation(ids[i - 1], ids[i], type, RelationTypeGroup.COMMON));
}
//chain tail -> head
relationService.saveRelation(tenantId, new EntityRelation(ids[ids.length - 1], ids[0], type, RelationTypeGroup.COMMON));
}
void createHorizontalRingRelations(TenantId tenantId, String type, List<Asset> assets) {
createLoopRelations(tenantId, type, assets.stream().map(Asset::getId).toArray(EntityId[]::new));
}
void createManyCustomRelationsBetweenTwoNodes(TenantId tenantId, String type, List<Asset> assets, List<Device> devices) {
for (int i = 1; i <= 5; i++) {
final String typeI = type + i;
createOneToManyRelations(tenantId, typeI, tenantId, assets.stream().map(Asset::getId).collect(Collectors.toList()));
assets.forEach(asset ->
createOneToManyRelations(tenantId, typeI, asset.getId(), devices.stream().map(Device::getId).collect(Collectors.toList())));
}
}
void createOneToManyRelations(TenantId tenantId, String type, EntityId from, List<EntityId> toIds) {
toIds.forEach(toId -> relationService.saveRelation(tenantId, new EntityRelation(from, toId, type, RelationTypeGroup.COMMON)));
}
@Test @Test
public void testSimpleFindEntityDataByQuery() throws InterruptedException { public void testSimpleFindEntityDataByQuery() throws InterruptedException {
@ -871,7 +905,7 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
} }
@Test @Test
public void testBuildNumericPredicateQueryOperations() throws ExecutionException, InterruptedException{ public void testBuildNumericPredicateQueryOperations() throws ExecutionException, InterruptedException {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
List<Long> temperatures = new ArrayList<>(); List<Long> temperatures = new ArrayList<>();
@ -1122,7 +1156,7 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
} }
@Test @Test
public void testBuildStringPredicateQueryOperations() throws ExecutionException, InterruptedException{ public void testBuildStringPredicateQueryOperations() throws ExecutionException, InterruptedException {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
List<String> attributeStrings = new ArrayList<>(); List<String> attributeStrings = new ArrayList<>();
@ -1142,11 +1176,11 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
devices.add(deviceService.saveDevice(device)); devices.add(deviceService.saveDevice(device));
//TO make sure devices have different created time //TO make sure devices have different created time
Thread.sleep(1); Thread.sleep(1);
List<StringFilterPredicate.StringOperation> operationValues= Arrays.asList(StringFilterPredicate.StringOperation.values()); List<StringFilterPredicate.StringOperation> operationValues = Arrays.asList(StringFilterPredicate.StringOperation.values());
StringFilterPredicate.StringOperation operation = operationValues.get(new Random().nextInt(operationValues.size())); StringFilterPredicate.StringOperation operation = operationValues.get(new Random().nextInt(operationValues.size()));
String operationName = operation.name(); String operationName = operation.name();
attributeStrings.add(operationName); attributeStrings.add(operationName);
switch(operation){ switch (operation) {
case EQUAL: case EQUAL:
equalStrings.add(operationName); equalStrings.add(operationName);
notContainsStrings.add(operationName); notContainsStrings.add(operationName);
@ -1302,7 +1336,7 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
} }
@Test @Test
public void testBuildStringPredicateQueryOperationsForEntityType() throws ExecutionException, InterruptedException{ public void testBuildStringPredicateQueryOperationsForEntityType() throws ExecutionException, InterruptedException {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
@ -1419,7 +1453,7 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
} }
@Test @Test
public void testBuildSimplePredicateQueryOperations() throws InterruptedException{ public void testBuildSimplePredicateQueryOperations() throws InterruptedException {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
@ -1492,7 +1526,7 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
return loadedEntities; return loadedEntities;
} }
private List<KeyFilter> createStringKeyFilters(String key, EntityKeyType keyType, StringFilterPredicate.StringOperation operation, String value){ private List<KeyFilter> createStringKeyFilters(String key, EntityKeyType keyType, StringFilterPredicate.StringOperation operation, String value) {
KeyFilter filter = new KeyFilter(); KeyFilter filter = new KeyFilter();
filter.setKey(new EntityKey(keyType, key)); filter.setKey(new EntityKey(keyType, key));
StringFilterPredicate predicate = new StringFilterPredicate(); StringFilterPredicate predicate = new StringFilterPredicate();
@ -1503,7 +1537,7 @@ public abstract class BaseEntityServiceTest extends AbstractServiceTest {
return Collections.singletonList(filter); return Collections.singletonList(filter);
} }
private KeyFilter createNumericKeyFilter(String key, EntityKeyType keyType, NumericFilterPredicate.NumericOperation operation, double value){ private KeyFilter createNumericKeyFilter(String key, EntityKeyType keyType, NumericFilterPredicate.NumericOperation operation, double value) {
KeyFilter filter = new KeyFilter(); KeyFilter filter = new KeyFilter();
filter.setKey(new EntityKey(keyType, key)); filter.setKey(new EntityKey(keyType, key));
NumericFilterPredicate predicate = new NumericFilterPredicate(); NumericFilterPredicate predicate = new NumericFilterPredicate();