Skip to content

Commit 1476f2d

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

File tree

2 files changed

+111
-71
lines changed

2 files changed

+111
-71
lines changed

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

Lines changed: 110 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@
1818
import com.ericsson.eiffel.remrem.generate.constants.RemremGenerateServiceConstants;
1919
import com.ericsson.eiffel.remrem.generate.exception.REMGenerateException;
2020
import com.ericsson.eiffel.remrem.protocol.MsgService;
21+
import com.fasterxml.jackson.core.JsonFactory;
22+
import com.fasterxml.jackson.core.JsonProcessingException;
23+
import com.fasterxml.jackson.databind.JsonMappingException;
24+
import com.fasterxml.jackson.databind.JsonNode;
25+
import com.fasterxml.jackson.databind.ObjectMapper;
26+
import com.fasterxml.jackson.databind.node.ArrayNode;
27+
import com.fasterxml.jackson.databind.node.ObjectNode;
2128
import com.google.gson.*;
2229

2330
import ch.qos.logback.classic.Logger;
31+
import com.google.gson.stream.JsonReader;
2432
import io.swagger.annotations.*;
2533

2634
import org.apache.commons.lang3.StringUtils;
@@ -42,11 +50,11 @@
4250

4351
import springfox.documentation.annotations.ApiIgnore;
4452

45-
import java.io.BufferedReader;
46-
import java.io.FileInputStream;
47-
import java.io.IOException;
48-
import java.io.InputStreamReader;
53+
import java.io.*;
54+
import java.lang.reflect.Type;
55+
import java.security.Key;
4956
import java.util.ArrayList;
57+
import java.util.LinkedHashMap;
5058
import java.util.List;
5159
import java.util.Map;
5260
import java.util.Map.Entry;
@@ -84,6 +92,11 @@ public void setRestTemplate(RestTemplate restTemplate) {
8492
this.restTemplate = restTemplate;
8593
}
8694

95+
96+
public String statusCode = "status code";
97+
public String statusMessage = "message";
98+
99+
public String statusResult = "result";
87100
/**
88101
* Returns event information as json element based on the message protocol,
89102
* taking message type and json body as input.
@@ -110,109 +123,134 @@ public ResponseEntity<?> generate(@ApiParam(value = "message protocol", required
110123
@ApiParam(value = RemremGenerateServiceConstants.LOOKUP_IN_EXTERNAL_ERS) @RequestParam(value = "lookupInExternalERs", required = false, defaultValue = "false") final Boolean lookupInExternalERs,
111124
@ApiParam(value = RemremGenerateServiceConstants.LOOKUP_LIMIT) @RequestParam(value = "lookupLimit", required = false, defaultValue = "1") final int lookupLimit,
112125
@ApiParam(value = RemremGenerateServiceConstants.LenientValidation) @RequestParam(value = "okToLeaveOutInvalidOptionalFields", required = false, defaultValue = "false") final Boolean okToLeaveOutInvalidOptionalFields,
113-
@ApiParam(value = "JSON message", required = true) @RequestBody String body){
126+
@ApiParam(value = "JSON message", required = true) @RequestBody String body) {
127+
JsonObject errorResponse = null;
114128
try {
115-
JsonElement bodyJson = JsonParser.parseString(body);
116-
129+
errorResponse = new JsonObject();
130+
JsonFactory jsonFactory = JsonFactory.builder().build().enable(com.fasterxml.jackson.core.JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
131+
ObjectMapper mapper = new ObjectMapper(jsonFactory);
132+
JsonNode node = mapper.readTree(body);
133+
Gson gson = new Gson();
134+
JsonElement element = gson.fromJson(node.toString(), JsonElement.class);
117135
return generate(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound, lookupInExternalERs,
118-
lookupLimit, okToLeaveOutInvalidOptionalFields, bodyJson);
119-
136+
lookupLimit, okToLeaveOutInvalidOptionalFields, element);
120137
} catch (JsonSyntaxException e) {
121-
JsonObject errorResponse = new JsonObject();
122138
String exceptionMessage = e.getMessage();
123139
log.error(exceptionMessage, e.getMessage());
124-
errorResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value());
125-
errorResponse.addProperty("result", "fatal");
126-
errorResponse.addProperty("message", "Invalid JSON parse data format due to: " + exceptionMessage);
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);
127143
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
144+
} catch (JsonProcessingException e) {
145+
String exceptionMessage = e.getMessage();
146+
log.info("duplicate key detected", exceptionMessage);
147+
errorResponse.addProperty(statusResult, "fatal");
148+
errorResponse.addProperty(statusMessage, "duplicate key detected, please check " + exceptionMessage);
149+
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
128150
}
129151
}
130152

131153
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) {
132154

133-
JsonArray results = new JsonArray();
155+
JsonArray generatedEventResults = new JsonArray();
134156
JsonObject errorResponse = new JsonObject();
135157
try {
136158
if (lookupLimit <= 0) {
137-
return new ResponseEntity<>("LookupLimit must be greater than or equals to 1", HttpStatus.valueOf(HttpStatus.BAD_REQUEST.value()));
159+
return new ResponseEntity<>("LookupLimit must be greater than or equals to 1", HttpStatus.BAD_REQUEST);
160+
}
161+
if (bodyJson == null) {
162+
log.error("Json event must not be null");
163+
errorResponse.addProperty(statusMessage, "bodyJson must not be null");
164+
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
138165
}
139166

140167
if (bodyJson.isJsonArray()) {
141-
142168
JsonArray jsonArray = bodyJson.getAsJsonArray();
143169
for (JsonElement element : jsonArray) {
144-
results.add(processEvent(msgProtocol, msgType,
170+
JsonObject generatedEvent = (processEvent(msgProtocol, msgType,
145171
failIfMultipleFound, failIfNoneFound, lookupInExternalERs, lookupLimit,
146172
okToLeaveOutInvalidOptionalFields, element.getAsJsonObject()));
173+
generatedEventResults.add(generatedEvent);
147174
}
148-
for (int i = 0; i < results.size(); i++) {
149-
JsonObject event = results.get(i).getAsJsonObject();
150-
if (event.has("meta")) {
151-
return new ResponseEntity<>(results, HttpStatus.OK);
152-
} else if (event.has("status code") && "400".equals(event.get("status code").toString())) {
153-
return new ResponseEntity<>(results, HttpStatus.BAD_REQUEST);
175+
176+
boolean hasSuccess = false;
177+
boolean hasFailed = false;
178+
for (JsonElement result : generatedEventResults) {
179+
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);
154192
} else {
155-
return new ResponseEntity<>(results, HttpStatus.SERVICE_UNAVAILABLE);
193+
return new ResponseEntity<>(generatedEventResults, HttpStatus.SERVICE_UNAVAILABLE);
156194
}
157195
}
158-
return new ResponseEntity<>(results, HttpStatus.ACCEPTED);
196+
return new ResponseEntity<>(generatedEventResults, HttpStatus.OK);
159197

160198
} else if (bodyJson.isJsonObject()) {
161-
JsonObject jsonObject = bodyJson.getAsJsonObject();
199+
JsonObject inputJsonObject = bodyJson.getAsJsonObject();
162200
JsonObject processedJson = processEvent(msgProtocol, msgType, failIfMultipleFound, failIfNoneFound,
163-
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, jsonObject);
201+
lookupInExternalERs, lookupLimit, okToLeaveOutInvalidOptionalFields, inputJsonObject);
164202

165203
if (processedJson.has("meta")) {
166204
return new ResponseEntity<>(processedJson, HttpStatus.OK);
167205
}
168-
if (processedJson.has("status code") && "400".equals(processedJson.get("status code").toString())) {
206+
if (processedJson.has(statusCode) && "400".equals(processedJson.get(statusCode).toString())) {
169207
return new ResponseEntity<>(processedJson, HttpStatus.BAD_REQUEST);
170208
} else {
171209
return new ResponseEntity<>(processedJson, HttpStatus.SERVICE_UNAVAILABLE);
172210
}
173211
} else {
174-
errorResponse.addProperty("Status code", HttpStatus.BAD_REQUEST.value());
175-
errorResponse.addProperty("result", "fail");
176-
errorResponse.addProperty("message", "Invalid JSON format,expected either single template or array of templates");
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");
177215
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
178216
}
179-
} catch (REMGenerateException e) {
180-
if (e.getMessage().contains(Integer.toString(HttpStatus.NOT_ACCEPTABLE.value()))) {
181-
errorResponse.addProperty("message", e.getMessage());
182-
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_ACCEPTABLE);
183-
} else if (e.getMessage().contains(Integer.toString(HttpStatus.EXPECTATION_FAILED.value()))) {
184-
errorResponse.addProperty("message", e.getMessage());
185-
return new ResponseEntity<>(errorResponse, HttpStatus.EXPECTATION_FAILED);
186-
} else if (e.getMessage().contains(Integer.toString(HttpStatus.EXPECTATION_FAILED.value()))) {
187-
errorResponse.addProperty("status code", HttpStatus.EXPECTATION_FAILED.value());
188-
189-
} else if (e.getMessage()
190-
.contains(Integer.toString(HttpStatus.SERVICE_UNAVAILABLE.value()))) {
191-
errorResponse.addProperty("message", e.getMessage());
192-
return new ResponseEntity<>(errorResponse, HttpStatus.SERVICE_UNAVAILABLE);
193-
} else {
194-
errorResponse.addProperty("status code", HttpStatus.UNPROCESSABLE_ENTITY.value());
195-
errorResponse.addProperty("message", e.getMessage());
196-
return new ResponseEntity<>(errorResponse, HttpStatus.UNPROCESSABLE_ENTITY);
217+
} catch (Exception e) {
218+
return handleException(e);
219+
}
220+
}
221+
222+
private ResponseEntity<JsonObject> handleException(Exception e){
223+
JsonObject errorResponse = new JsonObject();
224+
String exceptionMessage = e.getMessage();
225+
if (e instanceof REMGenerateException){
226+
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);
234+
}
197235
}
198-
errorResponse.addProperty("result", "fail");
199-
errorResponse.add("message", parser.parse(e.getMessage()));
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");
200247
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
201248

202-
} catch (JsonSyntaxException e) {
203-
log.error("Failed to parse JSON: ", e.getMessage());
204-
String exceptionMessage = e.getMessage();
205-
errorResponse.addProperty("status code", HttpStatus.INTERNAL_SERVER_ERROR.value());
206-
errorResponse.addProperty("result", "fail");
207-
errorResponse.addProperty("message", exceptionMessage);
208-
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
209-
210-
} catch (Exception e) {
211-
log.error("Unexpected exception caught", e);
212-
String exceptionMessage = e.getMessage();
213-
errorResponse.addProperty("status code", HttpStatus.BAD_REQUEST.value());
214-
errorResponse.addProperty("result", "fail");
215-
errorResponse.addProperty("message", exceptionMessage);
249+
} else {
250+
log.error("Unexpected exception caught", exceptionMessage);
251+
errorResponse.addProperty(statusCode, HttpStatus.BAD_REQUEST.value());
252+
errorResponse.addProperty(statusResult, "fail");
253+
errorResponse.addProperty(statusMessage, exceptionMessage);
216254
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
217255
}
218256
}
@@ -235,12 +273,14 @@ public JsonObject processEvent(String msgProtocol, String msgType, Boolean failI
235273
String response = msgService.generateMsg(msgType, event, isLenientEnabled(okToLeaveOutInvalidOptionalFields));
236274
parsedResponse = parser.parse(response);
237275

238-
if (!parsedResponse.getAsJsonObject().has(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD)) {
239-
return parsedResponse.getAsJsonObject();
276+
JsonObject parsedJson = parsedResponse.getAsJsonObject();
277+
278+
if (!parsedJson.has(RemremGenerateServiceConstants.JSON_ERROR_MESSAGE_FIELD)) {
279+
return parsedJson;
240280
} else {
241-
eventResponse.addProperty("status code", HttpStatus.BAD_REQUEST.value());
242-
eventResponse.addProperty("result", "fail");
243-
eventResponse.addProperty("message", RemremGenerateServiceConstants.TEMPLATE_ERROR);
281+
eventResponse.addProperty(statusCode, HttpStatus.BAD_REQUEST.value());
282+
eventResponse.addProperty(statusResult, "fail");
283+
eventResponse.addProperty(statusMessage, RemremGenerateServiceConstants.TEMPLATE_ERROR);
244284
return eventResponse;
245285
}
246286
}

service/src/test/java/com/ericsson/eiffel/remrem/generate/integrationtest/EiffelRemremControllerIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void testDuplicateKeyInBody() throws IOException {
173173
.contentType("application/json")
174174
.body(activityFinishedDuplicateKeysBody)
175175
.when()
176-
.post("/eiffelsemantics?msgType=EiffelActivityFinishedEven")
176+
.post("/eiffelsemantics?msgType=EiffelActivityFinishedEvent")
177177
.then()
178178
.statusCode(HttpStatus.SC_BAD_REQUEST);
179179
}

0 commit comments

Comments
 (0)