<commit_before>package com.marklogic.client.test.dataservices;

import static org.hamcrest.core.StringStartsWith.startsWith;

import java.io.File;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.util.Date;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Source;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.marklogic.client.DatabaseClient;
import com.marklogic.client.MarkLogicInternalException;
import com.marklogic.client.dataservices.CallBatcher;
import com.marklogic.client.dataservices.CallBatcher.CallArgsGenerator;
import com.marklogic.client.dataservices.CallManager;
import com.marklogic.client.dataservices.CallManager.CallArgs;
import com.marklogic.client.dataservices.CallManager.CallEvent;
import com.marklogic.client.dataservices.CallManager.OneCallEvent;
import com.marklogic.client.dataservices.impl.CallBatcherImpl;
import com.marklogic.client.document.JSONDocumentManager;
import com.marklogic.client.io.DocumentMetadataHandle;
import com.marklogic.client.io.marker.AbstractWriteHandle;
import com.marklogic.client.query.DeleteQueryDefinition;
import com.marklogic.client.query.QueryManager;
import com.marklogic.client.test.Common;

public class CallbackGeneratorTest {

    private static DatabaseClient db = Common.connect();
    private final static String ENDPOINT_DIRECTORY = "/javaApi/test/callbackGenerator/";

    private static CallManager callMgr = CallManager.on(db);
    private static EndpointUtil endpointUtil = new EndpointUtil(callMgr, ENDPOINT_DIRECTORY);
    private static CallManager.CallableEndpoint callableEndpoint;
    private static CallManager.OneCaller<Double> caller;
    
    private static CallBatcher<Void, OneCallEvent<Double>> batcher;
    
    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @BeforeClass
    public static void setup() {
    
        DatabaseClient adminClient = Common.newServerAdminClient("java-unittest-modules");
        JSONDocumentManager docMgr = adminClient.newJSONDocumentManager();

        DocumentMetadataHandle docMeta = new DocumentMetadataHandle();
        endpointUtil.setupEndpointSingleRequired(docMgr, docMeta, "singleAtomic", "double");
        adminClient.release();
        
        callableEndpoint = endpointUtil.makeCallableEndpoint("singleAtomic");
        caller = endpointUtil.makeOneCaller(callableEndpoint, Double.class);
       
    }

    @Test(expected = Test.None.class)
    public void forArgsGeneratorTest() {
        
        batcher = caller
                .batcher()
                .forArgsGenerator(result -> (result == null) ? caller.args().param("param1", 1.1) : result.getArgs())
                .onCallSuccess(event -> {
                    System.out.println(event.getItem());
                    })
                .onCallFailure((event, throwable) -> throwable.printStackTrace());

        batcher.startJob();
        batcher.flushAndWait();
        batcher.stopJob();
    }
    
    @Test
    public void forArgsGeneratorWithInputTest() {

        batcher = caller
                .batcher()
                .forArgsGenerator(result -> (result == null) ? caller.args().param("param1", 1.1) : result.getArgs());

        expectedException.expect(IllegalArgumentException.class);
        expectedException.expectMessage(startsWith("Cannot call add() when supplying arguments with generator."));
        batcher.add(null);
    }
    
    @Test
    public void callbatcherInitializeTest() {
        InitializeTest initializeTest = new InitializeTest();
        expectedException.expect(MarkLogicInternalException.class);
        expectedException.expectMessage(startsWith("Unsupported implementation of call arguments."));
        batcher = caller
                .batcher()
                .forArgsGenerator(result -> (result == null) ? initializeTest.apply(result) : result.getArgs());
        ((CallBatcherImpl) batcher).start(null);
    }
    
    private class InitializeTest implements CallArgsGenerator<CallManager.CallEvent> {

        @Override<commit_msg>A little more fine tuning.<commit_after>package com.marklogic.client.test.dataservices;

import static org.hamcrest.core.StringStartsWith.startsWith;

import java.io.File;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.util.Date;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Source;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.marklogic.client.DatabaseClient;
import com.marklogic.client.MarkLogicInternalException;
import com.marklogic.client.dataservices.CallBatcher;
import com.marklogic.client.dataservices.CallBatcher.CallArgsGenerator;
import com.marklogic.client.dataservices.CallManager;
import com.marklogic.client.dataservices.CallManager.CallArgs;
import com.marklogic.client.dataservices.CallManager.CallEvent;
import com.marklogic.client.dataservices.CallManager.OneCallEvent;
import com.marklogic.client.dataservices.impl.CallBatcherImpl;
import com.marklogic.client.document.JSONDocumentManager;
import com.marklogic.client.io.DocumentMetadataHandle;
import com.marklogic.client.io.marker.AbstractWriteHandle;
import com.marklogic.client.query.DeleteQueryDefinition;
import com.marklogic.client.query.QueryManager;
import com.marklogic.client.test.Common;

public class CallbackGeneratorTest {

    private static DatabaseClient db = Common.connect();
    private final static String ENDPOINT_DIRECTORY = "/javaApi/test/callbackGenerator/";

    private static CallManager callMgr = CallManager.on(db);
    private static EndpointUtil endpointUtil = new EndpointUtil(callMgr, ENDPOINT_DIRECTORY);
    private static CallManager.CallableEndpoint callableEndpoint;
    private static CallManager.OneCaller<Double> caller;
    
    private static CallBatcher<Void, OneCallEvent<Double>> batcher;
    
    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @BeforeClass
    public static void setup() {
    
        DatabaseClient adminClient = Common.newServerAdminClient("java-unittest-modules");
        JSONDocumentManager docMgr = adminClient.newJSONDocumentManager();

        DocumentMetadataHandle docMeta = new DocumentMetadataHandle();
        endpointUtil.setupEndpointSingleRequired(docMgr, docMeta, "singleAtomic", "double");
        adminClient.release();
        
        callableEndpoint = endpointUtil.makeCallableEndpoint("singleAtomic");
        caller = endpointUtil.makeOneCaller(callableEndpoint, Double.class);
       
    }

    @Test(expected = Test.None.class)
    public void forArgsGeneratorTest() {
        
        batcher = caller
                .batcher()
                .forArgsGenerator(result -> (result == null) ? caller.args().param("param1", 1.1) : result.getArgs())
                .onCallSuccess(event -> {
                    System.out.println(event.getItem());
                    })
                .onCallFailure((event, throwable) -> throwable.printStackTrace());

        batcher.startJob();
        batcher.flushAndWait();
        batcher.stopJob();
    }
    
    @Test
    public void forArgsGeneratorWithInputTest() {

        batcher = caller
                .batcher()
                .forArgsGenerator(result -> (result == null) ? caller.args().param("param1", 1.1) : result.getArgs());

        expectedException.expect(IllegalArgumentException.class);
        expectedException.expectMessage(startsWith("Cannot call add() when supplying arguments with generator."));
        batcher.add(null);
    }
    
    @Test
    public void callbatcherInitializeTest() {
        InitializeTest initializeTest = new InitializeTest();
        expectedException.expect(MarkLogicInternalException.class);
        expectedException.expectMessage(startsWith("Unsupported implementation of call arguments."));
        batcher = caller
                .batcher()
                .forArgsGenerator(result -> initializeTest.apply(result));
        ((CallBatcherImpl) batcher).start(null);
    }
    
    private class InitializeTest implements CallArgsGenerator<CallManager.CallEvent> {

        @Override
{'fraction_non_alphanumeric': 0.09871668311944719, 'fraction_numerical': 0.001535592848524734, 'mean_word_length': 3.8065366367949394, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4269568', 'n_tokens_mistral': 2506, 'n_tokens_neox': 2438, 'n_words': 450}