Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 41 additions & 15 deletions tools/spectral/ipa/__tests__/utils/extensions.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { describe, it, expect, toBe } from '@jest/globals';
import { describe, it, expect } from '@jest/globals';
import {
hasMethodWithVerbOverride,
hasCustomMethodOverride,
hasMethodVerbOverride,
hasOperationIdOverride,
getOperationIdOverride,
} from '../../rulesets/functions/utils/extensions';

const methodWithExtension = {
Expand All @@ -29,20 +30,23 @@ const endpointWithNoMethodExtension = {
exception: true,
};

describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
describe('hasMethodWithVerbOverride', () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored custom method rule approach to apply to each operation instead of each path in the OAS.

This method not needed, removed test

it('returns true if endpoint has method with the extension', () => {
expect(hasMethodWithVerbOverride(endpointWithMethodExtension)).toBe(true);
});
it('returns false if object does not a method with the extension', () => {
expect(hasMethodWithVerbOverride(endpointWithNoMethodExtension)).toBe(false);
});
});
});
const operationWithOperationIdOverride = {
operationId: 'operationId',
'x-xgen-operation-id-override': 'customOperationId',
};

const operationWithEmptyOperationIdOverride = {
operationId: 'operationId',
'x-xgen-operation-id-override': '',
};

const operationWithNoOperationIdOverride = {
operationId: 'operationId',
};

describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
describe('hasCustomMethodOverride', () => {
it('returns true if the method has the extension with the cusotmMethod boolean set to true', () => {
it('returns true if the method has the extension with the customMethod boolean set to true', () => {
expect(hasCustomMethodOverride(customMethod)).toBe(true);
});
it('returns false if the method does not have the extension', () => {
Expand All @@ -52,9 +56,7 @@ describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
expect(hasCustomMethodOverride(methodWithExtension)).toBe(false);
});
});
});

describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
describe('hasMethodVerbOverride', () => {
it('returns true if the method has the extension with the expected verb', () => {
expect(hasMethodVerbOverride(methodWithExtension, 'get')).toBe(true);
Expand All @@ -66,4 +68,28 @@ describe('tools/spectral/ipa/rulesets/functions/utils/extensions.js', () => {
expect(hasMethodVerbOverride(methodWithExtension, 'put')).toBe(false);
});
});

describe('hasOperationIdOverride', () => {
it('returns true if the method has the extension', () => {
expect(hasOperationIdOverride(operationWithOperationIdOverride)).toBe(true);
});
it('returns true if the method has the extension but with an empty value', () => {
expect(hasOperationIdOverride(operationWithEmptyOperationIdOverride)).toBe(true);
});
it('returns false if the method does not have the extension', () => {
expect(hasOperationIdOverride(operationWithNoOperationIdOverride)).toBe(false);
});
});

describe('getOperationIdOverride', () => {
it('returns the value if the method has the extension', () => {
expect(getOperationIdOverride(operationWithOperationIdOverride)).toBe('customOperationId');
});
it('returns an empty value if the method has the extension with an empty value', () => {
expect(getOperationIdOverride(operationWithEmptyOperationIdOverride)).toBe('');
});
it('returns undefined if the method does not have the extension', () => {
expect(getOperationIdOverride(operationWithNoOperationIdOverride)).toBe(undefined);
});
});
});
102 changes: 69 additions & 33 deletions tools/spectral/ipa/__tests__/utils/operationIdGeneration.test.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,79 @@
import { describe, expect, it } from '@jest/globals';
import { generateOperationID } from '../../rulesets/functions/utils/operationIdGeneration';
import {
generateOperationID,
numberOfWords,
shortenOperationId,
} from '../../rulesets/functions/utils/operationIdGeneration';

describe('tools/spectral/ipa/utils/operationIdGeneration.js', () => {
it('should singularize all nouns', () => {
expect(generateOperationID('create', '/groups/{groupId}/clusters')).toEqual('createGroupCluster');
expect(generateOperationID('delete', '/groups/{groupId}/clusters/{clusterName}')).toEqual('deleteGroupCluster');
expect(generateOperationID('get', '/groups/{groupId}/clusters/{clusterName}')).toEqual('getGroupCluster');
expect(generateOperationID('update', '/groups/{groupId}/clusters/{clusterName}')).toEqual('updateGroupCluster');
expect(generateOperationID('pause', '/groups/{groupId}/clusters/{clusterName}')).toEqual('pauseGroupCluster');
});
describe('generateOperationID', () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a describe to wrap these as part of generateOperationID function

it('should singularize all nouns', () => {
expect(generateOperationID('create', '/groups/{groupId}/clusters')).toEqual('createGroupCluster');
expect(generateOperationID('delete', '/groups/{groupId}/clusters/{clusterName}')).toEqual('deleteGroupCluster');
expect(generateOperationID('get', '/groups/{groupId}/clusters/{clusterName}')).toEqual('getGroupCluster');
expect(generateOperationID('update', '/groups/{groupId}/clusters/{clusterName}')).toEqual('updateGroupCluster');
expect(generateOperationID('pause', '/groups/{groupId}/clusters/{clusterName}')).toEqual('pauseGroupCluster');
});

it('should leave the final noun as is', () => {
expect(generateOperationID('list', '/groups/{groupId}/clusters')).toEqual('listGroupClusters');
expect(generateOperationID('get', '/groups/{groupId}/settings')).toEqual('getGroupSettings');
expect(generateOperationID('update', '/groups/{groupId}/settings')).toEqual('updateGroupSettings');
expect(generateOperationID('search', '/groups/{groupId}/clusters')).toEqual('searchGroupClusters');
expect(
generateOperationID(
'get',
'/groups/{groupId}/clusters/{clusterName}/{clusterView}/{databaseName}/{collectionName}/collStats/measurements'
)
).toEqual('getGroupClusterCollStatMeasurements');
expect(generateOperationID('grant', '/api/atlas/v2/groups/{groupId}/access')).toEqual('grantGroupAccess');
});

it('should split camelCase method names', () => {
expect(generateOperationID('addNode', '/groups/{groupId}/clusters/{clusterName}')).toEqual('addGroupClusterNode');
expect(generateOperationID('get', '/api/atlas/v2/groups/byName/{groupName}')).toEqual('getGroupByName');
expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/backup/exportBuckets/{exportBucketId}')).toEqual(
'exportGroupBackupBuckets'
);
});

it('should leave the final noun as is', () => {
expect(generateOperationID('list', '/groups/{groupId}/clusters')).toEqual('listGroupClusters');
expect(generateOperationID('get', '/groups/{groupId}/settings')).toEqual('getGroupSettings');
expect(generateOperationID('update', '/groups/{groupId}/settings')).toEqual('updateGroupSettings');
expect(generateOperationID('search', '/groups/{groupId}/clusters')).toEqual('searchGroupClusters');
expect(
generateOperationID(
'get',
'/groups/{groupId}/clusters/{clusterName}/{clusterView}/{databaseName}/{collectionName}/collStats/measurements'
)
).toEqual('getGroupClusterCollStatMeasurements');
expect(generateOperationID('grant', '/api/atlas/v2/groups/{groupId}/access')).toEqual('grantGroupAccess');
it('should accommodate legacy custom methods', () => {
expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/restartPrimaries')).toEqual(
'restartGroupClusterPrimaries'
);
expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/pipelines/{pipelineName}/pause')).toEqual(
'pauseGroupPipeline'
);
});

it('should return method when path is empty', () => {
expect(generateOperationID('get', '')).toEqual('get');
expect(generateOperationID('getInfo', '')).toEqual('getInfo');
});
});

it('should split camelCase method names', () => {
expect(generateOperationID('addNode', '/groups/{groupId}/clusters/{clusterName}')).toEqual('addGroupClusterNode');
expect(generateOperationID('get', '/api/atlas/v2/groups/byName/{groupName}')).toEqual('getGroupByName');
expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/backup/exportBuckets/{exportBucketId}')).toEqual(
'exportGroupBackupBuckets'
);
describe('numberOfWords', () => {
it('should count the number of words in a camelCase string', () => {
expect(numberOfWords('create')).toEqual(1);
expect(numberOfWords('createGroup')).toEqual(2);
expect(numberOfWords('createGroupCluster')).toEqual(3);
expect(numberOfWords('createGroupClusterIndex')).toEqual(4);
expect(numberOfWords('')).toEqual(0);
});
});

it('should accommodate legacy custom methods', () => {
expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/restartPrimaries')).toEqual(
'restartGroupClusterPrimaries'
);
expect(generateOperationID('', '/api/atlas/v2/groups/{groupId}/pipelines/{pipelineName}/pause')).toEqual(
'pauseGroupPipeline'
);
describe('shortenOperationId', () => {
it('should shorten operation IDs correctly', () => {
expect(shortenOperationId('createGroupClusterAutoScalingConfiguration')).toEqual(
'createAutoScalingConfiguration'
);
expect(shortenOperationId('getFederationSettingConnectedOrgConfigRoleMapping')).toEqual('getConfigRoleMapping');
});

it('should make no change if the operation ID is <= 4 words long or undefined', () => {
expect(shortenOperationId('createGroupClusterIndex')).toEqual('createGroupClusterIndex');
expect(shortenOperationId('create')).toEqual('create');
expect(shortenOperationId('')).toEqual('');
});
});
});
Loading
Loading