blob: dbeebf992fa1aff8c396e43f5e016ade7c389ebd (
plain)
1
2
3
4
5
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
module JSONMatchers
class SetJsonAttribute
def initialize(attributes)
@attributes = attributes
end
def format
@format ||= Puppet::Network::FormatHandler.format('pson')
end
def json(instance)
PSON.parse(instance.to_pson)
end
def attr_value(attrs, instance)
attrs = attrs.dup
hash = json(instance)['data']
while attrs.length > 0
name = attrs.shift
hash = hash[name]
end
hash
end
def to(value)
@value = value
self
end
def matches?(instance)
result = attr_value(@attributes, instance)
if @value
result == @value
else
! result.nil?
end
end
def failure_message_for_should(instance)
if @value
"expected #{instance.inspect} to set #{@attributes.inspect} to #{@value.inspect}; got #{attr_value(@attributes, instance).inspect}"
else
"expected #{instance.inspect} to set #{@attributes.inspect} but was nil"
end
end
def failure_message_for_should_not(instance)
if @value
"expected #{instance.inspect} not to set #{@attributes.inspect} to #{@value.inspect}"
else
"expected #{instance.inspect} not to set #{@attributes.inspect} to nil"
end
end
end
class SetJsonDocumentTypeTo
def initialize(type)
@type = type
end
def format
@format ||= Puppet::Network::FormatHandler.format('pson')
end
def matches?(instance)
json(instance)['document_type'] == @type
end
def json(instance)
PSON.parse(instance.to_pson)
end
def failure_message_for_should(instance)
"expected #{instance.inspect} to set document_type to #{@type.inspect}; got #{json(instance)['document_type'].inspect}"
end
def failure_message_for_should_not(instance)
"expected #{instance.inspect} not to set document_type to #{@type.inspect}"
end
end
class ReadJsonAttribute
def initialize(attribute)
@attribute = attribute
end
def format
@format ||= Puppet::Network::FormatHandler.format('pson')
end
def from(value)
@json = value
self
end
def as(as)
@value = as
self
end
def matches?(klass)
raise "Must specify json with 'from'" unless @json
@instance = format.intern(klass, @json)
if @value
@instance.send(@attribute) == @value
else
! @instance.send(@attribute).nil?
end
end
def failure_message_for_should(klass)
if @value
"expected #{klass} to read #{@attribute} from #{@json} as #{@value.inspect}; got #{@instance.send(@attribute).inspect}"
else
"expected #{klass} to read #{@attribute} from #{@json} but was nil"
end
end
def failure_message_for_should_not(klass)
if @value
"expected #{klass} not to set #{@attribute} to #{@value}"
else
"expected #{klass} not to set #{@attribute} to nil"
end
end
end
if !Puppet.features.microsoft_windows?
require 'json'
require 'json-schema'
class SchemaMatcher
JSON_META_SCHEMA = JSON.parse(File.read('api/schemas/json-meta-schema.json'))
def initialize(schema)
@schema = schema
end
def matches?(json)
JSON::Validator.validate!(JSON_META_SCHEMA, @schema)
JSON::Validator.validate!(@schema, json)
end
end
end
def validate_against(schema_file)
if Puppet.features.microsoft_windows?
pending("Schema checks cannot be done on windows because of json-schema problems")
else
schema = JSON.parse(File.read(schema_file))
SchemaMatcher.new(schema)
end
end
def set_json_attribute(*attributes)
SetJsonAttribute.new(attributes)
end
def set_json_document_type_to(type)
SetJsonDocumentTypeTo.new(type)
end
def read_json_attribute(attribute)
ReadJsonAttribute.new(attribute)
end
end
|