Home | History | Annotate | Download | only in cloud
      1 // Copyright (c) 2012 The Chromium 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 "chrome/browser/policy/cloud/mock_device_management_service.h"
      6 
      7 #include "base/strings/string_util.h"
      8 #include "net/base/net_errors.h"
      9 
     10 using testing::Action;
     11 
     12 namespace em = enterprise_management;
     13 
     14 namespace policy {
     15 namespace {
     16 
     17 // Common mock request job functionality.
     18 class MockRequestJobBase : public DeviceManagementRequestJob {
     19  public:
     20   MockRequestJobBase(JobType type,
     21                      MockDeviceManagementService* service)
     22       : DeviceManagementRequestJob(type),
     23         service_(service) {}
     24   virtual ~MockRequestJobBase() {}
     25 
     26  protected:
     27   virtual void Run() OVERRIDE {
     28     service_->StartJob(ExtractParameter(dm_protocol::kParamRequest),
     29                        gaia_token_,
     30                        ExtractParameter(dm_protocol::kParamOAuthToken),
     31                        dm_token_,
     32                        ExtractParameter(dm_protocol::kParamUserAffiliation),
     33                        ExtractParameter(dm_protocol::kParamDeviceID),
     34                        request_);
     35   }
     36 
     37  private:
     38   // Searches for a query parameter and returns the associated value.
     39   const std::string& ExtractParameter(const std::string& name) const {
     40     for (ParameterMap::const_iterator entry(query_params_.begin());
     41          entry != query_params_.end();
     42          ++entry) {
     43       if (name == entry->first)
     44         return entry->second;
     45     }
     46 
     47     return EmptyString();
     48   }
     49 
     50   MockDeviceManagementService* service_;
     51 
     52   DISALLOW_COPY_AND_ASSIGN(MockRequestJobBase);
     53 };
     54 
     55 // Synchronous mock request job that immediately completes on calling Run().
     56 class SyncRequestJob : public MockRequestJobBase {
     57  public:
     58   SyncRequestJob(JobType type,
     59                  MockDeviceManagementService* service,
     60                  DeviceManagementStatus status,
     61                  const em::DeviceManagementResponse& response)
     62       : MockRequestJobBase(type, service),
     63         status_(status),
     64         response_(response) {}
     65   virtual ~SyncRequestJob() {}
     66 
     67  protected:
     68   virtual void Run() OVERRIDE {
     69     MockRequestJobBase::Run();
     70     callback_.Run(status_, net::OK, response_);
     71   }
     72 
     73  private:
     74   DeviceManagementStatus status_;
     75   em::DeviceManagementResponse response_;
     76 
     77   DISALLOW_COPY_AND_ASSIGN(SyncRequestJob);
     78 };
     79 
     80 // Asynchronous job that allows the test to delay job completion.
     81 class AsyncRequestJob : public MockRequestJobBase,
     82                         public MockDeviceManagementJob {
     83  public:
     84   AsyncRequestJob(JobType type, MockDeviceManagementService* service)
     85       : MockRequestJobBase(type, service) {}
     86   virtual ~AsyncRequestJob() {}
     87 
     88  protected:
     89   virtual void RetryJob() OVERRIDE {
     90     if (!retry_callback_.is_null())
     91       retry_callback_.Run(this);
     92     Run();
     93   }
     94 
     95   virtual void SendResponse(
     96       DeviceManagementStatus status,
     97       const em::DeviceManagementResponse& response) OVERRIDE {
     98     callback_.Run(status, net::OK, response);
     99   }
    100 
    101  private:
    102   DISALLOW_COPY_AND_ASSIGN(AsyncRequestJob);
    103 };
    104 
    105 }  // namespace
    106 
    107 ACTION_P3(CreateSyncMockDeviceManagementJob, service, status, response) {
    108   return new SyncRequestJob(arg0, service, status, response);
    109 }
    110 
    111 ACTION_P2(CreateAsyncMockDeviceManagementJob, service, mock_job) {
    112   AsyncRequestJob* job = new AsyncRequestJob(arg0, service);
    113   *mock_job = job;
    114   return job;
    115 }
    116 
    117 MockDeviceManagementJob::~MockDeviceManagementJob() {}
    118 
    119 MockDeviceManagementService::MockDeviceManagementService()
    120     : DeviceManagementService(std::string()) {}
    121 
    122 MockDeviceManagementService::~MockDeviceManagementService() {}
    123 
    124 Action<MockDeviceManagementService::CreateJobFunction>
    125     MockDeviceManagementService::SucceedJob(
    126         const em::DeviceManagementResponse& response) {
    127   return CreateSyncMockDeviceManagementJob(this, DM_STATUS_SUCCESS, response);
    128 }
    129 
    130 Action<MockDeviceManagementService::CreateJobFunction>
    131     MockDeviceManagementService::FailJob(DeviceManagementStatus status) {
    132   const em::DeviceManagementResponse dummy_response;
    133   return CreateSyncMockDeviceManagementJob(this, status, dummy_response);
    134 }
    135 
    136 Action<MockDeviceManagementService::CreateJobFunction>
    137     MockDeviceManagementService::CreateAsyncJob(MockDeviceManagementJob** job) {
    138   return CreateAsyncMockDeviceManagementJob(this, job);
    139 }
    140 
    141 }  // namespace policy
    142