All files / lib/model/nw/dataSource FunctionImport.js

100% Statements 22/22
100% Branches 8/8
100% Functions 9/9
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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    1x     133x 181x         19x       19x             19x         19x 19x       4x                                               19x 19x 19x 19x 19x                     2x 2x 1x         1x                   3x       1x         1x  
"use strict";
 
const FunctionImportParameter = require("./FunctionImportParameter");
 
function defineProperty(element, prop, value) {
  Object.defineProperty(element, prop, {
    get: () => value,
  });
}
 
function initProperties(functionImport, schema) {
  let entitySet = functionImport.raw.$.EntitySet
    ? schema.getEntityContainer().getEntitySet(functionImport.raw.$.EntitySet)
    : undefined;
 
  defineProperty(
    functionImport,
    "httpMethod",
    functionImport.raw.$["m:HttpMethod"] || "GET"
  );
 
  // ReturnType is not mandatory in MC-CSLD, but current sap implementation (segw) requires return type now
  defineProperty(
    functionImport,
    "returnType",
    schema.getType(functionImport.raw.$.ReturnType)
  );
  defineProperty(functionImport, "entitySet", entitySet);
  defineProperty(
    functionImport,
    "parameters",
    (functionImport.raw.Parameter || []).map(
      (p) => new FunctionImportParameter(p, schema)
    )
  );
}
 
/**
 * Envelops an function import.
 *
 * There are substantial differences between MC-CSLD and OASIS-CSDL function imports.
 * SAP implementation follows MS-CSDL.
 *
 * https://docs.microsoft.com/en-us/openspecs/windows_protocols/mc-csdl/d867e86a-6905-4d05-9145-d677b11f8c39
 * http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/cs01/odata-csdl-xml-v4.01-cs01.html#sec_FunctionImport
 *
 * @class FunctionImport
 */
class FunctionImport {
  /**
   * Creates an instance of FunctionImport.
   * @param {Object} rawMetadata raw metadata object for the function import
   * @param {CsdlSchema} schema to resolve association reference
   * @memberof FunctionImport
   */
  constructor(rawMetadata, schema) {
    defineProperty(this, "raw", rawMetadata);
    defineProperty(this, "name", rawMetadata.$.Name);
    let extensions = [];
    defineProperty(this, "extensions", extensions);
    initProperties(this, schema);
  }
 
  /**
   * Gets parameter by its name.
   *
   * @param {String} [name] parameter name
   * @returns {FunctionImportParameter} function import parameter
   * @memberof FunctionImport
   */
  getParameter(name) {
    let param = this.parameters.find((p) => p.name === name);
    if (!param) {
      throw new Error(
        `Parameter ${name} not found for function import ${this.name}`
      );
    }
 
    return param;
  }
 
  /**
   * Gets legacy api object. (XML casing, maybe some other changes.)
   *
   * @returns {Object} legacy api object
   * @memberof FunctionImport
   */
  getLegacyApiObject() {
    return {
      Name: this.name,
      ReturnType: this.returnType.namespaceQualifiedName,
      HttpMethod: this.httpMethod,
      Parameter: this.parameters.map((p) => p.getLegacyApiObject()),
    };
  }
}
 
module.exports = FunctionImport;