UNPKG

7.18 kBMarkdownView Raw
1OData Mock
2============================
3#### Mocks rest calls given an OData specification
4
5
6Features
7---------------------------
8
9- **Supports OData edmx specification** <br>
10Copy 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
11
12- **Records Every Request made to the node** <br>
13Creates 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](https://www.npmjs.com/package/morgan) logging framework.
14
15- **Returns the OData specification as metadata** <br>
16By calling `/odata/$metadata` user can see the OData edmx specification being use in xml format
17
18- **Returns a dummy OAuth2 token** <br>
19By posting to url `odata/authorizationserver/oauth/token` the OAuth2 requirements in the request body, user can get a dummy OAuth2 token
20
21- **Based on the n-odata package** <br>
22the odata-mock application is based on the [n-odata-server](https://github.com/htammen/n-odata-server) project, which is based on [loopback](https://loopback.io/)
23
24Installation and Use
25--------------------------
26Install using [NPM](https://docs.npmjs.com/getting-started/what-is-npm).
27
28````bash
29npm install
30````
31Then you need to copy your OData edmx into the common directory as [EDMX_MODEL_SPECIFICATION.xml](https://github.com/kyma-incubator/varkes/blob/master/examples/odata-mock-app/EDMX_MODEL_SPECIFICATION.xml)<br>
32OR you could simply change the path in the [config.js](https://github.com/kyma-incubator/varkes/blob/master/examples/odata-mock-app/config.js) file specified by the "specification_file" element
33<br>
34When you run the mock application for the first time the [parser.js](https://github.com/kyma-incubator/varkes/blob/master/examples/odata-mock-app/common/utility/parser.js) module creates a javascript file and a json file for every entity defined in the edmx specifiaction using the templates [modelTemplate.json](https://github.com/kyma-incubator/varkes/blob/master/examples/odata-mock-app/common/models/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](https://github.com/kyma-incubator/varkes/blob/master/examples/odata-mock-app/common/models/jsModel.txt) as the js template replacing the 'placeholder' substring with the entity name.
35Also the [parser.js](https://github.com/kyma-incubator/varkes/blob/master/examples/odata-mock-app/common/utility/parser.js) module creates the definition of these entities in the [model-config.json](https://github.com/kyma-incubator/varkes/blob/master/examples/odata-mock-app/server/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.
36<br>
37The following is an example to an entity definition in the edmx file
38
39````xml
40<EntityType Name="AssignedInterestsType" sap:label="Marketing: Campaign Template-Interest" sap:content-version="1">
41 <Key>
42 <PropertyRef Name="ItemOfInterest"/>
43 <PropertyRef Name="CampaignTemplate"/>
44 </Key>
45 <Property Name="ItemOfInterest" Type="Edm.String" Nullable="false" MaxLength="40" sap:display-format="UpperCase" sap:label="Item of Interest"/>
46 <Property Name="CampaignTemplate" Type="Edm.String" Nullable="false" MaxLength="10" sap:display-format="UpperCase" sap:label="Campaign ID"/>
47 </EntityType>
48````
49<br>
50You 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
51
52````json
53{
54 "ids": {
55 "User": 1,
56 "AccessToken": 1,
57 "ACL": 1,
58 "RoleMapping": 1,
59 "Role": 1,
60 "person": 39,
61 "AssignedInterestsType": 2,
62 "TeamMembersType": 2
63 },
64 "models": {
65 "User": {},
66 "AccessToken": {},
67 "ACL": {},
68 "RoleMapping": {},
69 "Role": {},
70 "AssignedInterestsType": {
71 "1": "{\"ItemOfInterest\":\"Item1\",\"CampaignTemplate\":\"Item2\",\"id\":1}"
72 }
73 }
74}
75````
76
77
78Node js code
79--------------------------
80
81The entry point for the application is the server/server.js file which reads the edmx file [EDMX_MODEL_SPECIFICATION.xml](https://github.com/kyma-incubator/varkes/blob/master/examples/odata-mock-app/EDMX_MODEL_SPECIFICATION.xml) using [parser.js](https://github.com/kyma-incubator/varkes/blob/master/examples/odata-mock-app/common/utility/parser.js)
82<br>
83The mock application offers the following
84- **Parse the edmx specification file and creates entity files that represent databases** <br>
85
86- **Provide custom Error messages for given status codes given in the [config.js](https://github.com/kyma-incubator/varkes/blob/master/examples/odata-mock-app/config.js):** <br>
87The error messages are written in the config file as a json key called error_messages as follows:<br>
88
89````javascript
90error_messages: {
91 500: '{"error":\"Something went Wrong\"}',
92 401: '{"error":\"401 Entity does not exist\"}',
93 404: '{"error":\"404 Bad URL\"}'
94 }
95````
96<br>
97This 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.
98````javascript
99 function modifyResponseBody(req, res, next) {
100 var oldSend = res.send;
101
102 res.send = function (data) {
103
104 if (!arguments[0] || !arguments[0].statusCode) {
105 arguments[0] = {};
106 arguments[0].statusCode = 500;
107 }
108 if (app.config.error_messages.hasOwnProperty(arguments[0].statusCode)) {
109 arguments[0] = app.config.error_messages[arguments[0].statusCode];
110 }
111 oldSend.apply(res, arguments);
112 }
113 next();
114 }
115
116 app.use(modifyResponseBody);
117````
118Note: The messages (ex. '{"error":\"Something went Wrong\"}') could be any string doesn't have to be a json-like string<br>
119 Starting the application
120--------------------------
121There are two ways to start the application.
122
123- **start it as a node using npm command as follows:** <br>
124Go to the directory of the application and write in the terminal
125````bash
126node server/server.js <config.js path>
127````
128
129- **start it as a docker image as follows:** <br>
130
131Go to the directory of the application and write in the terminal
132````bash
133docker build -t <tag_name> .
134docker run -p <chosen_ip>:3000 <tag_name>
135````
136- **start the tests as follows:** <br>
137
138unit tests are written in the [test_odata.js](https://github.com/kyma-incubator/varkes/blob/master/examples/odata-mock-app/test/test_odata.js) file and the tests are run using the [mocha](https://mochajs.org/) framework.
139Go to the directory of the application and write in the terminal
140 ````bash
141npm test
142````
143
144
\No newline at end of file