-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathapi-failover-v4.component.spec.ts
More file actions
327 lines (278 loc) · 14.1 KB
/
api-failover-v4.component.spec.ts
File metadata and controls
327 lines (278 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { HttpTestingController } from '@angular/common/http/testing';
import { HarnessLoader } from '@angular/cdk/testing';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { MatSlideToggleHarness } from '@angular/material/slide-toggle/testing';
import { GioConfirmDialogHarness, GioSaveBarHarness } from '@gravitee/ui-particles-angular';
import { MatInputHarness } from '@angular/material/input/testing';
import { InteractivityChecker } from '@angular/cdk/a11y';
import { ActivatedRoute } from '@angular/router';
import { DivHarness } from '@gravitee/ui-particles-angular/testing';
import { ApiFailoverV4Component } from './api-failover-v4.component';
import { ApiFailoverV4Module } from './api-failover-v4.module';
import { CONSTANTS_TESTING, GioTestingModule } from '../../../shared/testing';
import { ApiV4, fakeApiV4 } from '../../../entities/management-api-v2';
import { GioTestingPermissionProvider } from '../../../shared/components/gio-permission/gio-permission.service';
describe('ApiV4FailoverComponent', () => {
const API_ID = 'apiId';
let fixture: ComponentFixture<ApiFailoverV4Component>;
let loader: HarnessLoader;
let httpTestingController: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [NoopAnimationsModule, GioTestingModule, ApiFailoverV4Module],
providers: [
{ provide: ActivatedRoute, useValue: { snapshot: { params: { apiId: API_ID } } } },
{ provide: GioTestingPermissionProvider, useValue: ['api-definition-u'] },
],
}).overrideProvider(InteractivityChecker, {
useValue: {
isFocusable: () => true, // This checks focus trap, set it to true to avoid the warning
},
});
});
beforeEach(() => {
fixture = TestBed.createComponent(ApiFailoverV4Component);
loader = TestbedHarnessEnvironment.loader(fixture);
TestbedHarnessEnvironment.documentRootLoader(fixture);
httpTestingController = TestBed.inject(HttpTestingController);
fixture.detectChanges();
});
afterEach(() => {
httpTestingController.verify();
});
it('should enable and set failover config', async () => {
const api = fakeApiV4({
id: API_ID,
});
expectApiGetRequest(api);
const saveBar = await loader.getHarness(GioSaveBarHarness);
expect(await saveBar.isVisible()).toBe(false);
const enabledSlideToggle = await loader.getHarness(MatSlideToggleHarness.with({ selector: '[formControlName="enabled"]' }));
expect(await enabledSlideToggle.isChecked()).toEqual(false);
// Check each field is disabled
const forceNextToggle = await loader.getHarness(
MatSlideToggleHarness.with({ selector: '[formControlName="forceNextEndpointOnFailure"]' }),
);
expect(await forceNextToggle.isChecked()).toEqual(false);
expect(await forceNextToggle.isDisabled()).toBe(true);
const maxRetriesInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="maxRetries"]' }));
expect(await maxRetriesInput.isDisabled()).toBe(true);
expect(await maxRetriesInput.getValue()).toEqual('2');
const failureConditionInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="failureCondition"]' }));
expect(await failureConditionInput.isDisabled()).toBe(true);
expect(await failureConditionInput.getValue()).toEqual('');
const slowCallDurationInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="slowCallDuration"]' }));
expect(await slowCallDurationInput.isDisabled()).toBe(true);
expect(await slowCallDurationInput.getValue()).toEqual('2000');
const openStateDurationInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="openStateDuration"]' }));
expect(await openStateDurationInput.isDisabled()).toBe(true);
expect(await openStateDurationInput.getValue()).toEqual('10000');
const maxFailuresInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="maxFailures"]' }));
expect(await maxFailuresInput.isDisabled()).toBe(true);
expect(await maxFailuresInput.getValue()).toEqual('5');
const perSubscriptionToggle = await loader.getHarness(MatSlideToggleHarness.with({ selector: '[formControlName="perSubscription"]' }));
expect(await perSubscriptionToggle.isChecked()).toEqual(true);
expect(await perSubscriptionToggle.isDisabled()).toBe(true);
// Enable Failover & set some values
await enabledSlideToggle.toggle();
await maxRetriesInput.setValue('2');
await slowCallDurationInput.setValue('200');
await openStateDurationInput.setValue('2000');
await maxFailuresInput.setValue('2');
expect(await saveBar.isSubmitButtonInvalid()).toEqual(false);
await saveBar.clickSubmit();
// Expect fetch api and update
expectApiGetRequest(api);
const req = httpTestingController.expectOne({ method: 'PUT', url: `${CONSTANTS_TESTING.env.v2BaseURL}/apis/${API_ID}` });
expect(req.request.body.failover).toStrictEqual({
enabled: true,
forceNextEndpointOnFailure: false,
maxRetries: 2,
failureCondition: '',
slowCallDuration: 200,
openStateDuration: 2000,
maxFailures: 2,
perSubscription: true,
});
});
it.each(['kafka', 'mock'])('should update failover config for %p endpoint', async endpointType => {
const api = fakeApiV4({
id: API_ID,
endpointGroups: [
{
name: 'default-group',
type: endpointType,
loadBalancer: {
type: 'ROUND_ROBIN',
},
endpoints: [
{
name: 'default',
type: endpointType,
weight: 1,
inheritConfiguration: false,
configuration: {},
},
],
},
],
failover: {
enabled: true,
maxRetries: 2,
slowCallDuration: 200,
openStateDuration: 2000,
maxFailures: 2,
perSubscription: true,
},
});
expectApiGetRequest(api);
const saveBar = await loader.getHarness(GioSaveBarHarness);
const enabledSlideToggle = await loader.getHarness(MatSlideToggleHarness.with({ selector: '[formControlName="enabled"]' }));
expect(await enabledSlideToggle.isChecked()).toEqual(true);
// Check each field is disabled
const maxRetriesInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="maxRetries"]' }));
expect(await maxRetriesInput.isDisabled()).toBe(false);
expect(await maxRetriesInput.getValue()).toEqual('2');
await maxRetriesInput.setValue('3');
const slowCallDurationInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="slowCallDuration"]' }));
expect(await slowCallDurationInput.isDisabled()).toBe(false);
expect(await slowCallDurationInput.getValue()).toEqual('200');
await slowCallDurationInput.setValue('300');
const openStateDurationInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="openStateDuration"]' }));
expect(await openStateDurationInput.isDisabled()).toBe(false);
expect(await openStateDurationInput.getValue()).toEqual('2000');
await openStateDurationInput.setValue('3000');
const maxFailuresInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="maxFailures"]' }));
expect(await maxFailuresInput.isDisabled()).toBe(false);
expect(await maxFailuresInput.getValue()).toEqual('2');
await maxFailuresInput.setValue('3');
const perSubscriptionToggle = await loader.getHarness(MatSlideToggleHarness.with({ selector: '[formControlName="perSubscription"]' }));
await perSubscriptionToggle.toggle();
expect(await perSubscriptionToggle.isChecked()).toEqual(false);
// Verify warning is displayed
const warningBanner = await loader.getAllHarnesses(DivHarness.with({ selector: '.banner__wrapper__title' }));
if (endpointType === 'kafka') {
expect(await warningBanner[0].getText()).toContain(
'Failover is not supported for Kafka endpoints. Enabling it will have no effect. Use the native Kafka Failover by providing multiple bootstrap servers.',
);
expect(await warningBanner[4].getText()).toContain('The circuit breaker will be configured for the whole API');
} else {
expect(await warningBanner[3].getText()).toContain('The circuit breaker will be configured for the whole API');
}
expect(await saveBar.isSubmitButtonInvalid()).toEqual(false);
await saveBar.clickSubmit();
// Confirm saving with global circuit breaker
const dialog = await TestbedHarnessEnvironment.documentRootLoader(fixture).getHarness(GioConfirmDialogHarness);
await dialog.confirm();
// Expect fetch api and update
expectApiGetRequest(api);
const req = httpTestingController.expectOne({ method: 'PUT', url: `${CONSTANTS_TESTING.env.v2BaseURL}/apis/${API_ID}` });
expect(req.request.body.failover).toStrictEqual({
enabled: true,
forceNextEndpointOnFailure: false,
maxRetries: 3,
failureCondition: '',
slowCallDuration: 300,
openStateDuration: 3000,
maxFailures: 3,
perSubscription: false,
});
});
it('should disable fields when origin is kubernetes', async () => {
const api = fakeApiV4({
id: API_ID,
failover: undefined,
definitionContext: { origin: 'KUBERNETES' },
});
expectApiGetRequest(api);
const saveBar = await loader.getHarness(GioSaveBarHarness);
expect(await saveBar.isVisible()).toBe(false);
const enabledSlideToggle = await loader.getHarness(MatSlideToggleHarness.with({ selector: '[formControlName="enabled"]' }));
expect(await enabledSlideToggle.isChecked()).toEqual(false);
// Check each field is disabled
const forceNextToggle = await loader.getHarness(
MatSlideToggleHarness.with({ selector: '[formControlName="forceNextEndpointOnFailure"]' }),
);
expect(await forceNextToggle.isDisabled()).toBe(true);
const maxRetriesInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="maxRetries"]' }));
expect(await maxRetriesInput.isDisabled()).toBe(true);
const failureConditionInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="failureCondition"]' }));
expect(await failureConditionInput.isDisabled()).toBe(true);
const slowCallDurationInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="slowCallDuration"]' }));
expect(await slowCallDurationInput.isDisabled()).toBe(true);
const openStateDurationInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="openStateDuration"]' }));
expect(await openStateDurationInput.isDisabled()).toBe(true);
const maxFailuresInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="maxFailures"]' }));
expect(await maxFailuresInput.isDisabled()).toBe(true);
const perSubscriptionToggle = await loader.getHarness(MatSlideToggleHarness.with({ selector: '[formControlName="perSubscription"]' }));
expect(await perSubscriptionToggle.isDisabled()).toBe(true);
expect(await perSubscriptionToggle.isChecked()).toEqual(true);
});
it('should display and submit failureCondition and forceNextEndpointOnFailure', async () => {
const api = fakeApiV4({
id: API_ID,
failover: {
enabled: true,
maxRetries: 2,
slowCallDuration: 200,
openStateDuration: 2000,
maxFailures: 2,
perSubscription: true,
failureCondition: '{#response.status >= 500}',
forceNextEndpointOnFailure: true,
},
});
expectApiGetRequest(api);
const saveBar = await loader.getHarness(GioSaveBarHarness);
// Verify forceNextEndpointOnFailure toggle is loaded with correct value
const forceNextToggle = await loader.getHarness(
MatSlideToggleHarness.with({ selector: '[formControlName="forceNextEndpointOnFailure"]' }),
);
expect(await forceNextToggle.isChecked()).toEqual(true);
expect(await forceNextToggle.isDisabled()).toBe(false);
// Verify failureCondition input is loaded with correct value
const failureConditionInput = await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="failureCondition"]' }));
expect(await failureConditionInput.getValue()).toEqual('{#response.status >= 500}');
expect(await failureConditionInput.isDisabled()).toBe(false);
// Modify values via UI controls
await forceNextToggle.toggle();
await failureConditionInput.setValue('{#response.status >= 502}');
await loader.getHarness(MatInputHarness.with({ selector: '[formControlName="maxRetries"]' })).then(input => input.setValue('3'));
expect(await saveBar.isSubmitButtonInvalid()).toEqual(false);
await saveBar.clickSubmit();
// Expect fetch api and update
expectApiGetRequest(api);
const req = httpTestingController.expectOne({ method: 'PUT', url: `${CONSTANTS_TESTING.env.v2BaseURL}/apis/${API_ID}` });
expect(req.request.body.failover).toStrictEqual({
enabled: true,
forceNextEndpointOnFailure: false,
maxRetries: 3,
failureCondition: '{#response.status >= 502}',
slowCallDuration: 200,
openStateDuration: 2000,
maxFailures: 2,
perSubscription: true,
});
});
function expectApiGetRequest(api: ApiV4) {
httpTestingController.expectOne({ url: `${CONSTANTS_TESTING.env.v2BaseURL}/apis/${api.id}`, method: 'GET' }).flush(api);
fixture.detectChanges();
}
});