okapi 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,7 +35,7 @@ This resource allows you to submit payment information to process your *shopping
35
35
  POST /payment
36
36
  { "cc": "12345678900", "cvc": "123", "expiry": "0112" }
37
37
  < 200
38
- { "receipt": "/payment/receipt/1aaa" }
38
+ { "receipt": "/payment/receipt/1" }
39
39
 
40
40
  -- JSON Schema Validations --
41
41
 
@@ -0,0 +1,5 @@
1
+ tests:
2
+ host: http://aptest.apiary.io
3
+ specs:
4
+ - tests.spec
5
+ - tests2.spec
@@ -0,0 +1,6 @@
1
+ VARS {"id2": "eee", "id": "5"}
2
+ POST /shopping-cart/{id2} {"id2": "eee2"}
3
+ POST
4
+ CONTINUE
5
+
6
+
@@ -1,4 +1,3 @@
1
1
  POST /shopping-cart/{id2} {"id2": "eee2"}
2
2
  POST /payment
3
3
 
4
-
File without changes
@@ -14,30 +14,46 @@ module Apiary
14
14
  @res_path = res_path
15
15
  end
16
16
 
17
- def get_requests(resources, blueprint)
17
+ def get_response(raw_resp, json_data, error, code)
18
+ { :resp => raw_resp,
19
+ :data => json_data ? json_data['requests'] : nil,
20
+ :status => json_data ? json_data['status'] : nil ,
21
+ :error => json_data ? json_data['error'] || error : error,
22
+ :code => code
23
+ }
24
+ end
25
+
26
+ def get_requests(resources, blueprint, all_resources = false, global_vars = {})
18
27
 
19
28
  resources_list = []
20
29
 
21
30
  resources.each() do |res|
22
31
  resources_list << {
23
- :resource => res.uri,
24
- :method => res.method,
25
- :params => res.params,
32
+ :resource => res['resource'],
33
+ :method => res['method'],
34
+ :params => res['params']
26
35
  }
27
36
  end
28
37
 
29
38
  data = {
30
39
  :resources => resources_list,
31
- :blueprint => blueprint
40
+ :blueprint => blueprint,
41
+ :all_resources => all_resources,
42
+ :global_vars => global_vars
32
43
  }.to_json()
33
-
44
+
34
45
  begin
35
46
  response = RestClient.post @apiary_url + @req_path, data, :content_type => :json, :accept => :json
36
- data = JSON.parse(response.to_str)
37
-
38
- {:resp => response, :data => data['requests'], :status => data['status'], :error => data['error']}
39
- rescue Exception => e
40
- {:resp => nil, :data => nil, :status => nil, :error => e}
47
+ get_response(response, JSON.parse(response.to_str), nil, response.code.to_i)
48
+ rescue RestClient::BadRequest, RestClient::InternalServerError => e
49
+ begin
50
+ data = JSON.parse(e.http_body)
51
+ get_response(nil, JSON.parse(e.http_body), data['error'], e.http_code.to_i)
52
+ rescue
53
+ get_response(nil, nil, e.to_s, e.http_code.to_i)
54
+ end
55
+ rescue Exception => e
56
+ get_response(nil, nil, e.to_s, nil)
41
57
  end
42
58
 
43
59
  end
@@ -69,12 +85,15 @@ module Apiary
69
85
 
70
86
  begin
71
87
  response = RestClient.post @apiary_url + @res_path, data, :content_type => :json, :accept => :json
72
-
73
- data = JSON.parse(response.to_str)
74
-
75
- {:resp => response, :data => data['requests'], :status => data['status'], :error => data['error']}
76
- rescue Exception => e
77
- {:resp => nil, :data => nil, :status => nil, :error => e}
88
+ get_response(response, JSON.parse(response.to_str), nil, response.code.to_i)
89
+ rescue RestClient::BadRequest, RestClient::InternalServerError => e
90
+ begin
91
+ get_response(nil, JSON.parse(e.http_body), data['error'], e.http_code.to_i)
92
+ rescue
93
+ get_response(nil, nil, e.to_s, e.http_code.to_i)
94
+ end
95
+ rescue Exception => e
96
+ get_response(nil, nil, e.to_s, nil)
78
97
  end
79
98
  end
80
99
  end
data/lib/okapi/cli.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
  require 'optparse'
3
+ require 'yaml'
3
4
 
4
5
  module Apiary
5
6
  module Okapi
@@ -9,67 +10,111 @@ module Apiary
9
10
  case args.first
10
11
  when 'help'
11
12
  puts Apiary::Okapi::Help.show
13
+ exit 0
12
14
  when 'version'
13
15
  puts VERSION
16
+ exit 0
14
17
  when 'okapi'
15
18
  puts Apiary::Okapi::Help.okapi
19
+ exit 0
16
20
  else
17
- run(parse_options!(args))
18
- end
21
+ parse_options!(args)
22
+ parse_config
23
+ @options[:blueprint] ||= BLUEPRINT_PATH
24
+ @options[:test_spec] ||= TEST_SPEC_PATHS
25
+ @options[:output] ||= OUTPUT
26
+ @options[:test_url] ||= TEST_URL
27
+ @options[:apiary_url] ||= APIARY_URL
28
+
29
+ @options[:test_spec] ||= TEST_SPEC_PATHS.gsub(' ','').split(',')
19
30
 
20
-
31
+ if @options[:params]
32
+ puts "running with :"
33
+ p @options
34
+ puts "\n"
35
+ end
36
+
37
+ exit run
38
+ end
21
39
  end
22
40
 
23
- def run(options)
24
- Apiary::Okapi::Test.new(options[:blueprint], options[:test_spec], options[:test_url], options[:output], options[:apiary_url]).run()
41
+ def run
42
+ pass = true
43
+ @options[:test_spec].each { |spec|
44
+ pass = Apiary::Okapi::Test.new(@options[:blueprint], spec, @options[:test_url], @options[:output], @options[:apiary_url]).run()
45
+ }
46
+ if pass
47
+ 0
48
+ else
49
+ 1
50
+ end
51
+ end
25
52
 
53
+ def parse_config
54
+ begin
55
+ if tests = YAML.load_file(@options[:config_path])['tests']
56
+ @options[:test_url] ||= tests['host'] if tests['host']
57
+ @options[:test_spec] ||= tests['specs'] if tests['specs']
58
+ end
59
+ rescue Errno::ENOENT => e
60
+ puts "Config file (#{@options[:config_path]}) not accessible ... skiping"
61
+ rescue Exception => e
62
+ puts "Config file (#{@options[:config_path]}) loading problem :#{e}"
63
+ exit 1
64
+ end
26
65
  end
27
66
 
28
67
  def parse_options!(args)
29
- options = {}
68
+ @options = {}
30
69
  options_parser = OptionParser.new do |opts|
70
+ opts.on("-c", "--config CONFIG",
71
+ "path config file (default: " + CONFIG_PATH + " )") do |config|
72
+ @options[:config_path] = config
73
+ end
74
+
31
75
  opts.on("-b", "--blueprint BLUEPRINT",
32
76
  "path to the blueprint (default: " + BLUEPRINT_PATH + " )") do |blueprint|
33
- options[:blueprint] = blueprint
77
+ @options[:blueprint] = blueprint
34
78
  end
35
79
 
36
80
  opts.on("-t","--test_spec TEST_SPEC",
37
- "path to the test specification (default: " + TEST_SPEC_PATH + " )") do |test_spec|
38
- options[:test_spec] = test_spec
81
+ "comma separated paths to the test specifications (default: " + TEST_SPEC_PATHS + " )") do |test_spec|
82
+ @options[:test_spec] = test_spec
39
83
  end
40
84
 
41
85
  opts.on("-o","--output OUTPUT",
42
86
  "output format (default" + OUTPUT + ")") do |output|
43
- options[:output] = output
87
+ @options[:output] = output
44
88
  end
45
89
 
46
90
  opts.on("-u","--test_url TEST_URL",
47
91
  "url to test (default" + TEST_URL + ")") do |test_url|
48
- options[:test_url] = test_url
92
+ @options[:test_url] = test_url
49
93
  end
50
94
 
51
95
  opts.on("-a","--apiary APIARY",
52
96
  "apiary url (default" + APIARY_URL + ")") do |apiary|
53
- options[:apiary_url] = apiary
54
- end
97
+ @options[:apiary_url] = apiary
98
+ end
55
99
 
100
+ opts.on("-p","--params [PARAMS]",
101
+ "show parameters" ) do |params|
102
+ @options[:params] = true
103
+ end
56
104
  end
57
105
 
58
106
  options_parser.parse!
59
- options[:blueprint] ||= BLUEPRINT_PATH
60
- options[:test_spec] ||= TEST_SPEC_PATH
61
- options[:output] ||= OUTPUT
62
- options[:test_url] ||= TEST_URL
63
- options[:apiary_url] ||= APIARY_URL
64
107
 
65
- raise OptionParser::InvalidOption, 'Blueprint file "' + options[:blueprint] + '" does not exist' if not File.file?(options[:blueprint])
66
- raise OptionParser::InvalidOption, 'Test specification file "' + options[:test_spec] + '" does not exist' if not File.file?(options[:test_spec])
67
-
68
- options
108
+ @options[:config_path] ||= CONFIG_PATH
109
+ @options[:test_spec] = @options[:test_spec].gsub(' ','').split(',') if @options[:test_spec]
110
+
111
+ @options
69
112
 
70
113
  rescue OptionParser::InvalidOption => e
114
+ puts "\n"
71
115
  puts e
72
- puts Apiary::Okapi::Help.banner
116
+ Apiary::Okapi::Help.banner
117
+ puts "\n"
73
118
  exit 1
74
119
  end
75
120
 
data/lib/okapi/config.rb CHANGED
@@ -1,12 +1,13 @@
1
1
  # encoding: utf-8
2
2
  module Apiary
3
3
  module Okapi
4
+ CONFIG_PATH = "apiary.yaml"
4
5
  APIARY_URL = "https://api.apiary.io"
5
6
  GET_REQUESTS_PATH = "/tests/get-requests"
6
7
  GET_RESULTS_PATH = "/tests/get-test-results"
7
8
  BLUEPRINT_PATH = "apiary.apib"
8
- TEST_SPEC_PATH = "tests.spec"
9
- OUTPUT = 'tap'
10
- TEST_URL = "127.0.0.1"
9
+ TEST_SPEC_PATHS = "tests.spec"
10
+ TEST_URL = "http://127.0.0.1"
11
+ OUTPUT = 'tap'
11
12
  end
12
13
  end
data/lib/okapi/output.rb CHANGED
@@ -4,7 +4,9 @@ module Apiary
4
4
  module Okapi
5
5
  class Output
6
6
  def self.get(output,resources, error)
7
- Apiary::Okapi::Outputs.const_get(output.to_s.capitalize).send(:new, resources, error).get()
7
+ output = Apiary::Okapi::Outputs.const_get(output.to_s.capitalize).send(:new, resources, error)
8
+ output.get()
9
+ output.status
8
10
  end
9
11
  end
10
12
  end
@@ -2,6 +2,9 @@ module Apiary
2
2
  module Okapi
3
3
  module Outputs
4
4
  class BaseOutput
5
+
6
+ attr_reader :status
7
+
5
8
  def initialize(resources, error)
6
9
  @resources = resources
7
10
  @error = error
@@ -9,15 +12,15 @@ module Apiary
9
12
  :count => 0,
10
13
  :give_up => false,
11
14
  }
15
+ @status = true
12
16
  get_results
13
17
  end
14
18
 
15
- def get
16
- p '------------------------------------------------------------------'
19
+ def get
17
20
  p @results[:count].to_s + ' tests'
18
21
  p @results[:give_up][:error].to_s if @results[:give_up]
19
22
  @results[:tests].each { |test|
20
- p '########################################'
23
+ p '-------------------------------------------------'
21
24
  p test[:test_no]
22
25
  p test[:description]
23
26
  if test[:pass]
@@ -30,11 +33,11 @@ module Apiary
30
33
  end
31
34
 
32
35
  def get_results
33
-
34
36
  @results[:count] = @resources.count
35
37
  @results[:tests] = []
36
38
  if @error
37
39
  @results[:give_up] = {:error => @error}
40
+ @status = false
38
41
  else
39
42
  counter = 0
40
43
  @resources.each { |res|
@@ -42,7 +45,7 @@ module Apiary
42
45
  test_res = {
43
46
  :test_no=> counter,
44
47
  :pass => (res.validation_result.status and res.response.validation_result.status),
45
- :description => res.method + ' ' + res.uri
48
+ :description => (res.method + ' ' + res.uri) + ((res.expanded_uri and res.uri != res.expanded_uri['url']) ? " (#{res.expanded_uri['url']}) " : '')
46
49
  }
47
50
  if not test_res[:pass]
48
51
  test_res[:exp] = {:request => {:pass => false}, :response => {:pass => false}}
@@ -50,12 +53,14 @@ module Apiary
50
53
  test_res[:exp][:request][:pass] = true
51
54
  else
52
55
  test_res[:exp][:request][:reasons] = get_fail_result(res.validation_result)
56
+ @status = false
53
57
  end
54
58
 
55
59
  if res.response.validation_result.status
56
60
  test_res[:exp][:response][:pass] = true
57
61
  else
58
62
  test_res[:exp][:response][:reasons] = get_fail_result(res.response.validation_result)
63
+ @status = false
59
64
  end
60
65
  end
61
66
  @results[:tests] << test_res
@@ -7,6 +7,11 @@ module Apiary
7
7
  class Tap < Apiary::Okapi::Outputs::BaseOutput
8
8
 
9
9
  def get
10
+ get_int
11
+ puts "\n\n"
12
+ end
13
+
14
+ def get_int
10
15
  puts "TAP version 13"
11
16
  puts "1..#{@results[:count].to_s}"
12
17
  if @results[:give_up]
@@ -23,7 +28,7 @@ module Apiary
23
28
  if not test[:pass]
24
29
  error_block(test)
25
30
  end
26
- }
31
+ }
27
32
  end
28
33
 
29
34
  def error_block(test)
@@ -5,34 +5,58 @@ module Apiary
5
5
  module Okapi
6
6
  class Parser
7
7
 
8
- attr_reader :data, :resources
8
+ attr_reader :data, :resources, :proces_all_bp_resources, :global_vars
9
9
 
10
10
  def initialize(spec_path)
11
+ if not File.exist? spec_path
12
+ raise Exception, "Test spec. file '#{spec_path}' not found"
13
+ end
11
14
  @data = read_file(spec_path)
15
+ @proces_all_bp_resources = false
12
16
  end
13
17
 
14
18
  def resources
15
- @resources ||= begin
16
- parse_data { |res|
17
- raise Exception, 'resource not defined' unless res["resource"]
18
- raise Exception, 'method not defined' unless res["method"]
19
-
20
- (@resources ||= []) << Apiary::Okapi::Resource.new(res["resource"], res["method"], res["params"])
21
- }
22
- @resources
23
- end
19
+ @resources ||= parse_data
24
20
  end
25
21
 
26
22
  def read_file(path)
27
23
  @data = []
28
- File.open(path).each do |line|
29
- @data << line if line.strip != ""
30
- end
24
+ File.open(path).each do |line|
25
+ @data << line if line.strip != ""
26
+ end
31
27
  @data
32
28
  end
33
29
 
30
+ def substituite_vars(local, global)
31
+ tmp = {}
32
+ global.each {|k,v|
33
+ tmp[k] = v
34
+ }
35
+ local.each {|k,v|
36
+ tmp[k] = v
37
+ }
38
+ tmp
39
+ end
40
+
34
41
  def parse_data
42
+ global_vars = {}
43
+ resources = []
35
44
  @data.each { |res|
45
+ if res.index('CONTINUE') == 0
46
+ @proces_all_bp_resources = true
47
+ next
48
+ end
49
+
50
+ if res.index('VARS') == 0
51
+ splited = res.split(' ',2)
52
+ begin
53
+ global_vars = JSON.parse splited[1] if splited[1] and splited[1] != ''
54
+ rescue Exception => e
55
+ raise Exception, "can not parse global parameters (#{e})"
56
+ end
57
+ next
58
+ end
59
+
36
60
  splited = res.split(' ',3)
37
61
 
38
62
  begin
@@ -40,13 +64,18 @@ module Apiary
40
64
  rescue Exception => e
41
65
  raise Exception, 'can not parse parameters for resource:' + res + "(#{e})"
42
66
  end
43
- out = {
44
- 'resource' => splited[1],
45
- 'method' => splited[0],
46
- 'params' => splited[2] || {}
47
- }
48
- yield out
67
+
68
+ if splited[1] and splited[1] != '' and splited[0] and splited[0] != ''
69
+ out = {
70
+ 'resource' => splited[1],
71
+ 'method' => splited[0],
72
+ 'params' => substituite_vars(splited[2] || {}, global_vars)
73
+ }
74
+ resources << out
75
+ end
49
76
  }
77
+ @global_vars = global_vars
78
+ resources
50
79
  end
51
80
 
52
81
  end
data/lib/okapi/test.rb CHANGED
@@ -5,31 +5,31 @@ module Apiary
5
5
  module Okapi
6
6
  class Test
7
7
  def initialize(blueprint_path, test_spec_path, test_url, output, apiary_url)
8
- @blueprint_path = blueprint_path || BLUEPRINT_PATH
9
- @test_spec_path = test_spec_path || TEST_SPEC_PATH
10
- @test_url = test_url || TEST_URL
11
- @output_format = output || OUTPUT
12
- @apiary_url = apiary_url || APIARY_URL
8
+ @blueprint_path = blueprint_path
9
+ @test_spec_path = test_spec_path
10
+ @test_url = test_url
11
+ @output_format = output
12
+ @apiary_url = apiary_url
13
13
  @req_path = GET_REQUESTS_PATH
14
14
  @res_path = GET_RESULTS_PATH
15
15
  @connector = Apiary::Okapi::ApiaryConnector.new(@apiary_url, @req_path, @res_path)
16
+ @proces_all_bp_resources = false
16
17
  @output = []
17
18
  @resources = []
18
- @error = nil
19
-
19
+ @error = nil
20
20
  end
21
21
 
22
22
  def run
23
23
  begin
24
24
  test()
25
25
  rescue Exception => e
26
+ p e
27
+ @resources = []
26
28
  @error = e
27
- end
28
-
29
+ end
29
30
  Apiary::Okapi::Output.get(@output_format, @resources, @error)
30
31
  end
31
32
 
32
-
33
33
  def test
34
34
  prepare()
35
35
  if not @resources.empty?
@@ -41,25 +41,30 @@ module Apiary
41
41
  end
42
42
 
43
43
  def prepare
44
- resources = parse_test_spec(@test_spec_path)
45
-
46
- data = get_requests_spec(resources)
44
+ @resources = []
45
+ parser = get_test_spec_parser(@test_spec_path)
46
+ resources = parser.resources
47
+ counter = 0
48
+
49
+ resources.each { |res|
50
+ counter += 1
51
+ raise Exception, "Rresource not defined for item #{counter.to_d} in #{@test_spec_path}" unless res["resource"]
52
+ raise Exception, "Method not defined for resource #{res["resource"].to_s} in #{@test_spec_path}" unless res["method"]
53
+ }
47
54
 
48
- raise Exception, 'Can not get request data from Apiary: ' + data[:error] || '' if data[:error] or data[:resp].code.to_i != 200
55
+ @proces_all_bp_resources = parser.proces_all_bp_resources
56
+ data = get_requests_spec(resources, parser.global_vars)
57
+
58
+ if data[:error] or data[:code] != 200
59
+ raise Exception, 'Can not get request data from Apiary: ' + data[:error] ? data[:error] : ''
60
+ end
49
61
 
50
62
  data[:data].each do |res|
51
63
  raise Exception, 'Resource error "' + res['error'] + '" for resource "' + res["method"] + ' ' + res["uri"] + '"' if res['error']
52
-
53
- resources.each do |resource|
54
- if resource.uri == res["uri"] and resource.method == res["method"]
55
- resource.expanded_uri = res["expandedUri"]
56
- resource.body = res["body"]
57
- resource.headers = res["headers"]
58
- break
59
- end
60
- end
64
+ @resources << Apiary::Okapi::Resource.new(res["uri"], res["method"], res["params"], res["expandedUri"], res["headers"], res["body"])
61
65
  end
62
- @resources = resources
66
+
67
+ @resources
63
68
  end
64
69
 
65
70
  def blueprint
@@ -88,9 +93,10 @@ module Apiary
88
93
  def evaluate
89
94
 
90
95
  data = @connector.get_results(@resources, blueprint)
91
-
92
- raise Exception, 'Can not get evaluation data from apiary' + data[:error] || '' if data[:resp].code.to_i != 200 or data[:error]
93
-
96
+ if data[:error] or data[:code] != 200
97
+ raise Exception, 'Can not get evaluation data from apiary: ' + data[:error] ? data[:error] : ''
98
+ end
99
+
94
100
  data[:data].each { |validation|
95
101
  @resources.each { |resource|
96
102
  if validation['resource']['uri'] == resource.uri and validation['resource']['method'] == resource.method
@@ -115,16 +121,19 @@ module Apiary
115
121
  }
116
122
  end
117
123
 
118
- def parse_test_spec(test_spec)
119
- Apiary::Okapi::Parser.new(test_spec).resources
124
+ def get_test_spec_parser(test_spec)
125
+ Apiary::Okapi::Parser.new(test_spec)
120
126
  end
121
127
 
122
128
  def parse_blueprint(blueprint_path)
129
+ if not File.exist? blueprint_path
130
+ raise Exception, "Blueprint file '#{blueprint_path}' not found"
131
+ end
123
132
  File.read(blueprint_path)
124
133
  end
125
134
 
126
- def get_requests_spec(resources)
127
- @connector.get_requests(resources, blueprint)
135
+ def get_requests_spec(resources, global_vars)
136
+ @connector.get_requests(resources, blueprint, @proces_all_bp_resources, global_vars)
128
137
  end
129
138
 
130
139
  end
data/lib/okapi/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module Apiary
3
3
  module Okapi
4
- VERSION = "0.0.6"
4
+ VERSION = "0.0.7"
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: okapi
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tu1ly
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-05-06 00:00:00 +02:00
18
+ date: 2013-05-07 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -62,6 +62,11 @@ files:
62
62
  - README.md
63
63
  - Rakefile
64
64
  - bin/okapi
65
+ - examples/apiary.apib
66
+ - examples/apiary.yaml
67
+ - examples/tests.spec
68
+ - examples/tests2.spec
69
+ - examples/tests_1.spec
65
70
  - lib/okapi.rb
66
71
  - lib/okapi/apiary_connector.rb
67
72
  - lib/okapi/cli.rb
@@ -77,9 +82,6 @@ files:
77
82
  - lib/okapi/test.rb
78
83
  - lib/okapi/version.rb
79
84
  - okapi.gemspec
80
- - test_files/apiary.apib
81
- - test_files/tests.spec
82
- - test_files/tests_1.spec
83
85
  has_rdoc: true
84
86
  homepage: http://apiary.io
85
87
  licenses: