Search in sources :

Example 6 with Collection

use of org.hypertrace.core.documentstore.Collection in project document-store by hypertrace.

the class PostgresDocStoreTest method testUpsertAndReturn.

@Test
public void testUpsertAndReturn() throws IOException {
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    Document document = Utils.createDocument("foo1", "bar1");
    Document resultDocument = collection.upsertAndReturn(new SingleValueKey("default", "testKey"), document);
    Assertions.assertEquals(document.toJson(), resultDocument.toJson());
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Collection(org.hypertrace.core.documentstore.Collection) Document(org.hypertrace.core.documentstore.Document) Test(org.junit.jupiter.api.Test)

Example 7 with Collection

use of org.hypertrace.core.documentstore.Collection in project entity-service by hypertrace.

the class EntityQueryServiceImplTest method testExecute_success_chunksize_2.

@Test
public void testExecute_success_chunksize_2() throws Exception {
    Collection mockEntitiesCollection = mock(Collection.class);
    Entity entity1 = Entity.newBuilder().setTenantId("tenant-1").setEntityType(TEST_ENTITY_TYPE).setEntityId(UUID.randomUUID().toString()).setEntityName("Test entity 1").putAttributes(EDS_COLUMN_NAME1, AttributeValue.newBuilder().setValue(org.hypertrace.entity.data.service.v1.Value.newBuilder().setString("foo1")).build()).build();
    Entity entity2 = Entity.newBuilder().setTenantId("tenant-1").setEntityType(TEST_ENTITY_TYPE).setEntityId(UUID.randomUUID().toString()).setEntityName("Test entity 2").putAttributes(EDS_COLUMN_NAME1, AttributeValue.newBuilder().setValue(org.hypertrace.entity.data.service.v1.Value.newBuilder().setString("foo2")).build()).build();
    Entity entity3 = Entity.newBuilder().setTenantId("tenant-1").setEntityType(TEST_ENTITY_TYPE).setEntityId(UUID.randomUUID().toString()).setEntityName("Test entity 3").putAttributes(EDS_COLUMN_NAME1, AttributeValue.newBuilder().setValue(org.hypertrace.entity.data.service.v1.Value.newBuilder().setString("foo2")).build()).build();
    List<Document> docs = List.of(new JSONDocument(JsonFormat.printer().print(entity1)), new JSONDocument(JsonFormat.printer().print(entity2)), new JSONDocument(JsonFormat.printer().print(entity3)), // this doc will result in parsing error
    new JSONDocument("{\"entityId\": [1, 2]}"));
    when(mockEntitiesCollection.aggregate(any())).thenReturn(convertToCloseableIterator(docs.iterator()));
    when(mockEntitiesCollection.search(any())).thenReturn(convertToCloseableIterator(docs.iterator()));
    EntityQueryRequest request = EntityQueryRequest.newBuilder().setEntityType(TEST_ENTITY_TYPE).addOrderBy(OrderByExpression.newBuilder().setExpression(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID1).build()))).build();
    StreamObserver<ResultSetChunk> mockResponseObserver = mock(StreamObserver.class);
    Context.current().withValue(RequestContext.CURRENT, mockRequestContextWithTenantId()).call(() -> {
        EntityQueryServiceImpl eqs = new EntityQueryServiceImpl(mockEntitiesCollection, mockMappingForAttributes1And2(), 2, false);
        eqs.execute(request, mockResponseObserver);
        return null;
    });
    verify(mockEntitiesCollection, times(0)).aggregate(any());
    verify(mockEntitiesCollection, times(1)).search(any());
    verify(mockResponseObserver, times(2)).onNext(any());
    verify(mockResponseObserver, times(1)).onCompleted();
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) Collection(org.hypertrace.core.documentstore.Collection) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Example 8 with Collection

use of org.hypertrace.core.documentstore.Collection in project entity-service by hypertrace.

the class EntityQueryServiceImplTest method testUpdate_success.

@Test
public void testUpdate_success() throws Exception {
    Collection mockEntitiesCollection = mockEntitiesCollection();
    Builder newStatus = LiteralConstant.newBuilder().setValue(Value.newBuilder().setValueType(ValueType.STRING).setString("NEW_STATUS"));
    EntityUpdateRequest updateRequest = EntityUpdateRequest.newBuilder().setEntityType(TEST_ENTITY_TYPE).addEntityIds("entity-id-1").setOperation(UpdateOperation.newBuilder().setSetAttribute(SetAttribute.newBuilder().setAttribute(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID2)).setValue(newStatus))).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID1))).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID2))).build();
    StreamObserver<ResultSetChunk> mockResponseObserver = mock(StreamObserver.class);
    Context.current().withValue(RequestContext.CURRENT, mockRequestContextWithTenantId()).call(() -> {
        EntityQueryServiceImpl eqs = new EntityQueryServiceImpl(mockEntitiesCollection, mockMappingForAttributes1And2(), 1, false);
        eqs.update(updateRequest, mockResponseObserver);
        return null;
    });
    verify(mockEntitiesCollection, times(1)).bulkUpdateSubDocs(eq(Map.of(new SingleValueKey("tenant1", "entity-id-1"), Map.of("attributes.status", new JSONDocument(DocStoreJsonFormat.printer().print(newStatus))))));
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Builder(org.hypertrace.entity.query.service.v1.LiteralConstant.Builder) Collection(org.hypertrace.core.documentstore.Collection) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) BulkEntityUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest) EntityUpdateRequest(org.hypertrace.entity.query.service.v1.EntityUpdateRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Example 9 with Collection

use of org.hypertrace.core.documentstore.Collection in project entity-service by hypertrace.

the class EntityQueryServiceImplTest method testExecute_withAliases.

@Test
@Disabled("Disabled until we enable querying based on the new Query DTO")
public void testExecute_withAliases() throws Exception {
    Collection mockEntitiesCollection = mock(Collection.class);
    List<Document> docs = List.of(new JSONDocument("{\n" + "    \"tenantId\": \"tenant-1\",\n" + "    \"entityId\": \"" + UUID.randomUUID() + "\",\n" + "    \"entityType\": \"" + TEST_ENTITY_TYPE + "\",\n" + "    \"entityName\": \"Test entity 1\",\n" + "    \"col1\": \"col1-value\",\n" + "    \"Entity\": \n" + "    {\n" + "        \"status\":\n" + "        {\n" + "            \"value\":\n" + "            {\n" + "                \"string\": \"col2-value\"\n" + "            }\n" + "        }\n" + "    }\n" + "}"));
    when(mockEntitiesCollection.aggregate(any())).thenReturn(convertToCloseableIterator(docs.iterator()));
    when(mockEntitiesCollection.search(any())).thenReturn(convertToCloseableIterator(docs.iterator()));
    EntityQueryRequest request = EntityQueryRequest.newBuilder().setEntityType(TEST_ENTITY_TYPE).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID1).setAlias("col1"))).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID2))).build();
    StreamObserver<ResultSetChunk> mockResponseObserver = mock(StreamObserver.class);
    Context.current().withValue(RequestContext.CURRENT, mockRequestContextWithTenantId()).run(() -> {
        EntityQueryServiceImpl eqs = new EntityQueryServiceImpl(mockEntitiesCollection, mockMappingForAttributes1And2(), 100, false);
        eqs.execute(request, mockResponseObserver);
    });
    ResultSetChunk expectedResponse = ResultSetChunk.newBuilder().setIsLastChunk(true).setResultSetMetadata(ResultSetMetadata.newBuilder().addColumnMetadata(ColumnMetadata.newBuilder().setColumnName("col1")).addColumnMetadata(ColumnMetadata.newBuilder().setColumnName(ATTRIBUTE_ID2))).addRow(Row.newBuilder().addColumn(Value.newBuilder().setString("col1-value")).addColumn(Value.newBuilder().setString("col2-value")).build()).build();
    verify(mockResponseObserver, times(1)).onNext(eq(expectedResponse));
    verify(mockResponseObserver, times(1)).onCompleted();
}
Also used : Collection(org.hypertrace.core.documentstore.Collection) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 10 with Collection

use of org.hypertrace.core.documentstore.Collection in project entity-service by hypertrace.

the class EntityQueryServiceImplTest method testExecute_success.

@Test
public void testExecute_success() throws Exception {
    Collection mockEntitiesCollection = mock(Collection.class);
    Entity entity1 = Entity.newBuilder().setTenantId("tenant-1").setEntityType(TEST_ENTITY_TYPE).setEntityId(UUID.randomUUID().toString()).setEntityName("Test entity 1").putAttributes(EDS_COLUMN_NAME1, AttributeValue.newBuilder().setValue(org.hypertrace.entity.data.service.v1.Value.newBuilder().setString("foo1")).build()).build();
    Entity entity2 = Entity.newBuilder().setTenantId("tenant-1").setEntityType(TEST_ENTITY_TYPE).setEntityId(UUID.randomUUID().toString()).setEntityName("Test entity 2").putAttributes(EDS_COLUMN_NAME1, AttributeValue.newBuilder().setValue(org.hypertrace.entity.data.service.v1.Value.newBuilder().setString("foo2")).build()).build();
    List<Document> docs = List.of(new JSONDocument(JsonFormat.printer().print(entity1)), new JSONDocument(JsonFormat.printer().print(entity2)), // this doc will result in parsing error
    new JSONDocument("{\"entityId\": [1, 2]}"));
    when(mockEntitiesCollection.aggregate(any())).thenReturn(convertToCloseableIterator(docs.iterator()));
    when(mockEntitiesCollection.search(any())).thenReturn(convertToCloseableIterator(docs.iterator()));
    EntityQueryRequest request = EntityQueryRequest.newBuilder().setEntityType(TEST_ENTITY_TYPE).addOrderBy(OrderByExpression.newBuilder().setExpression(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID1).build()))).build();
    StreamObserver<ResultSetChunk> mockResponseObserver = mock(StreamObserver.class);
    Context.current().withValue(RequestContext.CURRENT, mockRequestContextWithTenantId()).call(() -> {
        EntityQueryServiceImpl eqs = new EntityQueryServiceImpl(mockEntitiesCollection, mockMappingForAttributes1And2(), 1, false);
        eqs.execute(request, mockResponseObserver);
        return null;
    });
    verify(mockEntitiesCollection, times(0)).aggregate(any());
    verify(mockEntitiesCollection, times(1)).search(any());
    verify(mockResponseObserver, times(3)).onNext(any());
    verify(mockResponseObserver, times(1)).onCompleted();
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) Collection(org.hypertrace.core.documentstore.Collection) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Aggregations

Collection (org.hypertrace.core.documentstore.Collection)20 Test (org.junit.jupiter.api.Test)19 Document (org.hypertrace.core.documentstore.Document)18 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)17 SingleValueKey (org.hypertrace.core.documentstore.SingleValueKey)15 ArrayList (java.util.ArrayList)13 MongoCollection (com.mongodb.client.MongoCollection)12 Query (org.hypertrace.core.documentstore.Query)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 HashMap (java.util.HashMap)6 Key (org.hypertrace.core.documentstore.Key)6 Map (java.util.Map)5 BulkUpdateResult (org.hypertrace.core.documentstore.BulkUpdateResult)5 Filter (org.hypertrace.core.documentstore.Filter)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ResultSetChunk (org.hypertrace.entity.query.service.v1.ResultSetChunk)4 BulkArrayValueUpdateRequest (org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest)3 EntityQueryRequest (org.hypertrace.entity.query.service.v1.EntityQueryRequest)3 BulkUpdateRequest (org.hypertrace.core.documentstore.BulkUpdateRequest)2 Entity (org.hypertrace.entity.data.service.v1.Entity)2