<commit_before>'use strict';

const Code = require('code');
const Lab = require('lab');
const DataRetrievalRouter = require('../').DataRetrievalRouter;

const lab = exports.lab = Lab.script();
const experiment = lab.experiment;
const test = lab.test;

const expect = Code.expect;

/**
 * Rule based access control policy tests, based on username
 **/
experiment('RBAC internal modular information retrieval', () => {

    const dataRetriever = new DataRetrievalRouter();

    test('should register a valid retriever', (done) => {

        const retriever = (source, key, context) => {

            return 'key-' + key;
        };

        dataRetriever.register('test', retriever);

        dataRetriever.get('test:x', (err, result) => {

            expect(err).to.not.exist();
            expect(result).to.equal('key-x');
            done();
        });
    });

    test('should override a valid retriever (single handler)', (done) => {

        const retriever1 = (source, key, context) => {

            return key + '-1';
        };

        const retriever2 = (source, key, context) => {

            return key + '-2';
        };

        dataRetriever.register('test-override', retriever1);
        dataRetriever.register('test-override', retriever2, { override: true });

        dataRetriever.get('test-override:test', (err, result) => {

            expect(err).to.not.exist();
            expect(result).to.equal('test-2');

            done();
        });

    });

    test('should not override a valid retriever (single handler)', (done) => {

        const retriever1 = (source, key, context) => {

            return key + '-1';
        };

        const retriever2 = (source, key, context) => {

            return key + '-2';
        };

        dataRetriever.register('test-override-error', retriever1);

        expect(dataRetriever.register.bind(dataRetriever, 'test-override-error', retriever2)).to.throw();

        done();
    });

    test('should override a valid retriever (multiple handlers)', (done) => {

        const retriever1 = (source, key, context) => {

            return key + '-1';
        };

        const retriever2 = (source, key, context) => {

            return key + '-2';
        };

        dataRetriever.register(['test-override-multiple-1', 'test-override-multiple-2', 'test-override-multiple-3'], retriever1);
        dataRetriever.register(['test-override-multiple-2', 'test-override-multiple-4'], retriever2, { override: true }); // test-override-multiple-2 collides

        dataRetriever.get('test-override-multiple-1:test', (err, result) => {

            expect(err).to.not.exist();
            expect(result).to.equal('test-1');

            dataRetriever.get('test-override-multiple-2:test', (err, result) => {

                expect(err).to.not.exist();
                expect(result).to.equal('test-2');


                dataRetriever.get('test-override-multiple-3:test', (err, result) => {

                    expect(err).to.not.exist();
                    expect(result).to.equal('test-1');

                    dataRetriever.get('test-override-multiple-4:test', (err, result) => {

                        expect(err).to.not.exist();
                        expect(result).to.equal('test-2');

                        done();
                    });
                });
            });
        });
    });

    test('should not override a valid retriever (multiple handlers)', (done) => {

        const retriever1 = (source, key, context) => {

            return key + '-1';
        };

        const retriever2 = (source, key, context) => {

            return key + '-2';
        };

        dataRetriever.register(['test-override-error-multiple-1', 'test-override-error-multiple-2', 'test-override-error-multiple-3'], retriever1);
        expect(dataRetriever.register.bind(dataRetriever, ['test-override-error-multiple-2', 'test-override-error-multiple-4'], retriever2)).to.throw(Error, 'There is a data retriever already registered for the source: test-override-error-multiple-2');

        done();
    });

    test('should register a valid asynchronous retriever', (done) => {

        const retriever = (source, key, context, callback) => {

            callback(null, 'key-' + key);
        };

        dataRetriever.register('async-test', retriever);

        dataRetriever.get('async-test:x', (err, result) => {

            expect(err).to.not.exist();
            expect(result).to.equal('key-x');
            done();
        });
    });

    test('should return err in callback when an error is thrown (sync)', (done) => {

        const retriever = (source, key, context) => {

            throw new Error('test');
        };

        dataRetriever.register('sync-test-err-1', retriever);

        dataRetriever.get('sync-test-err-1:x', (err, result) => {<commit_msg>Created tests to try different combination of missing callback parameter on get<commit_after>'use strict';

const Code = require('code');
const Lab = require('lab');
const DataRetrievalRouter = require('../').DataRetrievalRouter;

const lab = exports.lab = Lab.script();
const experiment = lab.experiment;
const test = lab.test;

const expect = Code.expect;

/**
 * Rule based access control policy tests, based on username
 **/
experiment('RBAC internal modular information retrieval', () => {

    const dataRetriever = new DataRetrievalRouter();

    test('should register a valid retriever', (done) => {

        const retriever = (source, key, context) => {

            return 'key-' + key;
        };

        dataRetriever.register('test', retriever);

        dataRetriever.get('test:x', (err, result) => {

            expect(err).to.not.exist();
            expect(result).to.equal('key-x');
            done();
        });
    });

    test('should override a valid retriever (single handler)', (done) => {

        const retriever1 = (source, key, context) => {

            return key + '-1';
        };

        const retriever2 = (source, key, context) => {

            return key + '-2';
        };

        dataRetriever.register('test-override', retriever1);
        dataRetriever.register('test-override', retriever2, { override: true });

        dataRetriever.get('test-override:test', (err, result) => {

            expect(err).to.not.exist();
            expect(result).to.equal('test-2');

            done();
        });

    });

    test('should not override a valid retriever (single handler)', (done) => {

        const retriever1 = (source, key, context) => {

            return key + '-1';
        };

        const retriever2 = (source, key, context) => {

            return key + '-2';
        };

        dataRetriever.register('test-override-error', retriever1);

        expect(dataRetriever.register.bind(dataRetriever, 'test-override-error', retriever2)).to.throw();

        done();
    });

    test('should override a valid retriever (multiple handlers)', (done) => {

        const retriever1 = (source, key, context) => {

            return key + '-1';
        };

        const retriever2 = (source, key, context) => {

            return key + '-2';
        };

        dataRetriever.register(['test-override-multiple-1', 'test-override-multiple-2', 'test-override-multiple-3'], retriever1);
        dataRetriever.register(['test-override-multiple-2', 'test-override-multiple-4'], retriever2, { override: true }); // test-override-multiple-2 collides

        dataRetriever.get('test-override-multiple-1:test', (err, result) => {

            expect(err).to.not.exist();
            expect(result).to.equal('test-1');

            dataRetriever.get('test-override-multiple-2:test', (err, result) => {

                expect(err).to.not.exist();
                expect(result).to.equal('test-2');


                dataRetriever.get('test-override-multiple-3:test', (err, result) => {

                    expect(err).to.not.exist();
                    expect(result).to.equal('test-1');

                    dataRetriever.get('test-override-multiple-4:test', (err, result) => {

                        expect(err).to.not.exist();
                        expect(result).to.equal('test-2');

                        done();
                    });
                });
            });
        });
    });

    test('should not override a valid retriever (multiple handlers)', (done) => {

        const retriever1 = (source, key, context) => {

            return key + '-1';
        };

        const retriever2 = (source, key, context) => {

            return key + '-2';
        };

        dataRetriever.register(['test-override-error-multiple-1', 'test-override-error-multiple-2', 'test-override-error-multiple-3'], retriever1);
        expect(dataRetriever.register.bind(dataRetriever, ['test-override-error-multiple-2', 'test-override-error-multiple-4'], retriever2)).to.throw(Error, 'There is a data retriever already registered for the source: test-override-error-multiple-2');

        done();
    });

    test('should register a valid asynchronous retriever', (done) => {

        const retriever = (source, key, context, callback) => {

            callback(null, 'key-' + key);
        };

        dataRetriever.register('async-test', retriever);

        dataRetriever.get('async-test:x', (err, result) => {

            expect(err).to.not.exist();
            expect(result).to.equal('key-x');
            done();
        });
    });

    test('should not allow using get with context and without callback', (done) => {

        expect(dataRetriever.get.bind(null, 'get-with-context-without-callback:x', {})).to.throw(Error);
        done()
    });

    test('should not allow using get without context and without callback', (done) => {

        expect(dataRetriever.get.bind(null, 'get-with-context-without-callback:x', {})).to.throw(Error);
        done()
    });

    test('should return err in callback when an error is thrown (sync)', (done) => {

        const retriever = (source, key, context) => {

            throw new Error('test');
        };

        dataRetriever.register('sync-test-err-1', retriever);

        dataRetriever.get('sync-test-err-1:x', (err, result) => {
{'fraction_non_alphanumeric': 0.16139457447850358, 'fraction_numerical': 0.009205758495739888, 'mean_word_length': 2.2093023255813953, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3476004', 'n_tokens_mistral': 3052, 'n_tokens_neox': 2893, 'n_words': 603}