### Question:
ReferMetadataProvider { public ReferMetadata createReferMetadata(Type type, String ref) { Type actualType = type; if (isCollection(type)) { actualType = getCollectionGeneric(type); } for (ReferMetadataCreator creator : creators) { if (creator.isApplicable(actualType, ref.replace("[*]", ""))) { return creator.getReferMetadata(actualType, ref); } } throw new IllegalArgumentException(String.format("Unable to refer to field %s " + "Class doesn't implement map interface and " + "class doesn't have public field and " + "class doesn't have appropriate getter.", ref)); }  ReferMetadata createReferMetadata(Type type, String ref);  }### Answer:
@Test void shouldThrowExceptionWhenUnableToRefer() { String ref = "field"; ReferMetadataProvider referMetadataProvider = new ReferMetadataProvider(); assertThatThrownBy(() -> referMetadataProvider.createReferMetadata(InapplicableClass.class, ref)).isExactlyInstanceOf(IllegalArgumentException.class); }
@Test void shouldPreferMapWhenMapGetterFieldWhenNoGroupingOperator() { String ref = "field"; ReferMetadataProvider referMetadataProvider = new ReferMetadataProvider(); ReferMetadata referMetadata = referMetadataProvider.createReferMetadata(MapFieldGetter.class, ref); ReferMetadata expected = new ReferMetadata(String.class, "get(\"field\")", "field"); assertThat(referMetadata).isEqualTo(expected); }
@Test void shouldPreferMapWhenMapGetterWhenNoGroupingOperator() { String ref = "field"; ReferMetadataProvider referMetadataProvider = new ReferMetadataProvider(); ReferMetadata referMetadata = referMetadataProvider.createReferMetadata(MapGetter.class, ref); ReferMetadata expected = new ReferMetadata(String.class, "get(\"field\")", "field"); assertThat(referMetadata).isEqualTo(expected); }
@Test void shouldPreferMapWhenMapFieldWhenNoGroupingOperator() { String ref = "field"; ReferMetadataProvider referMetadataProvider = new ReferMetadataProvider(); ReferMetadata referMetadata = referMetadataProvider.createReferMetadata(MapField.class, ref); ReferMetadata expected = new ReferMetadata(String.class, "get(\"field\")", "field"); assertThat(referMetadata).isEqualTo(expected); }
@Test void shouldPreferFieldWhenGetterFieldWhenNoGroupingOperator() { String ref = "field"; ReferMetadataProvider referMetadataProvider = new ReferMetadataProvider(); ReferMetadata referMetadata = referMetadataProvider.createReferMetadata(FieldGetter.class, ref); ReferMetadata expected = new ReferMetadata(String.class, "field", "field"); assertThat(referMetadata).isEqualTo(expected); }
@Test void shouldPreferMapWhenMapGetterFieldWhenGroupingOperator() { String ref = "field[*]"; ReferMetadataProvider referMetadataProvider = new ReferMetadataProvider(); ReferMetadata referMetadata = referMetadataProvider.createReferMetadata(MapFieldGetter.class, ref); ReferMetadata expected = new ReferMetadata(String.class, "get(\"field\")", ref); assertThat(referMetadata).isEqualTo(expected); }
@Test void shouldPreferMapWhenMapGetterWhenGroupingOperator() { String ref = "field[*]"; ReferMetadataProvider referMetadataProvider = new ReferMetadataProvider(); ReferMetadata referMetadata = referMetadataProvider.createReferMetadata(MapGetter.class, ref); ReferMetadata expected = new ReferMetadata(String.class, "get(\"field\")", ref); assertThat(referMetadata).isEqualTo(expected); }
@Test void shouldPreferMapWhenMapFieldWhenGroupingOperator() { String ref = "field[*]"; ReferMetadataProvider referMetadataProvider = new ReferMetadataProvider(); ReferMetadata referMetadata = referMetadataProvider.createReferMetadata(MapField.class, ref); ReferMetadata expected = new ReferMetadata(String.class, "get(\"field\")", ref); assertThat(referMetadata).isEqualTo(expected); }
@Test void shouldPreferFieldWhenGetterFieldWhenGroupingOperator() { String ref = "field[*]"; ReferMetadataProvider referMetadataProvider = new ReferMetadataProvider(); ReferMetadata referMetadata = referMetadataProvider.createReferMetadata(FieldGetter.class, ref); ReferMetadata expected = new ReferMetadata(String.class, "field", ref); assertThat(referMetadata).isEqualTo(expected); }