Search in sources :

Example 6 with DatabaseClient

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

the class MarkLogicSourceTest 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);
    source.initialize(mockedContainer, inputProperties);
    ValidationResult result = source.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)

Example 7 with DatabaseClient

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

the class MarkLogicConnection method connect.

/**
 * Creates new {@link DatabaseClient} connection or gets referenced one from container.
 *
 * @param container
 * @return connection to MarkLogic database.
 * @throws IOException thrown if referenced connection is not connected.
 */
public DatabaseClient connect(RuntimeContainer container) {
    MarkLogicConnectionProperties properties = getMarkLogicConnectionProperties();
    if (properties.getReferencedComponentId() != null && container != null) {
        DatabaseClient client = (DatabaseClient) container.getComponentData(properties.getReferencedComponentId(), CONNECTION);
        if (client != null) {
            return client;
        }
        throw new MarkLogicException(new MarkLogicErrorCode(MESSAGES.getMessage("error.invalid.referenceConnection", properties.getReferencedComponentId())));
    }
    SecurityContext context = "BASIC".equals(properties.authentication.getValue()) ? new DatabaseClientFactory.BasicAuthContext(properties.username.getValue(), properties.password.getValue()) : new DatabaseClientFactory.DigestAuthContext(properties.username.getValue(), properties.password.getValue());
    DatabaseClient client = DatabaseClientFactory.newClient(properties.host.getValue(), properties.port.getValue(), properties.database.getValue(), context);
    testConnection(client);
    LOGGER.info("Connected to MarkLogic server");
    if (container != null) {
        container.setComponentData(container.getCurrentComponentId(), CONNECTION, client);
        LOGGER.info("Connection stored in container");
    }
    return client;
}
Also used : MarkLogicException(org.talend.components.marklogic.exceptions.MarkLogicException) DatabaseClient(com.marklogic.client.DatabaseClient) DatabaseClientFactory(com.marklogic.client.DatabaseClientFactory) MarkLogicConnectionProperties(org.talend.components.marklogic.tmarklogicconnection.MarkLogicConnectionProperties) MarkLogicErrorCode(org.talend.components.marklogic.exceptions.MarkLogicErrorCode) SecurityContext(com.marklogic.client.DatabaseClientFactory.SecurityContext)

Example 8 with DatabaseClient

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

the class MarkLogicDataSourceTest method testValidate.

@Test
public void testValidate() {
    datasource.getMarkLogicConnectionProperties().referencedComponent.componentInstanceId.setValue("reference");
    DatabaseClient client = Mockito.mock(DatabaseClient.class);
    RuntimeContainer container = Mockito.mock(RuntimeContainer.class);
    Mockito.when(container.getComponentData("reference", MarkLogicConnection.CONNECTION)).thenReturn(client);
    Assert.assertEquals(Result.OK, datasource.validate(container).getStatus());
    Mockito.verify(container, Mockito.only()).getComponentData("reference", MarkLogicConnection.CONNECTION);
}
Also used : DatabaseClient(com.marklogic.client.DatabaseClient) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) Test(org.junit.Test)

Example 9 with DatabaseClient

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

the class MarkLogicRowProcessorTest method testCloseReferencedConnectionNotReleased.

@Test
public void testCloseReferencedConnectionNotReleased() throws IOException {
    DatabaseClient mockedClient = mock(DatabaseClient.class);
    MarkLogicInputSink mockedSink = mock(MarkLogicInputSink.class);
    MarkLogicInputProperties inputProperties = new MarkLogicInputProperties("inputProps");
    inputProperties.init();
    inputProperties.connection.referencedComponent.componentInstanceId.setValue(MarkLogicConnectionDefinition.COMPONENT_NAME + "_1");
    MarkLogicInputWriteOperation writeOperation = new MarkLogicInputWriteOperation(mockedSink, inputProperties);
    MarkLogicRowProcessor rowProcessor = writeOperation.createWriter(null);
    when(mockedSink.connect(any(RuntimeContainer.class))).thenReturn(mockedClient);
    rowProcessor.open("1");
    verify(mockedClient).newDocumentManager();
    rowProcessor.close();
    verifyNoMoreInteractions(mockedClient);
}
Also used : DatabaseClient(com.marklogic.client.DatabaseClient) MarkLogicInputProperties(org.talend.components.marklogic.tmarklogicinput.MarkLogicInputProperties) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) Test(org.junit.Test)

Example 10 with DatabaseClient

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

the class MarkLogicRowProcessorTest method testClose.

@Test
public void testClose() throws IOException {
    DatabaseClient mockedClient = mock(DatabaseClient.class);
    MarkLogicInputSink mockedSink = mock(MarkLogicInputSink.class);
    MarkLogicInputProperties inputProperties = new MarkLogicInputProperties("inputProps");
    inputProperties.init();
    MarkLogicInputWriteOperation writeOperation = new MarkLogicInputWriteOperation(mockedSink, inputProperties);
    MarkLogicRowProcessor rowProcessor = writeOperation.createWriter(null);
    when(mockedSink.connect(any(RuntimeContainer.class))).thenReturn(mockedClient);
    String uId = "123";
    rowProcessor.open(uId);
    Result result = rowProcessor.close();
    assertNotNull(result);
    assertEquals(uId, result.getuId());
    verify(mockedClient).release();
}
Also used : DatabaseClient(com.marklogic.client.DatabaseClient) MarkLogicInputProperties(org.talend.components.marklogic.tmarklogicinput.MarkLogicInputProperties) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) Result(org.talend.components.api.component.runtime.Result) 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