/*
 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gbif.registry.cli.util;

import org.gbif.utils.file.FileUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

public final class RegistryCliUtils {

  private RegistryCliUtils() {}

  /** Prepare JDBC Connection. */
  public static Connection prepareConnection(
      String host, String databaseName, String user, String password) throws Exception {
    return DriverManager.getConnection(
        String.format("jdbc:postgresql://%s/%s", host, databaseName), user, password);
  }

  /** Read data from file and put it into String. */
  public static String getFileData(String filename) throws Exception {
    //noinspection ConstantConditions
    byte[] bytes =
        Files.readAllBytes(
            Paths.get(ClassLoader.getSystemClassLoader().getResource(filename).getFile()));

    return new String(bytes);
  }

  /** Load yaml config into Configuration class. */
  public static <T> T loadConfig(String configFile, Class<T> type) {
    try {
      ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
      InputStream is = FileUtils.classpathStream(configFile);
      T cfg = mapper.readValue(is, type);
      System.out.println(cfg);
      return cfg;
    } catch (IOException e) {
      throw new IllegalStateException(e);
    }
  }
}

@@ -661,16 +661,69 @@
 .util;%0A%0A
+import org.gbif.registry.cli.common.DbConfiguration;%0A
 import o
@@ -1108,16 +1108,321 @@
 s() %7B%7D%0A%0A
+  /** Prepare JDBC Connection. */%0A  public static Connection prepareConnection(DbConfiguration dbConfiguration) throws Exception %7B%0A    return prepareConnection(%0A        dbConfiguration.serverName,%0A        dbConfiguration.databaseName,%0A        dbConfiguration.user,%0A        dbConfiguration.password);%0A  %7D%0A%0A
   /** Pr

Add util test method RegistryCliUtils#prepareConnection