Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "buffet/base_api_handler.h" |
| 6 | |
| 7 | #include <base/strings/string_number_conversions.h> |
| 8 | #include <base/values.h> |
| 9 | #include <chromeos/http/http_transport_fake.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | #include "buffet/buffet_config.h" |
| 13 | #include "buffet/commands/command_manager.h" |
| 14 | #include "buffet/commands/unittest_utils.h" |
| 15 | #include "buffet/device_registration_info.h" |
| 16 | #include "buffet/states/mock_state_change_queue_interface.h" |
| 17 | #include "buffet/states/state_manager.h" |
| 18 | #include "buffet/storage_impls.h" |
| 19 | |
| 20 | namespace buffet { |
| 21 | |
| 22 | class BaseApiHandlerTest : public ::testing::Test { |
| 23 | protected: |
| 24 | void SetUp() override { |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 25 | transport_ = std::make_shared<chromeos::http::fake::Transport>(); |
| 26 | command_manager_ = std::make_shared<CommandManager>(); |
| 27 | state_manager_ = std::make_shared<StateManager>(&mock_state_change_queue_); |
| 28 | state_manager_->Startup(); |
| 29 | dev_reg_.reset(new DeviceRegistrationInfo( |
| 30 | command_manager_, state_manager_, |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 31 | std::unique_ptr<BuffetConfig>{new BuffetConfig{ |
| 32 | std::unique_ptr<StorageInterface>{new MemStorage}}}, |
| 33 | transport_, true)); |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 34 | handler_.reset(new BaseApiHandler{dev_reg_->AsWeakPtr(), command_manager_}); |
| 35 | } |
| 36 | |
| 37 | void LoadCommands(const std::string& command_definitions) { |
| 38 | auto json = unittests::CreateDictionaryValue(command_definitions.c_str()); |
| 39 | EXPECT_TRUE(command_manager_->LoadBaseCommands(*json, nullptr)); |
| 40 | EXPECT_TRUE(command_manager_->LoadCommands(*json, "", nullptr)); |
| 41 | } |
| 42 | |
| 43 | void AddCommand(const std::string& command) { |
| 44 | auto command_instance = buffet::CommandInstance::FromJson( |
| 45 | unittests::CreateDictionaryValue(command.c_str()).get(), |
| 46 | commands::attributes::kCommand_Visibility_Local, |
| 47 | command_manager_->GetCommandDictionary(), nullptr, nullptr); |
| 48 | EXPECT_TRUE(!!command_instance); |
| 49 | |
| 50 | std::string id{base::IntToString(++command_id_)}; |
| 51 | command_instance->SetID(id); |
| 52 | command_manager_->AddCommand(std::move(command_instance)); |
| 53 | EXPECT_EQ(CommandInstance::kStatusDone, |
| 54 | command_manager_->FindCommand(id)->GetStatus()); |
| 55 | } |
| 56 | |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 57 | std::shared_ptr<chromeos::http::fake::Transport> transport_; |
| 58 | std::unique_ptr<DeviceRegistrationInfo> dev_reg_; |
| 59 | std::shared_ptr<CommandManager> command_manager_; |
| 60 | testing::NiceMock<MockStateChangeQueueInterface> mock_state_change_queue_; |
| 61 | std::shared_ptr<StateManager> state_manager_; |
| 62 | std::unique_ptr<BaseApiHandler> handler_; |
| 63 | int command_id_{0}; |
| 64 | }; |
| 65 | |
| 66 | TEST_F(BaseApiHandlerTest, UpdateDeviceInfo) { |
| 67 | LoadCommands(R"({ |
| 68 | 'base': { |
| 69 | 'updateDeviceInfo': { |
| 70 | 'parameters': { |
| 71 | 'description': 'string', |
| 72 | 'name': { |
| 73 | 'type': 'string', |
| 74 | 'minLength': 1 |
| 75 | }, |
| 76 | 'location': 'string' |
| 77 | }, |
| 78 | 'results': {} |
| 79 | } |
| 80 | } |
| 81 | })"); |
| 82 | |
| 83 | AddCommand(R"({ |
| 84 | 'name' : 'base.updateDeviceInfo', |
| 85 | 'parameters': { |
| 86 | 'name': 'testName', |
| 87 | 'description': 'testDescription', |
| 88 | 'location': 'testLocation' |
| 89 | } |
| 90 | })"); |
| 91 | |
| 92 | const BuffetConfig& config{dev_reg_->GetConfig()}; |
| 93 | EXPECT_EQ("testName", config.name()); |
| 94 | EXPECT_EQ("testDescription", config.description()); |
| 95 | EXPECT_EQ("testLocation", config.location()); |
| 96 | } |
| 97 | |
| 98 | } // namespace buffet |