|
| 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