JsonSerializerImpl implements JsonSerializer { @Override public Result resultFrom(String response) { Matcher codeMatcher = CODE_PATTERN.matcher(response); if (!codeMatcher.find()) { return new Result.Builder() .code(1) .body(response) .build(); } String codeStr = codeMatcher.group(1); int code; try { code = Integer.parseInt(codeStr); } catch (Exception e) { code = -1; } Pattern p; if (code == 0) { p = UUID_PATTERN; } else { p = MESSAGE_PATTERN; } Matcher m = p.matcher(response); m.find(); String body = m.group(1); return new Result.Builder() .code(code) .body(body) .build(); } JsonSerializerImpl();  JsonSerializerImpl(boolean prettyPrint); @Override String toJson(Payload payload); @Override Result resultFrom(String response);  }
@Test public void shouldDeserializeErrorResponse() { Result result = new Result.Builder() .code(1) .body(ERROR_MESSAGE) .build(); JsonSerializerImpl sut = new JsonSerializerImpl(); assertThat(sut.resultFrom(ERROR_RESPONSE), is(result)); }
@Test public void shouldDeserializeUnexpectedErrorResponse() { Result result = new Result.Builder() .code(1) .body(UNEXPECTED_ERROR_RESPONSE) .build(); JsonSerializerImpl sut = new JsonSerializerImpl(); assertThat(sut.resultFrom(UNEXPECTED_ERROR_RESPONSE), is(result)); }
@Test public void shouldDeserializeSuccessResponse() { Result result = new Result.Builder() .code(0) .body(UUID) .build(); JsonSerializerImpl sut = new JsonSerializerImpl(); assertThat(sut.resultFrom(SUCCESS_RESPONSE), is(result)); }