rapture.repo.integration.LocalTestSetup.java Source code

Java tutorial

Introduction

Here is the source code for rapture.repo.integration.LocalTestSetup.java

Source

/**
 * Copyright (C) 2011-2015 Incapture Technologies LLC
 *
 * This is an autogenerated license statement. When copyright notices appear below
 * this one that copyright supercedes this statement.
 *
 * Unless required by applicable law or agreed to in writing, software is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied.
 *
 * Unless explicit permission obtained in writing this software cannot be distributed.
 */
package rapture.repo.integration;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

import com.mongodb.MongoClientURI;

import rapture.config.MultiValueConfigLoader;

/**
 * This class creates a user for the integration test db on a local host. You should use this if you're running the integration tests locally
 *
 * @author bardhi
 * @since 5/14/15.
 */
public class LocalTestSetup {
    private static final Logger log = Logger.getLogger(LocalTestSetup.class);

    public static void main(String[] args) throws IOException, InterruptedException {
        createUser();
    }

    public static void createUser() throws IOException, InterruptedException {
        String mongoHost = MultiValueConfigLoader.getConfig("MONGODB-integrationTest");
        log.info("Host is " + mongoHost);
        if (mongoHost != null) {
            MongoClientURI uri = new MongoClientURI(mongoHost);
            List<String> hosts = uri.getHosts();
            for (String host : hosts) {
                String[] cmdarray = createSetupCommand(host, uri.getDatabase(), uri.getUsername(),
                        new String(uri.getPassword()));
                Process process = Runtime.getRuntime().exec(cmdarray);
                int retVal = process.waitFor();
                log.info(String.format("retVal=%s", retVal));
                log.info("output is " + IOUtils.toString(process.getInputStream()));
                if (retVal != 0) {
                    log.info("error is " + IOUtils.toString(process.getErrorStream()));
                }
            }
        } else {
            log.error("mongo host is not defined!");
        }
    }

    private static String[] createSetupCommand(String host, String db, String user, String password) {
        List<String> list = Arrays.asList("mongo", "--host", host, "--port", "27017", "--eval", String.format(
                "db.getSiblingDB('%s').createUser({user: '%s', pwd: '%s', roles: [ 'readWrite', 'dbAdmin' ]})", db,
                user, password));
        log.info(String.format("Will run startup command %s", list));
        return list.toArray(new String[list.size()]);
    }
}