<commit_before>/*
 * Copyright 2019 Centreon (https://www.centreon.com/)
 *
 * 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.
 *
 * For more information : <EMAIL>
 *
 */

#include "com/centreon/broker/brokerrpc.hh"

#include <gtest/gtest.h>

#include <cstdio>
#include <fstream>
#include <json11.hpp>

#include "com/centreon/broker/log_v2.hh"
#include "com/centreon/broker/version.hh"

using namespace com::centreon;
using namespace com::centreon::broker;

class BrokerRpc : public ::testing::Test {
 public:
  void SetUp() override {}

  void TearDown() override {}

  std::list<std::string> execute(const std::string& command) {
    std::list<std::string> retval;
    char path[1024];
    std::ostringstream oss;
    oss << "test/rpc_client " << command;

    FILE* fp = popen(oss.str().c_str(), "r");
    while (fgets(path, sizeof(path), fp) != nullptr) {
      size_t count = strlen(path);
      if (count > 0)
        --count;
      retval.push_back(std::string(path, count));
    }
    pclose(fp);
    return retval;
  }
};

TEST_F(BrokerRpc, StartStop) {
  brokerrpc brpc("0.0.0.0", 40000, "test");
  ASSERT_NO_THROW(brpc.shutdown());
}

TEST_F(BrokerRpc, GetVersion) {
  std::ostringstream oss;
  oss << "GetVersion: major: " << version::major;
  brokerrpc brpc("0.0.0.0", 40000, "test");
  auto output = execute("GetVersion");
  ASSERT_EQ(output.size(), 3);
  ASSERT_EQ(output.front(), oss.str());
  oss.str("");
  oss << "patch: " << version::patch;
  ASSERT_EQ(output.back(), oss.str());
  brpc.shutdown();
}<commit_msg>fix(test): fix BrokerRpc.GetVersion unit test.<commit_after>/*
 * Copyright 2019 Centreon (https://www.centreon.com/)
 *
 * 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.
 *
 * For more information : <EMAIL>
 *
 */

#include "com/centreon/broker/brokerrpc.hh"

#include <gtest/gtest.h>

#include <cstdio>
#include <fstream>
#include <json11.hpp>

#include "com/centreon/broker/log_v2.hh"
#include "com/centreon/broker/version.hh"

using namespace com::centreon;
using namespace com::centreon::broker;

class BrokerRpc : public ::testing::Test {
 public:
  void SetUp() override {}

  void TearDown() override {}

  std::list<std::string> execute(const std::string& command) {
    std::list<std::string> retval;
    char path[1024];
    std::ostringstream oss;
    oss << "test/rpc_client " << command;

    FILE* fp = popen(oss.str().c_str(), "r");
    while (fgets(path, sizeof(path), fp) != nullptr) {
      size_t count = strlen(path);
      if (count > 0)
        --count;
      retval.push_back(std::string(path, count));
    }
    pclose(fp);
    return retval;
  }
};

TEST_F(BrokerRpc, StartStop) {
  brokerrpc brpc("0.0.0.0", 40000, "test");
  ASSERT_NO_THROW(brpc.shutdown());
}

TEST_F(BrokerRpc, GetVersion) {
  std::ostringstream oss;
  oss << "GetVersion: major: " << version::major;
  brokerrpc brpc("0.0.0.0", 40000, "test");
  auto output = execute("GetVersion");
#if CENTREON_BROKER_PATCH == 0
  ASSERT_EQ(output.size(), 2);
  ASSERT_EQ(output.front(), oss.str());
  oss.str("");
  oss << "minor: " << version::minor;
  ASSERT_EQ(output.back(), oss.str());
#else
  ASSERT_EQ(output.size(), 3);
  ASSERT_EQ(output.front(), oss.str());
  oss.str("");
  oss << "patch: " << version::patch;
  ASSERT_EQ(output.back(), oss.str());
#endif
  brpc.shutdown();
}
{'fraction_non_alphanumeric': 0.1559014869888476, 'fraction_numerical': 0.016728624535315983, 'mean_word_length': 4.040983606557377, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1431006', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1452, 'n_words': 389}