Commit c4fa829dd0f278e917be723c5aa4f6c7c41eca2d
Exists in
colab
and in
4 other branches
Merge pull request #304 from mezuro/hotspot_metric_configuration_creation
Hotspot metric configuration creation
Showing
31 changed files
with
514 additions
and
416 deletions
Show diff stats
app/assets/javascripts/metric_collector.js.coffee
| 1 | 1 | class @MetricCollector |
| 2 | 2 | |
| 3 | 3 | # Static Method |
| 4 | - @choose_metric: (metric_code, metric_collector_name) -> | |
| 4 | + @choose_metric: (metric_code, metric_collector_name, action_path) -> | |
| 5 | 5 | $("#metric_code").val(metric_code) |
| 6 | 6 | $("#metric_collector_name").val(metric_collector_name) |
| 7 | + $("form").attr('action', action_path) | |
| 7 | 8 | $("form").submit() | ... | ... |
app/controllers/base_metric_configurations_controller.rb
| 1 | -include OwnershipAuthentication | |
| 2 | -include MetricConfigurationsConcern | |
| 3 | - | |
| 4 | 1 | class BaseMetricConfigurationsController < ApplicationController |
| 5 | - before_action :authenticate_user!, except: [:show, :index] | |
| 2 | + include OwnershipAuthentication | |
| 3 | + include MetricConfigurationsConcern | |
| 4 | + | |
| 5 | + before_action :authenticate_user!, except: [:show] | |
| 6 | 6 | before_action :metric_configuration_owner?, only: [:edit, :update, :destroy] |
| 7 | 7 | before_action :kalibro_configuration_owner?, only: [:new, :create, :choose_metric] |
| 8 | - before_action :set_metric_configuration, only: [:show, :edit, :update, :destroy] | |
| 8 | + before_action :set_kalibro_configuration! | |
| 9 | + before_action :find_metric_configuration!, only: [:show, :edit, :update, :destroy] | |
| 10 | + before_action :new_metric_configuration!, only: [:create] | |
| 11 | + before_action :set_metric!, only: [:create, :update] | |
| 12 | + before_action :set_reading_group!, only: [:show, :edit, :create, :update] | |
| 13 | + | |
| 14 | + def show | |
| 15 | + @kalibro_ranges = @metric_configuration.kalibro_ranges | |
| 16 | + end | |
| 9 | 17 | |
| 10 | 18 | def new |
| 11 | - update_metric_configuration(MetricConfiguration.new) | |
| 19 | + @metric_configuration = MetricConfiguration.new | |
| 12 | 20 | end |
| 13 | 21 | |
| 14 | - def show | |
| 15 | - if metric_configuration | |
| 16 | - @reading_group = ReadingGroup.find(metric_configuration.reading_group_id) | |
| 17 | - @kalibro_ranges = metric_configuration.kalibro_ranges | |
| 18 | - else | |
| 19 | - raise NotImplementedError | |
| 20 | - end | |
| 22 | + def edit | |
| 21 | 23 | end |
| 22 | 24 | |
| 23 | 25 | def create |
| 24 | - update_metric_configuration(MetricConfiguration.new(metric_configuration_params)) | |
| 26 | + respond_to { |format| save_and_redir(format) } | |
| 27 | + end | |
| 28 | + | |
| 29 | + def update | |
| 30 | + respond_to do |format| | |
| 31 | + save_and_redir(format) do |metric_configuration| | |
| 32 | + metric_configuration.update(metric_configuration_params) | |
| 33 | + end | |
| 34 | + end | |
| 35 | + end | |
| 36 | + | |
| 37 | + def destroy | |
| 38 | + @metric_configuration.destroy | |
| 39 | + clear_caches | |
| 40 | + | |
| 41 | + respond_to do |format| | |
| 42 | + format.html { redirect_to kalibro_configuration_path(@kalibro_configuration.id) } | |
| 43 | + format.json { head :no_content } | |
| 44 | + end | |
| 25 | 45 | end |
| 26 | 46 | |
| 27 | 47 | protected |
| 28 | 48 | |
| 29 | - def metric_configuration | |
| 30 | - raise NotImplementedError | |
| 49 | + def save_and_redir(format) | |
| 50 | + new_record = @metric_configuration.id.nil? | |
| 51 | + result = block_given? ? (yield @metric_configuration) : @metric_configuration.save | |
| 52 | + | |
| 53 | + if result | |
| 54 | + clear_caches | |
| 55 | + | |
| 56 | + format.html do | |
| 57 | + redirect_to kalibro_configuration_path(@kalibro_configuration.id), | |
| 58 | + notice: t(new_record ? 'successfully_created' : 'successfully_updated', | |
| 59 | + record: t(@metric_configuration.class)) | |
| 60 | + end | |
| 61 | + format.json { render json: @metric_configuration, status: new_record ? :created : :ok } | |
| 62 | + else | |
| 63 | + failed_action(format) | |
| 64 | + end | |
| 31 | 65 | end |
| 32 | 66 | |
| 33 | - def update_metric_configuration (new_metric_configuration) | |
| 34 | - raise NotImplementedError | |
| 67 | + def failed_action(format, error = nil) | |
| 68 | + errors = @metric_configuration.kalibro_errors | |
| 69 | + errors << error unless error.nil? | |
| 70 | + | |
| 71 | + flash[:notice] = errors.join(', ') | |
| 72 | + | |
| 73 | + format.json { render json: { errors: errors }, status: :unprocessable_entity } | |
| 74 | + render_failure_html(format) | |
| 75 | + end | |
| 76 | + | |
| 77 | + def render_failure_html(format) | |
| 78 | + if action_name == 'create' | |
| 79 | + format.html { render 'new' } | |
| 80 | + elsif action_name == 'update' | |
| 81 | + format.html { render 'edit' } | |
| 82 | + else | |
| 83 | + format.html { redirect_to kalibro_configuration_path(@kalibro_configuration.id) } | |
| 84 | + end | |
| 85 | + end | |
| 86 | + | |
| 87 | + def clear_caches | |
| 88 | + Rails.cache.delete("#{@kalibro_configuration.id}_tree_metric_configurations") | |
| 89 | + Rails.cache.delete("#{@kalibro_configuration.id}_hotspot_metric_configurations") | |
| 35 | 90 | end |
| 36 | 91 | |
| 37 | - # Never trust parameters from the scary internet, only allow the white list through. | |
| 38 | - # TODO: this should be refactored to the concern metric configuration | |
| 92 | + # Notice: If you add some logic to this method, remove the :nocov: below | |
| 93 | + # :nocov: | |
| 39 | 94 | def metric_configuration_params |
| 40 | - params[:metric_configuration] | |
| 95 | + raise NotImplementedError | |
| 96 | + end | |
| 97 | + # :nocov: | |
| 98 | + | |
| 99 | + def set_kalibro_configuration! | |
| 100 | + @kalibro_configuration = KalibroConfiguration.find params[:kalibro_configuration_id].to_i | |
| 101 | + end | |
| 102 | + | |
| 103 | + def new_metric_configuration! | |
| 104 | + @metric_configuration = MetricConfiguration.new metric_configuration_params | |
| 105 | + @metric_configuration.kalibro_configuration_id = @kalibro_configuration.id | |
| 106 | + end | |
| 107 | + | |
| 108 | + def find_metric_configuration! | |
| 109 | + @metric_configuration = MetricConfiguration.find params[:id].to_i | |
| 110 | + | |
| 111 | + # Make sure the metric configuration is really from the kalibro configuration we're being told it is | |
| 112 | + if @metric_configuration.kalibro_configuration_id != @kalibro_configuration.id | |
| 113 | + raise KalibroClient::Errors::RecordNotFound | |
| 114 | + end | |
| 115 | + end | |
| 116 | + | |
| 117 | + def set_metric! | |
| 118 | + collector = KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name(params[:metric_collector_name]) | |
| 119 | + # FIXME: Some view pass metric code as a parameter instead of metric name | |
| 120 | + if params.key?(:metric_code) | |
| 121 | + metric = collector.find_metric_by_code(params[:metric_code]) | |
| 122 | + else | |
| 123 | + metric = collector.find_metric_by_name(params[:metric_name]) | |
| 124 | + end | |
| 125 | + | |
| 126 | + if !metric.nil? && metric.type == self.metric_type | |
| 127 | + @metric_configuration.metric = metric | |
| 128 | + return | |
| 129 | + end | |
| 130 | + | |
| 131 | + respond_to do |format| | |
| 132 | + failed_action(format, t('invalid_metric_or_collector')) | |
| 133 | + end | |
| 134 | + end | |
| 135 | + | |
| 136 | + def set_reading_group! | |
| 137 | + begin | |
| 138 | + @reading_group = ReadingGroup.find(@metric_configuration.reading_group_id) | |
| 139 | + rescue KalibroClient::Errors::RecordNotFound | |
| 140 | + respond_to do |format| | |
| 141 | + failed_action(format, t('invalid_model', model: ReadingGroup.model_name.human)) | |
| 142 | + end | |
| 143 | + end | |
| 144 | + end | |
| 145 | + | |
| 146 | + # Notice: If you add some logic to this method, remove the :nocov: below | |
| 147 | + # :nocov: | |
| 148 | + def metric_type | |
| 149 | + raise NotImplementedError | |
| 41 | 150 | end |
| 151 | + # :nocov: | |
| 42 | 152 | end | ... | ... |
app/controllers/compound_metric_configurations_controller.rb
| 1 | 1 | class CompoundMetricConfigurationsController < BaseMetricConfigurationsController |
| 2 | 2 | before_action :set_metric_configurations, only: [:new, :edit] |
| 3 | 3 | |
| 4 | - def create | |
| 5 | - super | |
| 6 | - @compound_metric_configuration.metric.type = "CompoundMetricSnapshot" | |
| 7 | - respond_to do |format| | |
| 8 | - create_and_redir(format) | |
| 9 | - end | |
| 10 | - Rails.cache.delete("#{params[:kalibro_configuration_id].to_i}_tree_metric_configurations") | |
| 11 | - end | |
| 12 | - | |
| 13 | - def show | |
| 14 | - update_metric_configuration(@metric_configuration) | |
| 15 | - super | |
| 16 | - end | |
| 17 | - | |
| 18 | - def edit | |
| 19 | - @compound_metric_configuration = @metric_configuration | |
| 20 | - @kalibro_configuration_id = params[:kalibro_configuration_id].to_i | |
| 21 | - @compound_metric_configuration.kalibro_configuration_id = @kalibro_configuration_id | |
| 22 | - end | |
| 23 | - | |
| 24 | - def update | |
| 25 | - respond_to do |format| | |
| 26 | - edit | |
| 27 | - if @compound_metric_configuration.update(metric_configuration_params) | |
| 28 | - format.html { redirect_to kalibro_configuration_path(@compound_metric_configuration.kalibro_configuration_id), notice: t('successfully_updated', :record => t('compound') + " " + @compound_metric_configuration.class.model_name.human) } | |
| 29 | - format.json { head :no_content } | |
| 30 | - else | |
| 31 | - failed_action(format, 'edit') | |
| 32 | - end | |
| 33 | - Rails.cache.delete("#{@compound_metric_configuration.kalibro_configuration_id}_tree_metric_configurations") | |
| 34 | - end | |
| 35 | - end | |
| 36 | - | |
| 37 | 4 | protected |
| 38 | 5 | |
| 39 | - def metric_configuration | |
| 40 | - @compound_metric_configuration | |
| 6 | + def set_metric! | |
| 7 | + @metric_configuration.metric.type = self.metric_type | |
| 41 | 8 | end |
| 42 | 9 | |
| 43 | - def update_metric_configuration (new_metric_configuration) | |
| 44 | - @kalibro_configuration_id = params[:kalibro_configuration_id] | |
| 45 | - @compound_metric_configuration = new_metric_configuration | |
| 46 | - end | |
| 47 | - | |
| 48 | - private | |
| 49 | - | |
| 50 | - # Duplicated code on create and update actions extracted here | |
| 51 | - def failed_action(format, destiny_action) | |
| 52 | - @kalibro_configuration_id = params[:kalibro_configuration_id] | |
| 53 | - | |
| 54 | - set_metric_configurations | |
| 55 | - format.html { render action: destiny_action } | |
| 56 | - format.json { render json: @compound_metric_configuration.kalibro_errors, status: :unprocessable_entity } | |
| 57 | - end | |
| 58 | - | |
| 59 | - #Code extracted from create action | |
| 60 | - def create_and_redir(format) | |
| 61 | - if @compound_metric_configuration.save | |
| 62 | - format.html { redirect_to kalibro_configuration_path(@compound_metric_configuration.kalibro_configuration_id), notice: t('successfully_created', :record => t('compound') + " " + @compound_metric_configuration.class.model_name.human) } | |
| 63 | - else | |
| 64 | - failed_action(format, 'new') | |
| 65 | - end | |
| 10 | + def metric_configuration_params | |
| 11 | + params.require(:metric_configuration).permit(:reading_group_id, :weight, :metric => [:name, :description, :script, :scope, :code]) | |
| 66 | 12 | end |
| 67 | 13 | |
| 68 | 14 | def set_metric_configurations |
| 69 | - @metric_configurations = MetricConfiguration.metric_configurations_of(params[:kalibro_configuration_id].to_i) | |
| 15 | + @metric_configurations = MetricConfiguration.metric_configurations_of(@kalibro_configuration.id) | |
| 70 | 16 | end |
| 71 | 17 | |
| 18 | + def metric_type | |
| 19 | + 'CompoundMetricSnapshot' | |
| 20 | + end | |
| 72 | 21 | end | ... | ... |
app/controllers/hotspot_metric_configurations_controller.rb
0 → 100644
| ... | ... | @@ -0,0 +1,19 @@ |
| 1 | +class HotspotMetricConfigurationsController < BaseMetricConfigurationsController | |
| 2 | + skip_before_action :set_reading_group! | |
| 3 | + | |
| 4 | + def metric_configuration_params | |
| 5 | + # Hotspot metric configurations don't accept any parameters on creation | |
| 6 | + # But we must make that explicit as the method isn't implemented in the parent class | |
| 7 | + params.permit | |
| 8 | + end | |
| 9 | + | |
| 10 | + def render_failure_html(format) | |
| 11 | + format.html { redirect_to kalibro_configuration_path(@kalibro_configuration.id) } | |
| 12 | + end | |
| 13 | + | |
| 14 | + protected | |
| 15 | + | |
| 16 | + def metric_type | |
| 17 | + 'HotspotMetricSnapshot' | |
| 18 | + end | |
| 19 | +end | ... | ... |
app/controllers/metric_configurations_controller.rb
| 1 | 1 | class MetricConfigurationsController < BaseMetricConfigurationsController |
| 2 | - def choose_metric | |
| 3 | - @kalibro_configuration = KalibroConfiguration.find(params[:kalibro_configuration_id].to_i) | |
| 4 | - @metric_configuration_id = params[:metric_configuration_id].to_i | |
| 5 | - @metric_collectors_names = KalibroClient::Entities::Processor::MetricCollectorDetails.all_names | |
| 6 | - end | |
| 2 | + before_action :set_reading_groups!, only: [:new, :edit] | |
| 7 | 3 | |
| 8 | 4 | def new |
| 9 | 5 | super |
| 10 | - # FIXME: find_by_name throws an exception instead of returning nil, unlike ActiveRecord's API | |
| 11 | - metric_configuration.metric = KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name(params[:metric_collector_name]).find_metric_by_code params[:metric_code] | |
| 12 | - @reading_groups = ReadingGroup.public_or_owned_by_user(current_user).map { |reading_group| | |
| 13 | - [reading_group.name, reading_group.id] | |
| 14 | - } | |
| 15 | - end | |
| 16 | - | |
| 17 | - def create | |
| 18 | - super | |
| 19 | - @metric_configuration.metric = KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name(params[:metric_collector_name]).find_metric_by_name params[:metric_name] | |
| 20 | - respond_to do |format| | |
| 21 | - create_and_redir(format) | |
| 22 | - end | |
| 23 | - clear_caches | |
| 24 | - end | |
| 25 | - | |
| 26 | - def edit | |
| 27 | - # FIXME: set the configuration id just once! | |
| 28 | - @kalibro_configuration_id = params[:kalibro_configuration_id] | |
| 29 | - @metric_configuration.kalibro_configuration_id = @kalibro_configuration_id | |
| 30 | - @reading_groups = ReadingGroup.public_or_owned_by_user(current_user).map { |reading_group| | |
| 31 | - [reading_group.name, reading_group.id] | |
| 32 | - } | |
| 6 | + # The metric parameter comes from the choose metric form | |
| 7 | + set_metric! | |
| 33 | 8 | end |
| 34 | 9 | |
| 35 | - def update | |
| 36 | - respond_to do |format| | |
| 37 | - @metric_configuration.kalibro_configuration_id = params[:kalibro_configuration_id] | |
| 38 | - if @metric_configuration.update(metric_configuration_params) | |
| 39 | - format.html { redirect_to(kalibro_configuration_path(@metric_configuration.kalibro_configuration_id), notice: t('successfully_updated', :record => t(metric_configuration.class))) } | |
| 40 | - format.json { head :no_content } | |
| 41 | - clear_caches | |
| 42 | - else | |
| 43 | - failed_action(format, 'edit') | |
| 44 | - end | |
| 45 | - end | |
| 46 | - end | |
| 47 | - | |
| 48 | - def destroy | |
| 49 | - @metric_configuration.destroy | |
| 50 | - respond_to do |format| | |
| 51 | - format.html { redirect_to kalibro_configuration_path(params[:kalibro_configuration_id]) } | |
| 52 | - format.json { head :no_content } | |
| 53 | - end | |
| 54 | - clear_caches | |
| 10 | + def choose_metric | |
| 11 | + @metric_collectors_names = KalibroClient::Entities::Processor::MetricCollectorDetails.all_names | |
| 55 | 12 | end |
| 56 | 13 | |
| 57 | 14 | protected |
| 58 | 15 | |
| 59 | - def metric_configuration | |
| 60 | - @metric_configuration | |
| 16 | + def metric_configuration_params | |
| 17 | + params.require(:metric_configuration).permit(:reading_group_id, :weight, :aggregation_form) | |
| 61 | 18 | end |
| 62 | 19 | |
| 63 | - def update_metric_configuration (new_metric_configuration) | |
| 64 | - @kalibro_configuration_id = params[:kalibro_configuration_id] | |
| 65 | - @metric_configuration = new_metric_configuration | |
| 20 | + def metric_type | |
| 21 | + 'NativeMetricSnapshot' | |
| 66 | 22 | end |
| 67 | 23 | |
| 68 | 24 | private |
| 69 | 25 | |
| 70 | - def clear_caches | |
| 71 | - Rails.cache.delete("#{params[:kalibro_configuration_id]}_tree_metric_configurations") | |
| 72 | - Rails.cache.delete("#{params[:kalibro_configuration_id]}_hotspot_metric_configurations") | |
| 73 | - end | |
| 74 | - | |
| 75 | - # FIXME: Duplicated code on create and update actions extracted here | |
| 76 | - def failed_action(format, destiny_action) | |
| 77 | - @kalibro_configuration_id = params[:kalibro_configuration_id] | |
| 78 | - | |
| 79 | - format.html { render action: destiny_action } | |
| 80 | - format.json { render json: @metric_configuration.kalibro_errors, status: :unprocessable_entity } | |
| 81 | - end | |
| 82 | - | |
| 83 | - # Code extracted from create action | |
| 84 | - def create_and_redir(format) | |
| 85 | - if @metric_configuration.save | |
| 86 | - format.html { redirect_to kalibro_configuration_path(@metric_configuration.kalibro_configuration_id), notice: t('successfully_created', :record => t(metric_configuration.class)) } | |
| 87 | - else | |
| 88 | - failed_action(format, 'new') | |
| 26 | + def set_reading_groups! | |
| 27 | + @reading_groups = ReadingGroup.public_or_owned_by_user(current_user).map do |reading_group| | |
| 28 | + [reading_group.name, reading_group.id] | |
| 89 | 29 | end |
| 90 | 30 | end |
| 91 | 31 | end | ... | ... |
app/helpers/metric_configurations_helper.rb
| ... | ... | @@ -17,4 +17,16 @@ module MetricConfigurationsHelper |
| 17 | 17 | # find_by_name throws an exception instead of returning nil, unlike ActiveRecord's API |
| 18 | 18 | KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name(metric_collector_name).supported_metrics |
| 19 | 19 | end |
| 20 | + | |
| 21 | + def choose_metric_path(metric, kalibro_configuration_id) | |
| 22 | + if metric.type == 'HotspotMetricSnapshot' | |
| 23 | + kalibro_configuration_hotspot_metric_configurations_path(kalibro_configuration_id: kalibro_configuration_id) | |
| 24 | + else | |
| 25 | + kalibro_configuration_new_metric_configuration_path(kalibro_configuration_id: kalibro_configuration_id) | |
| 26 | + end | |
| 27 | + end | |
| 28 | + | |
| 29 | + def hotspot_metric_configuration?(metric_configuration) | |
| 30 | + metric_configuration.metric.type == 'HotspotMetricSnapshot' | |
| 31 | + end | |
| 20 | 32 | end | ... | ... |
app/views/compound_metric_configurations/_form.html.erb
| 1 | -<%= render :partial => 'shared/form_errors', :locals => {:object => @compound_metric_configuration} %> | |
| 1 | +<%= render :partial => 'shared/form_errors', :locals => {:object => @metric_configuration} %> | |
| 2 | 2 | <div class="row margin-left-none"> |
| 3 | 3 | <div class="form-table col-md-9"> |
| 4 | 4 | |
| 5 | 5 | <%= f.fields_for :metric do |metric| %> |
| 6 | - <%= render partial: "metric_options", :locals => {:f => metric, :metric => @compound_metric_configuration.metric} %> | |
| 6 | + <%= render partial: "metric_options", :locals => {:f => metric, :metric => @metric_configuration.metric} %> | |
| 7 | 7 | <% end %> |
| 8 | 8 | |
| 9 | 9 | <div class="form-row"> |
| ... | ... | @@ -18,10 +18,10 @@ |
| 18 | 18 | </div> |
| 19 | 19 | </div> |
| 20 | 20 | |
| 21 | - <% if @compound_metric_configuration.persisted? %> | |
| 22 | - <%= hidden_field_tag(:reading_group_id, @compound_metric_configuration.reading_group_id) %> | |
| 23 | - <%= hidden_field_tag(:kalibro_configuration_id, @compound_metric_configuration.kalibro_configuration_id) %> | |
| 24 | - <%= link_to t('back'), kalibro_configuration_path(@compound_metric_configuration.kalibro_configuration_id), class: 'btn btn-default' %> | |
| 21 | + <% if @metric_configuration.persisted? %> | |
| 22 | + <%= hidden_field_tag(:reading_group_id, @metric_configuration.reading_group_id) %> | |
| 23 | + <%= hidden_field_tag(:kalibro_configuration_id, @metric_configuration.kalibro_configuration_id) %> | |
| 24 | + <%= link_to t('back'), kalibro_configuration_path(@metric_configuration.kalibro_configuration_id), class: 'btn btn-default' %> | |
| 25 | 25 | <% else %> |
| 26 | 26 | <div class="form-row"> |
| 27 | 27 | <div class="field-container"> |
| ... | ... | @@ -34,8 +34,8 @@ |
| 34 | 34 | </p> |
| 35 | 35 | </div> |
| 36 | 36 | </div> |
| 37 | - <%= f.hidden_field(:kalibro_configuration_id, value: @kalibro_configuration_id) %> | |
| 38 | - <%= link_to t('back'), kalibro_configuration_path(@kalibro_configuration_id), class: 'btn btn-default' %> | |
| 37 | + <%= f.hidden_field(:kalibro_configuration_id, value: @kalibro_configuration.id) %> | |
| 38 | + <%= link_to t('back'), kalibro_configuration_path(@kalibro_configuration.id), class: 'btn btn-default' %> | |
| 39 | 39 | <% end %> |
| 40 | 40 | <%= f.submit t('save'), class: 'btn btn-primary' %> |
| 41 | 41 | </div> | ... | ... |
app/views/compound_metric_configurations/edit.html.erb
| ... | ... | @@ -6,7 +6,7 @@ |
| 6 | 6 | |
| 7 | 7 | <br> |
| 8 | 8 | |
| 9 | -<%= form_for(@compound_metric_configuration, :url => kalibro_configuration_compound_metric_configuration_update_path(@compound_metric_configuration.kalibro_configuration_id, @compound_metric_configuration.id), method: :put) do |f| %> | |
| 9 | +<%= form_for(@metric_configuration, :url => kalibro_configuration_compound_metric_configuration_update_path(@metric_configuration.kalibro_configuration_id, @metric_configuration.id), method: :put) do |f| %> | |
| 10 | 10 | <%= render partial: 'form', locals: {f: f} %> |
| 11 | 11 | <% end %> |
| 12 | 12 | ... | ... |
app/views/compound_metric_configurations/new.html.erb
| ... | ... | @@ -6,7 +6,7 @@ |
| 6 | 6 | |
| 7 | 7 | <br> |
| 8 | 8 | |
| 9 | -<%= form_for(@compound_metric_configuration, :url => kalibro_configuration_compound_metric_configurations_path(@kalibro_configuration_id)) do |f| %> | |
| 9 | +<%= form_for(@metric_configuration, :url => kalibro_configuration_compound_metric_configurations_path(@kalibro_configuration.id)) do |f| %> | |
| 10 | 10 | <%= render partial: 'form', locals: {f: f} %> |
| 11 | 11 | <% end %> |
| 12 | 12 | ... | ... |
app/views/compound_metric_configurations/show.html.erb
| 1 | 1 | <div class="page-header"> |
| 2 | - <h1><%=@compound_metric_configuration.metric.name %></h1> | |
| 2 | + <h1><%=@metric_configuration.metric.name %></h1> | |
| 3 | 3 | </div> |
| 4 | 4 | |
| 5 | 5 | <p> |
| 6 | 6 | <strong> <%= t('description') %>:</strong> |
| 7 | - <% if @compound_metric_configuration.metric.description.nil? %> | |
| 7 | + <% if @metric_configuration.metric.description.nil? %> | |
| 8 | 8 | <%= t('no_description') %> |
| 9 | 9 | <% else %> |
| 10 | - <%= @compound_metric_configuration.metric.description %> | |
| 10 | + <%= @metric_configuration.metric.description %> | |
| 11 | 11 | <% end %> |
| 12 | 12 | </p> |
| 13 | 13 | |
| 14 | 14 | <p> |
| 15 | 15 | <strong> <%= t('script') %>:</strong> |
| 16 | - <%= @compound_metric_configuration.metric.script %> | |
| 16 | + <%= @metric_configuration.metric.script %> | |
| 17 | 17 | </p> |
| 18 | 18 | |
| 19 | 19 | <p> |
| 20 | 20 | <strong> <%= t('scope') %>:</strong> |
| 21 | - <%= @compound_metric_configuration.metric.scope %> | |
| 21 | + <%= @metric_configuration.metric.scope %> | |
| 22 | 22 | </p> |
| 23 | 23 | |
| 24 | 24 | <p> |
| 25 | 25 | <strong> <%= t('code') %>:</strong> |
| 26 | - <%= @compound_metric_configuration.metric.code %> | |
| 26 | + <%= @metric_configuration.metric.code %> | |
| 27 | 27 | </p> |
| 28 | 28 | |
| 29 | 29 | <p> |
| 30 | 30 | <strong> <%= t('weight') %>:</strong> |
| 31 | - <%= @compound_metric_configuration.weight %> | |
| 31 | + <%= @metric_configuration.weight %> | |
| 32 | 32 | </p> |
| 33 | 33 | |
| 34 | 34 | <p> |
| 35 | 35 | <strong> <%= t('metric_configurations_aggregation') %>:</strong> |
| 36 | - <%= @compound_metric_configuration.aggregation_form %> | |
| 36 | + <%= @metric_configuration.aggregation_form %> | |
| 37 | 37 | </p> |
| 38 | 38 | |
| 39 | 39 | <p> |
| ... | ... | @@ -44,9 +44,9 @@ |
| 44 | 44 | <hr> |
| 45 | 45 | |
| 46 | 46 | <h2> <%= t('ranges') %> </h2> |
| 47 | -<% if kalibro_configuration_owner? @compound_metric_configuration.kalibro_configuration_id %> | |
| 48 | - <%= link_to t_action(:add, KalibroRange), kalibro_configuration_metric_configuration_new_kalibro_range_path(@compound_metric_configuration.kalibro_configuration_id, | |
| 49 | - @compound_metric_configuration.id), class: 'btn btn-info' %> | |
| 47 | +<% if kalibro_configuration_owner? @metric_configuration.kalibro_configuration_id %> | |
| 48 | + <%= link_to t_action(:add, KalibroRange), kalibro_configuration_metric_configuration_new_kalibro_range_path(@metric_configuration.kalibro_configuration_id, | |
| 49 | + @metric_configuration.id), class: 'btn btn-info' %> | |
| 50 | 50 | <% end %> |
| 51 | 51 | |
| 52 | 52 | <table class="table table-hover"> |
| ... | ... | @@ -69,10 +69,10 @@ |
| 69 | 69 | <hr> |
| 70 | 70 | |
| 71 | 71 | <p> |
| 72 | - <%= link_to t('back'), kalibro_configuration_path(@compound_metric_configuration.kalibro_configuration_id), class: 'btn btn-default' %> | |
| 73 | -<% if kalibro_configuration_owner? @compound_metric_configuration.kalibro_configuration_id %> | |
| 74 | - <%= link_to t_action(:destroy, MetricConfiguration), kalibro_configuration_metric_configuration_path(@compound_metric_configuration.kalibro_configuration_id, | |
| 75 | - @compound_metric_configuration.id), method: :delete, data: { confirm: t('want_destroy_metric_configuration') }, | |
| 72 | + <%= link_to t('back'), kalibro_configuration_path(@metric_configuration.kalibro_configuration_id), class: 'btn btn-default' %> | |
| 73 | +<% if kalibro_configuration_owner? @metric_configuration.kalibro_configuration_id %> | |
| 74 | + <%= link_to t_action(:destroy, MetricConfiguration), kalibro_configuration_metric_configuration_path(@metric_configuration.kalibro_configuration_id, | |
| 75 | + @metric_configuration.id), method: :delete, data: { confirm: t('want_destroy_metric_configuration') }, | |
| 76 | 76 | class: 'btn btn-danger' %> |
| 77 | 77 | <% end %> |
| 78 | 78 | </p> | ... | ... |
app/views/kalibro_configurations/_metric_configurations.html.erb
| ... | ... | @@ -8,13 +8,15 @@ |
| 8 | 8 | <%= link_to_show_page(metric_configuration, @kalibro_configuration.id) %> |
| 9 | 9 | </td> |
| 10 | 10 | <% if kalibro_configuration_owner? @kalibro_configuration.id %> |
| 11 | + <% unless hotspot_metric_configuration?(metric_configuration) %> | |
| 12 | + <td> | |
| 13 | + <%= link_to_edit_form(metric_configuration, @kalibro_configuration.id) %> | |
| 14 | + </td> | |
| 15 | + <% end %> | |
| 11 | 16 | <td> |
| 12 | - <%= link_to_edit_form(metric_configuration, @kalibro_configuration.id) %> | |
| 13 | - </td> | |
| 14 | - <td> | |
| 15 | - <%= link_to t_action(:destroy, MetricConfiguration), kalibro_configuration_metric_configuration_path(@kalibro_configuration.id, metric_configuration.id), | |
| 16 | - method: :delete, data: { confirm: t('want_destroy_metric_configuration') }, | |
| 17 | - class: 'btn btn-danger' %> | |
| 17 | + <%= link_to t_action(:destroy, MetricConfiguration), kalibro_configuration_metric_configuration_path(@kalibro_configuration.id, metric_configuration.id), | |
| 18 | + method: :delete, data: { confirm: t('want_destroy_metric_configuration') }, | |
| 19 | + class: 'btn btn-danger' %> | |
| 18 | 20 | </td> |
| 19 | 21 | <% end %> |
| 20 | 22 | </tr> | ... | ... |
app/views/metric_configurations/_form.html.erb
app/views/metric_configurations/choose_metric.html.erb
| ... | ... | @@ -12,7 +12,9 @@ |
| 12 | 12 | <h3 class="jquery-ui-accordion"><%= metric_collector_name %></h3> |
| 13 | 13 | <div> |
| 14 | 14 | <% supported_metrics_of(metric_collector_name).each do |code, metric| %> |
| 15 | - <%= link_to metric.name, '#', onclick: "MetricCollector.choose_metric(\"#{code}\", \"#{metric_collector_name}\");", remote: true %><br> | |
| 15 | + <%= link_to metric.name, "#", | |
| 16 | + onclick: "MetricCollector.choose_metric(\"#{code}\", \"#{metric_collector_name}\", \"#{choose_metric_path(metric, @kalibro_configuration.id)}\");", | |
| 17 | + remote: true %><br> | |
| 16 | 18 | <% end %> |
| 17 | 19 | </div> |
| 18 | 20 | <% end %> | ... | ... |
app/views/metric_configurations/edit.html.erb
| ... | ... | @@ -2,7 +2,7 @@ |
| 2 | 2 | <h1><%= t('editing_metric_configuration') %></h1> |
| 3 | 3 | </div> |
| 4 | 4 | |
| 5 | -<%= form_for(@metric_configuration, :url => kalibro_configuration_metric_configuration_update_path(@kalibro_configuration_id, @metric_configuration.id), method: :put) do |f| %> | |
| 5 | +<%= form_for(@metric_configuration, :url => kalibro_configuration_metric_configuration_update_path(@kalibro_configuration.id, @metric_configuration.id), method: :put) do |f| %> | |
| 6 | 6 | <%= render partial: 'form', locals: {f: f} %> |
| 7 | 7 | <div class="row margin-left-none" style="margin-top: 20px"> |
| 8 | 8 | <%= f.submit t('save'), class: 'btn btn-primary' %> | ... | ... |
app/views/metric_configurations/new.html.erb
| ... | ... | @@ -24,10 +24,10 @@ |
| 24 | 24 | |
| 25 | 25 | <br> |
| 26 | 26 | |
| 27 | -<%= form_for(@metric_configuration, :url => kalibro_configuration_metric_configurations_path(@kalibro_configuration_id)) do |f| %> | |
| 27 | +<%= form_for(@metric_configuration, :url => kalibro_configuration_metric_configurations_path(@kalibro_configuration.id)) do |f| %> | |
| 28 | 28 | <%= render partial: 'form', locals: {f: f} %> |
| 29 | 29 | <div class="row margin-left-none" style="margin-top: 20px"> |
| 30 | 30 | <%= f.submit t('save'), class: 'btn btn-primary' %> |
| 31 | - <%= link_to t('back'), kalibro_configuration_choose_metric_path(@kalibro_configuration_id), class: 'btn btn-default' %> | |
| 31 | + <%= link_to t('back'), kalibro_configuration_choose_metric_path(@kalibro_configuration.id), class: 'btn btn-default' %> | |
| 32 | 32 | </div> |
| 33 | 33 | <% end %> | ... | ... |
config/locales/views/metric_configurations/en.yml
| ... | ... | @@ -37,6 +37,8 @@ en: |
| 37 | 37 | reading_group_helper_html: "The %{href} associated with this metric." |
| 38 | 38 | no_models: "There are no %{model} yet!" |
| 39 | 39 | choose_metric: "Choose a metric from a Base Tool:" |
| 40 | + invalid_metric_or_collector: "Invalid combination of metric collector, name/code and type" | |
| 41 | + invalid_model: "Invalid %{model}" | |
| 40 | 42 | scopes: |
| 41 | 43 | METHOD: "Method" |
| 42 | 44 | CLASS: "Class" | ... | ... |
config/locales/views/metric_configurations/pt.yml
| ... | ... | @@ -41,6 +41,8 @@ pt: |
| 41 | 41 | reading_group_helper_html: "O %{href} associado com esta métrica." |
| 42 | 42 | no_models: "Não há %{model} ainda!" |
| 43 | 43 | choose_metric: "Escolha uma métrica de um Coletor:" |
| 44 | + invalid_metric_or_collector: "Combinação inválida de coletor de métricas, nome/código e tipo" | |
| 45 | + invalid_model: "%{model} inválido" | |
| 44 | 46 | scopes: |
| 45 | 47 | METHOD: "Método" |
| 46 | 48 | CLASS: "Classe" | ... | ... |
config/routes.rb
| ... | ... | @@ -32,6 +32,8 @@ Rails.application.routes.draw do |
| 32 | 32 | |
| 33 | 33 | resources :compound_metric_configurations, except: [:destroy, :update] |
| 34 | 34 | put '/compound_metric_configurations/:id' => 'compound_metric_configurations#update', as: :compound_metric_configuration_update |
| 35 | + | |
| 36 | + resources :hotspot_metric_configurations, only: [:create] | |
| 35 | 37 | end |
| 36 | 38 | |
| 37 | 39 | resources :reading_groups do | ... | ... |
features/compound_metric_configuration/create.feature
| ... | ... | @@ -23,7 +23,7 @@ Feature: Compound Metric Configuration Creation |
| 23 | 23 | And I set the select field "Scope" as "Class" |
| 24 | 24 | And I set the select field "Reading Group" as "Scholar" |
| 25 | 25 | And I press the Save button |
| 26 | - Then I should see "Compound Metric Configuration was successfully created." | |
| 26 | + Then I should see "Metric Configuration was successfully created." | |
| 27 | 27 | And I click the show link of "My Compound Metric" |
| 28 | 28 | Then I should see "My Compound Metric" |
| 29 | 29 | And I should see "mcm" |
| ... | ... | @@ -38,7 +38,6 @@ Feature: Compound Metric Configuration Creation |
| 38 | 38 | And I have another compound metric configuration with code "Another_Code" within the given mezuro configuration |
| 39 | 39 | And I am at the Sample Configuration page |
| 40 | 40 | And I click the Add Metric link |
| 41 | - And I take a picture of the page | |
| 42 | 41 | And I click the Compound Metric link |
| 43 | 42 | When I fill the Name field with "My Compound Metric" |
| 44 | 43 | And I fill the Description field with "Some description" | ... | ... |
features/compound_metric_configuration/edition.feature
| ... | ... | @@ -23,12 +23,12 @@ Feature: Compound Metric Configuration edition |
| 23 | 23 | And I have a sample tree metric configuration within the given mezuro configuration |
| 24 | 24 | And I have a sample compound metric configuration within the given mezuro configuration |
| 25 | 25 | And I am at the Sample Configuration page |
| 26 | - When I click the edit link of the Coumpound Metric | |
| 26 | + When I click the edit link of the Compound Metric | |
| 27 | 27 | And I fill the Script field with "Another javascript" |
| 28 | 28 | And I fill the Code field with "Another_code" |
| 29 | 29 | And I press the Save button |
| 30 | 30 | Then I should see "Another_code" |
| 31 | - And I should see "Compound Metric Configuration was successfully updated." | |
| 31 | + And I should see "Metric Configuration was successfully updated." | |
| 32 | 32 | |
| 33 | 33 | @kalibro_configuration_restart @javascript |
| 34 | 34 | Scenario: trying to edit with blank fields | ... | ... |
features/hotspot_metric_configuration/create.feature
| ... | ... | @@ -11,8 +11,6 @@ Feature: Hotspot Metric Configuration Creation |
| 11 | 11 | And I am at the Sample Configuration page |
| 12 | 12 | And I click the Add Metric link |
| 13 | 13 | And I click the "MetricFu" h3 |
| 14 | - And I click the Duplicate Code link | |
| 15 | - When I fill the Weight field with "2" | |
| 16 | - And I press the Save button | |
| 14 | + When I click the Duplicate Code link | |
| 17 | 15 | Then I should see "Hotspot Metrics" |
| 18 | 16 | And I should see "Duplicate Code" | ... | ... |
features/repository/show/hotspot_metric_results.feature
| ... | ... | @@ -12,8 +12,6 @@ Feature: Repository hotspot metric results |
| 12 | 12 | And I click the Add Metric link |
| 13 | 13 | And I click the "MetricFu" h3 |
| 14 | 14 | And I click the Duplicate Code link |
| 15 | - When I fill the Weight field with "2" | |
| 16 | - And I press the Save button | |
| 17 | 15 | Given I have a sample project |
| 18 | 16 | And I have a sample ruby repository within the sample project |
| 19 | 17 | And I start to process that repository | ... | ... |
features/repository/show/modules_tree.feature
features/step_definitions/compound_metric_configuration_steps.rb
| ... | ... | @@ -22,7 +22,7 @@ When(/^I visit the sample compound metric configuration edit page$/) do |
| 22 | 22 | visit edit_kalibro_configuration_compound_metric_configuration_path(kalibro_configuration_id: @compound_metric_configuration.kalibro_configuration_id, id: @compound_metric_configuration.id) |
| 23 | 23 | end |
| 24 | 24 | |
| 25 | -When(/^I click the edit link of the Coumpound Metric$/) do | |
| 25 | +When(/^I click the edit link of the Compound Metric$/) do | |
| 26 | 26 | page.find('tr', :text => @compound_metric_configuration.metric.name).click_link('Edit') |
| 27 | 27 | end |
| 28 | 28 | ... | ... |
spec/controllers/base_metric_configurations_controller_spec.rb
| ... | ... | @@ -1,157 +0,0 @@ |
| 1 | -require 'rails_helper' | |
| 2 | - | |
| 3 | -class CleanInheritsFromBaseMetricConfigurationsController < BaseMetricConfigurationsController | |
| 4 | - def metric_configuration; super; end | |
| 5 | - def update_metric_configuration (new_metric_configuration); super; end | |
| 6 | -end | |
| 7 | - | |
| 8 | -class InheritsFromBaseMetricConfigurationsController < BaseMetricConfigurationsController | |
| 9 | - def new | |
| 10 | - render :nothing => true | |
| 11 | - super | |
| 12 | - end | |
| 13 | - | |
| 14 | - def create | |
| 15 | - render :nothing => true | |
| 16 | - super | |
| 17 | - end | |
| 18 | - | |
| 19 | - def show | |
| 20 | - update_metric_configuration(@metric_configuration) | |
| 21 | - super | |
| 22 | - render :nothing => true | |
| 23 | - end | |
| 24 | - | |
| 25 | - def metric_configuration | |
| 26 | - @metric_configuration | |
| 27 | - end | |
| 28 | - | |
| 29 | - def update_metric_configuration (new_metric_configuration) | |
| 30 | - @metric_configuration = new_metric_configuration | |
| 31 | - end | |
| 32 | - | |
| 33 | - def kalibro_ranges | |
| 34 | - @kalibro_ranges | |
| 35 | - end | |
| 36 | - | |
| 37 | - def reading_group | |
| 38 | - @reading_group | |
| 39 | - end | |
| 40 | -end | |
| 41 | - | |
| 42 | - | |
| 43 | -describe InheritsFromBaseMetricConfigurationsController, :type => :controller do | |
| 44 | - let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) } | |
| 45 | - | |
| 46 | - before do | |
| 47 | - Rails.application.routes.draw do | |
| 48 | - resources :kalibro_configurations do | |
| 49 | - match '/metric_configurations/choose_metric' => 'metric_configurations#choose_metric', as: :choose_metric, :via => [:get] | |
| 50 | - resources :inherits_from_base_metric_configurations do | |
| 51 | - match '/metric_configurations/new' => 'metric_configurations#new', as: :new_metric_configuration, :via => [:post] | |
| 52 | - match '/metric_configurations/:id' => 'metric_configurations#update', as: :metric_configuration_update, :via => [:put] | |
| 53 | - end | |
| 54 | - end | |
| 55 | - end | |
| 56 | - end | |
| 57 | - | |
| 58 | - after do | |
| 59 | - Rails.application.reload_routes! | |
| 60 | - end | |
| 61 | - | |
| 62 | - describe 'new' do | |
| 63 | - before :each do | |
| 64 | - sign_in FactoryGirl.create(:user) | |
| 65 | - end | |
| 66 | - | |
| 67 | - context 'when the current user owns the kalibro configuration' do | |
| 68 | - let!(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) } | |
| 69 | - before :each do | |
| 70 | - subject.expects(:kalibro_configuration_owner?).returns true | |
| 71 | - get :new, kalibro_configuration_id: kalibro_configuration.id | |
| 72 | - end | |
| 73 | - | |
| 74 | - it { expect(metric_configuration).not_to be_nil } | |
| 75 | - it { is_expected.to respond_with(:success) } | |
| 76 | - end | |
| 77 | - | |
| 78 | - context "when the current user doesn't own the kalibro configuration" do | |
| 79 | - before :each do | |
| 80 | - get :new, kalibro_configuration_id: kalibro_configuration.id | |
| 81 | - end | |
| 82 | - | |
| 83 | - it { is_expected.to redirect_to(kalibro_configurations_url(id: kalibro_configuration.id)) } | |
| 84 | - it { is_expected.to respond_with(:redirect) } | |
| 85 | - end | |
| 86 | - end | |
| 87 | - | |
| 88 | - describe 'create' do | |
| 89 | - let!(:metric_configuration_params) { FactoryGirl.build(:metric_configuration, metric: FactoryGirl.build(:metric)).to_hash } | |
| 90 | - let(:metric_collector) { FactoryGirl.build(:metric_collector) } | |
| 91 | - | |
| 92 | - before :each do | |
| 93 | - sign_in FactoryGirl.create(:user) | |
| 94 | - end | |
| 95 | - | |
| 96 | - context 'when the current user owns the kalibro configuration' do | |
| 97 | - before :each do | |
| 98 | - subject.expects(:kalibro_configuration_owner?).returns true | |
| 99 | - end | |
| 100 | - | |
| 101 | - context 'with valid fields' do | |
| 102 | - before :each do | |
| 103 | - post :create, kalibro_configuration_id: kalibro_configuration.id, metric_configuration: metric_configuration_params, metric_collector_name: metric_collector.name | |
| 104 | - end | |
| 105 | - | |
| 106 | - it { expect(subject.metric_configuration).not_to be_nil } | |
| 107 | - it { is_expected.to respond_with(:success) } | |
| 108 | - end | |
| 109 | - end | |
| 110 | - end | |
| 111 | - | |
| 112 | - describe 'show' do | |
| 113 | - let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) } | |
| 114 | - let(:reading_group) { FactoryGirl.build(:reading_group, :with_id) } | |
| 115 | - let(:kalibro_range) { FactoryGirl.build(:kalibro_range) } | |
| 116 | - | |
| 117 | - context 'with a valid metric_configuration' do | |
| 118 | - before :each do | |
| 119 | - ReadingGroup.expects(:find).with(metric_configuration.reading_group_id).returns(reading_group) | |
| 120 | - MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) | |
| 121 | - metric_configuration.expects(:kalibro_ranges).returns([kalibro_range]) | |
| 122 | - | |
| 123 | - get :show, kalibro_configuration_id: metric_configuration.kalibro_configuration_id.to_s, id: metric_configuration.id | |
| 124 | - end | |
| 125 | - | |
| 126 | - it { expect(subject.kalibro_ranges).not_to be_nil} | |
| 127 | - it { expect(subject.reading_group).not_to be_nil } | |
| 128 | - end | |
| 129 | - | |
| 130 | - context 'with a invalid metric_configuration' do | |
| 131 | - before :each do | |
| 132 | - subject.expects(:metric_configuration).returns(false) | |
| 133 | - end | |
| 134 | - | |
| 135 | - it 'should raise a NotImplementedError' do | |
| 136 | - expect { subject.show }.to raise_error(NotImplementedError) | |
| 137 | - end | |
| 138 | - end | |
| 139 | - end | |
| 140 | - | |
| 141 | - | |
| 142 | - context 'with a inheritance without overrides methods' do | |
| 143 | - subject { CleanInheritsFromBaseMetricConfigurationsController.new } | |
| 144 | - | |
| 145 | - describe 'metric_configuration' do | |
| 146 | - it 'should raise a NotImplementedError' do | |
| 147 | - expect { subject.metric_configuration }.to raise_error(NotImplementedError) | |
| 148 | - end | |
| 149 | - end | |
| 150 | - | |
| 151 | - describe 'update_metric_configuration' do | |
| 152 | - it 'should raise a NotImplementedError' do | |
| 153 | - expect { subject.update_metric_configuration(nil) }.to raise_error(NotImplementedError) | |
| 154 | - end | |
| 155 | - end | |
| 156 | - end | |
| 157 | -end |
spec/controllers/compound_metric_configurations_controller_spec.rb
| 1 | 1 | require 'rails_helper' |
| 2 | 2 | |
| 3 | 3 | describe CompoundMetricConfigurationsController, :type => :controller do |
| 4 | + let(:reading_group) { FactoryGirl.build(:reading_group, :with_id) } | |
| 4 | 5 | let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) } |
| 5 | 6 | |
| 6 | 7 | describe 'new' do |
| ... | ... | @@ -9,10 +10,12 @@ describe CompoundMetricConfigurationsController, :type => :controller do |
| 9 | 10 | end |
| 10 | 11 | |
| 11 | 12 | context 'when the current user owns the kalibro configuration' do |
| 12 | - let!(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
| 13 | + let!(:metric_configuration) { FactoryGirl.build(:metric_configuration, reading_group_id: reading_group.id) } | |
| 13 | 14 | before :each do |
| 15 | + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns kalibro_configuration | |
| 14 | 16 | subject.expects(:kalibro_configuration_owner?).returns true |
| 15 | 17 | MetricConfiguration.expects(:metric_configurations_of).with(kalibro_configuration.id).returns([metric_configuration]) |
| 18 | + | |
| 16 | 19 | get :new, kalibro_configuration_id: kalibro_configuration.id |
| 17 | 20 | end |
| 18 | 21 | |
| ... | ... | @@ -31,24 +34,25 @@ describe CompoundMetricConfigurationsController, :type => :controller do |
| 31 | 34 | end |
| 32 | 35 | |
| 33 | 36 | describe 'create' do |
| 34 | - let!(:metric_configuration_params) { FactoryGirl.build(:metric_configuration).to_hash } | |
| 35 | - let!(:metric_params) { FactoryGirl.build(:metric).to_hash } | |
| 36 | - let(:compound_metric_configuration) { FactoryGirl.build(:compound_metric_configuration) } | |
| 37 | + let(:compound_metric_configuration) { FactoryGirl.build(:compound_metric_configuration, reading_group_id: reading_group.id) } | |
| 38 | + let!(:metric_configuration_params) { compound_metric_configuration.to_hash } | |
| 37 | 39 | |
| 38 | 40 | before do |
| 39 | 41 | sign_in FactoryGirl.create(:user) |
| 40 | - metric_configuration_params["metric"] = metric_params | |
| 41 | 42 | end |
| 42 | 43 | |
| 43 | 44 | context 'when the current user owns the reading group' do |
| 44 | 45 | before :each do |
| 46 | + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns kalibro_configuration | |
| 45 | 47 | subject.expects(:kalibro_configuration_owner?).returns true |
| 46 | - Rails.cache.expects(:delete).with("#{kalibro_configuration.id}_tree_metric_configurations") | |
| 48 | + ReadingGroup.expects(:find).with(reading_group.id).returns(reading_group) | |
| 47 | 49 | end |
| 48 | 50 | |
| 49 | 51 | context 'with valid fields' do |
| 50 | 52 | before :each do |
| 51 | 53 | MetricConfiguration.any_instance.expects(:save).returns(true) |
| 54 | + Rails.cache.expects(:delete).with("#{kalibro_configuration.id}_tree_metric_configurations") | |
| 55 | + Rails.cache.expects(:delete).with("#{kalibro_configuration.id}_hotspot_metric_configurations") | |
| 52 | 56 | |
| 53 | 57 | post :create, kalibro_configuration_id: kalibro_configuration.id, metric_configuration: metric_configuration_params |
| 54 | 58 | end |
| ... | ... | @@ -59,7 +63,6 @@ describe CompoundMetricConfigurationsController, :type => :controller do |
| 59 | 63 | context 'with invalid fields' do |
| 60 | 64 | before :each do |
| 61 | 65 | MetricConfiguration.any_instance.expects(:save).returns(false) |
| 62 | - MetricConfiguration.expects(:metric_configurations_of).with(kalibro_configuration.id).returns([compound_metric_configuration]) | |
| 63 | 66 | post :create, kalibro_configuration_id: kalibro_configuration.id, metric_configuration: metric_configuration_params |
| 64 | 67 | end |
| 65 | 68 | |
| ... | ... | @@ -69,12 +72,12 @@ describe CompoundMetricConfigurationsController, :type => :controller do |
| 69 | 72 | end |
| 70 | 73 | |
| 71 | 74 | describe 'show' do |
| 72 | - let(:compound_metric_configuration) { FactoryGirl.build(:compound_metric_configuration_with_id) } | |
| 73 | - let(:reading_group) { FactoryGirl.build(:reading_group, :with_id) } | |
| 75 | + let(:compound_metric_configuration) { FactoryGirl.build(:compound_metric_configuration, :with_id, reading_group_id: reading_group.id) } | |
| 74 | 76 | let(:kalibro_range) { FactoryGirl.build(:kalibro_range) } |
| 75 | 77 | |
| 76 | 78 | before :each do |
| 77 | - ReadingGroup.expects(:find).with(compound_metric_configuration.reading_group_id).returns(reading_group) | |
| 79 | + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns kalibro_configuration | |
| 80 | + ReadingGroup.expects(:find).with(reading_group.id).returns(reading_group) | |
| 78 | 81 | MetricConfiguration.expects(:find).with(compound_metric_configuration.id).returns(compound_metric_configuration) |
| 79 | 82 | compound_metric_configuration.expects(:kalibro_ranges).returns([kalibro_range]) |
| 80 | 83 | |
| ... | ... | @@ -85,7 +88,7 @@ describe CompoundMetricConfigurationsController, :type => :controller do |
| 85 | 88 | end |
| 86 | 89 | |
| 87 | 90 | describe 'edit' do |
| 88 | - let(:compound_metric_configuration) { FactoryGirl.build(:compound_metric_configuration_with_id) } | |
| 91 | + let(:compound_metric_configuration) { FactoryGirl.build(:compound_metric_configuration, :with_id, reading_group_id: reading_group.id) } | |
| 89 | 92 | |
| 90 | 93 | context 'with a User logged in' do |
| 91 | 94 | before do |
| ... | ... | @@ -94,9 +97,12 @@ describe CompoundMetricConfigurationsController, :type => :controller do |
| 94 | 97 | |
| 95 | 98 | context 'when the user owns the compound metric configuration' do |
| 96 | 99 | before :each do |
| 100 | + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns kalibro_configuration | |
| 97 | 101 | subject.expects(:metric_configuration_owner?).returns(true) |
| 102 | + ReadingGroup.expects(:find).with(reading_group.id).returns(reading_group) | |
| 98 | 103 | MetricConfiguration.expects(:find).with(compound_metric_configuration.id).returns(compound_metric_configuration) |
| 99 | 104 | MetricConfiguration.expects(:metric_configurations_of).with(kalibro_configuration.id).returns([compound_metric_configuration]) |
| 105 | + | |
| 100 | 106 | get :edit, id: compound_metric_configuration.id, kalibro_configuration_id: compound_metric_configuration.kalibro_configuration_id.to_s |
| 101 | 107 | end |
| 102 | 108 | |
| ... | ... | @@ -124,8 +130,15 @@ describe CompoundMetricConfigurationsController, :type => :controller do |
| 124 | 130 | end |
| 125 | 131 | |
| 126 | 132 | describe 'update' do |
| 127 | - let(:compound_metric_configuration) { FactoryGirl.build(:compound_metric_configuration_with_id) } | |
| 128 | - let(:metric_configuration_params) { Hash[FactoryGirl.attributes_for(:compound_metric_configuration_with_id).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers | |
| 133 | + let(:compound_metric_configuration) { FactoryGirl.build(:compound_metric_configuration, :with_id, reading_group_id: reading_group.id) } | |
| 134 | + # Exclude the parameters that come in the URL (as the ones in the body will always be ignored) | |
| 135 | + let(:metric_configuration_params) { compound_metric_configuration.to_hash.except('id', 'kalibro_configuration_id', 'type') } | |
| 136 | + # Exclude the reading group id since it is set beforehand and not in the update params | |
| 137 | + let(:update_params) do | |
| 138 | + params = metric_configuration_params.dup | |
| 139 | + params['metric'] = params['metric'].except('type') | |
| 140 | + params | |
| 141 | + end | |
| 129 | 142 | |
| 130 | 143 | context 'when the user is logged in' do |
| 131 | 144 | before do |
| ... | ... | @@ -134,14 +147,17 @@ describe CompoundMetricConfigurationsController, :type => :controller do |
| 134 | 147 | |
| 135 | 148 | context 'when user owns the metric configuration' do |
| 136 | 149 | before :each do |
| 150 | + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns kalibro_configuration | |
| 151 | + ReadingGroup.expects(:find).with(reading_group.id).returns(reading_group) | |
| 152 | + MetricConfiguration.expects(:find).with(compound_metric_configuration.id).returns(compound_metric_configuration) | |
| 137 | 153 | subject.expects(:metric_configuration_owner?).returns true |
| 138 | - Rails.cache.expects(:delete).with("#{kalibro_configuration.id}_tree_metric_configurations") | |
| 139 | 154 | end |
| 140 | 155 | |
| 141 | 156 | context 'with valid fields' do |
| 142 | 157 | before :each do |
| 143 | - MetricConfiguration.expects(:find).with(compound_metric_configuration.id).returns(compound_metric_configuration) | |
| 144 | - MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(true) | |
| 158 | + MetricConfiguration.any_instance.expects(:update).with(update_params).returns(true) | |
| 159 | + Rails.cache.expects(:delete).with("#{kalibro_configuration.id}_tree_metric_configurations") | |
| 160 | + Rails.cache.expects(:delete).with("#{kalibro_configuration.id}_hotspot_metric_configurations") | |
| 145 | 161 | |
| 146 | 162 | post :update, kalibro_configuration_id: compound_metric_configuration.kalibro_configuration_id, id: compound_metric_configuration.id, metric_configuration: metric_configuration_params |
| 147 | 163 | end |
| ... | ... | @@ -152,9 +168,7 @@ describe CompoundMetricConfigurationsController, :type => :controller do |
| 152 | 168 | |
| 153 | 169 | context 'with an invalid field' do |
| 154 | 170 | before :each do |
| 155 | - MetricConfiguration.expects(:find).with(compound_metric_configuration.id).returns(compound_metric_configuration) | |
| 156 | - MetricConfiguration.expects(:metric_configurations_of).with(kalibro_configuration.id).returns([compound_metric_configuration]) | |
| 157 | - MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(false) | |
| 171 | + MetricConfiguration.any_instance.expects(:update).with(update_params).returns(false) | |
| 158 | 172 | |
| 159 | 173 | post :update, kalibro_configuration_id: compound_metric_configuration.kalibro_configuration_id, id: compound_metric_configuration.id, metric_configuration: metric_configuration_params |
| 160 | 174 | end | ... | ... |
spec/controllers/hotspot_metric_configuration_controller_spec.rb
0 → 100644
| ... | ... | @@ -0,0 +1,46 @@ |
| 1 | +require 'rails_helper' | |
| 2 | + | |
| 3 | +describe HotspotMetricConfigurationsController, :type => :controller do | |
| 4 | + let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) } | |
| 5 | + | |
| 6 | + describe 'create' do | |
| 7 | + let!(:metric_configuration) { FactoryGirl.build(:hotspot_metric_configuration) } | |
| 8 | + let(:metric_configuration_params) { metric_configuration.to_hash } | |
| 9 | + let(:metric_collector) { FactoryGirl.build(:metric_collector) } | |
| 10 | + | |
| 11 | + before do | |
| 12 | + sign_in FactoryGirl.create(:user) | |
| 13 | + end | |
| 14 | + | |
| 15 | + context 'when the current user owns the metric configuration' do | |
| 16 | + before :each do | |
| 17 | + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns kalibro_configuration | |
| 18 | + subject.expects(:kalibro_configuration_owner?).returns true | |
| 19 | + end | |
| 20 | + | |
| 21 | + context 'with valid fields' do | |
| 22 | + before :each do | |
| 23 | + MetricConfiguration.any_instance.expects(:save).returns(true) | |
| 24 | + KalibroClient::Entities::Processor::MetricCollectorDetails.expects(:find_by_name).with(metric_collector.name).returns(metric_collector) | |
| 25 | + metric_collector.expects(:find_metric_by_code).with(metric_configuration.metric.code).returns(metric_configuration.metric) | |
| 26 | + | |
| 27 | + post :create, kalibro_configuration_id: kalibro_configuration.id, metric_configuration: metric_configuration_params, metric_collector_name: metric_collector.name, metric_code: metric_configuration.metric.code | |
| 28 | + end | |
| 29 | + | |
| 30 | + it { is_expected.to respond_with(:redirect) } | |
| 31 | + end | |
| 32 | + | |
| 33 | + context 'with invalid fields' do | |
| 34 | + before :each do | |
| 35 | + MetricConfiguration.any_instance.expects(:save).returns(false) | |
| 36 | + KalibroClient::Entities::Processor::MetricCollectorDetails.expects(:find_by_name).with(metric_collector.name).returns(metric_collector) | |
| 37 | + metric_collector.expects(:find_metric_by_code).with(metric_configuration.metric.code).returns(metric_configuration.metric) | |
| 38 | + | |
| 39 | + post :create, kalibro_configuration_id: kalibro_configuration.id, metric_configuration: metric_configuration_params, metric_collector_name: metric_collector.name, metric_code: metric_configuration.metric.code | |
| 40 | + end | |
| 41 | + | |
| 42 | + it { is_expected.to respond_with(:redirect) } | |
| 43 | + end | |
| 44 | + end | |
| 45 | + end | |
| 46 | +end | ... | ... |
spec/controllers/metric_configurations_controller_spec.rb
| 1 | 1 | require 'rails_helper' |
| 2 | 2 | |
| 3 | 3 | describe MetricConfigurationsController, :type => :controller do |
| 4 | + let(:reading_group) { FactoryGirl.build(:reading_group, :with_id) } | |
| 4 | 5 | let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) } |
| 6 | + | |
| 5 | 7 | describe 'choose_metric' do |
| 6 | 8 | let(:metric_collector) { FactoryGirl.build(:metric_collector) } |
| 7 | 9 | before :each do |
| ... | ... | @@ -10,9 +12,9 @@ describe MetricConfigurationsController, :type => :controller do |
| 10 | 12 | |
| 11 | 13 | context 'when adding new metrics' do |
| 12 | 14 | before :each do |
| 15 | + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns kalibro_configuration | |
| 13 | 16 | subject.expects(:kalibro_configuration_owner?).returns true |
| 14 | 17 | KalibroClient::Entities::Processor::MetricCollectorDetails.expects(:all_names).returns([metric_collector]) |
| 15 | - KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns(kalibro_configuration) | |
| 16 | 18 | get :choose_metric, kalibro_configuration_id: kalibro_configuration.id |
| 17 | 19 | end |
| 18 | 20 | |
| ... | ... | @@ -33,11 +35,13 @@ describe MetricConfigurationsController, :type => :controller do |
| 33 | 35 | |
| 34 | 36 | context 'when the current user owns the kalibro configuration' do |
| 35 | 37 | before :each do |
| 38 | + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns kalibro_configuration | |
| 36 | 39 | ReadingGroup.expects(:public_or_owned_by_user).with(user).returns(reading_groups) |
| 37 | 40 | subject.expects(:kalibro_configuration_owner?).returns true |
| 38 | 41 | KalibroClient::Entities::Processor::MetricCollectorDetails.expects(:find_by_name).with(metric_collector.name).returns(metric_collector) |
| 39 | - metric_collector.expects(:find_metric_by_code).with(native_metric.code).returns(native_metric) | |
| 40 | - post :new, kalibro_configuration_id: kalibro_configuration.id, metric_code: native_metric.code, metric_collector_name: metric_collector.name | |
| 42 | + metric_collector.expects(:find_metric_by_name).with(native_metric.name).returns(native_metric) | |
| 43 | + | |
| 44 | + get :new, kalibro_configuration_id: kalibro_configuration.id, metric_name: native_metric.name, metric_collector_name: metric_collector.name | |
| 41 | 45 | end |
| 42 | 46 | |
| 43 | 47 | it { is_expected.to respond_with(:success) } |
| ... | ... | @@ -55,7 +59,7 @@ describe MetricConfigurationsController, :type => :controller do |
| 55 | 59 | end |
| 56 | 60 | |
| 57 | 61 | describe 'create' do |
| 58 | - let!(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
| 62 | + let!(:metric_configuration) { FactoryGirl.build(:metric_configuration, reading_group_id: reading_group.id) } | |
| 59 | 63 | let(:metric_configuration_params) { metric_configuration.to_hash } |
| 60 | 64 | let(:metric_collector) { FactoryGirl.build(:metric_collector) } |
| 61 | 65 | |
| ... | ... | @@ -65,26 +69,44 @@ describe MetricConfigurationsController, :type => :controller do |
| 65 | 69 | |
| 66 | 70 | context 'when the current user owns the metric configuration' do |
| 67 | 71 | before :each do |
| 72 | + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns kalibro_configuration | |
| 68 | 73 | subject.expects(:kalibro_configuration_owner?).returns true |
| 69 | 74 | end |
| 70 | 75 | |
| 71 | - context 'with valid fields' do | |
| 76 | + context 'with valid metric' do | |
| 72 | 77 | before :each do |
| 73 | - MetricConfiguration.any_instance.expects(:save).returns(true) | |
| 74 | 78 | KalibroClient::Entities::Processor::MetricCollectorDetails.expects(:find_by_name).with(metric_collector.name).returns(metric_collector) |
| 75 | 79 | metric_collector.expects(:find_metric_by_name).with(metric_configuration.metric.name).returns(metric_configuration.metric) |
| 80 | + ReadingGroup.expects(:find).with(reading_group.id).returns(reading_group) | |
| 81 | + end | |
| 76 | 82 | |
| 77 | - post :create, kalibro_configuration_id: kalibro_configuration.id, metric_configuration: metric_configuration_params, metric_collector_name: metric_collector.name, metric_name: metric_configuration.metric.name | |
| 83 | + context 'with valid fields' do | |
| 84 | + before :each do | |
| 85 | + MetricConfiguration.any_instance.expects(:save).returns(true) | |
| 86 | + | |
| 87 | + post :create, kalibro_configuration_id: kalibro_configuration.id, metric_configuration: metric_configuration_params, metric_collector_name: metric_collector.name, metric_name: metric_configuration.metric.name | |
| 88 | + end | |
| 89 | + | |
| 90 | + it { is_expected.to respond_with(:redirect) } | |
| 78 | 91 | end |
| 79 | 92 | |
| 80 | - it { is_expected.to respond_with(:redirect) } | |
| 93 | + context 'with invalid fields' do | |
| 94 | + before :each do | |
| 95 | + MetricConfiguration.any_instance.expects(:save).returns(false) | |
| 96 | + | |
| 97 | + post :create, kalibro_configuration_id: kalibro_configuration.id, metric_configuration: metric_configuration_params, metric_collector_name: metric_collector.name, metric_name: metric_configuration.metric.name | |
| 98 | + end | |
| 99 | + | |
| 100 | + it { is_expected.to render_template(:new) } | |
| 101 | + end | |
| 81 | 102 | end |
| 82 | 103 | |
| 83 | - context 'with invalid fields' do | |
| 104 | + context 'with invalid metric collector, metric or metric type' do | |
| 105 | + let(:invalid_metric) { FactoryGirl.build(:hotspot_metric) } | |
| 106 | + | |
| 84 | 107 | before :each do |
| 85 | - MetricConfiguration.any_instance.expects(:save).returns(false) | |
| 86 | 108 | KalibroClient::Entities::Processor::MetricCollectorDetails.expects(:find_by_name).with(metric_collector.name).returns(metric_collector) |
| 87 | - metric_collector.expects(:find_metric_by_name).with(metric_configuration.metric.name).returns(metric_configuration.metric) | |
| 109 | + metric_collector.expects(:find_metric_by_name).with(metric_configuration.metric.name).returns(invalid_metric) | |
| 88 | 110 | |
| 89 | 111 | post :create, kalibro_configuration_id: kalibro_configuration.id, metric_configuration: metric_configuration_params, metric_collector_name: metric_collector.name, metric_name: metric_configuration.metric.name |
| 90 | 112 | end |
| ... | ... | @@ -95,23 +117,59 @@ describe MetricConfigurationsController, :type => :controller do |
| 95 | 117 | end |
| 96 | 118 | |
| 97 | 119 | describe 'show' do |
| 98 | - let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) } | |
| 99 | - let(:reading_group) { FactoryGirl.build(:reading_group, :with_id) } | |
| 120 | + let(:metric_configuration) { FactoryGirl.build(:metric_configuration, :with_id, reading_group_id: reading_group.id) } | |
| 100 | 121 | let(:kalibro_range) { FactoryGirl.build(:kalibro_range) } |
| 101 | 122 | |
| 102 | 123 | before :each do |
| 103 | - ReadingGroup.expects(:find).with(metric_configuration.reading_group_id).returns(reading_group) | |
| 104 | - MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) | |
| 105 | - metric_configuration.expects(:kalibro_ranges).returns([kalibro_range]) | |
| 124 | + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns kalibro_configuration | |
| 125 | + end | |
| 126 | + | |
| 127 | + context 'with a valid metric configuration instance' do | |
| 128 | + before :each do | |
| 129 | + MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) | |
| 130 | + end | |
| 131 | + | |
| 132 | + context 'with valid parameters' do | |
| 133 | + before :each do | |
| 134 | + ReadingGroup.expects(:find).with(metric_configuration.reading_group_id).returns(reading_group) | |
| 135 | + metric_configuration.expects(:kalibro_ranges).returns([kalibro_range]) | |
| 136 | + | |
| 137 | + get :show, kalibro_configuration_id: metric_configuration.kalibro_configuration_id.to_s, id: metric_configuration.id | |
| 138 | + end | |
| 139 | + | |
| 140 | + it { is_expected.to render_template(:show) } | |
| 141 | + end | |
| 142 | + | |
| 143 | + context 'with invalid parameters' do | |
| 144 | + before :each do | |
| 145 | + ReadingGroup.expects(:find).with(metric_configuration.reading_group_id).raises(KalibroClient::Errors::RecordNotFound) | |
| 106 | 146 | |
| 107 | - get :show, kalibro_configuration_id: metric_configuration.kalibro_configuration_id.to_s, id: metric_configuration.id | |
| 147 | + get :show, kalibro_configuration_id: metric_configuration.kalibro_configuration_id.to_s, id: metric_configuration.id | |
| 148 | + end | |
| 149 | + | |
| 150 | + it { is_expected.to redirect_to(kalibro_configuration_path(kalibro_configuration.id)) } | |
| 151 | + end | |
| 108 | 152 | end |
| 109 | 153 | |
| 110 | - it { is_expected.to render_template(:show) } | |
| 154 | + context 'with an invalid metric configuration instance' do | |
| 155 | + let!(:invalid_metric_configuration) { FactoryGirl.build(:metric_configuration, reading_group_id: reading_group.id, kalibro_configuration_id: metric_configuration.kalibro_configuration_id + 1) } | |
| 156 | + | |
| 157 | + before :each do | |
| 158 | + MetricConfiguration.expects(:find).with(metric_configuration.id).returns(invalid_metric_configuration) | |
| 159 | + end | |
| 160 | + | |
| 161 | + context 'with valid parameters' do | |
| 162 | + before :each do | |
| 163 | + get :show, kalibro_configuration_id: metric_configuration.kalibro_configuration_id.to_s, id: metric_configuration.id | |
| 164 | + end | |
| 165 | + | |
| 166 | + it { is_expected.to respond_with(:not_found) } | |
| 167 | + end | |
| 168 | + end | |
| 111 | 169 | end |
| 112 | 170 | |
| 113 | 171 | describe 'edit' do |
| 114 | - let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) } | |
| 172 | + let(:metric_configuration) { FactoryGirl.build(:metric_configuration, :with_id, reading_group_id: reading_group.id) } | |
| 115 | 173 | let!(:reading_groups) { [FactoryGirl.build(:reading_group)] } |
| 116 | 174 | let!(:user) { FactoryGirl.create(:user) } |
| 117 | 175 | |
| ... | ... | @@ -122,9 +180,12 @@ describe MetricConfigurationsController, :type => :controller do |
| 122 | 180 | |
| 123 | 181 | context 'when the user owns the metric configuration' do |
| 124 | 182 | before :each do |
| 183 | + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns kalibro_configuration | |
| 184 | + ReadingGroup.expects(:find).with(reading_group.id).returns(reading_group) | |
| 125 | 185 | ReadingGroup.expects(:public_or_owned_by_user).with(user).returns(reading_groups) |
| 126 | 186 | subject.expects(:metric_configuration_owner?).returns(true) |
| 127 | 187 | MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) |
| 188 | + | |
| 128 | 189 | get :edit, id: metric_configuration.id, kalibro_configuration_id: metric_configuration.kalibro_configuration_id.to_s |
| 129 | 190 | end |
| 130 | 191 | |
| ... | ... | @@ -132,12 +193,11 @@ describe MetricConfigurationsController, :type => :controller do |
| 132 | 193 | end |
| 133 | 194 | |
| 134 | 195 | context 'when the user does not own the metric configuration' do |
| 135 | - before do | |
| 196 | + before :each do | |
| 136 | 197 | get :edit, id: metric_configuration.id, kalibro_configuration_id: metric_configuration.kalibro_configuration_id.to_s |
| 137 | 198 | end |
| 138 | 199 | |
| 139 | 200 | it { is_expected.to redirect_to(kalibro_configurations_path(id: metric_configuration.kalibro_configuration_id)) } |
| 140 | - it { is_expected.to respond_with(:redirect) } | |
| 141 | 201 | it { is_expected.to set_flash[:notice].to("You're not allowed to do this operation") } |
| 142 | 202 | end |
| 143 | 203 | end |
| ... | ... | @@ -152,40 +212,63 @@ describe MetricConfigurationsController, :type => :controller do |
| 152 | 212 | end |
| 153 | 213 | |
| 154 | 214 | describe 'update' do |
| 155 | - let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) } | |
| 156 | - let(:metric_configuration_params) { metric_configuration.to_hash } | |
| 215 | + let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id, reading_group_id: reading_group.id) } | |
| 216 | + let(:metric_configuration_params) { metric_configuration.to_hash.except('id', 'metric') } | |
| 217 | + let(:update_params) { metric_configuration_params.except('kalibro_configuration_id') } | |
| 157 | 218 | |
| 158 | 219 | context 'when the user is logged in' do |
| 159 | 220 | before do |
| 160 | 221 | sign_in FactoryGirl.create(:user) |
| 161 | 222 | end |
| 162 | 223 | |
| 163 | - context 'when user owns the metric configuration' do | |
| 224 | + context 'when the given metric exists' do | |
| 225 | + let!(:metric_collector) { mock('metric_collector') } | |
| 226 | + | |
| 164 | 227 | before :each do |
| 165 | - subject.expects(:metric_configuration_owner?).returns true | |
| 228 | + metric_collector.expects(:find_metric_by_code).with(metric_configuration.metric.code).returns(metric_configuration.metric) | |
| 229 | + | |
| 230 | + KalibroClient::Entities::Processor::MetricCollectorDetails.expects(:find_by_name). | |
| 231 | + with(metric_configuration.metric.metric_collector_name). | |
| 232 | + returns(metric_collector) | |
| 166 | 233 | end |
| 167 | 234 | |
| 168 | - context 'with valid fields' do | |
| 235 | + context 'and the user owns the metric configuration' do | |
| 169 | 236 | before :each do |
| 170 | - MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) | |
| 171 | - MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(true) | |
| 172 | - | |
| 173 | - post :update, kalibro_configuration_id: metric_configuration.kalibro_configuration_id, id: metric_configuration.id, metric_configuration: metric_configuration_params | |
| 237 | + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns kalibro_configuration | |
| 238 | + subject.expects(:metric_configuration_owner?).returns true | |
| 239 | + ReadingGroup.expects(:find).with(reading_group.id).returns(reading_group) | |
| 174 | 240 | end |
| 175 | 241 | |
| 176 | - it { is_expected.to redirect_to(kalibro_configuration_path(id: metric_configuration.kalibro_configuration_id)) } | |
| 177 | - it { is_expected.to respond_with(:redirect) } | |
| 178 | - end | |
| 242 | + context 'with valid fields' do | |
| 243 | + before :each do | |
| 244 | + MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) | |
| 245 | + MetricConfiguration.any_instance.expects(:update).with(update_params).returns(true) | |
| 179 | 246 | |
| 180 | - context 'with an invalid field' do | |
| 181 | - before :each do | |
| 182 | - MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) | |
| 183 | - MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(false) | |
| 247 | + post :update, | |
| 248 | + kalibro_configuration_id: metric_configuration.kalibro_configuration_id, id: metric_configuration.id, | |
| 249 | + metric_configuration: metric_configuration_params, | |
| 250 | + metric_collector_name: metric_configuration.metric.metric_collector_name, | |
| 251 | + metric_code: metric_configuration.metric.code | |
| 252 | + end | |
| 184 | 253 | |
| 185 | - post :update, kalibro_configuration_id: metric_configuration.kalibro_configuration_id, id: metric_configuration.id, metric_configuration: metric_configuration_params | |
| 254 | + it { is_expected.to redirect_to(kalibro_configuration_path(id: metric_configuration.kalibro_configuration_id)) } | |
| 255 | + it { is_expected.to respond_with(:redirect) } | |
| 186 | 256 | end |
| 187 | 257 | |
| 188 | - it { is_expected.to render_template(:edit) } | |
| 258 | + context 'with an invalid field' do | |
| 259 | + before :each do | |
| 260 | + MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) | |
| 261 | + MetricConfiguration.any_instance.expects(:update).with(update_params).returns(false) | |
| 262 | + | |
| 263 | + post :update, | |
| 264 | + kalibro_configuration_id: metric_configuration.kalibro_configuration_id, id: metric_configuration.id, | |
| 265 | + metric_configuration: metric_configuration_params, | |
| 266 | + metric_collector_name: metric_configuration.metric.metric_collector_name, | |
| 267 | + metric_code: metric_configuration.metric.code | |
| 268 | + end | |
| 269 | + | |
| 270 | + it { is_expected.to render_template(:edit) } | |
| 271 | + end | |
| 189 | 272 | end |
| 190 | 273 | end |
| 191 | 274 | |
| ... | ... | @@ -199,7 +282,6 @@ describe MetricConfigurationsController, :type => :controller do |
| 199 | 282 | end |
| 200 | 283 | end |
| 201 | 284 | |
| 202 | - | |
| 203 | 285 | describe 'destroy' do |
| 204 | 286 | let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) } |
| 205 | 287 | |
| ... | ... | @@ -210,6 +292,7 @@ describe MetricConfigurationsController, :type => :controller do |
| 210 | 292 | |
| 211 | 293 | context 'when the user owns the configuration' do |
| 212 | 294 | before :each do |
| 295 | + KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns kalibro_configuration | |
| 213 | 296 | subject.expects(:metric_configuration_owner?).returns true |
| 214 | 297 | metric_configuration.expects(:destroy) |
| 215 | 298 | MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) | ... | ... |
spec/helpers/metric_configurations_helper_spec.rb
| ... | ... | @@ -43,4 +43,40 @@ describe MetricConfigurationsHelper, :type => :helper do |
| 43 | 43 | expect(helper.supported_metrics_of(metric_collector_details.name)).to eq(metric_collector_details.supported_metrics) |
| 44 | 44 | end |
| 45 | 45 | end |
| 46 | + | |
| 47 | + describe 'choose_metric_path' do | |
| 48 | + let(:kalibro_configuration_id) { 1 } | |
| 49 | + | |
| 50 | + context 'with a tree metric' do | |
| 51 | + let(:metric) { FactoryGirl.build(:loc) } | |
| 52 | + | |
| 53 | + it 'is expected to generate the path for MetricConfigurationsController' do | |
| 54 | + expect(helper.choose_metric_path(metric, kalibro_configuration_id)).to eq(helper.kalibro_configuration_new_metric_configuration_path(kalibro_configuration_id: kalibro_configuration_id)) | |
| 55 | + end | |
| 56 | + end | |
| 57 | + | |
| 58 | + context 'with a hotspot metric' do | |
| 59 | + let(:metric) { FactoryGirl.build(:hotspot_metric) } | |
| 60 | + | |
| 61 | + it 'is expected to generate the path for HotspotMetricConfigurationsController' do | |
| 62 | + expect(helper.choose_metric_path(metric, kalibro_configuration_id)).to eq(helper.kalibro_configuration_hotspot_metric_configurations_path(kalibro_configuration_id: kalibro_configuration_id)) | |
| 63 | + end | |
| 64 | + end | |
| 65 | + end | |
| 66 | + | |
| 67 | + describe 'hotspot_metric_configuration?' do | |
| 68 | + let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
| 69 | + let(:hotspot_type) { 'HotspotMetricSnapshot' } | |
| 70 | + let(:other_type) { 'NativeMetricSnapshot' } | |
| 71 | + | |
| 72 | + it 'is expected to return true for a HotspotMetricSnapshot' do | |
| 73 | + metric_configuration.metric.expects(:type).returns(hotspot_type) | |
| 74 | + expect(helper.hotspot_metric_configuration?(metric_configuration)).to eq true | |
| 75 | + end | |
| 76 | + | |
| 77 | + it 'is expected to return false for every other type of metric' do | |
| 78 | + metric_configuration.metric.expects(:type).returns(other_type) | |
| 79 | + expect(helper.hotspot_metric_configuration?(metric_configuration)).to eq false | |
| 80 | + end | |
| 81 | + end | |
| 46 | 82 | end | ... | ... |
| ... | ... | @@ -0,0 +1,33 @@ |
| 1 | +#= require spec_helper | |
| 2 | +#= require metric_collector | |
| 3 | + | |
| 4 | +describe "MetricCollector", -> | |
| 5 | + describe 'choose_metric', -> | |
| 6 | + before -> | |
| 7 | + sinon.stub(window, "$") | |
| 8 | + | |
| 9 | + @metric_code = 'acc' | |
| 10 | + @metric_code_field = sinon.stub() | |
| 11 | + @metric_code_field.val = sinon.stub().withArgs(@metric_code) | |
| 12 | + | |
| 13 | + @metric_collector_name = 'Analizo' | |
| 14 | + @metric_collector_name_field = sinon.stub() | |
| 15 | + @metric_collector_name_field.val = sinon.stub().withArgs(@metric_collector_name) | |
| 16 | + | |
| 17 | + @form = sinon.stub() | |
| 18 | + @form.submit = sinon.stub() | |
| 19 | + @action_path = '/en/kalibro_configurations/1/metric_configurations/new' | |
| 20 | + @form.attr = sinon.stub().withArgs('action', @action_path) | |
| 21 | + | |
| 22 | + $.withArgs("#metric_code").returns(@metric_code_field) | |
| 23 | + $.withArgs("#metric_collector_name").returns(@metric_collector_name_field) | |
| 24 | + $.withArgs("form").returns(@form) | |
| 25 | + | |
| 26 | + it 'is expected to fill in the form and submit', -> | |
| 27 | + MetricCollector.choose_metric(@metric_code, @metric_collector_name, @action_path) | |
| 28 | + | |
| 29 | + sinon.assert.calledOnce(@metric_code_field.val, @metric_code) | |
| 30 | + sinon.assert.calledOnce(@metric_collector_name_field.val, @metric_collector_name) | |
| 31 | + sinon.assert.calledOnce(@form.submit) | |
| 32 | + sinon.assert.calledOnce(@form.attr, 'action', @action_path) | |
| 33 | + | ... | ... |
spec/routing/hotspot_metric_configurations_routing_spec.rb
0 → 100644
| ... | ... | @@ -0,0 +1,8 @@ |
| 1 | +require "rails_helper" | |
| 2 | + | |
| 3 | +describe HotspotMetricConfigurationsController, :type => :routing do | |
| 4 | + describe "routing" do | |
| 5 | + it { is_expected.to route(:post, '/kalibro_configurations/1/hotspot_metric_configurations'). | |
| 6 | + to(controller: :hotspot_metric_configurations, action: :create, kalibro_configuration_id: "1") } | |
| 7 | + end | |
| 8 | +end | |
| 0 | 9 | \ No newline at end of file | ... | ... |