Solved
wants to cover 100% junits on below model class, Any reference code ?
@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class PageModel {
@586265
private Page currentPage;
@586265
private ValueMap properties;
@586265
private InheritanceValueMap pageProperties;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* method to fetch page properties data of each tag and to return the tag titles
*
* @90521 propertyName
* @90521 isMulti
* @2007960
*/
public String getTagTitles(String propertyName, boolean isMulti, boolean isInherited) {
String tagTitle = null;
String tagPath = null;
String businessTagPath = null;
Value[] values = null;
try {
logger.debug("Inside getTagTitles: {}", currentPage);
ResourceResolver resourceResolver = currentPage.getContentResource().getResourceResolver();
if (isMulti) {
logger.debug("Inside getTagTitles multi tag property: {}", propertyName);
if (isInherited) {
values = null != pageProperties ? pageProperties.getInherited(propertyName, Value[].class) : null;
} else {
values = null != properties ? properties.get(propertyName, Value[].class) : null;
}
StringBuffer tagBuffer = new StringBuffer();
if (values != null) {
for (Value typeTagId : values) {
tagPath = typeTagId.getString();
tagBuffer.append(getTagTitle(resourceResolver, tagPath) + ";");
}
if (tagBuffer.length() != 0)
tagBuffer.deleteCharAt(tagBuffer.length() - 1);
}
tagTitle = tagBuffer.toString();
} else {
logger.debug("Inside getTagTitles single tag property: {}", propertyName);
if (isInherited) {
if (StringUtils.equalsIgnoreCase(propertyName, "cq:division")) {
if (StringUtils.isNotEmpty(properties.get("cq:businessArea", null))) {
tagPath = null != properties ? properties.get(propertyName, null) : null;
businessTagPath = null != properties ? properties.get("cq:businessArea", null) : null;
} else {
tagPath = null != pageProperties ? pageProperties.getInherited(propertyName, null) : null;
businessTagPath = null != pageProperties
? pageProperties.getInherited("cq:businessArea", null)
: null;
}
if (null != tagPath && !tagPath.startsWith(businessTagPath)) {
return null;
}
} else {
tagPath = null != pageProperties ? pageProperties.getInherited(propertyName, null) : null;
}
} else {
tagPath = null != properties ? properties.get(propertyName, null) : null;
}
tagTitle = getTagTitle(resourceResolver, tagPath);
}
} catch (RepositoryException e) {
logger.error("Error in getting tag title: {}", e);
}
logger.debug("Inside getTagTitles tag title before return: {}", tagTitle);
return tagTitle;
}
/**
* Method to get title from tag manager
*
* @90521 resourceResolver
* @90521 tagPath
* @2007960
*/
private static String getTagTitle(ResourceResolver resourceResolver, String tagPath) {
String tagTitle = null;
TagManager tagManager = resourceResolver.adaptTo(TagManager.class);
Tag resolve = Objects.requireNonNull(tagManager).resolve(tagPath);
if (null != resolve)
tagTitle = resolve.getTitle();
return tagTitle;
}
public String getContentLanguage() {
return getTagTitles("cq:contentLang", false, false);
}
public String getCountry() {
return getTagTitles("cq:country", true, true);
}