
// Copyright 2011 Splunk, Inc.
//
// Licensed 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.

exports.run = (function() {
    var Splunk      = require('../splunk').Splunk;
    var NodeHttp    = require('../platform/node/node_http').NodeHttp;
    var minitest    = require('../external/minitest');
    var assert      = require('assert');
    var utils       = Splunk.Utils;

    var http = new NodeHttp();
    var svc = new Splunk.Client.Service(http, { 
        scheme: "http",
        host: "localhost",
        port: "8000",
        username: "itay",
        password: "changeme",
    });

    var idCounter = 0;
    var getNextId = function() {
        return "id" + (idCounter++);
    };

    svc.login(function(success) {
        minitest.context("Client Tests", function() {
            this.setup(function() {
                this.service = svc;
            });

            this.assertion("Service exists", function(test) {
                assert.ok(this.service);
                test.finished();
            });

            this.assertion("Login succeeded", function(test) {
                assert.ok(this.service.sessionKey);
                test.finished();
            });

            this.assertion("Promise#Create+cancel job", function(test) {
                var sid = getNextId();
                var jobP = this.service.jobs().create('search index=_internal | head 1', {id: sid});
                jobP.when(
                    utils.bind(this, function(job) {   
                        assert.ok(job);
                        assert.strictEqual(job.sid, sid);

                        var cancelP = job.cancel();
                        cancelP.when(
                            function() {
                                test.finished();
                            }
                        );
                    })
                );
            });

            this.assertion("Callback#Create+cancel job", function(test) {
                var sid = getNextId();
                this.service.jobs().create('search index=_internal | head 1', {id: sid}, utils.bind(this, function(job) {   
                    assert.ok(job);
                    assert.strictEqual(job.sid, sid);

                    job.cancel(function() {
                        test.finished();
                    });
                })); 
            });

            this.assertion("Promise#Create job error", function(test) {
                var sid = getNextId();
                var jobP = this.service.jobs().create('index=_internal | head 1', {id: sid});
                jobP.whenFailed(
                    function() {
                        test.finished();
                    }
                );
            });

            this.assertion("Callback#Create job error", function(test) {
                var sid = getNextId();
                this.service.jobs().create('index=_internal | head 1', {id: sid}, {
                    success: function () { assert.ok(false); },
                    error: function() { test.finished(); },
                });
            });

            this.assertion("Promise#List jobs", function(test) {
                var jobListP = this.service.jobs().list();
                jobListP.when(
                    function(jobs) {
                        assert.ok(jobs);
                        assert.ok(jobs.length > 0);
                        test.finished();
                    }
                );
            });

            this.assertion("Callback#List jobs", function(test) {
                this.service.jobs().list(function(jobs) {
                    assert.ok(jobs);
                    assert.ok(jobs.length > 0);
                    test.finished();
                });
            });

            this.assertion("Promise#Contains job", function(test) {
                var sid = getNextId();
                var jobP = this.service.jobs().create('search index=_internal | head 1', {id: sid});
                jobP.when(
                    utils.bind(this, function(job) {   
                        assert.ok(job);
                        assert.strictEqual(job.sid, sid);

                        var containsP = this.service.jobs().contains(sid);
                        containsP.when(
                            function(contains) {
                                assert.ok(contains);

                                var cancelP = job.cancel();
                                cancelP.when(
                                    function() {
                                        test.finished();
                                    }
                                );
                            }
                        );
                    })
                );
            });

            this.assertion("Callback#Contains job", function(test) {
                var sid = getNextId();
                this.service.jobs().create('search index=_internal | head 1', {id: sid}, utils.bind(this, function(job) {   
                    assert.ok(job);
                    assert.strictEqual(job.sid, sid);

                    this.service.jobs().contains(sid, function(contains) {
                        assert.ok(contains);

                        job.cancel(function() {
                            test.finished();
                        });
                    });
                })); 
            });
        });
    });
}); 

if (module === require.main) {
    require('../external/minitest').setupListeners();
    exports.run();
}
@@ -1245,22 +1245,19 @@
 ontext(%22
-Client
+Job
  Tests%22,
@@ -1363,326 +1363,8 @@
 );%0A%0A
-            this.assertion(%22Service exists%22, function(test) %7B%0A                assert.ok(this.service);%0A                test.finished();%0A            %7D);%0A%0A            this.assertion(%22Login succeeded%22, function(test) %7B%0A                assert.ok(this.service.sessionKey);%0A                test.finished();%0A            %7D);%0A%0A
     

Test cleanup