<commit_before>require 'spec_helper'
require 'hailstorm/model/project'
require 'hailstorm/model/jmeter_plan'

describe Hailstorm::Model::JmeterPlan do
  JMX_FILE_NAME = 'hailstorm-site-basic'

  before(:all) do
    src_path = File.expand_path("../../../features/data/#{JMX_FILE_NAME}.jmx", __FILE__)
    dest_dir_path = File.join(Hailstorm.root, Hailstorm.app_dir)
    FileUtils.mkdir_p(dest_dir_path)
    FileUtils.cp(src_path, dest_dir_path)
  end

  before(:each) do
    @jmeter_plan = Hailstorm::Model::JmeterPlan.new
    class << @jmeter_plan
      attr_writer :properties_map
    end
    @jmeter_plan.test_plan_name = JMX_FILE_NAME
    @jmeter_plan.validate_plan = true
    @jmeter_plan.active = true

    @file_helper = mock(@jmeter_plan.send(:file_helper).class)
    @jmeter_plan.instance_variable_set('@file_helper', @file_helper)
  end

  context '#validate_plan' do
    context 'when all properties in plan are defined in the model properties' do
      it 'should be valid' do
        @jmeter_plan.properties_map = { NumUsers: 10, Duration: 180, ServerName: 'foo.com', RampUp: 0, StartupDelay: 0 }.stringify_keys
        expect(@jmeter_plan).to be_valid
      end
    end

    context 'when any property in plan is not defined in the model properties' do
      it 'should not be valid' do
        @jmeter_plan.properties_map = { Duration: 180, ServerName: 'foo.com', RampUp: 0, StartupDelay: 0 }.stringify_keys
        expect(@jmeter_plan).to_not be_valid
      end
    end

    context 'when a property with default value in plan is not defined in the model properties' do
      it 'should be valid' do
        @jmeter_plan.properties_map = { NumUsers: 10, Duration: 180, ServerName: 'foo.com' }.stringify_keys
        expect(@jmeter_plan).to be_valid
      end
    end
    
    context 'when JMeter plan does not have simple data writer' do
      it 'should not be valid' do
        @jmeter_plan.stub!(:extracted_property_names).and_return([])
        @jmeter_plan.stub!(:jmeter_document).and_yield(Nokogiri::XML.parse('<foo></foo>'))
        expect(@jmeter_plan).to_not be_valid
      end
    end
  end

  context 'when property with default value is defined in model properties as well' do
    it 'the value from model properties takes precedence' do
      @jmeter_plan.properties_map = { NumUsers: 10, Duration: 180, ServerName: 'foo.com', RampUp: 10 }.stringify_keys
      @jmeter_plan.send(:extracted_property_names)
      expect(@jmeter_plan.properties_map['RampUp']).to eq(10)
    end
  end

  context '.setup' do
    before(:each) do
      @project = Hailstorm::Model::Project.create!(project_code: __FILE__)
      Hailstorm::Model::JmeterPlan.any_instance.stub(:validate_plan)
    end
    it 'should disable load agents of existing plans' do
      Hailstorm.application.config.jmeter do |jmeter|
        jmeter.properties do |property|
          property['NumUsers'] = 10
          property['Duration'] = 60
        end
      end
      jmx_files = %w[a.jmx b.jmx]
      @file_helper.stub!(:dir_glob_enumerator).and_return(jmx_files.each)
      Hailstorm::Model::JmeterPlan.setup(@project, @file_helper)
      jmx_files.pop
      @file_helper.stub!(:dir_glob_enumerator).and_return(jmx_files.each)
      @project.reload
      Hailstorm::Model::JmeterPlan.setup(@project, @file_helper)
      expect(Hailstorm::Model::JmeterPlan.where(test_plan_name: 'a').first).to be_active
      expect(Hailstorm::Model::JmeterPlan.where(test_plan_name: 'b').first).to_not be_active
    end
    context 'test_plans not specified in configuration' do
      context 'test_plans present in app_dir' do
        it 'should save all test_plans in app_dir' do
          Hailstorm.application.config.jmeter do |jmeter|<commit_msg>Fix failing specs<commit_after>require 'spec_helper'
require 'hailstorm/model/project'
require 'hailstorm/model/jmeter_plan'

describe Hailstorm::Model::JmeterPlan do
  JMX_FILE_NAME = 'hailstorm-site-basic'
  SOURCE_JMX_PATH = File.expand_path("../../../features/data/#{JMX_FILE_NAME}.jmx", __FILE__)

  before(:all) do
    dest_dir_path = File.join(Hailstorm.root, Hailstorm.app_dir)
    FileUtils.mkdir_p(dest_dir_path)
    FileUtils.cp(SOURCE_JMX_PATH, dest_dir_path)
  end

  before(:each) do
    @jmeter_plan = Hailstorm::Model::JmeterPlan.new
    class << @jmeter_plan
      attr_writer :properties_map
    end
    @jmeter_plan.test_plan_name = JMX_FILE_NAME
    @jmeter_plan.validate_plan = true
    @jmeter_plan.active = true

    @file_helper = mock(@jmeter_plan.send(:file_helper).class)
    @jmeter_plan.instance_variable_set('@file_helper', @file_helper)
  end

  context '#validate_plan' do
    context 'when all properties in plan are defined in the model properties' do
      it 'should be valid' do
        @jmeter_plan.properties_map = { NumUsers: 10, Duration: 180, ServerName: 'foo.com', RampUp: 0, StartupDelay: 0 }.stringify_keys
        expect(@jmeter_plan).to be_valid
      end
    end

    context 'when any property in plan is not defined in the model properties' do
      it 'should not be valid' do
        @jmeter_plan.properties_map = { Duration: 180, ServerName: 'foo.com', RampUp: 0, StartupDelay: 0 }.stringify_keys
        expect(@jmeter_plan).to_not be_valid
      end
    end

    context 'when a property with default value in plan is not defined in the model properties' do
      it 'should be valid' do
        @jmeter_plan.properties_map = { NumUsers: 10, Duration: 180, ServerName: 'foo.com' }.stringify_keys
        expect(@jmeter_plan).to be_valid
      end
    end
    
    context 'when JMeter plan does not have simple data writer' do
      it 'should not be valid' do
        @jmeter_plan.stub!(:extracted_property_names).and_return([])
        @jmeter_plan.stub!(:jmeter_document).and_yield(Nokogiri::XML.parse('<foo></foo>'))
        expect(@jmeter_plan).to_not be_valid
      end
    end
  end

  context 'when property with default value is defined in model properties as well' do
    it 'the value from model properties takes precedence' do
      @jmeter_plan.properties_map = { NumUsers: 10, Duration: 180, ServerName: 'foo.com', RampUp: 10 }.stringify_keys
      @jmeter_plan.send(:extracted_property_names)
      expect(@jmeter_plan.properties_map['RampUp']).to eq(10)
    end
  end

  context '.setup' do
    before(:each) do
      @project = Hailstorm::Model::Project.create!(project_code: __FILE__)
      Hailstorm::Model::JmeterPlan
        .any_instance
        .stub(:test_plan_file_path)
        .and_return(SOURCE_JMX_PATH)
    end
    it 'should disable load agents of existing plans' do
      Hailstorm.application.config.jmeter do |jmeter|
        jmeter.properties do |property|
          property['NumUsers'] = 10
          property['Duration'] = 60
        end
      end
      jmx_files = %w[a.jmx b.jmx]
      @file_helper.stub!(:dir_glob_enumerator).and_return(jmx_files.each)
      Hailstorm::Model::JmeterPlan.setup(@project, @file_helper)
      jmx_files.pop
      @file_helper.stub!(:dir_glob_enumerator).and_return(jmx_files.each)
      @project.reload
      Hailstorm::Model::JmeterPlan.setup(@project, @file_helper)
      expect(Hailstorm::Model::JmeterPlan.where(test_plan_name: 'a').first).to be_active
      expect(Hailstorm::Model::JmeterPlan.where(test_plan_name: 'b').first).to_not be_active
    end
    context 'test_plans not specified in configuration' do
      context 'test_plans present in app_dir' do
        it 'should save all test_plans in app_dir' do
          Hailstorm.application.config.jmeter do |jmeter|
{'fraction_non_alphanumeric': 0.10343911831098128, 'fraction_numerical': 0.007967069446288673, 'mean_word_length': 3.5565638233514822, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6977022', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2435, 'n_words': 570}