Skip to content

Commit 55d720d

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

File tree

1 file changed

+41
-47
lines changed

1 file changed

+41
-47
lines changed

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

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
import java.util.Map;
5252
import java.util.Map.Entry;
5353

54+
import static com.ericsson.eiffel.remrem.generate.constants.RemremGenerateServiceConstants.*;
55+
5456
@RestController
5557
@RequestMapping("/*")
5658
@Api(value = "REMReM Generate Service", description = "REST API for generating Eiffel messages")
@@ -118,40 +120,37 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
118120
ObjectMapper mapper = new ObjectMapper(jsonFactory);
119121
JsonNode node = mapper.readTree(body);
120122
Gson gson = new Gson();
121-
JsonElement userInputJson = gson.fromJson(node.toString(), JsonElement.class);
123+
JsonElement inputJson = gson.fromJson(node.toString(), JsonElement.class);
122124
return generate(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound, lookupInExternalERs,
123-
lookupLimit, okToLeaveOutInvalidOptionalFields, userInputJson);
125+
lookupLimit, okToLeaveOutInvalidOptionalFields, inputJson);
124126
} catch (JsonSyntaxException e) {
125127
String exceptionMessage = e.getMessage();
126128
log.error("Invalid JSON parse data format due to:", e.getMessage());
127-
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_STATUS_CODE, HttpStatus.BAD_REQUEST.value());
128-
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_STATUS_RESULT, "fatal");
129-
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD, "Invalid JSON parse data format due to: " + exceptionMessage);
130-
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
129+
return createResponseEntity(HttpStatus.BAD_REQUEST,"Invalid JSON parse data format due to: " + exceptionMessage, "fatal",
130+
errorResponse);
131131
} catch (JsonProcessingException e) {
132132
String exceptionMessage = e.getMessage();
133-
log.info("duplicate key detected", exceptionMessage);
134-
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_STATUS_RESULT, "fatal");
135-
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD, "duplicate key detected, please check " + exceptionMessage);
136-
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
133+
log.info("Incorrect Json data", exceptionMessage);
134+
return createResponseEntity(HttpStatus.BAD_REQUEST,"Incorrect Json data: " + exceptionMessage, "fatal",
135+
errorResponse);
137136
}
138137
}
139138

140-
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 userInputData) {
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) {
141140

142141
JsonArray generatedEventResults = new JsonArray();
143142
JsonObject errorResponse = new JsonObject();
144143
try {
145144
if (lookupLimit <= 0) {
146145
return new ResponseEntity<>("LookupLimit must be greater than or equals to 1", HttpStatus.BAD_REQUEST);
147146
}
148-
if (userInputData == null) {
147+
if (inputData == null) {
149148
log.error("Json event must not be null");
150-
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD, "userInputData must not be null");
149+
errorResponse.addProperty(JSON_ERROR_MESSAGE_FIELD, "inputData must not be null");
151150
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
152151
}
153-
if (userInputData.isJsonArray()) {
154-
JsonArray inputEventJsonArray = userInputData.getAsJsonArray();
152+
if (inputData.isJsonArray()) {
153+
JsonArray inputEventJsonArray = inputData.getAsJsonArray();
155154
for (JsonElement element : inputEventJsonArray) {
156155
JsonObject generatedEvent = (processEvent(msgProtocol, msgType,
157156
failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit,
@@ -161,40 +160,44 @@ public ResponseEntity<?> generate(final String msgProtocol, final String msgType
161160
boolean success = true;
162161
for (JsonElement result : generatedEventResults) {
163162
JsonObject jsonObject = result.getAsJsonObject();
164-
success &= jsonObject.has(RemremGenerateServiceConstants.META);
163+
success &= jsonObject.has(META);
165164
}
166165
return new ResponseEntity<>(generatedEventResults, success ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
167166

168-
} else if (userInputData.isJsonObject()) {
169-
JsonObject inputJsonObject = userInputData.getAsJsonObject();
167+
} else if (inputData.isJsonObject()) {
168+
JsonObject inputJsonObject = inputData.getAsJsonObject();
170169
JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
171170
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, inputJsonObject);
172171

173-
if (processedJson.has(RemremGenerateServiceConstants.META)) {
172+
if (processedJson.has(META)) {
174173
return new ResponseEntity<>(processedJson, HttpStatus.OK);
175174
}
176-
if (processedJson.has(RemremGenerateServiceConstants.JSON_STATUS_CODE) && "400".equals(processedJson.get(RemremGenerateServiceConstants.JSON_STATUS_CODE).toString())) {
175+
if (processedJson.has(JSON_STATUS_CODE) && "400".equals(processedJson.get(JSON_STATUS_CODE).toString())) {
177176
return new ResponseEntity<>(processedJson, HttpStatus.BAD_REQUEST);
178177
} else {
179178
return new ResponseEntity<>(processedJson, HttpStatus.SERVICE_UNAVAILABLE);
180179
}
180+
181181
} else {
182-
return createResponseEntity(RemremGenerateServiceConstants.JSON_STATUS_CODE, RemremGenerateServiceConstants.JSON_STATUS_RESULT, RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD,
183-
HttpStatus.BAD_REQUEST, "Invalid JSON format,expected either single template or array of templates", "fail", errorResponse, HttpStatus.BAD_REQUEST.value());
182+
return createResponseEntity(HttpStatus.BAD_REQUEST, "Invalid JSON format,expected either single template or array of templates",
183+
"fail", errorResponse);
184184
}
185185
} catch (REMGenerateException | JsonSyntaxException e) {
186186
return handleException(e);
187187
}
188188
}
189189

190-
public ResponseEntity<JsonObject> createResponseEntity(String statusCode, String statusResult, String statusMessage,
191-
HttpStatus status, String errorMessage, String resultMessage, JsonObject errorResponse,
192-
int statusCodeValue) {
193-
errorResponse.addProperty(statusCode, statusCodeValue);
194-
errorResponse.addProperty(statusResult, resultMessage);
195-
errorResponse.addProperty(statusMessage, errorMessage);
190+
public ResponseEntity<JsonObject> createResponseEntity(HttpStatus status, String errorMessage, String resultMessage,
191+
JsonObject errorResponse) {
192+
initializeResponse(status, errorMessage, resultMessage, errorResponse);
196193
return new ResponseEntity<>(errorResponse, status);
194+
}
197195

196+
public void initializeResponse(HttpStatus status, String errorMessage, String resultMessage,
197+
JsonObject errorResponse) {
198+
errorResponse.addProperty(JSON_STATUS_CODE, status.value());
199+
errorResponse.addProperty(JSON_STATUS_RESULT, resultMessage);
200+
errorResponse.addProperty(JSON_ERROR_MESSAGE_FIELD, errorMessage);
198201
}
199202

200203
private ResponseEntity<JsonObject> handleException(Exception e) {
@@ -206,23 +209,17 @@ private ResponseEntity<JsonObject> handleException(Exception e) {
206209
);
207210
for (HttpStatus status : statuses) {
208211
if (exceptionMessage.contains(Integer.toString(status.value()))) {
209-
return createResponseEntity(RemremGenerateServiceConstants.JSON_STATUS_CODE, RemremGenerateServiceConstants.JSON_STATUS_RESULT,
210-
RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD,
211-
status, e.getMessage(), "fail", errorResponse, status.value());
212+
return createResponseEntity(
213+
status, e.getMessage(), "fail", errorResponse);
212214
}
213215
}
214-
return createResponseEntity(RemremGenerateServiceConstants.JSON_STATUS_CODE, RemremGenerateServiceConstants.JSON_STATUS_RESULT,
215-
RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD,
216-
HttpStatus.BAD_REQUEST, e.getMessage(), "fail", errorResponse, HttpStatus.BAD_REQUEST.value());
216+
return createResponseEntity(HttpStatus.BAD_REQUEST, e.getMessage(), "fail", errorResponse);
217217
} else if (e instanceof JsonSyntaxException) {
218218
log.error("Failed to parse JSON: ", exceptionMessage);
219-
return createResponseEntity(RemremGenerateServiceConstants.JSON_STATUS_CODE, RemremGenerateServiceConstants.JSON_STATUS_RESULT, RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD,
220-
HttpStatus.BAD_REQUEST, e.getMessage(), "fail", errorResponse, HttpStatus.BAD_REQUEST.value());
219+
return createResponseEntity(HttpStatus.BAD_REQUEST, e.getMessage(), "fail", errorResponse);
221220
} else {
222221
log.error("Unexpected exception caught", exceptionMessage);
223-
return createResponseEntity(RemremGenerateServiceConstants.JSON_STATUS_CODE, RemremGenerateServiceConstants.JSON_STATUS_RESULT,
224-
RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD,
225-
HttpStatus.INTERNAL_SERVER_ERROR, exceptionMessage, "fail", errorResponse, HttpStatus.INTERNAL_SERVER_ERROR.value());
222+
return createResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR, exceptionMessage, "fail", errorResponse);
226223
}
227224
}
228225

@@ -242,17 +239,14 @@ public JsonObject processEvent(String msgProtocol, String msgType, Boolean failI
242239

243240
if (msgService != null) {
244241
String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
245-
parsedResponse = parser.parse(response);
246-
242+
parsedResponse = JsonParser.parseString(response);
247243
JsonObject parsedJson = parsedResponse.getAsJsonObject();
248244

249-
if (!parsedJson.has(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD)) {
250-
return parsedJson;
251-
} else {
252-
eventResponse.addProperty(RemremGenerateServiceConstants.JSON_STATUS_CODE, HttpStatus.BAD_REQUEST.value());
253-
eventResponse.addProperty(RemremGenerateServiceConstants.JSON_STATUS_RESULT, "fail");
254-
eventResponse.addProperty(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD, RemremGenerateServiceConstants.TEMPLATE_ERROR);
245+
if (parsedJson.has(JSON_ERROR_MESSAGE_FIELD)) {
246+
createResponseEntity(HttpStatus.BAD_REQUEST, "fail", TEMPLATE_ERROR, eventResponse);
255247
return eventResponse;
248+
} else {
249+
return parsedJson;
256250
}
257251
}
258252
return eventResponse;

0 commit comments

Comments
 (0)