@Test public void When_Pass_Validation_Then_Do_Not_Throw_Assertion_Error() throws NoSuchMethodException, IOException { Method method = Providers.class.getDeclaredMethod("string", String.class); RequestBodyValid annotation = (RequestBodyValid) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<RequestBodyValid> metadata = new Metadata<>(Providers.class, method, null, annotation, type); RequestBody requestBody = (RequestBody) requestBodyValidMockery.legal(metadata); requestBodyValidMockery.validate(metadata, requestBody); }
/** * 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; }
@Test public void When_Call_Illegal_Then_Get_RequestBody_Illegal() throws NoSuchMethodException, IOException { Method method = Providers.class.getDeclaredMethod("string", String.class); RequestBodyValid annotation = (RequestBodyValid) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<RequestBodyValid> metadata = new Metadata<>(Providers.class, method, null, annotation, type); RequestBody requestBody = (RequestBody) requestBodyValidMockery.illegal(metadata); assertNotNull(requestBody); assertThat(requestBody.contentType().toString(), is("text/plain; charset=utf-8")); Buffer buffer = new Buffer(); requestBody.writeTo(buffer); RequestBody candidate = RequestBody.create(MediaType.parse("text/plain"), buffer.readUtf8()); exception.expect(AssertionError.class); requestBodyValidMockery.validate(metadata, candidate); }
@Before public void init() { requestBodyValidMockery = new RequestBodyValidMockery(); }
@Test public void When_Not_Pass_Validation_Then_Throw_Assertion_Error() throws NoSuchMethodException { Method method = Providers.class.getDeclaredMethod("string", String.class); RequestBodyValid annotation = (RequestBodyValid) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<RequestBodyValid> metadata = new Metadata<>(Providers.class, method, null, annotation, type); RequestBody requestBodySeed = RequestBody.create(MediaType.parse("text/plain"), ""); exception.expect(AssertionError.class); requestBodyValidMockery.validate(metadata, requestBodySeed); }
@Test public void When_Support_Type_Retrieved_Then_Is_Request_Body() throws NoSuchMethodException { Method method = Providers.class.getDeclaredMethod("string", String.class); RequestBodyValid annotation = (RequestBodyValid) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<RequestBodyValid> metadata = new Metadata<>(Providers.class, method, null, annotation, type); Type[] types = requestBodyValidMockery.supportedTypes(metadata); assertThat(types.length, is(1)); Class clazz = (Class) types[0]; assertEquals(clazz, RequestBody.class); }
@Test public void When_Call_Legal_With_Default_Then_Get_RequestBody_With_Default_Legal() throws NoSuchMethodException, IOException { Method method = Providers.class.getDeclaredMethod("stringWithDefault", String.class); RequestBodyValid annotation = (RequestBodyValid) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<RequestBodyValid> metadata = new Metadata<>(Providers.class, method, null, annotation, type); RequestBody requestBody = (RequestBody) requestBodyValidMockery.legal(metadata); assertNotNull(requestBody); assertThat(requestBody.contentType().toString(), is("text/plain; charset=utf-8")); Buffer buffer = new Buffer(); requestBody.writeTo(buffer); assertThat(buffer.readUtf8(), is("s1")); }
@Test public void When_Call_Illegal_With_Default_Then_Get_RequestBody_With_Default_Illegal() throws NoSuchMethodException, IOException { Method method = Providers.class.getDeclaredMethod("stringWithDefault", String.class); RequestBodyValid annotation = (RequestBodyValid) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<RequestBodyValid> metadata = new Metadata<>(Providers.class, method, null, annotation, type); RequestBody requestBody = (RequestBody) requestBodyValidMockery.illegal(metadata); assertNotNull(requestBody); assertThat(requestBody.contentType().toString(), is("text/plain; charset=utf-8")); Buffer buffer = new Buffer(); requestBody.writeTo(buffer); assertThat(buffer.readUtf8(), is("s1")); }
@Test public void When_Call_Legal_Then_Get_RequestBody_Legal() throws NoSuchMethodException, IOException { Method method = Providers.class.getDeclaredMethod("string", String.class); RequestBodyValid annotation = (RequestBodyValid) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<RequestBodyValid> metadata = new Metadata<>(Providers.class, method, null, annotation, type); RequestBody requestBody = (RequestBody) requestBodyValidMockery.legal(metadata); assertNotNull(requestBody); assertThat(requestBody.contentType().toString(), is("text/plain; charset=utf-8")); Buffer buffer = new Buffer(); requestBody.writeTo(buffer); RequestBody candidate = RequestBody.create(MediaType.parse("text/plain"), buffer.readUtf8()); requestBodyValidMockery.validate(metadata, candidate); }
/** * 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; }
/** * 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); } }