Skip to content

Commit 8a75805

Browse files
feat(e2e): add e2e test for update v4 apis using import
1 parent fbdcfd4 commit 8a75805

File tree

2 files changed

+277
-0
lines changed

2 files changed

+277
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { afterAll, afterEach, beforeAll, describe, expect, test } from '@jest/globals';
17+
import { APIsApi, ApiV4 } from '@gravitee/management-v2-webclient-sdk/src/lib';
18+
import { forManagementV2AsAdminUser } from '@gravitee/utils/configuration';
19+
import * as openapiv3 from '@api-test-resources/openapi-withExtensions.json';
20+
import { created, fail, noContent, succeed } from '@lib/jest-utils';
21+
import { MAPIV2ApisFaker } from '@gravitee/fixtures/management/MAPIV2ApisFaker';
22+
23+
const envId = 'DEFAULT';
24+
25+
const v2ApisResource = new APIsApi(forManagementV2AsAdminUser());
26+
27+
describe('API - V4 - Update via Swagger/OpenAPI import', () => {
28+
const specification = JSON.stringify(openapiv3);
29+
30+
describe('Update an existing API from an OpenAPI specification', () => {
31+
let createdApi: ApiV4;
32+
33+
beforeAll(async () => {
34+
createdApi = await created(
35+
v2ApisResource.createApiFromSwaggerRaw({
36+
envId,
37+
importSwaggerDescriptor: { payload: specification },
38+
}),
39+
);
40+
});
41+
42+
test('should update the API via PUT /_import/swagger', async () => {
43+
const updatedApi = await succeed(
44+
v2ApisResource.updateApiFromSwaggerRaw({
45+
envId,
46+
apiId: createdApi.id,
47+
importSwaggerDescriptor: { payload: specification },
48+
}),
49+
);
50+
51+
expect(updatedApi).toBeTruthy();
52+
expect(updatedApi.id).toStrictEqual(createdApi.id);
53+
expect(updatedApi.name).toBe('Gravitee.io Swagger API');
54+
expect(updatedApi.apiVersion).toBe('1.2.3');
55+
});
56+
57+
test('should get updated API with data from the specification', async () => {
58+
const fetchedApi = await succeed(
59+
v2ApisResource.getApiRaw({
60+
envId,
61+
apiId: createdApi.id,
62+
}),
63+
);
64+
expect(fetchedApi.id).toStrictEqual(createdApi.id);
65+
expect(fetchedApi.name).toBe('Gravitee.io Swagger API');
66+
});
67+
68+
afterAll(async () => {
69+
if (createdApi) {
70+
await noContent(v2ApisResource.deleteApiRaw({ envId, apiId: createdApi.id }));
71+
}
72+
});
73+
});
74+
75+
describe('Update a non-existent API from Swagger returns 404', () => {
76+
test('should fail with 404 when API does not exist', async () => {
77+
await fail(
78+
v2ApisResource.updateApiFromSwaggerRaw({
79+
envId,
80+
apiId: 'non-existent-api-id',
81+
importSwaggerDescriptor: { payload: specification },
82+
}),
83+
404,
84+
);
85+
});
86+
});
87+
88+
describe('Update API from Swagger preserves the API ID', () => {
89+
let importedApi: ApiV4;
90+
91+
afterEach(async () => {
92+
if (importedApi) {
93+
await noContent(v2ApisResource.deleteApiRaw({ envId, apiId: importedApi.id }));
94+
}
95+
});
96+
97+
test('should keep the same API ID after update', async () => {
98+
importedApi = await created(
99+
v2ApisResource.createApiFromSwaggerRaw({
100+
envId,
101+
importSwaggerDescriptor: { payload: specification },
102+
}),
103+
);
104+
105+
const updatedApi = await succeed(
106+
v2ApisResource.updateApiFromSwaggerRaw({
107+
envId,
108+
apiId: importedApi.id,
109+
importSwaggerDescriptor: { payload: specification },
110+
}),
111+
);
112+
113+
expect(updatedApi.id).toStrictEqual(importedApi.id);
114+
});
115+
116+
test('should update the API flows from the specification', async () => {
117+
importedApi = await created(
118+
v2ApisResource.createApiFromSwaggerRaw({
119+
envId,
120+
importSwaggerDescriptor: { payload: specification },
121+
}),
122+
);
123+
124+
const updatedApi = await succeed(
125+
v2ApisResource.updateApiFromSwaggerRaw({
126+
envId,
127+
apiId: importedApi.id,
128+
importSwaggerDescriptor: { payload: specification },
129+
}),
130+
);
131+
132+
expect(updatedApi.flows).toBeDefined();
133+
expect(updatedApi.flows.length).toBeGreaterThan(0);
134+
});
135+
});
136+
});
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { afterAll, describe, expect, test } from '@jest/globals';
17+
import { APIsApi, ApiV4 } from '@gravitee/management-v2-webclient-sdk/src/lib';
18+
import { forManagementV2AsApiUser } from '@gravitee/utils/configuration';
19+
import { MAPIV2ApisFaker } from '@gravitee/fixtures/management/MAPIV2ApisFaker';
20+
import { created, fail, noContent, succeed } from '@lib/jest-utils';
21+
22+
const envId = 'DEFAULT';
23+
24+
const v2ApisResource = new APIsApi(forManagementV2AsApiUser());
25+
26+
describe('API - V4 - Update via definition import', () => {
27+
describe('Update an existing API with a new definition', () => {
28+
let createdApi: ApiV4;
29+
const originalDefinition = MAPIV2ApisFaker.apiImportV4();
30+
31+
test('should create a v4 API to update later', async () => {
32+
createdApi = await created(
33+
v2ApisResource.createApiWithImportDefinitionRaw({
34+
envId,
35+
exportApiV4: originalDefinition,
36+
}),
37+
);
38+
expect(createdApi).toBeTruthy();
39+
expect(createdApi.id).toBeTruthy();
40+
});
41+
42+
test('should update the API via PUT /_import/definition', async () => {
43+
const updatedDefinition = MAPIV2ApisFaker.apiImportV4({
44+
api: MAPIV2ApisFaker.apiV4Proxy({
45+
name: 'Updated API Name',
46+
apiVersion: '2.0.0',
47+
description: 'Updated via definition import',
48+
listeners: createdApi.listeners,
49+
}),
50+
});
51+
52+
const updatedApi = await succeed(
53+
v2ApisResource.updateApiWithDefinitionRaw({
54+
envId,
55+
apiId: createdApi.id,
56+
exportApiV4: updatedDefinition,
57+
}),
58+
);
59+
60+
expect(updatedApi).toBeTruthy();
61+
expect(updatedApi.id).toStrictEqual(createdApi.id);
62+
expect(updatedApi.name).toBe('Updated API Name');
63+
expect(updatedApi.apiVersion).toBe('2.0.0');
64+
expect(updatedApi.description).toBe('Updated via definition import');
65+
});
66+
67+
test('should get updated API with new data', async () => {
68+
const fetchedApi = await succeed(
69+
v2ApisResource.getApiRaw({
70+
envId,
71+
apiId: createdApi.id,
72+
}),
73+
);
74+
expect(fetchedApi.id).toStrictEqual(createdApi.id);
75+
expect(fetchedApi.name).toBe('Updated API Name');
76+
expect(fetchedApi.apiVersion).toBe('2.0.0');
77+
});
78+
79+
afterAll(async () => {
80+
if (createdApi) {
81+
await noContent(v2ApisResource.deleteApiRaw({ envId, apiId: createdApi.id }));
82+
}
83+
});
84+
});
85+
86+
describe('Update a non-existent API returns 404', () => {
87+
test('should fail with 404 when API does not exist', async () => {
88+
await fail(
89+
v2ApisResource.updateApiWithDefinitionRaw({
90+
envId,
91+
apiId: 'non-existent-api-id',
92+
exportApiV4: MAPIV2ApisFaker.apiImportV4(),
93+
}),
94+
404,
95+
);
96+
});
97+
});
98+
99+
describe('Update API with a conflicting context path returns 400', () => {
100+
let api1: ApiV4;
101+
let api2: ApiV4;
102+
103+
test('should create first API', async () => {
104+
api1 = await created(
105+
v2ApisResource.createApiWithImportDefinitionRaw({
106+
envId,
107+
exportApiV4: MAPIV2ApisFaker.apiImportV4(),
108+
}),
109+
);
110+
});
111+
112+
test('should create second API', async () => {
113+
api2 = await created(
114+
v2ApisResource.createApiWithImportDefinitionRaw({
115+
envId,
116+
exportApiV4: MAPIV2ApisFaker.apiImportV4(),
117+
}),
118+
);
119+
});
120+
121+
test('should fail to update second API with the same context path as first API', async () => {
122+
await fail(
123+
v2ApisResource.updateApiWithDefinitionRaw({
124+
envId,
125+
apiId: api2.id,
126+
exportApiV4: MAPIV2ApisFaker.apiImportV4({
127+
api: MAPIV2ApisFaker.apiV4Proxy({
128+
listeners: api1.listeners,
129+
}),
130+
}),
131+
}),
132+
400,
133+
);
134+
});
135+
136+
afterAll(async () => {
137+
if (api1) await noContent(v2ApisResource.deleteApiRaw({ envId, apiId: api1.id }));
138+
if (api2) await noContent(v2ApisResource.deleteApiRaw({ envId, apiId: api2.id }));
139+
});
140+
});
141+
});

0 commit comments

Comments
 (0)