Skip to content

Commit 037c273

Browse files
Modified generate's endpoint generate to accept array of events
1 parent 55d720d commit 037c273

File tree

2 files changed

+84
-41
lines changed

2 files changed

+84
-41
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
@@ -63,4 +63,6 @@ public final class RemremGenerateServiceConstants {
6363
public static final String JSON_STATUS_RESULT = "result";
6464

6565
public static final String META = "meta";
66+
67+
public static final String JSON_ERROR_STATUS = "fail";
6668
}

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

Lines changed: 82 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,32 @@ public void setRestTemplate(RestTemplate restTemplate) {
8888

8989
/**
9090
* Returns event information as json element based on the message protocol,
91-
* taking message type and json body as input.
91+
* taking message type and json body of string type as input because just to parse
92+
* the string in to JsonElement not using JsonElement directly here.
93+
*
9294
* <p>
9395
* <p>
9496
* Parameters: msgProtocol - The message protocol, which tells us which
9597
* service to invoke. msgType - The type of message that needs to be
96-
* generated. bodyJson - The content of the message which is used in
98+
* generated. body - The content of the message which is used in
9799
* creating the event details.
98100
* <p>
99101
* Returns: The event information as a json element
100102
*/
101103

102104
@ApiOperation(value = "To generate eiffel event based on the message protocol", response = String.class)
103-
@ApiResponses(value = { @ApiResponse(code = 200, message = "Event sent successfully"),
105+
@ApiResponses(value = {@ApiResponse(code = 200, message = "Event sent successfully"),
104106
@ApiResponse(code = 400, message = "Malformed JSON"),
105107
@ApiResponse(code = 500, message = "Internal server error"),
106-
@ApiResponse(code = 503, message = "Message protocol is invalid") })
108+
@ApiResponse(code = 503, message = "Message protocol is invalid")})
107109
@RequestMapping(value = "/{mp" + REGEX + "}", method = RequestMethod.POST)
108110
public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required = true) @PathVariable("mp") final String msgProtocol,
109111
@ApiParam(value = "message type", required = true) @RequestParam("msgType") final String msgType,
110112
@ApiParam(value = "ER lookup result multiple found, Generate will fail") @RequestParam(value = "failIfMultipleFound", required = false, defaultValue = "false") final Boolean failIfMultipleFound,
111113
@ApiParam(value = "ER lookup result none found, Generate will fail") @RequestParam(value = "failIfNoneFound", required = false, defaultValue = "false") final Boolean failIfNoneFound,
112114
@ApiParam(value = RemremGenerateServiceConstants.LOOKUP_IN_EXTERNAL_ERS) @RequestParam(value = "lookupInExternalERs", required = false, defaultValue = "false") final Boolean lookupInExternalERs,
113115
@ApiParam(value = RemremGenerateServiceConstants.LOOKUP_LIMIT) @RequestParam(value = "lookupLimit", required = false, defaultValue = "1") final int lookupLimit,
114-
@ApiParam(value = RemremGenerateServiceConstants.LenientValidation) @RequestParam(value = "okToLeaveOutInvalidOptionalFields", required = false, defaultValue = "false") final Boolean okToLeaveOutInvalidOptionalFields,
116+
@ApiParam(value = RemremGenerateServiceConstants.LenientValidation) @RequestParam(value = "okToLeaveOutInvalidOptionalFields", required = false, defaultValue = "false") final Boolean okToLeaveOutInvalidOptionalFields,
115117
@ApiParam(value = "JSON message", required = true) @RequestBody String body) {
116118
JsonObject errorResponse = null;
117119
try {
@@ -123,32 +125,43 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
123125
JsonElement inputJson = gson.fromJson(node.toString(), JsonElement.class);
124126
return generate(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound, lookupInExternalERs,
125127
lookupLimit, okToLeaveOutInvalidOptionalFields, inputJson);
126-
} catch (JsonSyntaxException e) {
128+
} catch (JsonSyntaxException | JsonProcessingException e) {
127129
String exceptionMessage = e.getMessage();
128130
log.error("Invalid JSON parse data format due to:", e.getMessage());
129-
return createResponseEntity(HttpStatus.BAD_REQUEST,"Invalid JSON parse data format due to: " + exceptionMessage, "fatal",
130-
errorResponse);
131-
} catch (JsonProcessingException e) {
132-
String exceptionMessage = e.getMessage();
133-
log.info("Incorrect Json data", exceptionMessage);
134-
return createResponseEntity(HttpStatus.BAD_REQUEST,"Incorrect Json data: " + exceptionMessage, "fatal",
131+
return createResponseEntity(HttpStatus.BAD_REQUEST, "Invalid JSON parse data format due to: " + exceptionMessage, "fatal",
135132
errorResponse);
136133
}
137134
}
138135

139-
public ResponseEntity<?> generate(final String msgProtocol, final String msgType, final Boolean failIfMultipleFound, final Boolean failIfNoneFound, final Boolean lookupInExternalERs, final int lookupLimit, final Boolean okToLeaveOutInvalidOptionalFields, JsonElement inputData) {
136+
/**
137+
* Returns event information as json element based on the message protocol,
138+
* taking message type and json body as input
139+
* Here we basically add this to handle if inputData is of jsonArray type as well
140+
*
141+
* <p>
142+
* <p>
143+
* Parameters: msgProtocol - The message protocol, which tells us which
144+
* service to invoke. msgType - The type of message that needs to be
145+
* generated. inputData - The content of the message which is used in
146+
* creating the event details.
147+
* <p>
148+
* Returns: The event information as a json element
149+
*/
150+
public ResponseEntity<?> generate(final String msgProtocol, final String msgType, final Boolean failIfMultipleFound,
151+
final Boolean failIfNoneFound, final Boolean lookupInExternalERs, final int lookupLimit,
152+
final Boolean okToLeaveOutInvalidOptionalFields, JsonElement inputData) {
140153

141154
JsonArray generatedEventResults = new JsonArray();
142155
JsonObject errorResponse = new JsonObject();
143156
try {
144157
if (lookupLimit <= 0) {
145-
return new ResponseEntity<>("LookupLimit must be greater than or equals to 1", HttpStatus.BAD_REQUEST);
158+
return new ResponseEntity<>("Parameter 'lookupLimit' must be > 0", HttpStatus.BAD_REQUEST);
146159
}
147160
if (inputData == null) {
148-
log.error("Json event must not be null");
149-
errorResponse.addProperty(JSON_ERROR_MESSAGE_FIELD, "inputData must not be null");
150-
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
161+
createResponseEntity(HttpStatus.BAD_REQUEST, JSON_ERROR_STATUS,
162+
"Parameter 'inputData' must not be null", errorResponse);
151163
}
164+
152165
if (inputData.isJsonArray()) {
153166
JsonArray inputEventJsonArray = inputData.getAsJsonArray();
154167
for (JsonElement element : inputEventJsonArray) {
@@ -169,27 +182,40 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
169182
JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
170183
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, inputJsonObject);
171184

185+
HttpStatus status=null;
172186
if (processedJson.has(META)) {
173-
return new ResponseEntity<>(processedJson, HttpStatus.OK);
174-
}
175-
if (processedJson.has(JSON_STATUS_CODE) && "400".equals(processedJson.get(JSON_STATUS_CODE).toString())) {
176-
return new ResponseEntity<>(processedJson, HttpStatus.BAD_REQUEST);
187+
status = HttpStatus.OK;
188+
return new ResponseEntity<>(processedJson, status);
189+
} else if (processedJson.has(JSON_STATUS_CODE)) {
190+
String statusValue = processedJson.get(JSON_STATUS_CODE).toString();
191+
status = HttpStatus.resolve(Integer.parseInt(statusValue));
192+
return new ResponseEntity<>(processedJson, status);
177193
} else {
178194
return new ResponseEntity<>(processedJson, HttpStatus.SERVICE_UNAVAILABLE);
179195
}
180196

181197
} else {
182-
return createResponseEntity(HttpStatus.BAD_REQUEST, "Invalid JSON format,expected either single template or array of templates",
183-
"fail", errorResponse);
198+
return createResponseEntity(HttpStatus.BAD_REQUEST,
199+
"Invalid JSON format,expected either single template or array of templates",
200+
JSON_ERROR_STATUS, errorResponse);
184201
}
185202
} catch (REMGenerateException | JsonSyntaxException e) {
186203
return handleException(e);
187204
}
188205
}
189206

190-
public ResponseEntity<JsonObject> createResponseEntity(HttpStatus status, String errorMessage, String resultMessage,
207+
208+
/**
209+
* To display response in browser or application
210+
* @param status response code for the HTTP request
211+
* @param responseMessage the message according to response
212+
* @param resultMessage whatever the result this message gives you idea about that
213+
* @param errorResponse is to collect all the responses here.
214+
* @return ResponseEntity
215+
*/
216+
public ResponseEntity<JsonObject> createResponseEntity(HttpStatus status, String responseMessage, String resultMessage,
191217
JsonObject errorResponse) {
192-
initializeResponse(status, errorMessage, resultMessage, errorResponse);
218+
initializeResponse(status, responseMessage, resultMessage, errorResponse);
193219
return new ResponseEntity<>(errorResponse, status);
194220
}
195221

@@ -200,6 +226,12 @@ public void initializeResponse(HttpStatus status, String errorMessage, String re
200226
errorResponse.addProperty(JSON_ERROR_MESSAGE_FIELD, errorMessage);
201227
}
202228

229+
/**
230+
* To handle the exception in one method
231+
* @param e taken general exception here
232+
* @return ResponseEntity
233+
*/
234+
203235
private ResponseEntity<JsonObject> handleException(Exception e) {
204236
JsonObject errorResponse = new JsonObject();
205237
String exceptionMessage = e.getMessage();
@@ -210,21 +242,29 @@ private ResponseEntity<JsonObject> handleException(Exception e) {
210242
for (HttpStatus status : statuses) {
211243
if (exceptionMessage.contains(Integer.toString(status.value()))) {
212244
return createResponseEntity(
213-
status, e.getMessage(), "fail", errorResponse);
245+
status, e.getMessage(), JSON_ERROR_STATUS, errorResponse);
214246
}
215247
}
216-
return createResponseEntity(HttpStatus.BAD_REQUEST, e.getMessage(), "fail", errorResponse);
248+
return createResponseEntity(HttpStatus.BAD_REQUEST, e.getMessage(), JSON_ERROR_STATUS, errorResponse);
217249
} else if (e instanceof JsonSyntaxException) {
218250
log.error("Failed to parse JSON: ", exceptionMessage);
219-
return createResponseEntity(HttpStatus.BAD_REQUEST, e.getMessage(), "fail", errorResponse);
251+
return createResponseEntity(HttpStatus.BAD_REQUEST, e.getMessage(), JSON_ERROR_STATUS, errorResponse);
220252
} else {
221253
log.error("Unexpected exception caught", exceptionMessage);
222-
return createResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR, exceptionMessage, "fail", errorResponse);
254+
return createResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR, exceptionMessage, JSON_ERROR_STATUS, errorResponse);
223255
}
224256
}
225257

226258
/**
227-
* This helper method basically generate or process one event
259+
* This helper method basically generate or process single event
260+
* @param msgProtocol The message protocol, which tells us which service to invoke
261+
* @param msgType The type of message that needs to be generated. inputData
262+
* @param failIfMultipleFound
263+
* @param failIfNoneFound
264+
* @param lookupInExternalERs
265+
* @param lookupLimit
266+
* @param okToLeaveOutInvalidOptionalFields
267+
* @param jsonObject The content of the message which is used in creating the event details.
228268
* @return JsonObject generated event
229269
*/
230270

@@ -237,19 +277,20 @@ public JsonObject processEvent(String msgProtocol, String msgType, Boolean failI
237277
JsonObject event = erLookup(jsonObject, failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit);
238278
MsgService msgService = getMessageService(msgProtocol);
239279

240-
if (msgService != null) {
241-
String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
242-
parsedResponse = JsonParser.parseString(response);
243-
JsonObject parsedJson = parsedResponse.getAsJsonObject();
280+
if (msgService == null) {
281+
return createResponseEntity(HttpStatus.SERVICE_UNAVAILABLE, JSON_ERROR_STATUS,
282+
"No protocol service has been found registered", eventResponse).getBody();
283+
}
284+
String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
285+
parsedResponse = JsonParser.parseString(response);
286+
JsonObject parsedJson = parsedResponse.getAsJsonObject();
244287

245-
if (parsedJson.has(JSON_ERROR_MESSAGE_FIELD)) {
246-
createResponseEntity(HttpStatus.BAD_REQUEST, "fail", TEMPLATE_ERROR, eventResponse);
247-
return eventResponse;
248-
} else {
249-
return parsedJson;
250-
}
288+
if (parsedJson.has(JSON_ERROR_MESSAGE_FIELD)) {
289+
createResponseEntity(HttpStatus.BAD_REQUEST, JSON_ERROR_STATUS, TEMPLATE_ERROR, eventResponse);
290+
return eventResponse;
291+
} else {
292+
return parsedJson;
251293
}
252-
return eventResponse;
253294
}
254295

255296
private JsonObject erLookup(final JsonObject bodyJson, Boolean failIfMultipleFound, Boolean failIfNoneFound,

0 commit comments

Comments
 (0)