Skip to content

Commit 91a7765

Browse files
committed
add auto generated test cases
1 parent 63ac289 commit 91a7765

File tree

34 files changed

+2641
-6
lines changed

34 files changed

+2641
-6
lines changed

generate-code.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ def generate_webhook():
6161
-cp ./tools/openapi-generator-cli.jar:./generator/target/line-bot-sdk-nodejs-generator-openapi-generator-1.0.0.jar \\
6262
org.openapitools.codegen.OpenAPIGenerator \\
6363
generate \\
64-
--global-property modelDocs=false,apiDocs=false \\
64+
--global-property apiTest=false,modelDocs=false,apiDocs=false \\
6565
-g line-bot-sdk-nodejs-generator \\
6666
-o {output_path} \\
6767
-i line-openapi/{source_yaml} \\
6868
'''
6969
run_command(command)
7070
run_command(f'rm -rf lib/webhook/api/')
71+
run_command(f'rm -rf lib/webhook/tests/')
7172

7273
with open('lib/webhook/api.ts', 'w') as wfp:
7374
wfp.write("""export * from './model/models';""")

generator/src/main/java/line/bot/generator/LineBotSdkNodejsGeneratorGenerator.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.openapitools.codegen.CodegenDiscriminator;
1111
import org.openapitools.codegen.CodegenModel;
1212
import org.openapitools.codegen.CodegenOperation;
13+
import org.openapitools.codegen.CodegenParameter;
1314
import org.openapitools.codegen.CodegenType;
1415
import org.openapitools.codegen.languages.TypeScriptNodeClientCodegen;
1516
import org.openapitools.codegen.model.ModelMap;
@@ -77,6 +78,12 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap operations, L
7778
// formParam.isFile
7879
// }
7980
// if (op.getHasFormParams())
81+
for (CodegenParameter allParam : op.allParams) {
82+
// allParam.isModel
83+
// allParam.isNumber
84+
// allParam.isLong
85+
// allParam.isEnum
86+
}
8087
}
8188

8289
return operations;
@@ -174,7 +181,11 @@ protected ImmutableMap.Builder<String, Mustache.Lambda> addMustacheLambdas() {
174181
})
175182
.put("pathReplace", ((fragment, writer) -> {
176183
String text = fragment.execute();
177-
writer.write(pathReplacer(text));
184+
writer.write(pathReplacer(text, true));
185+
}))
186+
.put("pathReplaceDUMMY", ((fragment, writer) -> {
187+
String text = fragment.execute();
188+
writer.write(pathReplacer(text, false));
178189
}));
179190
}
180191

@@ -188,14 +199,20 @@ private String getEndpointFromClassName(String className) {
188199
}
189200
}
190201

191-
public static String pathReplacer(String template) {
202+
public static String pathReplacer(String template, boolean useVariable) {
192203
Pattern pattern = Pattern.compile("\\{(\\w+)\\}");
193204
Matcher matcher = pattern.matcher(template);
194205

195206
StringBuilder codeBuilder = new StringBuilder();
196207
while (matcher.find()) {
197208
String key = matcher.group(1);
198-
codeBuilder.append(".replace(\"{").append(key).append("}\", String(").append(key).append("))");
209+
codeBuilder.append(".replace(\"{").append(key).append("}\", String(");
210+
if (useVariable) {
211+
codeBuilder.append(key);
212+
} else {
213+
codeBuilder.append("\"DUMMY\"");
214+
}
215+
codeBuilder.append("))");
199216
}
200217

201218
return codeBuilder.toString();

generator/src/main/resources/line-bot-sdk-nodejs-generator/api-single.mustache

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ export class {{classname}} {
6161
{{#allParams}}
6262
* @param {{paramName}} {{description}}
6363
{{/allParams}}
64+
{{#isDeprecated}}
65+
* @deprecated
66+
{{/isDeprecated}}
67+
{{#externalDocs}}
68+
* {{description}}
69+
* @see <a href="{{url}}">{{summary}} Documentation</a>
70+
{{/externalDocs}}
6471
*/
6572
public async {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}) : Promise<{{#returnType}}{{{.}}}{{/returnType}}{{^returnType}}Types.MessageAPIResponseBase{{/returnType}}> {
6673
{{>apiBody/multipart}}
Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,101 @@
1-
// write test case here...
1+
{{#operations}}
2+
import { {{classname}} } from "../../api";
3+
{{/operations}}
4+
5+
{{#imports}}
6+
import { {{classname}} } from '../{{filename}}';
7+
{{/imports}}
8+
9+
import * as nock from "nock";
10+
import { deepEqual, equal } from "assert";
11+
12+
const pkg = require("../../../../package.json");
13+
14+
const channel_access_token = "test_channel_access_token";
15+
16+
{{#operations}}
17+
describe("{{classname}}", () => {
18+
before(() => nock.disableNetConnect());
19+
afterEach(() => nock.cleanAll());
20+
after(() => nock.enableNetConnect());
21+
22+
const client = new {{classname}}({
23+
channelAccessToken: channel_access_token,
24+
});
25+
26+
{{#operation}}
27+
it("{{nickname}}", async () => {
28+
const scope = nock("{{#lambda.endpoint}}{{classname}}{{/lambda.endpoint}}", {
29+
reqheaders: {
30+
Authorization: `Bearer ${channel_access_token}`,
31+
"User-Agent": `${pkg.name}/${pkg.version}`,
32+
},
33+
})
34+
.{{#lambda.lower}}{{{httpMethod}}}{{/lambda.lower}}((u) => u.includes("{{{path}}}"
35+
{{#allParams}}
36+
{{#isNumber}}
37+
{{=<% %>=}}
38+
.replace("{<% paramName %>}", "0") // number
39+
<%={{ }}=%>
40+
{{/isNumber}}
41+
{{#isInteger}}
42+
{{=<% %>=}}
43+
.replace("{<% paramName %>}", "0") // int
44+
<%={{ }}=%>
45+
{{/isInteger}}
46+
{{#isLong}}
47+
{{=<% %>=}}
48+
.replace("{<% paramName %>}", "0") // long
49+
<%={{ }}=%>
50+
{{/isLong}}
51+
{{#isString}}
52+
{{=<% %>=}}
53+
.replace("{<% paramName %>}", "DUMMY") // string
54+
<%={{ }}=%>
55+
{{/isString}}
56+
{{/allParams}}
57+
))
58+
.reply(200, {});
59+
60+
const res = await client.{{nickname}}(
61+
{{#allParams}}
62+
// {{paramName}}: {{dataType}}
63+
{{#isFile}}
64+
new Blob([]), // paramName={{paramName}}
65+
{{/isFile}}
66+
{{#isModel}}
67+
{} as unknown as {{{dataType}}}, // paramName={{paramName}}
68+
{{/isModel}}
69+
{{#isEnum}}
70+
"DUMMY" as unknown as {{{dataType}}}, // paramName={{paramName}}(enum)
71+
{{/isEnum}}
72+
{{^isEnum}}
73+
{{#isExplode}}
74+
"DUMMY" as unknown as {{{dataType}}}, // paramName={{paramName}}(enum)
75+
{{/isExplode}}
76+
{{^isExplode}}
77+
{{#isString}}
78+
"DUMMY", // {{paramName}}(string)
79+
{{/isString}}
80+
{{#isNumber}}
81+
0, // paramName={{paramName}}(number)
82+
{{/isNumber}}
83+
{{#isInteger}}
84+
0, // paramName={{paramName}}(integer)
85+
{{/isInteger}}
86+
{{#isLong}}
87+
0, // paramName={{paramName}}(long)
88+
{{/isLong}}
89+
{{#isBoolean}}
90+
true, // paramName={{paramName}}
91+
{{/isBoolean}}
92+
{{/isExplode}}
93+
{{/isEnum}}
94+
{{/allParams}}
95+
);
96+
equal(scope.isDone(), true);
97+
});
98+
99+
{{/operation}}
100+
});
101+
{{/operations}}

lib/channel-access-token/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ model/issueShortLivedChannelAccessTokenResponse.ts
1111
model/issueStatelessChannelAccessTokenResponse.ts
1212
model/models.ts
1313
model/verifyChannelAccessTokenResponse.ts
14+
tests/api/ChannelAccessTokenClientTest.spec.ts

lib/channel-access-token/api/channelAccessTokenClient.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ export class ChannelAccessTokenClient {
6868
* Gets all valid channel access token key IDs.
6969
* @param clientAssertionType &#x60;urn:ietf:params:oauth:client-assertion-type:jwt-bearer&#x60;
7070
* @param clientAssertion A JSON Web Token (JWT) (opens new window)the client needs to create and sign with the private key.
71+
*
72+
* @see <a href="https://developers.line.biz/en/reference/messaging-api/#get-all-valid-channel-access-token-key-ids-v2-1"> Documentation</a>
7173
*/
7274
public async getsAllValidChannelAccessTokenKeyIds(clientAssertionType: string, clientAssertion: string, ) : Promise<ChannelAccessTokenKeyIdsResponse> {
7375

@@ -94,6 +96,8 @@ export class ChannelAccessTokenClient {
9496
* @param grantType &#x60;client_credentials&#x60;
9597
* @param clientId Channel ID.
9698
* @param clientSecret Channel secret.
99+
*
100+
* @see <a href="https://developers.line.biz/en/reference/messaging-api/#issue-shortlived-channel-access-token"> Documentation</a>
97101
*/
98102
public async issueChannelToken(grantType?: string, clientId?: string, clientSecret?: string, ) : Promise<IssueShortLivedChannelAccessTokenResponse> {
99103

@@ -121,6 +125,8 @@ export class ChannelAccessTokenClient {
121125
* @param grantType client_credentials
122126
* @param clientAssertionType urn:ietf:params:oauth:client-assertion-type:jwt-bearer
123127
* @param clientAssertion A JSON Web Token the client needs to create and sign with the private key of the Assertion Signing Key.
128+
*
129+
* @see <a href="https://developers.line.biz/en/reference/messaging-api/#issue-channel-access-token-v2-1"> Documentation</a>
124130
*/
125131
public async issueChannelTokenByJWT(grantType?: string, clientAssertionType?: string, clientAssertion?: string, ) : Promise<IssueChannelAccessTokenResponse> {
126132

@@ -150,6 +156,8 @@ export class ChannelAccessTokenClient {
150156
* @param clientAssertion A JSON Web Token the client needs to create and sign with the private key of the Assertion Signing Key.
151157
* @param clientId Channel ID.
152158
* @param clientSecret Channel secret.
159+
*
160+
* @see <a href="https://developers.line.biz/en/reference/messaging-api/#issue-stateless-channel-access-token"> Documentation</a>
153161
*/
154162
public async issueStatelessChannelToken(grantType?: string, clientAssertionType?: string, clientAssertion?: string, clientId?: string, clientSecret?: string, ) : Promise<IssueStatelessChannelAccessTokenResponse> {
155163

@@ -177,6 +185,8 @@ export class ChannelAccessTokenClient {
177185
/**
178186
* Revoke short-lived or long-lived channel access token
179187
* @param accessToken Channel access token
188+
*
189+
* @see <a href="https://developers.line.biz/en/reference/messaging-api/#revoke-longlived-or-shortlived-channel-access-token"> Documentation</a>
180190
*/
181191
public async revokeChannelToken(accessToken?: string, ) : Promise<Types.MessageAPIResponseBase> {
182192

@@ -202,6 +212,8 @@ export class ChannelAccessTokenClient {
202212
* @param clientId Channel ID
203213
* @param clientSecret Channel Secret
204214
* @param accessToken Channel access token
215+
*
216+
* @see <a href="https://developers.line.biz/en/reference/messaging-api/#revoke-channel-access-token-v2-1"> Documentation</a>
205217
*/
206218
public async revokeChannelTokenByJWT(clientId?: string, clientSecret?: string, accessToken?: string, ) : Promise<Types.MessageAPIResponseBase> {
207219

@@ -227,6 +239,8 @@ export class ChannelAccessTokenClient {
227239
/**
228240
* Verify the validity of short-lived and long-lived channel access tokens
229241
* @param accessToken A short-lived or long-lived channel access token.
242+
*
243+
* @see <a href="https://developers.line.biz/en/reference/messaging-api/#verfiy-channel-access-token"> Documentation</a>
230244
*/
231245
public async verifyChannelToken(accessToken?: string, ) : Promise<VerifyChannelAccessTokenResponse> {
232246

@@ -250,6 +264,8 @@ export class ChannelAccessTokenClient {
250264
/**
251265
* You can verify whether a Channel access token with a user-specified expiration (Channel Access Token v2.1) is valid.
252266
* @param accessToken Channel access token with a user-specified expiration (Channel Access Token v2.1).
267+
*
268+
* @see <a href="https://developers.line.biz/en/reference/messaging-api/#verfiy-channel-access-token-v2-1"> Documentation</a>
253269
*/
254270
public async verifyChannelTokenByJWT(accessToken: string, ) : Promise<VerifyChannelAccessTokenResponse> {
255271

0 commit comments

Comments
 (0)