# Copyright 2017 Google Inc.
# 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.

# ----------------------------------------------------------------------------
#
#     ***     AUTO GENERATED CODE    ***    AUTO GENERATED CODE     ***
#
# ----------------------------------------------------------------------------
#
#     This file is automatically generated by Magic Modules and manual
#     changes will be clobbered when the file is regenerated.
#
#     Please read more about how to change this file in README.md and
#     CONTRIBUTING.md located at the root of this package.
#
# ----------------------------------------------------------------------------

require 'spec_helper'

describe Puppet::Type.type(:gstorage_bucket).provider(:google) do
  before(:all) do
    cred = Google::FakeAuthorization.new
    Puppet::Type.type(:gauth_credential)
                .define_singleton_method(:fetch) { |_resource| cred }
  end

  it '#instances' do
    expect { described_class.instances }.to raise_error(StandardError,
                                                        /not supported/)
  end

  context 'ensure == present' do
    context 'resource exists' do
      # Ensure present: resource exists, no change
      context 'no changes == no action' do
        # Ensure present: resource exists, no change, no name
        context 'title == name' do
          # Ensure present: resource exists, no change, no name, pass
          context 'title == name (pass)' do
            before do
              allow(Time).to receive(:now).and_return(
                Time.new(2017, 1, 2, 3, 4, 5)
              )
              expect_network_get_success 1, name: 'title0'
              expect_network_get_success 2, name: 'title1'
              expect_network_get_success 3, name: 'title2'
            end

            let(:catalog) do
              apply_with_error_check(
                <<-MANIFEST
                gstorage_bucket { 'title0':
                  ensure                        => present,
                  acl                           => [
                    {
                      bucket       => 'resource(bucket,0)',
                      domain       => 'test domain#0 data',
                      email        => 'test email#0 data',
                      entity       => 'test entity#0 data',
                      entity_id    => 'test entity_id#0 data',
                      id           => 'test id#0 data',
                      role         => 'OWNER',
                      project_team => {
                        team           => 'editors',
                        project_number => 'test project_number#0 data',
                      },
                    },
                    {
                      bucket       => 'resource(bucket,1)',
                      domain       => 'test domain#1 data',
                      email        => 'test email#1 data',
                      entity       => 'test entity#1 data',
                      entity_id    => 'test entity_id#1 data',
                      id           => 'test id#1 data',
                      role         => 'READER',
                      project_team => {
                        team           => 'owners',
                        project_number => 'test project_number#1 data',
                      },
                    },
                  ],
                  cors                          => [
                    {
                      max_age_seconds => 334924763,
                      method          => ['rr', 'ss', 'tt', 'uu', 'vv'],
                      origin          => ['ww', 'xx'],
                      response_header => ['jj', 'kk', 'll', 'mm', 'nn'],
                    },
                    {
                      max_age_seconds => 669849527,
                      method          => ['ll', 'mm', 'nn', 'oo', 'pp'],
                      origin          => ['vv', 'ww', 'xx'],
                      response_header => ['vv', 'ww', 'xx', 'yy', 'zz'],
                    },
                    {
                      max_age_seconds => 1004774291,
                      method          => ['ff', 'gg', 'hh', 'ii', 'jj'],
                      origin          => ['tt', 'uu', 'vv'],
                      response_header => ['hh', 'ii', 'jj', 'kk', 'll'],
                    },
                  ],
                  lifecycle                     => {
                    rule => [
                      {
                        action    => {
                          storage_class => 'test storage_class#0 data',
                          type          => 'Delete',
                        },
                        condition => {
                          age_days              => 1777301852,
                          created_before        => '1998-04-19T23:15:17+00:00',
                          is_live               => true,
                          matches_storage_class => ['ii', 'jj', 'kk', 'll'],
                          num_newer_versions    => 2057963480,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#1 data',
                          type          => 'SetStorageClass',
                        },
                        condition => {
                          age_days              => 3554603704,
                          created_before        => '2026-08-06T22:30:34+00:00',
                          is_live               => false,
                          matches_storage_class => ['ss', 'tt'],
                          num_newer_versions    => 4115926961,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#2 data',
                          type          => 'Delete',
                        },
                        condition => {
                          age_days              => 5331905557,
                          created_before        => '2054-11-23T21:45:51+00:00',
                          is_live               => true,
                          matches_storage_class => ['cc', 'dd', 'ee', 'ff'],
                          num_newer_versions    => 6173890442,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#3 data',
                          type          => 'SetStorageClass',
                        },
                        condition => {
                          age_days              => 7109207409,
                          created_before        => '2083-03-12T21:01:09+00:00',
                          is_live               => false,
                          matches_storage_class => ['nn', 'oo', 'pp'],
                          num_newer_versions    => 8231853923,
                        },
                      },
                    ],
                  },
                  location                      => 'test location#0 data',
                  logging                       => {
                    log_bucket        => 'test log_bucket#0 data',
                    log_object_prefix => 'test log_object_prefix#0 data',
                  },
                  metageneration                => 1438554838,
                  owner                         => {
                    entity    => 'test entity#0 data',
                    entity_id => 'test entity_id#0 data',
                  },
                  predefined_default_object_acl => 'authenticatedRead',
                  storage_class                 => 'MULTI_REGIONAL',
                  versioning                    => {
                    enabled => true,
                  },
                  website                       => {
                    main_page_suffix => 'test main_page_suffix#0 data',
                    not_found_page   => 'test not_found_page#0 data',
                  },
                  project                       => 'test project#0 data',
                  credential                    => 'cred0',
                }

                gstorage_bucket { 'title1':
                  ensure                        => present,
                  acl                           => [
                    {
                      bucket       => 'resource(bucket,1)',
                      domain       => 'test domain#1 data',
                      email        => 'test email#1 data',
                      entity       => 'test entity#1 data',
                      entity_id    => 'test entity_id#1 data',
                      id           => 'test id#1 data',
                      role         => 'READER',
                      project_team => {
                        team           => 'owners',
                        project_number => 'test project_number#1 data',
                      },
                    },
                    {
                      bucket       => 'resource(bucket,2)',
                      domain       => 'test domain#2 data',
                      email        => 'test email#2 data',
                      entity       => 'test entity#2 data',
                      entity_id    => 'test entity_id#2 data',
                      id           => 'test id#2 data',
                      role         => 'WRITER',
                      project_team => {
                        team           => 'viewers',
                        project_number => 'test project_number#2 data',
                      },
                    },
                    {
                      bucket       => 'resource(bucket,0)',
                      domain       => 'test domain#3 data',
                      email        => 'test email#3 data',
                      entity       => 'test entity#3 data',
                      entity_id    => 'test entity_id#3 data',
                      id           => 'test id#3 data',
                      role         => 'OWNER',
                      project_team => {
                        team           => 'editors',
                        project_number => 'test project_number#3 data',
                      },
                    },
                  ],
                  cors                          => [
                    {
                      max_age_seconds => 669849527,
                      method          => ['ll', 'mm', 'nn', 'oo', 'pp'],
                      origin          => ['vv', 'ww', 'xx'],
                      response_header => ['vv', 'ww', 'xx', 'yy', 'zz'],
                    },
                    {
                      max_age_seconds => 1004774291,
                      method          => ['ff', 'gg', 'hh', 'ii', 'jj'],
                      origin          => ['tt', 'uu', 'vv'],
                      response_header => ['hh', 'ii', 'jj', 'kk', 'll'],
                    },
                    {
                      max_age_seconds => 1339699054,
                      method          => ['vv', 'ww', 'xx', 'yy', 'zz'],
                      origin          => ['ss', 'tt', 'uu', 'vv'],
                      response_header => ['tt', 'uu', 'vv', 'ww', 'xx'],
                    },
                    {
                      max_age_seconds => 1674623818,
                      method          => ['tt', 'uu', 'vv', 'ww', 'xx'],
                      origin          => ['qq', 'rr', 'ss', 'tt'],
                      response_header => ['ff', 'gg', 'hh', 'ii', 'jj'],
                    },
                  ],
                  lifecycle                     => {
                    rule => [
                      {
                        action    => {
                          storage_class => 'test storage_class#1 data',
                          type          => 'SetStorageClass',
                        },
                        condition => {
                          age_days              => 3554603704,
                          created_before        => '2026-08-06T22:30:34+00:00',
                          is_live               => false,
                          matches_storage_class => ['ss', 'tt'],
                          num_newer_versions    => 4115926961,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#2 data',
                          type          => 'Delete',
                        },
                        condition => {
                          age_days              => 5331905557,
                          created_before        => '2054-11-23T21:45:51+00:00',
                          is_live               => true,
                          matches_storage_class => ['cc', 'dd', 'ee', 'ff'],
                          num_newer_versions    => 6173890442,
                        },
                      },
                    ],
                  },
                  location                      => 'test location#1 data',
                  logging                       => {
                    log_bucket        => 'test log_bucket#1 data',
                    log_object_prefix => 'test log_object_prefix#1 data',
                  },
                  metageneration                => 2877109677,
                  owner                         => {
                    entity    => 'test entity#1 data',
                    entity_id => 'test entity_id#1 data',
                  },
                  predefined_default_object_acl => 'bucketOwnerFullControl',
                  storage_class                 => 'REGIONAL',
                  versioning                    => {
                    enabled => false,
                  },
                  website                       => {
                    main_page_suffix => 'test main_page_suffix#1 data',
                    not_found_page   => 'test not_found_page#1 data',
                  },
                  project                       => 'test project#1 data',
                  credential                    => 'cred1',
                }

                gstorage_bucket { 'title2':
                  ensure                        => present,
                  acl                           => [
                    {
                      bucket       => 'resource(bucket,2)',
                      domain       => 'test domain#2 data',
                      email        => 'test email#2 data',
                      entity       => 'test entity#2 data',
                      entity_id    => 'test entity_id#2 data',
                      id           => 'test id#2 data',
                      role         => 'WRITER',
                      project_team => {
                        team           => 'viewers',
                        project_number => 'test project_number#2 data',
                      },
                    },
                    {
                      bucket       => 'resource(bucket,0)',
                      domain       => 'test domain#3 data',
                      email        => 'test email#3 data',
                      entity       => 'test entity#3 data',
                      entity_id    => 'test entity_id#3 data',
                      id           => 'test id#3 data',
                      role         => 'OWNER',
                      project_team => {
                        team           => 'editors',
                        project_number => 'test project_number#3 data',
                      },
                    },
                    {
                      bucket       => 'resource(bucket,1)',
                      domain       => 'test domain#4 data',
                      email        => 'test email#4 data',
                      entity       => 'test entity#4 data',
                      entity_id    => 'test entity_id#4 data',
                      id           => 'test id#4 data',
                      role         => 'READER',
                      project_team => {
                        team           => 'owners',
                        project_number => 'test project_number#4 data',
                      },
                    },
                  ],
                  cors                          => [
                    {
                      max_age_seconds => 1004774291,
                      method          => ['ff', 'gg', 'hh', 'ii', 'jj'],
                      origin          => ['tt', 'uu', 'vv'],
                      response_header => ['hh', 'ii', 'jj', 'kk', 'll'],
                    },
                    {
                      max_age_seconds => 1339699054,
                      method          => ['vv', 'ww', 'xx', 'yy', 'zz'],
                      origin          => ['ss', 'tt', 'uu', 'vv'],
                      response_header => ['tt', 'uu', 'vv', 'ww', 'xx'],
                    },
                    {
                      max_age_seconds => 1674623818,
                      method          => ['tt', 'uu', 'vv', 'ww', 'xx'],
                      origin          => ['qq', 'rr', 'ss', 'tt'],
                      response_header => ['ff', 'gg', 'hh', 'ii', 'jj'],
                    },
                    {
                      max_age_seconds => 2009548582,
                      method          => ['nn', 'oo', 'pp', 'qq', 'rr'],
                      origin          => ['pp', 'qq', 'rr', 'ss', 'tt'],
                      response_header => ['rr', 'ss', 'tt', 'uu', 'vv'],
                    },
                    {
                      max_age_seconds => 2344473345,
                      method          => ['gg', 'hh', 'ii', 'jj'],
                      origin          => ['oo', 'pp'],
                      response_header => ['cc', 'dd', 'ee', 'ff'],
                    },
                  ],
                  lifecycle                     => {
                    rule => [
                      {
                        action    => {
                          storage_class => 'test storage_class#2 data',
                          type          => 'Delete',
                        },
                        condition => {
                          age_days              => 5331905557,
                          created_before        => '2054-11-23T21:45:51+00:00',
                          is_live               => true,
                          matches_storage_class => ['cc', 'dd', 'ee', 'ff'],
                          num_newer_versions    => 6173890442,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#3 data',
                          type          => 'SetStorageClass',
                        },
                        condition => {
                          age_days              => 7109207409,
                          created_before        => '2083-03-12T21:01:09+00:00',
                          is_live               => false,
                          matches_storage_class => ['nn', 'oo', 'pp'],
                          num_newer_versions    => 8231853923,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#4 data',
                          type          => 'Delete',
                        },
                        condition => {
                          age_days              => 8886509262,
                          created_before        => '2111-06-30T20:16:26+00:00',
                          is_live               => true,
                          matches_storage_class => ['vv', 'ww', 'xx', 'yy', 'zz'],
                          num_newer_versions    => 10289817404,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#5 data',
                          type          => 'SetStorageClass',
                        },
                        condition => {
                          age_days              => 10663811114,
                          created_before        => '2139-10-17T19:31:43+00:00',
                          is_live               => false,
                          matches_storage_class => ['hh', 'ii', 'jj'],
                          num_newer_versions    => 12347780885,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#6 data',
                          type          => 'Delete',
                        },
                        condition => {
                          age_days              => 12441112967,
                          created_before        => '2168-02-03T18:47:01+00:00',
                          is_live               => true,
                          matches_storage_class => ['rr', 'ss', 'tt', 'uu', 'vv'],
                          num_newer_versions    => 14405744366,
                        },
                      },
                    ],
                  },
                  location                      => 'test location#2 data',
                  logging                       => {
                    log_bucket        => 'test log_bucket#2 data',
                    log_object_prefix => 'test log_object_prefix#2 data',
                  },
                  metageneration                => 4315664515,
                  owner                         => {
                    entity    => 'test entity#2 data',
                    entity_id => 'test entity_id#2 data',
                  },
                  predefined_default_object_acl => 'bucketOwnerRead',
                  storage_class                 => 'STANDARD',
                  versioning                    => {
                    enabled => true,
                  },
                  website                       => {
                    main_page_suffix => 'test main_page_suffix#2 data',
                    not_found_page   => 'test not_found_page#2 data',
                  },
                  project                       => 'test project#2 data',
                  credential                    => 'cred2',
                }
                MANIFEST
              ).catalog
            end

            context 'Gstorage_bucket[title0]' do
              subject do
                catalog.resource('Gstorage_bucket[title0]').provider
              end

              it 'was expected to be present', broken: true do
                pending('Implement tests where object references its own type.')
              end
            end

            context 'Gstorage_bucket[title1]' do
              subject do
                catalog.resource('Gstorage_bucket[title1]').provider
              end

              it 'was expected to be present', broken: true do
                pending('Implement tests where object references its own type.')
              end
            end

            context 'Gstorage_bucket[title2]' do
              subject do
                catalog.resource('Gstorage_bucket[title2]').provider
              end

              it 'was expected to be present', broken: true do
                pending('Implement tests where object references its own type.')
              end
            end
          end

          # Ensure present: resource exists, no change, no name, fail
          context 'title == name (fail)' do
            # TODO(nelsonjr): Implement new test format.
            subject { -> { raise '[placeholder] This should fail.' } }
            it { is_expected.to raise_error(RuntimeError, /placeholder/) }
          end
        end

        # Ensure present: resource exists, no change, has name
        context 'title != name' do
          # Ensure present: resource exists, no change, has name, pass
          context 'title != name (pass)' do
            before do
              allow(Time).to receive(:now).and_return(
                Time.new(2017, 1, 2, 3, 4, 5)
              )
              expect_network_get_success 1
              expect_network_get_success 2
              expect_network_get_success 3
            end

            let(:catalog) do
              apply_with_error_check(
                <<-MANIFEST
                gstorage_bucket { 'title0':
                  ensure                        => present,
                  acl                           => [
                    {
                      bucket       => 'resource(bucket,0)',
                      domain       => 'test domain#0 data',
                      email        => 'test email#0 data',
                      entity       => 'test entity#0 data',
                      entity_id    => 'test entity_id#0 data',
                      id           => 'test id#0 data',
                      role         => 'OWNER',
                      project_team => {
                        team           => 'editors',
                        project_number => 'test project_number#0 data',
                      },
                    },
                    {
                      bucket       => 'resource(bucket,1)',
                      domain       => 'test domain#1 data',
                      email        => 'test email#1 data',
                      entity       => 'test entity#1 data',
                      entity_id    => 'test entity_id#1 data',
                      id           => 'test id#1 data',
                      role         => 'READER',
                      project_team => {
                        team           => 'owners',
                        project_number => 'test project_number#1 data',
                      },
                    },
                  ],
                  cors                          => [
                    {
                      max_age_seconds => 334924763,
                      method          => ['rr', 'ss', 'tt', 'uu', 'vv'],
                      origin          => ['ww', 'xx'],
                      response_header => ['jj', 'kk', 'll', 'mm', 'nn'],
                    },
                    {
                      max_age_seconds => 669849527,
                      method          => ['ll', 'mm', 'nn', 'oo', 'pp'],
                      origin          => ['vv', 'ww', 'xx'],
                      response_header => ['vv', 'ww', 'xx', 'yy', 'zz'],
                    },
                    {
                      max_age_seconds => 1004774291,
                      method          => ['ff', 'gg', 'hh', 'ii', 'jj'],
                      origin          => ['tt', 'uu', 'vv'],
                      response_header => ['hh', 'ii', 'jj', 'kk', 'll'],
                    },
                  ],
                  lifecycle                     => {
                    rule => [
                      {
                        action    => {
                          storage_class => 'test storage_class#0 data',
                          type          => 'Delete',
                        },
                        condition => {
                          age_days              => 1777301852,
                          created_before        => '1998-04-19T23:15:17+00:00',
                          is_live               => true,
                          matches_storage_class => ['ii', 'jj', 'kk', 'll'],
                          num_newer_versions    => 2057963480,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#1 data',
                          type          => 'SetStorageClass',
                        },
                        condition => {
                          age_days              => 3554603704,
                          created_before        => '2026-08-06T22:30:34+00:00',
                          is_live               => false,
                          matches_storage_class => ['ss', 'tt'],
                          num_newer_versions    => 4115926961,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#2 data',
                          type          => 'Delete',
                        },
                        condition => {
                          age_days              => 5331905557,
                          created_before        => '2054-11-23T21:45:51+00:00',
                          is_live               => true,
                          matches_storage_class => ['cc', 'dd', 'ee', 'ff'],
                          num_newer_versions    => 6173890442,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#3 data',
                          type          => 'SetStorageClass',
                        },
                        condition => {
                          age_days              => 7109207409,
                          created_before        => '2083-03-12T21:01:09+00:00',
                          is_live               => false,
                          matches_storage_class => ['nn', 'oo', 'pp'],
                          num_newer_versions    => 8231853923,
                        },
                      },
                    ],
                  },
                  location                      => 'test location#0 data',
                  logging                       => {
                    log_bucket        => 'test log_bucket#0 data',
                    log_object_prefix => 'test log_object_prefix#0 data',
                  },
                  metageneration                => 1438554838,
                  name                          => 'test name#0 data',
                  owner                         => {
                    entity    => 'test entity#0 data',
                    entity_id => 'test entity_id#0 data',
                  },
                  predefined_default_object_acl => 'authenticatedRead',
                  storage_class                 => 'MULTI_REGIONAL',
                  versioning                    => {
                    enabled => true,
                  },
                  website                       => {
                    main_page_suffix => 'test main_page_suffix#0 data',
                    not_found_page   => 'test not_found_page#0 data',
                  },
                  project                       => 'test project#0 data',
                  credential                    => 'cred0',
                }

                gstorage_bucket { 'title1':
                  ensure                        => present,
                  acl                           => [
                    {
                      bucket       => 'resource(bucket,1)',
                      domain       => 'test domain#1 data',
                      email        => 'test email#1 data',
                      entity       => 'test entity#1 data',
                      entity_id    => 'test entity_id#1 data',
                      id           => 'test id#1 data',
                      role         => 'READER',
                      project_team => {
                        team           => 'owners',
                        project_number => 'test project_number#1 data',
                      },
                    },
                    {
                      bucket       => 'resource(bucket,2)',
                      domain       => 'test domain#2 data',
                      email        => 'test email#2 data',
                      entity       => 'test entity#2 data',
                      entity_id    => 'test entity_id#2 data',
                      id           => 'test id#2 data',
                      role         => 'WRITER',
                      project_team => {
                        team           => 'viewers',
                        project_number => 'test project_number#2 data',
                      },
                    },
                    {
                      bucket       => 'resource(bucket,0)',
                      domain       => 'test domain#3 data',
                      email        => 'test email#3 data',
                      entity       => 'test entity#3 data',
                      entity_id    => 'test entity_id#3 data',
                      id           => 'test id#3 data',
                      role         => 'OWNER',
                      project_team => {
                        team           => 'editors',
                        project_number => 'test project_number#3 data',
                      },
                    },
                  ],
                  cors                          => [
                    {
                      max_age_seconds => 669849527,
                      method          => ['ll', 'mm', 'nn', 'oo', 'pp'],
                      origin          => ['vv', 'ww', 'xx'],
                      response_header => ['vv', 'ww', 'xx', 'yy', 'zz'],
                    },
                    {
                      max_age_seconds => 1004774291,
                      method          => ['ff', 'gg', 'hh', 'ii', 'jj'],
                      origin          => ['tt', 'uu', 'vv'],
                      response_header => ['hh', 'ii', 'jj', 'kk', 'll'],
                    },
                    {
                      max_age_seconds => 1339699054,
                      method          => ['vv', 'ww', 'xx', 'yy', 'zz'],
                      origin          => ['ss', 'tt', 'uu', 'vv'],
                      response_header => ['tt', 'uu', 'vv', 'ww', 'xx'],
                    },
                    {
                      max_age_seconds => 1674623818,
                      method          => ['tt', 'uu', 'vv', 'ww', 'xx'],
                      origin          => ['qq', 'rr', 'ss', 'tt'],
                      response_header => ['ff', 'gg', 'hh', 'ii', 'jj'],
                    },
                  ],
                  lifecycle                     => {
                    rule => [
                      {
                        action    => {
                          storage_class => 'test storage_class#1 data',
                          type          => 'SetStorageClass',
                        },
                        condition => {
                          age_days              => 3554603704,
                          created_before        => '2026-08-06T22:30:34+00:00',
                          is_live               => false,
                          matches_storage_class => ['ss', 'tt'],
                          num_newer_versions    => 4115926961,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#2 data',
                          type          => 'Delete',
                        },
                        condition => {
                          age_days              => 5331905557,
                          created_before        => '2054-11-23T21:45:51+00:00',
                          is_live               => true,
                          matches_storage_class => ['cc', 'dd', 'ee', 'ff'],
                          num_newer_versions    => 6173890442,
                        },
                      },
                    ],
                  },
                  location                      => 'test location#1 data',
                  logging                       => {
                    log_bucket        => 'test log_bucket#1 data',
                    log_object_prefix => 'test log_object_prefix#1 data',
                  },
                  metageneration                => 2877109677,
                  name                          => 'test name#1 data',
                  owner                         => {
                    entity    => 'test entity#1 data',
                    entity_id => 'test entity_id#1 data',
                  },
                  predefined_default_object_acl => 'bucketOwnerFullControl',
                  storage_class                 => 'REGIONAL',
                  versioning                    => {
                    enabled => false,
                  },
                  website                       => {
                    main_page_suffix => 'test main_page_suffix#1 data',
                    not_found_page   => 'test not_found_page#1 data',
                  },
                  project                       => 'test project#1 data',
                  credential                    => 'cred1',
                }

                gstorage_bucket { 'title2':
                  ensure                        => present,
                  acl                           => [
                    {
                      bucket       => 'resource(bucket,2)',
                      domain       => 'test domain#2 data',
                      email        => 'test email#2 data',
                      entity       => 'test entity#2 data',
                      entity_id    => 'test entity_id#2 data',
                      id           => 'test id#2 data',
                      role         => 'WRITER',
                      project_team => {
                        team           => 'viewers',
                        project_number => 'test project_number#2 data',
                      },
                    },
                    {
                      bucket       => 'resource(bucket,0)',
                      domain       => 'test domain#3 data',
                      email        => 'test email#3 data',
                      entity       => 'test entity#3 data',
                      entity_id    => 'test entity_id#3 data',
                      id           => 'test id#3 data',
                      role         => 'OWNER',
                      project_team => {
                        team           => 'editors',
                        project_number => 'test project_number#3 data',
                      },
                    },
                    {
                      bucket       => 'resource(bucket,1)',
                      domain       => 'test domain#4 data',
                      email        => 'test email#4 data',
                      entity       => 'test entity#4 data',
                      entity_id    => 'test entity_id#4 data',
                      id           => 'test id#4 data',
                      role         => 'READER',
                      project_team => {
                        team           => 'owners',
                        project_number => 'test project_number#4 data',
                      },
                    },
                  ],
                  cors                          => [
                    {
                      max_age_seconds => 1004774291,
                      method          => ['ff', 'gg', 'hh', 'ii', 'jj'],
                      origin          => ['tt', 'uu', 'vv'],
                      response_header => ['hh', 'ii', 'jj', 'kk', 'll'],
                    },
                    {
                      max_age_seconds => 1339699054,
                      method          => ['vv', 'ww', 'xx', 'yy', 'zz'],
                      origin          => ['ss', 'tt', 'uu', 'vv'],
                      response_header => ['tt', 'uu', 'vv', 'ww', 'xx'],
                    },
                    {
                      max_age_seconds => 1674623818,
                      method          => ['tt', 'uu', 'vv', 'ww', 'xx'],
                      origin          => ['qq', 'rr', 'ss', 'tt'],
                      response_header => ['ff', 'gg', 'hh', 'ii', 'jj'],
                    },
                    {
                      max_age_seconds => 2009548582,
                      method          => ['nn', 'oo', 'pp', 'qq', 'rr'],
                      origin          => ['pp', 'qq', 'rr', 'ss', 'tt'],
                      response_header => ['rr', 'ss', 'tt', 'uu', 'vv'],
                    },
                    {
                      max_age_seconds => 2344473345,
                      method          => ['gg', 'hh', 'ii', 'jj'],
                      origin          => ['oo', 'pp'],
                      response_header => ['cc', 'dd', 'ee', 'ff'],
                    },
                  ],
                  lifecycle                     => {
                    rule => [
                      {
                        action    => {
                          storage_class => 'test storage_class#2 data',
                          type          => 'Delete',
                        },
                        condition => {
                          age_days              => 5331905557,
                          created_before        => '2054-11-23T21:45:51+00:00',
                          is_live               => true,
                          matches_storage_class => ['cc', 'dd', 'ee', 'ff'],
                          num_newer_versions    => 6173890442,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#3 data',
                          type          => 'SetStorageClass',
                        },
                        condition => {
                          age_days              => 7109207409,
                          created_before        => '2083-03-12T21:01:09+00:00',
                          is_live               => false,
                          matches_storage_class => ['nn', 'oo', 'pp'],
                          num_newer_versions    => 8231853923,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#4 data',
                          type          => 'Delete',
                        },
                        condition => {
                          age_days              => 8886509262,
                          created_before        => '2111-06-30T20:16:26+00:00',
                          is_live               => true,
                          matches_storage_class => ['vv', 'ww', 'xx', 'yy', 'zz'],
                          num_newer_versions    => 10289817404,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#5 data',
                          type          => 'SetStorageClass',
                        },
                        condition => {
                          age_days              => 10663811114,
                          created_before        => '2139-10-17T19:31:43+00:00',
                          is_live               => false,
                          matches_storage_class => ['hh', 'ii', 'jj'],
                          num_newer_versions    => 12347780885,
                        },
                      },
                      {
                        action    => {
                          storage_class => 'test storage_class#6 data',
                          type          => 'Delete',
                        },
                        condition => {
                          age_days              => 12441112967,
                          created_before        => '2168-02-03T18:47:01+00:00',
                          is_live               => true,
                          matches_storage_class => ['rr', 'ss', 'tt', 'uu', 'vv'],
                          num_newer_versions    => 14405744366,
                        },
                      },
                    ],
                  },
                  location                      => 'test location#2 data',
                  logging                       => {
                    log_bucket        => 'test log_bucket#2 data',
                    log_object_prefix => 'test log_object_prefix#2 data',
                  },
                  metageneration                => 4315664515,
                  name                          => 'test name#2 data',
                  owner                         => {
                    entity    => 'test entity#2 data',
                    entity_id => 'test entity_id#2 data',
                  },
                  predefined_default_object_acl => 'bucketOwnerRead',
                  storage_class                 => 'STANDARD',
                  versioning                    => {
                    enabled => true,
                  },
                  website                       => {
                    main_page_suffix => 'test main_page_suffix#2 data',
                    not_found_page   => 'test not_found_page#2 data',
                  },
                  project                       => 'test project#2 data',
                  credential                    => 'cred2',
                }
                MANIFEST
              ).catalog
            end

            context 'Gstorage_bucket[title0]' do
              subject do
                catalog.resource('Gstorage_bucket[title0]').provider
              end

              it 'was expected to be present', broken: true do
                pending('Implement tests where object references its own type.')
              end
            end

            context 'Gstorage_bucket[title1]' do
              subject do
                catalog.resource('Gstorage_bucket[title1]').provider
              end

              it 'was expected to be present', broken: true do
                pending('Implement tests where object references its own type.')
              end
            end

            context 'Gstorage_bucket[title2]' do
              subject do
                catalog.resource('Gstorage_bucket[title2]').provider
              end

              it 'was expected to be present', broken: true do
                pending('Implement tests where object references its own type.')
              end
            end
          end

          # Ensure present: resource exists, no change, has name, fail
          context 'title != name (fail)' do
            # TODO(nelsonjr): Implement new test format.
            subject { -> { raise '[placeholder] This should fail.' } }
            it { is_expected.to raise_error(RuntimeError, /placeholder/) }
          end
        end
      end

      # Ensure present: resource exists, changes
      context 'changes == action' do
        # Ensure present: resource exists, changes, no name
        context 'title == name' do
          # Ensure present: resource exists, changes, no name, pass
          context 'title == name (pass)' do
            # TODO(nelsonjr): Implement new test format.
          end

          # Ensure present: resource exists, changes, no name, fail
          context 'title == name (fail)' do
            # TODO(nelsonjr): Implement new test format.
            subject { -> { raise '[placeholder] This should fail.' } }
            it { is_expected.to raise_error(RuntimeError, /placeholder/) }
          end
        end

        # Ensure present: resource exists, changes, has name
        context 'title != name' do
          # Ensure present: resource exists, changes, has name, pass
          context 'title != name (pass)' do
            # TODO(nelsonjr): Implement new test format.
          end

          # Ensure present: resource exists, changes, has name, fail
          context 'title != name (fail)' do
            # TODO(nelsonjr): Implement new test format.
            subject { -> { raise '[placeholder] This should fail.' } }
            it { is_expected.to raise_error(RuntimeError, /placeholder/) }
          end
        end
      end
    end

    context 'resource missing' do
      # Ensure present: resource missing, ignore, no name
      context 'title == name' do
        # Ensure present: resource missing, ignore, no name, pass
        context 'title == name (pass)' do
          before(:each) do
            expect_network_get_failed 1, name: 'title0'
            expect_network_create \
              1,
              {
                'kind' => 'storage#bucket',
                'acl' => [
                  {
                    'bucket' => 'test name#0 data',
                    'domain' => 'test domain#0 data',
                    'email' => 'test email#0 data',
                    'entity' => 'test entity#0 data',
                    'entityId' => 'test entity_id#0 data',
                    'id' => 'test id#0 data',
                    'projectTeam' => {
                      'projectNumber' => 'test project_number#0 data',
                      'team' => 'editors'
                    },
                    'role' => 'OWNER'
                  },
                  {
                    'bucket' => 'test name#1 data',
                    'domain' => 'test domain#1 data',
                    'email' => 'test email#1 data',
                    'entity' => 'test entity#1 data',
                    'entityId' => 'test entity_id#1 data',
                    'id' => 'test id#1 data',
                    'projectTeam' => {
                      'projectNumber' => 'test project_number#1 data',
                      'team' => 'owners'
                    },
                    'role' => 'READER'
                  }
                ],
                'cors' => [
                  {
                    'maxAgeSeconds' => 334_924_763,
                    'method' => %w[rr ss tt uu vv],
                    'origin' => %w[ww xx],
                    'responseHeader' => %w[jj kk ll mm nn]
                  },
                  {
                    'maxAgeSeconds' => 669_849_527,
                    'method' => %w[ll mm nn oo pp],
                    'origin' => %w[vv ww xx],
                    'responseHeader' => %w[vv ww xx yy zz]
                  },
                  {
                    'maxAgeSeconds' => 1_004_774_291,
                    'method' => %w[ff gg hh ii jj],
                    'origin' => %w[tt uu vv],
                    'responseHeader' => %w[hh ii jj kk ll]
                  }
                ],
                'lifecycle' => {
                  'rule' => [
                    {
                      'action' => {
                        'storageClass' => 'test storage_class#0 data',
                        'type' => 'Delete'
                      },
                      'condition' => {
                        'age' => 1_777_301_852,
                        'createdBefore' => '1998-04-19T23:15:17+00:00',
                        'isLive' => true,
                        'matchesStorageClass' => %w[ii jj kk ll],
                        'numNewerVersions' => 2_057_963_480
                      }
                    },
                    {
                      'action' => {
                        'storageClass' => 'test storage_class#1 data',
                        'type' => 'SetStorageClass'
                      },
                      'condition' => {
                        'age' => 3_554_603_704,
                        'createdBefore' => '2026-08-06T22:30:34+00:00',
                        'isLive' => false,
                        'matchesStorageClass' => %w[ii jj kk ll],
                        'numNewerVersions' => 4_115_926_961
                      }
                    },
                    {
                      'action' => {
                        'storageClass' => 'test storage_class#2 data',
                        'type' => 'Delete'
                      },
                      'condition' => {
                        'age' => 5_331_905_557,
                        'createdBefore' => '2054-11-23T21:45:51+00:00',
                        'isLive' => true,
                        'matchesStorageClass' => %w[ii jj kk ll],
                        'numNewerVersions' => 6_173_890_442
                      }
                    },
                    {
                      'action' => {
                        'storageClass' => 'test storage_class#3 data',
                        'type' => 'SetStorageClass'
                      },
                      'condition' => {
                        'age' => 7_109_207_409,
                        'createdBefore' => '2083-03-12T21:01:09+00:00',
                        'isLive' => false,
                        'matchesStorageClass' => %w[ii jj kk ll],
                        'numNewerVersions' => 8_231_853_923
                      }
                    }
                  ]
                },
                'location' => 'test location#0 data',
                'logging' => {
                  'logBucket' => 'test log_bucket#0 data',
                  'logObjectPrefix' => 'test log_object_prefix#0 data'
                },
                'metageneration' => 1_438_554_838,
                'name' => 'title0',
                'owner' => {
                  'entity' => 'test entity#0 data',
                  'entityId' => 'test entity_id#0 data'
                },
                'storageClass' => 'MULTI_REGIONAL',
                'versioning' => {
                  'enabled' => true
                },
                'website' => {
                  'mainPageSuffix' => 'test main_page_suffix#0 data',
                  'notFoundPage' => 'test not_found_page#0 data'
                },
                'project' => 'test project#0 data',
                'predefinedDefaultObjectAcl' => 'authenticatedRead'
              },
              name: 'title0'
          end

          subject do
            apply_with_error_check(
              <<-MANIFEST
              gstorage_bucket { 'title0':
                ensure                        => present,
                acl                           => [
                  {
                    bucket       => 'resource(bucket,0)',
                    domain       => 'test domain#0 data',
                    email        => 'test email#0 data',
                    entity       => 'test entity#0 data',
                    entity_id    => 'test entity_id#0 data',
                    id           => 'test id#0 data',
                    role         => 'OWNER',
                    project_team => {
                      team           => 'editors',
                      project_number => 'test project_number#0 data',
                    },
                  },
                  {
                    bucket       => 'resource(bucket,1)',
                    domain       => 'test domain#1 data',
                    email        => 'test email#1 data',
                    entity       => 'test entity#1 data',
                    entity_id    => 'test entity_id#1 data',
                    id           => 'test id#1 data',
                    role         => 'READER',
                    project_team => {
                      team           => 'owners',
                      project_number => 'test project_number#1 data',
                    },
                  },
                ],
                cors                          => [
                  {
                    max_age_seconds => 334924763,
                    method          => ['rr', 'ss', 'tt', 'uu', 'vv'],
                    origin          => ['ww', 'xx'],
                    response_header => ['jj', 'kk', 'll', 'mm', 'nn'],
                  },
                  {
                    max_age_seconds => 669849527,
                    method          => ['ll', 'mm', 'nn', 'oo', 'pp'],
                    origin          => ['vv', 'ww', 'xx'],
                    response_header => ['vv', 'ww', 'xx', 'yy', 'zz'],
                  },
                  {
                    max_age_seconds => 1004774291,
                    method          => ['ff', 'gg', 'hh', 'ii', 'jj'],
                    origin          => ['tt', 'uu', 'vv'],
                    response_header => ['hh', 'ii', 'jj', 'kk', 'll'],
                  },
                ],
                lifecycle                     => {
                  rule => [
                    {
                      action    => {
                        storage_class => 'test storage_class#0 data',
                        type          => 'Delete',
                      },
                      condition => {
                        age_days              => 1777301852,
                        created_before        => '1998-04-19T23:15:17+00:00',
                        is_live               => true,
                        matches_storage_class => ['ii', 'jj', 'kk', 'll'],
                        num_newer_versions    => 2057963480,
                      },
                    },
                    {
                      action    => {
                        storage_class => 'test storage_class#1 data',
                        type          => 'SetStorageClass',
                      },
                      condition => {
                        age_days              => 3554603704,
                        created_before        => '2026-08-06T22:30:34+00:00',
                        is_live               => false,
                        matches_storage_class => ['ss', 'tt'],
                        num_newer_versions    => 4115926961,
                      },
                    },
                    {
                      action    => {
                        storage_class => 'test storage_class#2 data',
                        type          => 'Delete',
                      },
                      condition => {
                        age_days              => 5331905557,
                        created_before        => '2054-11-23T21:45:51+00:00',
                        is_live               => true,
                        matches_storage_class => ['cc', 'dd', 'ee', 'ff'],
                        num_newer_versions    => 6173890442,
                      },
                    },
                    {
                      action    => {
                        storage_class => 'test storage_class#3 data',
                        type          => 'SetStorageClass',
                      },
                      condition => {
                        age_days              => 7109207409,
                        created_before        => '2083-03-12T21:01:09+00:00',
                        is_live               => false,
                        matches_storage_class => ['nn', 'oo', 'pp'],
                        num_newer_versions    => 8231853923,
                      },
                    },
                  ],
                },
                location                      => 'test location#0 data',
                logging                       => {
                  log_bucket        => 'test log_bucket#0 data',
                  log_object_prefix => 'test log_object_prefix#0 data',
                },
                metageneration                => 1438554838,
                owner                         => {
                  entity    => 'test entity#0 data',
                  entity_id => 'test entity_id#0 data',
                },
                predefined_default_object_acl => 'authenticatedRead',
                storage_class                 => 'MULTI_REGIONAL',
                versioning                    => {
                  enabled => true,
                },
                website                       => {
                  main_page_suffix => 'test main_page_suffix#0 data',
                  not_found_page   => 'test not_found_page#0 data',
                },
                project                       => 'test project#0 data',
                credential                    => 'cred0',
              }

              gstorage_bucket { 'title1':
                ensure                        => present,
                acl                           => [
                  {
                    bucket       => 'resource(bucket,1)',
                    domain       => 'test domain#1 data',
                    email        => 'test email#1 data',
                    entity       => 'test entity#1 data',
                    entity_id    => 'test entity_id#1 data',
                    id           => 'test id#1 data',
                    role         => 'READER',
                    project_team => {
                      team           => 'owners',
                      project_number => 'test project_number#1 data',
                    },
                  },
                  {
                    bucket       => 'resource(bucket,2)',
                    domain       => 'test domain#2 data',
                    email        => 'test email#2 data',
                    entity       => 'test entity#2 data',
                    entity_id    => 'test entity_id#2 data',
                    id           => 'test id#2 data',
                    role         => 'WRITER',
                    project_team => {
                      team           => 'viewers',
                      project_number => 'test project_number#2 data',
                    },
                  },
                  {
                    bucket       => 'resource(bucket,0)',
                    domain       => 'test domain#3 data',
                    email        => 'test email#3 data',
                    entity       => 'test entity#3 data',
                    entity_id    => 'test entity_id#3 data',
                    id           => 'test id#3 data',
                    role         => 'OWNER',
                    project_team => {
                      team           => 'editors',
                      project_number => 'test project_number#3 data',
                    },
                  },
                ],
                cors                          => [
                  {
                    max_age_seconds => 669849527,
                    method          => ['ll', 'mm', 'nn', 'oo', 'pp'],
                    origin          => ['vv', 'ww', 'xx'],
                    response_header => ['vv', 'ww', 'xx', 'yy', 'zz'],
                  },
                  {
                    max_age_seconds => 1004774291,
                    method          => ['ff', 'gg', 'hh', 'ii', 'jj'],
                    origin          => ['tt', 'uu', 'vv'],
                    response_header => ['hh', 'ii', 'jj', 'kk', 'll'],
                  },
                  {
                    max_age_seconds => 1339699054,
                    method          => ['vv', 'ww', 'xx', 'yy', 'zz'],
                    origin          => ['ss', 'tt', 'uu', 'vv'],
                    response_header => ['tt', 'uu', 'vv', 'ww', 'xx'],
                  },
                  {
                    max_age_seconds => 1674623818,
                    method          => ['tt', 'uu', 'vv', 'ww', 'xx'],
                    origin          => ['qq', 'rr', 'ss', 'tt'],
                    response_header => ['ff', 'gg', 'hh', 'ii', 'jj'],
                  },
                ],
                lifecycle                     => {
                  rule => [
                    {
                      action    => {
                        storage_class => 'test storage_class#1 data',
                        type          => 'SetStorageClass',
                      },
                      condition => {
                        age_days              => 3554603704,
                        created_before        => '2026-08-06T22:30:34+00:00',
                        is_live               => false,
                        matches_storage_class => ['ss', 'tt'],
                        num_newer_versions    => 4115926961,
                      },
                    },
                    {
                      action    => {
                        storage_class => 'test storage_class#2 data',
                        type          => 'Delete',
                      },
                      condition => {
                        age_days              => 5331905557,
                        created_before        => '2054-11-23T21:45:51+00:00',
                        is_live               => true,
                        matches_storage_class => ['cc', 'dd', 'ee', 'ff'],
                        num_newer_versions    => 6173890442,
                      },
                    },
                  ],
                },
                location                      => 'test location#1 data',
                logging                       => {
                  log_bucket        => 'test log_bucket#1 data',
                  log_object_prefix => 'test log_object_prefix#1 data',
                },
                metageneration                => 2877109677,
                owner                         => {
                  entity    => 'test entity#1 data',
                  entity_id => 'test entity_id#1 data',
                },
                predefined_default_object_acl => 'bucketOwnerFullControl',
                storage_class                 => 'REGIONAL',
                versioning                    => {
                  enabled => false,
                },
                website                       => {
                  main_page_suffix => 'test main_page_suffix#1 data',
                  not_found_page   => 'test not_found_page#1 data',
                },
                project                       => 'test project#1 data',
                credential                    => 'cred1',
              }
              MANIFEST
            ).catalog.resource('Gstorage_bucket[title0]').provider.ensure
          end

          it 'was expected to be present', broken: true do
            pending('Implement tests where object references its own type.')
          end
        end

        # Ensure present: resource missing, ignore, no name, fail
        context 'title == name (fail)' do
          # TODO(nelsonjr): Implement new test format.
          subject { -> { raise '[placeholder] This should fail.' } }
          it { is_expected.to raise_error(RuntimeError, /placeholder/) }
        end
      end

      # Ensure present: resource missing, ignore, has name
      context 'title != name' do
        # Ensure present: resource missing, ignore, has name, pass
        context 'title != name (pass)' do
          before(:each) do
            expect_network_get_failed 1
            expect_network_create \
              1,
              'kind' => 'storage#bucket',
              'acl' => [
                {
                  'bucket' => 'test name#0 data',
                  'domain' => 'test domain#0 data',
                  'email' => 'test email#0 data',
                  'entity' => 'test entity#0 data',
                  'entityId' => 'test entity_id#0 data',
                  'id' => 'test id#0 data',
                  'projectTeam' => {
                    'projectNumber' => 'test project_number#0 data',
                    'team' => 'editors'
                  },
                  'role' => 'OWNER'
                },
                {
                  'bucket' => 'test name#1 data',
                  'domain' => 'test domain#1 data',
                  'email' => 'test email#1 data',
                  'entity' => 'test entity#1 data',
                  'entityId' => 'test entity_id#1 data',
                  'id' => 'test id#1 data',
                  'projectTeam' => {
                    'projectNumber' => 'test project_number#1 data',
                    'team' => 'owners'
                  },
                  'role' => 'READER'
                }
              ],
              'cors' => [
                {
                  'maxAgeSeconds' => 334_924_763,
                  'method' => %w[rr ss tt uu vv],
                  'origin' => %w[ww xx],
                  'responseHeader' => %w[jj kk ll mm nn]
                },
                {
                  'maxAgeSeconds' => 669_849_527,
                  'method' => %w[ll mm nn oo pp],
                  'origin' => %w[vv ww xx],
                  'responseHeader' => %w[vv ww xx yy zz]
                },
                {
                  'maxAgeSeconds' => 1_004_774_291,
                  'method' => %w[ff gg hh ii jj],
                  'origin' => %w[tt uu vv],
                  'responseHeader' => %w[hh ii jj kk ll]
                }
              ],
              'lifecycle' => {
                'rule' => [
                  {
                    'action' => {
                      'storageClass' => 'test storage_class#0 data',
                      'type' => 'Delete'
                    },
                    'condition' => {
                      'age' => 1_777_301_852,
                      'createdBefore' => '1998-04-19T23:15:17+00:00',
                      'isLive' => true,
                      'matchesStorageClass' => %w[ii jj kk ll],
                      'numNewerVersions' => 2_057_963_480
                    }
                  },
                  {
                    'action' => {
                      'storageClass' => 'test storage_class#1 data',
                      'type' => 'SetStorageClass'
                    },
                    'condition' => {
                      'age' => 3_554_603_704,
                      'createdBefore' => '2026-08-06T22:30:34+00:00',
                      'isLive' => false,
                      'matchesStorageClass' => %w[ii jj kk ll],
                      'numNewerVersions' => 4_115_926_961
                    }
                  },
                  {
                    'action' => {
                      'storageClass' => 'test storage_class#2 data',
                      'type' => 'Delete'
                    },
                    'condition' => {
                      'age' => 5_331_905_557,
                      'createdBefore' => '2054-11-23T21:45:51+00:00',
                      'isLive' => true,
                      'matchesStorageClass' => %w[ii jj kk ll],
                      'numNewerVersions' => 6_173_890_442
                    }
                  },
                  {
                    'action' => {
                      'storageClass' => 'test storage_class#3 data',
                      'type' => 'SetStorageClass'
                    },
                    'condition' => {
                      'age' => 7_109_207_409,
                      'createdBefore' => '2083-03-12T21:01:09+00:00',
                      'isLive' => false,
                      'matchesStorageClass' => %w[ii jj kk ll],
                      'numNewerVersions' => 8_231_853_923
                    }
                  }
                ]
              },
              'location' => 'test location#0 data',
              'logging' => {
                'logBucket' => 'test log_bucket#0 data',
                'logObjectPrefix' => 'test log_object_prefix#0 data'
              },
              'metageneration' => 1_438_554_838,
              'name' => 'test name#0 data',
              'owner' => {
                'entity' => 'test entity#0 data',
                'entityId' => 'test entity_id#0 data'
              },
              'storageClass' => 'MULTI_REGIONAL',
              'versioning' => {
                'enabled' => true
              },
              'website' => {
                'mainPageSuffix' => 'test main_page_suffix#0 data',
                'notFoundPage' => 'test not_found_page#0 data'
              },
              'project' => 'test project#0 data',
              'predefinedDefaultObjectAcl' => 'authenticatedRead'
          end

          subject do
            apply_with_error_check(
              <<-MANIFEST
              gstorage_bucket { 'title0':
                ensure                        => present,
                acl                           => [
                  {
                    bucket       => 'resource(bucket,0)',
                    domain       => 'test domain#0 data',
                    email        => 'test email#0 data',
                    entity       => 'test entity#0 data',
                    entity_id    => 'test entity_id#0 data',
                    id           => 'test id#0 data',
                    role         => 'OWNER',
                    project_team => {
                      team           => 'editors',
                      project_number => 'test project_number#0 data',
                    },
                  },
                  {
                    bucket       => 'resource(bucket,1)',
                    domain       => 'test domain#1 data',
                    email        => 'test email#1 data',
                    entity       => 'test entity#1 data',
                    entity_id    => 'test entity_id#1 data',
                    id           => 'test id#1 data',
                    role         => 'READER',
                    project_team => {
                      team           => 'owners',
                      project_number => 'test project_number#1 data',
                    },
                  },
                ],
                cors                          => [
                  {
                    max_age_seconds => 334924763,
                    method          => ['rr', 'ss', 'tt', 'uu', 'vv'],
                    origin          => ['ww', 'xx'],
                    response_header => ['jj', 'kk', 'll', 'mm', 'nn'],
                  },
                  {
                    max_age_seconds => 669849527,
                    method          => ['ll', 'mm', 'nn', 'oo', 'pp'],
                    origin          => ['vv', 'ww', 'xx'],
                    response_header => ['vv', 'ww', 'xx', 'yy', 'zz'],
                  },
                  {
                    max_age_seconds => 1004774291,
                    method          => ['ff', 'gg', 'hh', 'ii', 'jj'],
                    origin          => ['tt', 'uu', 'vv'],
                    response_header => ['hh', 'ii', 'jj', 'kk', 'll'],
                  },
                ],
                lifecycle                     => {
                  rule => [
                    {
                      action    => {
                        storage_class => 'test storage_class#0 data',
                        type          => 'Delete',
                      },
                      condition => {
                        age_days              => 1777301852,
                        created_before        => '1998-04-19T23:15:17+00:00',
                        is_live               => true,
                        matches_storage_class => ['ii', 'jj', 'kk', 'll'],
                        num_newer_versions    => 2057963480,
                      },
                    },
                    {
                      action    => {
                        storage_class => 'test storage_class#1 data',
                        type          => 'SetStorageClass',
                      },
                      condition => {
                        age_days              => 3554603704,
                        created_before        => '2026-08-06T22:30:34+00:00',
                        is_live               => false,
                        matches_storage_class => ['ss', 'tt'],
                        num_newer_versions    => 4115926961,
                      },
                    },
                    {
                      action    => {
                        storage_class => 'test storage_class#2 data',
                        type          => 'Delete',
                      },
                      condition => {
                        age_days              => 5331905557,
                        created_before        => '2054-11-23T21:45:51+00:00',
                        is_live               => true,
                        matches_storage_class => ['cc', 'dd', 'ee', 'ff'],
                        num_newer_versions    => 6173890442,
                      },
                    },
                    {
                      action    => {
                        storage_class => 'test storage_class#3 data',
                        type          => 'SetStorageClass',
                      },
                      condition => {
                        age_days              => 7109207409,
                        created_before        => '2083-03-12T21:01:09+00:00',
                        is_live               => false,
                        matches_storage_class => ['nn', 'oo', 'pp'],
                        num_newer_versions    => 8231853923,
                      },
                    },
                  ],
                },
                location                      => 'test location#0 data',
                logging                       => {
                  log_bucket        => 'test log_bucket#0 data',
                  log_object_prefix => 'test log_object_prefix#0 data',
                },
                metageneration                => 1438554838,
                name                          => 'test name#0 data',
                owner                         => {
                  entity    => 'test entity#0 data',
                  entity_id => 'test entity_id#0 data',
                },
                predefined_default_object_acl => 'authenticatedRead',
                storage_class                 => 'MULTI_REGIONAL',
                versioning                    => {
                  enabled => true,
                },
                website                       => {
                  main_page_suffix => 'test main_page_suffix#0 data',
                  not_found_page   => 'test not_found_page#0 data',
                },
                project                       => 'test project#0 data',
                credential                    => 'cred0',
              }

              gstorage_bucket { 'title1':
                ensure                        => present,
                acl                           => [
                  {
                    bucket       => 'resource(bucket,1)',
                    domain       => 'test domain#1 data',
                    email        => 'test email#1 data',
                    entity       => 'test entity#1 data',
                    entity_id    => 'test entity_id#1 data',
                    id           => 'test id#1 data',
                    role         => 'READER',
                    project_team => {
                      team           => 'owners',
                      project_number => 'test project_number#1 data',
                    },
                  },
                  {
                    bucket       => 'resource(bucket,2)',
                    domain       => 'test domain#2 data',
                    email        => 'test email#2 data',
                    entity       => 'test entity#2 data',
                    entity_id    => 'test entity_id#2 data',
                    id           => 'test id#2 data',
                    role         => 'WRITER',
                    project_team => {
                      team           => 'viewers',
                      project_number => 'test project_number#2 data',
                    },
                  },
                  {
                    bucket       => 'resource(bucket,0)',
                    domain       => 'test domain#3 data',
                    email        => 'test email#3 data',
                    entity       => 'test entity#3 data',
                    entity_id    => 'test entity_id#3 data',
                    id           => 'test id#3 data',
                    role         => 'OWNER',
                    project_team => {
                      team           => 'editors',
                      project_number => 'test project_number#3 data',
                    },
                  },
                ],
                cors                          => [
                  {
                    max_age_seconds => 669849527,
                    method          => ['ll', 'mm', 'nn', 'oo', 'pp'],
                    origin          => ['vv', 'ww', 'xx'],
                    response_header => ['vv', 'ww', 'xx', 'yy', 'zz'],
                  },
                  {
                    max_age_seconds => 1004774291,
                    method          => ['ff', 'gg', 'hh', 'ii', 'jj'],
                    origin          => ['tt', 'uu', 'vv'],
                    response_header => ['hh', 'ii', 'jj', 'kk', 'll'],
                  },
                  {
                    max_age_seconds => 1339699054,
                    method          => ['vv', 'ww', 'xx', 'yy', 'zz'],
                    origin          => ['ss', 'tt', 'uu', 'vv'],
                    response_header => ['tt', 'uu', 'vv', 'ww', 'xx'],
                  },
                  {
                    max_age_seconds => 1674623818,
                    method          => ['tt', 'uu', 'vv', 'ww', 'xx'],
                    origin          => ['qq', 'rr', 'ss', 'tt'],
                    response_header => ['ff', 'gg', 'hh', 'ii', 'jj'],
                  },
                ],
                lifecycle                     => {
                  rule => [
                    {
                      action    => {
                        storage_class => 'test storage_class#1 data',
                        type          => 'SetStorageClass',
                      },
                      condition => {
                        age_days              => 3554603704,
                        created_before        => '2026-08-06T22:30:34+00:00',
                        is_live               => false,
                        matches_storage_class => ['ss', 'tt'],
                        num_newer_versions    => 4115926961,
                      },
                    },
                    {
                      action    => {
                        storage_class => 'test storage_class#2 data',
                        type          => 'Delete',
                      },
                      condition => {
                        age_days              => 5331905557,
                        created_before        => '2054-11-23T21:45:51+00:00',
                        is_live               => true,
                        matches_storage_class => ['cc', 'dd', 'ee', 'ff'],
                        num_newer_versions    => 6173890442,
                      },
                    },
                  ],
                },
                location                      => 'test location#1 data',
                logging                       => {
                  log_bucket        => 'test log_bucket#1 data',
                  log_object_prefix => 'test log_object_prefix#1 data',
                },
                metageneration                => 2877109677,
                name                          => 'test name#1 data',
                owner                         => {
                  entity    => 'test entity#1 data',
                  entity_id => 'test entity_id#1 data',
                },
                predefined_default_object_acl => 'bucketOwnerFullControl',
                storage_class                 => 'REGIONAL',
                versioning                    => {
                  enabled => false,
                },
                website                       => {
                  main_page_suffix => 'test main_page_suffix#1 data',
                  not_found_page   => 'test not_found_page#1 data',
                },
                project                       => 'test project#1 data',
                credential                    => 'cred1',
              }
              MANIFEST
            ).catalog.resource('Gstorage_bucket[title0]').provider.ensure
          end

          it 'was expected to be present', broken: true do
            pending('Implement tests where object references its own type.')
          end
        end

        # Ensure present: resource missing, ignore, has name, fail
        context 'title != name (fail)' do
          # TODO(nelsonjr): Implement new test format.
          subject { -> { raise '[placeholder] This should fail.' } }
          it { is_expected.to raise_error(RuntimeError, /placeholder/) }
        end
      end
    end
  end

  context 'ensure == absent' do
    context 'resource missing' do
      # Ensure absent: resource missing, ignore, no name
      context 'title == name' do
        # Ensure absent: resource missing, ignore, no name, pass
        context 'title == name (pass)' do
          before(:each) do
            expect_network_get_failed 1, name: 'title0'
          end

          subject do
            apply_with_error_check(
              <<-MANIFEST
              gstorage_bucket { 'title0':
                ensure     => absent,
                project    => 'test project#0 data',
                credential => 'cred0',
              }
              MANIFEST
            ).catalog.resource('Gstorage_bucket[title0]')
              .provider.ensure
          end

          it { is_expected.to eq :absent }
        end

        # Ensure absent: resource missing, ignore, no name, fail
        context 'title == name (fail)' do
          # TODO(nelsonjr): Implement new test format.
          subject { -> { raise '[placeholder] This should fail.' } }
          it { is_expected.to raise_error(RuntimeError, /placeholder/) }
        end
      end

      # Ensure absent: resource missing, ignore, has name
      context 'title != name' do
        # Ensure absent: resource missing, ignore, has name, pass
        context 'title != name (pass)' do
          before(:each) do
            expect_network_get_failed 1
          end

          subject do
            apply_with_error_check(
              <<-MANIFEST
              gstorage_bucket { 'title0':
                ensure     => absent,
                name       => 'test name#0 data',
                project    => 'test project#0 data',
                credential => 'cred0',
              }
              MANIFEST
            ).catalog.resource('Gstorage_bucket[title0]')
              .provider.ensure
          end

          it { is_expected.to eq :absent }
        end

        # Ensure absent: resource missing, ignore, has name, fail
        context 'title != name (fail)' do
          # TODO(nelsonjr): Implement new test format.
          subject { -> { raise '[placeholder] This should fail.' } }
          it { is_expected.to raise_error(RuntimeError, /placeholder/) }
        end
      end
    end

    context 'resource exists' do
      # Ensure absent: resource exists, ignore, no name
      context 'title == name' do
        # Ensure absent: resource exists, ignore, no name, pass
        context 'title == name (pass)' do
          before(:each) do
            expect_network_get_success 1, name: 'title0'
            expect_network_delete 1, 'title0'
          end

          subject do
            apply_with_error_check(
              <<-MANIFEST
              gstorage_bucket { 'title0':
                ensure     => absent,
                project    => 'test project#0 data',
                credential => 'cred0',
              }
              MANIFEST
            ).catalog.resource('Gstorage_bucket[title0]')
              .provider.ensure
          end

          it { is_expected.to eq :absent }
        end

        # Ensure absent: resource exists, ignore, no name, fail
        context 'title == name (fail)' do
          # TODO(nelsonjr): Implement new test format.
          subject { -> { raise '[placeholder] This should fail.' } }
          it { is_expected.to raise_error(RuntimeError, /placeholder/) }
        end
      end

      # Ensure absent: resource exists, ignore, has name
      context 'title != name' do
        # Ensure absent: resource exists, ignore, has name, pass
        context 'title != name (pass)' do
          before(:each) do
            expect_network_get_success 1
            expect_network_delete 1
          end

          subject do
            apply_with_error_check(
              <<-MANIFEST
              gstorage_bucket { 'title0':
                ensure     => absent,
                name       => 'test name#0 data',
                project    => 'test project#0 data',
                credential => 'cred0',
              }
              MANIFEST
            ).catalog.resource('Gstorage_bucket[title0]')
              .provider.ensure
          end

          it { is_expected.to eq :absent }
        end

        # Ensure absent: resource exists, ignore, has name, fail
        context 'title != name (fail)' do
          # TODO(nelsonjr): Implement new test format.
          subject { -> { raise '[placeholder] This should fail.' } }
          it { is_expected.to raise_error(RuntimeError, /placeholder/) }
        end
      end
    end
  end

  context '#flush' do
    subject do
      Puppet::Type.type(:gstorage_bucket).new(
        ensure: :present,
        name: 'my-name'
      ).provider
    end
    context 'no-op' do
      it { subject.flush }
    end
    context 'modified object' do
      before do
        subject.dirty :some_property, 'current', 'newvalue'
      end
      context 'no-op if created' do
        before { subject.instance_variable_set(:@created, true) }
        it { expect { subject.flush }.not_to raise_error }
      end
      context 'no-op if deleted' do
        before { subject.instance_variable_set(:@deleted, true) }
        it { expect { subject.flush }.not_to raise_error }
      end
    end
  end

  context '#exports' do
    context 'exports all properties' do
      let(:resource1) { create_type 1 }
      before do
        expect_network_get_success 1
        described_class.prefetch(title0: resource1)
      end

      subject { resource1.exports }

      let(:expected_results) do
        {
          name: 'test name#0 data'
        }
      end
      it { is_expected.to eq(expected_results) }
    end
  end

  private

  def expect_network_get_success(id, data = {})
    id_data = data.fetch(:name, '').include?('title') ? 'title' : 'name'
    body = load_network_result("success#{id}~#{id_data}.yaml").to_json

    request = double('request')
    allow(request).to receive(:send).and_return(http_success(body))

    debug_network "!! GET #{self_link(uri_data(id).merge(data))}"
    expect(Google::Storage::Network::Get).to receive(:new)
      .with(self_link(uri_data(id).merge(data)),
            instance_of(Google::FakeAuthorization)) do |args|
      debug_network ">> GET #{args}"
      request
    end
  end

  def http_success(body)
    response = Net::HTTPOK.new(1.0, 200, 'OK')
    response.body = body
    response.instance_variable_set(:@read, true)
    response
  end

  def expect_network_get_failed(id, data = {})
    request = double('request')
    allow(request).to receive(:send).and_return(http_failed_object_missing)

    debug_network "!! #{self_link(uri_data(id).merge(data))}"
    expect(Google::Storage::Network::Get).to receive(:new)
      .with(self_link(uri_data(id).merge(data)),
            instance_of(Google::FakeAuthorization)) do |args|
      debug_network ">> GET [failed] #{args}"
      request
    end
  end

  def http_failed_object_missing
    Net::HTTPNotFound.new(1.0, 404, 'Not Found')
  end

  def expect_network_create(id, expected_body, data = {})
    merged_uri = uri_data(id).merge(data)
    body = { kind: 'storage#bucket' }.to_json

    request = double('request')
    allow(request).to receive(:send).and_return(http_success(body))

    debug_network "!! POST #{collection(merged_uri)}"
    expect(Google::Storage::Network::Post).to receive(:new)
      .with(collection(merged_uri), instance_of(Google::FakeAuthorization),
            'application/json', expected_body.to_json) do |args|
      debug_network ">> POST #{args} = body(#{body})"
      request
    end
  end

  def expect_network_delete(id, name = nil, data = {})
    delete_data = uri_data(id).merge(data)
    delete_data[:name] = name unless name.nil?
    body = { kind: 'storage#bucket' }.to_json

    request = double('request')
    allow(request).to receive(:send).and_return(http_success(body))

    debug_network "!! DELETE #{self_link(delete_data)}"
    expect(Google::Storage::Network::Delete).to receive(:new)
      .with(self_link(delete_data),
            instance_of(Google::FakeAuthorization)) do |args|
      debug_network ">> DELETE #{args}"
      request
    end
  end

  def load_network_result(file)
    results = File.join(File.dirname(__FILE__), 'data', 'network',
                        'gstorage_bucket', file)
    debug("Loading result file: #{results}")
    raise "Network result data file #{results}" unless File.exist?(results)
    data = YAML.safe_load(File.read(results))
    raise "Invalid network results #{results}" unless data.class <= Hash
    data
  end

  def debug(message)
    puts(message) if ENV['RSPEC_DEBUG']
  end

  def debug_network(message)
    puts("Network #{message}") \
      if ENV['RSPEC_DEBUG'] || ENV['RSPEC_HTTP_VERBOSE']
  end

  def create_type(id)
    Puppet::Type.type(:gstorage_bucket).new(
      ensure: :present,
      title: "title#{id - 1}",
      credential: "cred#{id - 1}",
      name: GoogleTests::Constants::B_NAME_DATA[(id - 1) \
        % GoogleTests::Constants::B_NAME_DATA.size],
      project: GoogleTests::Constants::B_PROJECT_DATA[(id - 1) \
        % GoogleTests::Constants::B_PROJECT_DATA.size]
    )
  end

  def expand_variables(template, data, extra_data = {})
    Puppet::Type.type(:gstorage_bucket).provider(:google)
                .expand_variables(template, data, extra_data)
  end

  def collection(data)
    URI.join(
      'https://www.googleapis.com/storage/v1/',
      expand_variables(
        'b?project={{project}}',
        data
      )
    )
  end

  def self_link(data)
    URI.join(
      'https://www.googleapis.com/storage/v1/',
      expand_variables(
        'b/{{name}}?projection=full',
        data
      )
    )
  end

  # Creates variable test data to comply with self_link URI parameters
  def uri_data(id)
    {
      name: GoogleTests::Constants::B_NAME_DATA[(id - 1) \
        % GoogleTests::Constants::B_NAME_DATA.size],
      project: GoogleTests::Constants::B_PROJECT_DATA[(id - 1) \
        % GoogleTests::Constants::B_PROJECT_DATA.size]
    }
  end
end
