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

## Statements: 100% (24 / 24) Branches: 100% (2 / 2) Functions: 100% (6 / 6) Lines: 100% (24 / 24)

All files » lib/services/server/ » unregistration.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

    1

    1

    1
    23
    23
    1

    1
    1

    22

    22
    22

    1
    23

    23

    1
    22

    22

    1
    23

    23

    1
    57

    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 async = require('async'),
        logger = require('logops'),
        context = {
            op: 'LWM2MLib.Unregistration'
        },
        registry;

    /**
     *  Generates the end of request handler that will generate the final response to the COAP Client.
     */
    function endUnregistration(req, res) {
        return function (error, result) {
            if (error) {
                logger.debug(context, 'Registration request ended up in error [%s] with code [%s]',
                    error.message, error.code);

                res.code = error.code;
                res.end(error.message);
            } else {
                logger.debug(context, 'Unregistration request ended successfully');

                res.code = '2.02';
                res.end('');
            }
        };
    }

    /**
     * Parse the pathname of the request to extract the device id and return it through the callback.
     *
     * @param {Object} req           Arriving COAP Request to be processed.
     */
    function parsePath(req, callback) {
        var pathElements = req.urlObj.pathname.split('/');

        callback(null, pathElements[2]);
    }

    /**
     * Invoke the user handler for this operation with the unregistered object as its only argument.
     *
     * @param {Function} handler        User handler for the unregistration.
     * @param {Object} removedObj       The removed Device object.
     */
    function applyHandler(handler, removedObj, callback) {
        logger.debug(context, 'Calling user handler for unregistration actions for device [%s]', removedObj.name);

        handler(removedObj, callback);
    }

    /**
     * Handle the registration operation.
     *
     * @param {Object} req          Arriving COAP Request to be handled.
     * @param {Object} res          Outgoing COAP Response.
     * @param {Function} handler    User handler to be executed if everything goes ok.
     */
    function handleUnregistration(req, res, handler) {
        logger.debug(context, 'Handling unregistration request');

        async.waterfall([
            async.apply(parsePath, req),
            registry.unregister,
            async.apply(applyHandler, handler)
        ], endUnregistration(req, res));
    }

    function setRegistry(newRegistry) {
        registry = newRegistry;
    }

    exports.init = setRegistry;
    exports.handle = handleUnregistration;