Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: inital comments and fixes #2372

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
730e1b0
fix: inital comments and fixes
johannes-kolbe Sep 16, 2024
01c0c5b
fix: keep existing services
broksy Sep 17, 2024
e40d22a
feat: reusable method and tests
broksy Sep 18, 2024
c7abe9c
fix: overwrite existing services
broksy Sep 18, 2024
40823f3
fix: add mockserver middleware
broksy Sep 18, 2024
e84bd47
fix: naming and issue without existing services
broksy Sep 18, 2024
23871a1
fix: functionality for older tests
broksy Sep 18, 2024
d19b21d
fix: adapt for updated ui5-config api
broksy Sep 19, 2024
303d78b
fix: failing tests
broksy Sep 19, 2024
8768495
feat: replace existing service data if path exists
broksy Sep 27, 2024
9b59393
Merge branch 'main' into fix/2371/mockserverconfig
broksy Sep 27, 2024
a712b37
Merge remote-tracking branch 'origin' into fix/2371/mockserverconfig
broksy Sep 27, 2024
0b1d8a5
Merge branch 'fix/2371/mockserverconfig' of https://github.com/SAP/op…
broksy Sep 27, 2024
7d9168a
fix: test data
broksy Sep 27, 2024
fe45d45
feat: overwrite option for mock-server cli
broksy Sep 27, 2024
ee5ab5e
fix: mockserver config cli
broksy Sep 27, 2024
18dcaf7
fix: adding of backend to ui5 yaml files
broksy Oct 9, 2024
f5223e7
fix: try catch block for missing fiori-tools-proxy
broksy Oct 9, 2024
0f259f8
feat: changeset
broksy Oct 9, 2024
7dedc74
fix: syntax for proxy middleware backend yaml
broksy Oct 10, 2024
df5dff0
fix: duplicated middlewares in local yaml
broksy Oct 10, 2024
9a3a043
fix: add to backend array case
broksy Oct 10, 2024
72fb9a8
fix: method for update and enhance of mockserver config
broksy Oct 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/late-rings-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@sap-ux/mockserver-config-writer': patch
'@sap-ux/odata-service-writer': patch
'@sap-ux/ui5-config': patch
'@sap-ux/create': patch
---

Support multiple services
10 changes: 7 additions & 3 deletions packages/create/src/cli/add/mockserver-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ async function addMockserverConfig(
await validateBasePath(basePath);
const webappPath = await getWebappPath(basePath);
const config: MockserverConfig = { webappPath };
let overwriteExisting: boolean = false;
if (interactive) {
const questions = getMockserverConfigQuestions({ webappPath });
config.ui5MockYamlConfig = await prompt(questions);
const questions = getMockserverConfigQuestions({ webappPath, askForOverwrite: true });
const responses = await prompt(questions);
config.ui5MockYamlConfig = responses?.path;
// User response for whether to overwrite existing services in mock-server config
overwriteExisting = !!responses?.overwrite;
}
const fs = await generateMockserverConfig(basePath, config);
const fs = await generateMockserverConfig(basePath, config, undefined, overwriteExisting);
await traceChanges(fs);
if (!simulate) {
fs.commit(() => {
Expand Down
43 changes: 42 additions & 1 deletion packages/create/test/unit/cli/add/mockserver-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,48 @@ describe('Test command add mockserver-config', () => {
expect(logLevelSpy).not.toBeCalled();
expect(loggerMock.debug).toBeCalled();
expect(loggerMock.error).not.toBeCalled();
expect(promptSpy).toBeCalledWith([{ webappPath: join(appRoot, 'webapp') }]);
expect(promptSpy).toBeCalledWith([{ webappPath: join(appRoot, 'webapp'), askForOverwrite: true }]);
expect(fsMock.commit).toBeCalled();
expect(spawnSpy).toBeCalled();
});

test('Test create-fiori add mockserver-config <appRoot> --interactive with overwrite option', async () => {
// Mock setup
jest.spyOn(mockserverWriter, 'getMockserverConfigQuestions').mockReturnValue([
{
name: 'path',
type: 'text',
message: 'Path to mocked service'
},
{
type: 'confirm',
name: 'overwrite',
message: 'Overwrite existing services'
}
]);
const promptSpy = jest.spyOn(prompts, 'prompt');

// Test execution
const command = new Command('add');
addAddMockserverConfigCommand(command);
await command.parseAsync(getArgv(['mockserver-config', appRoot, '--interactive']));

// Result check
expect(logLevelSpy).not.toBeCalled();
expect(loggerMock.debug).toBeCalled();
expect(loggerMock.error).not.toBeCalled();
expect(promptSpy).toBeCalledWith([
{
name: 'path',
type: 'text',
message: 'Path to mocked service'
},
{
message: 'Overwrite existing services',
name: 'overwrite',
type: 'confirm'
}
]);
expect(fsMock.commit).toBeCalled();
expect(spawnSpy).toBeCalled();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ import { enhanceYaml, removeUi5MockYaml } from './ui5-mock-yaml';
* @param basePath - the base path where the package.json and ui5.yaml is
* @param data - configuration of the mockserver
* @param fs - the memfs editor instance
* @param overwrite - optional, whether to overwrite existing services in mock-server config
* @returns Promise<Editor> - memfs editor instance with updated files
*/
export async function generateMockserverConfig(basePath: string, data: MockserverConfig, fs?: Editor): Promise<Editor> {
export async function generateMockserverConfig(
basePath: string,
data: MockserverConfig,
fs?: Editor,
overwrite = false
): Promise<Editor> {
if (!fs) {
fs = create(createStorage());
}
enhancePackageJson(fs, basePath, data.packageJsonConfig);
await enhanceYaml(fs, basePath, data.webappPath, data.ui5MockYamlConfig);
await enhanceYaml(fs, basePath, data.webappPath, data.ui5MockYamlConfig, overwrite);
return fs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ import { getMainServiceDataSource, getODataSources } from '../app-info';
* @param basePath - path to project root, where package.json and ui5.yaml is
* @param webappPath - path to webapp folder, where manifest.json is
* @param config - optional config passed in by consumer
* @param overwrite - optional, whether to overwrite existing services in mock-server config
*/
export async function enhanceYaml(
fs: Editor,
basePath: string,
webappPath: string,
config?: Ui5MockYamlConfig
config?: Ui5MockYamlConfig,
overwrite = false
): Promise<void> {
const ui5MockYamlPath = join(basePath, 'ui5-mock.yaml');
let mockConfig;
Expand All @@ -44,7 +46,7 @@ export async function enhanceYaml(
}));

if (fs.exists(ui5MockYamlPath)) {
mockConfig = await updateUi5MockYamlConfig(fs, ui5MockYamlPath, mockserverPath, annotationsConfig);
mockConfig = await updateUi5MockYamlConfig(fs, ui5MockYamlPath, mockserverPath, annotationsConfig, overwrite);
} else {
mockConfig = fs.exists(join(basePath, 'ui5.yaml'))
? await generateUi5MockYamlBasedOnUi5Yaml(fs, basePath, mockserverPath, annotationsConfig)
Expand All @@ -62,16 +64,22 @@ export async function enhanceYaml(
* @param ui5MockYamlPath - path to ui5-mock.yaml file
* @param [path] - optional url path the mockserver listens to
* @param annotationsConfig - optional annotations config to add to mockserver middleware
* @param overwrite - optional, whether to overwrite existing services in mock-server config
* @returns {*} {Promise<UI5Config>} - Update Yaml Doc
*/
async function updateUi5MockYamlConfig(
fs: Editor,
ui5MockYamlPath: string,
path?: string,
annotationsConfig?: MockserverConfig['annotations']
annotationsConfig?: MockserverConfig['annotations'],
overwrite = false
): Promise<UI5Config> {
const existingUi5MockYamlConfig = await UI5Config.newInstance(fs.read(ui5MockYamlPath));
existingUi5MockYamlConfig.updateCustomMiddleware(await getNewMockserverMiddleware(path, annotationsConfig));
if (overwrite) {
existingUi5MockYamlConfig.updateCustomMiddleware(await getNewMockserverMiddleware(path, annotationsConfig));
} else {
existingUi5MockYamlConfig.enhanceMockServerMiddleware(path, annotationsConfig);
}
return existingUi5MockYamlConfig;
}

Expand Down Expand Up @@ -115,7 +123,7 @@ async function generateNewUi5MockYamlConfig(
ui5MockYaml.setType('application');
ui5MockYaml.addFioriToolsProxydMiddleware({ ui5: {} });
ui5MockYaml.addFioriToolsAppReloadMiddleware();
ui5MockYaml.addMockServerMiddleware(path, annotationsConfig);
ui5MockYaml.enhanceMockServerMiddleware(path, annotationsConfig);
return ui5MockYaml;
}

Expand All @@ -131,7 +139,7 @@ async function getNewMockserverMiddleware(
annotationsConfig?: MockserverConfig['annotations']
): Promise<CustomMiddleware<MockserverConfig>> {
const ui5MockYaml = await UI5Config.newInstance('');
ui5MockYaml.addMockServerMiddleware(path, annotationsConfig);
ui5MockYaml.enhanceMockServerMiddleware(path, annotationsConfig);
const mockserverMiddleware = ui5MockYaml.findCustomMiddleware('sap-fe-mockserver');
if (!mockserverMiddleware) {
throw Error('Could not create new mockserver config');
Expand Down
29 changes: 22 additions & 7 deletions packages/mockserver-config-writer/src/prompt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ import { t } from '..';
* @param params - optional parameters used to fill default values
* @param params.webappPath - optional path to webapp folder, where manifest is
* @param params.fs - optional memfs editor instance
* @param params.askForOverwrite - optional, whether to overwrite existing services in mock-server config
* @returns - array of questions that serves as input for prompt module
*/
export function getMockserverConfigQuestions(params?: { webappPath?: string; fs?: Editor }): PromptObject[] {
const question: Partial<PromptObject> = {
export function getMockserverConfigQuestions(params?: {
webappPath?: string;
fs?: Editor;
askForOverwrite?: boolean;
}): PromptObject[] {
const prompts: PromptObject[] = [];
const questionPath: Partial<PromptObject> = {
name: 'path',
message: t('questions.pathToMock')
};
Expand All @@ -34,11 +40,20 @@ export function getMockserverConfigQuestions(params?: { webappPath?: string; fs?
});
}
if (choices.length > 0) {
question.type = 'select';
question.choices = choices;
question.initial = choices.findIndex((c) => c.value === mainDataSourceUri);
questionPath.type = 'select';
questionPath.choices = choices;
questionPath.initial = choices.findIndex((c) => c.value === mainDataSourceUri);
}
}
question.type ||= 'text';
return [question as PromptObject];
questionPath.type ||= 'text';
prompts.push(questionPath as PromptObject);
if (params?.askForOverwrite) {
const questionOverwrite: Partial<PromptObject> = {
type: 'confirm',
name: 'overwrite',
message: t('questions.overwrite')
};
prompts.push(questionOverwrite as PromptObject);
}
return prompts;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"questions": {
"pathToMock": "Path to mocked service"
"pathToMock": "Path to mocked service",
"overwrite": "Overwrite existing services"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "fe_lrop_v4_odata_none",
"version": "0.0.1",
"private": true,
"sapux": true,
"description": "My LROP V4 Application",
"keywords": [
"ui5",
"openui5",
"sapui5"
],
"main": "webapp/index.html",
"scripts": {
"start": "fiori run --open 'test/flpSandbox.html#felropv4odatanone-tile'",
"start-local": "fiori run --config ./ui5-local.yaml --open 'test/flpSandbox.html#felropv4odatanone-tile'",
"start-noflp": "fiori run --open 'index.html'",
"build": "ui5 build -a --clean-dest --include-task=generateManifestBundle generateCachebusterInfo",
"deploy": "fiori verify",
"deploy-config": "fiori add deploy-config",
"int-tests": "fiori run --config ./ui5-mock.yaml --open 'test/integration/opaTests.qunit.html'",
"start-mock": "fiori run --config ./ui5-mock.yaml --open 'test/flpSandbox.html#felropv4odatanone-tile'"
},
"devDependencies": {
"@ui5/cli": "^2.11.1",
"@ui5/fs": "^2.0.6",
"@ui5/logger": "^2.0.1",
"@sap/ux-ui5-tooling": "1",
"rimraf": "5.0.5",
"@sap/ux-specification": "UI5-1.91",
"@sap/ux-ui5-fe-mockserver-middleware": "1"
},
"ui5": {
"dependencies": [
"@sap/ux-ui5-tooling",
"@sap/ux-ui5-fe-mockserver-middleware"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
metadata:
name: 'app'
type: application
server:
customMiddleware:
- name: middleware-before
- name: sap-fe-mockserver
beforeMiddleware: fiori-tools-proxy
configuration:
services:
- urlPath: /some/previous/service/uri
metadataXmlPath: ./webapp/localService/metadata.xml
mockdataRootPath: ./webapp/localService/data
generateMockData: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
specVersion: '2.4'
metadata:
name: 'fe_lrop_v4_odata_none'
type: application
server:
customMiddleware:
- name: fiori-tools-proxy
afterMiddleware: compression
configuration:
ignoreCertError: false # If set to true, certificate errors will be ignored. E.g. self-signed certificates will be accepted
backend:
- path: /sap
url: https://sap-ux-mock-services-v4-lrop.cfapps.us10.hana.ondemand.com
ui5:
path:
- /resources
- /test-resources
url: https://ui5.sap.com
version: 1.91.0 # The UI5 version, for instance, 1.78.1. Empty means latest version
- name: fiori-tools-appreload
afterMiddleware: compression
configuration:
port: 35729
path: webapp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!DOCTYPE html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"_version": "1.48.0",
"sap.app": {
"id": "no-ui5-mock-config",
"title": "App without mockserver configuration",
"type": "application",
"dataSources": {
"mainService": {
"uri": "/path/to/odata/service/",
"type": "OData",
"settings": {
"odataVersion": "4.0"
}
}
},
"applicationVersion": {
"version": "1.0.0"
}
},
"sap.ui": {
"technology": "UI5",
"deviceTypes": {
"desktop": true,
"tablet": true,
"phone": true
}
},
"sap.ui5": {
"dependencies": {
"minUI5Version": "1.91.0"
},
"models": {
"": {
"dataSource": "mainService"
}
},
"contentDensities": {
"compact": true,
"cozy": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,27 @@ server:
annotations: []
"
`;

exports[`Test generateMockserverConfig() Add config to project with existing ui5-mock.yaml 1`] = `
"metadata:
name: 'app'
type: application
server:
customMiddleware:
- name: middleware-before
- name: sap-fe-mockserver
beforeMiddleware: csp
configuration:
services:
- urlPath: /some/previous/service/uri
metadataXmlPath: ./webapp/localService/metadata.xml
mockdataRootPath: ./webapp/localService/data
generateMockData: true
- urlPath: /path/to/odata/service
metadataPath: ./webapp/localService/metadata.xml
mockdataPath: ./webapp/localService/data
generateMockData: true
mountPath: /
annotations: []
"
`;
Loading
Loading