Search in sources :

Example 1 with Value

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());
}
Also used : Context(io.grpc.Context) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Builder(org.hypertrace.entity.query.service.v1.LiteralConstant.Builder) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) UpdateOperation(org.hypertrace.entity.query.service.v1.UpdateOperation) ResultSetMetadata(org.hypertrace.entity.query.service.v1.ResultSetMetadata) Query(org.hypertrace.core.documentstore.Query) Disabled(org.junit.jupiter.api.Disabled) ArgumentMatcher(org.mockito.ArgumentMatcher) StreamObserver(io.grpc.stub.StreamObserver) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) BulkEntityArrayAttributeUpdateResponse(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateResponse) BulkEntityArrayAttributeUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateRequest) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Value(org.hypertrace.entity.query.service.v1.Value) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) BulkEntityUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ColumnMetadata(org.hypertrace.entity.query.service.v1.ColumnMetadata) Test(org.junit.jupiter.api.Test) List(java.util.List) JsonFormat(com.google.protobuf.util.JsonFormat) TotalEntitiesRequest(org.hypertrace.entity.query.service.v1.TotalEntitiesRequest) Optional(java.util.Optional) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) LiteralConstant(org.hypertrace.entity.query.service.v1.LiteralConstant) Mockito.mock(org.mockito.Mockito.mock) SetAttribute(org.hypertrace.entity.query.service.v1.SetAttribute) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) Row(org.hypertrace.entity.query.service.v1.Row) Document(org.hypertrace.core.documentstore.Document) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) DocStoreJsonFormat(org.hypertrace.entity.service.util.DocStoreJsonFormat) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) Filter(org.hypertrace.core.documentstore.Filter) Mock(org.mockito.Mock) EntityUpdateRequest(org.hypertrace.entity.query.service.v1.EntityUpdateRequest) LENIENT(org.mockito.quality.Strictness.LENIENT) Nested(org.junit.jupiter.api.Nested) Collection(org.hypertrace.core.documentstore.Collection) ArgumentCaptor(org.mockito.ArgumentCaptor) EntityUpdateInfo(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest.EntityUpdateInfo) AttributeValue(org.hypertrace.entity.data.service.v1.AttributeValue) Entity(org.hypertrace.entity.data.service.v1.Entity) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) LinkedHashSet(java.util.LinkedHashSet) TotalEntitiesResponse(org.hypertrace.entity.query.service.v1.TotalEntitiesResponse) BulkArrayValueUpdateRequest(org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest) ColumnIdentifier(org.hypertrace.entity.query.service.v1.ColumnIdentifier) BulkUpdateResult(org.hypertrace.core.documentstore.BulkUpdateResult) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) DisplayName(org.junit.jupiter.api.DisplayName) Mockito(org.mockito.Mockito) TestUtils.convertToCloseableIterator(org.hypertrace.entity.TestUtils.convertToCloseableIterator) Expression(org.hypertrace.entity.query.service.v1.Expression) ValueType(org.hypertrace.entity.query.service.v1.ValueType) OrderByExpression(org.hypertrace.entity.query.service.v1.OrderByExpression) LinkedHashSet(java.util.LinkedHashSet) BulkEntityArrayAttributeUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateRequest) BulkEntityArrayAttributeUpdateResponse(org.hypertrace.entity.query.service.v1.BulkEntityArrayAttributeUpdateResponse) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) BulkArrayValueUpdateRequest(org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Test(org.junit.jupiter.api.Test)

Example 2 with Value

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;
    }
}
Also used : AttributeValue(org.hypertrace.entity.data.service.v1.AttributeValue) AttributeValue(org.hypertrace.entity.data.service.v1.AttributeValue) Value(org.hypertrace.entity.data.service.v1.Value) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 3 with Value

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();
}
Also used : ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) RelationalOperator(org.hypertrace.core.documentstore.expression.operators.RelationalOperator) RelationalExpression(org.hypertrace.core.documentstore.expression.impl.RelationalExpression) ValueType(org.hypertrace.entity.query.service.v1.ValueType) ConstantExpression(org.hypertrace.core.documentstore.expression.impl.ConstantExpression) ArrayList(java.util.ArrayList) IdentifierExpression(org.hypertrace.core.documentstore.expression.impl.IdentifierExpression) IdentifierConverter(org.hypertrace.entity.query.service.converter.identifier.IdentifierConverter) Value(org.hypertrace.entity.query.service.v1.Value) IdentifierConversionMetadata(org.hypertrace.entity.query.service.converter.identifier.IdentifierConversionMetadata)

Example 4 with Value

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();
}
Also used : ConversionException(org.hypertrace.entity.query.service.converter.ConversionException) RelationalOperator(org.hypertrace.core.documentstore.expression.operators.RelationalOperator) RelationalExpression(org.hypertrace.core.documentstore.expression.impl.RelationalExpression) ValueType(org.hypertrace.entity.query.service.v1.ValueType) ConstantExpression(org.hypertrace.core.documentstore.expression.impl.ConstantExpression) ArrayList(java.util.ArrayList) IdentifierExpression(org.hypertrace.core.documentstore.expression.impl.IdentifierExpression) IdentifierConverter(org.hypertrace.entity.query.service.converter.identifier.IdentifierConverter) Value(org.hypertrace.entity.query.service.v1.Value) Map(java.util.Map) IdentifierConversionMetadata(org.hypertrace.entity.query.service.converter.identifier.IdentifierConversionMetadata)

Example 5 with Value

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;
    }
}
Also used : RelationalOperator(org.hypertrace.core.documentstore.expression.operators.RelationalOperator) RelationalExpression(org.hypertrace.core.documentstore.expression.impl.RelationalExpression) ValueType(org.hypertrace.entity.query.service.v1.ValueType) IdentifierConverter(org.hypertrace.entity.query.service.converter.identifier.IdentifierConverter) ConstantExpression(org.hypertrace.core.documentstore.expression.impl.ConstantExpression) Value(org.hypertrace.entity.query.service.v1.Value) IdentifierConversionMetadata(org.hypertrace.entity.query.service.converter.identifier.IdentifierConversionMetadata) IdentifierExpression(org.hypertrace.core.documentstore.expression.impl.IdentifierExpression)

Aggregations

AdManagerServices (com.google.api.ads.admanager.axis.factory.AdManagerServices)162 ApiError (com.google.api.ads.admanager.axis.v202205.ApiError)162 ApiException (com.google.api.ads.admanager.axis.v202205.ApiException)162 AdManagerSession (com.google.api.ads.admanager.lib.client.AdManagerSession)162 ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)162 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)162 ValidationException (com.google.api.ads.common.lib.exception.ValidationException)162 Credential (com.google.api.client.auth.oauth2.Credential)162 RemoteException (java.rmi.RemoteException)162 Test (org.junit.Test)129 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202205.StatementBuilder)125 Value (com.google.firestore.v1.Value)108 ArrayValue (com.google.firestore.v1.ArrayValue)73 LinkedHashSet (java.util.LinkedHashSet)71 ObjectValue (com.google.firebase.firestore.model.ObjectValue)53 NullValue (com.google.protobuf.NullValue)50 MapValue (com.google.firestore.v1.MapValue)47 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)24 IOException (java.io.IOException)22