public DTOJsonMockery() { dtoMockery = new DTOMockery(); instantiateInterface = new InstantiateInterface(); }
@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); }
/** * 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_Support_Type_List_Then_Get_Its_Type() throws NoSuchMethodException { Method method = Providers.class.getDeclaredMethod("DTOListMock"); DTO annotation = method.getAnnotation(DTO.class); Type type = method.getGenericReturnType(); Metadata<DTO> metadata = new Metadata<>(Providers.class, method, null, annotation, type); Type[] types = dtoMockery.supportedTypes(metadata); assertThat(types.length, is(1)); ParameterizedType listType = (ParameterizedType) types[0]; assertEquals(listType.getRawType(), List.class); Class mockType = (Class) listType.getActualTypeArguments()[0]; assertEquals(mockType, Mock.class); }
/** * 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_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); }
@Test public void When_Call_Illegal_Then_Get_Null() 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 illegal = dtoMockery.illegal(metadata); assertNull(illegal); }
@Test public void When_Support_Type_Object_Then_Get_Its_Type() 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); Type[] types = dtoMockery.supportedTypes(metadata); assertThat(types.length, is(1)); Class clazz = (Class) types[0]; assertEquals(clazz, Mock.class); }
@Test public void When_Call_Legal_Then_Get_Legal() 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); assertThat(mock.s1, is(DTOArgsPass.class.getName())); }
@Test public void When_Support_Type_Raw_List_Then_Get_Its_Type() throws NoSuchMethodException { Method method = Providers.class.getDeclaredMethod("DTORawListMock"); DTO annotation = method.getAnnotation(DTO.class); Type type = method.getGenericReturnType(); Metadata<DTO> metadata = new Metadata<>(Providers.class, method, null, annotation, type); Type[] types = dtoMockery.supportedTypes(metadata); assertThat(types.length, is(1)); Class clazz = (Class) types[0]; assertEquals(clazz, List.class); }
@Before public void init() { dtoMockery = new DTOMockery(); }
@Test public void When_Support_Type_Map_Then_Get_Its_Type() throws NoSuchMethodException { Method method = Providers.class.getDeclaredMethod("DTOMapMockMock"); DTO annotation = method.getAnnotation(DTO.class); Type type = method.getGenericReturnType(); Metadata<DTO> metadata = new Metadata<>(Providers.class, method, null, annotation, type); Type[] types = dtoMockery.supportedTypes(metadata); assertThat(types.length, is(1)); ParameterizedType mapType = (ParameterizedType) types[0]; assertEquals(mapType.getRawType(), Map.class); Class stringType = (Class) mapType.getActualTypeArguments()[0]; assertEquals(stringType, String.class); Class mockType = (Class) mapType.getActualTypeArguments()[1]; assertEquals(mockType, Mock.class); }