Skip to content

Commit

Permalink
chore: suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
marianfoo committed Sep 13, 2024
1 parent 833ecf7 commit 5b276f2
Show file tree
Hide file tree
Showing 17 changed files with 2,054 additions and 57 deletions.
15 changes: 5 additions & 10 deletions packages/ui5-middleware-fe-mockserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@ pnpm

## Usage

In order to use the mock server, the npm module `@sap-ux/ui5-middleware-fe-mockserver` needs to be added as devDependency and ui5.dependencies to `package.json`, and a valid `ui5.yaml` configuration needs to be provided.
In order to use the mock server, the npm module `@sap-ux/ui5-middleware-fe-mockserver` needs to be added as devDependency to `package.json`, and a valid `ui5.yaml` configuration needs to be provided.
**Entries in package.json**

```
[..]
"devDependencies": {
"@sap-ux/ui5-middleware-fe-mockserver": "^2"
},
"ui5": {
"dependencies": [
"@sap-ux/ui5-middleware-fe-mockserver"
]
}
[..]
Expand All @@ -38,7 +33,7 @@ In order to use the mock server, the npm module `@sap-ux/ui5-middleware-fe-mocks

```
specVersion: '2.0'
specVersion: '3.0'
metadata:
name: <NAME>
type: application
Expand Down Expand Up @@ -77,16 +72,16 @@ Each service must provide at least two things
On top of that you can specify one of the following option

- mockdataPath : the path to the folder containing the mockdata files
- generateMockData : whether or not you want to use automatically generated mockdata
- generateMockData : whether or not you want to use automatically generated mockdata <-- TODO: what happens if i set this to false and only define 1 of 2 entites in the service? error in the frontend?
- forceNullableValuesToNull: determine if nullable properties should be generated as null or with a default value (defaults to false)

Additional option are available either per service of for all services if defined globally

- debug : toggle the debug mode
- watch : toggle the watch mode, the mockserver will restart the service where data or metadata have changed
- noETag : disable ETag support on metadata
- strictKeyMode : disable the default "loose" mode for the key matching, you can try this if the mockserver returns too much data
- contextBasedIsolation : enable the support of "tenants", by adding /tenant-xxx at the very start of your service call you will be able to work on tenant isolated data.
- strictKeyMode : disable the default "loose" mode for the key matching, you can try this if the mockserver returns too much data <-- TODO: maybe a little bit more background info about what "loose" means here
- contextBasedIsolation : enable the support of "tenants", by adding /tenant-xxx at the very start of your service call you will be able to work on tenant isolated data. <-- TODO: is was not clear to me what `by adding /tenant-xxx at the very start of your service call` means here, it was way more clearer with the usage of the url parameter `sap-client=xxx`

You can also define static annotation file using the `annotations` entry, each annotation must provide

Expand Down
1,532 changes: 1,485 additions & 47 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions samples/basic-usage/ui5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ server:
service:
urlBasePath: /here/goes/your/serviceurl
name: ''
# TODO: old variable names, rename them to the new ones
metadataXmlPath: ./webapp/localService/metadata.xml
# TODO: is mockdataPath necessary when folder not defined?
mockdataRootPath: ./webapp/localService/mockdata
generateMockData: true
1 change: 1 addition & 0 deletions samples/error-handling/ui5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ server:
name: ''
metadataXmlPath: ./webapp/localService/metadata.xml
mockdataRootPath: ./webapp/localService/mockdata
# TODO: Could not find custom middleware fiori-tools-appreload, referenced by project basic-usage
- name: fiori-tools-appreload
afterMiddleware: compression
configuration:
Expand Down
19 changes: 19 additions & 0 deletions samples/tenants/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Error Handling Sample

In this sample we just load a simple metadata file that the mockserver will leverage alongside with static mock data defined in json files

The mock data file must be named with the same name as the EntitySet you're mocking.



- [metadata file](./webapp/localService/metadata.xml)
- [RootEntity file](./webapp/localService/mockdata/RootEntity.js)

The RootEntity file implements the executeAction method now, meaning that it will be called when you define a custom action.
It also throw an error for a specific object with key ID === 1

## Used / Implemented API function

- [getInitialDataset](../../docs/MockserverAPI.md#getInitialDataset)
- [executeAction](../../docs/MockserverAPI.md#executeAction)
- [throwError](../../docs/MockserverAPI.md#throwError)
18 changes: 18 additions & 0 deletions samples/tenants/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "error-handling",
"version": "0.0.1",
"private": true,
"description": "Error handling sample for the mockserver",
"main": "webapp/index.html",
"scripts": {
"start": "ui5 serve --open 'index.html#errorHandling-tile'",
"start:100": "ui5 serve --open 'index.html?sap-client=100#errorHandling-tile'",
"start:200": "ui5 serve --open 'index.html?sap-client=200#errorHandling-tile'"
},
"devDependencies": {
"@sap-ux/ui5-middleware-fe-mockserver": "workspace:*",
"@sap/ux-ui5-tooling": "1.15.0",
"@ui5/cli": "3.11.1",
"rimraf": "3.0.2"
}
}
14 changes: 14 additions & 0 deletions samples/tenants/ui5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
specVersion: '2.4'
metadata:
name: 'basic-usage'
type: application
server:
customMiddleware:
- name: sap-fe-mockserver
beforeMiddleware: compression
configuration:
service:
urlBasePath: /here/goes/your/serviceurl
name: ''
metadataXmlPath: ./webapp/localService/metadata.xml
mockdataRootPath: ./webapp/localService/mockdata
9 changes: 9 additions & 0 deletions samples/tenants/webapp/Component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sap.ui.define(['sap/fe/core/AppComponent'], function (AppComponent) {
'use strict';

return AppComponent.extend('sap.fe.mockserver.errorHandling.errorHandling.Component', {
metadata: {
manifest: 'json'
}
});
});
18 changes: 18 additions & 0 deletions samples/tenants/webapp/annotations/annotation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
<edmx:Reference Uri="https://sap.github.io/odata-vocabularies/vocabularies/Common.xml">
<edmx:Include Namespace="com.sap.vocabularies.Common.v1" Alias="Common"/>
</edmx:Reference>
<edmx:Reference Uri="https://sap.github.io/odata-vocabularies/vocabularies/UI.xml">
<edmx:Include Namespace="com.sap.vocabularies.UI.v1" Alias="UI"/>
</edmx:Reference>
<edmx:Reference Uri="https://sap.github.io/odata-vocabularies/vocabularies/Communication.xml">
<edmx:Include Namespace="com.sap.vocabularies.Communication.v1" Alias="Communication"/>
</edmx:Reference>
<edmx:Reference Uri="/here/goes/your/serviceurl/$metadata">
<edmx:Include Namespace="sap.fe.test.TestService"/>
</edmx:Reference>
<edmx:DataServices>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="sap.fe.mockserver.basicUsage">
</Schema>
</edmx:DataServices>
</edmx:Edmx>
85 changes: 85 additions & 0 deletions samples/tenants/webapp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<!DOCTYPE html>
<html>
<!-- Copyright (c) 2015 SAP AG, All Rights Reserved -->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{appTitle}}</title>

<!-- Bootstrap the unified shell in sandbox mode for standalone usage.
The renderer is specified in the global Unified Shell configuration object "sap-ushell-config".
The fiori2 renderer will render the shell header allowing, for instance,
testing of additional application setting buttons.
The navigation target resolution service is configured in a way that the empty URL hash is
resolved to our own application.
This example uses relative path references for the SAPUI5 resources and test-resources;
it might be necessary to adapt them depending on the target runtime platform.
The sandbox platform is restricted to development or demo use cases and must NOT be used
for productive scenarios.
-->
<script type="text/javascript">
window['sap-ushell-config'] = {
defaultRenderer: 'fiori2',
bootstrapPlugins: {
RuntimeAuthoringPlugin: {
component: 'sap.ushell.plugins.rta',
config: {
validateAppVersion: false
}
}
},
renderers: {
fiori2: {
componentData: {
config: {
search: 'hidden'
}
}
}
},
applications: {
'errorHandling-tile': {
title: 'Basic Usage',
description: '',
additionalInformation: 'SAPUI5.Component=sap.fe.mockserver.errorHandling.errorHandling',
applicationType: 'URL',
url: './'
}
}
};
</script>

<script
src="https://ui5.sap.com/test-resources/sap/ushell/bootstrap/sandbox.js"
id="sap-ushell-bootstrap"></script>
<!-- Bootstrap the UI5 core library -->
<script
id="sap-ui-bootstrap"
src="https://ui5.sap.com/resources/sap-ui-core.js"
data-sap-ui-libs="sap.m, sap.ushell, sap.fe.templates"
data-sap-ui-async="true"
data-sap-ui-preload="async"
data-sap-ui-theme="sap_fiori_3"
data-sap-ui-compatVersion="edge"
data-sap-ui-language="en"
data-sap-ui-resourceroots='{"sap.fe.mockserver.errorHandling.errorHandling": "./"}'
data-sap-ui-frameOptions="allow">
// NON-SECURE setting for testing environment
</script>
<script type="text/javascript">
sap.ui.getCore().attachInit(function () {
// initialize the ushell sandbox component
sap.ushell.Container.createRenderer().placeAt('content');
});
</script>
</head>

<!-- UI Content -->

<body class="sapUiBody" id="content"></body>
</html>
42 changes: 42 additions & 0 deletions samples/tenants/webapp/localService/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const path = require('path');
const fs = require('fs');

module.exports = {
/**
* This function returns the initial dataset for the WorkstationSet entity.
* It reads the content from the corresponding JSON file and returns it as an array of objects.
* @param {string} contextId - Represents the tenant id for the current request (optional).
* @param {string} fileName - The name of the file to load with full path.
* @param {string} currentDir - The current directory of the file.
* @returns {Array} - The initial dataset for the WorkstationSet entity.
*/
getInitialDataSet: function (contextId, fileName, currentDir) {
let filePath = '';

// Extract the base file name without extension
const baseFileName = path.basename(fileName, path.extname(fileName)).replace(/\.js$/, '');

// Construct the file name based on contextId
const fileNameSuffix = contextId === 'tenant-default' ? '' : `-${contextId.replace('tenant-', '')}`;
const jsonFileName = `${baseFileName}${fileNameSuffix}.json`;

// Construct the full file path (from current directory)
filePath = path.join(currentDir, jsonFileName);

// Check if the file exists
if (!fs.existsSync(filePath)) {
console.warn(`File not found: ${filePath}`);
// try loading default file
filePath = path.join(currentDir, `${baseFileName}.json`);
if (!fs.existsSync(filePath)) {
console.warn(`File not found: ${filePath}`);
return [];
}
}

// Read and parse the JSON file
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'));

return data;
}
};
Loading

0 comments on commit 5b276f2

Please sign in to comment.