Search in sources :

Example 6 with ConsumerPatch

use of org.wikidata.query.rdf.tool.rdf.ConsumerPatch in project wikidata-query-rdf by wikimedia.

the class UpdatePatchAccumulatorUnitTest method test_add_then_remove_should_try_to_prune_removed_entity_shared_triples.

@Test
public void test_add_then_remove_should_try_to_prune_removed_entity_shared_triples() {
    PatchAccumulator accumulator = new PatchAccumulator(deserializer);
    accumulateDiff(accumulator, "UNRELATED", singletonList(stmt("uri:added-unrelated-entity")), singletonList(stmt("uri:deleted-unrelated-entity")), singletonList(stmt("uri:linked-shared")), singletonList(stmt("uri:unlinked-shared")));
    accumulateDiff(accumulator, "Q1", singletonList(stmt("uri:added-Q1")), singletonList(stmt("uri:deleted-Q1")), asList(stmt("uri:linked-shared"), stmt("uri:")), singletonList(stmt("uri:unlinked-shared")));
    accumulateDiff(accumulator, "Q1", emptyList(), emptyList(), singletonList(stmt("uri:")), emptyList());
    accumulateDelete(accumulator, "Q1");
    ConsumerPatch expected = new ConsumerPatch(singletonList(stmt("uri:added-unrelated-entity")), singletonList(stmt("uri:linked-shared")), singletonList(stmt("uri:deleted-unrelated-entity")), singletonList(stmt("uri:unlinked-shared")), singletonList("Q1"), Collections.emptyMap());
    assertThat(accumulator.asPatch()).withFailMessage("Deleting an entity should create a patch without any triples for this entity even shared ones").isEqualTo(expected);
}
Also used : ConsumerPatch(org.wikidata.query.rdf.tool.rdf.ConsumerPatch) Test(org.junit.Test)

Example 7 with ConsumerPatch

use of org.wikidata.query.rdf.tool.rdf.ConsumerPatch in project wikidata-query-rdf by wikimedia.

the class UpdatePatchAccumulatorUnitTest method test_cannot_accumulate_similar_triples_for_unrelated_entities.

@Test
public void test_cannot_accumulate_similar_triples_for_unrelated_entities() {
    PatchAccumulator accumulator = new PatchAccumulator(deserializer);
    accumulateDiff(accumulator, "Q1", singletonList(stmt("uri:added")), singletonList(stmt("uri:removed")), singletonList(stmt("uri:linked-shared")), singletonList(stmt("uri:unlinked-shared")));
    assertThatThrownBy(() -> accumulateDiff(accumulator, "Q2", singletonList(stmt("uri:added")), emptyList(), emptyList(), emptyList()), "Cannot add the same triple for a different entity (should probably be considered as a shared statement)").isInstanceOf(IllegalArgumentException.class);
    assertThatThrownBy(() -> accumulateDiff(accumulator, "Q2", emptyList(), singletonList(stmt("uri:removed")), emptyList(), emptyList()), "Cannot delete the same triple for a different entity (should probably be considered as a shared statement)").isInstanceOf(IllegalArgumentException.class);
    ConsumerPatch expectedPatch = accumulator.asPatch();
    accumulateDiff(accumulator, "Q2", emptyList(), emptyList(), singletonList(stmt("uri:linked-shared")), singletonList(stmt("uri:unlinked-shared")));
    assertThat(accumulator.asPatch()).withFailMessage("Accumulating same shared statements for different entities should result in the same patch").isEqualTo(expectedPatch);
}
Also used : ConsumerPatch(org.wikidata.query.rdf.tool.rdf.ConsumerPatch) Test(org.junit.Test)

Example 8 with ConsumerPatch

use of org.wikidata.query.rdf.tool.rdf.ConsumerPatch in project wikidata-query-rdf by wikimedia.

the class KafkaStreamConsumerUnitTest method test_prefer_reassembled_message.

@Test
public void test_prefer_reassembled_message() {
    int bufferedMessages = 250;
    TopicPartition topicPartition = new TopicPartition("test", 0);
    List<ConsumerRecord<String, MutationEventData>> allRecords = IntStream.range(0, bufferedMessages).mapToObj(i -> {
        EventsMeta meta = new EventsMeta(Instant.EPOCH, UUID.randomUUID().toString(), TEST_DOMAIN, TESTED_STREAM, "unused");
        MutationEventData diff = new DiffEventData(meta, "Q1", 1, Instant.EPOCH, i, bufferedMessages, MutationEventData.DIFF_OPERATION, new RDFDataChunk("<uri:a> <uri:a> <uri:" + i + "> .\n", RDFFormat.TURTLE.getDefaultMIMEType()), null, null, null);
        return new ConsumerRecord<String, MutationEventData>(topicPartition.topic(), topicPartition.partition(), i, null, diff);
    }).collect(toList());
    when(consumer.poll(any())).thenReturn(new ConsumerRecords<>(singletonMap(topicPartition, allRecords.subList(0, bufferedMessages / 2))), new ConsumerRecords<>(singletonMap(topicPartition, allRecords.subList(bufferedMessages / 2, allRecords.size()))), new ConsumerRecords<>(emptyMap()));
    KafkaStreamConsumer streamConsumer = new KafkaStreamConsumer(consumer, topicPartition, chunkDeser, 10, KafkaStreamConsumerMetricsListener.forRegistry(new MetricRegistry()), m -> true);
    StreamConsumer.Batch b = streamConsumer.poll(Duration.ofMillis(100));
    assertThat(b).isNotNull();
    ConsumerPatch patch = b.getPatch();
    assertThat(patch.getAdded().size()).isEqualTo(bufferedMessages);
    streamConsumer.acknowledge();
    b = streamConsumer.poll(Duration.ofMillis(100));
    assertThat(b).isNull();
}
Also used : Arrays(java.util.Arrays) DiffEventData(org.wikidata.query.rdf.updater.DiffEventData) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BiFunction(java.util.function.BiFunction) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) StatementHelper.statements(org.wikidata.query.rdf.test.StatementHelper.statements) Collections.singletonList(java.util.Collections.singletonList) RDFDataChunk(org.wikidata.query.rdf.updater.RDFDataChunk) RDFFormat(org.openrdf.rio.RDFFormat) MutationEventData(org.wikidata.query.rdf.updater.MutationEventData) Matchers.eq(org.mockito.Matchers.eq) Duration(java.time.Duration) Map(java.util.Map) ConsumerPatch(org.wikidata.query.rdf.tool.rdf.ConsumerPatch) OffsetCommitCallback(org.apache.kafka.clients.consumer.OffsetCommitCallback) RDFChunkSerializer(org.wikidata.query.rdf.updater.RDFChunkSerializer) TopicPartition(org.apache.kafka.common.TopicPartition) RDFParserSuppliers(org.wikidata.query.rdf.tool.rdf.RDFParserSuppliers) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) Instant(java.time.Instant) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Stream(java.util.stream.Stream) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) EventsMeta(org.wikidata.query.rdf.tool.change.events.EventsMeta) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) IntStream(java.util.stream.IntStream) Statement(org.openrdf.model.Statement) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) RDFParserRegistry(org.openrdf.rio.RDFParserRegistry) ArrayList(java.util.ArrayList) RDFWriterRegistry(org.openrdf.rio.RDFWriterRegistry) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) MetricRegistry(com.codahale.metrics.MetricRegistry) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) RDFChunkDeserializer(org.wikidata.query.rdf.updater.RDFChunkDeserializer) Collectors.toList(java.util.stream.Collectors.toList) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) MutationEventDataGenerator(org.wikidata.query.rdf.updater.MutationEventDataGenerator) Collections(java.util.Collections) MetricRegistry(com.codahale.metrics.MetricRegistry) DiffEventData(org.wikidata.query.rdf.updater.DiffEventData) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) MutationEventData(org.wikidata.query.rdf.updater.MutationEventData) TopicPartition(org.apache.kafka.common.TopicPartition) EventsMeta(org.wikidata.query.rdf.tool.change.events.EventsMeta) RDFDataChunk(org.wikidata.query.rdf.updater.RDFDataChunk) ConsumerPatch(org.wikidata.query.rdf.tool.rdf.ConsumerPatch) Test(org.junit.Test)

Example 9 with ConsumerPatch

use of org.wikidata.query.rdf.tool.rdf.ConsumerPatch in project wikidata-query-rdf by wikimedia.

the class UpdatePatchAccumulatorUnitTest method test_add_then_remove_same_entity_should_create_a_path_without_triples_for_this_entity.

@Test
public void test_add_then_remove_same_entity_should_create_a_path_without_triples_for_this_entity() {
    PatchAccumulator accumulator = new PatchAccumulator(deserializer);
    accumulateDiff(accumulator, "UNRELATED", singletonList(stmt("uri:added-unrelated-entity")), singletonList(stmt("uri:deleted-unrelated-entity")), singletonList(stmt("uri:linked-shared")), singletonList(stmt("uri:unlinked-shared")));
    accumulateDiff(accumulator, "Q1", singletonList(stmt("uri:added-Q1")), singletonList(stmt("uri:deleted-Q1")), singletonList(stmt("uri:linked-shared")), singletonList(stmt("uri:unlinked-shared")));
    accumulateDelete(accumulator, "Q1");
    ConsumerPatch expected = new ConsumerPatch(singletonList(stmt("uri:added-unrelated-entity")), singletonList(stmt("uri:linked-shared")), singletonList(stmt("uri:deleted-unrelated-entity")), singletonList(stmt("uri:unlinked-shared")), singletonList("Q1"), Collections.emptyMap());
    assertThat(accumulator.asPatch()).withFailMessage("Deleting an entity should create a patch without any triples for this entity").isEqualTo(expected);
}
Also used : ConsumerPatch(org.wikidata.query.rdf.tool.rdf.ConsumerPatch) Test(org.junit.Test)

Example 10 with ConsumerPatch

use of org.wikidata.query.rdf.tool.rdf.ConsumerPatch in project wikidata-query-rdf by wikimedia.

the class UpdatePatchAccumulatorUnitTest method test_duplicated_values_can_be_accumulated.

@Test
public void test_duplicated_values_can_be_accumulated() {
    PatchAccumulator accumulator = new PatchAccumulator(deserializer);
    MutationEventDataGenerator bigChunkEventGenerator = new MutationEventDataGenerator(serializer, RDFFormat.TURTLE.getDefaultMIMEType(), Integer.MAX_VALUE);
    accumulateDiff(accumulator, "Q1", asList(stmt("uri:added-1"), stmt("uri:added-1")), asList(stmt("uri:removed-1"), stmt("uri:removed-1")), asList(stmt("uri:linked-shared"), stmt("uri:linked-shared")), asList(stmt("uri:unlinked-shared"), stmt("uri:unlinked-shared")), bigChunkEventGenerator);
    ConsumerPatch actual = accumulator.asPatch();
    assertThat(actual.getAdded()).containsExactlyInAnyOrder(stmt("uri:added-1"));
    assertThat(actual.getRemoved()).contains(stmt("uri:removed-1"));
    assertThat(actual.getLinkedSharedElements()).containsExactlyInAnyOrder(stmt("uri:linked-shared"));
    assertThat(actual.getUnlinkedSharedElements()).contains(stmt("uri:unlinked-shared"));
}
Also used : MutationEventDataGenerator(org.wikidata.query.rdf.updater.MutationEventDataGenerator) ConsumerPatch(org.wikidata.query.rdf.tool.rdf.ConsumerPatch) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)11 ConsumerPatch (org.wikidata.query.rdf.tool.rdf.ConsumerPatch)11 MetricRegistry (com.codahale.metrics.MetricRegistry)4 MutationEventDataGenerator (org.wikidata.query.rdf.updater.MutationEventDataGenerator)4 Instant (java.time.Instant)3 ArrayList (java.util.ArrayList)3 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)3 Statement (org.openrdf.model.Statement)3 Duration (java.time.Duration)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 RunWith (org.junit.runner.RunWith)2 Matchers.any (org.mockito.Matchers.any)2 Mock (org.mockito.Mock)2 Mockito.times (org.mockito.Mockito.times)2 Mockito.verify (org.mockito.Mockito.verify)2