var resource = require('../../lib/util/resource');

var should = require('chai').should();

describe('util.resource', function () {
    describe('addExtension', function () {
        it('should throw exception if resource undefined', function () {
            should.Throw(function () {
                resource.addExtension();
            });
        });

        it('should throw exception if extension undefined', function () {
            should.Throw(function () {
                resource.addExtension({});
            });
        });

        it('should add extension', function () {
            var extension = {
                url: 'http://foo.com/bar',
                valueFoo: 'bar'
            };
            var candidate = {
                extension: [
                    {
                        url: 'http://test.com/nomatch'
                    }
                ]
            };

            var result = resource.addExtension(candidate, extension);

            should.exist(result);
            result.extension.length.should.equal(2);
            result.extension[1].valueFoo.should.equal('bar');
        });

        it('should update existing extension', function () {
            var extension = {
                url: 'http://foo.com/bar',
                valueFoo: 'bar'
            };
            var candidate = {
                extension: [
                    {
                        url: 'http://test.com/nomatch'
                    },
                    {
                        url: 'http://foo.com/bar',
                        valueFoo: 'xyz'
                    }
                ]
            };

            var result = resource.addExtension(candidate, extension);

            should.exist(result);
            result.extension.length.should.equal(2);
            result.extension[1].valueFoo.should.equal('bar');
        });

        it('should add extension when none exist', function () {
            var extension = {
                url: 'http://foo.com/bar',
                valueFoo: 'bar'
            };
            var candidate = {
            };

            var result = resource.addExtension(candidate, extension);

            should.exist(result);
            result.extension.length.should.equal(1);
            result.extension[0].valueFoo.should.equal('bar');
        });
    });

    describe('removeExtension', function () {
        it('should throw exception if resource undefined', function () {
            should.Throw(function () {
                resource.removeExtension();
            });
        });

        it('should throw exception if url undefined', function () {
            should.Throw(function () {
                resource.removeExtension({});
            });
        });

        it('should remove extension', function () {
            var url= 'http://foo.com/bar';
            var candidate = {
                extension: [
                    {
                        url: 'http://test.com/nomatch',
                        valueFoo: 'nomatch'
                    },
                    {
                        url: 'http://foo.com/bar',
                        valueFoo: 'bar'
                    }
                ]
            };

            var result = resource.removeExtension(candidate, url);

            should.exist(result);
            result.extension.length.should.equal(1);
            result.extension[0].valueFoo.should.equal('nomatch');
        });

        it('should not change resource when no extension matches', function () {
            var url= 'http://foo.com/bar';
            var candidate = {
                extension: [
                    {
                        url: 'http://test.com/nomatch',
                        valueFoo: 'nomatch'
                    },
                    {
                        url: 'http://foo.com/xyz',
                        valueFoo: 'xyz'
                    }
                ]
            };

            var result = resource.removeExtension(candidate, url);

            should.exist(result);
            result.extension.length.should.equal(2);
            result.extension[1].valueFoo.should.equal('xyz');
        });

        it('should not not change resource when no extensions exist', function () {
            var url= 'http://foo.com/bar';
            var candidate = {};

            var result = resource.removeExtension(candidate, url);

            should.exist(result);
            should.not.exist(result.extension);
        });
    });

});