Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(assertions): rename TemplateAssertions to Template #15823

Merged
merged 2 commits into from
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/@aws-cdk/assertions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@

Functions for writing test asserting against CDK applications, with focus on CloudFormation templates.

The `TemplateAssertions` class includes a set of methods for writing assertions against CloudFormation templates. Use one of the `TemplateAssertions.fromXxx()` static methods to create an instance of this class.
The `Template` class includes a set of methods for writing assertions against CloudFormation templates. Use one of the `Template.fromXxx()` static methods to create an instance of this class.

To create `TemplateAssertions` from CDK stack, start off with:
To create `Template` from CDK stack, start off with:

```ts
import { Stack } from '@aws-cdk/core';
import { TemplateAssertions } from '@aws-cdk/assertions';
import { Template } from '@aws-cdk/assertions';

const stack = new Stack(...)
...
const assert = TemplateAssertions.fromStack(stack);
const assert = Template.fromStack(stack);
```

Alternatively, assertions can be run on an existing CloudFormation template -

```ts
const template = fs.readFileSync('/path/to/template/file');
const assert = TemplateAssertions.fromString(template);
const assert = Template.fromString(template);
```

## Full Template Match
Expand Down
14 changes: 7 additions & 7 deletions packages/@aws-cdk/assertions/lib/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import * as assert from './vendored/assert';
* Typically used, as part of unit tests, to validate that the rendered
* CloudFormation template has expected resources and properties.
*/
export class TemplateAssertions {
export class Template {

/**
* Base your assertions on the CloudFormation template synthesized by a CDK `Stack`.
* @param stack the CDK Stack to run assertions on
*/
public static fromStack(stack: Stack): TemplateAssertions {
return new TemplateAssertions(toTemplate(stack));
public static fromStack(stack: Stack): Template {
return new Template(toTemplate(stack));
}

/**
* Base your assertions from an existing CloudFormation template formatted as a
* nested set of records.
* @param template the CloudFormation template formatted as a nested set of records
*/
public static fromTemplate(template: { [key: string] : any }): TemplateAssertions {
return new TemplateAssertions(template);
public static fromTemplate(template: { [key: string] : any }): Template {
return new Template(template);
}

/**
* Base your assertions from an existing CloudFormation template formatted as a string.
* @param template the CloudFormation template in
*/
public static fromString(template: string): TemplateAssertions {
return new TemplateAssertions(JSON.parse(template));
public static fromString(template: string): Template {
return new Template(JSON.parse(template));
}

private readonly inspector: assert.StackInspector;
Expand Down
40 changes: 20 additions & 20 deletions packages/@aws-cdk/assertions/test/template.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { CfnResource, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { Match, TemplateAssertions } from '../lib';
import { Match, Template } from '../lib';

describe('TemplateAssertions', () => {
describe('Template', () => {
describe('fromString', () => {
test('default', () => {
const assertions = TemplateAssertions.fromString(`{
const assertions = Template.fromString(`{
"Resources": {
"Foo": {
"Type": "Baz::Qux",
Expand All @@ -21,7 +21,7 @@ describe('TemplateAssertions', () => {
test('fails when root is not a stage', () => {
const c = new Construct(undefined as any, '');
const stack = new Stack(c, 'MyStack');
expect(() => TemplateAssertions.fromStack(stack)).toThrow(/must be part of a Stage or an App/);
expect(() => Template.fromStack(stack)).toThrow(/must be part of a Stage or an App/);
});
});

Expand All @@ -32,7 +32,7 @@ describe('TemplateAssertions', () => {
type: 'Foo::Bar',
});

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);
inspect.resourceCountIs('Foo::Bar', 1);

expect(() => inspect.resourceCountIs('Foo::Bar', 0)).toThrow(/has 1 resource of type Foo::Bar/);
Expand All @@ -44,7 +44,7 @@ describe('TemplateAssertions', () => {
test('no resource', () => {
const stack = new Stack();

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);
inspect.resourceCountIs('Foo::Bar', 0);

expect(() => inspect.resourceCountIs('Foo::Bar', 1)).toThrow(/has 0 resource of type Foo::Bar/);
Expand All @@ -59,7 +59,7 @@ describe('TemplateAssertions', () => {
properties: { baz: 'qux' },
});

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);
inspect.templateMatches({
Resources: {
Foo: {
Expand All @@ -77,7 +77,7 @@ describe('TemplateAssertions', () => {
properties: { baz: 'qux' },
});

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);
expect(() => inspect.templateMatches({
Resources: {
Foo: {
Expand All @@ -97,7 +97,7 @@ describe('TemplateAssertions', () => {
properties: { baz: 'qux' },
});

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);
inspect.hasResource('Foo::Bar', {
Properties: { baz: 'qux' },
});
Expand All @@ -118,7 +118,7 @@ describe('TemplateAssertions', () => {
properties: { baz: ['qux', 'quy'] },
});

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);
inspect.hasResource('Foo::Bar', {
Properties: { baz: Match.arrayWith(['qux']) },
});
Expand All @@ -139,7 +139,7 @@ describe('TemplateAssertions', () => {
properties: { flob: ['qux'] },
});

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);

expectToThrow(() => {
inspect.hasResource('Foo::Bar', {
Expand All @@ -157,7 +157,7 @@ describe('TemplateAssertions', () => {
properties: { baz: 'qux', fred: 'waldo' },
});

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);
inspect.hasResource('Foo::Bar', {
Properties: Match.objectLike({ baz: 'qux' }),
});
Expand All @@ -181,7 +181,7 @@ describe('TemplateAssertions', () => {
properties: { flob: 'waldo' },
});

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);

expectToThrow(() => {
inspect.hasResource('Foo::Bar', {
Expand All @@ -199,7 +199,7 @@ describe('TemplateAssertions', () => {
properties: { baz: 'qux' },
});

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);
inspect.hasResource('Foo::Bar', {
Properties: Match.objectLike({ foo: Match.absentProperty() }),
});
Expand All @@ -215,7 +215,7 @@ describe('TemplateAssertions', () => {
properties: { baz: 'qux', fred: 'waldo' },
});

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);
expect(() => inspect.hasResource('Foo::Baz', {
Properties: Match.objectLike({ baz: 'qux' }),
})).toThrow(/No resource/);
Expand All @@ -230,7 +230,7 @@ describe('TemplateAssertions', () => {
properties: { baz: 'qux', fred: 'waldo' },
});

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);
expect(inspect.findResources('Foo::Bar')).toEqual([{
Type: 'Foo::Bar',
Properties: { baz: 'qux', fred: 'waldo' },
Expand All @@ -244,7 +244,7 @@ describe('TemplateAssertions', () => {
properties: { baz: 'qux', fred: 'waldo' },
});

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);
expect(inspect.findResources('Foo::Baz')).toEqual([]);
});

Expand All @@ -255,7 +255,7 @@ describe('TemplateAssertions', () => {
properties: { baz: 'qux', fred: 'waldo' },
});

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);
expect(inspect.findResources('Foo::Bar', {
Properties: { baz: 'qux' },
}).length).toEqual(1);
Expand All @@ -268,7 +268,7 @@ describe('TemplateAssertions', () => {
properties: { baz: 'qux', fred: 'waldo' },
});

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);
expect(inspect.findResources('Foo::Bar', {
Properties: { baz: 'waldo' },
})).toEqual([]);
Expand All @@ -279,7 +279,7 @@ describe('TemplateAssertions', () => {
new CfnResource(stack, 'Foo', { type: 'Foo::Bar' });
new CfnResource(stack, 'Bar', { type: 'Foo::Bar' });

const inspect = TemplateAssertions.fromStack(stack);
const inspect = Template.fromStack(stack);
expect(inspect.findResources('Foo::Bar').length).toEqual(2);
});
});
Expand Down
20 changes: 10 additions & 10 deletions packages/@aws-cdk/aws-cloudwatch/test/alarm.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Match, TemplateAssertions } from '@aws-cdk/assertions';
import { Match, Template } from '@aws-cdk/assertions';
import { Duration, Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { Alarm, IAlarm, IAlarmAction, Metric, MathExpression, IMetric } from '../lib';
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('Alarm', () => {
});

// THEN
TemplateAssertions.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
MetricName: 'Metric',
Expand All @@ -93,7 +93,7 @@ describe('Alarm', () => {
});

// THEN
TemplateAssertions.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
MetricName: 'Metric',
Expand All @@ -119,7 +119,7 @@ describe('Alarm', () => {
});

// THEN
TemplateAssertions.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
MetricName: 'Metric',
Expand All @@ -146,7 +146,7 @@ describe('Alarm', () => {
});

// THEN
TemplateAssertions.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
MetricName: 'Metric',
Expand All @@ -173,7 +173,7 @@ describe('Alarm', () => {
});

// THEN
TemplateAssertions.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 3,
DatapointsToAlarm: 2,
Expand Down Expand Up @@ -203,7 +203,7 @@ describe('Alarm', () => {
alarm.addOkAction(new TestAlarmAction('C'));

// THEN
TemplateAssertions.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
AlarmActions: ['A'],
InsufficientDataActions: ['B'],
OKActions: ['C'],
Expand All @@ -225,7 +225,7 @@ describe('Alarm', () => {
});

// THEN
TemplateAssertions.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
ComparisonOperator: 'GreaterThanOrEqualToThreshold',
EvaluationPeriods: 2,
MetricName: 'Metric',
Expand All @@ -250,7 +250,7 @@ describe('Alarm', () => {
});

// THEN
TemplateAssertions.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
ExtendedStatistic: 'p99.9',
});

Expand All @@ -269,7 +269,7 @@ describe('Alarm', () => {
});

// THEN
TemplateAssertions.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
Statistic: Match.absentProperty(),
ExtendedStatistic: 'tm99.9999999999',
});
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-cloudwatch/test/composite-alarm.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TemplateAssertions } from '@aws-cdk/assertions';
import { Template } from '@aws-cdk/assertions';
import { Stack } from '@aws-cdk/core';
import { Alarm, AlarmRule, AlarmState, CompositeAlarm, Metric } from '../lib';

Expand Down Expand Up @@ -59,7 +59,7 @@ describe('CompositeAlarm', () => {
alarmRule,
});

TemplateAssertions.fromStack(stack).hasResourceProperties('AWS::CloudWatch::CompositeAlarm', {
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::CompositeAlarm', {
AlarmName: 'CompositeAlarm',
AlarmRule: {
'Fn::Join': [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TemplateAssertions } from '@aws-cdk/assertions';
import { Template } from '@aws-cdk/assertions';
import { Stack } from '@aws-cdk/core';
import { Alarm, GraphWidget, IWidget, Metric } from '../lib';

Expand Down Expand Up @@ -89,7 +89,7 @@ describe('cross environment', () => {
});

// THEN
TemplateAssertions.fromStack(stack1).hasResourceProperties('AWS::CloudWatch::Alarm', {
Template.fromStack(stack1).hasResourceProperties('AWS::CloudWatch::Alarm', {
MetricName: 'ACount',
Namespace: 'Test',
Period: 300,
Expand Down Expand Up @@ -128,4 +128,4 @@ function graphMetricsAre(stack: Stack, w: IWidget, metrics: any[]) {
yAxis: {},
},
}]);
}
}
Loading