Code coverage report for lib\services\queue\models\queuemessageresult.js

Statements: 95.12% (39 / 41)      Branches: 90% (18 / 20)      Functions: 100% (5 / 5)      Lines: 95.12% (39 / 41)      Ignored: none     

All files » lib/services/queue/models/ » queuemessageresult.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                                  1 1 1   1   1 29 13     29 1     29       29                     1 12 12   12 11 11 7   4     11       1     12     1 16 16 102 16 10   6     86       16     1 13   13 26 2       13 13     1
// 
// Copyright (c) Microsoft and contributors.  All rights reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//   http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 
// See the License for the specific language governing permissions and
// limitations under the License.
// 
 
// Module dependencies.
var azureCommon = require('./../../../common/common');
var xmlbuilder = azureCommon.xmlbuilder;
var Constants = azureCommon.Constants;
 
var HeaderConstants = Constants.HeaderConstants;
 
function QueueMessageResult(queue, messageid, popreceipt, metadata) {
  if (queue) {
    this.queue = queue;
  }
 
  if (messageid) {
    this.messageid = messageid;
  }
 
  Iif (popreceipt) {
    this.popreceipt = popreceipt;
  }
 
  Iif (metadata) {
    this.metadata = metadata;
  }
}
 
/**
* Builds an XML representation for a queue message
*
* @param  {string}  messageJs The queue message.
* @return {string} The XML queue message.
*/
QueueMessageResult.serialize = function (messageJs, encode) {
  var doc = xmlbuilder.create();
  doc = doc.begin(Constants.QueueConstants.QUEUE_MESSAGE_ELEMENT, { version: '1.0', encoding: 'utf-8' });
 
  if (messageJs) {
    var message;
    if (encode) {
      message = new Buffer(messageJs).toString('base64');
    } else {
      message = messageJs;
    }
    
    doc.ele(Constants.QueueConstants.MESSAGE_TEXT_ELEMENT)
        .txt(message)
      .up();
  } else {
    doc.ele(Constants.QueueConstants.MESSAGE_TEXT_ELEMENT).up();
  }
 
  return doc.doc().toString();
};
 
QueueMessageResult.parse = function (messageXml, encode) {
  var queueMessageResult = new QueueMessageResult();
  for (var property in messageXml) {
    if (property === Constants.QueueConstants.MESSAGE_TEXT_ELEMENT) {
      if (encode) {
        queueMessageResult.messagetext = new Buffer(messageXml[property], 'base64').toString();
      } else {
        queueMessageResult.messagetext = messageXml[property];
      }
    } else {
      queueMessageResult[property.toLowerCase()] = messageXml[property];
    }
  }
 
  return queueMessageResult;
};
 
QueueMessageResult.prototype.getPropertiesFromHeaders = function (headers) {
  var self = this;
 
  var setmessagePropertyFromHeaders = function (messageProperty, headerProperty) {
    if (!self[messageProperty] && headers[headerProperty.toLowerCase()]) {
      self[messageProperty] = headers[headerProperty.toLowerCase()];
    }
  };
 
  setmessagePropertyFromHeaders('popreceipt', HeaderConstants.POP_RECEIPT);
  setmessagePropertyFromHeaders('timenextvisible', HeaderConstants.TIME_NEXT_VISIBLE);
};
 
module.exports = QueueMessageResult;