Search in sources :

Example 1 with JSONDocument

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

the class MongoDocStoreTest method whenBulkUpdatingNonExistentRecords_thenExpectNothingToBeUpdatedOrCreated.

@Test
public void whenBulkUpdatingNonExistentRecords_thenExpectNothingToBeUpdatedOrCreated() throws Exception {
    Collection collection = datastore.getCollection(COLLECTION_NAME);
    ObjectNode objectNode = OBJECT_MAPPER.createObjectNode();
    objectNode.put("foo1", "bar1");
    objectNode.put("timestamp", 100);
    List<BulkUpdateRequest> toUpdate = new ArrayList<>();
    toUpdate.add(new BulkUpdateRequest(new SingleValueKey("tenant-1", "testKey1"), new JSONDocument(objectNode), new Filter(Op.LT, "timestamp", 100)));
    toUpdate.add(new BulkUpdateRequest(new SingleValueKey("tenant-1", "testKey2"), new JSONDocument(objectNode), new Filter(Op.LT, "timestamp", 100)));
    BulkUpdateResult result = collection.bulkUpdate(toUpdate);
    Assertions.assertEquals(0, result.getUpdatedCount());
    Query query = new Query();
    query.setFilter(new Filter(Op.EQ, "_id", new SingleValueKey("tenant-1", "testKey1").toString()));
    Iterator<Document> it = collection.search(query);
    assertFalse(it.hasNext());
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Query(org.hypertrace.core.documentstore.Query) Filter(org.hypertrace.core.documentstore.Filter) ArrayList(java.util.ArrayList) BulkUpdateResult(org.hypertrace.core.documentstore.BulkUpdateResult) MongoCollection(com.mongodb.client.MongoCollection) Collection(org.hypertrace.core.documentstore.Collection) BulkUpdateRequest(org.hypertrace.core.documentstore.BulkUpdateRequest) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Test(org.junit.jupiter.api.Test)

Example 2 with JSONDocument

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

the class Utils method createDocumentsFromResource.

public static Map<Key, Document> createDocumentsFromResource(String resourcePath) throws IOException {
    Optional<String> contentOptional = readFileFromResource(resourcePath);
    String json = contentOptional.orElseThrow();
    List<Map<String, Object>> maps = OBJECT_MAPPER.readValue(json, new TypeReference<>() {
    });
    Map<Key, Document> documentMap = new HashMap<>();
    for (Map<String, Object> map : maps) {
        Key key = new SingleValueKey("default", map.get(MongoCollection.ID_KEY).toString());
        Document value = new JSONDocument(map);
        documentMap.put(key, value);
    }
    return documentMap;
}
Also used : HashMap(java.util.HashMap) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) HashMap(java.util.HashMap) Map(java.util.Map) Key(org.hypertrace.core.documentstore.Key) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey)

Example 3 with JSONDocument

use of org.hypertrace.core.documentstore.JSONDocument 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 4 with JSONDocument

use of org.hypertrace.core.documentstore.JSONDocument 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 5 with JSONDocument

use of org.hypertrace.core.documentstore.JSONDocument 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)

Aggregations

JSONDocument (org.hypertrace.core.documentstore.JSONDocument)17 Document (org.hypertrace.core.documentstore.Document)10 Test (org.junit.jupiter.api.Test)10 SingleValueKey (org.hypertrace.core.documentstore.SingleValueKey)9 Collection (org.hypertrace.core.documentstore.Collection)8 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 MongoCollection (com.mongodb.client.MongoCollection)4 Query (org.hypertrace.core.documentstore.Query)4 ResultSetChunk (org.hypertrace.entity.query.service.v1.ResultSetChunk)4 ServiceException (com.google.protobuf.ServiceException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Entity (org.hypertrace.entity.data.service.v1.Entity)3 EntityQueryRequest (org.hypertrace.entity.query.service.v1.EntityQueryRequest)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BulkUpdateRequest (org.hypertrace.core.documentstore.BulkUpdateRequest)2 BulkUpdateResult (org.hypertrace.core.documentstore.BulkUpdateResult)2 Filter (org.hypertrace.core.documentstore.Filter)2