use of com.google.api.ads.admanager.axis.v202205.Value in project entity-service by hypertrace.
the class EntityQueryServiceImplTest method testBulkUpdateEntityArrayAttribute.
@Test
void testBulkUpdateEntityArrayAttribute() throws Exception {
List<String> entityIds = IntStream.rangeClosed(1, 5).mapToObj(i -> UUID.randomUUID().toString()).collect(Collectors.toList());
BulkEntityArrayAttributeUpdateRequest request = BulkEntityArrayAttributeUpdateRequest.newBuilder().addAllEntityIds(entityIds).setAttribute(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID3).build()).setEntityType(TEST_ENTITY_TYPE).setOperation(BulkEntityArrayAttributeUpdateRequest.Operation.OPERATION_ADD).addAllValues(List.of(LiteralConstant.newBuilder().setValue(Value.newBuilder().setString("Label1")).build(), LiteralConstant.newBuilder().setValue(Value.newBuilder().setString("Label2")).build())).build();
when(mockAttributeMapping.getDocStorePathByAttributeId(requestContext, ATTRIBUTE_ID3)).thenReturn(Optional.of(EDS_COLUMN_NAME3));
StreamObserver<BulkEntityArrayAttributeUpdateResponse> mockResponseObserver = mock(StreamObserver.class);
Context.current().withValue(RequestContext.CURRENT, mockRequestContextWithTenantId()).call(() -> {
EntityQueryServiceImpl eqs = new EntityQueryServiceImpl(entitiesCollection, mockAttributeMapping, 1, false);
eqs.bulkUpdateEntityArrayAttribute(request, mockResponseObserver);
return null;
});
ArgumentCaptor<BulkArrayValueUpdateRequest> argumentCaptor = ArgumentCaptor.forClass(BulkArrayValueUpdateRequest.class);
verify(entitiesCollection, times(1)).bulkOperationOnArrayValue(argumentCaptor.capture());
BulkArrayValueUpdateRequest bulkArrayValueUpdateRequest = argumentCaptor.getValue();
assertEquals(entityIds.stream().map(entityId -> new SingleValueKey("tenant1", entityId)).collect(Collectors.toCollection(LinkedHashSet::new)), bulkArrayValueUpdateRequest.getKeys());
assertEquals(BulkArrayValueUpdateRequest.Operation.ADD, bulkArrayValueUpdateRequest.getOperation());
assertEquals(EDS_COLUMN_NAME3 + ".valueList.values", bulkArrayValueUpdateRequest.getSubDocPath());
List<Document> subDocuments = bulkArrayValueUpdateRequest.getSubDocuments();
assertEquals(2, subDocuments.size());
assertEquals("{\"value\":{\"string\":\"Label1\"}}", subDocuments.get(0).toString());
assertEquals("{\"value\":{\"string\":\"Label2\"}}", subDocuments.get(1).toString());
}
use of com.google.api.ads.admanager.axis.v202205.Value in project entity-service by hypertrace.
the class DocStoreConverter method transform.
private static void transform(AttributeValue attributeValue, Filter filter, boolean isAttributeField) throws IOException {
switch(attributeValue.getTypeCase()) {
case VALUE:
{
Value value = attributeValue.getValue();
String fieldName = filter.getFieldName();
if (isAttributeField) {
fieldName = filter.getFieldName() + "." + "value" + "." + value.getTypeCase().name().toLowerCase();
}
filter.setFieldName(fieldName);
filter.setValue(getValue(value));
}
break;
case VALUE_LIST:
{
filter.setFieldName(createFieldNameForValueList(attributeValue, filter, isAttributeField));
if (filter.getOp().equals(Op.CONTAINS)) {
JsonNode mapNode = OBJECT_MAPPER.readTree(JSONFORMAT_PRINTER.print(attributeValue.getValue()));
Map map = OBJECT_MAPPER.convertValue(mapNode, Map.class);
filter.setValue(map);
} else if (filter.getOp().equals(Filter.Op.EQ)) {
List<Object> listNodes = new ArrayList<>();
for (AttributeValue v : attributeValue.getValueList().getValuesList()) {
listNodes.add(OBJECT_MAPPER.convertValue(OBJECT_MAPPER.readTree(JSONFORMAT_PRINTER.print(v)), Map.class));
}
filter.setValue(listNodes);
} else if (filter.getOp().equals(Op.IN)) {
List<Object> listNodes = new ArrayList<>();
for (AttributeValue v : attributeValue.getValueList().getValuesList()) {
listNodes.add(getValue(v.getValue()));
}
filter.setValue(listNodes);
} else {
throw new UnsupportedOperationException("Only CONTAINS, EQ and IN conditions supported for attribute values of type list");
}
}
break;
case VALUE_MAP:
{
if (filter.getOp().equals(Filter.Op.EQ)) {
String fieldName = filter.getFieldName();
if (isAttributeField) {
fieldName = filter.getFieldName() + "." + "valueMap";
}
filter.setFieldName(fieldName);
JsonNode mapNode = OBJECT_MAPPER.readTree(JSONFORMAT_PRINTER.print(attributeValue.getValueMap()));
Map map = OBJECT_MAPPER.convertValue(mapNode, Map.class);
filter.setValue(map);
} else {
throw new UnsupportedOperationException("Only EQ condition supported for attribute values of type map");
}
}
break;
}
}
use of com.google.api.ads.admanager.axis.v202205.Value in project entity-service by hypertrace.
the class ArrayFilteringExpressionConverter method convert.
@Override
public FilterTypeExpression convert(final ColumnIdentifier columnIdentifier, final Operator operator, final LiteralConstant constant, final RequestContext requestContext) throws ConversionException {
if (ARRAY_OPERATORS.contains(operator)) {
return primitiveFilteringExpressionConverter.convert(columnIdentifier, operator, constant, requestContext);
}
final String id = columnIdentifier.getColumnName();
final String subDocPath = getSubDocPathById(entityAttributeMapping, id, requestContext);
final Value value = constant.getValue();
final ValueType valueType = value.getValueType();
final List<?> list = valueOneOfAccessor.access(value, valueType);
if (list.isEmpty()) {
throw new ConversionException("Conversion of empty-list is unsupported");
}
final IdentifierConverter identifierConverter = identifierConverterFactory.getIdentifierConverter(id, subDocPath, valueType, requestContext);
final IdentifierConversionMetadata metadata = IdentifierConversionMetadata.builder().subDocPath(subDocPath).operator(operator).valueType(valueType).build();
final String suffixedSubDocPath = identifierConverter.convert(metadata, requestContext);
final List<RelationalExpression> expressions = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
final IdentifierExpression lhs = IdentifierExpression.of(String.format(suffixedSubDocPath, i));
final RelationalOperator relationalOperator = convertOperator(operator);
final ConstantExpression rhs = valueHelper.convertToConstantExpression(value, i);
final RelationalExpression expression = RelationalExpression.of(lhs, relationalOperator, rhs);
expressions.add(expression);
}
if (expressions.size() == 1) {
return expressions.get(0);
}
return LogicalExpression.builder().operator(LogicalOperator.AND).operands(expressions).build();
}
use of com.google.api.ads.admanager.axis.v202205.Value in project entity-service by hypertrace.
the class MapFilteringExpressionConverter method convert.
@Override
public FilterTypeExpression convert(final ColumnIdentifier columnIdentifier, final Operator operator, final LiteralConstant constant, final RequestContext requestContext) throws ConversionException {
if (!SUPPORTED_OPERATORS.contains(operator)) {
throw new ConversionException(String.format("Operator %s is not supported", operator));
}
final String id = columnIdentifier.getColumnName();
final String subDocPath = getSubDocPathById(entityAttributeMapping, id, requestContext);
final Value value = constant.getValue();
final ValueType valueType = value.getValueType();
final Map<?, ?> map = valueOneOfAccessor.access(value, valueType);
if (map.isEmpty()) {
throw new ConversionException("Conversion of empty-map is unsupported");
}
final IdentifierConverter identifierConverter = identifierConverterFactory.getIdentifierConverter(id, subDocPath, valueType, requestContext);
final IdentifierConversionMetadata metadata = IdentifierConversionMetadata.builder().subDocPath(subDocPath).operator(operator).valueType(valueType).build();
final String suffixedSubDocPath = identifierConverter.convert(metadata, requestContext);
final List<RelationalExpression> expressions = new ArrayList<>();
for (final Map.Entry<?, ?> entry : map.entrySet()) {
final IdentifierExpression lhs = IdentifierExpression.of(String.format(suffixedSubDocPath, entry.getKey()));
final RelationalOperator relationalOperator = convertOperator(operator);
final ConstantExpression rhs = valueHelper.convertToConstantExpression(value, entry.getKey());
final RelationalExpression expression = RelationalExpression.of(lhs, relationalOperator, rhs);
expressions.add(expression);
}
if (expressions.size() == 1) {
return expressions.get(0);
}
return LogicalExpression.builder().operator(LogicalOperator.AND).operands(expressions).build();
}
use of com.google.api.ads.admanager.axis.v202205.Value in project entity-service by hypertrace.
the class NullFilteringExpressionConverter method convert.
@Override
public FilterTypeExpression convert(final ColumnIdentifier columnIdentifier, final Operator operator, final LiteralConstant constant, final RequestContext requestContext) throws ConversionException {
final String id = columnIdentifier.getColumnName();
final String subDocPath = getSubDocPathById(entityAttributeMapping, id, requestContext);
final Value value = constant.getValue();
final ValueType valueType = value.getValueType();
final IdentifierConverter identifierConverter = identifierConverterFactory.getIdentifierConverter(id, subDocPath, valueType, requestContext);
final IdentifierConversionMetadata metadata = IdentifierConversionMetadata.builder().subDocPath(subDocPath).operator(operator).valueType(valueType).build();
final String suffixedSubDocPath = identifierConverter.convert(metadata, requestContext);
final IdentifierExpression identifierExpression = IdentifierExpression.of(suffixedSubDocPath);
final RelationalOperator relationalOperator = convertOperator(operator);
final ConstantExpression constantExpression = constantExpressionConverter.convert(constant, requestContext);
RelationalExpression relationalExpression = RelationalExpression.of(identifierExpression, relationalOperator, constantExpression);
switch(operator) {
// 'field' EQ 'null' -> 'field' EQ 'null' || 'field' NOT_EXISTS
case EQ:
RelationalExpression nonExistenceRelationalExpression = RelationalExpression.of(identifierExpression, convertOperator(Operator.NOT_EXISTS), constantExpression);
return LogicalExpression.builder().operator(LogicalOperator.OR).operands(List.of(nonExistenceRelationalExpression, relationalExpression)).build();
default:
return relationalExpression;
}
}
Aggregations