Search in sources :

Example 16 with DatabaseClient

use of com.marklogic.client.DatabaseClient in project components by Talend.

the class MarkLogicWriterTest method testUpsertWithAutoGenerateDocId.

@Test
public void testUpsertWithAutoGenerateDocId() throws IOException {
    MarkLogicSink sink = new MarkLogicSink();
    MarkLogicOutputProperties properties = new MarkLogicOutputProperties("outputProperties");
    properties.init();
    properties.connection.referencedComponent.componentInstanceId.setValue("Not null value");
    properties.docType.setValue(MarkLogicOutputProperties.DocType.BINARY);
    properties.action.setValue(MarkLogicOutputProperties.Action.UPSERT);
    properties.autoGenerateDocId.setValue(true);
    properties.docIdPrefix.setValue("somePrefix");
    sink.ioProperties = properties;
    RuntimeContainer mockedContainer = mock(RuntimeContainer.class);
    DatabaseClient mockedClient = mock(DatabaseClient.class);
    Mockito.when(mockedContainer.getComponentData(anyString(), anyString())).thenReturn(mockedClient);
    DocumentManager markLogicDocMngrMock = mock(DocumentManager.class);
    DocumentUriTemplate uriTemplateMock = mock(DocumentUriTemplate.class);
    DocumentDescriptor descriptorMock = mock(DocumentDescriptor.class);
    when(markLogicDocMngrMock.newDocumentUriTemplate(anyString())).thenReturn(uriTemplateMock);
    when(markLogicDocMngrMock.create(any(DocumentUriTemplate.class), any(AbstractWriteHandle.class))).thenReturn(descriptorMock);
    when(descriptorMock.getUri()).thenReturn("somePrefix/docId");
    MarkLogicWriter writer = sink.createWriteOperation().createWriter(mockedContainer);
    GenericData.Record indexedRecord = new GenericData.Record(properties.datasetProperties.main.schema.getValue());
    indexedRecord.put(0, "docId");
    File docContent = new File("someFile");
    indexedRecord.put(1, docContent);
    writer.open("123");
    writer.docMgr = markLogicDocMngrMock;
    writer.write(indexedRecord);
    verify(markLogicDocMngrMock).write(eq("somePrefix/docId"), any(FileHandle.class));
    assertFalse(((Collection<IndexedRecord>) writer.getSuccessfulWrites()).isEmpty());
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) FileHandle(com.marklogic.client.io.FileHandle) DocumentManager(com.marklogic.client.document.DocumentManager) DocumentUriTemplate(com.marklogic.client.document.DocumentUriTemplate) AbstractWriteHandle(com.marklogic.client.io.marker.AbstractWriteHandle) GenericData(org.apache.avro.generic.GenericData) DocumentDescriptor(com.marklogic.client.document.DocumentDescriptor) DatabaseClient(com.marklogic.client.DatabaseClient) MarkLogicOutputProperties(org.talend.components.marklogic.tmarklogicoutput.MarkLogicOutputProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) File(java.io.File) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) Test(org.junit.Test)

Example 17 with DatabaseClient

use of com.marklogic.client.DatabaseClient 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)

Example 18 with DatabaseClient

use of com.marklogic.client.DatabaseClient in project components by Talend.

the class MarkLogicDatastoreRuntimeTest method testDoHealthChecks.

@Test
public void testDoHealthChecks() {
    DatabaseClient client = Mockito.mock(DatabaseClient.class);
    RuntimeContainer container = Mockito.mock(RuntimeContainer.class);
    Mockito.when(container.getComponentData("reference", MarkLogicConnection.CONNECTION)).thenReturn(client);
    List<ValidationResult> healthChecks = (List<ValidationResult>) datastoreRuntime.doHealthChecks(container);
    Assert.assertEquals(1, healthChecks.size());
    Assert.assertEquals(ValidationResult.OK, healthChecks.get(0));
}
Also used : DatabaseClient(com.marklogic.client.DatabaseClient) List(java.util.List) ValidationResult(org.talend.daikon.properties.ValidationResult) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) Test(org.junit.Test)

Example 19 with DatabaseClient

use of com.marklogic.client.DatabaseClient in project components by Talend.

the class TMarkLogicCloseStandaloneTest method testCloseSuccess.

@Test
public void testCloseSuccess() {
    MarkLogicCloseProperties closeProperties = new MarkLogicCloseProperties("close");
    closeProperties.referencedComponent.componentInstanceId.setValue("referenced1");
    RuntimeContainer container = Mockito.mock(RuntimeContainer.class);
    closeStandalone.initialize(container, closeProperties);
    DatabaseClient client = Mockito.mock(DatabaseClient.class);
    Mockito.when(container.getComponentData(closeProperties.getReferencedComponentId(), MarkLogicConnection.CONNECTION)).thenReturn(client);
    closeStandalone.runAtDriver(container);
    Mockito.verify(container, Mockito.only()).getComponentData(closeProperties.getReferencedComponentId(), MarkLogicConnection.CONNECTION);
    Mockito.verify(client).release();
}
Also used : DatabaseClient(com.marklogic.client.DatabaseClient) MarkLogicCloseProperties(org.talend.components.marklogic.tmarklogicclose.MarkLogicCloseProperties) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) Test(org.junit.Test)

Example 20 with DatabaseClient

use of com.marklogic.client.DatabaseClient in project components by Talend.

the class MarkLogicInputSinkTest method testValidate.

@Test
public void testValidate() {
    MarkLogicInputProperties inputProperties = new MarkLogicInputProperties("inputProps");
    inputProperties.init();
    inputProperties.connection.referencedComponent.componentInstanceId.setValue("some value");
    RuntimeContainer mockedContainer = mock(RuntimeContainer.class);
    DatabaseClient client = mock(DatabaseClient.class);
    when(mockedContainer.getComponentData(inputProperties.connection.getReferencedComponentId(), "connection")).thenReturn(client);
    inputSink.initialize(mockedContainer, inputProperties);
    ValidationResult result = inputSink.validate(mockedContainer);
    assertEquals(ValidationResult.Result.OK, result.getStatus());
}
Also used : DatabaseClient(com.marklogic.client.DatabaseClient) ValidationResult(org.talend.daikon.properties.ValidationResult) MarkLogicInputProperties(org.talend.components.marklogic.tmarklogicinput.MarkLogicInputProperties) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) Test(org.junit.Test)

Aggregations

DatabaseClient (com.marklogic.client.DatabaseClient)21 Test (org.junit.Test)18 RuntimeContainer (org.talend.components.api.container.RuntimeContainer)18 MarkLogicInputProperties (org.talend.components.marklogic.tmarklogicinput.MarkLogicInputProperties)8 MarkLogicOutputProperties (org.talend.components.marklogic.tmarklogicoutput.MarkLogicOutputProperties)7 ValidationResult (org.talend.daikon.properties.ValidationResult)4 IndexedRecord (org.apache.avro.generic.IndexedRecord)3 MarkLogicConnectionProperties (org.talend.components.marklogic.tmarklogicconnection.MarkLogicConnectionProperties)3 DatabaseClientFactory (com.marklogic.client.DatabaseClientFactory)2 DocumentManager (com.marklogic.client.document.DocumentManager)2 SearchHandle (com.marklogic.client.io.SearchHandle)2 QueryManager (com.marklogic.client.query.QueryManager)2 StringQueryDefinition (com.marklogic.client.query.StringQueryDefinition)2 GenericData (org.apache.avro.generic.GenericData)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 SecurityContext (com.marklogic.client.DatabaseClientFactory.SecurityContext)1 ForbiddenUserException (com.marklogic.client.ForbiddenUserException)1 DocumentDescriptor (com.marklogic.client.document.DocumentDescriptor)1 DocumentUriTemplate (com.marklogic.client.document.DocumentUriTemplate)1 GenericDocumentManager (com.marklogic.client.document.GenericDocumentManager)1