OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
openproject/features/step_definitions/api_steps.rb

185 lines
7.3 KiB

#encoding: utf-8
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2013 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
require "benchmark"
When(/^I call the work_package\-api on project "(.*?)" requesting format "(.*?)" without any filters$/) do |project_name, format|
@project = Project.find(project_name)
@unfiltered_benchmark = Benchmark.measure("Unfiltered Results") do
visit api_v2_project_planning_elements_path(project_id: project_name, format: format)
end
end
Then(/^the json\-response should include (\d+) work package(s?)$/) do |number_of_wps, plural|
expect(work_package_names.size).to eql number_of_wps.to_i
end
Then(/^the json\-response should( not)? contain a work_package "(.*?)"$/) do |negation, work_package_name|
if negation
expect(work_package_names).not_to include work_package_name
else
expect(work_package_names).to include work_package_name
end
end
And(/^the json\-response for work_package "(.*?)" should have the type "(.*?)"$/) do |work_package_name, type_name|
work_package = lookup_work_package(work_package_name)
expect(work_package["planning_element_type"]["name"]).to eql type_name
end
And(/^the json\-response for work_package "(.*?)" should have the responsible "(.*?)"$/) do |work_package_name, responsible_name|
work_package = lookup_work_package(work_package_name)
expect(work_package["responsible"]["name"]).to eql responsible_name
end
Then(/^the json\-response for work_package "(.*?)" should have the due_date "(.*?)"$/) do |work_package_name, due_date|
work_package = lookup_work_package(work_package_name)
expect(work_package["due_date"]).to eql due_date.gsub('/','-') # normalize the date-format
end
And(/^the json\-response should say that "(.*?)" is parent of "(.*?)"$/) do |parent_name, child_name|
child = lookup_work_package(child_name)
expect(child["parent"]["subject"]).to eql parent_name
end
And(/^the json\-response should say that "(.*?)" has no parent$/) do |child_name|
child = child = lookup_work_package(child_name)
expect(child["parent"]).to be_nil
end
And(/^the json\-response should say that "(.*?)" has (\d+) child(ren)?$/) do |parent_name, nr_of_children,plural|
parent = child = lookup_work_package(parent_name)
expect(parent["children"].size).to eql nr_of_children.to_i
end
And(/^the work package "(.*?)" has the due_date "(.*?)"$/) do |work_package_name, due_date|
wp = WorkPackage.where(:subject => work_package_name).first
expect(wp.due_date).to eql Date.parse(due_date)
end
When(/^I call the work_package\-api on project "(.*?)" requesting format "(.*?)" filtering for status "(.*?)"$/) do |project_name, format, status_names|
statuses = Status.where(name: status_names.split(','))
get_filtered_json(project_name: project_name,
format: format,
filters: [:status_id],
operators: {status_id: "="},
values: {status_id: statuses.map(&:id)} )
end
Then(/^I call the work_package\-api on project "(.*?)" requesting format "(.*?)" filtering for type "(.*?)"$/) do |project_name, format, type_names|
types = Project.find_by_identifier(project_name).types.where(name: type_names.split(","))
get_filtered_json(project_name: project_name,
format: format,
filters: [:type_id],
operators: {type_id: "="},
values: {type_id: types.map(&:id)} )
end
When(/^I call the work_package\-api on project "(.*?)" requesting format "(.*?)" filtering for responsible "(.*?)"$/) do |project_name, format, responsible_names|
responsibles = User.where(login: responsible_names.split(','))
get_filtered_json(project_name: project_name,
format: format,
filters: [:responsible_id],
operators: {responsible_id: "="},
values: {responsible_id: responsibles.map(&:id)} )
end
And(/^I call the work_package\-api on project "(.*?)" at time "(.*?)" and filter for types "(.*?)"$/) do |project_name, at_time, type_names|
types = Project.find_by_identifier(project_name).types.where(name: type_names.split(','))
get_filtered_json(project_name: project_name,
format: 'json',
filters: [:type_id],
operators: {type_id: '='},
values: {type_id: types.map(&:id)},
at_time: DateTime.parse(at_time).to_i) # the api accepts the time as unix-timestamps(epoch)
end
And(/^there are (\d+) work packages of type "(.*?)" in project "(.*?)"$/) do |nr_of_wps, type_name, project_name|
project = Project.find_by_identifier(project_name)
type = project.types.find_by_name(type_name)
FactoryGirl.create_list(:work_package, nr_of_wps.to_i, project: project, type: type)
end
And(/^the time to get the unfiltered results should not exceed (\d+)\.(\d+)s$/) do |seconds,milliseconds|
puts "----Unfiltered Benchmark----"
puts @unfiltered_benchmark
@unfiltered_benchmark.total.should < "#{seconds}.#{milliseconds}".to_f
end
And(/^the time to get the filtered results should not exceed (\d+)\.(\d+)s$/) do |seconds, milliseconds|
puts "----Filtered Benchmark----"
puts @filtered_benchmark
@filtered_benchmark.total.should < "#{seconds}.#{milliseconds}".to_f
end
Then(/^the time to get the filtered results should be faster than the time to get the unfiltered results$/) do
@filtered_benchmark.total.should < @unfiltered_benchmark.total
end
def lookup_work_package(work_package_name)
work_package = decoded_json["planning_elements"].select {|wp| wp["subject"] == work_package_name}.first
end
def work_package_names
decoded_json["planning_elements"].map{|wp| wp["subject"]}
end
def decoded_json
@decoded_json ||= ActiveSupport::JSON.decode last_json
end
def last_json
page.source
end
def get_filtered_json(params)
@filtered_benchmark = Benchmark.measure("Filtered Results") do
visit api_v2_project_planning_elements_path(project_id: params[:project_name],
format: params[:format],
f: params[:filters],
op: params[:operators],
v: params[:values],
at_time: params[:at_time])
end
end