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());
}
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;
}
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();
}
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))))));
}
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();
}
Aggregations