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

100% Statements 24/24
100% Branches 4/4
100% Functions 9/9
100% Lines 23/23

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    1x 1x                                   11x 43x     11x 1x     11x 11x 1x     11x 20x   11x 3x     11x 1x         10x 10x 8x         4x 2x 1x     1x       1x  
"use strict";
 
const _ = require("lodash");
const AssociationSetEnd = require("./AssociationSetEnd");
 
/**
 * Envelops an association set.
 *
 * https://docs.microsoft.com/en-us/openspecs/windows_protocols/mc-csdl/84fdfd02-7b12-4aa3-a2eb-51bab109f439
 * (not present in OASIS-CSDL)
 *
 * @class AssociationSet
 */
class AssociationSet {
  /**
   * Creates an instance of AssociationSet.
   * @param {Object} rawMetadata raw metadata object for the association set
   * @param {CsdlSchema} schema to resolve association reference
   * @memberof Association
   */
  constructor(rawMetadata, schema) {
    Object.defineProperty(this, "raw", {
      get: () => rawMetadata,
    });
 
    Object.defineProperty(this, "name", {
      get: () => rawMetadata.$.Name,
    });
 
    let association = schema.resolveModelPath(rawMetadata.$.Association);
    Object.defineProperty(this, "association", {
      get: () => association,
    });
 
    let ends = _.get(rawMetadata, "End", []).map(
      (p) => new AssociationSetEnd(p, schema, association)
    );
    Object.defineProperty(this, "ends", {
      get: () => ends,
    });
 
    if (ends.length !== 2) {
      throw new Error(
        `AssociationSet MUST have exactly two End elements defined, but it has ${ends.length}.`
      );
    }
 
    let extensions = [];
    Object.defineProperty(this, "extensions", {
      get: () => extensions,
    });
  }
 
  getEndByRole(role) {
    let end = this.ends.find((e) => e.role === role);
    if (!end) {
      throw new Error(`AssociationSet end with role ${role} not found.`);
    }
 
    return end;
  }
}
 
module.exports = AssociationSet;