Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified generate's endpoint generate to accept array of events #226

Merged

Conversation

shudhansu-shekhar
Copy link
Contributor

@shudhansu-shekhar shudhansu-shekhar commented Jun 19, 2024

Applicable Issues

Description of the Change

In this after making this change, generate service endpoint generate accept array of events or templates

Alternate Designs

Possible Drawbacks

Sign-off

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license
indicated in the file; or

(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source
license and I have the right under that license to submit that
work with modifications, whether created in whole or in part
by me, under the same open source license (unless I am
permitted to submit under a different license), as indicated
in the file; or

(c) The contribution was provided directly to me by some other
person who certified (a), (b) or (c) and I have not modified
it.

(d) I understand and agree that this project and the contribution
are public and that a record of the contribution (including all
personal information I submit with it, including my sign-off) is
maintained indefinitely and may be redistributed consistent with
this project or the open source license(s) involved.

Signed-off-by: [email protected]

} catch (JsonSyntaxException e) {
JsonObject errorResponse = new JsonObject();
log.error("Unexpected exception caught due to parse json data", e.getMessage());
String exceptionMessage = e.getMessage();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use exceptionMessage at line 122, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

JsonObject jsonObject = bodyJson.getAsJsonObject();
JsonObject jsonObject1 = processEvent(jsonObject, msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields);
return new ResponseEntity<>(jsonObject1, HttpStatus.OK);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use better name for jsonObjec1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

} else {
errorResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value());
errorResponse.addProperty("result", "fail");
errorResponse.addProperty("message", "Invalid JSON format");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add more hints: something like "either single template or array of templates is expected".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return new ResponseEntity<>(jsonObject1, HttpStatus.OK);
} else {
errorResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value());
errorResponse.addProperty("result", "fail");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 125 states "Fail" with upper-case 'F'. Unify formatting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

errorResponse.addProperty("message", "Invalid JSON format");
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}
} catch (Exception e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use Exception, it's too general.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already handled particular exception in helper method. This one is handled like this in original as well.

parsedResponse = parser.parse(response);

if (lookupLimit <= 0) {
eventResponse.addProperty("status code", HttpStatus.BAD_REQUEST.value());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"status code" with lower-case 's'. Upper-case 's' is used everywhere else. It's good idea to define constants for common strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it similar everywhere

return new ResponseEntity<>(parsedResponse, HttpStatus.BAD_REQUEST);
eventResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value());
eventResponse.addProperty("Result", "Fail");
eventResponse.addProperty("Message", RemremGenerateServiceConstants.NOT_ACCEPTABLE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we understand what went wrong reading the error message?

Copy link
Contributor Author

@shudhansu-shekhar shudhansu-shekhar Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added extra template error constant variable in RemremGenerateServiceConstants.class Now meesage is looking more understandable

} else {
return new ResponseEntity<>(parser.parse(RemremGenerateServiceConstants.NO_SERVICE_ERROR),
HttpStatus.SERVICE_UNAVAILABLE);

}
} catch (REMGenerateException e1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why e1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}
eventResponse.addProperty("Result", "Fail");
eventResponse.add("Message", parser.parse(e1.getMessage()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if parsing fails?

log.error("Unexpected error caught", e);
eventResponse.addProperty("Status code", HttpStatus.INTERNAL_SERVER_ERROR.value());
eventResponse.addProperty("Result", "Fail");
eventResponse.addProperty("Message", RemremGenerateServiceConstants.INTERNAL_SERVER_ERROR);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the failure reason is hidden from customer?

Copy link
Contributor

@z-sztrom z-sztrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, define constants for all properties like "status code", "message", etc.

if(lookupLimit <= 0) {
return new ResponseEntity<>("LookupLimit must be greater than or equals to 1", HttpStatus.BAD_REQUEST);
if (lookupLimit <= 0) {
return new ResponseEntity<>("LookupLimit must be greater than or equals to 1", HttpStatus.valueOf(HttpStatus.BAD_REQUEST.value()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks strange: HttpStatus.valueOf(HttpStatus.BAD_REQUEST.value()). Please, simplify.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

for (int i = 0; i < results.size(); i++) {
JsonObject event = results.get(i).getAsJsonObject();
if (event.has("meta")) {
return new ResponseEntity<>(results, HttpStatus.OK);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status code cannot be determined from the first element.

return new ResponseEntity<>("LookupLimit must be greater than or equals to 1", HttpStatus.valueOf(HttpStatus.BAD_REQUEST.value()));
}

if (bodyJson.isJsonArray()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for null of bodyJson should be added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

else {
return new ResponseEntity<>(parser.parse(e1.getMessage()), HttpStatus.UNPROCESSABLE_ENTITY);
} catch (REMGenerateException e) {
if (e.getMessage().contains(Integer.toString(HttpStatus.NOT_ACCEPTABLE.value()))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dirty handling of exception. Each of the 'if's body has the same body, only some hard-coded values differ. Please, redesign handling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry but inside every if condition is doing different job and it seems there is some confusion here, it seems you provide comment on older one?

String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
parsedResponse = parser.parse(response);

if (!parsedResponse.getAsJsonObject().has(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't call the same method twice on the same target. Please, optimize.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah but where i can't find like this where you suggest?


if (msgService != null) {
String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
parsedResponse = parser.parse(response);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could parsedReseponse remain null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but how if there is response then how parsedResponse will be null?

@@ -173,7 +173,7 @@ public void testDuplicateKeyInBody() throws IOException {
.contentType("application/json")
.body(activityFinishedDuplicateKeysBody)
.when()
.post("/eiffelsemantics?msgType=EiffelActivityFinishedEvent")
.post("/eiffelsemantics?msgType=EiffelActivityFinishedEven")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 't' has been removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah sorry i just testing with this....i will do this

assertEquals(elem.getStatusCode(), HttpStatus.BAD_REQUEST);
}

@Test
public void testEiffel3FailureEventArray() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the test pass? Real events are passed to generate method instead of templates...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes....but we can use this real event?

Copy link
Contributor

@z-sztrom z-sztrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, check log messages. Unify them, ensure they have separators in front of appended values (e.g. "...URL" + url), etc.

}
}

public ResponseEntity<JsonObject> createResponseEntity(String statusCode, String statusResult, String statusMessage,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why to pass the first three values. They're always the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK


JsonObject parsedJson = parsedResponse.getAsJsonObject();

if (!parsedJson.has(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverse condition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


if (msgService != null) {
String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
parsedResponse = parser.parse(response);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parse() is marked as deprecated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we can use JsonParser() instead of this

} catch (JsonSyntaxException e) {
String exceptionMessage = e.getMessage();
log.error("Invalid JSON parse data format due to:", e.getMessage());
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_STATUS_CODE, HttpStatus.BAD_REQUEST.value());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createResponseEntity() should be used here.

HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNPROCESSABLE_ENTITY
);
for (HttpStatus status : statuses) {
if (exceptionMessage.contains(Integer.toString(status.value()))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess there should be more strict test, not just test for occurrence in message. The message is a JSON data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but whatever the test is handled by these exception error, it is same like as earlier..


JsonObject parsedJson = parsedResponse.getAsJsonObject();

if (!parsedJson.has(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import constants in order to skip class name when referencing a constant.

String exceptionMessage = e.getMessage();
log.info("duplicate key detected", exceptionMessage);
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_STATUS_RESULT, "fatal");
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD, "duplicate key detected, please check " + exceptionMessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you know that this is a duplicate key issue?

log.error("Invalid JSON parse data format due to:", e.getMessage());
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_STATUS_CODE, HttpStatus.BAD_REQUEST.value());
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_STATUS_RESULT, "fatal");
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD, "Invalid JSON parse data format due to: " + exceptionMessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No status code provided?

}
if (userInputData == null) {
log.error("Json event must not be null");
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD, "userInputData must not be null");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User doesn't know what 'userInputData' is. Use 'Input data' or something like that instead.

JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, inputJsonObject);

if (processedJson.has(RemremGenerateServiceConstants.META)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplify result handling as in line 164.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah but i checked the flow of code is like that we need the code as it is means need that service_unavailable part here that's why i not do any change regarding this in my latest push

else if (e1.getMessage().contains(Integer.toString(HttpStatus.EXPECTATION_FAILED.value()))) {
return new ResponseEntity<>(parser.parse(e1.getMessage()), HttpStatus.EXPECTATION_FAILED);
return createResponseEntity(RemremGenerateServiceConstants.JSON_STATUS_CODE, RemremGenerateServiceConstants.JSON_STATUS_RESULT, RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD,
HttpStatus.BAD_REQUEST, "Invalid JSON format,expected either single template or array of templates", "fail", errorResponse, HttpStatus.BAD_REQUEST.value());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A space is missing after comma (,).

if (!parsedJson.has(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD)) {
return parsedJson;
} else {
eventResponse.addProperty(RemremGenerateServiceConstants.JSON_STATUS_CODE, HttpStatus.BAD_REQUEST.value());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use createResponse() method. Please, check if the method can be used at other places.

log.error("Invalid JSON parse data format due to:", e.getMessage());
return createResponseEntity(HttpStatus.BAD_REQUEST,"Invalid JSON parse data format due to: " + exceptionMessage, "fatal",
errorResponse);
} catch (JsonProcessingException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think JsonSyntaxException and JsonProcessingException can be handled together. No need to distinguish between them.

} catch (JsonProcessingException e) {
String exceptionMessage = e.getMessage();
log.info("Incorrect Json data", exceptionMessage);
return createResponseEntity(HttpStatus.BAD_REQUEST,"Incorrect Json data: " + exceptionMessage, "fatal",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Json -> JSON

errorResponse);
} catch (JsonProcessingException e) {
String exceptionMessage = e.getMessage();
log.info("Incorrect Json data", exceptionMessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be log.error

if(lookupLimit <= 0) {
return new ResponseEntity<>("LookupLimit must be greater than or equals to 1", HttpStatus.BAD_REQUEST);
if (lookupLimit <= 0) {
return new ResponseEntity<>("LookupLimit must be greater than or equals to 1", HttpStatus.BAD_REQUEST);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should createResponseEntity be used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think here we need, here we just need to return one response not as many like in other scenerio

}
if (inputData == null) {
log.error("Json event must not be null");
errorResponse.addProperty(JSON_ERROR_MESSAGE_FIELD, "inputData must not be null");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should createResponseEntity be used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here as well

JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, inputJsonObject);

if (processedJson.has(META)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle result as inside loop at line 162.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not getting clearly here,

return new ResponseEntity<>(parser.parse(RemremGenerateServiceConstants.NO_SERVICE_ERROR),
HttpStatus.SERVICE_UNAVAILABLE);
return createResponseEntity(HttpStatus.BAD_REQUEST, "Invalid JSON format,expected either single template or array of templates",
"fail", errorResponse);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a constant for "fail".

for (HttpStatus status : statuses) {
if (exceptionMessage.contains(Integer.toString(status.value()))) {
return createResponseEntity(
status, e.getMessage(), "fail", errorResponse);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use constant for "fail". The same for lines 216 and 219.

JsonObject parsedJson = parsedResponse.getAsJsonObject();

if (parsedJson.has(JSON_ERROR_MESSAGE_FIELD)) {
createResponseEntity(HttpStatus.BAD_REQUEST, "fail", TEMPLATE_ERROR, eventResponse);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use constants for "fail".

}
return eventResponse;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot return an empty JSON object. Situation when msgService == null must be handled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah right we can handle this in same as original one means the how it handle earlier right?

else {
return new ResponseEntity<>(parser.parse(RemremGenerateServiceConstants.NO_SERVICE_ERROR),
HttpStatus.SERVICE_UNAVAILABLE);
}

lookupLimit, okToLeaveOutInvalidOptionalFields, inputJson);
} catch (JsonSyntaxException | JsonProcessingException e) {
String exceptionMessage = e.getMessage();
log.error("Invalid JSON parse data format due to:", e.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove ':'.

* taking message type and json body as input
* Here we basically add this to handle if inputData is of jsonArray type as well
*
* <p>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why two '

'?

if (inputData == null) {
createResponseEntity(HttpStatus.BAD_REQUEST, JSON_ERROR_STATUS,
"Parameter 'inputData' must not be null", errorResponse);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why to continue when inputData is null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return new ResponseEntity<>(processedJson, status);
} else if (processedJson.has(JSON_STATUS_CODE)) {
String statusValue = processedJson.get(JSON_STATUS_CODE).toString();
status = HttpStatus.resolve(Integer.parseInt(statusValue));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseInt can throw NumberFormatException and that should be handled here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

log.error("Unexpected exception caught", e);
return new ResponseEntity<>(parser.parse(RemremGenerateServiceConstants.INTERNAL_SERVER_ERROR),
HttpStatus.INTERNAL_SERVER_ERROR);
return createResponseEntity(HttpStatus.BAD_REQUEST, e.getMessage(), JSON_ERROR_STATUS, errorResponse);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why e.getMessage(), it's been stored in exceptionMessage(), the same line 251.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

* @param errorResponse is to collect all the responses here.
* @return ResponseEntity
*/
public ResponseEntity<JsonObject> createResponseEntity(HttpStatus status, String responseMessage, String resultMessage,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method should be also available in form createResponseEntity(HttpStatus, String, String) because errorResponse is usually passed as an empty object. Thus, it can be created internally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think it is necessary here because we need to store error many responses in one jsonobject , so we need this externally here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, dont' forget about this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

lookupLimit, okToLeaveOutInvalidOptionalFields, inputJson);
} catch (JsonSyntaxException | JsonProcessingException e) {
String exceptionMessage = e.getMessage();
log.error("Invalid JSON parse data format due to", e.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use exceptionMessage instead of e.getMessage().

@@ -199,7 +196,7 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
"Invalid JSON format,expected either single template or array of templates",
JSON_ERROR_STATUS, errorResponse);
}
} catch (REMGenerateException | JsonSyntaxException e) {
} catch (REMGenerateException | JsonSyntaxException | NumberFormatException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NumberFormatException cannot be handled that way. It's something completely different then the other ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok we can handle sperately like this right?

try {
status = HttpStatus.resolve(Integer.parseInt(statusValue));
return new ResponseEntity<>(processedJson, status);
} catch (NumberFormatException e){
return new ResponseEntity<>(e.getMessage(),HttpStatus.BAD_REQUEST);
}

* @param errorResponse is to collect all the responses here.
* @return ResponseEntity
*/
public ResponseEntity<JsonObject> createResponseEntity(HttpStatus status, String responseMessage, String resultMessage,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, dont' forget about this one.

Copy link
Contributor

@z-sztrom z-sztrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all newly added methods are documented.

String exceptionMessage = e.getMessage();
log.error("Invalid JSON parse data format due to", e.getMessage());
return createResponseEntity(HttpStatus.BAD_REQUEST, "Invalid JSON parse data format due to: "
+ exceptionMessage, "fatal");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should a constant be used instead of "fatal"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return new ResponseEntity<>(parser.parse(e1.getMessage()), HttpStatus.UNPROCESSABLE_ENTITY);
} catch (NumberFormatException e){
String exceptionMessage = e.getMessage();
log.error("Invalid number format", exceptionMessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really confusing for user... What would you do if you were user?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can logged like this?

log.error("Invalid number, please enter a valid status code value", exceptionMessage);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really confusing for user... What would you do if you were user?

we can logged like this?

log.error("Invalid number, please enter a valid status code value", exceptionMessage);

} else if (e instanceof JsonSyntaxException) {
log.error("Failed to parse JSON", exceptionMessage);
return createResponseEntity(HttpStatus.BAD_REQUEST, exceptionMessage, JSON_ERROR_STATUS);
} /*else if (e instanceof NumberFormatException) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

} else {
return new ResponseEntity<>(parsedResponse, HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(processedJson, HttpStatus.SERVICE_UNAVAILABLE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This strange state should be logged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah we can logged like this?

log.error("Unexpected error caught, due to service property is missing or wrong");

String exceptionMessage = e.getMessage();
if (e instanceof REMGenerateException) {
List<HttpStatus> statuses = List.of(
HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNPROCESSABLE_ENTITY
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split the line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

status = HttpStatus.resolve(Integer.parseInt(statusValue));
return new ResponseEntity<>(processedJson, status);
} catch (NumberFormatException e) {
log.error("Invalid status value: '" + statusValue + "' of response " + processedJson);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why to compose the same string twice? Store it in a variable and reuse it. The same 195 and 196.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do like this?

else {
String errorMessage = "There is no status value in the response " + processedJson;
log.error(errorMessage);
return createResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR, errorMessage, JSON_ERROR_STATUS);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's perfect solution.

} else {
return new ResponseEntity<>(parsedResponse, HttpStatus.BAD_REQUEST);
log.error("There is no status value: '" + statusValue + "' in the response" + processedJson);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a space after 'response'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a space after 'response'.

OK

return new ResponseEntity<>(parsedResponse, HttpStatus.BAD_REQUEST);
log.error("There is no status value: '" + statusValue + "' in the response" + processedJson);
return createResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR, "There is no status value: '"
+ statusValue + "' in the response " + processedJson, JSON_ERROR_STATUS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

statusValue is always null here. You should move its declaration from 180 to 185.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Copy link
Contributor Author

@shudhansu-shekhar shudhansu-shekhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For your two specific comment, we can do like this:

catch (NumberFormatException e) {
String errorMessage = "Invalid status value: '" + statusValue + "' of response " + processedJson;
log.error(errorMessage);
return createResponseEntity(HttpStatus.BAD_REQUEST, err, JSON_ERROR_STATUS);
}
} else {
String errorMessage = "There is no status value in the response " + processedJson;
log.error(errorMessage);
return createResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR, errorMessage, JSON_ERROR_STATUS);
}

else {
return new ResponseEntity<>(parser.parse(e1.getMessage()), HttpStatus.UNPROCESSABLE_ENTITY);
} catch (REMGenerateException e) {
if (e.getMessage().contains(Integer.toString(HttpStatus.NOT_ACCEPTABLE.value()))) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry but inside every if condition is doing different job and it seems there is some confusion here, it seems you provide comment on older one?


if (msgService != null) {
String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
parsedResponse = parser.parse(response);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but how if there is response then how parsedResponse will be null?

String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
parsedResponse = parser.parse(response);

if (!parsedResponse.getAsJsonObject().has(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah but where i can't find like this where you suggest?

@@ -173,7 +173,7 @@ public void testDuplicateKeyInBody() throws IOException {
.contentType("application/json")
.body(activityFinishedDuplicateKeysBody)
.when()
.post("/eiffelsemantics?msgType=EiffelActivityFinishedEvent")
.post("/eiffelsemantics?msgType=EiffelActivityFinishedEven")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah sorry i just testing with this....i will do this

assertEquals(elem.getStatusCode(), HttpStatus.BAD_REQUEST);
}

@Test
public void testEiffel3FailureEventArray() throws Exception {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes....but we can use this real event?

String exceptionMessage = e.getMessage();
if (e instanceof REMGenerateException) {
List<HttpStatus> statuses = List.of(
HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNPROCESSABLE_ENTITY
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

String exceptionMessage = e.getMessage();
log.error("Invalid JSON parse data format due to", e.getMessage());
return createResponseEntity(HttpStatus.BAD_REQUEST, "Invalid JSON parse data format due to: "
+ exceptionMessage, "fatal");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

} else {
return new ResponseEntity<>(parsedResponse, HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(processedJson, HttpStatus.SERVICE_UNAVAILABLE);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah we can logged like this?

log.error("Unexpected error caught, due to service property is missing or wrong");

return new ResponseEntity<>(parser.parse(e1.getMessage()), HttpStatus.UNPROCESSABLE_ENTITY);
} catch (NumberFormatException e){
String exceptionMessage = e.getMessage();
log.error("Invalid number format", exceptionMessage);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can logged like this?

log.error("Invalid number, please enter a valid status code value", exceptionMessage);

return new ResponseEntity<>(parser.parse(e1.getMessage()), HttpStatus.UNPROCESSABLE_ENTITY);
} catch (NumberFormatException e){
String exceptionMessage = e.getMessage();
log.error("Invalid number format", exceptionMessage);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really confusing for user... What would you do if you were user?

we can logged like this?

log.error("Invalid number, please enter a valid status code value", exceptionMessage);

Copy link
Contributor Author

@shudhansu-shekhar shudhansu-shekhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check my comment

* @param errorResponse is to collect all the responses here.
* @return ResponseEntity
*/
public ResponseEntity<JsonObject> createResponseEntity(HttpStatus status, String responseMessage, String resultMessage,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@z-sztrom z-sztrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add some examples of multiple responses, so user can see how to submit request and what kind of response can be expected.

| 422 | Unprocessable Entity | Link specific lookup options could not be fulfilled | Is returned if Link specific lookup options could not be matched with failIfMultipleFound and failIfNoneFound. |
| 500 | Internal Server Error | Internal server error | When REMReM Generate is down, possible to try again later when server is up |
| 503 | Service Unavailable | "No protocol service has been found registered" | When specified message protocol is not loaded |
| 207 | Multi Status | Multi-Status | Is returned when there's a partial success, i.e. some events failed, some successfully generated/published, return status should be 207. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, move after 200 status.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

| 422 | Unprocessable Entity | Link specific lookup options could not be fulfilled | Is returned if Link specific lookup options could not be matched with failIfMultipleFound and failIfNoneFound. |
| 500 | Internal Server Error | Internal server error | When REMReM Generate is down, possible to try again later when server is up |
| 503 | Service Unavailable | "No protocol service has been found registered" | When specified message protocol is not loaded |
| 207 | Multi Status | Multi-Status | Is returned when there's a partial success, i.e. some events failed, some successfully generated/published, return status should be 207. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is generate service, don't use generate/publish here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -139,7 +139,133 @@ Status codes are generated according to the below table.
| 503 | Service Unavailable | "No protocol service has been found registered" | When specified message protocol is not loaded |
| 207 | Multi Status | Multi-Status | Is returned when there's a partial success, i.e. some events failed, some successfully generated/published, return status should be 207. |

### Examples:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, provide responses, so user can see how the outputs are structured.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -139,7 +139,133 @@ Status codes are generated according to the below table.
| 503 | Service Unavailable | "No protocol service has been found registered" | When specified message protocol is not loaded |
| 207 | Multi Status | Multi-Status | Is returned when there's a partial success, i.e. some events failed, some successfully generated/published, return status should be 207. |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, move 207 after 200 status.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -139,6 +139,10 @@ Status codes are generated according to the below table.
| 503 | Service Unavailable | "No protocol service has been found registered" | When specified message protocol is not loaded |
| 207 | Multi Status | Multi-Status | Is returned when there's a partial success, i.e. some events failed, some successfully generated/published, return status should be 207. |


NOTE: In the array of event if any of the event have not correct or valid JSON format,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove 'correct or'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
]
},
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, fix the indentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

@shudhansu-shekhar shudhansu-shekhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix all the suggestion or comment as well and push the changes. Please check the comment

@@ -139,6 +139,10 @@ Status codes are generated according to the below table.
| 503 | Service Unavailable | "No protocol service has been found registered" | When specified message protocol is not loaded |
| 207 | Multi Status | Multi-Status | Is returned when there's a partial success, i.e. some events failed, some successfully generated/published, return status should be 207. |


NOTE: In the array of event if any of the event have not correct or valid JSON format,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -139,6 +139,10 @@ Status codes are generated according to the below table.
| 503 | Service Unavailable | "No protocol service has been found registered" | When specified message protocol is not loaded |
| 207 | Multi Status | Multi-Status | Is returned when there's a partial success, i.e. some events failed, some successfully generated/published, return status should be 207. |


NOTE: In the array of event if any of the event have not correct or valid JSON format,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

@shudhansu-shekhar shudhansu-shekhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, please check comment

Copy link
Contributor

@z-sztrom z-sztrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, tests passed.

@shudhansu-shekhar shudhansu-shekhar merged commit a870ce4 into eiffel-community:master Aug 14, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants