Skip to content

Commit 0917b24

Browse files
authored
Merge pull request #122 from yashkohli88/yk/refactor-origins-tests
Refactor of Origins API Tests for Improved Robustness and Efficiency
2 parents 15909f0 + 3972cb6 commit 0917b24

File tree

1 file changed

+58
-37
lines changed

1 file changed

+58
-37
lines changed

tools/integration/test/integration/e2e-test-service/originsTest.js

+58-37
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
const assert = require('assert')
55
const { callFetchWithRetry: callFetch } = require('../../../lib/fetch')
6-
const { devApiBaseUrl, prodApiBaseUrl, getComponents, origins } = require('../testConfig')
6+
const { devApiBaseUrl, getComponents, origins } = require('../testConfig')
77

88
const ORIGIN_EXCLUSION_LIST = ['go/golang', 'debsrc/debian', 'maven/mavengoogle']
99
const ORIGIN_REVISIONS_EXCLUSION_LIST = ['debsrc/debian']
1010

11-
const MAVEN_COMPONENT_GROUP_ID = 'maven/mavencentral/org.apache.httpcomponents'
12-
const MAVEN_COMPONENT_PARTIAL_GROUP_ID = 'maven/mavencentral/org.apache.httpcompon'
11+
const MAVEN_COMPONENT_GROUP_ID = 'maven/mavencentral/org.apache.httpcomponents//'
12+
const MAVEN_COMPONENT_PARTIAL_GROUP_ID = 'maven/org.apache.httpcompone'
1313
const GRADLE_COMPONENT_ENDPOINT = 'gradleplugin/io.github.lognet.grpc-spring-boot'
1414

1515
;(async function validateOriginsApi() {
@@ -20,52 +20,60 @@ const GRADLE_COMPONENT_ENDPOINT = 'gradleplugin/io.github.lognet.grpc-spring-boo
2020

2121
afterEach(() => new Promise(resolve => setTimeout(resolve, origins.timeout / 2)))
2222

23-
components.filter(isOriginAllowed).forEach(component => {
24-
it(`Validates Origins API response for ${component}`, () => compareOrigins(component))
23+
components.filter(isComponentIncluded).forEach(component => {
24+
it(`Validates Origins API response for ${component}`, () => validateOriginResponses(component))
2525
})
2626

27-
components.filter(isOriginWithRevisionsAllowed).forEach(component => {
28-
it(`Validates Origins API response with revisions for ${component}`, () => compareOriginsWithRevisions(component))
27+
components.filter(isComponentIncludedWithRevisions).forEach(component => {
28+
it(`Validates Origins API response with revisions for ${component}`, () =>
29+
validateOriginResponsesWithRevisions(component))
2930
})
3031

3132
it('Validates Origins API response for a Maven component with only a group ID', () =>
32-
compareOrigins(MAVEN_COMPONENT_GROUP_ID))
33+
validateOriginResponses(MAVEN_COMPONENT_GROUP_ID))
3334

3435
it('Validates Origins API response for a Maven component with a partial group ID for suggestion checks', () =>
35-
compareOrigins(MAVEN_COMPONENT_PARTIAL_GROUP_ID))
36+
validateEndpointWithRevisions(MAVEN_COMPONENT_PARTIAL_GROUP_ID, 'httpcore'))
3637

3738
it('Validates Origins API response for a Gradle plugin component', () =>
38-
compareEndpoints(GRADLE_COMPONENT_ENDPOINT))
39+
validateEndpointResponses(GRADLE_COMPONENT_ENDPOINT, 'io.github.lognet.grpc-spring-boot'))
3940

4041
it('Validates Origins API with revisions response for a Gradle plugin component', () =>
41-
compareEndpoints(`${GRADLE_COMPONENT_ENDPOINT}/revisions`))
42+
validateEndpointWithRevisions(GRADLE_COMPONENT_ENDPOINT, '4.6.0'))
4243
})
4344
})()
4445

45-
function extractIds(response) {
46-
return response.map(item => item.id)
46+
function isValueInResponse(response, value) {
47+
return response.some(item => item?.id?.includes(value))
4748
}
4849

49-
function assertResponsesMatch(actual, expected) {
50-
const sortedActualIds = extractIds(actual).sort()
51-
const sortedExpectedIds = extractIds(expected).sort()
50+
function isRevisionInResponse(response, value) {
51+
return response.some(item => item?.sha?.includes(value)) || response.some(item => item.includes(value))
52+
}
53+
54+
function assertPackageResponse(actual, value) {
55+
assert.ok(actual.length > 0, `No matching package returned`)
56+
assert.ok(isValueInResponse(actual, value), `Response does not contain expected package`)
57+
}
5258

53-
assert.deepStrictEqual(sortedActualIds, sortedExpectedIds)
59+
function assertRevisionResponse(actual, value) {
60+
assert.ok(actual.length > 0, `No matching version returned`)
61+
assert.ok(isRevisionInResponse(actual, value), `Response does not contain expected version`)
5462
}
5563

56-
function isCoordinateAllowed(coordinate, exclusionList) {
57-
return !exclusionList.some(excluded => coordinate.includes(excluded))
64+
function exclusionFilter(item, exclusionList) {
65+
return !exclusionList.some(excluded => item.includes(excluded))
5866
}
5967

60-
function isOriginAllowed(coordinate) {
61-
return isCoordinateAllowed(coordinate, ORIGIN_EXCLUSION_LIST)
68+
function isComponentIncluded(coordinate) {
69+
return exclusionFilter(coordinate, ORIGIN_EXCLUSION_LIST)
6270
}
6371

64-
function isOriginWithRevisionsAllowed(coordinate) {
65-
return isCoordinateAllowed(coordinate, ORIGIN_REVISIONS_EXCLUSION_LIST)
72+
function isComponentIncludedWithRevisions(coordinate) {
73+
return exclusionFilter(coordinate, ORIGIN_REVISIONS_EXCLUSION_LIST)
6674
}
6775

68-
function getProviderType(type, provider) {
76+
function resolveProviderType(type, provider) {
6977
switch (type) {
7078
case 'git':
7179
case 'gem':
@@ -81,8 +89,13 @@ function getProviderType(type, provider) {
8189

8290
function parseCoordinates(coordinates) {
8391
const [type, provider, namespaceOrEmpty, name, version] = coordinates.split('/')
84-
const namespace = namespaceOrEmpty === '-' ? '' : namespaceOrEmpty
85-
return { type, provider, namespace, name, version }
92+
return {
93+
type,
94+
provider,
95+
namespace: namespaceOrEmpty === '-' ? '' : namespaceOrEmpty,
96+
name,
97+
version
98+
}
8699
}
87100

88101
function buildCondaUrl(coordinates) {
@@ -92,24 +105,32 @@ function buildCondaUrl(coordinates) {
92105

93106
function buildOriginUrl(coordinates) {
94107
const { type, provider, namespace, name } = parseCoordinates(coordinates)
95-
const resolvedType = getProviderType(type, provider)
108+
const resolvedType = resolveProviderType(type, provider)
96109
return `${resolvedType}${namespace ? `/${namespace}` : ''}${name ? `/${name}` : ''}`
97110
}
98111

99-
async function compareEndpoints(endpoint) {
100-
const [devResponse, prodResponse] = await Promise.all([
101-
callFetch(`${devApiBaseUrl}/origins/${endpoint}`).then(res => res.json()),
102-
callFetch(`${prodApiBaseUrl}/origins/${endpoint}`).then(res => res.json())
103-
])
104-
assertResponsesMatch(devResponse, prodResponse)
112+
async function validateEndpointResponses(endpoint, expectedValue) {
113+
const devResponse = await callFetch(`${devApiBaseUrl}/origins/${endpoint}`).then(res => res.json())
114+
assertPackageResponse(devResponse, expectedValue)
105115
}
106116

107-
async function compareOriginsWithRevisions(coordinates) {
117+
async function validateEndpointWithRevisions(endpoint, expectedValue) {
118+
const devResponse = await callFetch(`${devApiBaseUrl}/origins/${endpoint}/revisions`).then(res => res.json())
119+
assertRevisionResponse(devResponse, expectedValue)
120+
}
121+
122+
async function validateOriginResponsesWithRevisions(coordinates) {
108123
const originUrl = buildOriginUrl(coordinates)
109-
await compareEndpoints(`${originUrl}/revisions`)
124+
const version = normalizeVersion(coordinates.split('/').at(-1))
125+
await validateEndpointWithRevisions(originUrl, version)
110126
}
111127

112-
async function compareOrigins(coordinates) {
128+
async function validateOriginResponses(coordinates) {
113129
const originUrl = coordinates.startsWith('conda/') ? buildCondaUrl(coordinates) : buildOriginUrl(coordinates)
114-
await compareEndpoints(`${originUrl}`)
130+
const componentName = coordinates.split('/').at(-2)
131+
await validateEndpointResponses(originUrl, componentName)
132+
}
133+
134+
function normalizeVersion(version) {
135+
return version.replace(/_[\w]+$/, '').replace(/^v/, '')
115136
}

0 commit comments

Comments
 (0)