### Question:
ProfileAttributesResolverImpl implements ProfileAttributesResolver { protected String getErrorFieldName(String templateString, String attrPlaceholder) { String attrLiteral = Pattern.quote(attrPlaceholder); Supplier<Pattern> findJsonFieldNamePattern = () -> Pattern.compile("(?m)\"(?<name>[\\w$]+)\"\\s*:\\s*\"([^\"]|(\\\\\"))*" + attrLiteral); Supplier<Pattern> findXmlFieldNamePattern = () -> Pattern.compile("(?s)<(?<name>[\\w.-]+)((?!<[\\w.-]+).)+" + attrLiteral); return Stream.of(findJsonFieldNamePattern, findXmlFieldNamePattern) .map(pattern -> { Matcher matcher = pattern.get().matcher(templateString); if (matcher.find()) { return matcher.group("name"); } else { return ""; } }) .filter(name -> !name.isEmpty()) .findFirst() .orElse(""); } ProfileAttributesResolverImpl(); @PostConstruct void init(); static boolean isSkipProfileAttributesResolving(AttributedObject resource); @Override boolean containsAttribute(String str); @Override boolean isParametrizedResource(Object resource, ProfileAttributeCategory... categories); @Override @SuppressWarnings("unchecked") T mergeResource(T resource); T mergeObject(T object, String identifier); String merge(String templateString, String identifier); String merge(String templateString, String identifier, ProfileAttributeEscapeStrategy escapeStrategy); void setAttributePlaceholderPatterns(List<String> attributePlaceholderPatterns); void setProfileAttributeService(ProfileAttributeService profileAttributeService); void setMessageSource(MessageSource messageSource); void setProfileAttributeCategories(List<ProfileAttributeCategory> profileAttributeCategories); void setExcludedResourcesFromAttrResolving(Set<String> excludedResourcesFromAttrResolving); void setEnabledResolving(boolean enabledResolving); void setParametrizedResourcePatterns(List<String> parametrizedResourcePatterns); void setValidator(Validator validator); static final String attributeNameGroup; static final String categoryGroup; static final String groupExpression; static final String PROFILE_ATTRIBUTE_EXCEPTION_SUBSTITUTION_BASE; static final String IN_RESOURCE_SUFFIX; }### Answer:
@Test public void getErrorFieldName_jsonFile_returnFieldName() { String fieldName = profileAttributesResolverImpl.getErrorFieldName( "{\n" + " \"name1\": \"value1\",\n" + " \"name2\": \"${attr1}\"\n" + "}", "${attr1}"); assertEquals(fieldName, "name2"); }
@Test public void getErrorFieldName_xmlFile_returnFieldName() { String fieldName = profileAttributesResolverImpl.getErrorFieldName( "\n" + "<el1>\n" + " <el2 attr1 = \"123\">\n" + " <el3 attr2=\"1\" attr3 = \"12\">\n" + " 0e 3e\n" + " 7e 52 ${attr1} 0a 57\n" + " </el3>\n" + " </el2>\n" + "</el1>", "${attr1}"); assertEquals(fieldName, "el3"); }
@Test public void getErrorFieldName_improperInputString_returnEmptyString() { String fieldName = profileAttributesResolverImpl.getErrorFieldName("abracadabra tra-la-la", "${attr1}"); assertEquals("", fieldName); }