Search in sources :

Example 1 with EntityDataServiceBlockingStub

use of org.hypertrace.entity.data.service.v1.EntityDataServiceGrpc.EntityDataServiceBlockingStub in project entity-service by hypertrace.

the class EntityDataServiceTest method testUpdateEntityWithPredicateViaUpsertAndMerge.

@Test
public void testUpdateEntityWithPredicateViaUpsertAndMerge() {
    // Scope this test to its own tenant for isolation
    String TENANT_ID = EntityDataServiceTest.TENANT_ID + "testUpdateEntityWithPredicateViaUpsertAndMerge";
    EntityDataServiceBlockingStub entityDataServiceStub = buildStubForTenant(TENANT_ID);
    setupEntityTypes(channel, TENANT_ID);
    Map<String, AttributeValue> createAttributes = Map.of("some-attr", stringValue("v1"));
    Map<String, AttributeValue> updateAttributes = Map.of("some-attr", stringValue("v2"));
    // Expect create and the first update to succeed, the second to fail
    UpsertCondition condition = UpsertCondition.newBuilder().setPropertyPredicate(Predicate.newBuilder().setAttributeKey("some-attr").setOperator(PredicateOperator.PREDICATE_OPERATOR_EQUALS).setValue(stringValue("v1"))).build();
    // V1 entity
    Entity entityToCreate = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.K8S_POD.name()).setEntityName("V1 UPDATE PREDICATE POD").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_EXTERNAL_ID), generateRandomUUIDAttrValue()).putAllAttributes(createAttributes).build();
    // Successful create
    Entity createdEntity = entityDataServiceStub.mergeAndUpsertEntity(MergeAndUpsertEntityRequest.newBuilder().setEntity(entityToCreate).setUpsertCondition(condition).build()).getEntity();
    assertEquals(stringValue("v1"), createdEntity.getAttributesMap().get("some-attr"));
    Entity entityUpdate = entityToCreate.toBuilder().putAllAttributes(updateAttributes).build();
    Entity updatedEntity = entityDataServiceStub.mergeAndUpsertEntity(MergeAndUpsertEntityRequest.newBuilder().setEntity(entityUpdate).setUpsertCondition(condition).build()).getEntity();
    // Successful update
    assertEquals(stringValue("v2"), updatedEntity.getAttributesMap().get("some-attr"));
    Entity secondEntityUpdate = entityToCreate.toBuilder().putAttributes("some-new-attr", stringValue("foo")).build();
    Entity secondUpdatedEntity = entityDataServiceStub.mergeAndUpsertEntity(MergeAndUpsertEntityRequest.newBuilder().setEntity(secondEntityUpdate).setUpsertCondition(condition).build()).getEntity();
    // No change, update rejected
    assertFalse(secondUpdatedEntity.getAttributesMap().containsKey("some-new-attr"));
    assertEquals(stringValue("v2"), secondUpdatedEntity.getAttributesMap().get("some-attr"));
    // V2 Entity
    entityToCreate = Entity.newBuilder().setTenantId(TENANT_ID).setEntityId(UUID.randomUUID().toString()).setEntityType(TEST_ENTITY_TYPE_V2).setEntityName("V2 entity predicate update").putAllAttributes(createAttributes).build();
    // Successful create
    createdEntity = entityDataServiceStub.mergeAndUpsertEntity(MergeAndUpsertEntityRequest.newBuilder().setEntity(entityToCreate).setUpsertCondition(condition).build()).getEntity();
    assertEquals(stringValue("v1"), createdEntity.getAttributesMap().get("some-attr"));
    entityUpdate = entityToCreate.toBuilder().putAllAttributes(updateAttributes).build();
    updatedEntity = entityDataServiceStub.mergeAndUpsertEntity(MergeAndUpsertEntityRequest.newBuilder().setEntity(entityUpdate).setUpsertCondition(condition).build()).getEntity();
    // Successful update
    assertEquals(stringValue("v2"), updatedEntity.getAttributesMap().get("some-attr"));
    secondEntityUpdate = entityToCreate.toBuilder().putAttributes("some-new-attr", stringValue("foo")).build();
    secondUpdatedEntity = entityDataServiceStub.mergeAndUpsertEntity(MergeAndUpsertEntityRequest.newBuilder().setEntity(secondEntityUpdate).setUpsertCondition(condition).build()).getEntity();
    // No change, update rejected
    assertFalse(secondUpdatedEntity.getAttributesMap().containsKey("some-new-attr"));
    assertEquals(stringValue("v2"), secondUpdatedEntity.getAttributesMap().get("some-attr"));
}
Also used : EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Entity(org.hypertrace.entity.data.service.v1.Entity) AttributeValue(org.hypertrace.entity.data.service.v1.AttributeValue) UpsertCondition(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityRequest.UpsertCondition) EntityDataServiceBlockingStub(org.hypertrace.entity.data.service.v1.EntityDataServiceGrpc.EntityDataServiceBlockingStub) Test(org.junit.jupiter.api.Test)

Example 2 with EntityDataServiceBlockingStub

use of org.hypertrace.entity.data.service.v1.EntityDataServiceGrpc.EntityDataServiceBlockingStub in project entity-service by hypertrace.

the class EntityDataServiceTest method testCreateEntityViaUpsertAndMerge.

@Test
public void testCreateEntityViaUpsertAndMerge() {
    // Scope this test to its own tenant for isolation
    String tenantId = EntityDataServiceTest.TENANT_ID + "_testCreateEntityViaUpsertAndMerge";
    EntityDataServiceBlockingStub entityDataServiceStub = buildStubForTenant(tenantId);
    setupEntityTypes(channel, tenantId);
    // V1 entity
    Entity entityToCreate = Entity.newBuilder().setTenantId(tenantId).setEntityType(EntityType.K8S_POD.name()).setEntityName("V1 POD").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_EXTERNAL_ID), generateRandomUUIDAttrValue()).build();
    Entity createdEntity = entityDataServiceStub.mergeAndUpsertEntity(MergeAndUpsertEntityRequest.newBuilder().setEntity(entityToCreate).build()).getEntity();
    Entity fetchedEntity = entityDataServiceStub.getById(ByIdRequest.newBuilder().setEntityId(createdEntity.getEntityId()).setEntityType(createdEntity.getEntityType()).build());
    assertEntityEquals(createdEntity, fetchedEntity);
    // V2 Entity
    entityToCreate = Entity.newBuilder().setTenantId(tenantId).setEntityId(UUID.randomUUID().toString()).setEntityType(TEST_ENTITY_TYPE_V2).setEntityName("V2 entity").build();
    createdEntity = entityDataServiceStub.mergeAndUpsertEntity(MergeAndUpsertEntityRequest.newBuilder().setEntity(entityToCreate).build()).getEntity();
    assertEntityEquals(entityToCreate, createdEntity);
    fetchedEntity = entityDataServiceStub.getById(ByIdRequest.newBuilder().setEntityId(createdEntity.getEntityId()).setEntityType(createdEntity.getEntityType()).build());
    assertEquals(createdEntity, fetchedEntity);
}
Also used : EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Entity(org.hypertrace.entity.data.service.v1.Entity) EntityDataServiceBlockingStub(org.hypertrace.entity.data.service.v1.EntityDataServiceGrpc.EntityDataServiceBlockingStub) Test(org.junit.jupiter.api.Test)

Example 3 with EntityDataServiceBlockingStub

use of org.hypertrace.entity.data.service.v1.EntityDataServiceGrpc.EntityDataServiceBlockingStub in project entity-service by hypertrace.

the class EntityDataServiceTest method testUpdateEntityViaUpsertAndMerge.

@Test
public void testUpdateEntityViaUpsertAndMerge() {
    // Scope this test to its own tenant for isolation
    String TENANT_ID = EntityDataServiceTest.TENANT_ID + "_testUpdateEntityViaUpsertAndMerge";
    EntityDataServiceBlockingStub entityDataServiceStub = buildStubForTenant(TENANT_ID);
    setupEntityTypes(channel, TENANT_ID);
    Map<String, AttributeValue> createAttributes = Map.of("some-attr", stringValue("v1"));
    Map<String, AttributeValue> updateAttributes = Map.of("some-other-attr", stringValue("v2"));
    // V1 entity
    Entity entityToCreate = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.K8S_POD.name()).setEntityName("V1 UPDATE POD").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_EXTERNAL_ID), generateRandomUUIDAttrValue()).putAllAttributes(createAttributes).build();
    entityDataServiceStub.mergeAndUpsertEntity(MergeAndUpsertEntityRequest.newBuilder().setEntity(entityToCreate).build());
    Entity entityUpdate = entityToCreate.toBuilder().clearAttributes().putAllAttributes(updateAttributes).build();
    Entity updatedEntity = entityDataServiceStub.mergeAndUpsertEntity(MergeAndUpsertEntityRequest.newBuilder().setEntity(entityUpdate).build()).getEntity();
    Map<String, AttributeValue> combinedAttributes = ImmutableMap.<String, AttributeValue>builder().putAll(createAttributes).putAll(entityToCreate.getIdentifyingAttributesMap()).putAll(updateAttributes).build();
    assertEquals(combinedAttributes, updatedEntity.getAttributesMap());
    // V2 Entity
    entityToCreate = Entity.newBuilder().setTenantId(TENANT_ID).setEntityId(UUID.randomUUID().toString()).setEntityType(TEST_ENTITY_TYPE_V2).setEntityName("V2 entity update").putAllAttributes(createAttributes).build();
    entityDataServiceStub.mergeAndUpsertEntity(MergeAndUpsertEntityRequest.newBuilder().setEntity(entityToCreate).build());
    entityUpdate = entityToCreate.toBuilder().clearAttributes().putAllAttributes(updateAttributes).build();
    updatedEntity = entityDataServiceStub.mergeAndUpsertEntity(MergeAndUpsertEntityRequest.newBuilder().setEntity(entityUpdate).build()).getEntity();
    combinedAttributes = ImmutableMap.<String, AttributeValue>builder().putAll(createAttributes).putAll(updateAttributes).build();
    assertEntityEquals(entityToCreate.toBuilder().clearAttributes().putAllAttributes(combinedAttributes).build(), updatedEntity);
}
Also used : EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Entity(org.hypertrace.entity.data.service.v1.Entity) AttributeValue(org.hypertrace.entity.data.service.v1.AttributeValue) EntityDataServiceBlockingStub(org.hypertrace.entity.data.service.v1.EntityDataServiceGrpc.EntityDataServiceBlockingStub) Test(org.junit.jupiter.api.Test)

Aggregations

EnrichedEntity (org.hypertrace.entity.data.service.v1.EnrichedEntity)3 Entity (org.hypertrace.entity.data.service.v1.Entity)3 EntityDataServiceBlockingStub (org.hypertrace.entity.data.service.v1.EntityDataServiceGrpc.EntityDataServiceBlockingStub)3 Test (org.junit.jupiter.api.Test)3 AttributeValue (org.hypertrace.entity.data.service.v1.AttributeValue)2 UpsertCondition (org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityRequest.UpsertCondition)1