<commit_before>/*
 *   Copyright (C) 2014 Pelagicore AB
 *   All rights reserved.
 */
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "pulsegateway.h"
#include "pulsegateway_unittest_data.h"
#include "generators.h"
#include "log.h"

using namespace pelagicore;

class PulseMockController:
    public ControllerAbstractInterface
{
public:

    virtual bool startApp()
    {
            return true;
    }

    virtual bool shutdown()
    {
        return true;
    }

    virtual bool hasBeenStarted() const
    {
        return true;
    }

    virtual bool initialize()
    {
        return true;
    }

    MOCK_METHOD1(systemCall,
        bool(const std::string &cmd));

    MOCK_METHOD2(setEnvironmentVariable,
                 bool(const std::string &variable, const std::string &value));
};


class MockSystemcallInterface:
    public SystemcallAbstractInterface
{
public:
    virtual bool makeCall(const std::string &cmd, int &exitCode)
    {
        exitCode = 0;
        return true;
    }

    MOCK_METHOD1(makeCall, bool(const std::string &cmd));

    MOCK_METHOD3(makePopenCall, pid_t(const std::string &command, int *infp, int *outfp));

    MOCK_METHOD3(makePcloseCall, bool(pid_t pid, int infp, int outfp));

};

using ::testing::DefaultValue;
using ::testing::InSequence;
using ::testing::_;
using ::testing::Return;
using ::testing::NiceMock;
using ::testing::StrictMock;
using ::testing::StrEq;
using ::testing::A;

class PulseGatewayTest:
    public ::testing::Test
{
protected:
    virtual void SetUp()
    {
        DefaultValue<bool>::Set(true);

        // ContainerName and gatewayDir must be passed to constructor
        containerName = "fake-containerName";
        containerRoot = "/fake-directory/";

        containerDir = containerRoot + "/" + containerName;
        gatewayDir = containerDir + "/gateways";
    }

    virtual void TearDown()
    {
        using ::testing::DefaultValue;
        DefaultValue<bool>::Clear();
    }

    NiceMock<PulseMockController> controllerInterface;
    std::string containerName;
    std::string gatewayDir;
    std::string containerRoot;
    std::string containerDir;
};

/*! Test that id is correct ("pulseaudio")
 */
TEST_F(PulseGatewayTest, TestIdEqualspulseaudio) {
    PulseGateway gw(gatewayDir, containerName, controllerInterface);
    ASSERT_STREQ(gw.id().c_str(), "pulseaudio");
}

/*! Test well-formed configuration that enables the gateway.
 *
 *  Tests with configurations that are syntactically correct, i.e. not
 *  causing errors, and that enables audio.
 */
class PulseGatewayValidConfig:
    public testing::TestWithParam<pulseTestData> {};

INSTANTIATE_TEST_CASE_P(InstantiationName, PulseGatewayValidConfig,
                        ::testing::ValuesIn(validConfigs));

TEST_P(PulseGatewayValidConfig, TestCanParseValidConfig) {
    StrictMock<PulseMockController> controllerInterface;
    PulseGateway gw("fake-gatewayDir", "fake-containerName", controllerInterface);

    struct pulseTestData config = GetParam();

    DefaultValue<bool>::Set(true);
    EXPECT_CALL(controllerInterface, setEnvironmentVariable(
        A<const std::string &>(),
        A<const std::string &>()));

    bool success = gw.setConfig(config.data);
    DefaultValue<bool>::Clear();
    ASSERT_TRUE(success);

    success = gw.activate();
    ASSERT_TRUE(success);

    ASSERT_TRUE(gw.teardown());
}


/*! Test malformed configuration that disables the gateway.
 *
 *  Tests with configurations that are syntactically incorrect, i.e.
 *  causing parsing errors.
 */
class PulseGatewayInvalidConfig:
    public testing::TestWithParam<pulseTestData> {};

INSTANTIATE_TEST_CASE_P(InstantiationName, PulseGatewayInvalidConfig,<commit_msg>pulsegateway_unittest.cpp: Disabled test that interacts mostly with pulseaudio. This test seems to test pulseaudio more than anytning and can fail due to the environment set up.<commit_after>/*
 *   Copyright (C) 2014 Pelagicore AB
 *   All rights reserved.
 */
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "pulsegateway.h"
#include "pulsegateway_unittest_data.h"
#include "generators.h"
#include "log.h"

using namespace pelagicore;

class PulseMockController:
    public ControllerAbstractInterface
{
public:

    virtual bool startApp()
    {
            return true;
    }

    virtual bool shutdown()
    {
        return true;
    }

    virtual bool hasBeenStarted() const
    {
        return true;
    }

    virtual bool initialize()
    {
        return true;
    }

    MOCK_METHOD1(systemCall,
        bool(const std::string &cmd));

    MOCK_METHOD2(setEnvironmentVariable,
                 bool(const std::string &variable, const std::string &value));
};


class MockSystemcallInterface:
    public SystemcallAbstractInterface
{
public:
    virtual bool makeCall(const std::string &cmd, int &exitCode)
    {
        exitCode = 0;
        return true;
    }

    MOCK_METHOD1(makeCall, bool(const std::string &cmd));

    MOCK_METHOD3(makePopenCall, pid_t(const std::string &command, int *infp, int *outfp));

    MOCK_METHOD3(makePcloseCall, bool(pid_t pid, int infp, int outfp));

};

using ::testing::DefaultValue;
using ::testing::InSequence;
using ::testing::_;
using ::testing::Return;
using ::testing::NiceMock;
using ::testing::StrictMock;
using ::testing::StrEq;
using ::testing::A;

class PulseGatewayTest:
    public ::testing::Test
{
protected:
    virtual void SetUp()
    {
        DefaultValue<bool>::Set(true);

        // ContainerName and gatewayDir must be passed to constructor
        containerName = "fake-containerName";
        containerRoot = "/fake-directory/";

        containerDir = containerRoot + "/" + containerName;
        gatewayDir = containerDir + "/gateways";
    }

    virtual void TearDown()
    {
        using ::testing::DefaultValue;
        DefaultValue<bool>::Clear();
    }

    NiceMock<PulseMockController> controllerInterface;
    std::string containerName;
    std::string gatewayDir;
    std::string containerRoot;
    std::string containerDir;
};

/*! Test that id is correct ("pulseaudio")
 */
TEST_F(PulseGatewayTest, TestIdEqualspulseaudio) {
    PulseGateway gw(gatewayDir, containerName, controllerInterface);
    ASSERT_STREQ(gw.id().c_str(), "pulseaudio");
}

/*! Test well-formed configuration that enables the gateway.
 *
 *  Tests with configurations that are syntactically correct, i.e. not
 *  causing errors, and that enables audio.
 */
class PulseGatewayValidConfig:
    public testing::TestWithParam<pulseTestData> {};

INSTANTIATE_TEST_CASE_P(InstantiationName, PulseGatewayValidConfig,
                        ::testing::ValuesIn(validConfigs));

TEST_P(PulseGatewayValidConfig, DISABLED_TestCanParseValidConfig) {
    StrictMock<PulseMockController> controllerInterface;
    PulseGateway gw("fake-gatewayDir", "fake-containerName", controllerInterface);

    struct pulseTestData config = GetParam();

    DefaultValue<bool>::Set(true);
    EXPECT_CALL(controllerInterface, setEnvironmentVariable(
        A<const std::string &>(),
        A<const std::string &>()));

    bool success = gw.setConfig(config.data);
    DefaultValue<bool>::Clear();
    ASSERT_TRUE(success);

    success = gw.activate();
    ASSERT_TRUE(success);

    ASSERT_TRUE(gw.teardown());
}


/*! Test malformed configuration that disables the gateway.
 *
 *  Tests with configurations that are syntactically incorrect, i.e.
 *  causing parsing errors.
 */
class PulseGatewayInvalidConfig:
    public testing::TestWithParam<pulseTestData> {};

INSTANTIATE_TEST_CASE_P(InstantiationName, PulseGatewayInvalidConfig,
{'fraction_non_alphanumeric': 0.11130114458623866, 'fraction_numerical': 0.002631232732535193, 'mean_word_length': 3.8022741629816803, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5210489', 'n_tokens_mistral': 2288, 'n_tokens_neox': 2225, 'n_words': 524}