Skip to content

Commit 1904ad8

Browse files
CLOUDP-331821: Add op ID override validation
1 parent 975c7b2 commit 1904ad8

12 files changed

+768
-173
lines changed

tools/spectral/ipa/__tests__/utils/extensions.test.js

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { describe, it, expect, toBe } from '@jest/globals';
1+
import { describe, it, expect } from '@jest/globals';
22
import {
3-
hasMethodWithVerbOverride,
43
hasCustomMethodOverride,
54
hasMethodVerbOverride,
5+
hasOperationIdOverride,
6+
getOperationIdOverride,
67
} from '../../rulesets/functions/utils/extensions';
78

89
const methodWithExtension = {
@@ -29,20 +30,23 @@ const endpointWithNoMethodExtension = {
2930
exception: true,
3031
};
3132

32-
describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
33-
describe('hasMethodWithVerbOverride', () => {
34-
it('returns true if endpoint has method with the extension', () => {
35-
expect(hasMethodWithVerbOverride(endpointWithMethodExtension)).toBe(true);
36-
});
37-
it('returns false if object does not a method with the extension', () => {
38-
expect(hasMethodWithVerbOverride(endpointWithNoMethodExtension)).toBe(false);
39-
});
40-
});
41-
});
33+
const operationWithOperationIdOverride = {
34+
operationId: 'operationId',
35+
'x-xgen-operation-id-override': 'customOperationId',
36+
};
37+
38+
const operationWithEmptyOperationIdOverride = {
39+
operationId: 'operationId',
40+
'x-xgen-operation-id-override': '',
41+
};
42+
43+
const operationWithNoOperationIdOverride = {
44+
operationId: 'operationId',
45+
};
4246

4347
describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
4448
describe('hasCustomMethodOverride', () => {
45-
it('returns true if the method has the extension with the cusotmMethod boolean set to true', () => {
49+
it('returns true if the method has the extension with the customMethod boolean set to true', () => {
4650
expect(hasCustomMethodOverride(customMethod)).toBe(true);
4751
});
4852
it('returns false if the method does not have the extension', () => {
@@ -52,9 +56,7 @@ describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
5256
expect(hasCustomMethodOverride(methodWithExtension)).toBe(false);
5357
});
5458
});
55-
});
5659

57-
describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
5860
describe('hasMethodVerbOverride', () => {
5961
it('returns true if the method has the extension with the expected verb', () => {
6062
expect(hasMethodVerbOverride(methodWithExtension, 'get')).toBe(true);
@@ -66,4 +68,28 @@ describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
6668
expect(hasMethodVerbOverride(methodWithExtension, 'put')).toBe(false);
6769
});
6870
});
71+
72+
describe('hasOperationIdOverride', () => {
73+
it('returns true if the method has the extension', () => {
74+
expect(hasOperationIdOverride(operationWithOperationIdOverride)).toBe(true);
75+
});
76+
it('returns true if the method has the extension but with an empty value', () => {
77+
expect(hasOperationIdOverride(operationWithEmptyOperationIdOverride)).toBe(true);
78+
});
79+
it('returns false if the method does not have the extension', () => {
80+
expect(hasOperationIdOverride(operationWithNoOperationIdOverride)).toBe(false);
81+
});
82+
});
83+
84+
describe('getOperationIdOverride', () => {
85+
it('returns the value if the method has the extension', () => {
86+
expect(getOperationIdOverride(operationWithOperationIdOverride)).toBe('customOperationId');
87+
});
88+
it('returns an empty value if the method has the extension with an empty value', () => {
89+
expect(getOperationIdOverride(operationWithEmptyOperationIdOverride)).toBe('');
90+
});
91+
it('returns undefined if the method does not have the extension', () => {
92+
expect(getOperationIdOverride(operationWithNoOperationIdOverride)).toBe(undefined);
93+
});
94+
});
6995
});
Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,79 @@
11
import { describe, expect, it } from '@jest/globals';
2-
import { generateOperationID } from '../../rulesets/functions/utils/operationIdGeneration';
2+
import {
3+
generateOperationID,
4+
numberOfWords,
5+
shortenOperationId,
6+
} from '../../rulesets/functions/utils/operationIdGeneration';
37

48
describe('tools/spectral/ipa/utils/operationIdGeneration.js', () => {
5-
it('should singularize all nouns', () => {
6-
expect(generateOperationID('create', '/groups/{groupId}/clusters')).toEqual('createGroupCluster');
7-
expect(generateOperationID('delete', '/groups/{groupId}/clusters/{clusterName}')).toEqual('deleteGroupCluster');
8-
expect(generateOperationID('get', '/groups/{groupId}/clusters/{clusterName}')).toEqual('getGroupCluster');
9-
expect(generateOperationID('update', '/groups/{groupId}/clusters/{clusterName}')).toEqual('updateGroupCluster');
10-
expect(generateOperationID('pause', '/groups/{groupId}/clusters/{clusterName}')).toEqual('pauseGroupCluster');
11-
});
9+
describe('generateOperationID', () => {
10+
it('should singularize all nouns', () => {
11+
expect(generateOperationID('create', '/groups/{groupId}/clusters')).toEqual('createGroupCluster');
12+
expect(generateOperationID('delete', '/groups/{groupId}/clusters/{clusterName}')).toEqual('deleteGroupCluster');
13+
expect(generateOperationID('get', '/groups/{groupId}/clusters/{clusterName}')).toEqual('getGroupCluster');
14+
expect(generateOperationID('update', '/groups/{groupId}/clusters/{clusterName}')).toEqual('updateGroupCluster');
15+
expect(generateOperationID('pause', '/groups/{groupId}/clusters/{clusterName}')).toEqual('pauseGroupCluster');
16+
});
17+
18+
it('should leave the final noun as is', () => {
19+
expect(generateOperationID('list', '/groups/{groupId}/clusters')).toEqual('listGroupClusters');
20+
expect(generateOperationID('get', '/groups/{groupId}/settings')).toEqual('getGroupSettings');
21+
expect(generateOperationID('update', '/groups/{groupId}/settings')).toEqual('updateGroupSettings');
22+
expect(generateOperationID('search', '/groups/{groupId}/clusters')).toEqual('searchGroupClusters');
23+
expect(
24+
generateOperationID(
25+
'get',
26+
'/groups/{groupId}/clusters/{clusterName}/{clusterView}/{databaseName}/{collectionName}/collStats/measurements'
27+
)
28+
).toEqual('getGroupClusterCollStatMeasurements');
29+
expect(generateOperationID('grant', '/api/atlas/v2/groups/{groupId}/access')).toEqual('grantGroupAccess');
30+
});
31+
32+
it('should split camelCase method names', () => {
33+
expect(generateOperationID('addNode', '/groups/{groupId}/clusters/{clusterName}')).toEqual('addGroupClusterNode');
34+
expect(generateOperationID('get', '/api/atlas/v2/groups/byName/{groupName}')).toEqual('getGroupByName');
35+
expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/backup/exportBuckets/{exportBucketId}')).toEqual(
36+
'exportGroupBackupBuckets'
37+
);
38+
});
1239

13-
it('should leave the final noun as is', () => {
14-
expect(generateOperationID('list', '/groups/{groupId}/clusters')).toEqual('listGroupClusters');
15-
expect(generateOperationID('get', '/groups/{groupId}/settings')).toEqual('getGroupSettings');
16-
expect(generateOperationID('update', '/groups/{groupId}/settings')).toEqual('updateGroupSettings');
17-
expect(generateOperationID('search', '/groups/{groupId}/clusters')).toEqual('searchGroupClusters');
18-
expect(
19-
generateOperationID(
20-
'get',
21-
'/groups/{groupId}/clusters/{clusterName}/{clusterView}/{databaseName}/{collectionName}/collStats/measurements'
22-
)
23-
).toEqual('getGroupClusterCollStatMeasurements');
24-
expect(generateOperationID('grant', '/api/atlas/v2/groups/{groupId}/access')).toEqual('grantGroupAccess');
40+
it('should accommodate legacy custom methods', () => {
41+
expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/restartPrimaries')).toEqual(
42+
'restartGroupClusterPrimaries'
43+
);
44+
expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/pipelines/{pipelineName}/pause')).toEqual(
45+
'pauseGroupPipeline'
46+
);
47+
});
48+
49+
it('should return method when path is empty', () => {
50+
expect(generateOperationID('get', '')).toEqual('get');
51+
expect(generateOperationID('getInfo', '')).toEqual('getInfo');
52+
});
2553
});
2654

27-
it('should split camelCase method names', () => {
28-
expect(generateOperationID('addNode', '/groups/{groupId}/clusters/{clusterName}')).toEqual('addGroupClusterNode');
29-
expect(generateOperationID('get', '/api/atlas/v2/groups/byName/{groupName}')).toEqual('getGroupByName');
30-
expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/backup/exportBuckets/{exportBucketId}')).toEqual(
31-
'exportGroupBackupBuckets'
32-
);
55+
describe('numberOfWords', () => {
56+
it('should count the number of words in a camelCase string', () => {
57+
expect(numberOfWords('create')).toEqual(1);
58+
expect(numberOfWords('createGroup')).toEqual(2);
59+
expect(numberOfWords('createGroupCluster')).toEqual(3);
60+
expect(numberOfWords('createGroupClusterIndex')).toEqual(4);
61+
expect(numberOfWords('')).toEqual(0);
62+
});
3363
});
3464

35-
it('should accommodate legacy custom methods', () => {
36-
expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/restartPrimaries')).toEqual(
37-
'restartGroupClusterPrimaries'
38-
);
39-
expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/pipelines/{pipelineName}/pause')).toEqual(
40-
'pauseGroupPipeline'
41-
);
65+
describe('shortenOperationId', () => {
66+
it('should shorten operation IDs correctly', () => {
67+
expect(shortenOperationId('createGroupClusterAutoScalingConfiguration')).toEqual(
68+
'createAutoScalingConfiguration'
69+
);
70+
expect(shortenOperationId('getFederationSettingConnectedOrgConfigRoleMapping')).toEqual('getConfigRoleMapping');
71+
});
72+
73+
it('should make no change if the operation ID is <= 4 words long or undefined', () => {
74+
expect(shortenOperationId('createGroupClusterIndex')).toEqual('createGroupClusterIndex');
75+
expect(shortenOperationId('create')).toEqual('create');
76+
expect(shortenOperationId('')).toEqual('');
77+
});
4278
});
4379
});

0 commit comments

Comments
 (0)