<commit_before>#
# Cookbook Name:: rackspace_cloudbackup
#
# Copyright 2014, Rackspace, US, 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.

require 'spec_helper'
require 'webmock/rspec'

require_relative '../../../libraries/MockRcbuApiWrapper.rb'

include WebMock::API

module RcbuApiWrapperTestHelpers
  def test_data
    return {
      api_username: 'Test API Username',
      api_key:      'Test API Key',
      region:       'TESTREGION',    # Needs to be UPCASE
      agent_id:     'TestAgentID', # I believe in the real API this needs to be an int, but our code doesn't care
      api_url:      'http://mockidentity.local/',
    }
  end
  module_function :test_data

  

end

describe 'RcbuApiWrapper' do
  describe 'initialize' do
    before :each do
      @test_data = RcbuApiWrapperTestHelpers.test_data
      WebMock.disable_net_connect!
      @test_obj = Opscode::Rackspace::CloudBackup::MockRcbuApiWrapper.new(@test_data[:api_username], @test_data[:api_key], @test_data[:region], @test_data[:agent_id], @test_data[:api_url])
    end

    it 'sets the agent_id class instance variable' do
      @test_obj.agent_id.should eql @test_data[:agent_id]
    end

    it 'sets the api_url class instance variable' do
      @test_obj.api_url.should eql @test_data[:api_url]
    end

    it 'sets the api token class instance variable' do
      @test_obj.token.should eql '<PASSWORD>'
    end

    it 'sets the rcbu API URL class instance variable' do
      @test_obj.rcbu_api_url.should eql 'https://MockRcbuApiWrapper.dummy.api.local/'
    end
  end

  describe '_identity_data' do
    before :each do
      @test_data = RcbuApiWrapperTestHelpers.test_data
      WebMock.disable_net_connect!
      @test_obj = Opscode::Rackspace::CloudBackup::MockRcbuApiWrapper.new(@test_data[:api_username], @test_data[:api_key], @test_data[:region], @test_data[:agent_id], @test_data[:api_url])
    end

    it 'has none of your shennanigans' do
      expect { @test_obj._identity_data('foo', 'bar') }.to raise_exception
    end
  end

  # These are grouped because we need one to test the other.
  describe 'stateful lookup_configurations / create_config mocks: ' do
    before :each do
      @test_data = RcbuApiWrapperTestHelpers.test_data
      WebMock.disable_net_connect!
      @test_obj = Opscode::Rackspace::CloudBackup::MockRcbuApiWrapper.new(@test_data[:api_username], @test_data[:api_key], @test_data[:region], @test_data[:agent_id], @test_data[:api_url])
    end

    it 'lookup_configurations sets configurations class instance variable to nil when mock_configurations class variable is empty' do
      @test_obj.mock_configurations.should eql []
      @test_obj.lookup_configurations
      @test_obj.configurations.should eql nil
    end

    it 'create_config adds config to mock_configurations class variable' do
      @test_obj.mock_configurations.should eql []
      test_data = { 'name' => 'dataW', 'key1' => 'dataW-1', 'key2' => 'dataW-2' }
      @test_obj.create_config(test_data)
      @test_obj.mock_configurations[0].should eql test_data
    end

    it 'create_config adds configs to mock_configurations class variable' do
      @test_obj.mock_configurations.should eql []
      test_data = { 'name' => 'dataW', 'key1' => 'dataW-1', 'key2' => 'dataW-2' }
      10.times do
        @test_obj.create_config(test_data)
      end
      @test_obj.mock_configurations.length.should eql 10
    end     

    it 'lookup_configurations sets configurations class instance variable to mock_configurations when mock_configurations class variable is not empty' do
      @test_obj.mock_configurations.should eql []
      test_data = { 'name' => 'dataW', 'key1' => 'dataW-1', 'key2' => 'dataW-2' }
      @test_obj.create_config(test_data)
      @test_obj.mock_configurations[0].should eql test_data
      @test_obj.lookup_configurations
      @test_obj.configurations.should eql @test_obj.mock_configurations
    end
  end

  describe 'update_config' do
    before :each do
      @test_data = RcbuApiWrapperTestHelpers.test_data
      WebMock.disable_net_connect!
      @test_obj = Opscode::Rackspace::CloudBackup::MockRcbuApiWrapper.new(@test_data[:api_username], @test_data[:api_key], @test_data[:region], @test_data[:agent_id], @test_data[:api_url])
    end
    
    it 'fails if the config_id doesn\'t exist in mock_configurations class variable' do
      @test_obj.mock_configurations.should eql []
      expect { @test_obj.update_config(12345, { foo: 'bar' }) }.to raise_exception
    end

    it 'updates existing entries in the mock_configurations class variable' do
      @test_obj.mock_configurations.should eql []
      initial_data = { 'name' => 'dataI', 'key1' => 'dataI-1', 'key2' => 'dataI-2', 'BackupConfigurationId' => 76543 }
      @test_obj.create_config(initial_data)
      @test_obj.mock_configurations[0].should eql initial_data<commit_msg>Fix copypasta namespace error, clean up whitespace<commit_after>#
# Cookbook Name:: rackspace_cloudbackup
#
# Copyright 2014, Rackspace, US, 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.

require 'spec_helper'
require 'webmock/rspec'

require_relative '../../../libraries/MockRcbuApiWrapper.rb'

include WebMock::API

module MockRcbuApiWrapperTestHelpers
  def test_data
    return {
      api_username: 'Test API Username',
      api_key:      'Test API Key',
      region:       'TESTREGION',    # Needs to be UPCASE
      agent_id:     'TestAgentID', # I believe in the real API this needs to be an int, but our code doesn't care
      api_url:      'http://mockidentity.local/',
    }
  end
  module_function :test_data
end

describe 'RcbuApiWrapper' do
  describe 'initialize' do
    before :each do
      @test_data = MockRcbuApiWrapperTestHelpers.test_data
      WebMock.disable_net_connect!
      @test_obj = Opscode::Rackspace::CloudBackup::MockRcbuApiWrapper.new(@test_data[:api_username], @test_data[:api_key], @test_data[:region], @test_data[:agent_id], @test_data[:api_url])
    end

    it 'sets the agent_id class instance variable' do
      @test_obj.agent_id.should eql @test_data[:agent_id]
    end

    it 'sets the api_url class instance variable' do
      @test_obj.api_url.should eql @test_data[:api_url]
    end

    it 'sets the api token class instance variable' do
      @test_obj.token.should eql '<PASSWORD>'
    end

    it 'sets the rcbu API URL class instance variable' do
      @test_obj.rcbu_api_url.should eql 'https://MockRcbuApiWrapper.dummy.api.local/'
    end
  end

  describe '_identity_data' do
    before :each do
      @test_data = MockRcbuApiWrapperTestHelpers.test_data
      WebMock.disable_net_connect!
      @test_obj = Opscode::Rackspace::CloudBackup::MockRcbuApiWrapper.new(@test_data[:api_username], @test_data[:api_key], @test_data[:region], @test_data[:agent_id], @test_data[:api_url])
    end

    it 'has none of your shennanigans' do
      expect { @test_obj._identity_data('foo', 'bar') }.to raise_exception
    end
  end

  # These are grouped because we need one to test the other.
  describe 'stateful lookup_configurations / create_config mocks: ' do
    before :each do
      @test_data = MockRcbuApiWrapperTestHelpers.test_data
      WebMock.disable_net_connect!
      @test_obj = Opscode::Rackspace::CloudBackup::MockRcbuApiWrapper.new(@test_data[:api_username], @test_data[:api_key], @test_data[:region], @test_data[:agent_id], @test_data[:api_url])
    end

    it 'lookup_configurations sets configurations class instance variable to nil when mock_configurations class variable is empty' do
      @test_obj.mock_configurations.should eql []
      @test_obj.lookup_configurations
      @test_obj.configurations.should eql nil
    end

    it 'create_config adds config to mock_configurations class variable' do
      @test_obj.mock_configurations.should eql []
      test_data = { 'name' => 'dataW', 'key1' => 'dataW-1', 'key2' => 'dataW-2' }
      @test_obj.create_config(test_data)
      @test_obj.mock_configurations[0].should eql test_data
    end

    it 'create_config adds configs to mock_configurations class variable' do
      @test_obj.mock_configurations.should eql []
      test_data = { 'name' => 'dataW', 'key1' => 'dataW-1', 'key2' => 'dataW-2' }
      10.times do
        @test_obj.create_config(test_data)
      end
      @test_obj.mock_configurations.length.should eql 10
    end     

    it 'lookup_configurations sets configurations class instance variable to mock_configurations when mock_configurations class variable is not empty' do
      @test_obj.mock_configurations.should eql []
      test_data = { 'name' => 'dataW', 'key1' => 'dataW-1', 'key2' => 'dataW-2' }
      @test_obj.create_config(test_data)
      @test_obj.mock_configurations[0].should eql test_data
      @test_obj.lookup_configurations
      @test_obj.configurations.should eql @test_obj.mock_configurations
    end
  end

  describe 'update_config' do
    before :each do
      @test_data = MockRcbuApiWrapperTestHelpers.test_data
      WebMock.disable_net_connect!
      @test_obj = Opscode::Rackspace::CloudBackup::MockRcbuApiWrapper.new(@test_data[:api_username], @test_data[:api_key], @test_data[:region], @test_data[:agent_id], @test_data[:api_url])
    end
    
    it 'fails if the config_id doesn\'t exist in mock_configurations class variable' do
      @test_obj.mock_configurations.should eql []
      expect { @test_obj.update_config(12345, { foo: 'bar' }) }.to raise_exception
    end

    it 'updates existing entries in the mock_configurations class variable' do
      @test_obj.mock_configurations.should eql []
      initial_data = { 'name' => 'dataI', 'key1' => 'dataI-1', 'key2' => 'dataI-2', 'BackupConfigurationId' => 76543 }
      @test_obj.create_config(initial_data)
      @test_obj.mock_configurations[0].should eql initial_data
{'fraction_non_alphanumeric': 0.1010016694490818, 'fraction_numerical': 0.0076052680393248005, 'mean_word_length': 3.9102914389799635, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '245491', 'n_tokens_mistral': 3466, 'n_tokens_neox': 3426, 'n_words': 885}