JD2022-TU1/main/extern/Hue/libhuestream/test/TestConnectionFlowExistingBridge.cpp

206 lines
6.9 KiB
C++

#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <memory>
#include <huestream/connect/ConnectionFlow.h>
#include "MockBridgeAuthenticator.h"
#include "MockBridgeSearcher.h"
#include "TestableConnectionFlow.h"
#include "MockConnectionFlowFactory.h"
#include "MessageDispatcherStub.h"
#include "TestConnectionFlowBase.h"
#include "huestream/config/HueConfig.h"
using namespace huestream;
using namespace huestream::connectionflow;
class TestConnectionFlow_ExistingBridge: public TestConnectionFlowBase {
public:
BridgePtr _existingBridge;
void SetUp() override {
TestConnectionFlowBase::SetUp();
_existingBridge = make_shared<Bridge>("001788FFFE2007DD", "192.168.1.37", true);
_existingBridge->SetModelId("BSB002");
_existingBridge->SetApiversion("1.19.0");
_existingBridge->SetUser("HSJKHSDKJHKJDHIUHHFYU&e213");
_existingBridge->SetClientKey("00000000000000000000000000000000");
_existingBridge->SetIsAuthorized(true);
}
void connect_starts_bridge_loading(bool background = false) {
expect_message(PROCEDURE_STARTED, INFO);
expect_on_storage_accesser_load();
expect_message(START_LOADING, INFO);
if (background) {
_connectionFlow->ConnectToBridgeBackground();
} else {
_connectionFlow->ConnectToBridge();
}
_messageDispatcher->ExecutePendingActions();
}
void finish_loading() {
expect_message(FINISH_LOADING_BRIDGE_CONFIGURED, INFO, _existingBridge);
expect_initiate_full_config_retrieval();
_storageAccesser->load_callback(OPERATION_SUCCESS, _existingBridge);
_messageDispatcher->ExecutePendingActions();
}
void finish_loading_execute_fullconfig_with_valid_bridge() {
finish_loading();
set_bridge_in_full_config(1);
expect_on_storage_accesser_save();
expect_message(FINISH_RETRIEVING_READY_TO_START, INFO, _existingBridge, BRIDGE_OK);
_fullConfigRetriever->RetrieveCallback(OPERATION_SUCCESS, _fullConfigRetriever->Bridge);
_messageDispatcher->ExecutePendingActions();
}
void finish_loading_execute_fullconfig_with_no_groups() {
finish_loading();
set_bridge_in_full_config(0);
expect_on_storage_accesser_save();
expect_message(FINISH_RETRIEVING_ACTION_REQUIRED, INFO, _existingBridge, BRIDGE_NO_GROUP_AVAILABLE);
expect_message(DONE_ACTION_REQUIRED, INFO, _existingBridge, BRIDGE_NO_GROUP_AVAILABLE);
expect_message(NO_GROUP_AVAILABLE, USER, _existingBridge);
_fullConfigRetriever->RetrieveCallback(OPERATION_SUCCESS, _fullConfigRetriever->Bridge);
_messageDispatcher->ExecutePendingActions();
}
void finish_loading_execute_fullconfig_not_authorized_starts_pushlink(int index) {
finish_loading();
set_bridge_not_authorized_for_full_config();
expect_message(FINISH_RETRIEVING_FAILED, INFO);
expect_message(START_AUTHORIZING, INFO);
expect_message(PRESS_PUSH_LINK, USER);
expect_on_authenticator_authenticate(index);
_fullConfigRetriever->RetrieveCallback(OPERATION_FAILED, _fullConfigRetriever->Bridge);
_messageDispatcher->ExecutePendingActions();
}
void finish_loading_execute_fullconfig_not_found_starts_bridgesearch() {
finish_loading();
set_bridge_not_found_for_full_config();
expect_message(FINISH_RETRIEVING_FAILED, INFO);
expect_message(START_SEARCHING, INFO);
expect_on_searcher_search_new(false);
_fullConfigRetriever->RetrieveCallback(OPERATION_FAILED, _fullConfigRetriever->Bridge);
_messageDispatcher->ExecutePendingActions();
}
void finish_loading_start_bridgesearch() {
expect_message(FINISH_LOADING_BRIDGE_CONFIGURED, INFO, _existingBridge);
expect_message(START_SEARCHING, INFO);
expect_on_searcher_search_new(false);
_storageAccesser->load_callback(OPERATION_SUCCESS, _existingBridge);
_messageDispatcher->ExecutePendingActions();
}
void connect_bridge_found_on_different_ip(int index) {
connect_starts_bridge_loading();
finish_loading_execute_fullconfig_not_found_starts_bridgesearch();
expect_message(FINISH_SEARCH_BRIDGES_FOUND, INFO);
expect_initiate_full_config_retrieval();
finish_search_with_three_bridges(get_last_searcher());
retrieve_full_config(index, 1);
}
void connect_bridge_is_lost() {
connect_starts_bridge_loading();
finish_loading_execute_fullconfig_not_found_starts_bridgesearch();
searching_once_with_no_bridges_found_starts_new_bridge_search();
searching_twice_with_no_bridges_found_finalizes(_existingBridge, true);
ASSERT_THAT(_existingBridge->IsAuthorized(), Eq(true));
ASSERT_THAT(_existingBridge->IsValidIp(), Eq(false));
}
};
INSTANTIATE_TEST_CASE_P(ValidBridgeInFullConfig,
TestConnectionFlow_ExistingBridge, Values(0,1));
TEST_P(TestConnectionFlow_ExistingBridge, ValidBridgeInFullConfig) {
set_autostart_based_on_param(GetParam());
connect_starts_bridge_loading();
finish_loading_execute_fullconfig_with_valid_bridge();
finish(_existingBridge);
}
TEST_F(TestConnectionFlow_ExistingBridge, NoGroupsInFullConfig) {
connect_starts_bridge_loading();
finish_loading_execute_fullconfig_with_no_groups();
}
TEST_F(TestConnectionFlow_ExistingBridge, AuthorizationIsLost) {
connect_starts_bridge_loading();
_bridges->at(0) = _existingBridge;
finish_loading_execute_fullconfig_not_authorized_starts_pushlink(0);
finish_authorization_successfully_and_retrieve_full_config(0);
}
TEST_F(TestConnectionFlow_ExistingBridge, IpAddressIsChanged) {
_existingBridge->SetId(_bridges->at(1)->GetId());
connect_bridge_found_on_different_ip(1);
}
TEST_F(TestConnectionFlow_ExistingBridge, BridgeIsLost) {
_existingBridge->SetId(_bridges->at(1)->GetId());
connect_bridge_is_lost();
}
TEST_F(TestConnectionFlow_ExistingBridge, BridgeWasPreviouslyLostAndNowBack) {
_existingBridge->SetId(_bridges->at(1)->GetId());
_existingBridge->SetIsValidIp(false);
connect_starts_bridge_loading();
finish_loading_execute_fullconfig_with_valid_bridge();
finish(_existingBridge);
}
TEST_F(TestConnectionFlow_ExistingBridge, BridgeWasPreviouslyLostAndNowOnDifferentIp) {
_existingBridge->SetId(_bridges->at(1)->GetId());
_existingBridge->SetIsValidIp(false);
connect_bridge_found_on_different_ip(1);
}
TEST_F(TestConnectionFlow_ExistingBridge, BridgeWasPreviouslyLostAndNowStill) {
_existingBridge->SetId(_bridges->at(1)->GetId());
_existingBridge->SetIsValidIp(false);
connect_bridge_is_lost();
}
TEST_F(TestConnectionFlow_ExistingBridge, BridgeIsV1) {
_existingBridge->SetId(_bridges->at(1)->GetId());
_existingBridge->SetModelId("BSB001");
ASSERT_TRUE(_existingBridge->IsConnectable());
connect_starts_bridge_loading();
finish_loading_start_bridgesearch();
searching_with_three_bridges_found_starts_pushlink_on_all_three_bridges();
finish_authorization_successfully_and_retrieve_full_config(2);
}
TEST_F(TestConnectionFlow_ExistingBridge, ConnectBackgroundValidBridgeInFullConfig) {
connect_starts_bridge_loading(true);
finish_loading_execute_fullconfig_with_valid_bridge();
finish(_existingBridge);
}