var pyrusConnectClinical = require('../lib/index').pyrusConnectClinical;
var pyrusConnect = require('../lib/index').pyrusConnect;
var pyrusCloud = require('../lib/index').pyrusCloud;
var should = require('chai').should();

describe('pyrusConnectClinical', function () {
    it('should fulfill promise with a data object for a valid GET request', function (done) {

        function checkResponse(data) {

            if (!data)
                throw new Error;

            var res = JSON.parse(data);
            res.resourceType.should.equal("Bundle");
            res.entry[0].id.should.equal("813");
            done();
        }

        function handleError(err) {
            done(err);
        }

        pyrusConnectClinical.get('/Patient/813', 'organisation=50002')
            .then(checkResponse)
            .catch(handleError);
    });

    it('should fulfill promise for a valid POST request', function (done) {

        function handleError(err) {
            done(err);
        }

        var data = "some test data";
        pyrusConnectClinical.post('/Patient/500112', data, 'organisation=50002')
            .then(function(data){
                done();
            })
            .catch(handleError);
    });

    it('should reject promise for an invalid GET request', function (done) {

        function checkResponse(data) {
            var err = new Error("Should not arrive here!");
            done(err);
        }

        function handleError(err) {
            done();
        }

        pyrusConnectClinical.get('/Patient/1234567', 'organisation=50002')
            .then(checkResponse)
            .catch(handleError);
    });

    it('should reject promise for an invalid POST request', function (done) {

        function checkResponse(data) {
            var err = new Error("Should not arrive here!");
            done(err);
        }

        function handleError(err) {
            done();
        }

        var data = "some test data";
        pyrusConnectClinical.post('/Patient/500112', data, 'organisation=999999')
            .then(checkResponse)
            .catch(handleError);
    });

    it('should fulfill promise for a valid CONFIG call', function (done) {

        function handleError(err) {
            done(err);
        }

        pyrusConnectClinical.config('adapterCache=true')
            .then(function(){
                done();
            })
            .catch(handleError);
    });
});

describe('pyrusConnect', function () {

    var app = pyrusConnect();

    it('should be a function', function() {
        app.get.should.be.a('function');

    });

    it('should provide a response for a valid lookup request', function (done) {

        function checkResponse(res) {

            res.statusCode.should.equal(200);
            res.body.should.exist;
            res.body.resourceType.should.equal('Bundle');
            res.body.entry.length.should.equal(2);
            done();
        }

        function handleError(err) {
            done(err);
        }

        app.get('/UCC001/Patient/813')
            .then(checkResponse)
            .catch(handleError);
    });

    it('should provide a response for a valid search request', function (done) {

        function checkResponse(res) {

            res.statusCode.should.equal(200);
            res.body.should.exist;
            res.body.resourceType.should.equal('Bundle');
            res.body.entry.length.should.equal(40);
            done();
        }

        function handleError(err) {
            done(err);
        }

        app.get('/UCC001/Slot?schedule=6222&date=2015-09-29')
            .then(checkResponse)
            .catch(handleError);
    });

    it('should provide an error with Operational Outcome for an invalid lookup request', function (done) {

        function checkResponse(res) {
            var err = new Error("Should not arrive here!");
            done(err);
        }

        function handleError(err) {
            err.status.should.equal(404);
            err.message.should.equal('cannot GET http://127.0.0.1:3002/UCC001/Patient/12345 (404)');
            done();
        }

        app.get('/UCC001/Patient/12345')
            .then(checkResponse)
            .catch(handleError);
    });

    it('should provide a response for a valid POST request', function (done) {
        function checkResponse(res) {

            res.statusCode.should.equal(201);
            done();
        }

        function handleError(err) {
            done(err);
        }

        app.post('/UCC001/Appointment')
            .then(checkResponse)
            .catch(handleError);

    });

    it('should reject unsupported methods', function (done) {
        function checkResponse(res) {

            res.statusCode.should.equal(405);
            done();
        }

        function handleError(err) {
            done(err);
        }

        app.del('/UCC002/Appointment')
            .then(checkResponse)
            .catch(handleError);

    });

});

describe('pyrusCloud', function () {
    app = pyrusCloud();

    it('should fulfill promise with a data object for a valid GET request', function (done) {

        function checkResponse(res) {

            res.statusCode.should.equal(200);
            res.body.should.exist;
            res.body.resourceType.should.equal('Bundle');
            res.body.entry.length.should.equal(6);

            res.body.entry.forEach(function(entry) {
                entry.content.meta.should.exist;
                entry.content.meta.extension.should.exist;
            });

            done();
        }

        function handleError(err) {
            done(err);
        }

        app.get('/Slot')
            .then(checkResponse)
            .catch(handleError);
    });

    it('should reject promise with an invalid GET request', function (done) {

        function checkResponse(res) {
            var err = new Error("Should not arrive here!");
            done(err);
        }

        function handleError(err) {
            err.status.should.equal(404);
            err.message.should.equal('cannot GET http://127.0.0.1:3001/fhir/Slot/1234567 (404)');
            done();
        }

        app.get('/Slot/1234567')
            .then(checkResponse)
            .catch(handleError);
    });

    it('should fulfill promise for a valid POST request', function (done) {

        function checkResponse(res) {

            res.statusCode.should.equal(201);
            res.headers.location.should.exist
            done();
        }

        function handleError(err) {
            done(err);
        }

        app.post('/Patient/5bc23ace', "")
            .then(checkResponse)
            .catch(handleError);
    });

    it('should fulfill promise for a valid PUT request', function (done) {

        function checkResponse(res) {

            res.statusCode.should.equal(200);
            done();
        }

        function handleError(err) {
            done(err);
        }

        app.put('/Slot/acf241778', "")
            .then(checkResponse)
            .catch(handleError);
    });

    it('should fulfill promise for a valid DEL request', function (done) {

        function checkResponse(res) {

            res.statusCode.should.equal(200);
            done();
        }

        function handleError(err) {
            done(err);
        }

        app.del('/Appointment/123dca98', "")
            .then(checkResponse)
            .catch(handleError);
    });


});





