public RequestBodyValidMockery() { validMockery = new ValidMockery(); }
/** * Validate {@code candidate} based on the regex pattern supplied. */ @Override public void validate(Metadata<Valid> metadata, Object candidate) throws AssertionError { Valid valid = metadata.getAnnotation(); String regex = valid.value(); String input = String.valueOf(candidate); if (!input.matches(regex)) throw new AssertionError(errorMessage(input,regex)); }
/** * Serialize the illegal object as content of a {@code RequestBody} deferring to {@link ValidMockery} for object creation. * @see ValidMockery */ @Override public Object illegal(Metadata<RequestBodyValid> metadata) { Object illegal = validMockery.illegal(validMetadata(metadata)); illegal = illegal != null ? illegal.toString() : ""; RequestBody requestBodyIllegal = RequestBody .create(MediaType.parse("text/plain"), illegal.toString()); return requestBodyIllegal; }
@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())); } } }
/** * Serialize the legal object as content of a {@code RequestBody} deferring to {@link ValidMockery} for object creation. * @see ValidMockery */ @Override public Object legal(Metadata<RequestBodyValid> metadata) { Object legal = validMockery.legal(validMetadata(metadata)); RequestBody requestBodyLegal = RequestBody .create(MediaType.parse("text/plain"), legal.toString()); return requestBodyLegal; }
/** * 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); } }
@Test public void When_Support_Type_Retrieved_Then_Is_Numeric_And_Text() { Type[] supportedTypes = validMockery.supportedTypes(null); assertThat(supportedTypes.length, is(10)); Type[] expectedTypes = SupportedTypes.concat(SupportedTypes.NUMERIC, SupportedTypes.TEXT); for (Type expected : expectedTypes) { assertTrue(Arrays.asList(supportedTypes).contains(expected)); } }
@Theory @Test public void When_Legal_With_Default_Value_Then_Get_Default(Data data) throws NoSuchMethodException { String methodName = data.methodName + "WithDefault"; 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); String result = String .valueOf(validMockery.legal(metadata)); assertThat(result, is(data.defaultSeed)); }
@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); } }
@Test public void When_Illegal_With_Default_Value_Then_Get_Default() throws NoSuchMethodException { Method method = ProvidersIllegal.class .getDeclaredMethod("checkIllegalDefault", String.class); Valid annotation = (Valid) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Valid> metadata = new Metadata<>(Providers.class, method, null, annotation, type); assertThat(((String)validMockery.illegal(metadata)), is("illegal")); }
@Before public void init() { validMockery = new ValidMockery(); }
@Test public void When_Call_Illegal_With_Primitive_Type_Then_Illegal_Matches_Expected_Type() throws NoSuchMethodException { Method method = ProvidersIllegal.class.getDeclaredMethod("checkIllegalsPrimitives", int.class, long.class, double.class, float.class); Valid annotation = (Valid) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<Valid> metadata = new Metadata<>(Providers.class, method, null, annotation, type); assertThat((int) validMockery.illegal(metadata), is(0)); annotation = (Valid) method.getParameterAnnotations()[1][0]; type = method.getGenericParameterTypes()[1]; metadata = new Metadata<>(Providers.class, method, null, annotation, type); assertThat((long) validMockery.illegal(metadata), is(0l)); annotation = (Valid) method.getParameterAnnotations()[2][0]; type = method.getGenericParameterTypes()[2]; metadata = new Metadata<>(Providers.class, method, null, annotation, type); assertThat((double) validMockery.illegal(metadata), is(0d)); annotation = (Valid) method.getParameterAnnotations()[3][0]; type = method.getGenericParameterTypes()[3]; metadata = new Metadata<>(Providers.class, method, null, annotation, type); assertThat((float) validMockery.illegal(metadata), is(0f)); }
Metadata<Valid> metadata = new Metadata<>(Providers.class, method, null, annotation, type); assertTrue(((String)validMockery.illegal(metadata)) .isEmpty()); metadata = new Metadata<>(Providers.class, method, null, annotation, type); assertThat(((Integer)validMockery.illegal(metadata)), is(0)); metadata = new Metadata<>(Providers.class, method, null, annotation, type); assertThat(((Long)validMockery.illegal(metadata)), is(0l)); metadata = new Metadata<>(Providers.class, method, null, annotation, type); assertThat(((Double)validMockery.illegal(metadata)), is(0d)); metadata = new Metadata<>(Providers.class, method, null, annotation, type); assertThat(((Float)validMockery.illegal(metadata)), is(0f));