Skip to content

Commit

Permalink
Merge pull request #6 from FINTmodels/feature-problems
Browse files Browse the repository at this point in the history
Add Problem type to event response.
  • Loading branch information
fsjovatsen authored Apr 25, 2018
2 parents b091c40 + 6586b75 commit e7830aa
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/main/java/no/fint/event/model/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -385,6 +386,26 @@ public void setStatusCode(String statusCode) {
response.setStatusCode(statusCode);
}

/**
* @return see {@link EventResponse#problems} for more information
*/
public List<Problem> getProblems() {
if (response == null) {
response = new EventResponse();
}
return response.getProblems();
}

/**
* @param problems See {@link EventResponse#problems} for more information
*/
public void setProblems(List<Problem> 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.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/no/fint/event/model/EventResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public class EventResponse implements Serializable {
Expand All @@ -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<Problem> problems;
}
19 changes: 19 additions & 0 deletions src/main/java/no/fint/event/model/Problem.java
Original file line number Diff line number Diff line change
@@ -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;
}
31 changes: 31 additions & 0 deletions src/test/groovy/no/fint/event/model/EventSpec.groovy
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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"
}
}
40 changes: 40 additions & 0 deletions src/test/groovy/no/fint/event/model/EventUtilSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
26 changes: 26 additions & 0 deletions src/test/resources/event.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit e7830aa

Please sign in to comment.