Skip to content

Commit

Permalink
feat: ps - rework executionPhase & policy to use FlowPhase
Browse files Browse the repository at this point in the history
We need to keep PS compatible with all APIM < 4.6
  • Loading branch information
ThibaudAV committed Oct 18, 2024
1 parent 259fbfd commit 9eab2b8
Show file tree
Hide file tree
Showing 25 changed files with 444 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
GioPolicyStudioStepEditDialogData,
GioPolicyStudioStepEditDialogResult,
} from '../step-edit-dialog/gio-ps-step-edit-dialog.component';
import { ExecutionPhase, isPolicy, isSharedPolicyGroupPolicy, Step, GenericPolicy } from '../../models';
import { FlowPhase, isPolicy, isSharedPolicyGroupPolicy, Step, GenericPolicy } from '../../models';

@Component({
standalone: true,
Expand All @@ -47,7 +47,7 @@ export class GioPolicyStudioDetailsPhaseStepComponent implements OnChanges {
public genericPolicies: GenericPolicy[] = [];

@Input({ required: true })
public executionPhase!: ExecutionPhase;
public flowPhase!: FlowPhase;

@Output()
public stepChange = new EventEmitter<Step>();
Expand Down Expand Up @@ -120,7 +120,7 @@ ${this.policyNotFound === 'SHARED_POLICY_GROUP' ? 'Note: The Gateway will ignore
readOnly: this.readOnly,
genericPolicy: this.genericPolicy,
step: this.step,
executionPhase: this.executionPhase,
flowPhase: this.flowPhase,
},
role: 'alertdialog',
id: 'gioPolicyStudioPolicyFormDialog',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
[class.disabled]="readOnly || !stepVM.step.enabled"
[genericPolicies]="genericPolicies"
[step]="stepVM.step"
[executionPhase]="policyExecutionPhase"
[flowPhase]="policyFlowPhase"
(stepChange)="onStepChange(stepVM.index, $event)"
(deleted)="onStepDeleted(stepVM.index)"
(disabled)="onStepDisabled(stepVM.index)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
GioPolicyStudioPoliciesCatalogDialogData,
GioPolicyStudioPoliciesCatalogDialogResult,
} from '../policies-catalog-dialog/gio-ps-policies-catalog-dialog.component';
import { ApiType, ConnectorInfo, ExecutionPhase, Step } from '../../models';
import { ApiType, ConnectorInfo, FlowPhase, Step } from '../../models';
import { GioPolicyStudioDetailsPhaseStepComponent } from '../flow-details-phase-step/gio-ps-flow-details-phase-step.component';
import { GenericPolicy } from '../../models/policy/GenericPolicy';

Expand Down Expand Up @@ -79,7 +79,7 @@ export class GioPolicyStudioDetailsPhaseComponent implements OnChanges {
public genericPolicies: GenericPolicy[] = [];

@Input()
public policyExecutionPhase!: ExecutionPhase;
public policyFlowPhase!: FlowPhase;

@Input()
public trialUrl?: string;
Expand Down Expand Up @@ -136,7 +136,7 @@ export class GioPolicyStudioDetailsPhaseComponent implements OnChanges {
>(GioPolicyStudioPoliciesCatalogDialogComponent, {
data: {
genericPolicies: this.genericPolicies,
executionPhase: this.policyExecutionPhase,
flowPhase: this.policyFlowPhase,
apiType: this.apiType,
trialUrl: this.trialUrl,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
[apiType]="apiType"
[genericPolicies]="genericPolicies"
[trialUrl]="trialUrl"
policyExecutionPhase="REQUEST"
policyFlowPhase="REQUEST"
(stepsChange)="onStepsChange('request', $event)"
></gio-ps-flow-details-phase>
<gio-ps-flow-details-phase
Expand All @@ -73,7 +73,7 @@
[apiType]="apiType"
[genericPolicies]="genericPolicies"
[trialUrl]="trialUrl"
policyExecutionPhase="RESPONSE"
policyFlowPhase="RESPONSE"
(stepsChange)="onStepsChange('response', $event)"
></gio-ps-flow-details-phase>
</mat-tab>
Expand All @@ -89,7 +89,7 @@
[apiType]="apiType"
[genericPolicies]="genericPolicies"
[trialUrl]="trialUrl"
policyExecutionPhase="MESSAGE_REQUEST"
policyFlowPhase="PUBLISH"
(stepsChange)="onStepsChange('publish', $event)"
></gio-ps-flow-details-phase>
<gio-ps-flow-details-phase
Expand All @@ -103,7 +103,7 @@
[apiType]="apiType"
[genericPolicies]="genericPolicies"
[trialUrl]="trialUrl"
policyExecutionPhase="MESSAGE_RESPONSE"
policyFlowPhase="SUBSCRIBE"
(stepsChange)="onStepsChange('subscribe', $event)"
></gio-ps-flow-details-phase>
</mat-tab>
Expand All @@ -121,7 +121,7 @@
[apiType]="apiType"
[genericPolicies]="genericPolicies"
[trialUrl]="trialUrl"
policyExecutionPhase="REQUEST"
policyFlowPhase="REQUEST"
(stepsChange)="onStepsChange('request', $event)"
></gio-ps-flow-details-phase>
<gio-ps-flow-details-phase
Expand All @@ -135,7 +135,7 @@
[apiType]="apiType"
[genericPolicies]="genericPolicies"
[trialUrl]="trialUrl"
policyExecutionPhase="RESPONSE"
policyFlowPhase="RESPONSE"
(stepsChange)="onStepsChange('response', $event)"
></gio-ps-flow-details-phase>
</ng-container>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
-->
<h2 mat-dialog-title class="title">
Policies for <span class="title__phase">{{ executionPhaseLabel }}</span> phase
Policies for <span class="title__phase">{{ flowPhaseLabel }}</span> phase

<button class="title__closeBtn" mat-icon-button aria-label="Close dialog" [mat-dialog-close]="">
<mat-icon svgIcon="gio:cancel"></mat-icon>
Expand Down Expand Up @@ -120,7 +120,7 @@ <h2 mat-dialog-title class="title">

<gio-ps-step-form
[genericPolicy]="selectedPolicy"
[executionPhase]="executionPhase"
[flowPhase]="flowPhase"
(stepChange)="onStepChange($event)"
(isValid)="onIsValid($event)"
></gio-ps-step-form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { MatInputHarness } from '@angular/material/input/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';

import { fakeAllPolicies } from '../../models/index-testing';
import { ApiType, ExecutionPhase, isPolicy, isSharedPolicyGroupPolicy, toGenericPolicies, GenericPolicy } from '../../models';
import { ApiType, FlowPhase, isPolicy, isSharedPolicyGroupPolicy, toGenericPolicies, GenericPolicy, fromPolicyInput } from '../../models';
import { GioPolicyStudioService } from '../../policy-studio/gio-policy-studio.service';
import { fakePolicyDocumentation, fakePolicySchema } from '../../models/policy/PolicySchema.fixture';
import { fakeAllSharedPolicyGroupPolicies } from '../../models/policy/SharedPolicyGroupPolicy.fixture';
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('GioPolicyStudioPoliciesCatalogDialogComponent', () => {
let fixture: ComponentFixture<TestComponent>;
let loader: HarnessLoader;

const createTestingComponent = (apiType: ApiType, executionPhase: ExecutionPhase) => {
const createTestingComponent = (apiType: ApiType, flowPhase: FlowPhase) => {
TestBed.configureTestingModule({
declarations: [TestComponent],
imports: [
Expand Down Expand Up @@ -103,22 +103,22 @@ describe('GioPolicyStudioPoliciesCatalogDialogComponent', () => {
component = fixture.componentInstance;
component.dialogData = {
apiType,
genericPolicies: toGenericPolicies(fakeAllPolicies(), fakeAllSharedPolicyGroupPolicies()),
executionPhase,
genericPolicies: toGenericPolicies(fakeAllPolicies().map(fromPolicyInput), fakeAllSharedPolicyGroupPolicies()),
flowPhase,
trialUrl: 'https://gravitee.io/self-hosted-trial',
};
loader = TestbedHarnessEnvironment.documentRootLoader(fixture);
};

describe('When ApiType = MESSAGE and ExecutionPhase = REQUEST', () => {
describe('When ApiType = MESSAGE and FlowPhase = REQUEST', () => {
beforeEach(() => {
createTestingComponent('MESSAGE', 'REQUEST');
});

it('should display policies catalog', async () => {
await componentTestingOpenDialog();

await expectPoliciesCatalogContent('Request', p => isPolicy(p) && p.message?.includes('REQUEST'));
await expectPoliciesCatalogContent('Request', p => isPolicy(p) && p.flowPhaseCompatibility?.HTTP_MESSAGE?.includes('REQUEST'));

await expectPoliciesCatalogContent('Request', p => isSharedPolicyGroupPolicy(p) && p.apiType === 'MESSAGE' && p.phase === 'REQUEST');
});
Expand Down Expand Up @@ -194,44 +194,41 @@ describe('GioPolicyStudioPoliciesCatalogDialogComponent', () => {

const policiesCatalogDialog = await loader.getHarness(GioPolicyStudioPoliciesCatalogDialogHarness);

expect(await policiesCatalogDialog.getPoliciesName()).toHaveLength(10);
expect(await policiesCatalogDialog.getPoliciesName()).toHaveLength(11);
await policiesCatalogDialog.selectCategoryFilter('Others');
expect(await policiesCatalogDialog.getPoliciesName()).toHaveLength(4);
expect(await policiesCatalogDialog.getPoliciesName()).toHaveLength(5);

await policiesCatalogDialog.searchFilter('Policy to test UI');
expect(await policiesCatalogDialog.getPoliciesName()).toHaveLength(1);
});
});

describe('When ApiType = MESSAGE and ExecutionPhase = RESPONSE', () => {
describe('When ApiType = MESSAGE and FlowPhase = RESPONSE', () => {
beforeEach(() => {
createTestingComponent('MESSAGE', 'RESPONSE');
});

it('should display policies catalog', async () => {
await componentTestingOpenDialog();

await expectPoliciesCatalogContent('Response', p => isPolicy(p) && p.message?.includes('RESPONSE'));
await expectPoliciesCatalogContent('Response', p => isPolicy(p) && p.flowPhaseCompatibility?.HTTP_MESSAGE?.includes('RESPONSE'));
await expectPoliciesCatalogContent(
'Response',
p => isSharedPolicyGroupPolicy(p) && p.apiType === 'MESSAGE' && p.phase === 'RESPONSE',
);
});
});

describe('When ApiType = MESSAGE and ExecutionPhase = MESSAGE_REQUEST', () => {
describe('When ApiType = MESSAGE and FlowPhase = PUBLISH', () => {
beforeEach(() => {
createTestingComponent('MESSAGE', 'MESSAGE_REQUEST');
createTestingComponent('MESSAGE', 'PUBLISH');
});

it('should display policies catalog', async () => {
await componentTestingOpenDialog();

await expectPoliciesCatalogContent('Publish', p => isPolicy(p) && p.message?.includes('MESSAGE_REQUEST'));
await expectPoliciesCatalogContent(
'Publish',
p => isSharedPolicyGroupPolicy(p) && p.apiType === 'MESSAGE' && p.phase === 'MESSAGE_REQUEST',
);
await expectPoliciesCatalogContent('Publish', p => isPolicy(p) && p.flowPhaseCompatibility?.HTTP_MESSAGE?.includes('PUBLISH'));
await expectPoliciesCatalogContent('Publish', p => isSharedPolicyGroupPolicy(p) && p.apiType === 'MESSAGE' && p.phase === 'PUBLISH');
});

it('should select a policy and add it with messageCondition', async () => {
Expand Down Expand Up @@ -270,31 +267,31 @@ describe('GioPolicyStudioPoliciesCatalogDialogComponent', () => {
});
});

describe('When ApiType = MESSAGE and ExecutionPhase = MESSAGE_RESPONSE', () => {
describe('When ApiType = MESSAGE and FlowPhase = SUBSCRIBE', () => {
beforeEach(() => {
createTestingComponent('MESSAGE', 'MESSAGE_RESPONSE');
createTestingComponent('MESSAGE', 'SUBSCRIBE');
});

it('should display policies catalog', async () => {
await componentTestingOpenDialog();

await expectPoliciesCatalogContent('Subscribe', p => isPolicy(p) && p.message?.includes('MESSAGE_RESPONSE'));
await expectPoliciesCatalogContent('Subscribe', p => isPolicy(p) && p.flowPhaseCompatibility?.HTTP_MESSAGE?.includes('SUBSCRIBE'));
await expectPoliciesCatalogContent(
'Subscribe',
p => isSharedPolicyGroupPolicy(p) && p.apiType === 'MESSAGE' && p.phase === 'MESSAGE_RESPONSE',
p => isSharedPolicyGroupPolicy(p) && p.apiType === 'MESSAGE' && p.phase === 'SUBSCRIBE',
);
});
});

describe('When ApiType = PROXY and ExecutionPhase = REQUEST', () => {
describe('When ApiType = PROXY and FlowPhase = REQUEST', () => {
beforeEach(() => {
createTestingComponent('PROXY', 'REQUEST');
});

it('should display policies catalog', async () => {
await componentTestingOpenDialog();

await expectPoliciesCatalogContent('Request', p => isPolicy(p) && p.proxy?.includes('REQUEST'));
await expectPoliciesCatalogContent('Request', p => isPolicy(p) && p.flowPhaseCompatibility?.HTTP_PROXY?.includes('REQUEST'));
await expectPoliciesCatalogContent('Request', p => isSharedPolicyGroupPolicy(p) && p.apiType === 'PROXY' && p.phase === 'REQUEST');
});

Expand All @@ -309,15 +306,15 @@ describe('GioPolicyStudioPoliciesCatalogDialogComponent', () => {
});
});

describe('When ApiType = PROXY and ExecutionPhase = RESPONSE', () => {
describe('When ApiType = PROXY and FlowPhase = RESPONSE', () => {
beforeEach(() => {
createTestingComponent('PROXY', 'RESPONSE');
});

it('should display policies catalog', async () => {
await componentTestingOpenDialog();

await expectPoliciesCatalogContent('Response', p => isPolicy(p) && p.proxy?.includes('RESPONSE'));
await expectPoliciesCatalogContent('Response', p => isPolicy(p) && p.flowPhaseCompatibility?.HTTP_PROXY?.includes('RESPONSE'));
await expectPoliciesCatalogContent('Response', p => isSharedPolicyGroupPolicy(p) && p.apiType === 'PROXY' && p.phase === 'RESPONSE');
});
});
Expand All @@ -328,7 +325,7 @@ describe('GioPolicyStudioPoliciesCatalogDialogComponent', () => {
expect(await phaseDialog.getPhase()).toEqual(phaseLabel);
expect(await phaseDialog.getPoliciesName()).toStrictEqual(
expect.arrayContaining(
toGenericPolicies(fakeAllPolicies(), fakeAllSharedPolicyGroupPolicies())
toGenericPolicies(fakeAllPolicies().map(fromPolicyInput), fakeAllSharedPolicyGroupPolicies())
.filter(policiesFilter)
.map(policy => policy.name),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,25 @@ import { GioBannerModule, GioIconsModule } from '@gravitee/ui-particles-angular'
import { MatChipsModule } from '@angular/material/chips';
import { MatTooltip } from '@angular/material/tooltip';

import { ApiType, ExecutionPhase, GenericPolicy, isPolicy, isSharedPolicyGroupPolicy, Step } from '../../models';
import { ApiType, FlowPhase, GenericPolicy, isPolicy, isSharedPolicyGroupPolicy, Step } from '../../models';
import { GioPolicyStudioStepFormComponent } from '../step-form/gio-ps-step-form.component';

export type GioPolicyStudioPoliciesCatalogDialogData = {
genericPolicies: GenericPolicy[];
executionPhase: ExecutionPhase;
flowPhase: FlowPhase;
apiType: ApiType;
trialUrl?: string;
};

export type GioPolicyStudioPoliciesCatalogDialogResult = undefined | Step;

const executionPhaseLabels: Record<ExecutionPhase, string> = {
const flowPhaseLabels: Record<FlowPhase, string> = {
REQUEST: 'Request',
RESPONSE: 'Response',
MESSAGE_REQUEST: 'Publish',
MESSAGE_RESPONSE: 'Subscribe',
PUBLISH: 'Publish',
SUBSCRIBE: 'Subscribe',
CONNECT: 'Connect',
INTERACT: 'Interact',
};

type PolicyVM = {
Expand Down Expand Up @@ -78,8 +80,8 @@ type PolicyVM = {
export class GioPolicyStudioPoliciesCatalogDialogComponent implements OnDestroy {
public policies: PolicyVM[] = [];

public executionPhase!: ExecutionPhase;
public executionPhaseLabel!: string;
public flowPhase!: FlowPhase;
public flowPhaseLabel!: string;

public selectedPolicy?: GenericPolicy;

Expand All @@ -106,19 +108,23 @@ export class GioPolicyStudioPoliciesCatalogDialogComponent implements OnDestroy
public dialogRef: MatDialogRef<GioPolicyStudioPoliciesCatalogDialogComponent, GioPolicyStudioPoliciesCatalogDialogResult>,
@Inject(MAT_DIALOG_DATA) flowDialogData: GioPolicyStudioPoliciesCatalogDialogData,
) {
this.executionPhase = flowDialogData.executionPhase;
this.executionPhaseLabel = executionPhaseLabels[flowDialogData.executionPhase];
this.flowPhase = flowDialogData.flowPhase;
this.flowPhaseLabel = flowPhaseLabels[flowDialogData.flowPhase];

this.allPolicies = flowDialogData.genericPolicies
.filter(genericPolicy => {
if (isPolicy(genericPolicy)) {
if (flowDialogData.apiType === 'PROXY') {
return genericPolicy.proxy?.includes(flowDialogData.executionPhase);
return genericPolicy.flowPhaseCompatibility?.HTTP_PROXY?.includes(flowDialogData.flowPhase);
} else if (flowDialogData.apiType === 'MESSAGE') {
return genericPolicy.flowPhaseCompatibility?.HTTP_MESSAGE?.includes(flowDialogData.flowPhase);
} else {
// TODO: Implement for NATIVE API
throw new Error('Not implemented');
}
return genericPolicy.message?.includes(flowDialogData.executionPhase);
}
if (isSharedPolicyGroupPolicy(genericPolicy)) {
return genericPolicy.apiType === flowDialogData.apiType && genericPolicy.phase === flowDialogData.executionPhase;
return genericPolicy.apiType === flowDialogData.apiType && genericPolicy.phase === flowDialogData.flowPhase;
}
// Not expected
throw new Error('Unknown policy type');
Expand Down
Loading

0 comments on commit 9eab2b8

Please sign in to comment.