Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.hypertrace.entity.type.service.v1.AttributeType;

public class EntityNormalizer {

private final EntityTypeClient entityTypeV2Client;
private final EntityIdGenerator idGenerator;
private final IdentifyingAttributeCache identifyingAttributeCache;
Expand All @@ -31,8 +32,8 @@ public EntityNormalizer(
* Normalizes the entity to a canonical, ready-to-upsert form
*
* @param receivedEntity
* @throws RuntimeException If entity can not be normalized
* @return
* @throws RuntimeException If entity can not be normalized
*/
Entity normalize(String tenantId, Entity receivedEntity) {
if (StringUtils.isEmpty(receivedEntity.getEntityType())) {
Expand Down Expand Up @@ -97,11 +98,7 @@ private boolean requiresIdentifyingAttributes(Entity entity) {
}

private boolean isV2Type(String entityType) {
return this.entityTypeV2Client
.get(entityType)
.map(unused -> true)
.onErrorReturnItem(false)
.blockingGet();
return this.entityTypeV2Client.get(entityType).map(Optional::isPresent).blockingGet();
}

private void verifyMatchingIdentifyingAttributes(String tenantId, Entity request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public EntityQueryServiceImpl(
entityChangeEventGenerator,
new EntityAttributeChangeEvaluator(config, entityAttributeMapping),
entityCounterMetricSender,
entityTypeChannel,
EntityTypeClient.builder(entityTypeChannel).build(),
!config.hasPathOrNull(CHUNK_SIZE_CONFIG)
? DEFAULT_CHUNK_SIZE
: config.getInt(CHUNK_SIZE_CONFIG),
Expand All @@ -173,7 +173,7 @@ public EntityQueryServiceImpl(
EntityChangeEventGenerator entityChangeEventGenerator,
EntityAttributeChangeEvaluator entityAttributeChangeEvaluator,
EntityCounterMetricSender entityCounterMetricSender,
Channel entityTypeChannel,
EntityTypeClient entityTypeClient,
int chunkSize,
int maxEntitiesToDelete,
int maxStringLengthForUpdate) {
Expand All @@ -185,7 +185,7 @@ public EntityQueryServiceImpl(
entityAttributeChangeEvaluator,
entityCounterMetricSender,
new EntityFetcher(entitiesCollection, DOCUMENT_PARSER),
entityTypeChannel,
entityTypeClient,
chunkSize,
maxEntitiesToDelete,
maxStringLengthForUpdate);
Expand All @@ -199,7 +199,7 @@ public EntityQueryServiceImpl(
EntityAttributeChangeEvaluator entityAttributeChangeEvaluator,
EntityCounterMetricSender entityCounterMetricSender,
EntityFetcher entityFetcher,
Channel entityTypeChannel,
EntityTypeClient entityTypeClient,
int chunkSize,
int maxEntitiesToDelete,
int maxStringLengthForUpdate) {
Expand All @@ -212,7 +212,6 @@ public EntityQueryServiceImpl(
this.entityFetcher = entityFetcher;
this.entityAttributeChangeEvaluator = entityAttributeChangeEvaluator;
this.entityCounterMetricSender = entityCounterMetricSender;
EntityTypeClient entityTypeClient = EntityTypeClient.builder(entityTypeChannel).build();
IdentifyingAttributeCache identifyingAttributeCache = new IdentifyingAttributeCache(datastore);
this.entityNormalizer =
new EntityNormalizer(entityTypeClient, new EntityIdGenerator(), identifyingAttributeCache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
import org.hypertrace.core.documentstore.SingleValueKey;
import org.hypertrace.entity.data.service.EntityDataServiceImpl.ErrorMessages;
Expand All @@ -26,6 +27,7 @@

@ExtendWith(MockitoExtension.class)
class EntityNormalizerTest {

private static final String TENANT_ID = "tenant";
private static final String V1_ENTITY_TYPE = "v1-entity";
private static final String V2_ENTITY_TYPE = "v2-entity";
Expand Down Expand Up @@ -58,16 +60,16 @@ void throwsOnMissingEntityType() {
void throwsOnV1EntityTypeMissingIdAttr() {
when(this.mockIdAttrCache.getIdentifyingAttributes(TENANT_ID, V1_ENTITY_TYPE))
.thenReturn(List.of(V1_ID_ATTR));
when(this.mockEntityTypeClient.get(V1_ENTITY_TYPE))
.thenReturn(Single.error(new RuntimeException()));
when(this.mockEntityTypeClient.get(V1_ENTITY_TYPE)).thenReturn(Single.just(Optional.empty()));
Entity inputEntity = Entity.newBuilder().setEntityType(V1_ENTITY_TYPE).build();

Exception exception =
assertThrows(
IllegalArgumentException.class,
() -> this.normalizer.normalize(TENANT_ID, inputEntity));
assertEquals(
"Received and expected identifying attributes differ. Received: [] . Expected: [required-attr]",
"Received and expected identifying attributes differ. Received: [] . Expected: "
+ "[required-attr]",
exception.getMessage());
}

Expand All @@ -79,8 +81,7 @@ void normalizesV1EntityTypeWithExtraIdAttr() {
.thenReturn("generated-id");
when(this.mockIdAttrCache.getIdentifyingAttributes(TENANT_ID, V1_ENTITY_TYPE))
.thenReturn(List.of(V1_ID_ATTR));
when(this.mockEntityTypeClient.get(V1_ENTITY_TYPE))
.thenReturn(Single.error(new RuntimeException()));
when(this.mockEntityTypeClient.get(V1_ENTITY_TYPE)).thenReturn(Single.just(Optional.empty()));
Entity inputEntity =
Entity.newBuilder()
.setEntityType(V1_ENTITY_TYPE)
Expand All @@ -99,7 +100,7 @@ void normalizesV1EntityTypeWithExtraIdAttr() {
@Test
void throwsOnV2EntityMissingId() {
when(this.mockEntityTypeClient.get(V2_ENTITY_TYPE))
.thenReturn(Single.just(EntityType.getDefaultInstance()));
.thenReturn(Single.just(Optional.of(EntityType.getDefaultInstance())));
Entity inputEntity = Entity.newBuilder().setEntityType(V2_ENTITY_TYPE).build();

Exception exception =
Expand All @@ -116,8 +117,7 @@ void normalizesV1EntityWithAttrs() {
.thenReturn("generated-id");
when(this.mockIdAttrCache.getIdentifyingAttributes(TENANT_ID, V1_ENTITY_TYPE))
.thenReturn(List.of(V1_ID_ATTR));
when(this.mockEntityTypeClient.get(V1_ENTITY_TYPE))
.thenReturn(Single.error(new RuntimeException()));
when(this.mockEntityTypeClient.get(V1_ENTITY_TYPE)).thenReturn(Single.just(Optional.empty()));
Entity inputEntity =
Entity.newBuilder()
.setEntityType(V1_ENTITY_TYPE)
Expand All @@ -136,7 +136,7 @@ void normalizesV1EntityWithAttrs() {
@Test
void normalizesV2EntityWithId() {
when(this.mockEntityTypeClient.get(V2_ENTITY_TYPE))
.thenReturn(Single.just(EntityType.getDefaultInstance()));
.thenReturn(Single.just(Optional.of(EntityType.getDefaultInstance())));
Entity inputEntity =
Entity.newBuilder().setEntityType(V2_ENTITY_TYPE).setEntityId("input-id").build();

Expand All @@ -147,7 +147,7 @@ void normalizesV2EntityWithId() {
@Test
void returnsV2TypeKeyForV2Entity() {
when(this.mockEntityTypeClient.get(V2_ENTITY_TYPE))
.thenReturn(Single.just(EntityType.getDefaultInstance()));
.thenReturn(Single.just(Optional.of(EntityType.getDefaultInstance())));

assertEquals(
new EntityV2TypeDocKey(TENANT_ID, V2_ENTITY_TYPE, "id-in"),
Expand All @@ -161,8 +161,7 @@ void returnsV2TypeKeyForV2Entity() {

@Test
void returnsSimpleKeyForV1Entity() {
when(this.mockEntityTypeClient.get(V1_ENTITY_TYPE))
.thenReturn(Single.error(new RuntimeException()));
when(this.mockEntityTypeClient.get(V1_ENTITY_TYPE)).thenReturn(Single.just(Optional.empty()));

// Getting a key for a v1 entity when provided with direct id
assertEquals(
Expand Down
Loading