From 3ecdecfb809e6432224da103259bd38a2ab42f83 Mon Sep 17 00:00:00 2001
From: Lukas Krause <lkrause@suse.de>
Date: Fri, 25 May 2018 14:18:05 +0200
Subject: [PATCH 18/27] Dev: Create validation class for colocation and order
 model

---
 hawk/app/models/colocation.rb               |  1 +
 hawk/app/models/constraint.rb               |  3 +++
 hawk/app/models/order.rb                    | 25 ----------------------
 hawk/app/validators/colocation_validator.rb | 33 +++++++++++++++++++++++++++++
 hawk/app/validators/order_validator.rb      | 28 ++++++++++++++++++++++++
 5 files changed, 65 insertions(+), 25 deletions(-)
 create mode 100644 hawk/app/validators/colocation_validator.rb
 create mode 100644 hawk/app/validators/order_validator.rb

diff --git a/hawk/app/models/colocation.rb b/hawk/app/models/colocation.rb
index f58b4757..21e2c168 100644
--- a/hawk/app/models/colocation.rb
+++ b/hawk/app/models/colocation.rb
@@ -52,6 +52,7 @@ class Colocation < Constraint
     end
   end
 
+
   protected
 
   def shell_syntax
diff --git a/hawk/app/models/constraint.rb b/hawk/app/models/constraint.rb
index 0227be4b..6f295ca0 100644
--- a/hawk/app/models/constraint.rb
+++ b/hawk/app/models/constraint.rb
@@ -2,10 +2,13 @@
 # See COPYING for license.
 
 class Constraint < Record
+  include ActiveModel::Validations
   class CommandError < StandardError
   end
 
   attribute :object_type, Symbol
+  validates_with ColocationValidator, if: Proc.new { |a| a.is_a? Colocation }
+  validates_with OrderValidator, if: Proc.new { |a| a.is_a? Order } 
 
   validate do |record|
     # to validate a new record:
diff --git a/hawk/app/models/order.rb b/hawk/app/models/order.rb
index cf012e0e..7b7fbde7 100644
--- a/hawk/app/models/order.rb
+++ b/hawk/app/models/order.rb
@@ -6,31 +6,6 @@ class Order < Constraint
   attribute :symmetrical, Boolean
   attribute :resources, Array[Hash]
 
-  validates :score,
-    presence: { message: _("Score is required") }
-
-  validate do |record|
-    record.score.strip!
-
-    unless [
-      "mandatory",
-      "optional",
-      "serialize",
-      "inf",
-      "-inf",
-      "infinity",
-      "-infinity"
-    ].include? record.score.downcase
-      unless record.score.match(/^-?[0-9]+$/)
-        errors.add :score, _("Invalid score value")
-      end
-    end
-
-    if record.resources.length < 2
-      errors.add :base, _("Constraint must consist of at least two separate resources")
-    end
-  end
-
   def resources
     @resources ||= []
   end
diff --git a/hawk/app/validators/colocation_validator.rb b/hawk/app/validators/colocation_validator.rb
new file mode 100644
index 00000000..9c2a2ab8
--- /dev/null
+++ b/hawk/app/validators/colocation_validator.rb
@@ -0,0 +1,33 @@
+class ColocationValidator < ActiveModel::Validator
+  
+  def validate(record)  
+    record.score.strip!
+    unless [
+      "mandatory",
+      "advisory",
+      "inf",
+      "-inf",
+      "infinity",
+      "-infinity"
+    ].include? record.score.downcase
+      unless record.score.match(/^-?[0-9]+$/)
+        record.errors[:score] << _("Invalid score value")
+      end
+    end
+
+    if record.score.to_s.strip.empty?
+      record.errors[:score] << _("Score is required")
+    end
+
+    unless record.node_attr.blank?
+      unless record.node_attr.match(/\A[a-zA-Z0-9_-]+\z/)
+        record.errors[:node_attr] << _("Invalid node attribute")
+      end
+    end
+
+    if record.resources.length < 2
+      record.errors[:base] << _("Constraint must consist of at least two separate resources")
+    end
+  end
+
+end
\ No newline at end of file
diff --git a/hawk/app/validators/order_validator.rb b/hawk/app/validators/order_validator.rb
new file mode 100644
index 00000000..64bb0583
--- /dev/null
+++ b/hawk/app/validators/order_validator.rb
@@ -0,0 +1,28 @@
+class OrderValidator < ActiveModel::Validator
+    
+  def validate(record)
+    record.score.strip!
+    unless [
+      "mandatory",
+      "optional",
+      "serialize",
+      "inf",
+      "-inf",
+      "infinity",
+      "-infinity"
+    ].include? record.score.downcase
+      unless record.score.match(/^-?[0-9]+$/)
+        record.errors[:score] << _("Invalid score value")
+      end
+    end
+
+    if record.score.to_s.strip.empty?
+      record.errors[:score] << _("Score is required")
+    end
+
+    if record.resources.length < 2
+      record.errors[:base] << _("Constraint must consist of at least two separate resources")
+    end
+  end
+
+end
\ No newline at end of file
-- 
2.16.4

