Skip to content

Commit

Permalink
[command][frisby] fix frisby skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Mar 19, 2024
1 parent c413b2d commit e54477f
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions src/main/java/io/yupiik/hcms/cli/GenerateFrisbySkeleton.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private String generate(final Entity entity, final Collection<Model.JsonRpcMetho
.sorted()
.collect(joining());

return "const { jsonrpcUrl, frisby, Joi /* , login */ } = require('../../env');\n" + "\n"
return "const { jsonRpc, Joi /* , frisby, login, jsonrpcUrl */ } = require('../../env');\n\n"
+ "describe('"
+ entity.name() + " entity', () => {\n"
+ " // IMPORTANT: assumes the entity has no initial provisioning,\n"
Expand All @@ -139,13 +139,13 @@ private String generate(final Entity entity, final Collection<Model.JsonRpcMetho
? findByIdMissing(entity, ids, properties)
: "")
+ (create ? create(entity, createData) : "")
+ (create && (crud || methods.contains(Model.JsonRpcMethodType.FIND_BY_ID))
? findById(entity, idValues, createData)
: "")
+ (create && (crud || methods.contains(Model.JsonRpcMethodType.UPDATE))
? update(entity, ids, idValues, properties)
: "")
+ (create && (crud || methods.contains(Model.JsonRpcMethodType.FIND_ALL)) ? findAllPage1(entity) : "")
+ (create && (crud || methods.contains(Model.JsonRpcMethodType.FIND_BY_ID))
? findById(entity, idValues, createData)
: "")
+ (create && (crud || methods.contains(Model.JsonRpcMethodType.FIND_ALL))
? deleteById(entity, idValues)
: "");
Expand All @@ -165,7 +165,7 @@ private String update(
.sorted()
.collect(joining());
return "\n" + " it('"
+ entity.name() + ".update', () => frisby\n" + " .jsonRpc({\n"
+ entity.name() + ".update', () => jsonRpc({\n"
+ " jsonrpc: '2.0',\n"
+ " method: '"
+ entity.name() + ".update',\n" + " params: {\n"
Expand All @@ -175,15 +175,17 @@ private String update(
+ " .expect('status', 200)\n"
+ " .expectNot('json', 'error')\n"
+ " .expect('json', {\n"
+ " result: {\n"
+ updateData
+ " },\n"
+ " })\n"
+ " .expect('jsonTypes', 'result', schema));\n";
}

private String create(final Entity entity, final String createData) {
return "\n" + " let created;\n"
+ " it('"
+ entity.name() + ".create', () => frisby\n" + " .jsonRpc({\n"
+ entity.name() + ".create', () => jsonRpc({\n"
+ " jsonrpc: '2.0',\n"
+ " method: '"
+ entity.name() + ".create',\n" + " params: {\n"
Expand All @@ -199,14 +201,14 @@ private String create(final Entity entity, final String createData) {
+ " .expect('json', {\n"
+ " result: {\n"
+ createData
+ " }\n"
+ " },\n"
+ " })\n"
+ " .expect('jsonTypes', 'result', schema));\n";
}

private String deleteById(final Entity entity, final String idValues) {
return "\n" + " it('"
+ entity.name() + ".deleteById', () => frisby\n" + " .jsonRpc({\n"
+ entity.name() + ".deleteById', () => jsonRpc({\n"
+ " jsonrpc: '2.0',\n"
+ " method: '"
+ entity.name() + ".deleteById',\n" + " params: {\n"
Expand All @@ -222,7 +224,7 @@ private String deleteById(final Entity entity, final String idValues) {
private String findByIdMissing(
final Entity entity, final Collection<String> ids, final Map<String, Model.JsonSchema> properties) {
return "\n" + " it('"
+ entity.name() + ".findById not found', () => frisby\n" + " .jsonRpc({\n"
+ entity.name() + ".findById not found', () => jsonRpc({\n"
+ " jsonrpc: '2.0',\n"
+ " method: '"
+ entity.name() + ".findById',\n" + " params: {\n"
Expand All @@ -246,7 +248,7 @@ private String findByIdMissing(

private String findById(final Entity entity, final String idValues, final String createData) {
return "\n" + " it('"
+ entity.name() + ".findById not found', () => frisby\n" + " .jsonRpc({\n"
+ entity.name() + ".findById found', () => jsonRpc({\n"
+ " jsonrpc: '2.0',\n"
+ " method: '"
+ entity.name() + ".findById',\n" + " params: {\n"
Expand All @@ -256,13 +258,15 @@ private String findById(final Entity entity, final String idValues, final String
+ " .expect('status', 200)\n"
+ " .expectNot('json', 'error')\n"
+ " .expect('json', {\n"
+ " result: {\n"
+ createData
+ " })\n";
+ " },\n"
+ " }));\n";
}

private String findAllPage1(final Entity entity) {
return "\n" + " it('"
+ entity.name() + ".findAll page 1', () => frisby\n" + " .jsonRpc({\n"
+ entity.name() + ".findAll page 1', () => jsonRpc({\n"
+ " jsonrpc: '2.0',\n"
+ " method: '"
+ entity.name() + ".findAll',\n" + " params: {\n"
Expand All @@ -278,7 +282,7 @@ private String findAllPage1(final Entity entity) {

private String findAllEmpty(final Entity entity) {
return "\n" + " it('"
+ entity.name() + ".findAll empty', () => frisby\n" + " .jsonRpc({\n"
+ entity.name() + ".findAll empty', () => jsonRpc({\n"
+ " jsonrpc: '2.0',\n"
+ " method: '"
+ entity.name() + ".findAll',\n" + " params: {\n"
Expand Down Expand Up @@ -378,6 +382,18 @@ The default enables to use HCMS binary (native for linux) but you can switch to
const jsonrpcUrl = globalThis.HCMS_URL;
const jsonRpc = body => frisby
.post(jsonrpcUrl, { body: JSON.stringify(body) })
.then(res => addMsg({
message: JSON.stringify({
request: body,
response: {
status: res.status,
json: res.json,
},
}, null, 2),
}).then(() => res));
const cachedTokens = {};
async function login(params) {
const key = `${params.username}:${params.password}`;
Expand All @@ -386,8 +402,8 @@ async function login(params) {
return Promise.resolve(existing);
}
return frisby
.jsonRpc({
return jsonRpc(
{
jsonrpc: '2.0',
method: 'hcms.security.login',
params,
Expand All @@ -402,20 +418,8 @@ async function login(params) {
module.exports = {
jsonrpcUrl,
login,
frisby: {
...frisby,
jsonRpc: body => frisby
.jsonRpc({ body: JSON.stringify(body) })
.then(res => addMsg({
message: JSON.stringify({
request: body,
response: {
status: res.status,
json: res.json,
},
}, null, 2),
}).then(() => res)),
},
frisby,
jsonRpc,
Joi: frisby.Joi,
};
""");
Expand Down

0 comments on commit e54477f

Please sign in to comment.