/** * Deserialize the content of a {@code RequestBody} as the DTO object and defer to its mockery to perform the validation. * @see ValidMockery */ @Override public void validate(Metadata<RequestBodyValid> metadata, Object candidate) throws AssertionError { try { RequestBody requestBody = (RequestBody) candidate; Buffer buffer = new Buffer(); requestBody.writeTo(buffer); String body = buffer.readUtf8(); validMockery.validate(validMetadata(metadata), body); } catch (IOException e) { throw new RuntimeException(e); } }
@Theory @Test public void When_Pass_Validation_Then_Do_Not_Throw_Assertion_Error(Data data) throws NoSuchMethodException { String methodName = data.methodName; Method method = Providers.class.getDeclaredMethod(methodName, data.classParam); Valid annotation = (Valid) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Valid> metadata = new Metadata<>(Providers.class, method, null, annotation, type); for (Object legal : data.legal) { validMockery.validate(metadata, legal); } }
@Theory @Test public void When_Call_Legal_Then_Get_Legal(Data data) throws NoSuchMethodException { String methodName = data.methodName; Method method = Providers.class.getDeclaredMethod(methodName, data.classParam); Valid annotation = (Valid) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Valid> metadata = new Metadata<>(Providers.class, method, null, annotation, type); for (int i = 0; i < 50; i++) { Object result = validMockery.legal(metadata); assertThat(result, instanceOf(data.classParam)); validMockery.validate(metadata, result); } }
@Theory @Test public void When_Not_Pass_Validation_Then_Throw_Assertion_Error(Data data) throws NoSuchMethodException { String methodName = data.methodName; Method method = Providers.class.getDeclaredMethod(methodName, data.classParam); Valid annotation = (Valid) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Valid> metadata = new Metadata<>(Providers.class, method, null, annotation, type); for (Object illegal : data.illegal) { try { validMockery.validate(metadata, illegal); fail(); } catch (AssertionError e) { String expectedMessage = validMockery.errorMessage(String.valueOf(illegal), metadata.getAnnotation().value()); assertThat(expectedMessage, is(e.getMessage())); } } }