/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*global module: true, ok: true, asyncTest: true, equal: true, expect: true, $q: true,
 gym: true, start: true */

$q(document).ready(function() {
	"use strict";

	var selectors = {
		nameInput: "input[name='p::name']",
		nameNullErrorFeedback: "li.feedbackPanelERROR > span:contains(\"'Name' is required\")",
		nameRangeErrorFeedback: "li.feedbackPanelERROR > span:contains(\"'Name' length must be between 2 and 30\")"

	};

	var submit = function($) {
		return gym.click($('input[value=Submit]'));
	};

	module('Bean validation');

	asyncTest('name', function () {
		expect(6);

		gym.load('/bean-validation')
			.then(function($) {

				// enter name without value (@NonNull)
				var $input = $(selectors.nameInput);
				$input.val('');

				equal($(selectors.nameNullErrorFeedback).length, 0, 'The feedback message for null name is NOT there');

				return submit($);
			}).then(function($) {

				equal($(selectors.nameNullErrorFeedback).length, 1, 'The feedback message for null name is there');

				// enter name with not enough characters (less than 2)
				var $input = $(selectors.nameInput);
				$input.val('a');

				equal($(selectors.nameRangeErrorFeedback).length, 0, 'The feedback message for invalid name is NOT there');

				return submit($);
			}).then(function($) {

				equal($(selectors.nameRangeErrorFeedback).length, 1, 'The feedback message for invalid name is there');

				// enter name with enough characters (more than 2, less than 30)
				var $input = $(selectors.nameInput);
				$input.val('abc');

				return submit($);
			}).then(function($) {

				equal($(selectors.nameRangeErrorFeedback).length, 0, 'The feedback message for invalid name is NOT there');

				// enter name with more than 30 characters
				var $input = $(selectors.nameInput);
				$input.val('abcdefghijklmnopqrstuvwxyzABCDE');

				return submit($);
			}).then(function($) {

				equal($(selectors.nameRangeErrorFeedback).length, 1, 'The feedback message for invalid name is NOT there');
			}).always(start);
	});

});
