test/rest_api_test.rb in ardtweeno-0.2.5 vs test/rest_api_test.rb in ardtweeno-0.3.0

- old
+ new

@@ -1,62 +1,77 @@ +ENV['RACK_ENV'] = 'test' + require 'rubygems' # Require the REST API Sinatra app require File.expand_path(File.dirname(__FILE__) + "/../lib/ardtweeno/restapi.rb") require 'test/unit' require 'rack/test' require 'date' -ENV['RACK_ENV'] = 'test' - class RESTAPITest < Test::Unit::TestCase include Rack::Test::Methods def app RESTAPI end + attr_accessor :dispatch, :confdata def setup # Create a DateTime instance today = DateTime.now theDate = today.year.to_s() + "-" + "%02d" % today.month.to_s() + "-" + "%02d" % today.day.to_s() + @dispatcher = Ardtweeno::Dispatcher.instance + @nodeList = Array.new + + 5.times do |i| + @nodeList << Ardtweeno::Node.new("node#{i}", "abcdef#{i}") + end + + @dispatcher.nodeManager = Ardtweeno::NodeManager.new({:nodelist => @nodeList}) + + 3.times do |i| + @dispatcher.store('{"data":[23.5,997.5,65],"key":"abcdef1"}') + end + # Default values @date = theDate @hour = ("%02d" % today.hour).to_s @minute = ("%02d" % today.min).to_s end # Check root redirects to /home successfully def test_root get "/" - follow_redirect! - assert_equal("http://example.org/home", last_request.url) + assert_equal("http://example.org/", last_request.url) assert last_response.ok? end # Check manual create post page loads def test_create_post + postURI = @dispatcher.getPostsURI + # Check the form loads ok - get "/b97cb9ae44747ee263363463b7e56/create/post" + get "/#{postURI}/create/post" - assert_equal("http://example.org/b97cb9ae44747ee263363463b7e56/create/post", last_request.url) + assert_equal("http://example.org/#{postURI}/create/post", last_request.url) assert last_response.ok? # Add a post to the system - post "/b97cb9ae44747ee263363463b7e56/create/post", + post "/#{postURI}/create/post", params={"title"=>'Test Title', "content"=>'Test Content', "code"=>'Test Code'} follow_redirect! - assert_equal("http://example.org/home", last_request.url) + assert_equal("http://example.org/", last_request.url) assert last_response.ok? end @@ -74,16 +89,26 @@ get "/home" assert last_response.body.include?("Ardtweeno is an application gateway which bridges") assert last_response.ok? end + + + # Test /status + def test_status + get "/status" + + assert last_response.body.include?("System Status") + assert last_response.ok? + end # Test retrieval of zones def test_retrieve_zones get "/api/v1/zones", params={:zonename=>"testzone0", :key=>"1230aea77d7bd38898fec74a75a87738dea9f657"} json = JSON.parse(last_response.body) + assert_equal(1, json["found"]) assert_equal(2, json["total"]) get "/api/v1/zones", params={:zonename=>"testzonez", :key=>"1230aea77d7bd38898fec74a75a87738dea9f657"} json = JSON.parse(last_response.body) @@ -106,11 +131,10 @@ assert_equal(2, json["total"]) end - # Test retrieval of packets def test_retrieve_nodes get "/api/v1/nodes", params={:key=>"1230aea77d7bd38898fec74a75a87738dea9f657"} json = JSON.parse(last_response.body) assert_equal(5, json["found"]) @@ -172,31 +196,37 @@ end # Test post of packets def test_post_packets + + # Get initial number of packets in the system + get "/api/v1/packets", params={:node=>"node1", :key=>"1230aea77d7bd38898fec74a75a87738dea9f657"} + json = JSON.parse(last_response.body) + initialNo = json["total"].to_i + # Add a packet post "/api/v1/packets", params={:key=>"1230aea77d7bd38898fec74a75a87738dea9f657", :payload=>'{"key":"abcdef1", "data":[50.0]}'} assert last_response.ok? # Test to ensure it was added successfully get "/api/v1/packets", params={:node=>"node1", :key=>"1230aea77d7bd38898fec74a75a87738dea9f657"} json = JSON.parse(last_response.body) - assert_equal(1, json["found"]) - assert_equal(1, json["total"]) + assert_equal(initialNo + 1, json["found"]) + assert_equal(initialNo + 1, json["total"]) # Add another packet post "/api/v1/packets", params={:key=>"1230aea77d7bd38898fec74a75a87738dea9f657", :payload=>'{"key":"abcdef1", "data":[50.0]}'} assert last_response.ok? # Test to once again check if it was added successfully get "/api/v1/packets", params={:node=>"node1", :key=>"1230aea77d7bd38898fec74a75a87738dea9f657"} json = JSON.parse(last_response.body) - assert_equal(2, json["found"]) - assert_equal(2, json["total"]) + assert_equal(initialNo + 2, json["found"]) + assert_equal(initialNo + 2, json["total"]) end # Test the system start parser command @@ -234,13 +264,13 @@ end # Test the system status command def test_system_status - get "/api/v1/system/status", params={:key=>"1230aea77d7bd38898fec74a75a87738dea9f657"} + get "/api/v1/system/status" assert last_response.ok? - result = {:running=>@dispatcher.running?}.to_json + result = @dispatcher.status?.to_json assert_equal(result, last_response.body) end \ No newline at end of file