@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); }
@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_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); }
@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); }
/** * 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); }