Top Level Namespace

Defined Under Namespace

Modules: AutoapiTesting

Instance Method Summary collapse

Instance Method Details

#convert(schema) ⇒ Object

convert schema from OAS 3.0 to 3.1



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/autoapi/convert.rb', line 8

def convert(schema)
  if schema.class == Array
    schema.map { |child| convert child }
  elsif schema.class == Hash
    if schema['nullable']
      # not sure what to do when/if this happens
      raise 'no type' unless schema['type']

      schema['type'] = [schema['type'], nil]
    end
    schema.transform_values { |child| convert child }
  else
    schema
  end
end

#run_autoapi(oas_filename) ⇒ Object

check if file of generated tests exists and return that otherwise execute autoapi to create the file first



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/autoapi/run_autoapi.rb', line 6

def run_autoapi(oas_filename)
  use_cache = ENV['CI'] || ENV.fetch('AUTOAPI_USE_CACHE', 'false').casecmp('true').zero?
  # json_filename = Rails.root.join("doc/api_docs/autoapi/generated_tests/#{oas_filename.split('.')[0]}.json")
  json_filename = Rails.root.join("doc/api_docs/autoapi/generated_tests/#{oas_filename.split('.')[0]}.json")
  if use_cache && File.exist?(json_filename)
    puts 'Reusing existing test file'
    JSON.parse(File.read(json_filename))
  else
    puts 'Generating test file with AutoAPI'
    jar_path = File.join(File.dirname(__FILE__), 'AutoAPI-0.0.1.jar')
    oas_path = Rails.root.join('doc/api_docs', oas_filename)
    json = `java -jar #{jar_path} --generatetests #{oas_path}`
    begin
      autoapi_test_fixture = JSON.parse(json)
    rescue JSON::ParserError
      raise "Error Generating test cases with AutoAPI. \n" + json
    end
    json_file_dir = File.dirname(json_filename)
    unless File.directory?(json_file_dir)
      FileUtils.mkdir_p(json_file_dir)
    end
    File.write(json_filename, json)
    autoapi_test_fixture
  end
end