text: /*
   * Copyright (c) 2020 Seagate Technology LLC and/or its Affiliates
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   *
   * For any questions about this software or licensing,
   * please email opensource@seagate.com or cortx-questions@seagate.com.
   *
   */
  
  #include <gmock/gmock.h>
  #include <gtest/gtest.h>
  
  #include "mock_s3_bucket_metadata.h"
  #include "mock_s3_motr_wrapper.h"
  #include "mock_s3_factory.h"
  #include "mock_s3_request_object.h"
  #include "s3_delete_bucket_policy_action.h"
  
  using ::testing::Invoke;
  using ::testing::AtLeast;
  using ::testing::ReturnRef;
  
  class S3DeleteBucketPolicyActionTest : public testing::Test {
   protected:  // You should make the members protected s.t. they can be
               // accessed from sub-classes.
    S3DeleteBucketPolicyActionTest() {
      evhtp_request_t *req = NULL;
      EvhtpInterface *evhtp_obj_ptr = new EvhtpWrapper();
      bucket_name = "seagatebucket";
      request_mock = std::make_shared<MockS3RequestObject>(req, evhtp_obj_ptr);
      EXPECT_CALL(*request_mock, get_bucket_name())
          .WillRepeatedly(ReturnRef(bucket_name));
      ptr_mock_s3_motr_api = std::make_shared<MockS3Motr>();
      bucket_meta_factory = std::make_shared<MockS3BucketMetadataFactory>(
          request_mock, ptr_mock_s3_motr_api);
      std::map<std::string, std::string> input_headers;
      input_headers["Authorization"] = "1";
      EXPECT_CALL(*request_mock, get_in_headers_copy()).Times(1).WillOnce(
          ReturnRef(input_headers));
      action_under_test_ptr = std::make_shared<S3DeleteBucketPolicyAction>(
          request_mock, bucket_meta_factory);
    }
  
    std::shared_ptr<MockS3RequestObject> request_mock;
    std::shared_ptr<MockS3Motr> ptr_mock_s3_motr_api;
    std::shared_ptr<S3DeleteBucketPolicyAction> action_under_test_ptr;
    std::shared_ptr<MockS3BucketMetadataFactory> bucket_meta_factory;
    std::string bucket_name;
  };
  
  TEST_F(S3DeleteBucketPolicyActionTest, Constructor) {
    EXPECT_NE(0, action_under_test_ptr->number_of_tasks());
  }
  
  TEST_F(S3DeleteBucketPolicyActionTest, DeleteBucketPolicy) {
    action_under_test_ptr->bucket_metadata =
        action_under_test_ptr->bucket_metadata_factory
            ->create_bucket_metadata_obj(request_mock);
  
    std::string MockJsonResponse("Mockresponse");
    EXPECT_CALL(*(bucket_meta_factory->mock_bucket_metadata), get_state())
        .WillRepeatedly(Return(S3BucketMetadataState::present));
    EXPECT_CALL(*(bucket_meta_factory->mock_bucket_metadata),
                get_policy_as_json()).WillRepeatedly(ReturnRef(MockJsonResponse));
    EXPECT_CALL(*(bucket_meta_factory->mock_bucket_metadata), deletepolicy())
        .Times(AtLeast(1));
    EXPECT_CALL(*(bucket_meta_factory->mock_bucket_metadata), update(_, _))
        .Times(AtLeast(1));
    action_under_test_ptr->delete_bucket_policy();
  }
  
  TEST_F(S3DeleteBucketPolicyActionTest, DeleteBucketPolicySuccessful) {
    action_under_test_ptr->bucket_metadata =
        action_under_test_ptr->bucket_metadata_factory
            ->create_bucket_metadata_obj(request_mock);
  
    EXPECT_CALL(*(bucket_meta_factory->mock_bucket_metadata), get_state())
        .WillRepeatedly(Return(S3BucketMetadataState::present));
    EXPECT_CALL(*request_mock, send_response(204, _)).Times(AtLeast(1));
    action_under_test_ptr->delete_bucket_policy_successful();
  }
  
  TEST_F(S3DeleteBucketPolicyActionTest, DeleteBucketPolicyFailed) {
    action_under_test_ptr->bucket_metadata =
        action_under_test_ptr->bucket_metadata_factory
            ->create_bucket_metadata_obj(request_mock);
  
    EXPECT_CALL(*(bucket_meta_factory->mock_bucket_metadata), get_state())
        .WillRepeatedly(Return(S3BucketMetadataState::failed));
    EXPECT_CALL(*request_mock, set_out_header_value(_, _)).Times(AtLeast(1));
    EXPECT_CALL(*request_mock, send_response(500, _)).Times(AtLeast(1));
    action_under_test_ptr->delete_bucket_policy_failed();
  }
  
  TEST_F(S3DeleteBucketPolicyActionTest, SendResponseToClientNoSuchBucket) {
    action_under_test_ptr->set_s3_error("NoSuchBucket");
    EXPECT_CALL(*request_mock, set_out_header_value(_, _)).Times(AtLeast(1));
    EXPECT_CALL(*request_mock, send_response(404, _)).Times(AtLeast(1));
    action_under_test_ptr->send_response_to_s3_client();
  }
  
  TEST_F(S3DeleteBucketPolicyActionTest, SendResponseToClientSuccess) {
    action_under_test_ptr->bucket_metadata =
        action_under_test_ptr->bucket_metadata_factory
            ->create_bucket_metadata_obj(request_mock);
    EXPECT_CALL(*request_mock, send_response(204, _)).Times(AtLeast(1));
    action_under_test_ptr->send_response_to_s3_client();
  }
  
  TEST_F(S3DeleteBucketPolicyActionTest, SendResponseToClientInternalError) {
    action_under_test_ptr->set_s3_error("InternalError");
    EXPECT_CALL(*request_mock, set_out_header_value(_, _)).Times(AtLeast(1));
    EXPECT_CALL(*request_mock, send_response(500, _)).Times(AtLeast(1));
    action_under_test_ptr->send_response_to_s3_client();
  }
  
  