Java call mongodump

Note: the version just supports macOS and linux.
100% testing coverage, please feel free to use.

Usage

new MongoDump.Builder()
                .runtime(mockRuntime)
                .uri("mongodb://127.0.0.1:27017/gt_ut")
                .archive("dbname.archive")
                .commandPath("/usr/local/bin/mongodump")
                .build()
                .execute();

Source Code

MongoDump.java

package learningops.mongo;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author learningops
 * @date 22/03/2018
 */
public class MongoDump {
    private static final Logger LOG = LoggerFactory.getLogger(MongoDump.class);

    private String uri;
    private String archive;
    private String commandPath;
    private Runtime runtime;

    public static class Builder {

        private String uri;
        private String archive;
        private String commandPath;
        private Runtime runtime;


        public Builder archive(String archive) {
            this.archive = archive;
            return this;
        }

        public Builder runtime(Runtime runtime) {
            this.runtime = runtime;
            return this;
        }

        public Builder commandPath(String commandPath) {
            this.commandPath = commandPath;
            return this;
        }

        public Builder uri(String uri) {
            this.uri = uri;
            return this;
        }

        public MongoDump build() {
            MongoDump result = new MongoDump();
            result.uri = checkNotNull(uri, "uri was null.");
            result.commandPath = checkNotNull(commandPath, "commandPath was null.");
            result.archive = checkNotNull(archive, "archive was null.");
            Runtime rt = runtime;
            if (rt == null) {
                rt = Runtime.getRuntime();
            }
            result.runtime = rt;
            return result;
        }
    }

    public String execute() {
        try {
            String command = String.format("%s --archive=%s --uri=%s", commandPath, archive, uri);
            LOG.debug("command: {}", command);
            Process runtimeProcess = runtime.exec(new String[]{"/bin/sh", "-c", command});
            int exitValue = runtimeProcess.waitFor();
            if (exitValue != 0) {
                InputStream error = runtimeProcess.getErrorStream();
                String errorMessage = IOUtils.toString(error, "UTF-8");
                throw new MongoDumpException(errorMessage);
            }
            InputStream message = runtimeProcess.getInputStream();
            return IOUtils.toString(message, "UTF-8");
        } catch (MongoDumpException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

MongoDumpException.java

package learningops.mongo;

/**
 * @author learningops
 * @date 22/03/2018
 */
public class MongoDumpException extends RuntimeException {
    public MongoDumpException(String message) {
        super(message);
    }
}

Unit Testing

package learningops.mongo;

import org.apache.commons.io.IOUtils;
import org.testng.annotations.Test;

import java.io.InputStream;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

/**
 * @author learningops
 * @date 22/03/2018
 */
public class MongoDumpTest {

    @Test
    public void test() throws Exception {
        Runtime mockRuntime = mock(Runtime.class);
        Process mockProcess = mock(Process.class);
        when(mockProcess.getInputStream()).thenReturn(IOUtils.toInputStream("success message", "UTF-8"));
        when(mockRuntime.exec(new String[]{anyString()})).thenReturn(mockProcess);
        when(mockProcess.waitFor()).thenReturn(0);
        String result = new MongoDump.Builder()
                .runtime(mockRuntime)
                .uri("mongodb://127.0.0.1:27017/gt_ut")
                .archive("dbname.archive")
                .commandPath("/usr/local/bin/mongodump")
                .build()
                .execute();
        assertEquals(result, "success message");
    }

    @Test(expectedExceptions = {MongoDumpException.class}, expectedExceptionsMessageRegExp = "error message")
    public void unknownTermination() throws Exception {
        Runtime mockRuntime = mock(Runtime.class);
        Process mockProcess = mock(Process.class);
        when(mockRuntime.exec(new String[]{anyString()})).thenReturn(mockProcess);
        when(mockProcess.waitFor()).thenReturn(1);
        InputStream errorStream = IOUtils.toInputStream("error message", "UTF-8");
        when(mockProcess.getErrorStream()).thenReturn(errorStream);
        new MongoDump.Builder()
                .runtime(mockRuntime)
                .uri("mongodb://127.0.0.1:27017/dbname")
                .archive("dbname.archive")
                .commandPath("/usr/local/bin/mongodump")
                .build()
                .execute();
    }

    @Test(expectedExceptions = {RuntimeException.class})
    public void unknownException() throws Exception {
        Runtime mockRuntime = mock(Runtime.class);
        Process mockProcess = mock(Process.class);
        when(mockRuntime.exec(new String[]{anyString()})).thenReturn(mockProcess);
        InputStream errorStream = IOUtils.toInputStream("error message", "UTF-8");
        when(mockProcess.getErrorStream()).thenReturn(errorStream);
        new MongoDump.Builder()
                .runtime(mockRuntime)
                .uri("mongodb://127.0.0.1:27017/dbname")
                .archive("dbname.archive")
                .commandPath("/usr/local/bin/mongodump")
                .build()
                .execute();
    }

    @Test
    public void buildWithDefaultRuntime() {
        new MongoDump.Builder()
                .uri("mongodb://127.0.0.1:27017/dbname")
                .archive("dbname.archive")
                .commandPath("/usr/local/bin/mongodump")
                .build();
    }

}
    原文作者:learningops
    原文地址: https://segmentfault.com/a/1190000013924559
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞