Search in sources :

Example 1 with FragmentData

use of com.adobe.cq.dam.cfm.FragmentData in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ContentFragmentMockAdapter method getMockElement.

/**
 * Creates a mock of a content element for a text-only (if {@code model} is {@code null}) or structured
 * (if {@code model} is not {@code null}) content fragment.
 */
private ContentElement getMockElement(Resource resource, String name, Resource model) {
    // get the respective element
    MockElement element;
    if (model == null) {
        element = getTextOnlyElement(resource, name);
    } else {
        element = getStructuredElement(resource, model, name);
    }
    if (element == null) {
        return null;
    }
    /* create mock objects */
    // mock data type
    DataType dataType = mock(DataType.class, withSettings().lenient());
    when(dataType.isMultiValue()).thenReturn(element.isMultiValued);
    when(dataType.getTypeString()).thenReturn(element.typeString);
    // mock fragment data
    FragmentData data = mock(FragmentData.class, withSettings().lenient());
    when(data.getValue()).thenReturn(element.isMultiValued ? element.values : element.values[0]);
    when(data.getValue(String.class)).thenReturn(element.values[0]);
    when(data.getValue(String[].class)).thenReturn(element.values);
    when(data.getContentType()).thenReturn(element.contentType);
    when(data.getDataType()).thenReturn(dataType);
    // mock content element
    ContentElement contentElement = mock(ContentElement.class, withSettings().lenient());
    when(contentElement.getName()).thenReturn(element.name);
    when(contentElement.getTitle()).thenReturn(element.title);
    when(contentElement.getContent()).thenReturn(element.values[0]);
    when(contentElement.getContentType()).thenReturn(element.contentType);
    when(contentElement.getValue()).thenReturn(data);
    // mock variations
    Map<String, ContentVariation> variations = new LinkedHashMap<>();
    for (MockVariation variation : element.variations.values()) {
        FragmentData variationData = mock(FragmentData.class, withSettings().lenient());
        when(variationData.getValue()).thenReturn(element.isMultiValued ? variation.values : variation.values[0]);
        when(variationData.getValue(String.class)).thenReturn(variation.values[0]);
        when(variationData.getValue(String[].class)).thenReturn(variation.values);
        when(variationData.getContentType()).thenReturn(variation.contentType);
        when(variationData.getDataType()).thenReturn(dataType);
        ContentVariation contentVariation = mock(ContentVariation.class, withSettings().lenient());
        when(contentVariation.getName()).thenReturn(variation.name);
        when(contentVariation.getTitle()).thenReturn(variation.title);
        when(contentVariation.getContent()).thenReturn(variation.values[0]);
        when(contentVariation.getContentType()).thenReturn(variation.contentType);
        when(contentVariation.getValue()).thenReturn(variationData);
        variations.put(variation.name, contentVariation);
    }
    when(contentElement.getVariations()).thenReturn(variations.values().iterator());
    when(contentElement.getVariation(any(String.class))).thenAnswer(invocation -> {
        String variationName = invocation.getArgument(0);
        return variations.get(variationName);
    });
    return contentElement;
}
Also used : ContentElement(com.adobe.cq.dam.cfm.ContentElement) ContentVariation(com.adobe.cq.dam.cfm.ContentVariation) DataType(com.adobe.cq.dam.cfm.DataType) FragmentData(com.adobe.cq.dam.cfm.FragmentData) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ContentElement (com.adobe.cq.dam.cfm.ContentElement)1 ContentVariation (com.adobe.cq.dam.cfm.ContentVariation)1 DataType (com.adobe.cq.dam.cfm.DataType)1 FragmentData (com.adobe.cq.dam.cfm.FragmentData)1 LinkedHashMap (java.util.LinkedHashMap)1