Class: TestTree

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/lti2_commons/test/test_jsonpath.rb

Constant Summary collapse

ARGV =
['', "--name", "test_print"]

Instance Method Summary collapse

Instance Method Details

#assert_first(jsonpath, expected_value = nil) ⇒ Object

asserts if singleton expected_value is provided, else prints result useful for preparing new tests



179
180
181
182
183
184
185
186
# File 'lib/lti2_commons/test/test_jsonpath.rb', line 179

def assert_first(jsonpath, expected_value=nil)
  try_result = JsonPath.new(jsonpath).on(@json_obj).first
  if expected_value
    assert_equal expected_value, try_result
  else
    puts "#{jsonpath}: #{try_result.inspect}"
  end
end

#assert_node(jsonpath, expected_value = nil) ⇒ Object

asserts if expected_value is provided, else prints result useful for preparing new tests



168
169
170
171
172
173
174
175
# File 'lib/lti2_commons/test/test_jsonpath.rb', line 168

def assert_node(jsonpath, expected_value=nil)
  try_result = JsonPath.new(jsonpath).on(@json_obj)
  if expected_value
    assert_equal expected_value, try_result
  else
    puts "#{jsonpath}: #{try_result.inspect}"
  end
end

#setupObject



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
# File 'lib/lti2_commons/test/test_jsonpath.rb', line 7

def setup
  @json_str =
<<PROXY
{
"@context" : [
    "http://www.imsglobal.org/imspurl/lti/v2/ctx/ToolProxy",
    "http://purl.org/blackboard/ctx/v1/iconStyle"
],
"@type" : "ToolProxy",
"@id" : "http://fabericious..com/ToolProxy/869e5ce5-214c-4e85-86c6-b99e8458a592",
"lti_version" : "LTI-2p0",
"tool_proxy_guid" : "869e5ce5-214c-4e85-86c6-b99e8458a592",
"tool_consumer_profile" : "http://lms.example.com/profile/b6ffa601-ce1d-4549-9ccf-145670a964d4",
"tool_profile" : {
  "lti_version" : "LTI-2p0",
  "product_instance" : {
    "guid" : "fd75124a-140e-470f-944c-114d2d92bb40",
    "product_info" : {
      "product_name" : {
        "default_value" : "Acme Assessments",
        "key" : "tool.name"
      },
      "description" : {
        "default_value" : "Acme Assessments provide an interactive test format.",
        "key" : "tool.description"
      },
      "product_version" : "10.3",
      "technical_description" : {
        "default_value" : "Support provided for all LTI 1 extensions as well as LTI 2",
        "key" : "tool.technical"
      },
      "product_family" : {
        "code" : "assessment-tool",
        "vendor" : {
          "code" : "acme.com",
          "name" : {
            "default_value" : "Acme",
            "key" : "tool.vendor.name"
          },
          "description" : {
            "default_value" : "Acme is a leading provider of interactive tools for education",
            "key" : "tool.vendor.description"
          },
          "website" : "http://acme.example.com",
          "timestamp" : "2012-04-05T09:08:16-04:00",
          "contact" : {
            "email" : "[email protected]"
          }
        }
      }
    },
    "support" : {
      "email" : "[email protected]"
    },
    "service_provider" : {
      "guid" : "18e7ea50-3d6d-4f6b-aff2-ed3ab577716c",
      "provider_name" : {
        "default_value" : "Acme Hosting",
        "key" : "service_provider.name"
      },
      "description" : {
        "default_value" : "Provider of high performance managed hosting environments",
        "key" : "service_provider.description"
      },
      "support" : {
        "email" : "[email protected]"
      },
      "timestamp" : "2012-04-05T09:08:16-04:00"
    }
  },
  "base_url_choice" : [
     { "default_base_url" : "http://acme.example.com",
       "secure_base_url" : "https://acme.example.com",
       "selector" : {
         "applies_to" : [
           "IconEndpoint",
           "MessageHandler"
         ]
       }
     }
   ],
  "resource_handler" : [
    {
      "name" : {
        "default_value" : "Acme Assessment",
        "key" : "assessment.resource.name"
      },
      "description" : {
        "default_value" : "An interactive assessment using the Acme scale.",
        "key" : "assessment.resource.description"
      },
      "message" : {
        "message_type" : "basic-lti-launch-request",
        "path" : "/handler/launchRequest",
        "capability" : [
          "Result.autocreate",
          "Result.sourcedGUID"
        ],
        "parameter" : [
          { "name" : "result_id",
            "variable" : "$Result.sourcedGUID"
          },
          { "name" : "discipline",
            "fixed" : "chemistry"
          }
        ]
      },
      "icon_info" : [
         {
           "default_location" : {
             "path" : "/images/bb/en/icon.png"
           },
           "key" : "iconStyle.default.path"
         },
         { "icon_style" : "BbListElementIcon",
           "default_location" : {
             "path" : "/images/bb/en/listElement.png"
           },
           "key" : "iconStyle.bb.listElement.path"
         },
         { "icon_style" : "BbPushButtonIcon",
           "default_location" : {
             "path" : "images/bb/en/pushButton.png"
           },
           "key" : "iconStyle.bb.pushButton.path"
         }
       ]
    }
  ]
},
"security_contract" : {
  "shared_secret" : "ThisIsASecret!",
  "tool_service" : [
    { "@type" : "RestServiceProfile",
      "service" : "http://lms.example.com/profile/b6ffa601-ce1d-4549-9ccf-145670a964d4#ToolProxy.collection",
      "action" : "POST"
    },
    { "@type" : "RestServiceProfile",
      "service" : "http://lms.example.com/profile/b6ffa601-ce1d-4549-9ccf-145670a964d4#ToolProxy.item",
      "action" : [
        "GET",
        "PUT"
      ]
    },
    { "@type" : "RestService",
      "service" : "http://lms.example.com/profile/b6ffa601-ce1d-4549-9ccf-145670a964d4#Result.item",
      "action" : [
        "GET",
        "PUT"
      ]
    }
  ]
}
}
PROXY

@json_obj = JSON.parse(@json_str)
end

#test_arraysObject



208
209
210
211
# File 'lib/lti2_commons/test/test_jsonpath.rb', line 208

def test_arrays
  assert_node "tool_profile.resource_handler[0].message.path", ["/handler/launchRequest"]
  assert_node 'tool_profile.resource_handler[0].message.message_type', ["basic-lti-launch-request"]
end

#test_basicsObject



200
201
202
203
204
205
206
# File 'lib/lti2_commons/test/test_jsonpath.rb', line 200

def test_basics
  assert_node 'tool_proxy_guid', ["869e5ce5-214c-4e85-86c6-b99e8458a592"]
  assert_node 'security_contract.shared_secret', ["ThisIsASecret!"]
  # 2 dots goes through array(s)
  assert_node 'tool_profile.base_url_choice..default_base_url', ["http://acme.example.com"]
  assert_node 'lti_version', ["LTI-2p0"]
end

#test_enumerateObject



235
236
237
238
239
240
# File 'lib/lti2_commons/test/test_jsonpath.rb', line 235

def test_enumerate
  enum = JsonPath.new("$..*")[@json_obj]
  counter = 0
  enum.each {|node| counter += 1}
  assert_equal 114, counter
end

#test_filterObject



227
228
229
230
231
232
233
# File 'lib/lti2_commons/test/test_jsonpath.rb', line 227

def test_filter
  assert_first 'security_contract.tool_service[0].action', "POST"
  assert_first 'security_contract.tool_service[?(@["action"]=="POST")]', 
    {"service"=>"http://lms.example.com/profile/b6ffa601-ce1d-4549-9ccf-145670a964d4#ToolProxy.collection", "action"=>"POST", "@type"=>"RestServiceProfile"}
  # assert_first 'security_contract.tool_service[?(@["action"]=="POST")]'
  assert_equal "POST", JsonPath.new('security_contract.tool_service[?(@["action"]=="POST")]').on(@json_obj).first['action']
end

#test_filter0Object



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/lti2_commons/test/test_jsonpath.rb', line 213

def test_filter0
  # resource_handler = JsonPath.new('tool_profile.resource_handler').on(@json_obj)
  # assert_node 'tool_profile.resource_handler[?(true)]'
  # assert_node 'tool_profile.resource_handler.[?(@["message"]["message_type"]=="basic-lti-launch-request")]'
  #find matching message_type and dig down to get message.path
  assert_equal "/handler/launchRequest", 
    JsonPath.new('tool_profile.resource_handler.[?(@["message"]["message_type"]=="basic-lti-launch-request")]').on(@json_obj).first['message']['path']

  # do the same replying JsonPath
  assert_equal "/handler/launchRequest", JsonPath.new('@..message..path').on(
    JsonPath.new('tool_profile.resource_handler.[?(@["message"]["message_type"]=="basic-lti-launch-request")]').on(@json_obj)).first

end

#test_firstObject



196
197
198
# File 'lib/lti2_commons/test/test_jsonpath.rb', line 196

def test_first
  assert_equal "869e5ce5-214c-4e85-86c6-b99e8458a592", JsonPath.new('tool_proxy_guid').on(@json_str).first
end

#test_path_on_jsonObject



188
189
190
191
192
193
194
# File 'lib/lti2_commons/test/test_jsonpath.rb', line 188

def test_path_on_json
  # Note that same result for JSON string or loaded JSON object 
  assert_equal ["869e5ce5-214c-4e85-86c6-b99e8458a592"], JsonPath.new('tool_proxy_guid').on(@json_str)
  assert_equal ["869e5ce5-214c-4e85-86c6-b99e8458a592"], JsonPath.new('tool_proxy_guid').on(@json_obj)
  # assert_node 'tool_proxy_guid'
  assert_node 'tool_proxy_guid', ["869e5ce5-214c-4e85-86c6-b99e8458a592"]
end

#test_printObject



247
248
249
250
251
# File 'lib/lti2_commons/test/test_jsonpath.rb', line 247

def test_print
  root = JsonPath.new("$").on(@json_str).first
  # puts root.to_json
  puts JSON.pretty_generate root
end