Skip to Content
Technical Articles
Jacek Woz万博新体育手机客户端niczak作者的资料照片

[VS Code] Enhanced generation of mock files for OData services

When testing UI5 applications using UI5MockServeryou can generate mock data for you OData service, but they are often not very useful if you have rich frontend with some logic based on specific values. You can also prepare and edit the files manually and keep updating them when you change something in your service – which is super annoying if you are starting from scratch with the application without the data model and backend at the beginning. Please also remember that The Principle Of Unnecessary Manual Work says:

“It’s a tedious task to do work, which can be done by computers”.

OK, there is no such principle in the real world stated anywhere, but it was exactly the case when some time ago I put more advanced mock data files generation in my Brackets-UI5 extension and now – after tuning and polishing – I ported it toVS Code.

I will explain it on a case.

UsingFiori extensionsI generated a sample list-report app for the service SEPMRA_SHOP available in the trialABAP 1909 Dockersystem. I added few annotations to expose fields, then run it with default mock data (npm run start-mock):

The list page:

The object page:

Not nice. In the list page I get rather dumb data, in the object page I get nothing.

With start-mock option, no data files are generated. But I can create them with my extension, using the command:

Ten I get them inmockdatafolder

Now app is refreshed, the list page looks like before, but finally I get something in the object page:

Still not nice for me. I could manually edit files, but remember about the principle.

Let’s say that for product names and description I would like to have something which more closely mimic the real word products; description should be also longer. For currencies I need only USD and PLN, for dimensions – cm and mm, also I would like to use more standardized metrics set, not such fully random ones. For each product I would like to have more reviews, also with longer texts for rating texts. For testing I’m ok with 10 products, but 100 reviews randomly assigned to each product. Finally for suppliers would be also better to have more real-world style address data.

After creating file.rules.jsonwith my requirements, I can again run the command and regenerate data files. The app will be automatically reloaded thanks to the excellentUI5 toolingwhich runs under the hood of Fiori extensions.

This time the list page is nicer:

The object page (no, I did not set the kitty image manually….):

and the Supplier section:

Much better.

  • ✔️ I usedfaker.jsto generate values
  • ✔️ I used my own value sets
  • ✔️ I was able to relate the data
  • ✔️ I can experiment and just hit the command to regenerate files
  • ✔️ I did not have to edit metadata (in fact it can be taken via http, not only from a local file)

Needed configuration comes from a special.rules.jsonfile, for the case above it looks like:

{ "variables": { "currencies": ["USD", "PLN"], "uom": ["cm", "mm"], "measures": [1, 5, 10, 15, 20, 25, 30, 50, 100], "productIDs": ["Id 1", "Id 2", "Id 3", "Id 4", "Id 5", "Id 6", "Id 7", "Id 8", "Id 9", "Id 10"] }, "distinctValues": ["Products"], "predefined": { "Product": { "Id": "$ref:productIDs", "CurrencyCode": "$ref:currencies", "DimensionUnit": "$ref:uom", "DimensionHeight": "$ref:measures", "DimensionWidth": "$ref:measures" }, "Review": { "ProductId": "$ref:productIDs" } }, "faker": { "Product": { "Name": "commerce.productName", "Description": "commerce.productDescription", "ImageUrl": "image.image" }, "Review": { "UserDisplayName": "internet.userName", "Comment": "lorem.paragraph" }, "Supplier": { "Name": "company.companyName", "Email": "internet.email", "ContactPhone1": "phone.phoneNumber", "FormattedAddress": "{{address.streetAddress}}, {{address.zipCode}} {{address.city}}, {{address.country}}", "WebAddress": "internet.url" } }, "lengthOf": { "Reviews": 100 } }

How to get such data is described on the extension’s page:

https://marketplace.visualstudio.com/items?itemName=jacek-wozniczak.vscode-ui5-odata-mock-generator

PS. You can also check my other extension for UI5 API reference in the side bar:

https://marketplace.visualstudio.com/items?itemName=jacek-wozniczak.vscode-ui5-api-reference

Blog post://www.bouseh.com/2021/06/08/vs-code-ui5-api-reference-in-the-side-bar/

Assigned Tags

      7 Comments
      You must be登录comment or reply to a post.
      Author's profile photo Margot Wollny
      Margot Wollny

      Now also available for VS Code - that's so cool! Thanks!

      Jacek Woz万博新体育手机客户端niczak作者的资料照片
      Jacek Wozniczak
      Blog Post Author

      Thank you!

      Author's profile photo Ronnie André Bjørvik Sletta
      Ronnie André Bjørvik Sletta

      Great workJacek Wozniczak! Thanks for sharing, I'll make sure to check it out, and see how I can include it in my workflow.

      Jacek Woz万博新体育手机客户端niczak作者的资料照片
      Jacek Wozniczak
      Blog Post Author

      Thank you!

      Author's profile photo Marco Martini
      Marco Martini

      Great job! Would be nice to have something like this inside business application studio.

      Author's profile photo Zaki Zaman
      Zaki Zaman

      Hi Jacek,

      Is there a way by which we can specify unique values to be generated in the mock data? For example you have specified this in the rules.json file

      "productIDs": ["Id 1", "Id 2", "Id 3", "Id 4", "Id 5", "Id 6", "Id 7", "Id 8", "Id 9", "Id 10"]

      But when the files are generated, the productID which is a key field have duplicate entries. I have specified LenghtOf the entity to be 10 exactly matching the variable count. So there are 10 entries created but then many of them are repeating.

      Jacek Woz万博新体育手机客户端niczak作者的资料照片
      Jacek Wozniczak
      Blog Post Author

      Hi Zaki,

      I have corrected the rules.json in the example - added predefined ids to Product entity and set is as distinct

      "predefined": { "Product": { "Id": "$ref:productIDs",
      "distinctValues": ["Products"],

      Then you should get non-repeating entries.

      BR,

      Jacek