bool
InstanceAvailabilityStatusChangedEvent::addInstance(
        const CommonAPI::Address &_address,
        const instance_id_t &_instanceId) {
    std::lock_guard<std::mutex> lock(instancesMutex_);
    if (instancesForward_.find(_instanceId) == instancesForward_.end()) {
        instancesForward_[_instanceId] = _address.getInstance();
        instancesBackward_[_address.getInstance()] = _instanceId;
        return true;
    }
    return false;
}
TEST_F(AddressTranslatorTest, CheckWellKnownNameInsertWorks) {
    std::shared_ptr<CommonAPI::DBus::DBusAddressTranslator> translator = CommonAPI::DBus::DBusAddressTranslator::get();

    std::string commonApiAddressRef = "local:my.well.insert.other.interface:my.well.insert.other.instance";
    CommonAPI::DBus::DBusAddress dbusAddressInsertRef("my.well.known.name", "/my/well/insert/instance", "my.well.insert.interface");

    CommonAPI::DBus::DBusAddress dbusAddressResult;
    CommonAPI::Address commonApiAddressResult;

    translator->insert(commonApiAddressRef,
                dbusAddressInsertRef.getService(),
                dbusAddressInsertRef.getObjectPath(),
                dbusAddressInsertRef.getInterface());

    translator->translate(CommonAPI::DBus::DBusAddress(dbusAddressInsertRef.getService(), dbusAddressInsertRef.getObjectPath(), dbusAddressInsertRef.getInterface()), commonApiAddressResult);
    std::cout << dbusAddressInsertRef.getService() << " " << dbusAddressInsertRef.getObjectPath() << " " << dbusAddressInsertRef.getInterface() << std::endl;
    std::cout << commonApiAddressResult.getDomain() << " " << commonApiAddressResult.getInterface() << " " << commonApiAddressResult.getInstance() << std::endl;
    ASSERT_EQ(commonApiAddressRef, commonApiAddressResult.getAddress());

    translator->translate(commonApiAddressRef, dbusAddressResult);
    std::cout << dbusAddressResult.getService() << " " << dbusAddressResult.getObjectPath() << " " << dbusAddressResult.getInterface() << std::endl;
    ASSERT_EQ(dbusAddressInsertRef.getService(), dbusAddressResult.getService());
    ASSERT_EQ(dbusAddressInsertRef.getObjectPath(), dbusAddressResult.getObjectPath());
    ASSERT_EQ(dbusAddressInsertRef.getInterface(), dbusAddressResult.getInterface());
}
TEST_F(AddressTranslatorTest, UniqueAddressHandlingTranslateWorks) {
    std::shared_ptr<CommonAPI::DBus::DBusAddressTranslator> translator = CommonAPI::DBus::DBusAddressTranslator::get();

    std::string commonApiAddressRef = "local:my.unique.translate.interface:my.unique.translate.instance";
    CommonAPI::DBus::DBusAddress dbusAddressInsertRef(":1.6", "/my/unique/translate/instance", "my.unique.translate.interface");

    CommonAPI::DBus::DBusAddress dbusAddressResult;
    CommonAPI::Address commonApiAddressResult;

    translator->translate(CommonAPI::DBus::DBusAddress(dbusAddressInsertRef.getService(), dbusAddressInsertRef.getObjectPath(), dbusAddressInsertRef.getInterface()), commonApiAddressResult);
    std::cout << dbusAddressInsertRef.getService() << " " << dbusAddressInsertRef.getObjectPath() << " " << dbusAddressInsertRef.getInterface() << std::endl;
    std::cout << commonApiAddressResult.getDomain() << " " << commonApiAddressResult.getInterface() << " " << commonApiAddressResult.getInstance() << std::endl;
    ASSERT_EQ(commonApiAddressRef, commonApiAddressResult.getAddress());

    translator->translate(commonApiAddressRef, dbusAddressResult);
    std::cout << dbusAddressResult.getService() << " " << dbusAddressResult.getObjectPath() << " " << dbusAddressResult.getInterface() << std::endl;
    ASSERT_EQ(dbusAddressInsertRef.getService(), dbusAddressResult.getService());
    ASSERT_EQ(dbusAddressInsertRef.getObjectPath(), dbusAddressResult.getObjectPath());
    ASSERT_EQ(dbusAddressInsertRef.getInterface(), dbusAddressResult.getInterface());
}
TEST_F(AddressTranslatorTest, InsertAddressNotPossibleConflictConfigFile) {
    std::shared_ptr<CommonAPI::DBus::DBusAddressTranslator> translator = CommonAPI::DBus::DBusAddressTranslator::get();

    CommonAPI::DBus::DBusAddress dbusAddressInsertRef("my.new.service.config_my.new.instance.config", "/my/new/instance/config", "my.new.service.config");
    std::string commonApiAddressInsertRef = "local:my.new.service.config:my.new.instance.config";

    CommonAPI::DBus::DBusAddress dbusAddressResult;
    CommonAPI::Address commonApiAddressResult;

    // try to overwrite address
    translator->insert(commonApiAddresses[1],
            dbusAddressInsertRef.getService(),
            dbusAddressInsertRef.getObjectPath(),
            dbusAddressInsertRef.getInterface());

    //check that inserting was not possible
    translator->translate(commonApiAddresses[1], dbusAddressResult);
    std::cout << dbusAddressResult.getService() << " " << dbusAddressResult.getObjectPath() << " " << dbusAddressResult.getInterface() << std::endl;
    ASSERT_EQ(dbusAddresses[1].getService(), dbusAddressResult.getService());
    ASSERT_EQ(dbusAddresses[1].getObjectPath(), dbusAddressResult.getObjectPath());
    ASSERT_EQ(dbusAddresses[1].getInterface(), dbusAddressResult.getInterface());

    translator->translate(CommonAPI::DBus::DBusAddress(dbusAddressInsertRef.getService(), dbusAddressInsertRef.getObjectPath(), dbusAddressInsertRef.getInterface()), commonApiAddressResult);
    std::cout << dbusAddressInsertRef.getService() << " " << dbusAddressInsertRef.getObjectPath() << " " << dbusAddressInsertRef.getInterface() << std::endl;
    std::cout << commonApiAddressResult.getDomain() << " " << commonApiAddressResult.getInterface() << " " << commonApiAddressResult.getInstance() << std::endl;
    ASSERT_EQ(commonApiAddressInsertRef, commonApiAddressResult.getAddress());

    translator->translate(CommonAPI::DBus::DBusAddress(dbusAddresses[1].getService(), dbusAddresses[1].getObjectPath(), dbusAddresses[1].getInterface()), commonApiAddressResult);
    std::cout << dbusAddresses[1].getService() << " " << dbusAddresses[1].getObjectPath() << " " << dbusAddresses[1].getInterface() << std::endl;
    std::cout << commonApiAddressResult.getDomain() << " " << commonApiAddressResult.getInterface() << " " << commonApiAddressResult.getInstance() << std::endl;
    ASSERT_EQ(commonApiAddresses[1], commonApiAddressResult.getAddress());
}
TEST_F(AddressTranslatorTest, InsertAddressPossible) {
    std::shared_ptr<CommonAPI::DBus::DBusAddressTranslator> translator = CommonAPI::DBus::DBusAddressTranslator::get();

    std::string commonApiAddressRef = "local:my.service:my.instance";

    CommonAPI::DBus::DBusAddress dbusAddressInsertRef("my.new.service_my.new.instance", "/my/new/instance", "my.new.service");
    CommonAPI::DBus::DBusAddress dbusAddressSecondInsertRef("my.new.second.service_my.new.second.instance", "/my/new/second/instance", "my.new.second.service");
    std::string commonApiSecondInsertAddressRef = "local:my.new.second.service:my.new.second.instance";

    CommonAPI::DBus::DBusAddress dbusAddressResult;
    CommonAPI::Address commonApiAddressResult;

    // insert new address
    translator->insert(commonApiAddressRef,
            dbusAddressInsertRef.getService(),
            dbusAddressInsertRef.getObjectPath(),
            dbusAddressInsertRef.getInterface());

    //check inserted address
    translator->translate(commonApiAddressRef, dbusAddressResult);
    std::cout << dbusAddressResult.getService() << " " << dbusAddressResult.getObjectPath() << " " << dbusAddressResult.getInterface() << std::endl;
    ASSERT_EQ(dbusAddressInsertRef.getService(), dbusAddressResult.getService());
    ASSERT_EQ(dbusAddressInsertRef.getObjectPath(), dbusAddressResult.getObjectPath());
    ASSERT_EQ(dbusAddressInsertRef.getInterface(), dbusAddressResult.getInterface());

    translator->translate(CommonAPI::DBus::DBusAddress(dbusAddressInsertRef.getService(), dbusAddressInsertRef.getObjectPath(), dbusAddressInsertRef.getInterface()), commonApiAddressResult);
    std::cout << dbusAddressInsertRef.getService() << " " << dbusAddressInsertRef.getObjectPath() << " " << dbusAddressInsertRef.getInterface() << std::endl;
    std::cout << commonApiAddressResult.getDomain() << " " << commonApiAddressResult.getInterface() << " " << commonApiAddressResult.getInstance() << std::endl;
    ASSERT_EQ(commonApiAddressRef, commonApiAddressResult.getAddress());

    // try overwriting address
    translator->insert(commonApiAddressRef,
            dbusAddressSecondInsertRef.getService(),
            dbusAddressSecondInsertRef.getObjectPath(),
            dbusAddressSecondInsertRef.getInterface());

    //check overwritten not possible
    translator->translate(commonApiAddressRef, dbusAddressResult);
    std::cout << dbusAddressResult.getService() << " " << dbusAddressResult.getObjectPath() << " " << dbusAddressResult.getInterface() << std::endl;
    ASSERT_EQ(dbusAddressInsertRef.getService(), dbusAddressResult.getService());
    ASSERT_EQ(dbusAddressInsertRef.getObjectPath(), dbusAddressResult.getObjectPath());
    ASSERT_EQ(dbusAddressInsertRef.getInterface(), dbusAddressResult.getInterface());

    translator->translate(CommonAPI::DBus::DBusAddress(dbusAddressSecondInsertRef.getService(), dbusAddressSecondInsertRef.getObjectPath(), dbusAddressSecondInsertRef.getInterface()), commonApiAddressResult);
    std::cout << dbusAddressSecondInsertRef.getService() << " " << dbusAddressSecondInsertRef.getObjectPath() << " " << dbusAddressSecondInsertRef.getInterface() << std::endl;
    std::cout << commonApiAddressResult.getDomain() << " " << commonApiAddressResult.getInterface() << " " << commonApiAddressResult.getInstance() << std::endl;
    ASSERT_EQ(commonApiSecondInsertAddressRef, commonApiAddressResult.getAddress());

    translator->translate(CommonAPI::DBus::DBusAddress(dbusAddressInsertRef.getService(), dbusAddressInsertRef.getObjectPath(), dbusAddressInsertRef.getInterface()), commonApiAddressResult);
    std::cout << dbusAddressInsertRef.getService() << " " << dbusAddressInsertRef.getObjectPath() << " " << dbusAddressInsertRef.getInterface() << std::endl;
    std::cout << commonApiAddressResult.getDomain() << " " << commonApiAddressResult.getInterface() << " " << commonApiAddressResult.getInstance() << std::endl;
    ASSERT_EQ(commonApiAddressRef, commonApiAddressResult.getAddress());
}
TEST_F(AddressTranslatorTest, ParsesCommonAPIAddresses) {
    std::shared_ptr<CommonAPI::DBus::DBusAddressTranslator> translator = CommonAPI::DBus::DBusAddressTranslator::get();

    for(unsigned int i = 0; i < commonApiAddresses.size(); i++) {
        CommonAPI::Address commonApiAddress;
        translator->translate(CommonAPI::DBus::DBusAddress(dbusAddresses[i].getService(), dbusAddresses[i].getObjectPath(), dbusAddresses[i].getInterface()), commonApiAddress);
        std::cout << dbusAddresses[i].getService() << " " << dbusAddresses[i].getObjectPath() << " " << dbusAddresses[i].getInterface() << std::endl;
        std::cout << commonApiAddress.getDomain() << " " << commonApiAddress.getInterface() << " " << commonApiAddress.getInstance() << std::endl;
        ASSERT_EQ(commonApiAddresses[i], commonApiAddress.getAddress());
    }
}