Class: Ape::SortingValidator

Inherits:
Validator show all
Defined in:
lib/ape/validators/sorting_validator.rb

Instance Attribute Summary

Attributes inherited from Validator

#authent, #reporter

Instance Method Summary collapse

Methods inherited from Validator

custom_validators, instance

Methods included from Util

extended, included

Methods included from ValidatorDsl

included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ape::Validator

Instance Method Details

#validate(opts = {}) ⇒ Object



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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ape/validators/sorting_validator.rb', line 6

def validate(opts = {})
  coll = opts[:entry_collection]
  reporter.info(self, "TESTING: Collection re-ordering after PUT.")
  
  # We'll post three mini entries to the collection
  poster = Poster.new(coll.href, @authent)
  ['One', 'Two', 'Three'].each do |num|
    sleep 2
    text = Samples.mini_entry.gsub('Mini-1', "Mini #{num}")
    name = "Posting Mini #{num}"
    worked = poster.post(Names::AtomEntryMediaType, text)
    reporter.save_dialog(name, poster)
    if !worked
      reporter.error(self, "Can't POST Mini #{name}: #{poster.last_error}", name)
      return
    end
  end

  # now let's grab the collection & check the order
  wanted = ['Mini One', 'Mini Two', 'Mini Three']
  two = nil
  entries = Feed.read(coll.href, 'Entries with multi-post', reporter)
  entries.each do |from_feed|
    want = wanted.pop
    unless from_feed.child_content('title').index(want)
      reporter.error(self, "Entries feed out of order after multi-post.")
      return
    end
    two = from_feed if want == 'Mini Two'
    break if wanted.empty?
  end
  reporter.success(self, "Entries correctly ordered after multi-post.")
  
  # let's update one of them; have to fetch it first to get the ETag
  link = two.link('edit', self)
  unless link
    reporter.error(self, "Can't check entry without edit link, entry id: #{two.get_child('id/text()')}")
    return
  end
  two_resp = check_resource(link, 'fetch two', Names::AtomMediaType, false)
  
  correctly_ordered = false
  if two_resp
    etag = two_resp.header 'etag'
      
    putter = Putter.new(link, @authent)
    putter.set_header('If-Match', etag)
  
    name = 'Updating mini-entry with PUT'
    sleep 2
    updated = two_resp.body.gsub('Mini Two', 'Mini-4')
    unless putter.put(Names::AtomEntryMediaType, updated)
      reporter.save_dialog(name, putter)
      reporter.error(self, "Can't update mini-entry at #{link}", name)
      return
    end
    # now the order should have changed
    wanted = ['Mini One', 'Mini Three', 'Mini-4']
    correctly_ordered = true
  else
    reporter.error(self, "Mini Two entry not received. Can't assure the correct order after update.")
    wanted = ['Mini One', 'Mini Two', 'Mini Three']
  end
  
  entries = Feed.read(coll.href, 'Entries post-update', reporter)
  entries.each do |from_feed|
    want = wanted.pop
    unless from_feed.child_content('title').index(want)
      reporter.error(self, "Entries feed out of order after update of multi-post.")
      return
    end
    
    # next to godliness
    delete_entry(from_feed)
    
    break if wanted.empty?
  end
  reporter.success(self, "Entries correctly ordered after update of multi-post.")  if correctly_ordered
  
end