<%@ taglib uri="/struts-tags" prefix="ww" %>
<ww:set name="successMessageKey">${param.messagePrefix}.testconnection.success</ww:set>
<ww:set name="failureMessageKey">${param.messagePrefix}.testconnection.invalid</ww:set>
        function testConfiguration() {
            var data = {};
            AJS.$("#${param.formId}").serializeArray().map(function(x){data[x.name] = x.value;});
            var $testConnectionButton = AJS.$('#test-connection');
            $testConnectionButton.prop("disabled", true);
            var $testConnectionSpinner = AJS.$("#test-connection-spinner");
            $testConnectionSpinner.spin();
            AJS.$.ajax({
                url: AJS.contextPath() + "/rest/directorymanagement/1/directory/test${param.directoryType}/${param.directoryId}",
                type: 'POST',
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json'
            }).done(function() {

                require(['aui/flag'], function(flag) {
                    var myFlag = flag({
                        type: 'success',
                        title: '<ww:property value="getText(#successMessageKey)"/>'
                    });
                    setTimeout(function() {
                        myFlag.close();
                    }, 5000);
                });
            }).fail(function(jqxhr) {
                if (jqxhr.status == 401) {
                    window.location.replace(AJS.contextPath() + "/console/login.action");
                } else {
                    require(['aui/flag'], function (flag) {
                        flag({
                            type: 'error',
                            title: '<ww:property value="getText(#failureMessageKey)"/>' + " " + extractErrorMessage(jqxhr.responseText)
                        });
                    });
                }
            }).always(function() {
                $testConnectionButton.prop("disabled", false);
                $testConnectionSpinner.spinStop();
            });
        }

        function extractErrorMessage(responseText) {
            try {
                return JSON.parse(responseText).message;
            } catch (e) {
                return responseText
            }
        }
