Skip to content

Commit e58d75b

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

File tree

2 files changed

+76
-99
lines changed

2 files changed

+76
-99
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,10 @@ public final class RemremGenerateServiceConstants {
5757
public static final String LenientValidation = "okToLeaveOutInvalidOptionalFields true will remove the optional "
5858
+ "event fields from the input event data that does not validate successfully, "
5959
+ "and add those removed field information into customData/remremGenerateFailures";
60+
61+
public static final String JSON_STATUS_CODE = "status code";
62+
63+
public static final String JSON_STATUS_RESULT = "result";
64+
65+
public static final String META = "meta";
6066
}

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

Lines changed: 70 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@
2323
import com.fasterxml.jackson.databind.JsonMappingException;
2424
import com.fasterxml.jackson.databind.JsonNode;
2525
import com.fasterxml.jackson.databind.ObjectMapper;
26-
import com.fasterxml.jackson.databind.node.ArrayNode;
27-
import com.fasterxml.jackson.databind.node.ObjectNode;
2826
import com.google.gson.*;
2927

3028
import ch.qos.logback.classic.Logger;
31-
import com.google.gson.stream.JsonReader;
3229
import io.swagger.annotations.*;
3330

3431
import org.apache.commons.lang3.StringUtils;
@@ -39,7 +36,6 @@
3936
import org.springframework.http.MediaType;
4037
import org.springframework.http.RequestEntity;
4138
import org.springframework.http.ResponseEntity;
42-
import org.springframework.lang.NonNull;
4339
import org.springframework.web.bind.annotation.PathVariable;
4440
import org.springframework.web.bind.annotation.RequestBody;
4541
import org.springframework.web.bind.annotation.RequestMapping;
@@ -51,10 +47,6 @@
5147
import springfox.documentation.annotations.ApiIgnore;
5248

5349
import java.io.*;
54-
import java.lang.reflect.Type;
55-
import java.security.Key;
56-
import java.util.ArrayList;
57-
import java.util.LinkedHashMap;
5850
import java.util.List;
5951
import java.util.Map;
6052
import java.util.Map.Entry;
@@ -92,11 +84,6 @@ public void setRestTemplate(RestTemplate restTemplate) {
9284
this.restTemplate = restTemplate;
9385
}
9486

95-
96-
public String statusCode = "status code";
97-
public String statusMessage = "message";
98-
99-
public String statusResult = "result";
10087
/**
10188
* Returns event information as json element based on the message protocol,
10289
* taking message type and json body as input.
@@ -131,127 +118,111 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
131118
ObjectMapper mapper = new ObjectMapper(jsonFactory);
132119
JsonNode node = mapper.readTree(body);
133120
Gson gson = new Gson();
134-
JsonElement element = gson.fromJson(node.toString(), JsonElement.class);
121+
JsonElement userInputJson = gson.fromJson(node.toString(), JsonElement.class);
135122
return generate(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound, lookupInExternalERs,
136-
lookupLimit, okToLeaveOutInvalidOptionalFields, element);
123+
lookupLimit, okToLeaveOutInvalidOptionalFields, userInputJson);
137124
} catch (JsonSyntaxException e) {
138125
String exceptionMessage = e.getMessage();
139-
log.error(exceptionMessage, e.getMessage());
140-
errorResponse.addProperty(statusCode, HttpStatus.BAD_REQUEST.value());
141-
errorResponse.addProperty(statusResult, "fatal");
142-
errorResponse.addProperty(statusMessage, "Invalid JSON parse data format due to: " + exceptionMessage);
126+
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);
143130
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
144131
} catch (JsonProcessingException e) {
145132
String exceptionMessage = e.getMessage();
146133
log.info("duplicate key detected", exceptionMessage);
147-
errorResponse.addProperty(statusResult, "fatal");
148-
errorResponse.addProperty(statusMessage, "duplicate key detected, please check " + exceptionMessage);
134+
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_STATUS_RESULT, "fatal");
135+
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD, "duplicate key detected, please check " + exceptionMessage);
149136
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
150137
}
151138
}
152139

153-
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 bodyJson) {
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) {
154141

155142
JsonArray generatedEventResults = new JsonArray();
156143
JsonObject errorResponse = new JsonObject();
157144
try {
158145
if (lookupLimit <= 0) {
159146
return new ResponseEntity<>("LookupLimit must be greater than or equals to 1", HttpStatus.BAD_REQUEST);
160147
}
161-
if (bodyJson == null) {
148+
if (userInputData == null) {
162149
log.error("Json event must not be null");
163-
errorResponse.addProperty(statusMessage, "bodyJson must not be null");
150+
errorResponse.addProperty(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD, "userInputData must not be null");
164151
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
165152
}
166-
167-
if (bodyJson.isJsonArray()) {
168-
JsonArray jsonArray = bodyJson.getAsJsonArray();
169-
for (JsonElement element : jsonArray) {
153+
if (userInputData.isJsonArray()) {
154+
JsonArray inputEventJsonArray = userInputData.getAsJsonArray();
155+
for (JsonElement element : inputEventJsonArray) {
170156
JsonObject generatedEvent = (processEvent(msgProtocol, msgType,
171157
failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit,
172158
okToLeaveOutInvalidOptionalFields, element.getAsJsonObject()));
173159
generatedEventResults.add(generatedEvent);
174160
}
175-
176-
boolean hasSuccess = false;
177-
boolean hasFailed = false;
161+
boolean success = true;
178162
for (JsonElement result : generatedEventResults) {
179163
JsonObject jsonObject = result.getAsJsonObject();
180-
181-
if (jsonObject.has("meta")) {
182-
hasSuccess = true;
183-
} else if (jsonObject.has(statusCode) &&
184-
"400".equals(jsonObject.get(statusCode).toString())) {
185-
hasFailed = true;
186-
}
187-
188-
if (hasSuccess) {
189-
return new ResponseEntity<>(generatedEventResults, HttpStatus.OK);
190-
} else if (hasFailed) {
191-
return new ResponseEntity<>(generatedEventResults, HttpStatus.BAD_REQUEST);
192-
} else {
193-
return new ResponseEntity<>(generatedEventResults, HttpStatus.SERVICE_UNAVAILABLE);
194-
}
164+
success &= jsonObject.has(RemremGenerateServiceConstants.META);
195165
}
196-
return new ResponseEntity<>(generatedEventResults, HttpStatus.OK);
166+
return new ResponseEntity<>(generatedEventResults, success ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
197167

198-
} else if (bodyJson.isJsonObject()) {
199-
JsonObject inputJsonObject = bodyJson.getAsJsonObject();
168+
} else if (userInputData.isJsonObject()) {
169+
JsonObject inputJsonObject = userInputData.getAsJsonObject();
200170
JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
201171
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, inputJsonObject);
202172

203-
if (processedJson.has("meta")) {
173+
if (processedJson.has(RemremGenerateServiceConstants.META)) {
204174
return new ResponseEntity<>(processedJson, HttpStatus.OK);
205175
}
206-
if (processedJson.has(statusCode) && "400".equals(processedJson.get(statusCode).toString())) {
176+
if (processedJson.has(RemremGenerateServiceConstants.JSON_STATUS_CODE) && "400".equals(processedJson.get(RemremGenerateServiceConstants.JSON_STATUS_CODE).toString())) {
207177
return new ResponseEntity<>(processedJson, HttpStatus.BAD_REQUEST);
208178
} else {
209179
return new ResponseEntity<>(processedJson, HttpStatus.SERVICE_UNAVAILABLE);
210180
}
211181
} else {
212-
errorResponse.addProperty(statusCode, HttpStatus.BAD_REQUEST.value());
213-
errorResponse.addProperty(statusResult, "fail");
214-
errorResponse.addProperty(statusMessage, "Invalid JSON format,expected either single template or array of templates");
215-
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
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());
216184
}
217-
} catch (Exception e) {
185+
} catch (REMGenerateException | JsonSyntaxException e) {
218186
return handleException(e);
219187
}
220188
}
221189

222-
private ResponseEntity<JsonObject> handleException(Exception e){
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);
196+
return new ResponseEntity<>(errorResponse, status);
197+
198+
}
199+
200+
private ResponseEntity<JsonObject> handleException(Exception e) {
223201
JsonObject errorResponse = new JsonObject();
224202
String exceptionMessage = e.getMessage();
225-
if (e instanceof REMGenerateException){
203+
if (e instanceof REMGenerateException) {
226204
List<HttpStatus> statuses = List.of(
227-
HttpStatus.NOT_ACCEPTABLE,HttpStatus.EXPECTATION_FAILED,HttpStatus.SERVICE_UNAVAILABLE,HttpStatus.UNPROCESSABLE_ENTITY
228-
);
229-
for (HttpStatus status:statuses){
230-
if (exceptionMessage.contains(Integer.toString(status.value()))){
231-
errorResponse.addProperty(statusCode, status.value());
232-
errorResponse.addProperty("message ", exceptionMessage);
233-
return new ResponseEntity<>(errorResponse, status);
205+
HttpStatus.NOT_ACCEPTABLE, HttpStatus.EXPECTATION_FAILED, HttpStatus.SERVICE_UNAVAILABLE, HttpStatus.UNPROCESSABLE_ENTITY
206+
);
207+
for (HttpStatus status : statuses) {
208+
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());
234212
}
235213
}
236-
errorResponse.addProperty(statusMessage, exceptionMessage);
237-
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
238-
} else if (e instanceof JsonSyntaxException){
239-
log.error("Failed to parse JSON: ", exceptionMessage);
240-
errorResponse.addProperty(statusCode, HttpStatus.INTERNAL_SERVER_ERROR.value());
241-
errorResponse.addProperty(statusResult, "fail");
242-
errorResponse.addProperty(statusMessage, exceptionMessage);
243-
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
244-
} else if (e instanceof NullPointerException){
245-
log.info(exceptionMessage);
246-
errorResponse.addProperty(statusMessage, "Json event must not be null");
247-
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
248-
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());
217+
} else if (e instanceof JsonSyntaxException) {
218+
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());
249221
} else {
250222
log.error("Unexpected exception caught", exceptionMessage);
251-
errorResponse.addProperty(statusCode, HttpStatus.BAD_REQUEST.value());
252-
errorResponse.addProperty(statusResult, "fail");
253-
errorResponse.addProperty(statusMessage, exceptionMessage);
254-
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
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());
255226
}
256227
}
257228

@@ -261,29 +232,29 @@ private ResponseEntity<JsonObject> handleException(Exception e){
261232
*/
262233

263234
public JsonObject processEvent(String msgProtocol, String msgType, Boolean failIfMultipleFound,
264-
Boolean failIfNoneFound,Boolean lookupInExternalERs, int lookupLimit,
265-
Boolean okToLeaveOutInvalidOptionalFields, JsonObject jsonObject) throws REMGenerateException, JsonSyntaxException{
235+
Boolean failIfNoneFound, Boolean lookupInExternalERs, int lookupLimit,
236+
Boolean okToLeaveOutInvalidOptionalFields, JsonObject jsonObject) throws REMGenerateException, JsonSyntaxException {
266237
JsonObject eventResponse = new JsonObject();
267-
JsonElement parsedResponse = null;
238+
JsonElement parsedResponse;
268239

269-
JsonObject event = erLookup(jsonObject, failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit);
270-
MsgService msgService = getMessageService(msgProtocol);
240+
JsonObject event = erLookup(jsonObject, failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit);
241+
MsgService msgService = getMessageService(msgProtocol);
271242

272-
if (msgService != null) {
273-
String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
274-
parsedResponse = parser.parse(response);
243+
if (msgService != null) {
244+
String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
245+
parsedResponse = parser.parse(response);
275246

276-
JsonObject parsedJson = parsedResponse.getAsJsonObject();
247+
JsonObject parsedJson = parsedResponse.getAsJsonObject();
277248

278-
if (!parsedJson.has(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD)) {
279-
return parsedJson;
280-
} else {
281-
eventResponse.addProperty(statusCode, HttpStatus.BAD_REQUEST.value());
282-
eventResponse.addProperty(statusResult, "fail");
283-
eventResponse.addProperty(statusMessage, RemremGenerateServiceConstants.TEMPLATE_ERROR);
284-
return eventResponse;
285-
}
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);
255+
return eventResponse;
286256
}
257+
}
287258
return eventResponse;
288259
}
289260

0 commit comments

Comments
 (0)