Search in sources :

Example 1 with StringQueryDefinition

use of com.marklogic.client.query.StringQueryDefinition in project components by Talend.

the class MarkLogicCriteriaReaderTest method testStartWithoutDocContentField.

@Test
public void testStartWithoutDocContentField() throws IOException {
    StringQueryDefinition mockedStringQueryDefinition = mock(StringQueryDefinition.class);
    QueryManager mockedQueryManager = mock(QueryManager.class);
    when(mockedQueryManager.newStringDefinition()).thenReturn(mockedStringQueryDefinition);
    when(mockedQueryManager.newStringDefinition(anyString())).thenReturn(mockedStringQueryDefinition);
    DatabaseClient mockedClient = mock(DatabaseClient.class);
    when(mockedClient.newDocumentManager()).thenReturn(null);
    when(mockedClient.newQueryManager()).thenReturn(mockedQueryManager);
    SearchHandle searchHandle = Mockito.mock(SearchHandle.class);
    Mockito.when(mockedQueryManager.search(Mockito.eq(mockedStringQueryDefinition), Mockito.any(SearchHandle.class))).thenReturn(searchHandle);
    MarkLogicSource mockedSource = mock(MarkLogicSource.class);
    when(mockedSource.connect(any(RuntimeContainer.class))).thenReturn(mockedClient);
    MarkLogicInputProperties properties = new MarkLogicInputProperties("inputProperties");
    properties.init();
    Schema.Field docIdField = new Schema.Field("docId", AvroUtils._string(), null, (Object) null, Schema.Field.Order.IGNORE);
    properties.datasetProperties.main.schema.setValue(Schema.createRecord("docIdOnlySchema", null, null, false, Collections.singletonList(docIdField)));
    MarkLogicCriteriaReader criteriaReader = new MarkLogicCriteriaReader(mockedSource, null, properties);
    System.out.println(criteriaReader.start());
}
Also used : StringQueryDefinition(com.marklogic.client.query.StringQueryDefinition) DatabaseClient(com.marklogic.client.DatabaseClient) SearchHandle(com.marklogic.client.io.SearchHandle) Schema(org.apache.avro.Schema) QueryManager(com.marklogic.client.query.QueryManager) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) MarkLogicInputProperties(org.talend.components.marklogic.tmarklogicinput.MarkLogicInputProperties) Test(org.junit.Test)

Example 2 with StringQueryDefinition

use of com.marklogic.client.query.StringQueryDefinition in project components by Talend.

the class MarkLogicDatasetRuntimeTest method testWorkflow.

@SuppressWarnings("unchecked")
@Test
public void testWorkflow() {
    RuntimeContainer container = Mockito.mock(RuntimeContainer.class);
    MarkLogicConnectionProperties connection = new MarkLogicConnectionProperties("datastore");
    connection.referencedComponent.componentInstanceId.setValue("reference");
    DatabaseClient connectionClient = Mockito.mock(DatabaseClient.class);
    GenericDocumentManager docManager = Mockito.mock(GenericDocumentManager.class);
    Mockito.when(connectionClient.newDocumentManager()).thenReturn(docManager);
    QueryManager queryManager = Mockito.mock(QueryManager.class);
    Mockito.when(connectionClient.newQueryManager()).thenReturn(queryManager);
    StringQueryDefinition stringQueryDefinition = Mockito.mock(StringQueryDefinition.class);
    Mockito.when(queryManager.newStringDefinition()).thenReturn(stringQueryDefinition);
    SearchHandle searchHandle = Mockito.mock(SearchHandle.class);
    Mockito.when(queryManager.search(Mockito.eq(stringQueryDefinition), Mockito.any(SearchHandle.class))).thenReturn(searchHandle);
    MatchDocumentSummary[] results = new MatchDocumentSummary[2];
    MatchDocumentSummary firstRecord = Mockito.mock(MatchDocumentSummary.class);
    MatchDocumentSummary secondRecord = Mockito.mock(MatchDocumentSummary.class);
    results[0] = firstRecord;
    results[1] = secondRecord;
    Mockito.when(container.getComponentData("reference", MarkLogicConnection.CONNECTION)).thenReturn(connectionClient);
    MarkLogicDatasetProperties properties = new MarkLogicDatasetProperties("dataset");
    connection.init();
    properties.setDatastoreProperties(connection);
    properties.init();
    datasetRuntime.initialize(container, properties);
    Assert.assertEquals(2, datasetRuntime.getSchema().getFields().size());
    Assert.assertEquals("docId", datasetRuntime.getSchema().getFields().get(0).name());
    Assert.assertEquals("docContent", datasetRuntime.getSchema().getFields().get(1).name());
    Mockito.when(searchHandle.getMatchResults()).thenReturn(results);
    Mockito.when(searchHandle.getTotalResults()).thenReturn(2l);
    Mockito.when(docManager.readAs(Mockito.anyString(), Mockito.any(Class.class))).thenReturn(new Object());
    Consumer<IndexedRecord> consumer = Mockito.mock(Consumer.class);
    datasetRuntime.getSample(2, consumer);
    Mockito.verify(consumer, Mockito.times(2)).accept(Mockito.any(IndexedRecord.class));
}
Also used : GenericDocumentManager(com.marklogic.client.document.GenericDocumentManager) IndexedRecord(org.apache.avro.generic.IndexedRecord) MatchDocumentSummary(com.marklogic.client.query.MatchDocumentSummary) StringQueryDefinition(com.marklogic.client.query.StringQueryDefinition) DatabaseClient(com.marklogic.client.DatabaseClient) SearchHandle(com.marklogic.client.io.SearchHandle) MarkLogicConnectionProperties(org.talend.components.marklogic.tmarklogicconnection.MarkLogicConnectionProperties) QueryManager(com.marklogic.client.query.QueryManager) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) Test(org.junit.Test)

Aggregations

DatabaseClient (com.marklogic.client.DatabaseClient)2 SearchHandle (com.marklogic.client.io.SearchHandle)2 QueryManager (com.marklogic.client.query.QueryManager)2 StringQueryDefinition (com.marklogic.client.query.StringQueryDefinition)2 Test (org.junit.Test)2 RuntimeContainer (org.talend.components.api.container.RuntimeContainer)2 GenericDocumentManager (com.marklogic.client.document.GenericDocumentManager)1 MatchDocumentSummary (com.marklogic.client.query.MatchDocumentSummary)1 Schema (org.apache.avro.Schema)1 IndexedRecord (org.apache.avro.generic.IndexedRecord)1 MarkLogicConnectionProperties (org.talend.components.marklogic.tmarklogicconnection.MarkLogicConnectionProperties)1 MarkLogicInputProperties (org.talend.components.marklogic.tmarklogicinput.MarkLogicInputProperties)1