Skip to content

Commit dfa1799

Browse files
Modified generate's endpoint generate to accept array of events
1 parent 145f2b8 commit dfa1799

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

service/src/main/java/com/ericsson/eiffel/remrem/generate/constants/RemremGenerateServiceConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@ public final class RemremGenerateServiceConstants {
6565
public static final String META = "meta";
6666

6767
public static final String JSON_ERROR_STATUS = "fail";
68+
69+
public static final String JSON_FATAL_STATUS = "fatal";
6870
}

service/src/main/java/com/ericsson/eiffel/remrem/generate/controller/RemremGenerateController.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
115115
@ApiParam(value = RemremGenerateServiceConstants.LenientValidation) @RequestParam(value = "okToLeaveOutInvalidOptionalFields", required = false, defaultValue = "false") final Boolean okToLeaveOutInvalidOptionalFields,
116116
@ApiParam(value = "JSON message", required = true) @RequestBody String body) {
117117
try {
118-
JsonFactory jsonFactory = JsonFactory.builder().build().enable(com.fasterxml.jackson.core.JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
118+
JsonFactory jsonFactory = JsonFactory.builder().build().enable(com.fasterxml.jackson.core.JsonParser
119+
.Feature.STRICT_DUPLICATE_DETECTION);
119120
ObjectMapper mapper = new ObjectMapper(jsonFactory);
120121
JsonNode node = mapper.readTree(body);
121122
Gson gson = new Gson();
@@ -126,7 +127,7 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
126127
String exceptionMessage = e.getMessage();
127128
log.error("Invalid JSON parse data format due to", e.getMessage());
128129
return createResponseEntity(HttpStatus.BAD_REQUEST, "Invalid JSON parse data format due to: "
129-
+ exceptionMessage, "fatal");
130+
+ exceptionMessage, JSON_FATAL_STATUS);
130131
}
131132
}
132133

@@ -152,8 +153,8 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
152153
return new ResponseEntity<>("Parameter 'lookupLimit' must be > 0", HttpStatus.BAD_REQUEST);
153154
}
154155
if (inputData == null) {
155-
return createResponseEntity(HttpStatus.BAD_REQUEST, JSON_ERROR_STATUS,
156-
"Parameter 'inputData' must not be null");
156+
return createResponseEntity(HttpStatus.BAD_REQUEST, "Parameter 'inputData' must not be null",
157+
JSON_ERROR_STATUS);
157158
}
158159

159160
if (inputData.isJsonArray()) {
@@ -175,28 +176,31 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
175176
JsonObject inputJsonObject = inputData.getAsJsonObject();
176177
JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
177178
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, inputJsonObject);
178-
HttpStatus status = null;
179+
HttpStatus status;
180+
String statusValue = null;
179181
if (processedJson.has(META)) {
180182
status = HttpStatus.OK;
181183
return new ResponseEntity<>(processedJson, status);
182184
} else if (processedJson.has(JSON_STATUS_CODE)) {
183-
String statusValue = processedJson.get(JSON_STATUS_CODE).toString();
185+
statusValue = processedJson.get(JSON_STATUS_CODE).toString();
186+
try {
184187
status = HttpStatus.resolve(Integer.parseInt(statusValue));
185188
return new ResponseEntity<>(processedJson, status);
186-
189+
} catch (NumberFormatException e) {
190+
log.error("Invalid status value: '" + statusValue + "' of response " + processedJson);
191+
return createResponseEntity(HttpStatus.BAD_REQUEST, "Invalid status value: '"
192+
+ statusValue + "' of response " + processedJson, JSON_ERROR_STATUS);
193+
}
187194
} else {
188-
return new ResponseEntity<>(processedJson, HttpStatus.SERVICE_UNAVAILABLE);
195+
log.error("There is no status value: '" + statusValue + "' in the response" + processedJson);
196+
return createResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR, "There is no status value: '"
197+
+ statusValue + "' in the response " + processedJson, JSON_ERROR_STATUS);
189198
}
190-
191199
} else {
192200
return createResponseEntity(HttpStatus.BAD_REQUEST,
193201
"Invalid JSON format,expected either single template or array of templates",
194202
JSON_ERROR_STATUS);
195203
}
196-
} catch (NumberFormatException e){
197-
String exceptionMessage = e.getMessage();
198-
log.error("Invalid number format", exceptionMessage);
199-
return new ResponseEntity<>(exceptionMessage, HttpStatus.BAD_REQUEST);
200204
} catch (REMGenerateException | JsonSyntaxException e) {
201205
return handleException(e);
202206
}
@@ -220,8 +224,7 @@ public ResponseEntity<JsonObject> createResponseEntity(HttpStatus status, String
220224
return createResponseEntity(status, responseMessage, resultMessage, new JsonObject());
221225
}
222226

223-
public void initializeResponse(HttpStatus status, String errorMessage, String resultMessage,
224-
JsonObject errorResponse) {
227+
public void initializeResponse(HttpStatus status, String errorMessage, String resultMessage, JsonObject errorResponse) {
225228
errorResponse.addProperty(JSON_STATUS_CODE, status.value());
226229
errorResponse.addProperty(JSON_STATUS_RESULT, resultMessage);
227230
errorResponse.addProperty(JSON_ERROR_MESSAGE_FIELD, errorMessage);
@@ -235,23 +238,20 @@ public void initializeResponse(HttpStatus status, String errorMessage, String re
235238
private ResponseEntity<JsonObject> handleException(Exception e) {
236239
String exceptionMessage = e.getMessage();
237240
if (e instanceof REMGenerateException) {
238-
List<HttpStatus> statuses = List.of(
239-
HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNPROCESSABLE_ENTITY
241+
List<HttpStatus> statuseList = List.of(
242+
HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.SERVICE_UNAVAILABLE,
243+
HttpStatus.UNPROCESSABLE_ENTITY
240244
);
241-
for (HttpStatus status : statuses) {
245+
for (HttpStatus status : statuseList) {
242246
if (exceptionMessage.contains(Integer.toString(status.value()))) {
243-
return createResponseEntity(
244-
status, e.getMessage(), JSON_ERROR_STATUS);
247+
return createResponseEntity(status, exceptionMessage, JSON_ERROR_STATUS);
245248
}
246249
}
247250
return createResponseEntity(HttpStatus.BAD_REQUEST, exceptionMessage, JSON_ERROR_STATUS);
248251
} else if (e instanceof JsonSyntaxException) {
249252
log.error("Failed to parse JSON", exceptionMessage);
250253
return createResponseEntity(HttpStatus.BAD_REQUEST, exceptionMessage, JSON_ERROR_STATUS);
251-
} /*else if (e instanceof NumberFormatException) {
252-
log.error("Invalid number format", exceptionMessage);
253-
return createResponseEntity(HttpStatus.BAD_REQUEST, exceptionMessage, JSON_ERROR_STATUS, errorResponse);
254-
}*/ else {
254+
} else {
255255
log.error("Unexpected exception caught", exceptionMessage);
256256
return createResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR, exceptionMessage, JSON_ERROR_STATUS);
257257
}
@@ -279,8 +279,8 @@ public JsonObject processEvent(String msgProtocol, String msgType, Boolean failI
279279
MsgService msgService = getMessageService(msgProtocol);
280280

281281
if (msgService == null) {
282-
return createResponseEntity(HttpStatus.SERVICE_UNAVAILABLE, JSON_ERROR_STATUS,
283-
"No protocol service has been found registered").getBody();
282+
return createResponseEntity(HttpStatus.SERVICE_UNAVAILABLE,
283+
"No protocol service has been found registered", JSON_ERROR_STATUS).getBody();
284284
}
285285
String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
286286
parsedResponse = JsonParser.parseString(response);

0 commit comments

Comments
 (0)