<commit_before>/**
 * Created by nmande on 4/12/16.
 */


describe('CloudWidgetViewController', function () {


    var controller;
    var scope;
    var cloudData;
    var AWSGlobalData = {
        "compute": {
            "ec2Instances": 3015,
            "running": 1900,
            "stopped": 300,
            "excluded": 910
        },
        "s3": {
            "s3Buckets": 9000,
            "encrypted": 35,
            "tagged": 45,
            "compliant": 54
        }
    };

    function retrieveTestDate(dayOffset) {
        var currentDate = new Date();
        currentDate.setDate(currentDate.getDate() + dayOffset);
        var dd = currentDate.getDate();
        var mm = currentDate.getMonth() + 1;
        var yyyy = currentDate.getFullYear();

        if (dd < 10) {
            dd = '0' + dd
        }
        if (mm < 10) {
            mm = '0' + mm
        }

        return mm + '/' + dd + '/' + yyyy;
    }

    // load the controller's module
    beforeEach(module(HygieiaConfig.module));
    beforeEach(module(HygieiaConfig.module + '.core'));

    beforeEach(module(function ($provide) {
        $provide.factory('cloudData', function () {

            return {
                getAWSGlobalData: getAWSGlobalData,
                getAWSInstancesByTag: getAWSInstancesByTag
            };

            function getAWSGlobalData() {
                return AWSGlobalData;
            }

            function getAWSInstancesByTag() {
                return null;
            }
        })
    }));


    // inject the required services and instantiate the controller
    beforeEach(
        function () {
            inject(function ($rootScope, cloudData, $controller) {
                scope = $rootScope.$new();

                scope.widgetConfig = {
                    options: {
                        tag: "MyTag"
                    }
                };

                controller = $controller('CloudWidgetViewController', {
                    $scope: scope,
                    cloudData: cloudData
                });
            })
        });

    describe('changeSortDirection()', function () {
        describe('When I call changeSortDirection', function () {
            describe('And no sort key has been created', function () {
                it('Then I expect to have one sort key added to sortType with a "-" at the front', function () {

                    //Arrange
                    var key = 'AMI';
                    var expected = ['-AMI'];

                    //Act
                    controller.changeSortDirection(key);

                    //Assert
                    expect(angular.equals(controller.sortType, expected)).toBeTruthy();
                });
            });

            describe('And one non-matching sort key exists', function () {
                it('Then I expect to have a new sort key added to the front of sortType with a "-" at the front', function () {

                    //Arrange
                    var firstKey = 'instanceID';
                    var secondKey = 'AMI';
                    var expected = ['-AMI','-instanceID'];

                    //Act
                    controller.changeSortDirection(firstKey);<commit_msg>Additional tests for cloud/view.js controller<commit_after>/**
 * Created by nmande on 4/12/16.
 */


describe('CloudWidgetViewController', function () {


    var controller;
    var scope;
    var cloudData;
    var AWSGlobalData = {
        "compute": {
            "ec2Instances": 3015,
            "running": 1900,
            "stopped": 300,
            "excluded": 910
        },
        "s3": {
            "s3Buckets": 9000,
            "encrypted": 35,
            "tagged": 45,
            "compliant": 54
        }
    };

    function retrieveTestDate(dayOffset) {
        var currentDate = new Date();
        currentDate.setDate(currentDate.getDate() + dayOffset);
        var dd = currentDate.getDate();
        var mm = currentDate.getMonth() + 1;
        var yyyy = currentDate.getFullYear();

        if (dd < 10) {
            dd = '0' + dd
        }
        if (mm < 10) {
            mm = '0' + mm
        }

        return mm + '/' + dd + '/' + yyyy;
    }

    // load the controller's module
    beforeEach(module(HygieiaConfig.module));
    beforeEach(module(HygieiaConfig.module + '.core'));

    beforeEach(module(function ($provide) {
        $provide.factory('cloudData', function () {

            return {
                getAWSGlobalData: getAWSGlobalData,
                getAWSInstancesByTag: getAWSInstancesByTag
            };

            function getAWSGlobalData() {
                return AWSGlobalData;
            }

            function getAWSInstancesByTag() {
                return null;
            }
        })
    }));


    // inject the required services and instantiate the controller
    beforeEach(
        function () {
            inject(function ($rootScope, cloudData, $controller) {
                scope = $rootScope.$new();

                scope.widgetConfig = {
                    options: {
                        tag: "MyTag"
                    }
                };

                controller = $controller('CloudWidgetViewController', {
                    $scope: scope,
                    cloudData: cloudData
                });
            })
        });


    describe('calculateUtilization()', function () {
        describe('When I call calculateUtilization', function () {
            describe('And AWS instances is undefined', function () {
                it('Then I expect "N/A" to be returned', function () {

                    //Arrange
                    controller.instancesByTag = undefined;
                    var expected = 'N/A';

                    //Act
                    var actual = controller.calculateUtilization();

                    //Assert
                    expect(actual).toBe(expected);
                });
            });

            describe('And no AWS instances exists', function () {
                it('Then I expect "N/A" to be returned', function () {

                    //Arrange
                    controller.instancesByTag = [];
                    var expected = 'N/A';

                    //Act
                    var actual = controller.calculateUtilization();

                    //Assert
                    expect(actual).toBe(expected);
                });
            });

            describe('And AWS instances exist with the cpu utilization values', function () {
                it('Then I expect the average of the cpu utilization to be returned', function () {

                    //Arrange
                    controller.instancesByTag = [
                        { "cpuUtilization": 10 },
                        { "cpuUtilization": 20 },
                        { "cpuUtilization": 30 },
                    ];
                    var expected = 20;

                    //Act
                    var actual = controller.calculateUtilization();

                    //Assert
                    expect(actual).toBe(expected);
                });
            });



        });
    });


    describe('getSortDirection()', function () {
        describe('When I call getSortDirection', function () {
            describe('And no sort key has been created', function () {
                it('Then I expect "unsorted" to be returned', function () {

                    //Arrange
                    var key = 'AMI';
                    var expected = "unsorted";

                    //Act
                    var actual = controller.getSortDirection(key);

                    //Assert
                    expect(actual).toBe(expected);
                });
            });

            describe('And the sort key is "-"', function () {
                it('Then I expect "sort-amount-desc" to be returned', function () {

                    //Arrange
                    var key = 'AMI';
                    var expected = "sort-amount-desc";

                    //Act
                    controller.changeSortDirection(key);
                    var actual = controller.getSortDirection(key);

                    //Assert
                    expect(actual).toBe(expected);
                });
            });

            describe('And the sort key is "+"', function () {
                it('Then I expect "sort-amount-asc" to be returned', function () {

                    //Arrange
                    var key = 'AMI';
                    var expected = "sort-amount-asc";

                    //Act
                    controller.changeSortDirection(key);
                    controller.changeSortDirection(key);
                    var actual = controller.getSortDirection(key);

                    //Assert
                    expect(actual).toBe(expected);
                });
            });

        });
    });



    describe('changeSortDirection()', function () {
        describe('When I call changeSortDirection', function () {
            describe('And no sort key has been created', function () {
                it('Then I expect to have one sort key added to sortType with a "-" at the front', function () {

                    //Arrange
                    var key = 'AMI';
                    var expected = ['-AMI'];

                    //Act
                    controller.changeSortDirection(key);

                    //Assert
                    expect(angular.equals(controller.sortType, expected)).toBeTruthy();
                });
            });

            describe('And one non-matching sort key exists', function () {
                it('Then I expect to have a new sort key added to the front of sortType with a "-" at the front', function () {

                    //Arrange
                    var firstKey = 'instanceID';
                    var secondKey = 'AMI';
                    var expected = ['-AMI','-instanceID'];

                    //Act
                    controller.changeSortDirection(firstKey);
{'fraction_non_alphanumeric': 0.10716777988236467, 'fraction_numerical': 0.008573422390589173, 'mean_word_length': 1.2313167259786477, 'pattern_counts': {'":': 23, '<': 7, '<?xml version=': 0, '>': 3, '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': '2555111', 'n_tokens_mistral': 2578, 'n_tokens_neox': 2281, 'n_words': 598}