// Copyright 2023 Bloomberg Finance L.P
//
// 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.

#include <buildboxcommonmetrics_metricsconfigutil.h>
#include <buildboxcommonmetrics_testingutils.h>
#include <buildboxcommonmetrics_totaldurationmetricvalue.h>
#include <env.h>
#include <executioncontext.h>

#include <string>
#include <unordered_map>

#include <gtest/gtest.h>

using namespace recc;
using namespace buildboxcommon::buildboxcommonmetrics;

TEST(MetricTest, VerifyNoAddedTag)
{
    std::string result;

    MetricsConfigType configEmptyTagsInflux("", "", false, 15, {}, "",
                                            "influx");
    result = MetricsConfigUtil::formatTags(configEmptyTagsInflux);
    assert(result == "");

    MetricsConfigType configEmptyTagsNoFormat("", "", false, 15, {}, "", "");
    result = MetricsConfigUtil::formatTags(configEmptyTagsNoFormat);
    assert(result == "");

    std::unordered_map<std::string, std::string> tags = {{"dummy", "test"}};
    MetricsConfigType configTagsNoFormat("", "", false, 15, tags, "", "");
    result = MetricsConfigUtil::formatTags(configTagsNoFormat);
    assert(result == "");

    MetricsConfigType configTagsNoneFormat("", "", false, 15, tags, "",
                                           "none");
    result = MetricsConfigUtil::formatTags(configTagsNoneFormat);
    assert(result == "");
}

TEST(MetricTest, VerifyStatsdFormats)
{
    std::unordered_map<std::string, std::string> tags = {{"dummy", "test"}};
    std::string result;

    MetricsConfigType configInflux("", "", false, 15, tags, "", "influx");
    result = MetricsConfigUtil::formatTags(configInflux);
    assert(result == ",dummy=test");

    MetricsConfigType configGraphite("", "", false, 15, tags, "", "graphite");
    result = MetricsConfigUtil::formatTags(configGraphite);
    assert(result == ";dummy=test");

    MetricsConfigType configDog("", "", false, 15, tags, "", "dog");
    result = MetricsConfigUtil::formatTags(configDog);
    assert(result == "|#dummy=test");
}

TEST(MetricTest, VerifyMultipleTags)
{
    std::unordered_map<std::string, std::string> tags = {
        {"dummy", "test"}, {"foo", "bar"}, {"saltTag", "test_salt"}};
    std::string result;

    MetricsConfigType config("", "", false, 15, tags, "", "influx");
    result = MetricsConfigUtil::formatTags(config);
    // Check that result starts with comma and contains all tags
    // (order may vary with unordered_map)
    assert(result[0] == ',');
    assert(result.find("dummy=test") != std::string::npos);
    assert(result.find("foo=bar") != std::string::npos);
    assert(result.find("saltTag=test_salt") != std::string::npos);
}
