1. 程式人生 > >【JS程式碼提高--004】:JS 的函式裡延遲返回結果已經UT的編寫

【JS程式碼提高--004】:JS 的函式裡延遲返回結果已經UT的編寫

//upload.js

"use strict";

var _ = require("underscore");
var Promise = require("bluebird");
var mzframe = require("@rcp/mzframe");

function emptyMnlParametersInRCurrentIfNeeded(rCurrentContents, rCurrentSbtsId) {
    var isNodeoamStartupNotFinished = function(contents) {
        return !(contents.swpackage &&
            (contents.swpackage.buildDescriptorId || contents.swpackage.cbtsSoftwareReleaseVersion));
    };

    var
emptyMnlParamters = function(rCurrentContents) { return Promise.try(function() { var needChangedContents = rCurrentContents; var mnlContents = needChangedContents.mrbts[rCurrentSbtsId].mnl; var firstKey = _.first(_.keys(mnlContents)); var isMnlParametersExists = mnlContents && mnlContents[firstKey] && mnlContents[firstKey].properties && mnlContents[firstKey].properties.parameters; if
(isMnlParametersExists) { var feature1058Parameters = ["activeSWReleaseVersion", "passiveSWReleaseVersion", "plannedSWReleaseVersion", "activeSWActivationTime"]; _.each(mnlContents[firstKey].properties.parameters, function(val, key) { if (_.contains(feature1058Parameters, key)) { mnlContents[firstKey].properties.parameters[key] = ""
; } }); } return needChangedContents; }); }; return new Promise(function(resolve) { mzframe.BimResource.read("/mz/p/v1/pbts/1/fsm/1/filesystem/2") .then(function(contents) { if (isNodeoamStartupNotFinished(contents)) { resolve(emptyMnlParamters(rCurrentContents)); } else { setTimeout(function timer() { resolve(rCurrentContents); }, 3 * 1000); } }) .catch(function(err) { logger.warn("emptying Mnl in rCurrent encounters something wrong! mnl parameters not changed, failed reason: %s", err.message); resolve(rCurrentContents); }); }); } module.exports.emptyMnlParametersInRCurrentIfNeeded = emptyMnlParametersInRCurrentIfNeeded;
"use strict";
var assert = require("chai").assert;
var upload = require("../index");
var inq = require("inquisitor");
var Promise = require("bluebird");
var mzframe = require("***");
var zurvan = require("zurvan");

 describe("emptyMnlParametersInRCurrentIfNeeded", function() {
     this.timeout(15 * 1000);
     var suite;

     beforeEach(function() {
         suite = {};
         suite.rCurrentContents = {
             "mrbts": {
                 "123": {
                     "properties": {
                         "distName": "MRBTS-123",
                         "class": "MRBTS",
                         "parameters": {
                             "passUnitList": [
                                 {
                                     "btsName": "Nokia China HangZhou"
                                 }
                             ]
                         }
                     },
                     "mnl": {
                         "1": {
                             "properties": {
                                 "distName": "MRBTS-123/MNL-1",
                                 "class": "MNL",
                                 "parameters": {
                                     "productVariantPlanned": "55gClassicalBTS55",
                                     "activeSWReleaseVersion": "version 1.0",
                                     "passiveSWReleaseVersion": "version 2.0",
                                     "activeSWActivationTime": "active time",
                                     "plannedSWReleaseVersion": "version 3.0"
                                 }
                             }
                         }
                     }
                 }
             },
             "transientState": {
                 "metadataVersion": "SBTS5G18A_1804_002"
             }
         };
         suite.rCurrentSbtsId = 123;

         return zurvan.interceptTimers();
     });

     afterEach(function() {
         suite = null;
         return zurvan.releaseTimers();
     });

     it("should empty mnl parameters when nodeoam startup not completed", function() {
         var expectEmptyMnl = {
             "productVariantPlanned": "55gClassicalBTS55",
             "activeSWReleaseVersion": "",
             "passiveSWReleaseVersion": "",
             "activeSWActivationTime": "",
             "plannedSWReleaseVersion": ""
         };

         inq.mock(mzframe.BimResource, "read");
         inq.expect(mzframe.BimResource.read).once.with.args("/mz/p/v1/pbts/1/fsm/1/filesystem/2")
             .returns(Promise.resolve({swpackage: {}}));

         upload.emptyMnlParametersInRCurrentIfNeeded(suite.rCurrentContents, suite.rCurrentSbtsId)
             .then(function(data) {
                 assert.deepEqual(data.mrbts[suite.rCurrentSbtsId].mnl["1"].properties.parameters, expectEmptyMnl);
             });
     });

     it("should not change mnl parameters when nodeoam startup completed", function(done) {
         var expectNotChangeMnl = {
             "productVariantPlanned": "55gClassicalBTS55",
             "activeSWReleaseVersion": "version 1.0",
             "passiveSWReleaseVersion": "version 2.0",
             "activeSWActivationTime": "active time",
             "plannedSWReleaseVersion": "version 3.0"
         };

         inq.mock(mzframe.BimResource, "read");
         inq.expect(mzframe.BimResource.read).once.with.args("/mz/p/v1/pbts/1/fsm/1/filesystem/2")
             .returns(
                 Promise.resolve({swpackage: {
                     "cbtsSoftwareReleaseVersion": "5GBTS_18A_4.8.334",
                     "buildDescriptorId": "5G18A_FSMK_0004_000008_000334"
                 }})
             );
         var actualResult;
         zurvan.advanceTime(4 * 1000)
             .then(function() {
                 assert.deepEqual(actualResult.mrbts[suite.rCurrentSbtsId].mnl["1"].properties.parameters, expectNotChangeMnl);
             }).then(done);

         upload.emptyMnlParametersInRCurrentIfNeeded(suite.rCurrentContents, suite.rCurrentSbtsId)
             .then(function(data) {
                 actualResult = data;
             });
     });

     it("should not change mnl parameters when exception happens", function() {
         var expectNotChangeMnl = {
             "productVariantPlanned": "55gClassicalBTS55",
             "activeSWReleaseVersion": "version 1.0",
             "passiveSWReleaseVersion": "version 2.0",
             "activeSWActivationTime": "active time",
             "plannedSWReleaseVersion": "version 3.0"
         };

         inq.mock(mzframe.BimResource, "read");
         inq.expect(mzframe.BimResource.read).once.with.args("/mz/p/v1/pbts/1/fsm/1/filesystem/2")
             .returns(
                 Promise.reject(new Error("read bim error!"))
             );
         upload.emptyMnlParametersInRCurrentIfNeeded(suite.rCurrentContents, suite.rCurrentSbtsId)
             .then(function(data) {
                 assert.deepEqual(data.mrbts[suite.rCurrentSbtsId].mnl["1"].properties.parameters, expectNotChangeMnl);
             });
     });

     it("should get changed parameters when mnl changed after nodeoam startup completed before timeout", function(done) {
         var expectnewChangedMnl = {
             "productVariantPlanned": "ChangedName",
             "activeSWReleaseVersion": "version 1.0_changed",
             "passiveSWReleaseVersion": "version 2.0",
             "activeSWActivationTime": "active time",
             "plannedSWReleaseVersion": "version 3.0"
         };

         inq.mock(mzframe.BimResource, "read");
         inq.expect(mzframe.BimResource.read).once.with.args("/mz/p/v1/pbts/1/fsm/1/filesystem/2")
             .returns(
                 Promise.resolve({swpackage: {
                     "cbtsSoftwareReleaseVersion": "5GBTS_18A_4.8.334",
                     "buildDescriptorId": "5G18A_FSMK_0004_000008_000334"
                 }})
             );
         var actualResult;
         zurvan.advanceTime(2 * 1000)
             .then(function() {
                 suite.rCurrentContents.mrbts[suite.rCurrentSbtsId].mnl["1"].properties.parameters = {
                     "productVariantPlanned": "ChangedName",
                     "activeSWReleaseVersion": "version 1.0_changed",
                     "passiveSWReleaseVersion": "version 2.0",
                     "activeSWActivationTime": "active time",
                     "plannedSWReleaseVersion": "version 3.0"
                 };

                 return zurvan.advanceTime(2 * 1000).then(function() {
                     assert.deepEqual(actualResult.mrbts[suite.rCurrentSbtsId].mnl["1"].properties.parameters, expectnewChangedMnl);
                 });
             }).then(done);

         upload.emptyMnlParametersInRCurrentIfNeeded(suite.rCurrentContents, suite.rCurrentSbtsId)
             .then(function(data) {
                 actualResult = data;
             });
        });
  });