use of com.google.cloud.dialogflow.v2.EntityType in project entity-service by hypertrace.
the class EntityTypeDocumentTest method testProtoConversion.
@Test
public void testProtoConversion() {
EntityType entityType = EntityType.newBuilder().setName("API").setAttributeScope("API").setIdAttributeKey("id").setNameAttributeKey("name").setTimestampAttributeKey("timestamp").addRequiredConditions(EntityFormationCondition.newBuilder().setRequiredKey("other")).build();
Assertions.assertEquals(entityType, EntityTypeDocument.fromProto("testTenant", entityType).toProto());
}
use of com.google.cloud.dialogflow.v2.EntityType in project entity-service by hypertrace.
the class EntityTypeDocumentTest method testFromJsonMissingField.
@Test
public void testFromJsonMissingField() throws InvalidProtocolBufferException, JsonProcessingException {
EntityType entityType = EntityType.newBuilder().setName("API").setAttributeScope("API").setIdAttributeKey("id").setNameAttributeKey("name").build();
String entityTypeJson = JsonFormat.printer().print(entityType);
Assertions.assertEquals(entityType, EntityTypeDocument.fromJson(entityTypeJson).toProto());
}
use of com.google.cloud.dialogflow.v2.EntityType in project entity-service by hypertrace.
the class EntityTypeServiceImpl method deleteEntityTypes.
@Override
public void deleteEntityTypes(EntityTypeFilter request, StreamObserver<Empty> responseObserver) {
Optional<String> tenantId = RequestContext.CURRENT.get().getTenantId();
if (tenantId.isEmpty()) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
try {
Iterator<Document> documents = entityTypeCol.search(transform(tenantId.get(), request, false));
StreamSupport.stream(Spliterators.spliteratorUnknownSize(documents, 0), false).map(document -> {
try {
EntityType.Builder builder = EntityType.newBuilder();
PARSER.merge(document.toJson(), builder);
return builder;
} catch (InvalidProtocolBufferException e) {
LOG.warn(String.format("Error processing entityType: %s", document.toJson()), e);
return null;
}
}).filter(Objects::nonNull).forEach(entityTypeBuilder -> entityTypeCol.delete(new SingleValueKey(tenantId.get(), entityTypeBuilder.getName())));
responseObserver.onNext(Empty.newBuilder().build());
responseObserver.onCompleted();
} catch (Exception ex) {
LOG.error("Error deleting entity types matching filter:{}", request);
responseObserver.onError(ex);
}
}
use of com.google.cloud.dialogflow.v2.EntityType in project entity-service by hypertrace.
the class EntityTypeServiceImpl method upsertEntityType.
@Override
public void upsertEntityType(EntityType request, StreamObserver<EntityType> responseObserver) {
Optional<String> tenantId = RequestContext.CURRENT.get().getTenantId();
if (tenantId.isEmpty()) {
responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
return;
}
try {
// TODO: Since we currently use the same object that's received in the request while
// persisting in the doc store, we need to add tenantId explicitly here.
// We need to split these objects later.
EntityType newRequest = EntityType.newBuilder(request).setTenantId(tenantId.get()).build();
entityTypeCol.upsert(new SingleValueKey(tenantId.get(), request.getName()), new JSONDocument(PROTO_PRINTER.print(newRequest)));
responseObserver.onNext(newRequest);
responseObserver.onCompleted();
} catch (IOException ioe) {
responseObserver.onError(new RuntimeException("Error upserting EntityType:" + request, ioe));
}
}
use of com.google.cloud.dialogflow.v2.EntityType in project entity-service by hypertrace.
the class EntityTypeCachingClientTest method supportsMultipleConcurrentCacheKeys.
@Test
void supportsMultipleConcurrentCacheKeys() throws Exception {
EntityType defaultRetrieved = this.grpcTestContext.call(() -> this.typeClient.get("first").blockingGet());
assertSame(this.type1, defaultRetrieved);
verify(this.mockTypeService, times(1)).queryEntityTypes(any(), any());
RequestContext otherMockContext = mock(RequestContext.class);
when(otherMockContext.getTenantId()).thenReturn(Optional.of("other tenant"));
Context otherGrpcContext = Context.current().withValue(RequestContext.CURRENT, otherMockContext);
EntityType otherContextType = EntityType.newBuilder(this.type1).build();
this.responseTypes = List.of(otherContextType);
EntityType otherRetrieved = otherGrpcContext.call(() -> this.typeClient.get("first").blockingGet());
assertSame(otherContextType, otherRetrieved);
assertNotSame(defaultRetrieved, otherRetrieved);
verify(this.mockTypeService, times(2)).queryEntityTypes(any(), any());
verifyNoMoreInteractions(this.mockTypeService);
assertSame(defaultRetrieved, this.grpcTestContext.call(() -> this.typeClient.get("first").blockingGet()));
assertSame(otherRetrieved, otherGrpcContext.call(() -> this.typeClient.get("first").blockingGet()));
}
Aggregations