Skip to content

Update generate endpoint for gen1 template #231

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

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.ericsson.eiffel.remrem.generate.config.ErLookUpConfig;
import com.ericsson.eiffel.remrem.generate.constants.RemremGenerateServiceConstants;
import com.ericsson.eiffel.remrem.generate.exception.ProtocolHandlerNotFoundException;
import com.ericsson.eiffel.remrem.generate.exception.REMGenerateException;
import com.ericsson.eiffel.remrem.protocol.MsgService;
import com.fasterxml.jackson.core.JsonFactory;
Expand Down Expand Up @@ -168,46 +169,50 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
"The number of events in the input array is too high: " + inputEventJsonArray.size() + " > "
+ maxSizeOfInputArray + "; you can modify the property 'maxSizeOfInputArray' to increase it.");
}
int successCount = 0;
int failedCount = 0;
for (JsonElement element : inputEventJsonArray) {
JsonObject generatedEvent = (processEvent(msgProtocol, msgType,
failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit,
okToLeaveOutInvalidOptionalFields, element.getAsJsonObject()));
generatedEventResults.add(generatedEvent);
}
boolean allSuccess = true;
boolean partialSuccess = false;
for (JsonElement result : generatedEventResults) {
JsonObject jsonObject = result.getAsJsonObject();
allSuccess &= jsonObject.has(META);
partialSuccess |= jsonObject.has(META);
try {
JsonObject generatedEvent = (processEvent(msgProtocol, msgType,
failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit,
okToLeaveOutInvalidOptionalFields, element.getAsJsonObject()));
generatedEventResults.add(generatedEvent);
successCount++;
} catch (ProtocolHandlerNotFoundException e) {
// Rethrow the exception. All events in array use the same protocol. If it's handler
// cannot be found for one event, the others will fail, too.
throw e;
} catch (REMGenerateException e) {
// Something went wrong. Add failure description to array of results.
/* partialSuccess = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

remove unused code

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

allSuccess=false;*/
failedCount++;
JsonObject response = new JsonObject();
createResponseEntity(HttpStatus.BAD_REQUEST, e.getMessage(), JSON_ERROR_STATUS, response);
generatedEventResults.add(response);
}
}
HttpStatus eventStatus = HttpStatus.BAD_REQUEST;
if (allSuccess){
HttpStatus eventStatus;
if (failedCount == 0) {
eventStatus = HttpStatus.OK;
}
else if (partialSuccess){
eventStatus = HttpStatus.MULTI_STATUS;
} else if (successCount > 0 && failedCount > 0) {
eventStatus = HttpStatus.MULTI_STATUS;
} else {
eventStatus = HttpStatus.BAD_REQUEST;
}
return new ResponseEntity<>(generatedEventResults, eventStatus);

} else if (inputData.isJsonObject()) {
JsonObject inputJsonObject = inputData.getAsJsonObject();
JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, inputJsonObject);
HttpStatus status;
if (processedJson.has(META)) {
status = HttpStatus.OK;
if (!processedJson.has(JSON_STATUS_CODE)) {
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(processedJson, status);
} else if (processedJson.has(JSON_STATUS_CODE)) {
String statusValue = processedJson.get(JSON_STATUS_CODE).toString();
try {
status = HttpStatus.resolve(Integer.parseInt(statusValue));
return new ResponseEntity<>(processedJson, status);
} catch (NumberFormatException e) {
String errorMessage = "Invalid status value: '" + statusValue + "' of response " + processedJson;
log.error(errorMessage);
return createResponseEntity(HttpStatus.BAD_REQUEST, errorMessage, JSON_ERROR_STATUS);
}
HttpStatus status = HttpStatus.resolve(Integer.parseInt(statusValue));
return new ResponseEntity<>(processedJson, status);
} else {
String errorMessage = "There is no status value in the response " + processedJson;
log.error(errorMessage);
Expand All @@ -223,6 +228,7 @@ else if (partialSuccess){
}
}


/**
* To display response in browser or application
* @param status response code for the HTTP request
Expand Down Expand Up @@ -316,10 +322,13 @@ public JsonObject processEvent(String msgProtocol, String msgType, Boolean failI
JsonObject parsedJson = parsedResponse.getAsJsonObject();

if (parsedJson.has(JSON_ERROR_MESSAGE_FIELD)) {
JsonObject eventResponse = new JsonObject();
createResponseEntity(HttpStatus.BAD_REQUEST, parsedJson.toString(), JSON_ERROR_STATUS, eventResponse);
return eventResponse;
// JsonObject eventResponse = new JsonObject();
Copy link
Contributor

Choose a reason for hiding this comment

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

remove unused code

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

// createResponseEntity(HttpStatus.BAD_REQUEST, parsedJson.toString(), JSON_ERROR_STATUS, eventResponse);
// return eventResponse;
throw new REMGenerateException(response);
} else {
/* JsonObject eventResponse = new JsonObject();
Copy link
Contributor

Choose a reason for hiding this comment

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

remove unused code

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

createResponseEntity(HttpStatus.OK, parsedJson.toString(), "SUCCESS", eventResponse);*/
return parsedJson;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.ericsson.eiffel.remrem.generate.exception;

public class ProtocolHandlerNotFoundException extends REMGenerateException {
public ProtocolHandlerNotFoundException(String message) {
super(message);
}
}