30
30
31
31
public class AddHttpApiKeyAuthPluginTest {
32
32
@ Test
33
- public void httpApiKeyAuthClient () {
34
- MockManifest manifest = new MockManifest ();
35
- PluginContext context = PluginContext .builder ()
36
- .pluginClassLoader (getClass ().getClassLoader ())
37
- .model (Model .assembler ()
38
- .addImport (getClass ().getResource ("http-api-key-auth-trait.smithy" ))
39
- .discoverModels ()
40
- .assemble ()
41
- .unwrap ())
42
- .fileManifest (manifest )
43
- .settings (Node .objectNodeBuilder ()
44
- .withMember ("service" , Node .from ("smithy.example#Example" ))
45
- .withMember ("package" , Node .from ("example" ))
46
- .withMember ("packageVersion" , Node .from ("1.0.0" ))
47
- .build ())
48
- .build ();
49
-
50
- new TypeScriptCodegenPlugin ().execute (context );
51
-
52
- // Ensure that the client imports the config properly and calls the resolve function.
53
- assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/ExampleClient.ts" ).get (),
54
- containsString ("from \" ./middleware/HttpApiKeyAuth\" " ));
55
- assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/ExampleClient.ts" ).get (),
56
- containsString ("= resolveHttpApiKeyAuthConfig(" ));
57
-
58
- // Ensure that the GetFoo operation imports the middleware and uses it with all the options.
59
- assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/commands/GetFooCommand.ts" ).get (),
60
- containsString ("from \" ../middleware/HttpApiKeyAuth\" " ));
61
- assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/commands/GetFooCommand.ts" ).get (),
62
- containsString ("this.middlewareStack.use(getHttpApiKeyAuthPlugin(configuration, { scheme: 'ApiKey', "
63
- + "in: 'header', name: 'Authorization'}));" ));
64
-
65
- // Ensure that the GetBar operation does not import the middleware or use it.
66
- assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/commands/GetBarCommand.ts" ).get (),
67
- not (containsString ("from \" ../middleware/HttpApiKeyAuth\" " )));
68
- assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/commands/GetBarCommand.ts" ).get (),
69
- not (containsString ("this.middlewareStack.use(getHttpApiKeyAuthPlugin" )));
33
+ public void httpApiKeyAuthClientOnService () {
34
+ testInjects ("http-api-key-auth-trait.smithy" , ", { scheme: 'ApiKey', in: 'header', name: 'Authorization'}" );
35
+ }
70
36
71
- // Make sure that the middleware file was written and exports the plugin symbol.
72
- assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/middleware/HttpApiKeyAuth/index.ts" ).get (),
73
- containsString ("export const getHttpApiKeyAuthPlugin" ));
37
+ @ Test
38
+ public void httpApiKeyAuthClientOnOperation () {
39
+ testInjects ("http-api-key-auth-trait-on-operation.smithy" ,
40
+ ", { scheme: 'ApiKey', in: 'header', name: 'Authorization'}" );
74
41
}
75
42
76
43
// This should be identical to the httpApiKeyAuthClient test except for the parameters provided
77
44
// to the middleware.
78
45
@ Test
79
46
public void httpApiKeyAuthClientNoScheme () {
80
- MockManifest manifest = new MockManifest ();
81
- PluginContext context = PluginContext .builder ()
82
- .pluginClassLoader (getClass ().getClassLoader ())
83
- .model (Model .assembler ()
84
- .addImport (getClass ().getResource ("http-api-key-auth-trait-no-scheme.smithy" ))
85
- .discoverModels ()
86
- .assemble ()
87
- .unwrap ())
88
- .fileManifest (manifest )
89
- .settings (Node .objectNodeBuilder ()
90
- .withMember ("service" , Node .from ("smithy.example#Example" ))
91
- .withMember ("package" , Node .from ("example" ))
92
- .withMember ("packageVersion" , Node .from ("1.0.0" ))
93
- .build ())
94
- .build ();
47
+ testInjects ("http-api-key-auth-trait-no-scheme.smithy" , ", { in: 'header', name: 'Authorization'}" );
48
+ }
95
49
96
- new TypeScriptCodegenPlugin ().execute (context );
50
+ private void testInjects (String filename , String extra ) {
51
+ MockManifest manifest = generate (filename );
97
52
98
53
// Ensure that the client imports the config properly and calls the resolve function.
99
54
assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/ExampleClient.ts" ).get (),
@@ -105,8 +60,7 @@ public void httpApiKeyAuthClientNoScheme() {
105
60
assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/commands/GetFooCommand.ts" ).get (),
106
61
containsString ("from \" ../middleware/HttpApiKeyAuth\" " ));
107
62
assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/commands/GetFooCommand.ts" ).get (),
108
- containsString ("this.middlewareStack.use(getHttpApiKeyAuthPlugin(configuration, { "
109
- + "in: 'header', name: 'Authorization'}));" ));
63
+ containsString ("this.middlewareStack.use(getHttpApiKeyAuthPlugin(configuration" + extra + "));" ));
110
64
111
65
// Ensure that the GetBar operation does not import the middleware or use it.
112
66
assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/commands/GetBarCommand.ts" ).get (),
@@ -119,13 +73,13 @@ public void httpApiKeyAuthClientNoScheme() {
119
73
containsString ("export const getHttpApiKeyAuthPlugin" ));
120
74
}
121
75
122
- @ Test
123
- public void notAnHttpApiKeyAuthClient () {
76
+ private MockManifest generate ( String filename )
77
+ {
124
78
MockManifest manifest = new MockManifest ();
125
79
PluginContext context = PluginContext .builder ()
126
80
.pluginClassLoader (getClass ().getClassLoader ())
127
81
.model (Model .assembler ()
128
- .addImport (getClass ().getResource ("endpoint-trait.smithy" ))
82
+ .addImport (getClass ().getResource (filename ))
129
83
.discoverModels ()
130
84
.assemble ()
131
85
.unwrap ())
@@ -139,43 +93,25 @@ public void notAnHttpApiKeyAuthClient() {
139
93
140
94
new TypeScriptCodegenPlugin ().execute (context );
141
95
142
- // Make sure that the config and middleware were not added to the client.
143
- assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/ExampleClient.ts" ).get (),
144
- not (containsString ("= resolveHttpApiKeyAuthConfig(" )));
145
- assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/ExampleClient.ts" ).get (),
146
- not (containsString ("from \" ./middleware/HttpApiKeyAuth\" " )));
147
-
148
- // Make sure that the import and middleware were not used in the operation.
149
- assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/commands/GetFooCommand.ts" ).get (),
150
- not (containsString ("from \" ../middleware/HttpApiKeyAuth\" " )));
151
- assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/commands/GetFooCommand.ts" ).get (),
152
- not (containsString ("this.middlewareStack.use(getHttpApiKeyAuthPlugin(configuration" )));
96
+ return manifest ;
97
+ }
153
98
154
- // Make sure that the middleware file was not written.
155
- assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/middleware/HttpApiKeyAuth/index.ts" )
156
- .isPresent (),
157
- is (false ));
99
+ @ Test
100
+ public void notAnHttpApiKeyAuthClient () {
101
+ testDoesNotInject ("endpoint-trait.smithy" );
158
102
}
159
103
160
104
@ Test
161
105
public void httpApiKeyAuthClientWithAllOptionalAuthOperations () {
162
- MockManifest manifest = new MockManifest ();
163
- PluginContext context = PluginContext .builder ()
164
- .pluginClassLoader (getClass ().getClassLoader ())
165
- .model (Model .assembler ()
166
- .addImport (getClass ().getResource ("http-api-key-auth-trait-unused.smithy" ))
167
- .discoverModels ()
168
- .assemble ()
169
- .unwrap ())
170
- .fileManifest (manifest )
171
- .settings (Node .objectNodeBuilder ()
172
- .withMember ("service" , Node .from ("smithy.example#Example" ))
173
- .withMember ("package" , Node .from ("example" ))
174
- .withMember ("packageVersion" , Node .from ("1.0.0" ))
175
- .build ())
176
- .build ();
106
+ testDoesNotInject ("http-api-key-auth-trait-all-optional.smithy" );
107
+ }
108
+ @ Test
109
+ public void httpApiKeyAuthClientWithNoAuthTraits () {
110
+ testDoesNotInject ("http-api-key-auth-trait-unused.smithy" );
111
+ }
177
112
178
- new TypeScriptCodegenPlugin ().execute (context );
113
+ private void testDoesNotInject (String filename ) {
114
+ MockManifest manifest = generate (filename );
179
115
180
116
// Make sure that the config and middleware were not added to the client.
181
117
assertThat (manifest .getFileString (CodegenUtils .SOURCE_FOLDER + "/ExampleClient.ts" ).get (),
0 commit comments