Skip to content

Commit

Permalink
Add simple test.
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlock committed Nov 22, 2019
1 parent 52e9442 commit ad5b871
Show file tree
Hide file tree
Showing 4 changed files with 461 additions and 0 deletions.
15 changes: 15 additions & 0 deletions java/resource-overrides/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@
<version>28.1-jre</version>
</dependency>

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.skyscreamer/jsonassert -->
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>

</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public ResourceOverridesStack(final Construct parent, final String name) {
// The default child resource is called `Resource`, but secondary resources, such as
// an LaunchConfig, InstanceRole will have a different ID.
// See https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.ConstructNode.html#defaultchild
// You can see all the resources under given construct by running `cdk synth` and looking for `aws:cdk:path`
//
CfnLaunchConfiguration launchConfiguration = (CfnLaunchConfiguration) asg.getNode().findChild("LaunchConfig");
launchConfiguration.addPropertyOverride("Foo.Bar", "Hello");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package software.amazon.awscdk.examples;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import software.amazon.awscdk.core.App;
import software.amazon.awscdk.core.ConstructNode;
import software.amazon.awscdk.core.IConstruct;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.cxapi.CloudFormationStackArtifact;

import java.io.IOException;

public class ResourceOverridesStackTest {
private static final ObjectMapper JSON = new ObjectMapper();

@Test
public void shouldGenerateValidCloudFormationTemplate() throws Exception {
App app = new App();
Stack stack = new ResourceOverridesStack(app, "resource-overrides");

String actual = getStackTemplateJson(stack)
.toPrettyString();
String expected = readJsonFromResource("testResourceOverrides.expected.json")
.toPrettyString();

JSONAssert.assertEquals(expected, actual, JSONCompareMode.LENIENT);
}

private static JsonNode readJsonFromResource(String resourceName) throws IOException {
return JSON.readTree(
ResourceOverridesStackTest.class.getResource(resourceName)
);
}

private static JsonNode getStackTemplateJson(Stack stack) {
IConstruct root = stack.getNode().getRoot();
CloudFormationStackArtifact stackArtifact = ConstructNode.synth(root.getNode())
.getStackByName(stack.getStackName());

return JSON.valueToTree(stackArtifact.getTemplate());
}
}
Loading

0 comments on commit ad5b871

Please sign in to comment.