This package has been deprecated

Author message:

Use @varkes/odata-mock instead

varkes-odata-mock

0.3.11 • Public • Published

OData Mock

npm version

Mocks rest calls given an OData specification

After startup, you have access to:

Starting the application

Run local

To run it local, specify a config file (as located in the /test folder) like that:

npm install
node server/server <varkes_config.js>

Run local using docker

To run it using docker, call:

docker run -p 10000:10000 eu.gcr.io/kyma-project/incubator/develop/varkes-odata-mock:latest

Run in Kyma

To run the mock using Kyma as runtime envrironment, run the following kubectl command to set up a namespace:

kubectl create namespace mocks
kubectl label namespace mocks env=true

and to deploy the mock

kubectl apply -n mocks -f https://raw.githubusercontent.com/kyma-incubator/varkes/master/odata-mock/deployment/deployment.yaml

Features

  • Supports OData edmx specification
    Copy the edmx specification to the [EDMX_MODEL_SPECIFICATION.xml]((https://github.com/kyma-incubator/varkes/blob/master/examples/odata-mock-app/EDMX_MODEL_SPECIFICATION.xml) file and the Engine will automatically create data model files for the entities specified in the specification

  • Records Every Request made to the node
    Creates a requests.log file that contains the urls being called, the header of the request and the body of the request if exists using the morgan logging framework.

  • Returns the OData specification as metadata
    By calling /odata/$metadata user can see the OData edmx specification being use in xml format

  • Returns a dummy OAuth2 token
    By posting to url odata/authorizationserver/oauth/token the OAuth2 requirements in the request body, user can get a dummy OAuth2 token

  • Based on the n-odata package
    the odata-mock application is based on the n-odata-server project, which is based on loopback

Installation and Use

Install using NPM.

npm install

Then you need to copy your OData edmx into the common directory as EDMX_MODEL_SPECIFICATION.xml
OR you could simply change the path in the config.js file specified by the "specification" element
When you run the mock application for the first time the parser.js module creates a javascript file and a json file for every entity defined in the edmx specifiaction using the templates modelTemplate.json as the json template ( for every model the name is the entity name and the plural is the entity name and adding an 's' at the end so if the entity name is 'user' the plural used in the get endpoint is 'users') and jsModel.txt as the js template replacing the 'placeholder' substring with the entity name. Also the parser.js module creates the definition of these entities in the model-config.json file.You can think of this file as a catalog for our database defining User Roles, security permission along side the entity definitions.
The following is an example to an entity definition in the edmx file

<EntityType Name="AssignedInterestsType" sap:label="Marketing: Campaign Template-Interest" sap:content-version="1">
    <Key>
     <PropertyRef Name="ItemOfInterest"/>
     <PropertyRef Name="CampaignTemplate"/>
    </Key>
    <Property Name="ItemOfInterest" Type="Edm.String" Nullable="false" MaxLength="40" sap:display-format="UpperCase" sap:label="Item of Interest"/>
    <Property Name="CampaignTemplate" Type="Edm.String" Nullable="false" MaxLength="10" sap:display-format="UpperCase" sap:label="Campaign ID"/>
   </EntityType>

You can add data to an Entity using the [data.json](https://github.com/kyma-incubator/varkes/blob/master/examples/odata-mock-app/storage/data.json) file. So for the above entity the file will look like this
{
  "ids": {
    "User": 1,
    "AccessToken": 1,
    "ACL": 1,
    "RoleMapping": 1,
    "Role": 1,
    "person": 39,
    "AssignedInterestsType": 2,
    "TeamMembersType": 2
  },
  "models": {
    "User": {},
    "AccessToken": {},
    "ACL": {},
    "RoleMapping": {},
    "Role": {},
    "AssignedInterestsType": {
      "1": "{\"ItemOfInterest\":\"Item1\",\"CampaignTemplate\":\"Item2\",\"id\":1}"
    }
  }
}

Node js code

The entry point for the application is the server/server.js file which reads the edmx file EDMX_MODEL_SPECIFICATION.xml using parser.js
The mock application offers the following

  • Parse the edmx specification file and creates entity files that represent databases

  • Provide custom Error messages for given status codes given in the config.js:
    The error messages are written in the config file as a json key called error_messages as follows:

error_messages: {
        500: '{"error":\"Something went Wrong\"}',
        401: '{"error":\"401 Entity does not exist\"}',
        404: '{"error":\"404 Bad URL\"}'
    }

This json object is processed by the [routes.js](https://github.com/kyma-incubator/varkes/blob/master/examples/odata-mock-app/server/boot/routes.js) which checks if the error code exists in the config file and if so it sends it's corresponding message as response as follows. ````javascript function modifyResponseBody(req, res, next) { var oldSend = res.send;
    res.send = function (data) {

        if (!arguments[0] || !arguments[0].statusCode) {
            arguments[0] = {};
            arguments[0].statusCode = 500;
        }
        if (app.config.error_messages.hasOwnProperty(arguments[0].statusCode)) {
            arguments[0] = app.config.error_messages[arguments[0].statusCode];
        }
        oldSend.apply(res, arguments);
    }
    next();
}

app.use(modifyResponseBody);
Note: The messages (ex. '{"error":\"Something went Wrong\"}') could be any string doesn't have to be a json-like string<br>


Readme

Keywords

none

Package Sidebar

Install

npm i varkes-odata-mock

Weekly Downloads

15

Version

0.3.11

License

Apache-2.0

Unpacked Size

36.4 kB

Total Files

18

Last publish

Collaborators

  • badrawi