diff --git a/src/main/java/no/fint/event/model/Event.java b/src/main/java/no/fint/event/model/Event.java index 7fbeb0e..c9b0a51 100644 --- a/src/main/java/no/fint/event/model/Event.java +++ b/src/main/java/no/fint/event/model/Event.java @@ -333,7 +333,8 @@ public void addObject(Object data) { /** * @return see {@link EventRequest} for more information. */ - private EventRequest getRequest() { + @JsonIgnore + public EventRequest getRequest() { return request; } @@ -385,6 +386,26 @@ public void setStatusCode(String statusCode) { response.setStatusCode(statusCode); } + /** + * @return see {@link EventResponse#problems} for more information + */ + public List getProblems() { + if (response == null) { + response = new EventResponse(); + } + return response.getProblems(); + } + + /** + * @param problems See {@link EventResponse#problems} for more information + */ + public void setProblems(List problems) { + if (response == null) { + response = new EventResponse(); + } + response.setProblems(problems); + } + /** * Return true if the event has action HEALTH. * Is not included in the json representation. diff --git a/src/main/java/no/fint/event/model/EventResponse.java b/src/main/java/no/fint/event/model/EventResponse.java index 66d7a3c..c71b928 100644 --- a/src/main/java/no/fint/event/model/EventResponse.java +++ b/src/main/java/no/fint/event/model/EventResponse.java @@ -3,6 +3,7 @@ import lombok.Data; import java.io.Serializable; +import java.util.List; @Data public class EventResponse implements Serializable { @@ -25,4 +26,8 @@ public class EventResponse implements Serializable { */ private ResponseStatus responseStatus; + /** + * Shows the problems the system has found when processing this event's request. + */ + private List problems; } diff --git a/src/main/java/no/fint/event/model/Problem.java b/src/main/java/no/fint/event/model/Problem.java new file mode 100644 index 0000000..f8e51cf --- /dev/null +++ b/src/main/java/no/fint/event/model/Problem.java @@ -0,0 +1,19 @@ +package no.fint.event.model; + +import lombok.Data; + +@Data +public class Problem { + /** + * The name of the field the problem relates to. Either a simple name or a JSON Path to the field. + */ + private String field; + /** + * Human understandable message describing the problem. + */ + private String message; + /** + * Machine traceable error code indicating the type of problem. + */ + private String code; +} diff --git a/src/test/groovy/no/fint/event/model/EventSpec.groovy b/src/test/groovy/no/fint/event/model/EventSpec.groovy index 582ae8c..42eda0f 100644 --- a/src/test/groovy/no/fint/event/model/EventSpec.groovy +++ b/src/test/groovy/no/fint/event/model/EventSpec.groovy @@ -1,5 +1,7 @@ package no.fint.event.model +import com.fasterxml.jackson.databind.ObjectMapper +import groovy.json.JsonSlurper import spock.lang.Specification class EventSpec extends Specification { @@ -156,4 +158,33 @@ class EventSpec extends Specification { event.getMessage() event.getStatusCode() } + + def "Serialize response object to JSON"() { + given: + def event = new Event(action: 'UPDATE_SOMETHING', source: 'Spock', orgId: 'mock.no', client: 'none') + + when: + event.setResponseStatus(ResponseStatus.ACCEPTED) + event.setMessage("Doubleplus super") + event.setStatusCode("R2D2") + event.setProblems([new Problem(field: "monkey", message: "Only chimpanzees allowed", code: 9999)]) + + then: + event.getResponseStatus() == ResponseStatus.ACCEPTED + event.getMessage() + event.getStatusCode() + event.getProblems().size() == 1 + + when: + def jsonSlurper = new JsonSlurper() + def objectMapper = new ObjectMapper() + def result = objectMapper.writeValueAsString(event.getResponse()) + println(result) + def object = jsonSlurper.parseText(result) + + then: + object + object.problems + object.responseStatus == "ACCEPTED" + } } diff --git a/src/test/groovy/no/fint/event/model/EventUtilSpec.groovy b/src/test/groovy/no/fint/event/model/EventUtilSpec.groovy index f024d92..4286482 100644 --- a/src/test/groovy/no/fint/event/model/EventUtilSpec.groovy +++ b/src/test/groovy/no/fint/event/model/EventUtilSpec.groovy @@ -101,4 +101,44 @@ class EventUtilSpec extends Specification { event == null } + def "Read Event from event.json"() { + given: + def objectMapper = new ObjectMapper() + def json = "{\n" + + " \"corrId\": \"a91cdb9b-0292-4baf-9a27-578642634129\",\n" + + " \"action\": \"GET_ALL\",\n" + + " \"status\": \"NEW\",\n" + + " \"time\": 1524131147134,\n" + + " \"orgId\": \"rogfk.no\",\n" + + " \"source\": \"fk\",\n" + + " \"client\": \"myClient\",\n" + + " \"data\": [],\n" + + " \"message\": \"There is a disturbance in the Force\",\n" + + " \"query\": \"what\",\n" + + " \"problems\": [\n" + + " {\n" + + " \"field\": \"monkey\",\n" + + " \"message\": \"Only chimpanzees allowed\",\n" + + " \"code\": \"9999\"\n" + + " },\n" + + " {\n" + + " \"field\": \"jedi\",\n" + + " \"message\": \"Luke not found\",\n" + + " \"code\": \"44\"\n" + + " }\n" + + " ],\n" + + " \"statusCode\": \"JEDI-XX\",\n" + + " \"responseStatus\": \"ERROR\"\n" + + "}" + + when: + def event = objectMapper.readValue(getClass().getResourceAsStream("/event.json"), Event) + + then: + event + event.problems.size() == 2 + event.status == Status.NEW + event.responseStatus == ResponseStatus.ERROR + event.statusCode == "JEDI-XX" + } } diff --git a/src/test/resources/event.json b/src/test/resources/event.json new file mode 100644 index 0000000..241d04c --- /dev/null +++ b/src/test/resources/event.json @@ -0,0 +1,26 @@ +{ + "corrId": "a91cdb9b-0292-4baf-9a27-578642634129", + "action": "GET_ALL", + "status": "NEW", + "time": 1524131147134, + "orgId": "rogfk.no", + "source": "fk", + "client": "myClient", + "data": [], + "message": "There is a disturbance in the Force", + "query": "what", + "problems": [ + { + "field": "monkey", + "message": "Only chimpanzees allowed", + "code": "9999" + }, + { + "field": "jedi", + "message": "Luke not found", + "code": "44" + } + ], + "statusCode": "JEDI-XX", + "responseStatus": "ERROR" +}