use of org.hypertrace.entity.data.service.v1.Entities in project entity-service by hypertrace.
the class EntityQueryServiceTest method testExecuteWithCompositeAttributeFilters.
@Test
void testExecuteWithCompositeAttributeFilters() {
// create and upsert some entities
List<String> filterValue1 = List.of(generateRandomUUID(), generateRandomUUID());
AttributeValueList.Builder attributeValueListBuilder = AttributeValueList.newBuilder();
filterValue1.stream().map(this::generateAttrValue).forEach(attributeValueListBuilder::addValues);
Entity entity1 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 1").putAttributes("labels", AttributeValue.newBuilder().setValueList(attributeValueListBuilder).build()).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
Entity createdEntity1 = entityDataServiceClient.upsert(entity1);
assertNotNull(createdEntity1);
assertFalse(createdEntity1.getEntityId().trim().isEmpty());
String filterValue2 = "DISCOVERED";
Entity entity2 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 2").putAttributes(apiAttributesMap.get(API_DISCOVERY_STATE_ATTR), createAttribute(filterValue2)).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
Entity createdEntity2 = entityDataServiceClient.upsert(entity2);
assertNotNull(createdEntity2);
assertFalse(createdEntity2.getEntityId().trim().isEmpty());
Map<String, String> filterValue3 = Map.of("uuid", generateRandomUUID());
Map<String, AttributeValue> attributeValueMap = filterValue3.entrySet().stream().collect(toUnmodifiableMap(Entry::getKey, e -> generateAttrValue(e.getValue())));
AttributeValueMap attributeMap = AttributeValueMap.newBuilder().putAllValues(attributeValueMap).build();
Entity entity3 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 3").putAttributes("http_url", AttributeValue.newBuilder().setValueMap(attributeMap).build()).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
Entity createdEntity3 = entityDataServiceClient.upsert(entity3);
assertNotNull(createdEntity3);
assertFalse(createdEntity3.getEntityId().trim().isEmpty());
Entity entity4 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 4").putAttributes("http_url", generateRandomUUIDAttrValue()).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
Entity createdEntity4 = entityDataServiceClient.upsert(entity4);
assertNotNull(createdEntity4);
assertFalse(createdEntity4.getEntityId().trim().isEmpty());
Entity entity5 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 5").putAttributes(apiAttributesMap.get(API_DISCOVERY_STATE_ATTR), generateRandomUUIDAttrValue()).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
Entity createdEntity5 = entityDataServiceClient.upsert(entity5);
assertNotNull(createdEntity5);
assertFalse(createdEntity5.getEntityId().trim().isEmpty());
EntityQueryRequest queryRequest = EntityQueryRequest.newBuilder().setEntityType(EntityType.SERVICE.name()).setFilter(Filter.newBuilder().setOperator(Operator.OR).addChildFilter(Filter.newBuilder().setOperator(Operator.IN).setLhs(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(API_LABELS_ATTR))).setRhs(Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setValueType(STRING_ARRAY).addAllStringArray(filterValue1))))).addChildFilter(Filter.newBuilder().setOperator(Operator.EQ).setLhs(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(API_DISCOVERY_STATE_ATTR))).setRhs(Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setValueType(STRING).setString(filterValue2))))).addChildFilter(Filter.newBuilder().setOperator(Operator.EQ).setLhs(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(API_HTTP_URL_ATTR))).setRhs(Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setValueType(STRING_MAP).putAllStringMap(filterValue3))))).build()).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("API.id").build()).build()).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("API.name").build()).build()).build();
Iterator<ResultSetChunk> resultSetChunkIterator = GrpcClientRequestContextUtil.executeWithHeadersContext(HEADERS, () -> entityQueryServiceClient.execute(queryRequest));
List<ResultSetChunk> list = Lists.newArrayList(resultSetChunkIterator);
assertEquals(2, list.size());
assertEquals(2, list.get(0).getRowCount());
assertEquals(0, list.get(0).getChunkId());
assertFalse(list.get(0).getIsLastChunk());
assertEquals(1, list.get(1).getRowCount());
assertEquals(1, list.get(1).getChunkId());
assertTrue(list.get(1).getIsLastChunk());
assertEquals(createdEntity1.getEntityId(), list.get(0).getRow(0).getColumn(0).getString());
assertEquals(createdEntity1.getEntityName(), list.get(0).getRow(0).getColumn(1).getString());
assertEquals(createdEntity2.getEntityId(), list.get(0).getRow(1).getColumn(0).getString());
assertEquals(createdEntity2.getEntityName(), list.get(0).getRow(1).getColumn(1).getString());
assertEquals(createdEntity3.getEntityId(), list.get(1).getRow(0).getColumn(0).getString());
assertEquals(createdEntity3.getEntityName(), list.get(1).getRow(0).getColumn(1).getString());
assertTrue(list.get(0).getResultSetMetadata().getColumnMetadataCount() > 0);
assertTrue(list.get(1).getResultSetMetadata().getColumnMetadataCount() > 0);
}
use of org.hypertrace.entity.data.service.v1.Entities in project entity-service by hypertrace.
the class EntityQueryServiceTest method testCreateAndGetEntity.
@Test
public void testCreateAndGetEntity() {
// creating an api entity with attributes
Entity.Builder apiEntityBuilder = createApiEntity(SERVICE_ID, API_NAME, API_TYPE);
apiEntityBuilder.putAttributes(apiAttributesMap.get(API_DISCOVERY_STATE_ATTR), createAttribute("DISCOVERED")).putAttributes(apiAttributesMap.get(API_HTTP_METHOD_ATTR), createAttribute("GET"));
entityDataServiceClient.upsert(apiEntityBuilder.build());
// querying the api entities
EntityQueryRequest entityQueryRequest = EntityQueryRequest.newBuilder().setEntityType(EntityType.API.name()).addSelection(createExpression(API_DISCOVERY_STATE_ATTR)).build();
Iterator<ResultSetChunk> resultSetChunkIterator = GrpcClientRequestContextUtil.executeWithHeadersContext(HEADERS, () -> entityQueryServiceClient.execute(entityQueryRequest));
List<String> values = new ArrayList<>();
while (resultSetChunkIterator.hasNext()) {
ResultSetChunk chunk = resultSetChunkIterator.next();
for (Row row : chunk.getRowList()) {
for (int i = 0; i < row.getColumnCount(); i++) {
String value = row.getColumnList().get(i).getString();
values.add(value);
}
}
}
assertEquals(1, values.size());
assertEquals("DISCOVERED", values.get(0));
}
Aggregations