/** * Deserialize the content of a {@code RequestBody} as the DTO object and defer to its mockery to perform the validation. * @see DTO.Behaviour */ @Override public void validate(Metadata<RequestBodyDTO> metadata, Object candidate) throws AssertionError { if (candidate == null) { dtoJsonMockery.validate(DTOJsonMetadata(metadata), candidate); } try { RequestBody requestBody = (RequestBody) candidate; Buffer buffer = new Buffer(); requestBody.writeTo(buffer); String body = buffer.readUtf8(); buffer.close(); dtoJsonMockery.validate(DTOJsonMetadata(metadata), body); } catch (IOException e) { throw new RuntimeException(e); } }
@Test public void When_Pass_Validation_Then_Do_Not_Throw_Assertion_Error() throws NoSuchMethodException { Method method = ProvidersJsonConverter.class.getDeclaredMethod("mock", String.class); DTOJson annotation = (DTOJson) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<DTOJson> metadata = new Metadata<>(ProvidersJsonConverter.class, method, null, annotation, type); String json = (String) dtoJsonMockery.legal(metadata); dtoJsonMockery.validate(metadata, json); }
@Test public void When_Not_Pass_Validation_Then_Throw_Assertion_Error() throws NoSuchMethodException { Method method = ProvidersJsonConverter.class.getDeclaredMethod("mock", String.class); DTOJson annotation = (DTOJson) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<DTOJson> metadata = new Metadata<>(ProvidersJsonConverter.class, method, null, annotation, type); exception.expect(AssertionError.class); dtoJsonMockery.validate(metadata, null); }