# Code coverage report for lib/services/server/mongodbDeviceRegistry.js

## Statements: 76.47% (52 / 68) Branches: 43.75% (7 / 16) Functions: 70.59% (12 / 17) Lines: 76.47% (52 / 68)

All files » lib/services/server/ » mongodbDeviceRegistry.js

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199

    1

    1

    1
    2

    2

    2
    2

    2
    2

    2
    1

    1

    1
    2
    2
    1

    1

    1

    1
    2

    1
    1
    7

    7

    1
    7

    7
    7
    7
    7

    7

    7

    1

    1
    4
    4

    4

    1

    1
    1

    1

    1

    1
    4

    1
    1
    1
    1
    1
    1
    1
    1/*
     * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
     *
     * This file is part of iotagent-lwm2m-lib
     *
     * iotagent-lwm2m-lib is free software: you can redistribute it and/or
     * modify it under the terms of the GNU Affero General Public License as
     * published by the Free Software Foundation, either version 3 of the License,
     * or (at your option) any later version.
     *
     * iotagent-lwm2m-lib is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     * See the GNU Affero General Public License for more details.
     *
     * You should have received a copy of the GNU Affero General Public
     * License along with iotagent-lwm2m-lib.
     * If not, seehttp://www.gnu.org/licenses/.
     *
     * For those usages not covered by the GNU Affero General Public License
     * please contact with::[contacto@tid.es]
     */

    'use strict';

    var errors = require('../../errors'),
        dbService = require('../model/dbConn'),
        Device = require('../model/Device');

    /**
     * Generic function to retrieve a device based on a parameter value. This is an auxiliary function meant to abstract
     * all the getBySomething functions.
     *
     * @param {String} parameterName        Name of the parameter that is used to identify the device.
     * @param {String} parameterValue       Value of the parameter to check.
     */
    function getByParameter(parameterName, parameterValue, callback) {
        var query,
            filter = {};

        filter[parameterName] = parameterValue;

        query = Device.model.findOne(filter);
        query.select({__v: 0});

        query.exec(function handleGet(error, data) {
            Iif (error) {
                callback(errors.InternalDbError(error));
            } else if (data) {
                callback(null, data);
            } else {
                callback(new errors.DeviceNotFound(parameterValue));
            }
        });
    }

    /**
     * Auxiliary function to transform a Mongoose DAO to a plain JS Object as part of the callback process. It returns a
     * function that will invoke the callback passed as a parameter transforming the received model object before.
     */
    function toObject(callback) {
        return function (error, result) {
            if (error) {
                callback(error);
            } else {
                callback(null, result.toObject());
            }
        };
    }

    /**
     * Retrieves from the registry the object identified by the given id.
     *
     * @param {String} id       Id of the object to be retrieved.
     */
    function getObject(id, callback) {
        getByParameter('id', id, toObject(callback));
    }

    /**
     * Gets the device that has the device name passed as a parameter (should be unique) or return a DeviceNotFound error
     * in case none exist.
     *
     * @param {String} deviceName       Name of the device to retrieve.
     */
    function getByName(deviceName, callback) {
        getByParameter('name', deviceName, toObject(callback));
    }

    /**
     * Inserts the given object in the registry. The generated ID is returned through the callback.
     *
     * @param {Object} object       Object to insert into the registry.
     */
    function register(object, callback) {
        function saveDeviceHandler(error, deviceDAO) {
            Iif (error) {
                callback(errors.InternalDbError(error));
            } else {
                callback(null, deviceDAO.id);
            }
        }

        function mongoStore(innerCb) {
            var deviceObj = new Device.model();

            deviceObj.address = object.address;
            deviceObj.lifetime = object.lifetime;
            deviceObj.name = object.name;
            deviceObj.type = object.type;

            deviceObj.save(innerCb);
        }

        mongoStore(saveDeviceHandler);
    }

    /**
     * Removes the object identified by this id from the registry. The removed object is passed as the first callback
     * parameter.
     *
     * @param {Integer} id          Identifier of the object to be removed.
     */
    function unregister(id, callback) {
        Device.model.findOneAndRemove({ id: id }, function(error, device) {
            if (error) {
                callback(errors.InternalDbError(error));
            } else if (device) {
                callback(null, device.toObject());
            } else {
                callback(errors.DeviceNotFound(id));
            }
        });
    }

    /**
     * Remove all the objects from the registry.
     */
    function clean(callback) {
        Device.model.remove({}, function(error, number) {
            Iif (error) {
                callback(errors.InternalDbError(error));
            } else {
                callback(null);
            }
        });
    }

    /**
     * Update the object identified with the given id with the object value passed as a parameter.
     *
     * @param {String} id       Id of the object to update.
     * @param {Object} obj      New object value to insert in the registry.
     */
    function update(id, obj, callback) {
        getByParameter('id', id, function(error, objDAO) {
            if (error) {
                callback(error);
            } else {
                objDAO.id = obj.id;
                objDAO.type = objDAO.type;
                objDAO.save(toObject(callback));
            }
        });
    }

    /**
     * Returns an array of all the devices as the parameter of the callback.
     */
    function list(callback) {
        var condition = {},
            query;

        query = Device.model.find(condition).sort();

        query.exec(callback);
    }

    /**
     * Initializes the device registry based on the parameter found in the configuration. The MongoDB config object should
     * contain at least the host string needed to connect to MongoDB and the database name where to store the device info.
     * The configuration object to use should be the one corresponding to the general server configuration, although all
     * the Mongo specific information should be stored under the 'deviceRegistry' section.
     *
     * @param {Object} config           Configuration object containing a deviceRegistry attribute with the info.
     */

    function init(newConfig, callback) {
        dbService.init(newConfig.deviceRegistry.host, newConfig.deviceRegistry.db, callback);
    }

    exports.register = register;
    exports.unregister = unregister;
    exports.get = getObject;
    exports.update = update;
    exports.clean = clean;
    exports.list = list;
    exports.getByName = getByName;
    exports.init = init;