Skip to content

Commit e9a8433

Browse files
authored
Endpoint /generateAndPublish doesn't validate all events correctly (#318)
Endpoint /generateAndPublish uses MsgService.validateMsg() method to validate generated event.
1 parent a72eebd commit e9a8433

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
## 2.1.10
3+
Message validation generalized for /generateAndPublish
4+
25
## 2.1.9
36
com.fasterxml.jackson.core:jackson-core uplifted
47

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<version>2.0.15</version>
1010
</parent>
1111
<properties>
12-
<eiffel-remrem-publish.version>2.1.9</eiffel-remrem-publish.version>
12+
<eiffel-remrem-publish.version>2.1.10</eiffel-remrem-publish.version>
1313
<eiffel-remrem-semantics.version>2.4.2</eiffel-remrem-semantics.version>
1414
</properties>
1515
<artifactId>eiffel-remrem-publish</artifactId>

publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/controller/ProducerController.java

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.util.*;
1818

19+
import com.ericsson.eiffel.remrem.protocol.ValidationResult;
1920
import com.ericsson.eiffel.remrem.publish.service.*;
2021
import com.google.gson.*;
2122
import org.slf4j.LoggerFactory;
@@ -392,6 +393,8 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String
392393

393394
responseStatus = response.getStatusCode();
394395
String responseBody = null;
396+
// TODO We should not rely on bodyJson (an input string), but rather on given
397+
// result, i.e. response.getBody() and check this for type (object or array).
395398
if (bodyJson.isJsonObject()) {
396399
responseBody = "[" + response.getBody() + "]";
397400
} else if (bodyJson.isJsonArray()) {
@@ -407,8 +410,8 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String
407410
if (msgService != null && msgProtocol != null) {
408411
rmqHelper.rabbitMqPropertiesInit(msgProtocol);
409412
}
410-
responseEvents = processingValidEvent(responseBody, msgProtocol, userDomain,
411-
tag, routingKey);
413+
responseEvents = processingValidEvent(responseBody, msgProtocol, msgType, userDomain,
414+
tag, routingKey, okToLeaveOutInvalidOptionalFields);
412415
} else {
413416
return response;
414417
}
@@ -423,7 +426,8 @@ public ResponseEntity generateAndPublish(final String msgProtocol, final String
423426
} else if (bodyJson.isJsonArray()) {
424427
responseBody = responseMessage;
425428
}
426-
responseEvents = processingValidEvent(responseBody, msgProtocol, userDomain, tag, routingKey);
429+
responseEvents = processingValidEvent(responseBody, msgProtocol, msgType, userDomain,
430+
tag, routingKey, okToLeaveOutInvalidOptionalFields);
427431
return new ResponseEntity<>(responseEvents, HttpStatus.BAD_REQUEST);
428432
}
429433
//Status here is the status returned from generate service, except BAD_REQUEST which already handled above
@@ -477,8 +481,8 @@ public void initializeResponse(HttpStatus status, String errorMessage, String re
477481
* @return list of responses
478482
*/
479483
// TODO Why public?
480-
public List<Map<String, Object>> processingValidEvent(String eventResponseMessage, final String msgProtocol, final String userDomain,
481-
final String tag, final String routingKey) {
484+
public List<Map<String, Object>> processingValidEvent(String eventResponseMessage, final String msgProtocol, final String msgType,
485+
final String userDomain, final String tag, final String routingKey, final Boolean okToLeaveOutInvalidOptionalFields) {
482486
MsgService msgService = PublishUtils.getMessageService(msgProtocol, msgServices);
483487
List<Map<String, Object>> responseEvent = new ArrayList<>();
484488
Map<String, Object> eventResponse = null;
@@ -510,16 +514,15 @@ public List<Map<String, Object>> processingValidEvent(String eventResponseMessag
510514
}
511515

512516
for (int i = 0; i < eventArray.size(); i++) {
517+
// TODO Having hash map here is very chaotic: key are sometimes predefined values,
518+
// e.g. JSON_STATUS_RESULT, JSON_EVENT_MESSAGE_FIELD, but also a general string
519+
// as in case of reporting a bad request.
513520
eventResponse = new HashMap<>();
514-
JsonElement jsonResponseEvent = eventArray.get(i);
515-
if (isValid(jsonResponseEvent)) {
516-
JsonObject validResponse = jsonResponseEvent.getAsJsonObject();
517-
if (validResponse == null) {
518-
String errorMessage = "Invalid, array item expected to be in the form of JSON object";
519-
log.error(errorMessage);
520-
eventResponse.put(errorMessage, HttpStatus.BAD_REQUEST);
521-
}
522-
String validResponseBody = validResponse.toString();
521+
// TODO Could it be null?
522+
JsonObject jsonResponseEvent = eventArray.get(i).getAsJsonObject();
523+
ValidationResult validationResult = msgService.validateMsg(msgType, jsonResponseEvent, okToLeaveOutInvalidOptionalFields);
524+
if (validationResult.isValid()) {
525+
String validResponseBody = jsonResponseEvent.toString();
523526
SendResult result = messageService.send(validResponseBody, msgService, userDomain, tag, routingKey);
524527
eventResponse.put(JSON_STATUS_RESULT, result);
525528
} else {
@@ -540,18 +543,6 @@ public JsonObject getAsJsonObject(JsonElement jsonElement) {
540543
return jsonObject;
541544
}
542545

543-
/**
544-
* This helper method check the json event is valid or not
545-
* @param jsonElement An event which need to check
546-
* @return boolean type value like it is valid or not
547-
*/
548-
private boolean isValid(JsonElement jsonElement) {
549-
JsonObject jsonObject = getAsJsonObject(jsonElement);
550-
return jsonObject != null &&
551-
jsonObject.has(META) && jsonObject.has(DATA) && jsonObject.has(LINKS) &&
552-
!jsonObject.has(JSON_STATUS_CODE);
553-
}
554-
555546
/**
556547
* @return this method returns the current version of publish and all loaded
557548
* protocols.
@@ -563,5 +554,4 @@ public JsonElement getVersions() {
563554
Map<String, Map<String, String>> versions = new VersionService().getMessagingVersions();
564555
return parser.parse(versions.toString());
565556
}
566-
567557
}

publish-service/src/test/java/com/ericsson/eiffel/remrem/publish/service/EiffelRemremCommonControllerUnitTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.HashMap;
2424
import java.util.Map;
2525

26+
import com.ericsson.eiffel.remrem.protocol.ValidationResult;
2627
import org.junit.Before;
2728
import org.junit.Test;
2829
import org.junit.runner.RunWith;
@@ -111,6 +112,7 @@ public void setUp() throws Exception {
111112

112113
when(service.getServiceName()).thenReturn("eiffelsemantics");
113114
when(service2.getServiceName()).thenReturn("eiffelsemantics");
115+
when(service.validateMsg(ArgumentMatchers.anyString(), ArgumentMatchers.any(JsonObject.class), ArgumentMatchers.anyBoolean())).thenReturn(new ValidationResult(true, "OK"));
114116
// TODO remove in a future
115117
// 'getHttpStatus' is no more available in MessageService as it required a status value
116118
// preventing parallel correct publishing of events.

0 commit comments

Comments
 (0)