Skip to content
Browse files

Introduced Policies for RespondTo specification

  • Loading branch information...
1 parent b1e5df4 commit 707d40e1d3b8738a7b2c65161214c27ad1dff399 @krakatoa krakatoa committed
Showing with 116 additions and 0 deletions.
  1. +60 −0 lib/protocol/policies/respond_to.rb
  2. +2 −0 lib/protocol/specification.rb
  3. +54 −0 spec/respond_to_spec.rb
View
60 lib/protocol/policies/respond_to.rb
@@ -0,0 +1,60 @@
+module Cucub
+ module Protocol
+ module Policies
+ class RespondTo
+
+ LVL_ANY = 0
+ LVL_SAME = 1
+ LVL_SPECIFIC = 2
+
+ def initialize(instance_pair, class_pair, zone_pair)
+ @valid_instances = Cucub::Protocol::Policies::RespondTo.parse_policy_pair_to_hash(instance_pair)
+ @valid_classes = Cucub::Protocol::Policies::RespondTo.parse_policy_pair_to_hash(class_pair)
+ @valid_zones = Cucub::Protocol::Policies::RespondTo.parse_policy_pair_to_hash(zone_pair)
+ end
+
+ def valid_instances_string
+ valid_string_for(@valid_instances)
+ end
+
+ def valid_classes_string
+ valid_string_for(@valid_classes)
+ end
+
+ def valid_zones_string
+ valid_string_for(@valid_zones)
+ end
+
+ private
+ def valid_string_for(hash)
+ result = Cucub::Protocol::Policies::RespondTo.check_level_string(hash[:check_level])
+ result << "{#{hash[:specific]}}" if hash[:check_level] == Cucub::Protocol::Policies::RespondTo::LVL_SPECIFIC
+ result
+ end
+
+ def self.check_level_string(check_level)
+ case check_level
+ when LVL_ANY
+ "any"
+ when LVL_SAME
+ "same"
+ when LVL_SPECIFIC
+ "specific"
+ end
+ end
+
+ # A policy check_level/array is composed of a check_level plus a specific data (if it applies)
+ def self.parse_policy_pair_to_hash(pair)
+ pair = [pair] unless pair.is_a? Array
+ policy_hash = { :check_level => pair[0] }
+ policy_hash.merge!(:specific => pair[1]) if pair[1]
+ policy_hash
+ end
+
+ def self.parse(text_config)
+ Cucub::Protocol::Policies::RespondTo.new(instance_pair, class_pair, zone_pair)
+ end
+ end
+ end
+ end
+end
View
2 lib/protocol/specification.rb
@@ -1,3 +1,5 @@
+require_relative './policies/respond_to'
+
module Cucub
module Protocol
class Specification
View
54 spec/respond_to_spec.rb
@@ -0,0 +1,54 @@
+require 'spec_helper'
+
+describe Cucub::Protocol::Policies::RespondTo do
+ #before do
+ # @filepath = File.dirname(File.expand_path(__FILE__))
+ #end
+ describe "#valid_instances_string" do
+ context "when parsing a valid config" do
+ subject { Cucub::Protocol::Policies::RespondTo.new(Cucub::Protocol::Policies::RespondTo::LVL_ANY, Cucub::Protocol::Policies::RespondTo::LVL_ANY, Cucub::Protocol::Policies::RespondTo::LVL_ANY) }
+ it { subject.valid_instances_string.should == "any" }
+ it { subject.valid_classes_string.should == "any" }
+ it { subject.valid_zones_string.should == "any" }
+ end
+
+ #context "when parsing an unexisting file" do
+ # before { subject.set_path(@filepath + "/mock/unexisting.ini") }
+ # it { subject.parse.should == false }
+ #end
+
+ end
+
+ describe ".parse_policy_pair_to_hash" do
+ context "when parsing a valid config" do
+ subject { Cucub::Protocol::Policies::RespondTo.parse_policy_pair_to_hash(Cucub::Protocol::Policies::RespondTo::LVL_ANY) }
+ it { subject[:check_level] == Cucub::Protocol::Policies::RespondTo::LVL_ANY }
+ end
+
+ context "when parsing a valid config" do
+ subject { Cucub::Protocol::Policies::RespondTo.parse_policy_pair_to_hash([Cucub::Protocol::Policies::RespondTo::LVL_SPECIFIC, "myclass"]) }
+ it { subject[:check_level] == Cucub::Protocol::Policies::RespondTo::LVL_SPECIFIC }
+ it { subject[:specific] == "myclass" }
+ end
+
+ #context "when parsing an unexisting file" do
+ # before { subject.set_path(@filepath + "/mock/unexisting.ini") }
+ # it { subject.parse.should == false }
+ #end
+
+ end
+ describe ".parse" do
+ #subject { Cucub::Protocol::Loader.new }
+ #context "when parsing a valid config" do
+ # subject { Cucub::Protocol::Policies::RespondTo.parse("test") }
+ # it { subject.valid_instances_string.should == "test" }
+ #end
+
+ #context "when parsing an unexisting file" do
+ # before { subject.set_path(@filepath + "/mock/unexisting.ini") }
+ # it { subject.parse.should == false }
+ #end
+
+ end
+
+end

0 comments on commit 707d40e

Please sign in to comment.
Something went wrong with that request. Please try again.