Skip to content

Commit

Permalink
feat: add empty response validations
Browse files Browse the repository at this point in the history
  • Loading branch information
shiva-rakshith committed Jan 23, 2022
1 parent 8d2c8d7 commit b8a609f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ public ResponseEntity<Object> validateReqAndPushToKafka(Map<String, Object> requ
}
}

protected HeaderAudit getAuditData(Request request, String apiAction) {
HeaderAudit audit = new HeaderAudit();
protected List<HeaderAudit> getAuditData(Request request, String apiAction) {
List<HeaderAudit> auditResponse = new ArrayList<>();
if (ON_ACTION_APIS.contains(apiAction)) {
audit = auditService.search(new SearchRequestDTO(new HashMap<>(Collections.singletonMap(CORRELATION_ID, request.getCorrelationId())))).get(0);
auditResponse = auditService.search(new SearchRequestDTO(new HashMap<>(Collections.singletonMap(CORRELATION_ID, request.getCorrelationId()))));
}
return audit;
return auditResponse;
}

protected void setResponseParams(Request request, Response response){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.swasth.hcx.managers.HealthCheckManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.swasth.common.utils.Constants.*;
Expand All @@ -41,25 +42,29 @@ public ResponseEntity<Object> status(@RequestBody Map<String, Object> requestBod
if (!hcxHeaders.containsKey(STATUS_FILTERS) || ((Map<String, Object>) hcxHeaders.get(STATUS_FILTERS)).isEmpty()) {
throw new ClientException("Invalid request, status filters is missing or empty.");
}
List<HeaderAudit> auditResponse = auditService.search(new SearchRequestDTO((Map<String, String>) hcxHeaders.get(STATUS_FILTERS)));
if(auditResponse.isEmpty()){
throw new ClientException("Invalid api call id is passed in status filters, details not found");
}
// Assuming a single result will be fetched for given api_call_id
HeaderAudit result = auditService.search(new SearchRequestDTO((Map<String, String>) hcxHeaders.get(STATUS_FILTERS))).get(0);
if (!hcxHeaders.get(SENDER_CODE).equals(result.getSender_code())) {
HeaderAudit auditData = auditResponse.get(0);
if (!hcxHeaders.get(SENDER_CODE).equals(auditData.getSender_code())) {
throw new ClientException("Request does not belongs to sender");
}
String entityType = result.getAction().split("/")[2];
String entityType = auditData.getAction().split("/")[2];
if (!STATUS_SEARCH_ALLOWED_ENTITIES.contains(entityType)) {
throw new ClientException("Invalid entity, status search allowed only for entities: " + STATUS_SEARCH_ALLOWED_ENTITIES);
}
StatusResponse statusResponse = new StatusResponse(entityType, result.getSender_code(), result.getRecipient_code(), (String) result.getStatus());
StatusResponse statusResponse = new StatusResponse(entityType, auditData.getSender_code(), auditData.getRecipient_code(), (String) auditData.getStatus());
Map<String,Object> statusResponseMap = JSONUtils.convert(statusResponse, HashMap.class);
if (result.getStatus().equals("request.queued")) {
if (auditData.getStatus().equals("request.queued")) {
response.setResult(statusResponseMap);
} else if (result.getStatus().equals("request.dispatched")) {
} else if (auditData.getStatus().equals("request.dispatched")) {
response.setResult(statusResponseMap);
processAndSendEvent(HCX_STATUS, topic, request);
} else {
// TODO: handle for other status
System.out.println("TODO for status " + result.getStatus());
System.out.println("TODO for status " + auditData.getStatus());
}
return new ResponseEntity<>(response, HttpStatus.ACCEPTED);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.swasth.common.dto;

import kong.unirest.Header;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.swasth.common.exception.ClientException;
Expand All @@ -25,7 +26,7 @@ public Request(Map<String, Object> body) throws Exception {
this.hcxHeaders = JSONUtils.decodeBase64String(((String) body.get(Constants.PAYLOAD)).split("\\.")[0], Map.class);
}

public void validate(List<String> mandatoryHeaders, HeaderAudit auditData, String apiAction, int timestampRange) throws ClientException {
public void validate(List<String> mandatoryHeaders, List<HeaderAudit> auditResponse, String apiAction, int timestampRange) throws ClientException {

List<String> missingHeaders = mandatoryHeaders.stream().filter(key -> !hcxHeaders.containsKey(key)).collect(Collectors.toList());
if (!missingHeaders.isEmpty()) {
Expand All @@ -36,7 +37,8 @@ public void validate(List<String> mandatoryHeaders, HeaderAudit auditData, Strin
}

if(ON_ACTION_APIS.contains(apiAction)) {
validateCondition(!getCorrelationId().equals(auditData.getCorrelation_id()), ErrorCodes.CLIENT_ERR_MISSING_CORRELATION_ID_RES, "Response contains invalid correlation id");
validateCondition(auditResponse.isEmpty(), ErrorCodes.CLIENT_ERR_MISSING_CORRELATION_ID_RES, "Response contains invalid correlation id");
HeaderAudit auditData = auditResponse.get(0);
if(!auditData.getWorkflow_id().isEmpty()) {
validateCondition(!getWorkflowId().equals(auditData.getWorkflow_id()), ErrorCodes.CLIENT_ERR_MISSING_WORKFLOW_ID_RES, "Response contains invalid correlation id");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ private Map<String,Object> getSearchFilters(){
}

@Override
public void validate(List<String> mandatoryHeaders, HeaderAudit auditData, String apiAction, int timestampRange) throws ClientException {
super.validate(mandatoryHeaders, auditData, apiAction, timestampRange);
public void validate(List<String> mandatoryHeaders, List<HeaderAudit> auditResponse, String apiAction, int timestampRange) throws ClientException {
super.validate(mandatoryHeaders, auditResponse, apiAction, timestampRange);

if(Constants.HCX_REGISTRY_CODE.equals(getRecipientCode())){
throw new ClientException(ErrorCodes.CLIENT_ERR_INVALID_SEARCH, "Search recipient code must be hcx registry code");
Expand Down

0 comments on commit b8a609f

Please sign in to comment.