1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Test support library for response payloads.
#ifndef CHROME_BROWSER_POLICY_MESSAGING_LAYER_UTIL_TEST_RESPONSE_PAYLOAD_H_
#define CHROME_BROWSER_POLICY_MESSAGING_LAYER_UTIL_TEST_RESPONSE_PAYLOAD_H_
#include <optional>
#include "base/values.h"
#include "chrome/browser/policy/messaging_layer/util/reporting_server_connector.h"
#include "components/reporting/util/statusor.h"
namespace reporting {
// Class to build response based on an input request.
// Example:
//
// auto simple_successful_response = ResponseBuilder(std::move(request))
// .Build();
// auto failed_response = ResponseBuilder(std::move(request))
// .SetSuccess(false)
// .SetForceConfirm(true)
// .Build();
//
// For the document of what response payload should look like, search for
// "{{{Note}}} ERP Response Payload Overview" in the codebase.
class ResponseBuilder {
public:
ResponseBuilder() = default;
// We need a copy constructor here because base::Value::Dict has deleted its
// copy constructor and gtest will implicitly copy a ResponseBuilder object
// when it uses |MakeUploadEncryptedReportAction|, which we cannot work
// around.
ResponseBuilder(const ResponseBuilder& other);
ResponseBuilder(ResponseBuilder&& other) = default;
explicit ResponseBuilder(const base::Value::Dict& request);
explicit ResponseBuilder(base::Value::Dict&& request);
ResponseBuilder& SetForceConfirm(bool force_confirm);
ResponseBuilder& SetNull(bool null);
ResponseBuilder& SetRequest(const base::Value::Dict& request);
ResponseBuilder& SetRequest(base::Value::Dict&& request);
ResponseBuilder& SetSuccess(bool success);
// Build the response to be fed to a
// |::policy::CloudPolicyClient::ResponseCallback| object.
//
// Because we are focusing on testing here, the build here won't change
// members of the |ResponseBuilder| instance so that it can be called repeated
// for convenience. Additionally, this allows us to add more const qualifiers
// throughout the tests so that we reduce chances of incorrect test code
// (which itself isn't tested!).
StatusOr<base::Value::Dict> Build() const;
private:
// The request that was sent to the server.
base::Value::Dict request_;
// Parameters that can be tuned.
struct {
// Whether response should be a success or not
bool success = true;
// Whether forceConfirm is set to true or not
bool force_confirm = false;
// Whether response should be null
bool null = false;
} params_;
};
// Helper functor to be used with EXPECT_CALL and |UploadEncryptedReport|. It
// takes a |ResponseBuilder| object, and builds the response dict based on it
// and the input request from |UploadEncryptedReport|. Example:
//
//
// EXPECT_CALL(*client_,
// UploadEncryptedReport(IsDataUploadRequestValid(), _, _))
// .WillOnce(MakeUploadEncryptedReportAction(
// std::move(ResponseBuilder()
// .SetForceConfirm(force_confirm_by_server))));
//
// This class should be able to handle the majority of cases for defining mocked
// |UploadEncryptedReport| actions. In other circumstances, consider feeding a
// lambda function that uses |ResponseBuilder| to .WillOnce()/.WillRepeatedly().
//
// We don't define |MakeUploadEncryptedReportAction| a lambda function here
// because the mutable nature and the size of the argument list makes it far
// less readable.
class MakeUploadEncryptedReportAction {
public:
explicit MakeUploadEncryptedReportAction(
ResponseBuilder&& response_builder = ResponseBuilder());
void operator()(base::Value::Dict request,
std::optional<base::Value::Dict> context,
ReportingServerConnector::ResponseCallback callback);
private:
ResponseBuilder response_builder_;
};
} // namespace reporting
#endif // CHROME_BROWSER_POLICY_MESSAGING_LAYER_UTIL_TEST_RESPONSE_PAYLOAD_H_
|