/*
 *
 * This file is a part of the CaosDB Project.
 *
 * Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
 * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 *
 */

#include "connection.h"
#include <gtest/gtest-message.h>
#include <gtest/gtest-test-part.h>
#include <memory>
#include "gtest/gtest_pred_impl.h"

using caosdb::info::v1alpha1::VersionInfo;

TEST(test_connection, config_somehost_25323) {
  int port = 25323;
  std::string host = "somehost";
  auto config = std::make_unique<caosdb::InsecureCaosDBConnectionConfig>(host, port);

  ASSERT_EQ(host, config->getHost());
  ASSERT_EQ(port, config->getPort());
}

TEST(test_connection, connect_somehost_42347_fails) {
  int port = 42347;
  std::string host = "somehost";
  auto config = std::make_shared<caosdb::InsecureCaosDBConnectionConfig>(host, port);
  caosdb::CaosDBConnection connection(config);

  ASSERT_THROW(connection.getVersionInfo(), std::runtime_error);
}

TEST(test_connection, connection_caosdb_server_8080_success) {
  int port = 8080;
  std::string host = "localhost";
  int major = 0;
  int minor = 5;
  int patch = 0;
  std::string pre_release = "SNAPSHOT";

  auto config = std::make_shared<caosdb::InsecureCaosDBConnectionConfig>(host, port);
  auto connection = std::make_unique<caosdb::CaosDBConnection>(config);

  auto v_info = connection->getVersionInfo();
  ASSERT_EQ(major, v_info.major());
  ASSERT_EQ(minor, v_info.minor());
  ASSERT_EQ(patch, v_info.patch());
  ASSERT_EQ(pre_release, v_info.pre_release());
  ASSERT_NE("", v_info.build());
}