public RequestBodyDTOMockery() { dtoJsonMockery = new DTOJsonMockery(); }
/** * Serialize the DTO illegal object deferred from its mockery as the content of a {@code RequestBody}. * @see DTO.Behaviour */ @Override public Object illegal(Metadata<RequestBodyDTO> metadata) { Object illegal = dtoJsonMockery.illegal(DTOJsonMetadata(metadata)); RequestBody requestBodyIllegal = RequestBody .create(MediaType.parse("text/plain"), illegal.toString()); return requestBodyIllegal; }
/** * Serialize the DTO legal object deferred from its mockery as the content of a {@code RequestBody}. * @see DTO.Behaviour */ @Override public Object legal(Metadata<RequestBodyDTO> metadata) { Object legal = dtoJsonMockery.legal(DTOJsonMetadata(metadata)); RequestBody requestBodyLegal = RequestBody .create(MediaType.parse("text/plain"), legal.toString()); return requestBodyLegal; }
/** * Serialize the DTO legal object deferred from its mockery as a json {@code String}. * @see DTO.Behaviour */ @Override public Object legal(Metadata<DTOJson> metadata) { JolyglotGenerics jolyglot = jolyglot(metadata); Object seed = dtoMockery.legal(DTOMetadata(metadata)); return jolyglot.toJson(seed); }
@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); }
/** * 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_Support_Type_Retrieved_Then_Is_String() { Type[] types = dtoJsonMockery.supportedTypes(null); assertThat(types.length, is(1)); Class clazz = (Class) types[0]; assertEquals(clazz, String.class); }
@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); }
/** * Deserialize the content of a json {@code String} as the DTO object deferred from its mockery to perform the validation. * @see DTO.Behaviour */ @Override public void validate(Metadata<DTOJson> metadata, Object candidate) throws AssertionError { if (candidate == null) { dtoMockery.validate(DTOMetadata(metadata), candidate); } Metadata<DTO> dtoMetadata = DTOMetadata(metadata); Type type = dtoMockery.supportedTypes(dtoMetadata)[0]; String json = (String) candidate; JolyglotGenerics jolyglot = jolyglot(metadata); Object object; try { object = jolyglot.fromJson(json, type); } catch (RuntimeException e) { object = null; } dtoMockery.validate(DTOMetadata(metadata), object); }
@Test public void When_Class_Has_Not_Json_Converter_Then_Throw_Exception() throws NoSuchMethodException { Method method = Providers.class.getDeclaredMethod("mock", String.class); DTOJson annotation = (DTOJson) method.getParameterAnnotations()[0][0]; Type type = method.getGenericParameterTypes()[0]; Metadata<DTOJson> metadata = new Metadata<>(Providers.class, method, null, annotation, type); exception.expect(RuntimeException.class); dtoJsonMockery.legal(metadata); }
@Test public void When_Call_Illegal_Then_Then_Get_Empty_String() 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 illegal = (String) dtoJsonMockery.illegal(metadata); assertTrue(illegal.isEmpty()); }
@Before public void init() { dtoJsonMockery = new DTOJsonMockery(); }
@Test public void When_Call_Legal_Then_Get_Legal() 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); assertThat(json, is(jsonMockSample())); }