/** * 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_Pass_Validation_Then_Do_Not_Throw_Assertion_Error() throws NoSuchMethodException { Method method = Providers.class.getDeclaredMethod("DTO"); DTO annotation = method.getAnnotation(DTO.class); Type type = method.getGenericReturnType(); Object[] args = {DTOArgsPass.class.getName()}; Metadata<DTO> metadata = new Metadata<>(Providers.class, method, args, annotation, type); Mock mock = (Mock) dtoMockery.legal(metadata); dtoMockery.validate(metadata, mock); }
@Test public void When_Not_Pass_Validation_Then_Throw_Assertion_Error() throws NoSuchMethodException { Method method = Providers.class.getDeclaredMethod("DTO"); DTO annotation = method.getAnnotation(DTO.class); Type type = method.getGenericReturnType(); Metadata<DTO> metadata = new Metadata<>(Providers.class, method, null, annotation, type); Object object = new Mock(""); exception.expect(AssertionError.class); dtoMockery.validate(metadata, object); }