From 730e1b045968bf897d5ca5a70f59980d27dc9602 Mon Sep 17 00:00:00 2001 From: Johannes Kolbe Date: Mon, 16 Sep 2024 12:25:07 +0200 Subject: [PATCH 01/20] fix: inital comments and fixes --- packages/ui5-config/src/middlewares.ts | 7 +++++-- packages/ui5-config/src/ui5config.ts | 11 +++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/ui5-config/src/middlewares.ts b/packages/ui5-config/src/middlewares.ts index 9fd41b70ef..a173e5ca24 100644 --- a/packages/ui5-config/src/middlewares.ts +++ b/packages/ui5-config/src/middlewares.ts @@ -116,11 +116,14 @@ export const getMockServerMiddlewareConfig = ( beforeMiddleware: 'csp', configuration: { mountPath: '/', + // Services should be empty in case no service is provided services: [] services: [ { urlPath: path ?? '', - metadataPath: './webapp/localService/metadata.xml', - mockdataPath: './webapp/localService/data', + metadataPath: 'metadataPath', + // mockdata path should not be generated in case no mock data exists + mockdataPath: 'mockdataPath', + // In case of update, this user value should not be overwritten generateMockData: true } ], diff --git a/packages/ui5-config/src/ui5config.ts b/packages/ui5-config/src/ui5config.ts index 38c351a9df..1535c14335 100644 --- a/packages/ui5-config/src/ui5config.ts +++ b/packages/ui5-config/src/ui5config.ts @@ -322,9 +322,12 @@ export class UI5Config { * @memberof UI5Config */ public addMockServerMiddleware(path?: string, annotationsConfig?: MockserverConfig['annotations']): this { - this.document.appendTo({ + const middleware = getMockServerMiddlewareConfig(path, annotationsConfig); + this.document.updateAt({ path: 'server.customMiddleware', - value: getMockServerMiddlewareConfig(path, annotationsConfig) + matcher: { key: 'name', value: middleware.name }, + value: middleware, + mode: 'merge' }); return this; } @@ -488,7 +491,7 @@ export class UI5Config { path: 'server.customMiddleware', matcher: { key: 'name', value: name }, value: middleware, - mode: 'overwrite' + mode: 'merge' }); } else { this.addCustomMiddleware([middleware]); @@ -503,7 +506,7 @@ export class UI5Config { * @returns {UI5Config} the UI5Config instance * @memberof UI5Config */ - private mergeCustomMiddleware(middleware: CustomMiddleware): this { + public mergeCustomMiddleware(middleware: CustomMiddleware): this { const name = middleware.name; if (this.findCustomMiddleware(name)) { this.document.updateAt({ From 01c0c5b8cb6bc85bf9906156d477467b999f1f8d Mon Sep 17 00:00:00 2001 From: broksy Date: Tue, 17 Sep 2024 18:10:08 +0300 Subject: [PATCH 02/20] fix: keep existing services --- packages/ui5-config/src/middlewares.ts | 2 ++ packages/ui5-config/src/ui5config.ts | 30 ++++++++++++++++++- .../test/__snapshots__/index.test.ts.snap | 21 +++++++++++++ packages/ui5-config/test/index.test.ts | 24 +++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/packages/ui5-config/src/middlewares.ts b/packages/ui5-config/src/middlewares.ts index a173e5ca24..d82deca4bf 100644 --- a/packages/ui5-config/src/middlewares.ts +++ b/packages/ui5-config/src/middlewares.ts @@ -107,6 +107,7 @@ export function getFioriToolsProxyMiddlewareConfig( } export const getMockServerMiddlewareConfig = ( + services: MockserverConfig['services'] = [], path?: string, annotationsConfig: MockserverConfig['annotations'] = [] ): CustomMiddleware => { @@ -118,6 +119,7 @@ export const getMockServerMiddlewareConfig = ( mountPath: '/', // Services should be empty in case no service is provided services: [] services: [ + ...services, { urlPath: path ?? '', metadataPath: 'metadataPath', diff --git a/packages/ui5-config/src/ui5config.ts b/packages/ui5-config/src/ui5config.ts index 1535c14335..e02710c1b2 100644 --- a/packages/ui5-config/src/ui5config.ts +++ b/packages/ui5-config/src/ui5config.ts @@ -322,7 +322,35 @@ export class UI5Config { * @memberof UI5Config */ public addMockServerMiddleware(path?: string, annotationsConfig?: MockserverConfig['annotations']): this { - const middleware = getMockServerMiddlewareConfig(path, annotationsConfig); + const middleware = getMockServerMiddlewareConfig(undefined, path, annotationsConfig); + this.document.updateAt({ + path: 'server.customMiddleware', + matcher: { key: 'name', value: middleware.name }, + value: middleware, + mode: 'merge' + }); + return this; + } + + /** + * Updates mockserver instance services. + * + * @param path option path that is to be mocked + * @param annotationsConfig optional annotations config that is to be mocked + * @returns {UI5Config} the UI5Config instance + * @memberof UI5Config + */ + public updateMockServerMiddlewareServices( + path?: string, + annotationsConfig?: MockserverConfig['annotations'] + ): this { + const customMockserverMiddleware = this.findCustomMiddleware('sap-fe-mockserver'); + const customMockserverMiddlewareConfig = customMockserverMiddleware?.configuration as MockserverConfig; + const middleware = getMockServerMiddlewareConfig( + customMockserverMiddlewareConfig.services, + path, + annotationsConfig + ); this.document.updateAt({ path: 'server.customMiddleware', matcher: { key: 'name', value: middleware.name }, diff --git a/packages/ui5-config/test/__snapshots__/index.test.ts.snap b/packages/ui5-config/test/__snapshots__/index.test.ts.snap index 11f2a24874..ea043bb218 100644 --- a/packages/ui5-config/test/__snapshots__/index.test.ts.snap +++ b/packages/ui5-config/test/__snapshots__/index.test.ts.snap @@ -551,3 +551,24 @@ exports[`UI5Config setMetadata set name and copyright 1`] = ` copyright: © " `; + +exports[`UI5Config updateMockServerMiddlewareServices add with given path and annotationsConfig 1`] = ` +"server: + customMiddleware: + - name: sap-fe-mockserver + beforeMiddleware: csp + configuration: + mountPath: / + services: + - urlPath: /~different-testpath~ + metadataPath: different-metadataPath + generateMockData: true + - urlPath: /~testpath~ + metadataPath: metadataPath + mockdataPath: mockdataPath + generateMockData: true + annotations: + - localPath: ./webapp/annotations/annotations.xml + urlPath: annotations.xml +" +`; diff --git a/packages/ui5-config/test/index.test.ts b/packages/ui5-config/test/index.test.ts index c6d38d0f5c..ca4549af64 100644 --- a/packages/ui5-config/test/index.test.ts +++ b/packages/ui5-config/test/index.test.ts @@ -1,6 +1,7 @@ import { AuthenticationType } from '@sap-ux/store'; import type { BspApp, UI5ProxyConfig } from '../src'; import { UI5Config } from '../src'; +import type { MockserverConfig } from '../src/types'; describe('UI5Config', () => { // values for testing @@ -263,6 +264,29 @@ describe('UI5Config', () => { }); }); + describe('updateMockServerMiddlewareServices', () => { + test('add with given path and annotationsConfig', () => { + const mockserverMiddlewareConfig: MockserverConfig = { + mountPath: '/', + services: [ + { + urlPath: '/~different-testpath~', + metadataPath: 'different-metadataPath', + generateMockData: true + } + ] + }; + const customMockserverMiddleware = { + name: 'sap-fe-mockserver', + beforeMiddleware: 'csp', + configuration: mockserverMiddlewareConfig + }; + ui5Config.addCustomMiddleware([customMockserverMiddleware]); + ui5Config.updateMockServerMiddlewareServices(path, annotationsConfig); + expect(ui5Config.toString()).toMatchSnapshot(); + }); + }); + test('getAppReloadMiddlewareConfig', () => { ui5Config.addFioriToolsAppReloadMiddleware(); expect(ui5Config.toString()).toMatchSnapshot(); From e40d22a092bb509a297d6e3eeb6b5021f1186880 Mon Sep 17 00:00:00 2001 From: broksy Date: Wed, 18 Sep 2024 10:44:56 +0300 Subject: [PATCH 03/20] feat: reusable method and tests --- packages/ui5-config/src/ui5config.ts | 9 +--- .../test/__snapshots__/index.test.ts.snap | 38 +++++++++++++++ packages/ui5-config/test/index.test.ts | 46 ++++++++++++------- 3 files changed, 70 insertions(+), 23 deletions(-) diff --git a/packages/ui5-config/src/ui5config.ts b/packages/ui5-config/src/ui5config.ts index e02710c1b2..7b18c5b59b 100644 --- a/packages/ui5-config/src/ui5config.ts +++ b/packages/ui5-config/src/ui5config.ts @@ -351,12 +351,7 @@ export class UI5Config { path, annotationsConfig ); - this.document.updateAt({ - path: 'server.customMiddleware', - matcher: { key: 'name', value: middleware.name }, - value: middleware, - mode: 'merge' - }); + this.mergeCustomMiddleware(middleware); return this; } @@ -534,7 +529,7 @@ export class UI5Config { * @returns {UI5Config} the UI5Config instance * @memberof UI5Config */ - public mergeCustomMiddleware(middleware: CustomMiddleware): this { + private mergeCustomMiddleware(middleware: CustomMiddleware): this { const name = middleware.name; if (this.findCustomMiddleware(name)) { this.document.updateAt({ diff --git a/packages/ui5-config/test/__snapshots__/index.test.ts.snap b/packages/ui5-config/test/__snapshots__/index.test.ts.snap index ea043bb218..cb84bf48df 100644 --- a/packages/ui5-config/test/__snapshots__/index.test.ts.snap +++ b/packages/ui5-config/test/__snapshots__/index.test.ts.snap @@ -552,6 +552,25 @@ exports[`UI5Config setMetadata set name and copyright 1`] = ` " `; +exports[`UI5Config updateMockServerMiddlewareServices add with given path 1`] = ` +"server: + customMiddleware: + - name: sap-fe-mockserver + beforeMiddleware: csp + configuration: + mountPath: / + services: + - urlPath: /~different-testpath~ + metadataPath: different-metadataPath + generateMockData: true + - urlPath: /~testpath~ + metadataPath: metadataPath + mockdataPath: mockdataPath + generateMockData: true + annotations: [] +" +`; + exports[`UI5Config updateMockServerMiddlewareServices add with given path and annotationsConfig 1`] = ` "server: customMiddleware: @@ -572,3 +591,22 @@ exports[`UI5Config updateMockServerMiddlewareServices add with given path and an urlPath: annotations.xml " `; + +exports[`UI5Config updateMockServerMiddlewareServices add without path 1`] = ` +"server: + customMiddleware: + - name: sap-fe-mockserver + beforeMiddleware: csp + configuration: + mountPath: / + services: + - urlPath: /~different-testpath~ + metadataPath: different-metadataPath + generateMockData: true + - urlPath: '' + metadataPath: metadataPath + mockdataPath: mockdataPath + generateMockData: true + annotations: [] +" +`; diff --git a/packages/ui5-config/test/index.test.ts b/packages/ui5-config/test/index.test.ts index ca4549af64..a5f60a489d 100644 --- a/packages/ui5-config/test/index.test.ts +++ b/packages/ui5-config/test/index.test.ts @@ -265,23 +265,37 @@ describe('UI5Config', () => { }); describe('updateMockServerMiddlewareServices', () => { - test('add with given path and annotationsConfig', () => { - const mockserverMiddlewareConfig: MockserverConfig = { - mountPath: '/', - services: [ - { - urlPath: '/~different-testpath~', - metadataPath: 'different-metadataPath', - generateMockData: true - } - ] - }; - const customMockserverMiddleware = { - name: 'sap-fe-mockserver', - beforeMiddleware: 'csp', - configuration: mockserverMiddlewareConfig - }; + const mockserverMiddlewareConfig: MockserverConfig = { + mountPath: '/', + services: [ + { + urlPath: '/~different-testpath~', + metadataPath: 'different-metadataPath', + generateMockData: true + } + ] + }; + const customMockserverMiddleware = { + name: 'sap-fe-mockserver', + beforeMiddleware: 'csp', + configuration: mockserverMiddlewareConfig + }; + + beforeEach(() => { ui5Config.addCustomMiddleware([customMockserverMiddleware]); + }); + + test('add with given path', () => { + ui5Config.updateMockServerMiddlewareServices(path); + expect(ui5Config.toString()).toMatchSnapshot(); + }); + + test('add without path', () => { + ui5Config.updateMockServerMiddlewareServices(); + expect(ui5Config.toString()).toMatchSnapshot(); + }); + + test('add with given path and annotationsConfig', () => { ui5Config.updateMockServerMiddlewareServices(path, annotationsConfig); expect(ui5Config.toString()).toMatchSnapshot(); }); From c7abe9c0bd082cd24f946d3c18ad38327e048b07 Mon Sep 17 00:00:00 2001 From: broksy Date: Wed, 18 Sep 2024 11:00:40 +0300 Subject: [PATCH 04/20] fix: overwrite existing services --- packages/ui5-config/src/ui5config.ts | 47 +++++++++++++++---- .../test/__snapshots__/index.test.ts.snap | 16 +++++++ packages/ui5-config/test/index.test.ts | 6 +++ 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/packages/ui5-config/src/ui5config.ts b/packages/ui5-config/src/ui5config.ts index 7b18c5b59b..c411d44c11 100644 --- a/packages/ui5-config/src/ui5config.ts +++ b/packages/ui5-config/src/ui5config.ts @@ -336,22 +336,29 @@ export class UI5Config { * Updates mockserver instance services. * * @param path option path that is to be mocked - * @param annotationsConfig optional annotations config that is to be mocked + * @param annotationsConfig optional, annotations config that is to be mocked + * @param overwrite optional, whether services should be overwritten * @returns {UI5Config} the UI5Config instance * @memberof UI5Config */ public updateMockServerMiddlewareServices( path?: string, - annotationsConfig?: MockserverConfig['annotations'] + annotationsConfig?: MockserverConfig['annotations'], + overwrite = false ): this { - const customMockserverMiddleware = this.findCustomMiddleware('sap-fe-mockserver'); - const customMockserverMiddlewareConfig = customMockserverMiddleware?.configuration as MockserverConfig; - const middleware = getMockServerMiddlewareConfig( - customMockserverMiddlewareConfig.services, - path, - annotationsConfig - ); - this.mergeCustomMiddleware(middleware); + if (overwrite) { + const middleware = getMockServerMiddlewareConfig(undefined, path, annotationsConfig); + this.overwriteCustomMiddleware(middleware); + } else { + const customMockserverMiddleware = this.findCustomMiddleware('sap-fe-mockserver'); + const customMockserverMiddlewareConfig = customMockserverMiddleware?.configuration as MockserverConfig; + const middleware = getMockServerMiddlewareConfig( + customMockserverMiddlewareConfig.services, + path, + annotationsConfig + ); + this.mergeCustomMiddleware(middleware); + } return this; } @@ -542,6 +549,26 @@ export class UI5Config { return this; } + /** + * Overwrites existing custom middleware with the passed config. + * + * @param middleware - middleware config + * @returns {UI5Config} the UI5Config instance + * @memberof UI5Config + */ + private overwriteCustomMiddleware(middleware: CustomMiddleware): this { + const name = middleware.name; + if (this.findCustomMiddleware(name)) { + this.document.updateAt({ + path: 'server.customMiddleware', + matcher: { key: 'name', value: name }, + value: middleware, + mode: 'overwrite' + }); + } + return this; + } + /** * Returns the serve static config. * diff --git a/packages/ui5-config/test/__snapshots__/index.test.ts.snap b/packages/ui5-config/test/__snapshots__/index.test.ts.snap index cb84bf48df..c1a5009556 100644 --- a/packages/ui5-config/test/__snapshots__/index.test.ts.snap +++ b/packages/ui5-config/test/__snapshots__/index.test.ts.snap @@ -592,6 +592,22 @@ exports[`UI5Config updateMockServerMiddlewareServices add with given path and an " `; +exports[`UI5Config updateMockServerMiddlewareServices add with overwrite 1`] = ` +"server: + customMiddleware: + - name: sap-fe-mockserver + beforeMiddleware: csp + configuration: + mountPath: / + services: + - urlPath: /~testpath~ + metadataPath: metadataPath + mockdataPath: mockdataPath + generateMockData: true + annotations: [] +" +`; + exports[`UI5Config updateMockServerMiddlewareServices add without path 1`] = ` "server: customMiddleware: diff --git a/packages/ui5-config/test/index.test.ts b/packages/ui5-config/test/index.test.ts index a5f60a489d..c6b9d057d3 100644 --- a/packages/ui5-config/test/index.test.ts +++ b/packages/ui5-config/test/index.test.ts @@ -285,6 +285,12 @@ describe('UI5Config', () => { ui5Config.addCustomMiddleware([customMockserverMiddleware]); }); + test('add with overwrite', () => { + // should overwrite existing services + ui5Config.updateMockServerMiddlewareServices(path, undefined, true); + expect(ui5Config.toString()).toMatchSnapshot(); + }); + test('add with given path', () => { ui5Config.updateMockServerMiddlewareServices(path); expect(ui5Config.toString()).toMatchSnapshot(); From 40823f3b96d3080d3ae8b183c73d5a26043214b7 Mon Sep 17 00:00:00 2001 From: broksy Date: Wed, 18 Sep 2024 12:35:56 +0300 Subject: [PATCH 05/20] fix: add mockserver middleware --- packages/ui5-config/src/ui5config.ts | 6 ++---- .../ui5-config/test/__snapshots__/index.test.ts.snap | 12 ++++++------ 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/ui5-config/src/ui5config.ts b/packages/ui5-config/src/ui5config.ts index c411d44c11..801772c6f5 100644 --- a/packages/ui5-config/src/ui5config.ts +++ b/packages/ui5-config/src/ui5config.ts @@ -323,11 +323,9 @@ export class UI5Config { */ public addMockServerMiddleware(path?: string, annotationsConfig?: MockserverConfig['annotations']): this { const middleware = getMockServerMiddlewareConfig(undefined, path, annotationsConfig); - this.document.updateAt({ + this.document.appendTo({ path: 'server.customMiddleware', - matcher: { key: 'name', value: middleware.name }, - value: middleware, - mode: 'merge' + value: middleware }); return this; } diff --git a/packages/ui5-config/test/__snapshots__/index.test.ts.snap b/packages/ui5-config/test/__snapshots__/index.test.ts.snap index c1a5009556..6b9b8ccb37 100644 --- a/packages/ui5-config/test/__snapshots__/index.test.ts.snap +++ b/packages/ui5-config/test/__snapshots__/index.test.ts.snap @@ -319,8 +319,8 @@ exports[`UI5Config addMockServerMiddleware add with given path 1`] = ` mountPath: / services: - urlPath: /~testpath~ - metadataPath: ./webapp/localService/metadata.xml - mockdataPath: ./webapp/localService/data + metadataPath: metadataPath + mockdataPath: mockdataPath generateMockData: true annotations: [] " @@ -335,8 +335,8 @@ exports[`UI5Config addMockServerMiddleware add with path and annotationsConfig 1 mountPath: / services: - urlPath: /~testpath~ - metadataPath: ./webapp/localService/metadata.xml - mockdataPath: ./webapp/localService/data + metadataPath: metadataPath + mockdataPath: mockdataPath generateMockData: true annotations: - localPath: ./webapp/annotations/annotations.xml @@ -353,8 +353,8 @@ exports[`UI5Config addMockServerMiddleware add without path 1`] = ` mountPath: / services: - urlPath: '' - metadataPath: ./webapp/localService/metadata.xml - mockdataPath: ./webapp/localService/data + metadataPath: metadataPath + mockdataPath: mockdataPath generateMockData: true annotations: [] " From e84bd47cc39da602a0f9b33cdde235d9f2a9392b Mon Sep 17 00:00:00 2001 From: broksy Date: Wed, 18 Sep 2024 15:50:19 +0300 Subject: [PATCH 06/20] fix: naming and issue without existing services --- packages/ui5-config/src/middlewares.ts | 25 ++++++---- packages/ui5-config/src/ui5config.ts | 50 ++++--------------- .../test/__snapshots__/index.test.ts.snap | 42 ++++------------ packages/ui5-config/test/index.test.ts | 23 +++++---- 4 files changed, 47 insertions(+), 93 deletions(-) diff --git a/packages/ui5-config/src/middlewares.ts b/packages/ui5-config/src/middlewares.ts index d82deca4bf..df08fcbc71 100644 --- a/packages/ui5-config/src/middlewares.ts +++ b/packages/ui5-config/src/middlewares.ts @@ -118,17 +118,20 @@ export const getMockServerMiddlewareConfig = ( configuration: { mountPath: '/', // Services should be empty in case no service is provided services: [] - services: [ - ...services, - { - urlPath: path ?? '', - metadataPath: 'metadataPath', - // mockdata path should not be generated in case no mock data exists - mockdataPath: 'mockdataPath', - // In case of update, this user value should not be overwritten - generateMockData: true - } - ], + services: + services.length > 0 + ? [ + ...services, + { + urlPath: path ?? '', + metadataPath: 'metadataPath', + // mockdata path should not be generated in case no mock data exists + mockdataPath: 'mockdataPath', + // In case of update, this user value should not be overwritten + generateMockData: true + } + ] + : undefined, annotations: annotationsConfig } }; diff --git a/packages/ui5-config/src/ui5config.ts b/packages/ui5-config/src/ui5config.ts index 801772c6f5..fdee2d6e0c 100644 --- a/packages/ui5-config/src/ui5config.ts +++ b/packages/ui5-config/src/ui5config.ts @@ -331,32 +331,22 @@ export class UI5Config { } /** - * Updates mockserver instance services. + * Updates a instance of the mockserver middleware of the config. * * @param path option path that is to be mocked * @param annotationsConfig optional, annotations config that is to be mocked - * @param overwrite optional, whether services should be overwritten * @returns {UI5Config} the UI5Config instance * @memberof UI5Config */ - public updateMockServerMiddlewareServices( - path?: string, - annotationsConfig?: MockserverConfig['annotations'], - overwrite = false - ): this { - if (overwrite) { - const middleware = getMockServerMiddlewareConfig(undefined, path, annotationsConfig); - this.overwriteCustomMiddleware(middleware); - } else { - const customMockserverMiddleware = this.findCustomMiddleware('sap-fe-mockserver'); - const customMockserverMiddlewareConfig = customMockserverMiddleware?.configuration as MockserverConfig; - const middleware = getMockServerMiddlewareConfig( - customMockserverMiddlewareConfig.services, - path, - annotationsConfig - ); - this.mergeCustomMiddleware(middleware); - } + public updateMockServerMiddleware(path?: string, annotationsConfig?: MockserverConfig['annotations']): this { + const customMockserverMiddleware = this.findCustomMiddleware('sap-fe-mockserver'); + const customMockserverMiddlewareConfig = customMockserverMiddleware?.configuration as MockserverConfig; + const middleware = getMockServerMiddlewareConfig( + customMockserverMiddlewareConfig?.services, + path, + annotationsConfig + ); + this.updateCustomMiddleware(middleware); return this; } @@ -547,26 +537,6 @@ export class UI5Config { return this; } - /** - * Overwrites existing custom middleware with the passed config. - * - * @param middleware - middleware config - * @returns {UI5Config} the UI5Config instance - * @memberof UI5Config - */ - private overwriteCustomMiddleware(middleware: CustomMiddleware): this { - const name = middleware.name; - if (this.findCustomMiddleware(name)) { - this.document.updateAt({ - path: 'server.customMiddleware', - matcher: { key: 'name', value: name }, - value: middleware, - mode: 'overwrite' - }); - } - return this; - } - /** * Returns the serve static config. * diff --git a/packages/ui5-config/test/__snapshots__/index.test.ts.snap b/packages/ui5-config/test/__snapshots__/index.test.ts.snap index 6b9b8ccb37..c7386413b4 100644 --- a/packages/ui5-config/test/__snapshots__/index.test.ts.snap +++ b/packages/ui5-config/test/__snapshots__/index.test.ts.snap @@ -317,11 +317,6 @@ exports[`UI5Config addMockServerMiddleware add with given path 1`] = ` beforeMiddleware: csp configuration: mountPath: / - services: - - urlPath: /~testpath~ - metadataPath: metadataPath - mockdataPath: mockdataPath - generateMockData: true annotations: [] " `; @@ -333,11 +328,6 @@ exports[`UI5Config addMockServerMiddleware add with path and annotationsConfig 1 beforeMiddleware: csp configuration: mountPath: / - services: - - urlPath: /~testpath~ - metadataPath: metadataPath - mockdataPath: mockdataPath - generateMockData: true annotations: - localPath: ./webapp/annotations/annotations.xml urlPath: annotations.xml @@ -351,11 +341,6 @@ exports[`UI5Config addMockServerMiddleware add without path 1`] = ` beforeMiddleware: csp configuration: mountPath: / - services: - - urlPath: '' - metadataPath: metadataPath - mockdataPath: mockdataPath - generateMockData: true annotations: [] " `; @@ -552,7 +537,7 @@ exports[`UI5Config setMetadata set name and copyright 1`] = ` " `; -exports[`UI5Config updateMockServerMiddlewareServices add with given path 1`] = ` +exports[`UI5Config updateMockServerMiddlewareServices update with given path (existing services) 1`] = ` "server: customMiddleware: - name: sap-fe-mockserver @@ -571,28 +556,18 @@ exports[`UI5Config updateMockServerMiddlewareServices add with given path 1`] = " `; -exports[`UI5Config updateMockServerMiddlewareServices add with given path and annotationsConfig 1`] = ` +exports[`UI5Config updateMockServerMiddlewareServices update with given path (no existing services) 1`] = ` "server: customMiddleware: - name: sap-fe-mockserver beforeMiddleware: csp configuration: mountPath: / - services: - - urlPath: /~different-testpath~ - metadataPath: different-metadataPath - generateMockData: true - - urlPath: /~testpath~ - metadataPath: metadataPath - mockdataPath: mockdataPath - generateMockData: true - annotations: - - localPath: ./webapp/annotations/annotations.xml - urlPath: annotations.xml + annotations: [] " `; -exports[`UI5Config updateMockServerMiddlewareServices add with overwrite 1`] = ` +exports[`UI5Config updateMockServerMiddlewareServices update with given path and annotationsConfig (existing services) 1`] = ` "server: customMiddleware: - name: sap-fe-mockserver @@ -600,15 +575,20 @@ exports[`UI5Config updateMockServerMiddlewareServices add with overwrite 1`] = ` configuration: mountPath: / services: + - urlPath: /~different-testpath~ + metadataPath: different-metadataPath + generateMockData: true - urlPath: /~testpath~ metadataPath: metadataPath mockdataPath: mockdataPath generateMockData: true - annotations: [] + annotations: + - localPath: ./webapp/annotations/annotations.xml + urlPath: annotations.xml " `; -exports[`UI5Config updateMockServerMiddlewareServices add without path 1`] = ` +exports[`UI5Config updateMockServerMiddlewareServices update without path (existing services) 1`] = ` "server: customMiddleware: - name: sap-fe-mockserver diff --git a/packages/ui5-config/test/index.test.ts b/packages/ui5-config/test/index.test.ts index c6b9d057d3..9a467533b7 100644 --- a/packages/ui5-config/test/index.test.ts +++ b/packages/ui5-config/test/index.test.ts @@ -264,7 +264,8 @@ describe('UI5Config', () => { }); }); - describe('updateMockServerMiddlewareServices', () => { + describe('updateMockServerMiddleware', () => { + const MOCKSERVER_MIDDLEWARE_NAME = 'sap-fe-mockserver'; const mockserverMiddlewareConfig: MockserverConfig = { mountPath: '/', services: [ @@ -276,7 +277,7 @@ describe('UI5Config', () => { ] }; const customMockserverMiddleware = { - name: 'sap-fe-mockserver', + name: MOCKSERVER_MIDDLEWARE_NAME, beforeMiddleware: 'csp', configuration: mockserverMiddlewareConfig }; @@ -285,24 +286,24 @@ describe('UI5Config', () => { ui5Config.addCustomMiddleware([customMockserverMiddleware]); }); - test('add with overwrite', () => { - // should overwrite existing services - ui5Config.updateMockServerMiddlewareServices(path, undefined, true); + test('update with given path (no existing services)', () => { + ui5Config.removeCustomMiddleware(MOCKSERVER_MIDDLEWARE_NAME); + ui5Config.updateMockServerMiddleware(path); expect(ui5Config.toString()).toMatchSnapshot(); }); - test('add with given path', () => { - ui5Config.updateMockServerMiddlewareServices(path); + test('update with given path (existing services)', () => { + ui5Config.updateMockServerMiddleware(path); expect(ui5Config.toString()).toMatchSnapshot(); }); - test('add without path', () => { - ui5Config.updateMockServerMiddlewareServices(); + test('update without path (existing services)', () => { + ui5Config.updateMockServerMiddleware(); expect(ui5Config.toString()).toMatchSnapshot(); }); - test('add with given path and annotationsConfig', () => { - ui5Config.updateMockServerMiddlewareServices(path, annotationsConfig); + test('update with given path and annotationsConfig (existing services)', () => { + ui5Config.updateMockServerMiddleware(path, annotationsConfig); expect(ui5Config.toString()).toMatchSnapshot(); }); }); From 23871a1eda0ddb133b53ec6cb6af7999077d5135 Mon Sep 17 00:00:00 2001 From: broksy Date: Wed, 18 Sep 2024 17:46:38 +0300 Subject: [PATCH 07/20] fix: functionality for older tests --- packages/ui5-config/src/middlewares.ts | 49 ++++++++++++------- packages/ui5-config/src/ui5config.ts | 2 +- .../test/__snapshots__/index.test.ts.snap | 28 +++++++++-- 3 files changed, 57 insertions(+), 22 deletions(-) diff --git a/packages/ui5-config/src/middlewares.ts b/packages/ui5-config/src/middlewares.ts index df08fcbc71..a8a319f923 100644 --- a/packages/ui5-config/src/middlewares.ts +++ b/packages/ui5-config/src/middlewares.ts @@ -106,10 +106,39 @@ export function getFioriToolsProxyMiddlewareConfig( return { config: fioriToolsProxy, comments }; } -export const getMockServerMiddlewareConfig = ( +const handleServicesForMiddlewareConfig = ( + forceAddService: boolean, services: MockserverConfig['services'] = [], + path?: string +): MockserverConfig['services'] => { + const serviceData = { + urlPath: path ?? '', + metadataPath: './webapp/localService/metadata.xml', + mockdataPath: './webapp/localService/data', + generateMockData: true + }; + const placeholderService = { + urlPath: path ?? '', + metadataPath: 'metadataPath', + // mockdata path should not be generated in case no mock data exists + mockdataPath: 'mockdataPath', + // In case of update, this user value should not be overwritten + generateMockData: true + }; + if (forceAddService) { + return [serviceData]; + } else if (services.length === 0) { + return [placeholderService]; + } else { + return [...services, placeholderService]; + } +}; + +export const getMockServerMiddlewareConfig = ( + services: MockserverConfig['services'], path?: string, - annotationsConfig: MockserverConfig['annotations'] = [] + annotationsConfig: MockserverConfig['annotations'] = [], + forceAddService = false ): CustomMiddleware => { path = path?.replace(/\/$/, ''); // Mockserver is sensitive to trailing '/' return { @@ -117,21 +146,7 @@ export const getMockServerMiddlewareConfig = ( beforeMiddleware: 'csp', configuration: { mountPath: '/', - // Services should be empty in case no service is provided services: [] - services: - services.length > 0 - ? [ - ...services, - { - urlPath: path ?? '', - metadataPath: 'metadataPath', - // mockdata path should not be generated in case no mock data exists - mockdataPath: 'mockdataPath', - // In case of update, this user value should not be overwritten - generateMockData: true - } - ] - : undefined, + services: handleServicesForMiddlewareConfig(forceAddService, services, path), annotations: annotationsConfig } }; diff --git a/packages/ui5-config/src/ui5config.ts b/packages/ui5-config/src/ui5config.ts index fdee2d6e0c..95e99d3394 100644 --- a/packages/ui5-config/src/ui5config.ts +++ b/packages/ui5-config/src/ui5config.ts @@ -322,7 +322,7 @@ export class UI5Config { * @memberof UI5Config */ public addMockServerMiddleware(path?: string, annotationsConfig?: MockserverConfig['annotations']): this { - const middleware = getMockServerMiddlewareConfig(undefined, path, annotationsConfig); + const middleware = getMockServerMiddlewareConfig(undefined, path, annotationsConfig, true); this.document.appendTo({ path: 'server.customMiddleware', value: middleware diff --git a/packages/ui5-config/test/__snapshots__/index.test.ts.snap b/packages/ui5-config/test/__snapshots__/index.test.ts.snap index c7386413b4..75898af1be 100644 --- a/packages/ui5-config/test/__snapshots__/index.test.ts.snap +++ b/packages/ui5-config/test/__snapshots__/index.test.ts.snap @@ -317,6 +317,11 @@ exports[`UI5Config addMockServerMiddleware add with given path 1`] = ` beforeMiddleware: csp configuration: mountPath: / + services: + - urlPath: /~testpath~ + metadataPath: ./webapp/localService/metadata.xml + mockdataPath: ./webapp/localService/data + generateMockData: true annotations: [] " `; @@ -328,6 +333,11 @@ exports[`UI5Config addMockServerMiddleware add with path and annotationsConfig 1 beforeMiddleware: csp configuration: mountPath: / + services: + - urlPath: /~testpath~ + metadataPath: ./webapp/localService/metadata.xml + mockdataPath: ./webapp/localService/data + generateMockData: true annotations: - localPath: ./webapp/annotations/annotations.xml urlPath: annotations.xml @@ -341,6 +351,11 @@ exports[`UI5Config addMockServerMiddleware add without path 1`] = ` beforeMiddleware: csp configuration: mountPath: / + services: + - urlPath: '' + metadataPath: ./webapp/localService/metadata.xml + mockdataPath: ./webapp/localService/data + generateMockData: true annotations: [] " `; @@ -537,7 +552,7 @@ exports[`UI5Config setMetadata set name and copyright 1`] = ` " `; -exports[`UI5Config updateMockServerMiddlewareServices update with given path (existing services) 1`] = ` +exports[`UI5Config updateMockServerMiddleware update with given path (existing services) 1`] = ` "server: customMiddleware: - name: sap-fe-mockserver @@ -556,18 +571,23 @@ exports[`UI5Config updateMockServerMiddlewareServices update with given path (ex " `; -exports[`UI5Config updateMockServerMiddlewareServices update with given path (no existing services) 1`] = ` +exports[`UI5Config updateMockServerMiddleware update with given path (no existing services) 1`] = ` "server: customMiddleware: - name: sap-fe-mockserver beforeMiddleware: csp configuration: mountPath: / + services: + - urlPath: /~testpath~ + metadataPath: metadataPath + mockdataPath: mockdataPath + generateMockData: true annotations: [] " `; -exports[`UI5Config updateMockServerMiddlewareServices update with given path and annotationsConfig (existing services) 1`] = ` +exports[`UI5Config updateMockServerMiddleware update with given path and annotationsConfig (existing services) 1`] = ` "server: customMiddleware: - name: sap-fe-mockserver @@ -588,7 +608,7 @@ exports[`UI5Config updateMockServerMiddlewareServices update with given path and " `; -exports[`UI5Config updateMockServerMiddlewareServices update without path (existing services) 1`] = ` +exports[`UI5Config updateMockServerMiddleware update without path (existing services) 1`] = ` "server: customMiddleware: - name: sap-fe-mockserver From d19b21dd8cd8654560f8447639e069f4e6b9dea3 Mon Sep 17 00:00:00 2001 From: broksy Date: Thu, 19 Sep 2024 16:17:14 +0300 Subject: [PATCH 08/20] fix: adapt for updated ui5-config api --- .../src/mockserver-config/ui5-mock-yaml.ts | 2 +- .../fixtures/ui5-mock-config/package.json | 38 +++++++++++++++++ .../fixtures/ui5-mock-config/ui5-mock.yaml | 14 +++++++ .../test/fixtures/ui5-mock-config/ui5.yaml | 24 +++++++++++ .../ui5-mock-config/webapp/index.html | 1 + .../ui5-mock-config/webapp/manifest.json | 42 +++++++++++++++++++ .../__snapshots__/index.test.ts.snap | 24 +++++++++++ .../test/unit/mockserver-config/index.test.ts | 9 ++++ .../mockserver-config/ui5-mock-yaml.test.ts | 2 +- 9 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 packages/mockserver-config-writer/test/fixtures/ui5-mock-config/package.json create mode 100644 packages/mockserver-config-writer/test/fixtures/ui5-mock-config/ui5-mock.yaml create mode 100644 packages/mockserver-config-writer/test/fixtures/ui5-mock-config/ui5.yaml create mode 100644 packages/mockserver-config-writer/test/fixtures/ui5-mock-config/webapp/index.html create mode 100644 packages/mockserver-config-writer/test/fixtures/ui5-mock-config/webapp/manifest.json diff --git a/packages/mockserver-config-writer/src/mockserver-config/ui5-mock-yaml.ts b/packages/mockserver-config-writer/src/mockserver-config/ui5-mock-yaml.ts index 0edd8bd688..6c8973d1cc 100644 --- a/packages/mockserver-config-writer/src/mockserver-config/ui5-mock-yaml.ts +++ b/packages/mockserver-config-writer/src/mockserver-config/ui5-mock-yaml.ts @@ -71,7 +71,7 @@ async function updateUi5MockYamlConfig( annotationsConfig?: MockserverConfig['annotations'] ): Promise { const existingUi5MockYamlConfig = await UI5Config.newInstance(fs.read(ui5MockYamlPath)); - existingUi5MockYamlConfig.updateCustomMiddleware(await getNewMockserverMiddleware(path, annotationsConfig)); + existingUi5MockYamlConfig.updateMockServerMiddleware(path, annotationsConfig); return existingUi5MockYamlConfig; } diff --git a/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/package.json b/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/package.json new file mode 100644 index 0000000000..8fead8aa15 --- /dev/null +++ b/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/package.json @@ -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" + ] + } +} diff --git a/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/ui5-mock.yaml b/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/ui5-mock.yaml new file mode 100644 index 0000000000..b494fdfca2 --- /dev/null +++ b/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/ui5-mock.yaml @@ -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 \ No newline at end of file diff --git a/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/ui5.yaml b/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/ui5.yaml new file mode 100644 index 0000000000..e2f834915a --- /dev/null +++ b/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/ui5.yaml @@ -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 diff --git a/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/webapp/index.html b/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/webapp/index.html new file mode 100644 index 0000000000..763b0739be --- /dev/null +++ b/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/webapp/index.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/webapp/manifest.json b/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/webapp/manifest.json new file mode 100644 index 0000000000..0ede3445e5 --- /dev/null +++ b/packages/mockserver-config-writer/test/fixtures/ui5-mock-config/webapp/manifest.json @@ -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 + } + } +} diff --git a/packages/mockserver-config-writer/test/unit/mockserver-config/__snapshots__/index.test.ts.snap b/packages/mockserver-config-writer/test/unit/mockserver-config/__snapshots__/index.test.ts.snap index 7e738ae6ca..12cbb5f189 100644 --- a/packages/mockserver-config-writer/test/unit/mockserver-config/__snapshots__/index.test.ts.snap +++ b/packages/mockserver-config-writer/test/unit/mockserver-config/__snapshots__/index.test.ts.snap @@ -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: [] +" +`; diff --git a/packages/mockserver-config-writer/test/unit/mockserver-config/index.test.ts b/packages/mockserver-config-writer/test/unit/mockserver-config/index.test.ts index 17a3a92184..5f3f662bd2 100644 --- a/packages/mockserver-config-writer/test/unit/mockserver-config/index.test.ts +++ b/packages/mockserver-config-writer/test/unit/mockserver-config/index.test.ts @@ -17,6 +17,15 @@ describe('Test generateMockserverConfig()', () => { }); expect(fs.read(join(basePath, 'ui5-mock.yaml'))).toMatchSnapshot(); }); + + test('Add config to project with existing ui5-mock.yaml', async () => { + const basePath = join(__dirname, '../../fixtures/ui5-mock-config'); + const webappPath = join(basePath, 'webapp'); + + const fs = await generateMockserverConfig(basePath, { webappPath }); + const ui5MockYaml = join(basePath, 'ui5-mock.yaml'); + expect(fs.read(ui5MockYaml)).toMatchSnapshot(); + }); }); describe('Test removeMockserverConfig()', () => { diff --git a/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts b/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts index 423763a940..ffb474c206 100644 --- a/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts +++ b/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts @@ -149,7 +149,7 @@ server: - name: sap-fe-mockserver beforeMiddleware: fiori-tools-proxy configuration: - service: + services: urlBasePath: /some/previous/service/uri name: '' metadataXmlPath: ./webapp/localService/metadata.xml From 303d78b1221bcb8f81d5e7307e32e70ffc3c7946 Mon Sep 17 00:00:00 2001 From: broksy Date: Thu, 19 Sep 2024 16:32:57 +0300 Subject: [PATCH 09/20] fix: failing tests --- packages/ui5-config/src/middlewares.ts | 19 ++++++++++--------- .../test/__snapshots__/index.test.ts.snap | 12 ++++++------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/ui5-config/src/middlewares.ts b/packages/ui5-config/src/middlewares.ts index a8a319f923..6c47022ce2 100644 --- a/packages/ui5-config/src/middlewares.ts +++ b/packages/ui5-config/src/middlewares.ts @@ -117,20 +117,21 @@ const handleServicesForMiddlewareConfig = ( mockdataPath: './webapp/localService/data', generateMockData: true }; - const placeholderService = { - urlPath: path ?? '', - metadataPath: 'metadataPath', - // mockdata path should not be generated in case no mock data exists - mockdataPath: 'mockdataPath', - // In case of update, this user value should not be overwritten - generateMockData: true - }; if (forceAddService) { return [serviceData]; } else if (services.length === 0) { + // some dummy data (later overwritten with real service data) to make sure there is always service defined + const placeholderService = { + urlPath: path ?? '', + metadataPath: 'metadataPath', + // mockdata path should not be generated in case no mock data exists + mockdataPath: 'mockdataPath', + // In case of update, this user value should not be overwritten + generateMockData: true + }; return [placeholderService]; } else { - return [...services, placeholderService]; + return [...services, serviceData]; } }; diff --git a/packages/ui5-config/test/__snapshots__/index.test.ts.snap b/packages/ui5-config/test/__snapshots__/index.test.ts.snap index 75898af1be..dc5c04fcb3 100644 --- a/packages/ui5-config/test/__snapshots__/index.test.ts.snap +++ b/packages/ui5-config/test/__snapshots__/index.test.ts.snap @@ -564,8 +564,8 @@ exports[`UI5Config updateMockServerMiddleware update with given path (existing s metadataPath: different-metadataPath generateMockData: true - urlPath: /~testpath~ - metadataPath: metadataPath - mockdataPath: mockdataPath + metadataPath: ./webapp/localService/metadata.xml + mockdataPath: ./webapp/localService/data generateMockData: true annotations: [] " @@ -599,8 +599,8 @@ exports[`UI5Config updateMockServerMiddleware update with given path and annotat metadataPath: different-metadataPath generateMockData: true - urlPath: /~testpath~ - metadataPath: metadataPath - mockdataPath: mockdataPath + metadataPath: ./webapp/localService/metadata.xml + mockdataPath: ./webapp/localService/data generateMockData: true annotations: - localPath: ./webapp/annotations/annotations.xml @@ -620,8 +620,8 @@ exports[`UI5Config updateMockServerMiddleware update without path (existing serv metadataPath: different-metadataPath generateMockData: true - urlPath: '' - metadataPath: metadataPath - mockdataPath: mockdataPath + metadataPath: ./webapp/localService/metadata.xml + mockdataPath: ./webapp/localService/data generateMockData: true annotations: [] " From 8768495e5e8333c8dafb58bbce6024dcfaf4646c Mon Sep 17 00:00:00 2001 From: broksy Date: Fri, 27 Sep 2024 14:18:24 +0300 Subject: [PATCH 10/20] feat: replace existing service data if path exists --- packages/ui5-config/src/middlewares.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/ui5-config/src/middlewares.ts b/packages/ui5-config/src/middlewares.ts index 6c47022ce2..e790635812 100644 --- a/packages/ui5-config/src/middlewares.ts +++ b/packages/ui5-config/src/middlewares.ts @@ -131,7 +131,18 @@ const handleServicesForMiddlewareConfig = ( }; return [placeholderService]; } else { - return [...services, serviceData]; + // check if service with given path already exists + let existingServiceIndex: number = -1; + services.forEach((service, index: number) => { + if (service.urlPath === path) { + existingServiceIndex = index; + } + }); + if (existingServiceIndex > -1) { + services[existingServiceIndex] = serviceData; + } else { + return [...services, serviceData]; + } } }; From 7d9168a10cd83150767959fcdb5e552726ec801d Mon Sep 17 00:00:00 2001 From: broksy Date: Fri, 27 Sep 2024 15:44:48 +0300 Subject: [PATCH 11/20] fix: test data --- .../__snapshots__/ui5-mock-yaml.test.ts.snap | 23 ++++++++++++++----- .../mockserver-config/ui5-mock-yaml.test.ts | 10 ++++---- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/mockserver-config-writer/test/unit/mockserver-config/__snapshots__/ui5-mock-yaml.test.ts.snap b/packages/mockserver-config-writer/test/unit/mockserver-config/__snapshots__/ui5-mock-yaml.test.ts.snap index 58b5837459..9ab76a6253 100644 --- a/packages/mockserver-config-writer/test/unit/mockserver-config/__snapshots__/ui5-mock-yaml.test.ts.snap +++ b/packages/mockserver-config-writer/test/unit/mockserver-config/__snapshots__/ui5-mock-yaml.test.ts.snap @@ -153,12 +153,17 @@ server: - name: sap-fe-mockserver beforeMiddleware: csp configuration: - mountPath: / services: + - urlBasePath: /some/previous/service/uri + name: '' + metadataXmlPath: ./webapp/localService/metadata.xml + mockdataRootPath: ./webapp/localService/data + generateMockData: true - urlPath: path/to/service metadataPath: ./webapp/localService/metadata.xml mockdataPath: ./webapp/localService/data generateMockData: true + mountPath: / annotations: [] - name: middleware-after " @@ -175,12 +180,17 @@ server: - name: sap-fe-mockserver beforeMiddleware: csp configuration: - mountPath: / services: + - urlBasePath: /some/previous/service/uri + name: '' + metadataXmlPath: ./webapp/localService/metadata.xml + mockdataRootPath: ./webapp/localService/data + generateMockData: true - urlPath: ds/uri metadataPath: ./webapp/localService/metadata.xml mockdataPath: ./webapp/localService/data generateMockData: true + mountPath: / annotations: [] - name: middleware-after " @@ -197,12 +207,13 @@ server: - name: sap-fe-mockserver beforeMiddleware: csp configuration: - mountPath: / services: - - urlPath: '' - metadataPath: ./webapp/localService/metadata.xml - mockdataPath: ./webapp/localService/data + - urlBasePath: /some/previous/service/uri + name: '' + metadataXmlPath: ./webapp/localService/metadata.xml + mockdataRootPath: ./webapp/localService/data generateMockData: true + mountPath: / annotations: - localPath: ./webapp/localService/SEPMRA_PROD_MAN.xml urlPath: /sap/opu/odata/IWFND/CATALOGSERVICE;v=2/Annotations(TechnicalName='SEPMRA_PROD_MAN',Version='0001')/$value/ diff --git a/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts b/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts index ffb474c206..14c540cc37 100644 --- a/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts +++ b/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts @@ -150,11 +150,11 @@ server: beforeMiddleware: fiori-tools-proxy configuration: services: - urlBasePath: /some/previous/service/uri - name: '' - metadataXmlPath: ./webapp/localService/metadata.xml - mockdataRootPath: ./webapp/localService/data - generateMockData: true + - urlBasePath: /some/previous/service/uri + name: '' + metadataXmlPath: ./webapp/localService/metadata.xml + mockdataRootPath: ./webapp/localService/data + generateMockData: true - name: middleware-after`, [manifestJsonPath]: manifestContent }); From fe45d45a99f352a425fb1659db7231dbed9098bd Mon Sep 17 00:00:00 2001 From: broksy Date: Fri, 27 Sep 2024 18:02:05 +0300 Subject: [PATCH 12/20] feat: overwrite option for mock-server cli --- .../create/src/cli/add/mockserver-config.ts | 10 ++++-- .../unit/cli/add/mockserver-config.test.ts | 31 +++++++++++++++++++ .../src/mockserver-config/index.ts | 10 ++++-- .../src/mockserver-config/ui5-mock-yaml.ts | 16 +++++++--- .../src/prompt/index.ts | 29 ++++++++++++----- .../mockserver-config-writer.i18n.json | 3 +- .../mockserver-config/ui5-mock-yaml.test.ts | 2 +- .../test/unit/prompt/index.test.ts | 31 +++++++++++++++++++ .../test/__snapshots__/index.test.ts.snap | 10 +++++- 9 files changed, 123 insertions(+), 19 deletions(-) diff --git a/packages/create/src/cli/add/mockserver-config.ts b/packages/create/src/cli/add/mockserver-config.ts index 3d640ec7e4..2da84215ad 100644 --- a/packages/create/src/cli/add/mockserver-config.ts +++ b/packages/create/src/cli/add/mockserver-config.ts @@ -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(() => { diff --git a/packages/create/test/unit/cli/add/mockserver-config.test.ts b/packages/create/test/unit/cli/add/mockserver-config.test.ts index 25831713fa..19cbb8e4ad 100644 --- a/packages/create/test/unit/cli/add/mockserver-config.test.ts +++ b/packages/create/test/unit/cli/add/mockserver-config.test.ts @@ -108,6 +108,37 @@ describe('Test command add mockserver-config', () => { expect(spawnSpy).toBeCalled(); }); + test('Test create-fiori add mockserver-config --interactive with overwrite option', async () => { + // Mock setup + jest.spyOn(mockserverWriter, 'getMockserverConfigQuestions').mockReturnValue([ + { + 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([ + { + message: 'Overwrite existing services', + name: 'overwrite', + type: 'confirm' + } + ]); + expect(fsMock.commit).toBeCalled(); + expect(spawnSpy).toBeCalled(); + }); + test('Test create-fiori add mockserver-config --verbose', async () => { // Test execution const command = new Command('add'); diff --git a/packages/mockserver-config-writer/src/mockserver-config/index.ts b/packages/mockserver-config-writer/src/mockserver-config/index.ts index af735d6352..c281f6cabd 100644 --- a/packages/mockserver-config-writer/src/mockserver-config/index.ts +++ b/packages/mockserver-config-writer/src/mockserver-config/index.ts @@ -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 - memfs editor instance with updated files */ -export async function generateMockserverConfig(basePath: string, data: MockserverConfig, fs?: Editor): Promise { +export async function generateMockserverConfig( + basePath: string, + data: MockserverConfig, + fs?: Editor, + overwrite = false +): Promise { 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; } diff --git a/packages/mockserver-config-writer/src/mockserver-config/ui5-mock-yaml.ts b/packages/mockserver-config-writer/src/mockserver-config/ui5-mock-yaml.ts index 6c8973d1cc..dada12e30e 100644 --- a/packages/mockserver-config-writer/src/mockserver-config/ui5-mock-yaml.ts +++ b/packages/mockserver-config-writer/src/mockserver-config/ui5-mock-yaml.ts @@ -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 { const ui5MockYamlPath = join(basePath, 'ui5-mock.yaml'); let mockConfig; @@ -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) @@ -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} - Update Yaml Doc */ async function updateUi5MockYamlConfig( fs: Editor, ui5MockYamlPath: string, path?: string, - annotationsConfig?: MockserverConfig['annotations'] + annotationsConfig?: MockserverConfig['annotations'], + overwrite = false ): Promise { const existingUi5MockYamlConfig = await UI5Config.newInstance(fs.read(ui5MockYamlPath)); - existingUi5MockYamlConfig.updateMockServerMiddleware(path, annotationsConfig); + if (overwrite) { + existingUi5MockYamlConfig.updateCustomMiddleware(await getNewMockserverMiddleware(path, annotationsConfig)); + } else { + existingUi5MockYamlConfig.updateMockServerMiddleware(path, annotationsConfig); + } return existingUi5MockYamlConfig; } diff --git a/packages/mockserver-config-writer/src/prompt/index.ts b/packages/mockserver-config-writer/src/prompt/index.ts index f1daf6432e..1e3b9120ee 100644 --- a/packages/mockserver-config-writer/src/prompt/index.ts +++ b/packages/mockserver-config-writer/src/prompt/index.ts @@ -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 = { +export function getMockserverConfigQuestions(params?: { + webappPath?: string; + fs?: Editor; + askForOverwrite?: boolean; +}): PromptObject[] { + const prompts: PromptObject[] = []; + const questionPath: Partial = { name: 'path', message: t('questions.pathToMock') }; @@ -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 = { + type: 'confirm', + name: 'overwrite', + message: t('questions.overwrite') + }; + prompts.push(questionOverwrite as PromptObject); + } + return prompts; } diff --git a/packages/mockserver-config-writer/src/translations/mockserver-config-writer.i18n.json b/packages/mockserver-config-writer/src/translations/mockserver-config-writer.i18n.json index 942544770a..72cc7e30b2 100644 --- a/packages/mockserver-config-writer/src/translations/mockserver-config-writer.i18n.json +++ b/packages/mockserver-config-writer/src/translations/mockserver-config-writer.i18n.json @@ -1,5 +1,6 @@ { "questions": { - "pathToMock": "Path to mocked service" + "pathToMock": "Path to mocked service", + "overwrite": "Overwrite existing services" } } diff --git a/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts b/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts index 14c540cc37..90d6eb390b 100644 --- a/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts +++ b/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts @@ -126,7 +126,7 @@ describe('Test enhanceYaml()', () => { findCustomMiddleware: () => undefined } as unknown as UI5Config); const fs = getFsWithUi5MockYaml('{}'); - await expect(enhanceYaml(fs, basePath, webappPath)).rejects.toThrow('mockserver'); + await expect(enhanceYaml(fs, basePath, webappPath, undefined, true)).rejects.toThrow('mockserver'); }); function getFs(files: { [path: string]: string }): Editor { diff --git a/packages/mockserver-config-writer/test/unit/prompt/index.test.ts b/packages/mockserver-config-writer/test/unit/prompt/index.test.ts index 4cf7d38d11..ab80c72e8f 100644 --- a/packages/mockserver-config-writer/test/unit/prompt/index.test.ts +++ b/packages/mockserver-config-writer/test/unit/prompt/index.test.ts @@ -31,6 +31,37 @@ describe('Test function getMockserverConfigQuestions()', () => { ]); }); + test('Question for overwrite, no mem-fs passed', () => { + expect(getMockserverConfigQuestions({ askForOverwrite: true })).toEqual([ + { + name: 'path', + type: 'text', + message: t('questions.pathToMock') + }, + { + name: 'overwrite', + type: 'confirm', + message: t('questions.overwrite') + } + ]); + }); + + test('Question for overwrite, mem-fs passed', () => { + const fs = create(createStorage()); + expect(getMockserverConfigQuestions({ fs, askForOverwrite: true })).toEqual([ + { + name: 'path', + type: 'text', + message: t('questions.pathToMock') + }, + { + name: 'overwrite', + type: 'confirm', + message: t('questions.overwrite') + } + ]); + }); + test('Question with one choice, mem-fs passed, no odata version', () => { const fs = create(createStorage()); fs.writeJSON(join('/webapp/manifest.json'), { diff --git a/packages/ui5-config/test/__snapshots__/index.test.ts.snap b/packages/ui5-config/test/__snapshots__/index.test.ts.snap index 89f1183c1c..a75bb73ab8 100644 --- a/packages/ui5-config/test/__snapshots__/index.test.ts.snap +++ b/packages/ui5-config/test/__snapshots__/index.test.ts.snap @@ -44,6 +44,13 @@ exports[`UI5Config add/find/update/removeCustomMiddleware updateMiddleware exist - name: custom-middleware afterMiddleware: ~newMiddleware configuration: + ui5: + path: + - /resources + - /test-resources + url: http://ui5.example + version: 1.95.1 + debug: true newValue: should: overwrite existing " @@ -420,7 +427,7 @@ exports[`UI5Config addServeStaticConfig update serve static config 1`] = ` "server: customMiddleware: - name: fiori-tools-servestatic - beforeMiddleware: fiori-tools-proxy + afterMiddleware: compression configuration: paths: - path: /resources/targetapp @@ -433,6 +440,7 @@ exports[`UI5Config addServeStaticConfig update serve static config 1`] = ` - path: /~other src: /~otherSrc fallthrough: false + beforeMiddleware: fiori-tools-proxy - name: fiori-tools-proxy afterMiddleware: compression configuration: From ee5ab5e9ae2568410445e4d9e4ca89c2cc4b2690 Mon Sep 17 00:00:00 2001 From: broksy Date: Fri, 27 Sep 2024 18:20:18 +0300 Subject: [PATCH 13/20] fix: mockserver config cli --- packages/create/src/cli/add/mockserver-config.ts | 4 ++-- .../test/unit/cli/add/mockserver-config.test.ts | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/create/src/cli/add/mockserver-config.ts b/packages/create/src/cli/add/mockserver-config.ts index 2da84215ad..c85ad47f1d 100644 --- a/packages/create/src/cli/add/mockserver-config.ts +++ b/packages/create/src/cli/add/mockserver-config.ts @@ -58,9 +58,9 @@ async function addMockserverConfig( if (interactive) { const questions = getMockserverConfigQuestions({ webappPath, askForOverwrite: true }); const responses = await prompt(questions); - config.ui5MockYamlConfig = responses.path; + config.ui5MockYamlConfig = responses?.path; // User response for whether to overwrite existing services in mock-server config - overwriteExisting = !!responses.overwrite; + overwriteExisting = !!responses?.overwrite; } const fs = await generateMockserverConfig(basePath, config, undefined, overwriteExisting); await traceChanges(fs); diff --git a/packages/create/test/unit/cli/add/mockserver-config.test.ts b/packages/create/test/unit/cli/add/mockserver-config.test.ts index 19cbb8e4ad..bb7e648bb7 100644 --- a/packages/create/test/unit/cli/add/mockserver-config.test.ts +++ b/packages/create/test/unit/cli/add/mockserver-config.test.ts @@ -103,7 +103,7 @@ 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(); }); @@ -111,6 +111,11 @@ describe('Test command add mockserver-config', () => { test('Test create-fiori add mockserver-config --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', @@ -129,6 +134,11 @@ describe('Test command add mockserver-config', () => { 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', From 18dcaf7576e436a9c7be5b1ac0719cb64810b4ca Mon Sep 17 00:00:00 2001 From: broksy Date: Wed, 9 Oct 2024 15:20:00 +0300 Subject: [PATCH 14/20] fix: adding of backend to ui5 yaml files --- packages/odata-service-writer/src/index.ts | 16 ++++++-- packages/ui5-config/src/ui5config.ts | 29 ++++++++++---- .../test/__snapshots__/index.test.ts.snap | 40 ++++++++++++++++++- packages/ui5-config/test/index.test.ts | 23 ++++++++++- 4 files changed, 95 insertions(+), 13 deletions(-) diff --git a/packages/odata-service-writer/src/index.ts b/packages/odata-service-writer/src/index.ts index 6f0340c72f..a3e919a224 100644 --- a/packages/odata-service-writer/src/index.ts +++ b/packages/odata-service-writer/src/index.ts @@ -109,10 +109,18 @@ async function generate(basePath: string, service: OdataService, fs?: Editor): P ui5LocalConfigPath = join(dirname(paths.ui5Yaml), 'ui5-local.yaml'); if (fs.exists(ui5LocalConfigPath)) { ui5LocalConfig = await UI5Config.newInstance(fs.read(ui5LocalConfigPath)); - ui5LocalConfig.addFioriToolsProxydMiddleware({ - backend: [service.previewSettings as ProxyBackend], - ignoreCertError: service.ignoreCertError - }); + try { + ui5LocalConfig.addBackendToFioriToolsProxydMiddleware(service.previewSettings as ProxyBackend); + } catch (error: any) { + if (error instanceof YAMLError && error.code === yamlErrorCode.nodeNotFound) { + ui5LocalConfig.addFioriToolsProxydMiddleware({ + backend: [service.previewSettings as ProxyBackend], + ignoreCertError: service.ignoreCertError + }); + } else { + throw error; + } + } } } diff --git a/packages/ui5-config/src/ui5config.ts b/packages/ui5-config/src/ui5config.ts index ca79b71618..c83fc521f4 100644 --- a/packages/ui5-config/src/ui5config.ts +++ b/packages/ui5-config/src/ui5config.ts @@ -267,7 +267,7 @@ export class UI5Config { } /** - * Adds a backend configuration to an existing fiori-tools-proxy middleware. If the config does not contain a fiori-tools-proxy middleware, an error is thrown. + * Adds a backend configuration to an existing fiori-tools-proxy middleware keeping any existing backend configurations. If the config does not contain a fiori-tools-proxy middleware, an error is thrown. * * @param backend config of backend that is to be proxied * @returns {UI5Config} the UI5Config instance @@ -280,12 +280,27 @@ export class UI5Config { throw new Error('Could not find fiori-tools-proxy'); } const comments = getBackendComments(backend); - const backendNode = this.document.createNode({ value: backend, comments }); - - this.document - .getMap({ start: proxyMiddleware as YAMLMap, path: 'configuration' }) - .set('backend', [backendNode]); - + let backendNode; + const proxyMiddlewareYamlContent = this.findCustomMiddleware(fioriToolsProxy); + const proxyMiddlewareConfig = proxyMiddlewareYamlContent?.configuration as FioriToolsProxyConfig; + // Avoid adding duplicates by checking existing backend configs + if (proxyMiddlewareConfig?.backend) { + if (!proxyMiddlewareConfig.backend.find((existingBackend) => existingBackend.url === backend.url)) { + // Create new 'backend' node in yaml for middleware config keeping previous backend definitions + backendNode = this.document.createNode({ + value: [...proxyMiddlewareConfig.backend, backend], + comments + }); + } + } else { + // Create new 'backend' node in yaml for middleware config + backendNode = this.document.createNode({ value: backend, comments }); + } + if (backendNode) { + this.document + .getMap({ start: proxyMiddleware as YAMLMap, path: 'configuration' }) + .set('backend', [backendNode]); + } return this; } diff --git a/packages/ui5-config/test/__snapshots__/index.test.ts.snap b/packages/ui5-config/test/__snapshots__/index.test.ts.snap index a75bb73ab8..e5a1788070 100644 --- a/packages/ui5-config/test/__snapshots__/index.test.ts.snap +++ b/packages/ui5-config/test/__snapshots__/index.test.ts.snap @@ -156,7 +156,45 @@ exports[`UI5Config addAbapDeployTask use open source task 1`] = ` " `; -exports[`UI5Config addBackendToFioriToolsProxydMiddleware add proxy without out backend first and then call add backend 1`] = ` +exports[`UI5Config addBackendToFioriToolsProxydMiddleware add proxy with backend first and then call add backend for existing backend 1`] = ` +"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: + - url: http://localhost:8080 + path: /~testpath~ + ui5: + path: + - /resources + - /test-resources + url: https://ui5.sap.com +" +`; + +exports[`UI5Config addBackendToFioriToolsProxydMiddleware add proxy with backend first and then call add backend for unexisting backend 1`] = ` +"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: + - - url: http://different.host:8080 + path: /~testpath~ + - url: http://localhost:8080 + path: /~testpath~ + ui5: + path: + - /resources + - /test-resources + url: https://ui5.sap.com +" +`; + +exports[`UI5Config addBackendToFioriToolsProxydMiddleware add proxy without backend first and then call add backend 1`] = ` "server: customMiddleware: - name: fiori-tools-proxy diff --git a/packages/ui5-config/test/index.test.ts b/packages/ui5-config/test/index.test.ts index fea35bd35f..4e64c9df1c 100644 --- a/packages/ui5-config/test/index.test.ts +++ b/packages/ui5-config/test/index.test.ts @@ -213,7 +213,7 @@ describe('UI5Config', () => { }); describe('addBackendToFioriToolsProxydMiddleware', () => { - test('add proxy without out backend first and then call add backend', () => { + test('add proxy without backend first and then call add backend', () => { ui5Config.addFioriToolsProxydMiddleware({ ui5: {} }); ui5Config.addBackendToFioriToolsProxydMiddleware({ url, @@ -222,6 +222,27 @@ describe('UI5Config', () => { expect(ui5Config.toString()).toMatchSnapshot(); }); + test('add proxy with backend first and then call add backend for existing backend', () => { + ui5Config.addFioriToolsProxydMiddleware({ ui5: {}, backend: [{ url, path }] }); + ui5Config.addBackendToFioriToolsProxydMiddleware({ + url, + path + }); + expect(ui5Config.toString()).toMatchSnapshot(); + }); + + test('add proxy with backend first and then call add backend for unexisting backend', () => { + ui5Config.addFioriToolsProxydMiddleware({ + ui5: {}, + backend: [{ url: 'http://different.host:8080', path }] + }); + ui5Config.addBackendToFioriToolsProxydMiddleware({ + url, + path + }); + expect(ui5Config.toString()).toMatchSnapshot(); + }); + test('should add comments with backend authentication type as reentrance ticket', () => { ui5Config.addFioriToolsProxydMiddleware({ ui5: {} }); ui5Config.addBackendToFioriToolsProxydMiddleware({ From f5223e7f26071a6dd99a8af3ce48b2dfecf5970d Mon Sep 17 00:00:00 2001 From: broksy Date: Wed, 9 Oct 2024 16:33:24 +0300 Subject: [PATCH 15/20] fix: try catch block for missing fiori-tools-proxy --- packages/odata-service-writer/src/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/odata-service-writer/src/index.ts b/packages/odata-service-writer/src/index.ts index a3e919a224..12c886f99c 100644 --- a/packages/odata-service-writer/src/index.ts +++ b/packages/odata-service-writer/src/index.ts @@ -112,7 +112,11 @@ async function generate(basePath: string, service: OdataService, fs?: Editor): P try { ui5LocalConfig.addBackendToFioriToolsProxydMiddleware(service.previewSettings as ProxyBackend); } catch (error: any) { - if (error instanceof YAMLError && error.code === yamlErrorCode.nodeNotFound) { + // If error informs about missing fiori-tools-proxy then create it + if ( + (error instanceof YAMLError && error.code === yamlErrorCode.nodeNotFound) || + error.message === 'Could not find fiori-tools-proxy' + ) { ui5LocalConfig.addFioriToolsProxydMiddleware({ backend: [service.previewSettings as ProxyBackend], ignoreCertError: service.ignoreCertError From 0f259f871f5a7181f43cd88580319f3b0addd972 Mon Sep 17 00:00:00 2001 From: broksy Date: Wed, 9 Oct 2024 17:00:07 +0300 Subject: [PATCH 16/20] feat: changeset --- .changeset/late-rings-greet.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .changeset/late-rings-greet.md diff --git a/.changeset/late-rings-greet.md b/.changeset/late-rings-greet.md new file mode 100644 index 0000000000..3bebe113b4 --- /dev/null +++ b/.changeset/late-rings-greet.md @@ -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 From 7dedc74950d04189e3318c0cd1fbd2b1dd8c9949 Mon Sep 17 00:00:00 2001 From: broksy Date: Thu, 10 Oct 2024 15:26:39 +0300 Subject: [PATCH 17/20] fix: syntax for proxy middleware backend yaml --- packages/ui5-config/src/ui5config.ts | 4 ++-- packages/ui5-config/test/__snapshots__/index.test.ts.snap | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/ui5-config/src/ui5config.ts b/packages/ui5-config/src/ui5config.ts index c83fc521f4..0775b1e3a2 100644 --- a/packages/ui5-config/src/ui5config.ts +++ b/packages/ui5-config/src/ui5config.ts @@ -294,12 +294,12 @@ export class UI5Config { } } else { // Create new 'backend' node in yaml for middleware config - backendNode = this.document.createNode({ value: backend, comments }); + backendNode = this.document.createNode({ value: [backend], comments }); } if (backendNode) { this.document .getMap({ start: proxyMiddleware as YAMLMap, path: 'configuration' }) - .set('backend', [backendNode]); + .set('backend', backendNode); } return this; } diff --git a/packages/ui5-config/test/__snapshots__/index.test.ts.snap b/packages/ui5-config/test/__snapshots__/index.test.ts.snap index e5a1788070..72b47c1d25 100644 --- a/packages/ui5-config/test/__snapshots__/index.test.ts.snap +++ b/packages/ui5-config/test/__snapshots__/index.test.ts.snap @@ -182,10 +182,10 @@ exports[`UI5Config addBackendToFioriToolsProxydMiddleware add proxy with backend configuration: ignoreCertError: false # If set to true, certificate errors will be ignored. E.g. self-signed certificates will be accepted backend: - - - url: http://different.host:8080 - path: /~testpath~ - - url: http://localhost:8080 - path: /~testpath~ + - url: http://different.host:8080 + path: /~testpath~ + - url: http://localhost:8080 + path: /~testpath~ ui5: path: - /resources From df5dff0d63f92841efd14ff618ac6b2c6d55eaeb Mon Sep 17 00:00:00 2001 From: broksy Date: Thu, 10 Oct 2024 15:36:48 +0300 Subject: [PATCH 18/20] fix: duplicated middlewares in local yaml --- packages/odata-service-writer/src/index.ts | 4 ++-- .../test/__snapshots__/index.test.ts.snap | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/odata-service-writer/src/index.ts b/packages/odata-service-writer/src/index.ts index 12c886f99c..bfc81b7cd1 100644 --- a/packages/odata-service-writer/src/index.ts +++ b/packages/odata-service-writer/src/index.ts @@ -139,9 +139,9 @@ async function generate(basePath: string, service: OdataService, fs?: Editor): P ui5MockYamlConfig: { path: service.path } }; await generateMockserverConfig(basePath, config, fs); - // add mockserver middleware to ui5-local.yaml + // add or update mockserver middleware to ui5-local.yaml if (ui5LocalConfig) { - ui5LocalConfig.addMockServerMiddleware(service.path); + ui5LocalConfig.updateMockServerMiddleware(service.path); } } diff --git a/packages/odata-service-writer/test/__snapshots__/index.test.ts.snap b/packages/odata-service-writer/test/__snapshots__/index.test.ts.snap index d6a5e4c381..044c78bf58 100644 --- a/packages/odata-service-writer/test/__snapshots__/index.test.ts.snap +++ b/packages/odata-service-writer/test/__snapshots__/index.test.ts.snap @@ -37,8 +37,8 @@ Object { mountPath: / services: - urlPath: /sap/odata/testme - metadataPath: ./webapp/localService/metadata.xml - mockdataPath: ./webapp/localService/data + metadataPath: metadataPath + mockdataPath: mockdataPath generateMockData: true annotations: [] ", @@ -2817,8 +2817,8 @@ Object { mountPath: / services: - urlPath: /sap/odata/testme - metadataPath: ./webapp/localService/metadata.xml - mockdataPath: ./webapp/localService/data + metadataPath: metadataPath + mockdataPath: mockdataPath generateMockData: true annotations: [] ", From 9a3a0437ff74b2e1661ef79981dcabef23e65c3e Mon Sep 17 00:00:00 2001 From: broksy Date: Thu, 10 Oct 2024 16:27:32 +0300 Subject: [PATCH 19/20] fix: add to backend array case --- packages/ui5-config/src/ui5config.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/ui5-config/src/ui5config.ts b/packages/ui5-config/src/ui5config.ts index 0775b1e3a2..5fc260226c 100644 --- a/packages/ui5-config/src/ui5config.ts +++ b/packages/ui5-config/src/ui5config.ts @@ -283,23 +283,27 @@ export class UI5Config { let backendNode; const proxyMiddlewareYamlContent = this.findCustomMiddleware(fioriToolsProxy); const proxyMiddlewareConfig = proxyMiddlewareYamlContent?.configuration as FioriToolsProxyConfig; - // Avoid adding duplicates by checking existing backend configs + // Add new entry to existing backend configurations in yaml if (proxyMiddlewareConfig?.backend) { + // Avoid adding duplicates by checking existing backend configs if (!proxyMiddlewareConfig.backend.find((existingBackend) => existingBackend.url === backend.url)) { - // Create new 'backend' node in yaml for middleware config keeping previous backend definitions backendNode = this.document.createNode({ - value: [...proxyMiddlewareConfig.backend, backend], + value: backend, comments }); + const configuration = this.document.getMap({ + start: proxyMiddleware as YAMLMap, + path: 'configuration' + }); + const backendConfigs = this.document.getSequence({ start: configuration, path: 'backend' }); + backendConfigs.add(backendNode); } } else { - // Create new 'backend' node in yaml for middleware config - backendNode = this.document.createNode({ value: [backend], comments }); - } - if (backendNode) { + // Create a new 'backend' node in yaml for middleware config + backendNode = this.document.createNode({ value: backend, comments }); this.document .getMap({ start: proxyMiddleware as YAMLMap, path: 'configuration' }) - .set('backend', backendNode); + .set('backend', [backendNode]); } return this; } From 72fb9a85590ef69180e4fd1ee1d972a8f6b7d96c Mon Sep 17 00:00:00 2001 From: broksy Date: Fri, 11 Oct 2024 13:16:15 +0300 Subject: [PATCH 20/20] fix: method for update and enhance of mockserver config --- .../src/mockserver-config/ui5-mock-yaml.ts | 6 +- .../mockserver-config/ui5-mock-yaml.test.ts | 2 +- packages/odata-service-writer/src/index.ts | 2 +- .../test/__snapshots__/index.test.ts.snap | 8 +- packages/ui5-config/src/middlewares.ts | 2 +- packages/ui5-config/src/ui5config.ts | 45 +++-- .../test/__snapshots__/index.test.ts.snap | 158 +++++++++--------- packages/ui5-config/test/index.test.ts | 103 ++++++------ 8 files changed, 160 insertions(+), 166 deletions(-) diff --git a/packages/mockserver-config-writer/src/mockserver-config/ui5-mock-yaml.ts b/packages/mockserver-config-writer/src/mockserver-config/ui5-mock-yaml.ts index dada12e30e..0dddf43a19 100644 --- a/packages/mockserver-config-writer/src/mockserver-config/ui5-mock-yaml.ts +++ b/packages/mockserver-config-writer/src/mockserver-config/ui5-mock-yaml.ts @@ -78,7 +78,7 @@ async function updateUi5MockYamlConfig( if (overwrite) { existingUi5MockYamlConfig.updateCustomMiddleware(await getNewMockserverMiddleware(path, annotationsConfig)); } else { - existingUi5MockYamlConfig.updateMockServerMiddleware(path, annotationsConfig); + existingUi5MockYamlConfig.enhanceMockServerMiddleware(path, annotationsConfig); } return existingUi5MockYamlConfig; } @@ -123,7 +123,7 @@ async function generateNewUi5MockYamlConfig( ui5MockYaml.setType('application'); ui5MockYaml.addFioriToolsProxydMiddleware({ ui5: {} }); ui5MockYaml.addFioriToolsAppReloadMiddleware(); - ui5MockYaml.addMockServerMiddleware(path, annotationsConfig); + ui5MockYaml.enhanceMockServerMiddleware(path, annotationsConfig); return ui5MockYaml; } @@ -139,7 +139,7 @@ async function getNewMockserverMiddleware( annotationsConfig?: MockserverConfig['annotations'] ): Promise> { 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'); diff --git a/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts b/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts index 90d6eb390b..1574dae0dd 100644 --- a/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts +++ b/packages/mockserver-config-writer/test/unit/mockserver-config/ui5-mock-yaml.test.ts @@ -122,7 +122,7 @@ describe('Test enhanceYaml()', () => { test(`Should throw error in case new added middleware can't be found by name 'sap-fe-mockserver'`, async () => { jest.spyOn(UI5Config, 'newInstance').mockResolvedValue({ - addMockServerMiddleware: jest.fn(), + enhanceMockServerMiddleware: jest.fn(), findCustomMiddleware: () => undefined } as unknown as UI5Config); const fs = getFsWithUi5MockYaml('{}'); diff --git a/packages/odata-service-writer/src/index.ts b/packages/odata-service-writer/src/index.ts index bfc81b7cd1..445c348714 100644 --- a/packages/odata-service-writer/src/index.ts +++ b/packages/odata-service-writer/src/index.ts @@ -141,7 +141,7 @@ async function generate(basePath: string, service: OdataService, fs?: Editor): P await generateMockserverConfig(basePath, config, fs); // add or update mockserver middleware to ui5-local.yaml if (ui5LocalConfig) { - ui5LocalConfig.updateMockServerMiddleware(service.path); + ui5LocalConfig.enhanceMockServerMiddleware(service.path); } } diff --git a/packages/odata-service-writer/test/__snapshots__/index.test.ts.snap b/packages/odata-service-writer/test/__snapshots__/index.test.ts.snap index 044c78bf58..d6a5e4c381 100644 --- a/packages/odata-service-writer/test/__snapshots__/index.test.ts.snap +++ b/packages/odata-service-writer/test/__snapshots__/index.test.ts.snap @@ -37,8 +37,8 @@ Object { mountPath: / services: - urlPath: /sap/odata/testme - metadataPath: metadataPath - mockdataPath: mockdataPath + metadataPath: ./webapp/localService/metadata.xml + mockdataPath: ./webapp/localService/data generateMockData: true annotations: [] ", @@ -2817,8 +2817,8 @@ Object { mountPath: / services: - urlPath: /sap/odata/testme - metadataPath: metadataPath - mockdataPath: mockdataPath + metadataPath: ./webapp/localService/metadata.xml + mockdataPath: ./webapp/localService/data generateMockData: true annotations: [] ", diff --git a/packages/ui5-config/src/middlewares.ts b/packages/ui5-config/src/middlewares.ts index e349916aff..3359b8b226 100644 --- a/packages/ui5-config/src/middlewares.ts +++ b/packages/ui5-config/src/middlewares.ts @@ -168,7 +168,7 @@ const handleServicesForMiddlewareConfig = ( }; export const getMockServerMiddlewareConfig = ( - services: MockserverConfig['services'], + services: MockserverConfig['services'] = [], path?: string, annotationsConfig: MockserverConfig['annotations'] = [], forceAddService = false diff --git a/packages/ui5-config/src/ui5config.ts b/packages/ui5-config/src/ui5config.ts index 5fc260226c..25e9582ed6 100644 --- a/packages/ui5-config/src/ui5config.ts +++ b/packages/ui5-config/src/ui5config.ts @@ -351,39 +351,32 @@ export class UI5Config { } /** - * Adds a instance of the mockserver middleware to the config. + * Enhances mockserver middleware by adding a new instance of the mockserver middleware to the config or updating existing one. * * @param path option path that is to be mocked * @param annotationsConfig optional annotations config that is to be mocked * @returns {UI5Config} the UI5Config instance * @memberof UI5Config */ - public addMockServerMiddleware(path?: string, annotationsConfig?: MockserverConfig['annotations']): this { - const middleware = getMockServerMiddlewareConfig(undefined, path, annotationsConfig, true); - this.document.appendTo({ - path: 'server.customMiddleware', - value: middleware - }); - return this; - } - - /** - * Updates a instance of the mockserver middleware of the config. - * - * @param path option path that is to be mocked - * @param annotationsConfig optional, annotations config that is to be mocked - * @returns {UI5Config} the UI5Config instance - * @memberof UI5Config - */ - public updateMockServerMiddleware(path?: string, annotationsConfig?: MockserverConfig['annotations']): this { + public enhanceMockServerMiddleware(path?: string, annotationsConfig?: MockserverConfig['annotations']): this { const customMockserverMiddleware = this.findCustomMiddleware('sap-fe-mockserver'); - const customMockserverMiddlewareConfig = customMockserverMiddleware?.configuration as MockserverConfig; - const middleware = getMockServerMiddlewareConfig( - customMockserverMiddlewareConfig?.services, - path, - annotationsConfig - ); - this.updateCustomMiddleware(middleware); + // Update existing + if (customMockserverMiddleware) { + const customMockserverMiddlewareConfig = customMockserverMiddleware?.configuration as MockserverConfig; + const middleware = getMockServerMiddlewareConfig( + customMockserverMiddlewareConfig?.services, + path, + annotationsConfig + ); + this.updateCustomMiddleware(middleware); + } else { + // Create a new middleware instance + const middleware = getMockServerMiddlewareConfig([], path, annotationsConfig, true); + this.document.appendTo({ + path: 'server.customMiddleware', + value: middleware + }); + } return this; } diff --git a/packages/ui5-config/test/__snapshots__/index.test.ts.snap b/packages/ui5-config/test/__snapshots__/index.test.ts.snap index 72b47c1d25..ecf9247d55 100644 --- a/packages/ui5-config/test/__snapshots__/index.test.ts.snap +++ b/packages/ui5-config/test/__snapshots__/index.test.ts.snap @@ -355,56 +355,6 @@ exports[`UI5Config addFioriToolsProxydMiddleware add without backend or but all " `; -exports[`UI5Config addMockServerMiddleware add with given path 1`] = ` -"server: - customMiddleware: - - name: sap-fe-mockserver - beforeMiddleware: csp - configuration: - mountPath: / - services: - - urlPath: /~testpath~ - metadataPath: ./webapp/localService/metadata.xml - mockdataPath: ./webapp/localService/data - generateMockData: true - annotations: [] -" -`; - -exports[`UI5Config addMockServerMiddleware add with path and annotationsConfig 1`] = ` -"server: - customMiddleware: - - name: sap-fe-mockserver - beforeMiddleware: csp - configuration: - mountPath: / - services: - - urlPath: /~testpath~ - metadataPath: ./webapp/localService/metadata.xml - mockdataPath: ./webapp/localService/data - generateMockData: true - annotations: - - localPath: ./webapp/annotations/annotations.xml - urlPath: annotations.xml -" -`; - -exports[`UI5Config addMockServerMiddleware add without path 1`] = ` -"server: - customMiddleware: - - name: sap-fe-mockserver - beforeMiddleware: csp - configuration: - mountPath: / - services: - - urlPath: '' - metadataPath: ./webapp/localService/metadata.xml - mockdataPath: ./webapp/localService/data - generateMockData: true - annotations: [] -" -`; - exports[`UI5Config addServeStaticConfig add with multiple paths (existing config) 1`] = ` "server: customMiddleware: @@ -563,42 +513,57 @@ exports[`UI5Config addUi5ToFioriToolsProxydMiddleware add ui5 config to empty to " `; -exports[`UI5Config getAppReloadMiddlewareConfig 1`] = ` +exports[`UI5Config enhanceMockServerMiddleware add add with given path 1`] = ` "server: customMiddleware: - - name: fiori-tools-appreload - afterMiddleware: compression + - name: sap-fe-mockserver + beforeMiddleware: csp configuration: - port: 35729 - path: webapp - delay: 300 -" -`; - -exports[`UI5Config getType / setType replace type 1`] = ` -"type: library -" -`; - -exports[`UI5Config getType / setType set type 1`] = ` -"type: application + mountPath: / + services: + - urlPath: /~testpath~ + metadataPath: ./webapp/localService/metadata.xml + mockdataPath: ./webapp/localService/data + generateMockData: true + annotations: [] " `; -exports[`UI5Config setMetadata replace metadata 1`] = ` -"metadata: - name: the.replaced.name +exports[`UI5Config enhanceMockServerMiddleware add add with path and annotationsConfig 1`] = ` +"server: + customMiddleware: + - name: sap-fe-mockserver + beforeMiddleware: csp + configuration: + mountPath: / + services: + - urlPath: /~testpath~ + metadataPath: ./webapp/localService/metadata.xml + mockdataPath: ./webapp/localService/data + generateMockData: true + annotations: + - localPath: ./webapp/annotations/annotations.xml + urlPath: annotations.xml " `; -exports[`UI5Config setMetadata set name and copyright 1`] = ` -"metadata: - name: test.name - copyright: © +exports[`UI5Config enhanceMockServerMiddleware add add without path 1`] = ` +"server: + customMiddleware: + - name: sap-fe-mockserver + beforeMiddleware: csp + configuration: + mountPath: / + services: + - urlPath: '' + metadataPath: ./webapp/localService/metadata.xml + mockdataPath: ./webapp/localService/data + generateMockData: true + annotations: [] " `; -exports[`UI5Config updateMockServerMiddleware update with given path (existing services) 1`] = ` +exports[`UI5Config enhanceMockServerMiddleware update update with given path (existing services) 1`] = ` "server: customMiddleware: - name: sap-fe-mockserver @@ -617,7 +582,7 @@ exports[`UI5Config updateMockServerMiddleware update with given path (existing s " `; -exports[`UI5Config updateMockServerMiddleware update with given path (no existing services) 1`] = ` +exports[`UI5Config enhanceMockServerMiddleware update update with given path (no existing services) 1`] = ` "server: customMiddleware: - name: sap-fe-mockserver @@ -626,14 +591,14 @@ exports[`UI5Config updateMockServerMiddleware update with given path (no existin mountPath: / services: - urlPath: /~testpath~ - metadataPath: metadataPath - mockdataPath: mockdataPath + metadataPath: ./webapp/localService/metadata.xml + mockdataPath: ./webapp/localService/data generateMockData: true annotations: [] " `; -exports[`UI5Config updateMockServerMiddleware update with given path and annotationsConfig (existing services) 1`] = ` +exports[`UI5Config enhanceMockServerMiddleware update update with given path and annotationsConfig (existing services) 1`] = ` "server: customMiddleware: - name: sap-fe-mockserver @@ -654,7 +619,7 @@ exports[`UI5Config updateMockServerMiddleware update with given path and annotat " `; -exports[`UI5Config updateMockServerMiddleware update without path (existing services) 1`] = ` +exports[`UI5Config enhanceMockServerMiddleware update update without path (existing services) 1`] = ` "server: customMiddleware: - name: sap-fe-mockserver @@ -672,3 +637,38 @@ exports[`UI5Config updateMockServerMiddleware update without path (existing serv annotations: [] " `; + +exports[`UI5Config getAppReloadMiddlewareConfig 1`] = ` +"server: + customMiddleware: + - name: fiori-tools-appreload + afterMiddleware: compression + configuration: + port: 35729 + path: webapp + delay: 300 +" +`; + +exports[`UI5Config getType / setType replace type 1`] = ` +"type: library +" +`; + +exports[`UI5Config getType / setType set type 1`] = ` +"type: application +" +`; + +exports[`UI5Config setMetadata replace metadata 1`] = ` +"metadata: + name: the.replaced.name +" +`; + +exports[`UI5Config setMetadata set name and copyright 1`] = ` +"metadata: + name: test.name + copyright: © +" +`; diff --git a/packages/ui5-config/test/index.test.ts b/packages/ui5-config/test/index.test.ts index 4e64c9df1c..21dcd6995c 100644 --- a/packages/ui5-config/test/index.test.ts +++ b/packages/ui5-config/test/index.test.ts @@ -287,63 +287,64 @@ describe('UI5Config', () => { }); }); - describe('addMockServerMiddleware', () => { - test('add with given path', () => { - ui5Config.addMockServerMiddleware(path); - expect(ui5Config.toString()).toMatchSnapshot(); - }); - - test('add without path', () => { - ui5Config.addMockServerMiddleware(); - expect(ui5Config.toString()).toMatchSnapshot(); - }); - test('add with path and annotationsConfig', () => { - ui5Config.addMockServerMiddleware(path, annotationsConfig); - expect(ui5Config.toString()).toMatchSnapshot(); + describe('enhanceMockServerMiddleware', () => { + describe('add', () => { + test('with given path', () => { + ui5Config.enhanceMockServerMiddleware(path); + expect(ui5Config.toString()).toMatchSnapshot(); + }); + test('without path', () => { + ui5Config.enhanceMockServerMiddleware(); + expect(ui5Config.toString()).toMatchSnapshot(); + }); + test('with path and annotationsConfig', () => { + ui5Config.enhanceMockServerMiddleware(path, annotationsConfig); + expect(ui5Config.toString()).toMatchSnapshot(); + }); }); - }); - - describe('updateMockServerMiddleware', () => { - const MOCKSERVER_MIDDLEWARE_NAME = 'sap-fe-mockserver'; - const mockserverMiddlewareConfig: MockserverConfig = { - mountPath: '/', - services: [ - { - urlPath: '/~different-testpath~', - metadataPath: 'different-metadataPath', - generateMockData: true - } - ] - }; - const customMockserverMiddleware = { - name: MOCKSERVER_MIDDLEWARE_NAME, - beforeMiddleware: 'csp', - configuration: mockserverMiddlewareConfig - }; + describe('update', () => { + const MOCKSERVER_MIDDLEWARE_NAME = 'sap-fe-mockserver'; + const mockserverMiddlewareConfig: MockserverConfig = { + mountPath: '/', + services: [ + { + urlPath: '/~different-testpath~', + metadataPath: 'different-metadataPath', + generateMockData: true + } + ] + }; + const customMockserverMiddleware = { + name: MOCKSERVER_MIDDLEWARE_NAME, + beforeMiddleware: 'csp', + configuration: mockserverMiddlewareConfig + }; - beforeEach(() => { - ui5Config.addCustomMiddleware([customMockserverMiddleware]); - }); + beforeEach(() => { + // Make sure middleware already exists to trigger the update + ui5Config.addCustomMiddleware([customMockserverMiddleware]); + }); - test('update with given path (no existing services)', () => { - ui5Config.removeCustomMiddleware(MOCKSERVER_MIDDLEWARE_NAME); - ui5Config.updateMockServerMiddleware(path); - expect(ui5Config.toString()).toMatchSnapshot(); - }); + test('with given path (no existing services)', () => { + ui5Config.removeCustomMiddleware(MOCKSERVER_MIDDLEWARE_NAME); + ui5Config.enhanceMockServerMiddleware(path); + expect(ui5Config.toString()).toMatchSnapshot(); + }); - test('update with given path (existing services)', () => { - ui5Config.updateMockServerMiddleware(path); - expect(ui5Config.toString()).toMatchSnapshot(); - }); + test('with given path (existing services)', () => { + ui5Config.enhanceMockServerMiddleware(path); + expect(ui5Config.toString()).toMatchSnapshot(); + }); - test('update without path (existing services)', () => { - ui5Config.updateMockServerMiddleware(); - expect(ui5Config.toString()).toMatchSnapshot(); - }); + test('without path (existing services)', () => { + ui5Config.enhanceMockServerMiddleware(); + expect(ui5Config.toString()).toMatchSnapshot(); + }); - test('update with given path and annotationsConfig (existing services)', () => { - ui5Config.updateMockServerMiddleware(path, annotationsConfig); - expect(ui5Config.toString()).toMatchSnapshot(); + test('with given path and annotationsConfig (existing services)', () => { + ui5Config.enhanceMockServerMiddleware(path, annotationsConfig); + expect(ui5Config.toString()).toMatchSnapshot(); + }); }); });