Search in sources :

Example 6 with GenerateSplitPointsFromSample

use of uk.gov.gchq.gaffer.operation.impl.GenerateSplitPointsFromSample in project Gaffer by gchq.

the class AbstractSampleElementsForSplitPointsHandlerTest method shouldSampleApproximatelyHalfOfElements.

@Test
public void shouldSampleApproximatelyHalfOfElements() throws OperationException {
    // Given
    final int numSplits = 3;
    final List<Element> elements = IntStream.range(0, 1000 * numSplits).mapToObj(i -> new Entity(TestGroups.ENTITY, "vertex_" + i)).collect(Collectors.toList());
    final AbstractSampleElementsForSplitPointsHandler<?, S> handler = createHandler();
    final SampleElementsForSplitPoints operation = new SampleElementsForSplitPoints.Builder<>().input(elements).numSplits(numSplits).proportionToSample(0.5f).build();
    final S store = createStore();
    // When
    handler.doOperation(operation, new Context(), store);
    // Then
    final ArgumentCaptor<GenerateSplitPointsFromSample> generateSplitPointsFromSampleCaptor = ArgumentCaptor.forClass(GenerateSplitPointsFromSample.class);
    verify(store).execute(generateSplitPointsFromSampleCaptor.capture(), any(Context.class));
    final int maximumExpectedSampleSize = (int) ((elements.size() / 2) * 1.1);
    assertExpectedNumberOfSplitPointsAndSampleSizeOfNoMoreThan(generateSplitPointsFromSampleCaptor, numSplits, maximumExpectedSampleSize);
}
Also used : IntStream(java.util.stream.IntStream) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) StringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) SchemaEntityDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) StreamSupport(java.util.stream.StreamSupport) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TestGroups(uk.gov.gchq.gaffer.commonutil.TestGroups) SampleElementsForSplitPoints(uk.gov.gchq.gaffer.operation.impl.SampleElementsForSplitPoints) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) GenerateSplitPointsFromSample(uk.gov.gchq.gaffer.operation.impl.GenerateSplitPointsFromSample) Entity(uk.gov.gchq.gaffer.data.element.Entity) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Store(uk.gov.gchq.gaffer.store.Store) List(java.util.List) Context(uk.gov.gchq.gaffer.store.Context) Schema(uk.gov.gchq.gaffer.store.schema.Schema) LimitExceededException(uk.gov.gchq.gaffer.commonutil.exception.LimitExceededException) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TestTypes(uk.gov.gchq.gaffer.store.TestTypes) OperationException(uk.gov.gchq.gaffer.operation.OperationException) Context(uk.gov.gchq.gaffer.store.Context) Entity(uk.gov.gchq.gaffer.data.element.Entity) SampleElementsForSplitPoints(uk.gov.gchq.gaffer.operation.impl.SampleElementsForSplitPoints) GenerateSplitPointsFromSample(uk.gov.gchq.gaffer.operation.impl.GenerateSplitPointsFromSample) Element(uk.gov.gchq.gaffer.data.element.Element) Test(org.junit.jupiter.api.Test)

Example 7 with GenerateSplitPointsFromSample

use of uk.gov.gchq.gaffer.operation.impl.GenerateSplitPointsFromSample in project Gaffer by gchq.

the class AbstractGenerateSplitPointsFromSampleHandlerTest method shouldThrowExceptionForNullInput.

@Test
public void shouldThrowExceptionForNullInput() throws OperationException {
    // Given
    final AbstractGenerateSplitPointsFromSampleHandler<?, S> handler = createHandler();
    final GenerateSplitPointsFromSample operation = new GenerateSplitPointsFromSample.Builder<>().numSplits(1).build();
    assertThatExceptionOfType(OperationException.class).isThrownBy(() -> handler.doOperation(operation, new Context(), createStore())).withMessageContaining("input is required");
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) GenerateSplitPointsFromSample(uk.gov.gchq.gaffer.operation.impl.GenerateSplitPointsFromSample) Test(org.junit.jupiter.api.Test)

Example 8 with GenerateSplitPointsFromSample

use of uk.gov.gchq.gaffer.operation.impl.GenerateSplitPointsFromSample in project Gaffer by gchq.

the class AbstractGenerateSplitPointsFromSampleHandlerTest method verifySplits.

protected void verifySplits(final List<Integer> indexes, final List<String> sample, final List<?> splits, final AbstractGenerateSplitPointsFromSampleHandler<?, S> handler) throws OperationException {
    final GenerateSplitPointsFromSample operatation = new GenerateSplitPointsFromSample.Builder<>().input(sample).numSplits(Integer.MAX_VALUE).build();
    final List<?> allElementsAsSplits = handler.doOperation(operatation, new Context(), createStore());
    final List<Object> expectedSplits = new ArrayList<>(indexes.size());
    for (final Integer index : indexes) {
        expectedSplits.add(allElementsAsSplits.get(index));
    }
    assertEquals(expectedSplits, splits);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) GenerateSplitPointsFromSample(uk.gov.gchq.gaffer.operation.impl.GenerateSplitPointsFromSample) ArrayList(java.util.ArrayList)

Example 9 with GenerateSplitPointsFromSample

use of uk.gov.gchq.gaffer.operation.impl.GenerateSplitPointsFromSample in project Gaffer by gchq.

the class AbstractGenerateSplitPointsFromSampleHandlerTest method shouldCalculateRequiredNumberOfSplits.

@Test
public void shouldCalculateRequiredNumberOfSplits() throws OperationException {
    // Given
    final int numSplits = 3;
    final List<String> sample = createSampleOfSize(numSplits * 10);
    final AbstractGenerateSplitPointsFromSampleHandler<?, S> handler = createHandler();
    final GenerateSplitPointsFromSample operation = new GenerateSplitPointsFromSample.Builder<>().input(sample).numSplits(numSplits).build();
    // When
    final List<?> splits = handler.doOperation(operation, new Context(), createStore());
    // Then
    verifySplits(Arrays.asList(6, 14, 21), sample, splits, handler);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) GenerateSplitPointsFromSample(uk.gov.gchq.gaffer.operation.impl.GenerateSplitPointsFromSample) Test(org.junit.jupiter.api.Test)

Example 10 with GenerateSplitPointsFromSample

use of uk.gov.gchq.gaffer.operation.impl.GenerateSplitPointsFromSample in project Gaffer by gchq.

the class AbstractGenerateSplitPointsFromSampleHandlerTest method shouldDeduplicateElements.

@Test
public void shouldDeduplicateElements() throws OperationException {
    // Given
    final int numSplits = 3;
    final List<String> sample = Collections.nCopies(numSplits * 10, "key1");
    final AbstractGenerateSplitPointsFromSampleHandler<?, S> handler = createHandler();
    final GenerateSplitPointsFromSample operation = new GenerateSplitPointsFromSample.Builder<>().input(sample).numSplits(numSplits).build();
    final S store = createStore();
    // When
    final List<?> splits = handler.doOperation(operation, new Context(), createStore());
    // Then
    assertThat(splits).hasSize(1);
    verifySplits(Collections.singletonList(0), sample, splits, handler);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) GenerateSplitPointsFromSample(uk.gov.gchq.gaffer.operation.impl.GenerateSplitPointsFromSample) Test(org.junit.jupiter.api.Test)

Aggregations

GenerateSplitPointsFromSample (uk.gov.gchq.gaffer.operation.impl.GenerateSplitPointsFromSample)14 Context (uk.gov.gchq.gaffer.store.Context)14 Test (org.junit.jupiter.api.Test)13 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 IntStream (java.util.stream.IntStream)7 ArgumentCaptor (org.mockito.ArgumentCaptor)7 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)7 Mockito.verify (org.mockito.Mockito.verify)7 TestGroups (uk.gov.gchq.gaffer.commonutil.TestGroups)7 Element (uk.gov.gchq.gaffer.data.element.Element)7 OperationException (uk.gov.gchq.gaffer.operation.OperationException)7 SampleElementsForSplitPoints (uk.gov.gchq.gaffer.operation.impl.SampleElementsForSplitPoints)7 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 BDDMockito.given (org.mockito.BDDMockito.given)4 Mockito.mock (org.mockito.Mockito.mock)4 Edge (uk.gov.gchq.gaffer.data.element.Edge)4 StoreException (uk.gov.gchq.gaffer.store.StoreException)4