Skip to content

Commit

Permalink
fix: method for update and enhance of mockserver config
Browse files Browse the repository at this point in the history
  • Loading branch information
broksy committed Oct 11, 2024
1 parent 9a3a043 commit 72fb9a8
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -139,7 +139,7 @@ async function getNewMockserverMiddleware(
annotationsConfig?: MockserverConfig['annotations']
): Promise<CustomMiddleware<MockserverConfig>> {
const ui5MockYaml = await UI5Config.newInstance('');
ui5MockYaml.addMockServerMiddleware(path, annotationsConfig);
ui5MockYaml.enhanceMockServerMiddleware(path, annotationsConfig);
const mockserverMiddleware = ui5MockYaml.findCustomMiddleware('sap-fe-mockserver');
if (!mockserverMiddleware) {
throw Error('Could not create new mockserver config');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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('{}');
Expand Down
2 changes: 1 addition & 1 deletion packages/odata-service-writer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
",
Expand Down Expand Up @@ -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: []
",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui5-config/src/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const handleServicesForMiddlewareConfig = (
};

export const getMockServerMiddlewareConfig = (
services: MockserverConfig['services'],
services: MockserverConfig['services'] = [],
path?: string,
annotationsConfig: MockserverConfig['annotations'] = [],
forceAddService = false
Expand Down
45 changes: 19 additions & 26 deletions packages/ui5-config/src/ui5config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
158 changes: 79 additions & 79 deletions packages/ui5-config/test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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: ©
"
`;
Loading

0 comments on commit 72fb9a8

Please sign in to comment.