|
| 1 | +package software.amazon.smithy.aws.typescript.codegen; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.containsString; |
| 5 | +import static org.hamcrest.Matchers.not; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import software.amazon.smithy.build.MockManifest; |
| 9 | +import software.amazon.smithy.build.PluginContext; |
| 10 | +import software.amazon.smithy.model.Model; |
| 11 | +import software.amazon.smithy.model.node.Node; |
| 12 | +import software.amazon.smithy.typescript.codegen.TypeScriptCodegenPlugin; |
| 13 | + |
| 14 | +public class AddUserAgentDependencyTest { |
| 15 | + @Test |
| 16 | + public void addsUserAgentForAwsService() { |
| 17 | + Model model = Model.assembler() |
| 18 | + .addImport(getClass().getResource("NotSame.smithy")) |
| 19 | + .discoverModels() |
| 20 | + .assemble() |
| 21 | + .unwrap(); |
| 22 | + MockManifest manifest = new MockManifest(); |
| 23 | + PluginContext context = PluginContext.builder() |
| 24 | + .pluginClassLoader(getClass().getClassLoader()) |
| 25 | + .model(model) |
| 26 | + .fileManifest(manifest) |
| 27 | + .settings(Node.objectNodeBuilder() |
| 28 | + .withMember("service", Node.from("smithy.example#OriginalName")) |
| 29 | + .withMember("package", Node.from("example")) |
| 30 | + .withMember("packageVersion", Node.from("1.0.0")) |
| 31 | + .build()) |
| 32 | + .build(); |
| 33 | + new TypeScriptCodegenPlugin().execute(context); |
| 34 | + |
| 35 | + // Check that one of the many dependencies was added. |
| 36 | + assertThat(manifest.getFileString("package.json").get(), |
| 37 | + containsString(AwsDependency.AWS_SDK_UTIL_USER_AGENT_NODE.packageName)); |
| 38 | + |
| 39 | + // Check that both the config files were updated. |
| 40 | + assertThat(manifest.getFileString("runtimeConfig.ts").get(), containsString("defaultUserAgent")); |
| 41 | + assertThat(manifest.getFileString("runtimeConfig.ts").get(), containsString("packageInfo.version")); |
| 42 | + assertThat(manifest.getFileString("runtimeConfig.ts").get(), containsString("ClientSharedValues.serviceId")); |
| 43 | + |
| 44 | + assertThat(manifest.getFileString("runtimeConfig.browser.ts").get(), containsString("defaultUserAgent")); |
| 45 | + assertThat(manifest.getFileString("runtimeConfig.browser.ts").get(), containsString("packageInfo.version")); |
| 46 | + assertThat(manifest.getFileString("runtimeConfig.browser.ts").get(), containsString("ClientSharedValues.serviceId")); |
| 47 | + |
| 48 | + // Check that the dependency interface was updated. |
| 49 | + assertThat(manifest.getFileString("NotSameClient.ts").get(), containsString("defaultUserAgentProvider?")); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void addsUserAgentForNonAwsService() { |
| 54 | + Model model = Model.assembler() |
| 55 | + .addImport(getClass().getResource("NonAwsService.smithy")) |
| 56 | + .discoverModels() |
| 57 | + .assemble() |
| 58 | + .unwrap(); |
| 59 | + MockManifest manifest = new MockManifest(); |
| 60 | + PluginContext context = PluginContext.builder() |
| 61 | + .pluginClassLoader(getClass().getClassLoader()) |
| 62 | + .model(model) |
| 63 | + .fileManifest(manifest) |
| 64 | + .settings(Node.objectNodeBuilder() |
| 65 | + .withMember("service", Node.from("smithy.example#ExampleService")) |
| 66 | + .withMember("package", Node.from("example")) |
| 67 | + .withMember("packageVersion", Node.from("1.0.0")) |
| 68 | + .build()) |
| 69 | + .build(); |
| 70 | + new TypeScriptCodegenPlugin().execute(context); |
| 71 | + |
| 72 | + // Check that one of the many dependencies was added. |
| 73 | + assertThat(manifest.getFileString("package.json").get(), |
| 74 | + containsString(AwsDependency.AWS_SDK_UTIL_USER_AGENT_NODE.packageName)); |
| 75 | + |
| 76 | + // Check that both the config files were updated. |
| 77 | + assertThat(manifest.getFileString("runtimeConfig.ts").get(), containsString("defaultUserAgent")); |
| 78 | + assertThat(manifest.getFileString("runtimeConfig.ts").get(), containsString("packageInfo.version")); |
| 79 | + assertThat(manifest.getFileString("runtimeConfig.ts").get(), not(containsString("ClientSharedValues.serviceId"))); |
| 80 | + |
| 81 | + assertThat(manifest.getFileString("runtimeConfig.browser.ts").get(), containsString("defaultUserAgent")); |
| 82 | + assertThat(manifest.getFileString("runtimeConfig.browser.ts").get(), containsString("packageInfo.version")); |
| 83 | + assertThat(manifest.getFileString("runtimeConfig.browser.ts").get(), not(containsString("ClientSharedValues.serviceId"))); |
| 84 | + |
| 85 | + // Check that the dependency interface was updated. |
| 86 | + assertThat(manifest.getFileString("ExampleServiceClient.ts").get(), containsString("defaultUserAgentProvider?")); |
| 87 | + } |
| 88 | +} |
0 commit comments