|
| 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 | +import { faker } from '@faker-js/faker'; |
| 23 | + |
| 24 | +const envId = 'DEFAULT'; |
| 25 | + |
| 26 | +const v2ApisResource = new APIsApi(forManagementV2AsAdminUser()); |
| 27 | + |
| 28 | +describe('API - V4 - Update via Swagger/OpenAPI import', () => { |
| 29 | + const specification = JSON.stringify(openapiv3); |
| 30 | + |
| 31 | + describe('Update an existing API from an OpenAPI specification', () => { |
| 32 | + let createdApi: ApiV4; |
| 33 | + |
| 34 | + beforeAll(async () => { |
| 35 | + createdApi = await created( |
| 36 | + v2ApisResource.createApiFromSwaggerRaw({ |
| 37 | + envId, |
| 38 | + importSwaggerDescriptor: { payload: specification }, |
| 39 | + }), |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + test('should update the API via PUT /_import/swagger', async () => { |
| 44 | + const updatedApi = await succeed( |
| 45 | + v2ApisResource.updateApiFromSwaggerRaw({ |
| 46 | + envId, |
| 47 | + apiId: createdApi.id, |
| 48 | + importSwaggerDescriptor: { payload: specification }, |
| 49 | + }), |
| 50 | + ); |
| 51 | + |
| 52 | + expect(updatedApi).toBeTruthy(); |
| 53 | + expect(updatedApi.id).toStrictEqual(createdApi.id); |
| 54 | + expect(updatedApi.name).toBe('Gravitee.io Swagger API'); |
| 55 | + expect(updatedApi.apiVersion).toBe('1.2.3'); |
| 56 | + }); |
| 57 | + |
| 58 | + test('should get updated API with data from the specification', async () => { |
| 59 | + const fetchedApi = await succeed( |
| 60 | + v2ApisResource.getApiRaw({ |
| 61 | + envId, |
| 62 | + apiId: createdApi.id, |
| 63 | + }), |
| 64 | + ); |
| 65 | + expect(fetchedApi.id).toStrictEqual(createdApi.id); |
| 66 | + expect(fetchedApi.name).toBe('Gravitee.io Swagger API'); |
| 67 | + }); |
| 68 | + |
| 69 | + afterAll(async () => { |
| 70 | + if (createdApi) { |
| 71 | + await noContent(v2ApisResource.deleteApiRaw({ envId, apiId: createdApi.id })); |
| 72 | + } |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('Update a non-existent API from Swagger returns 404', () => { |
| 77 | + test('should fail with 404 when API does not exist', async () => { |
| 78 | + await fail( |
| 79 | + v2ApisResource.updateApiFromSwaggerRaw({ |
| 80 | + envId, |
| 81 | + apiId: faker.string.uuid(), |
| 82 | + importSwaggerDescriptor: { payload: specification }, |
| 83 | + }), |
| 84 | + 404, |
| 85 | + ); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('Update API from Swagger preserves the API ID', () => { |
| 90 | + let importedApi: ApiV4; |
| 91 | + |
| 92 | + afterEach(async () => { |
| 93 | + if (importedApi) { |
| 94 | + await noContent(v2ApisResource.deleteApiRaw({ envId, apiId: importedApi.id })); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + test('should keep the same API ID after update', async () => { |
| 99 | + importedApi = await created( |
| 100 | + v2ApisResource.createApiFromSwaggerRaw({ |
| 101 | + envId, |
| 102 | + importSwaggerDescriptor: { payload: specification }, |
| 103 | + }), |
| 104 | + ); |
| 105 | + |
| 106 | + const updatedApi = await succeed( |
| 107 | + v2ApisResource.updateApiFromSwaggerRaw({ |
| 108 | + envId, |
| 109 | + apiId: importedApi.id, |
| 110 | + importSwaggerDescriptor: { payload: specification }, |
| 111 | + }), |
| 112 | + ); |
| 113 | + |
| 114 | + expect(updatedApi.id).toStrictEqual(importedApi.id); |
| 115 | + }); |
| 116 | + |
| 117 | + test('should update the API flows from the specification', async () => { |
| 118 | + importedApi = await created( |
| 119 | + v2ApisResource.createApiFromSwaggerRaw({ |
| 120 | + envId, |
| 121 | + importSwaggerDescriptor: { payload: specification }, |
| 122 | + }), |
| 123 | + ); |
| 124 | + |
| 125 | + const updatedApi = await succeed( |
| 126 | + v2ApisResource.updateApiFromSwaggerRaw({ |
| 127 | + envId, |
| 128 | + apiId: importedApi.id, |
| 129 | + importSwaggerDescriptor: { payload: specification }, |
| 130 | + }), |
| 131 | + ); |
| 132 | + |
| 133 | + expect(updatedApi.flows).toBeDefined(); |
| 134 | + expect(updatedApi.flows.length).toBeGreaterThan(0); |
| 135 | + }); |
| 136 | + |
| 137 | + test('should preserve flow ids when re-importing the same OpenAPI', async () => { |
| 138 | + importedApi = await created( |
| 139 | + v2ApisResource.createApiFromSwaggerRaw({ |
| 140 | + envId, |
| 141 | + importSwaggerDescriptor: { payload: specification }, |
| 142 | + }), |
| 143 | + ); |
| 144 | + |
| 145 | + const beforeUpdate = await succeed( |
| 146 | + v2ApisResource.getApiRaw({ |
| 147 | + envId, |
| 148 | + apiId: importedApi.id, |
| 149 | + }), |
| 150 | + ); |
| 151 | + const flowIdsBefore = ('flows' in beforeUpdate ? (beforeUpdate.flows ?? []) : []).map((flow) => flow.id); |
| 152 | + |
| 153 | + await succeed( |
| 154 | + v2ApisResource.updateApiFromSwaggerRaw({ |
| 155 | + envId, |
| 156 | + apiId: importedApi.id, |
| 157 | + importSwaggerDescriptor: { payload: specification }, |
| 158 | + }), |
| 159 | + ); |
| 160 | + |
| 161 | + const afterUpdate = await succeed( |
| 162 | + v2ApisResource.getApiRaw({ |
| 163 | + envId, |
| 164 | + apiId: importedApi.id, |
| 165 | + }), |
| 166 | + ); |
| 167 | + const flowIdsAfter = ('flows' in afterUpdate ? (afterUpdate.flows ?? []) : []).map((flow) => flow.id); |
| 168 | + |
| 169 | + expect(flowIdsBefore.length).toBeGreaterThan(0); |
| 170 | + expect(flowIdsAfter.length).toEqual(flowIdsBefore.length); |
| 171 | + expect(new Set(flowIdsAfter)).toEqual(new Set(flowIdsBefore)); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments