Skip to content

Commit

Permalink
add java & python snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Niranjan Jayakar committed Jun 4, 2021
1 parent 0aa8145 commit 94dfb1d
Showing 1 changed file with 144 additions and 0 deletions.
144 changes: 144 additions & 0 deletions text/0328-polyglot-assert.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,65 @@ This module allows asserting the contents of CloudFormation templates.
To run assertions based on a CDK `Stack`, start off with -

```ts
// In typescript
import { Stack } from '@aws-cdk/core';
import { TemplateAssertionsBeta1 } from '@aws-cdk/assertions';

const stack = new cdk.Stack(...)
...
const assert = TemplateAssertionsBeta1.fromStack(stack);
```

```java
// In Java
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.assertions.TemplateAssertionsBeta1;

Stack stack = new Stack(...)
...
TemplateAssertionsBeta1 assert = TemplateAssertionsBeta1.fromStack(stack);
```

```py
# In Python
from aws_cdk import (core, assertions)

stack = core.Stack(...)
...
assertion = assertions.TemplateAssertionsBeta1.fromStack(stack)
```

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

```ts
// In typescript
const template = fs.readFileSync('/path/to/template/file');
const assert = TemplateAssertionsBeta1.fromTemplate(template);
```

```java
// In Java
import com.google.commons.io.Files; // using Google Guava. Use any utility of your choice

String template = Files.readString("/path/to/template/file");
TemplateAssertionsBeta1 assert = TemplateAssertionsBeta1.fromTemplate(template);
```

```py
# In Python
with open('/path/to/template/file', 'r') as file:
template = file.readlines().join('')
...
assertion = assertions.TemplateAssertionsBeta1.fromTemplate(template)
```

#### Full Template Match

The simplest assertion would be to assert that the template matches a given
template.

```ts
// In typescript
assert.assertTemplateMatches({
Resources: {
Type: 'Foo::Bar',
Expand All @@ -67,15 +108,60 @@ assert.assertTemplateMatches({
});
```

```java
// In Java, using text blocks and Gson
import com.google.gson.Gson;

String json = """
{
"Resources": {
"Type": "Foo::Bar",
"Properties": {
"Baz": false
}
}
} """;

Map expected = new Gson().fromJson(json, Map.class);
assert.assertTemplateMatches(expected);
```

```py
# In Python
import json

expected = """
{
"Resources": {
"Type": "Foo::Bar",
"Properties": {
"Baz": false
}
}
} """

assertion.assertTemplateMatches(json.loads(expected))
```

#### Counting Resources

This module allows asserting the number of resources of a specific type found
in a template.

```ts
// In typescript
assert.assertResourceCountIs('Foo::Bar', 2);
```

```java
// In Java
assert.assertResourceCountIs("Foo::Bar", 2);
```

```py
assertion.assertResourceCountIs('Foo::Bar', 2)
```

#### Resource Matching

Beyond resource counting, the module also allows asserting that a resource with
Expand All @@ -92,6 +178,35 @@ assert.assertHasResource('Foo::Bar', {
});
```

```java
// In Java, using text blocks and Gson
import com.google.gson.Gson;

String json = """
{
"Foo": "Bar",
"Baz": 5,
"Qux": [ "Waldo", "Fred" ],
} """;

Map expected = new Gson().fromJson(json, Map.class);
assert.assertHasResource("Foo::Bar", expected);
```

```py
# in Python
import json

expected = """
{
"Foo": "Bar",
"Baz": 5,
"Qux": [ "Waldo", "Fred" ],
} """;

assertion.assertHasResource('Foo::Bar', json.loads(expected))
```

The same method allows asserting the complete definition of the 'Resource'
which can be used to verify things other sections like `DependsOn`, `Metadata`,
`DeletionProperty`, etc.
Expand All @@ -105,6 +220,35 @@ assert.assertHasResource('Foo::Bar', {
});
```

```java
// In Java, using text blocks and Gson
import com.google.gson.Gson;

String json = """
{
"Properties": { "Foo": "Bar" },
"DependsOn": [ "Waldo", "Fred" ],
} """;

Map expected = new Gson().fromJson(json, Map.class);
assert.assertHasResource("Foo::Bar", expected,
new AssertResourceOptionsBeta1.Builder().part(ResourcePartBeta1.COMPLETE).build());
```

```py
# In Python
import json

expected = """
{
"Properties": { "Foo": "Bar" },
"DependsOn": [ "Waldo", "Fred" ],
} """;

assertion.assertHasResource('Foo::Bar', json.loads(expected),
assertion.AssertResourceOptionsBeta1(part=ResourcePartBeta1.COMPLETE))
```

## FAQ

### What are we launching today?
Expand Down

0 comments on commit 94dfb1d

Please sign in to comment.