Skip to content

Commit

Permalink
implement MockContentFragment_ContentElement_Structured getValue() me…
Browse files Browse the repository at this point in the history
…thod
  • Loading branch information
Koen Kicken authored and Koen Kicken committed Nov 20, 2023
1 parent e18f0c7 commit 2d86a49
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ public void removeVariation(ContentVariation variation) throws ContentFragmentEx
props.remove(structuredDataKey);
}

@Override
public FragmentData getValue() {
Object value = structuredDataProps.get(structuredDataKey);
return new MockFragmentData(value, value.getClass().isArray());
}

// --- unsupported operations ---

Expand All @@ -168,11 +173,6 @@ public ContentVariation getResolvedVariation(String variationName) {
throw new UnsupportedOperationException();
}

@Override
public FragmentData getValue() {
throw new UnsupportedOperationException();
}

@Override
public void setValue(FragmentData fragmentData) throws ContentFragmentException {
throw new UnsupportedOperationException();
Expand Down
37 changes: 37 additions & 0 deletions core/src/main/java/io/wcm/testing/mock/aem/MockDataType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.wcm.testing.mock.aem;

import com.adobe.cq.dam.cfm.DataType;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class MockDataType implements DataType {

boolean isArray;

public MockDataType(boolean isArray) {
this.isArray = isArray;
}

@Override
public @Nullable String getSemanticType() {
return StringUtils.EMPTY;
}

@Override
public boolean isMultiValue() {
return isArray;
}

// --- unsupported operations ---

@Override
public @NotNull String getTypeString() {
throw new UnsupportedOperationException();
}

@Override
public @NotNull String getValueType() {
throw new UnsupportedOperationException();
}
}
65 changes: 65 additions & 0 deletions core/src/main/java/io/wcm/testing/mock/aem/MockFragmentData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.wcm.testing.mock.aem;

import com.adobe.cq.dam.cfm.ContentFragmentException;
import com.adobe.cq.dam.cfm.DataType;
import com.adobe.cq.dam.cfm.FragmentData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Calendar;

public class MockFragmentData implements FragmentData {

private Object value;
private String contentType;
private final MockDataType mockDataType;

public MockFragmentData(Object value, boolean isArray) {
this.value = value;
this.mockDataType = new MockDataType(isArray);
}

@Override
public @NotNull DataType getDataType() {
return mockDataType;
}

@Override
public <T> @Nullable T getValue(Class<T> type) {
if (type.isInstance(value)) {
return (T) value;
} else {
return null;
}
}

@Override
public boolean isTypeSupported(Class type) {
return type.isInstance(value);
}

@Override
public @Nullable Object getValue() {
return value;
}

@Override
public void setValue(@Nullable Object value) throws ContentFragmentException {
this.value = value;
}

@Override
public @Nullable String getContentType() {
return contentType;
}

@Override
public void setContentType(@Nullable String contentType) {
this.contentType = contentType;
}

@Override
public @Nullable Calendar getLastModified() {
return null;
}
}

0 comments on commit 2d86a49

Please sign in to comment.