diff --git a/src/main/java/org/rundeck/client/api/model/ErrorResponse.java b/src/main/java/org/rundeck/client/api/model/ErrorResponse.java index 2b5a2b3a..f144c331 100644 --- a/src/main/java/org/rundeck/client/api/model/ErrorResponse.java +++ b/src/main/java/org/rundeck/client/api/model/ErrorResponse.java @@ -18,10 +18,9 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import org.rundeck.client.util.Xml; -import org.simpleframework.xml.Attribute; -import org.simpleframework.xml.Element; -import org.simpleframework.xml.Path; -import org.simpleframework.xml.Root; +import org.simpleframework.xml.*; + +import java.util.List; @JsonIgnoreProperties(ignoreUnknown = true) @Root(strict = false) @@ -39,10 +38,14 @@ public class ErrorResponse implements ErrorDetail { @Path("error") public String errorCode; - @Element + @Element(required = false) @Path("error") public String message; + @Path("error") + @ElementList(entry = "message", required = false) + public List messages; + public String toCodeString() { if (null != errorCode) { return String.format( @@ -61,7 +64,11 @@ public String getErrorCode() { @Override public String getErrorMessage() { - return message != null ? message : error; + return message != null ? message : + messages != null ? messages.size() == 1 + ? messages.get(0) + : messages.toString() + : error; } @Override diff --git a/src/test/groovy/org/rundeck/client/api/model/ErrorResponseSpec.groovy b/src/test/groovy/org/rundeck/client/api/model/ErrorResponseSpec.groovy index 7277e8ec..c0c1d29b 100644 --- a/src/test/groovy/org/rundeck/client/api/model/ErrorResponseSpec.groovy +++ b/src/test/groovy/org/rundeck/client/api/model/ErrorResponseSpec.groovy @@ -7,6 +7,7 @@ import retrofit2.Retrofit import retrofit2.converter.jackson.JacksonConverterFactory import retrofit2.converter.simplexml.SimpleXmlConverterFactory import spock.lang.Specification +import spock.lang.Unroll import java.lang.annotation.Annotation @@ -15,6 +16,7 @@ import java.lang.annotation.Annotation * @since 12/13/17 */ class ErrorResponseSpec extends Specification { + @Unroll def "xml parse code not required"() { given: def retrofit = new Retrofit.Builder().baseUrl('http://test'). @@ -34,6 +36,8 @@ class ErrorResponseSpec extends Specification { result.errorCode == code result.error == error result.message == message + result.messages == null + result.errorMessage == message result.apiVersion == version where: @@ -51,4 +55,88 @@ class ErrorResponseSpec extends Specification { 'blah' | 20 } + + @Unroll + def "xml parse multi message"() { + given: + def retrofit = new Retrofit.Builder().baseUrl('http://test'). + addConverterFactory(SimpleXmlConverterFactory.create()). + build() + + when: + Converter converter = retrofit.responseBodyConverter( + ErrorResponse.class, + [] as Annotation[], + ); + ErrorResponse result = converter.convert(ResponseBody.create(MediaType.parse('application/xml'), xmlText)) + + then: + + result != null + result.errorCode == code + result.error == error + result.messages == messages + result.errorMessage == messages.toString() + result.apiVersion == version + + where: + xmlText | + code | + error | + messages | + version + 'blahblah2' | + 'xyz' | + 'true' | + ['blah', 'blah2'] | + 20 + + } + + def "json parse with multiple messages"() { + given: + def retrofit = new Retrofit.Builder().baseUrl('http://test'). + addConverterFactory(JacksonConverterFactory.create()). + build() + + when: + Converter converter = retrofit.responseBodyConverter( + ErrorResponse.class, + [] as Annotation[], + ); + ErrorResponse result = converter.convert( + ResponseBody.create(MediaType.parse('application/json'), jsonText) + ) + + then: + + result != null + result.errorCode == code + result.error == error + result.message == message + result.messages == messages + result.apiVersion == version + + where: + jsonText | + code | + error | + message | + messages | + version + '{"error":true,"apiversion":21,"errorCode":"test","messages":["a","b"]}' | + 'test' | + 'true' | + null | + ['a', 'b'] | + 21 + '{"error":true,"apiversion":21,"errorCode":"api.error.unknown","message":"A message"}' | + 'api.error.unknown' | + 'true' | + 'A message' | + null | + 21 + + } }