diff --git a/core/src/main/java/io/wcm/testing/mock/aem/MockContentFragment_ContentElement_Structured.java b/core/src/main/java/io/wcm/testing/mock/aem/MockContentFragment_ContentElement_Structured.java index b15b9768..b7b908a3 100644 --- a/core/src/main/java/io/wcm/testing/mock/aem/MockContentFragment_ContentElement_Structured.java +++ b/core/src/main/java/io/wcm/testing/mock/aem/MockContentFragment_ContentElement_Structured.java @@ -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 MockContentFragment_MockFragmentData(value, value.getClass().isArray()); + } // --- unsupported operations --- @@ -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(); diff --git a/core/src/main/java/io/wcm/testing/mock/aem/MockContentFragment_MockDataType.java b/core/src/main/java/io/wcm/testing/mock/aem/MockContentFragment_MockDataType.java new file mode 100644 index 000000000..cab55916 --- /dev/null +++ b/core/src/main/java/io/wcm/testing/mock/aem/MockContentFragment_MockDataType.java @@ -0,0 +1,56 @@ +/* + * #%L + * wcm.io + * %% + * Copyright (C) 2023 wcm.io + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package io.wcm.testing.mock.aem; + +import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import com.adobe.cq.dam.cfm.DataType; + +class MockContentFragment_MockDataType implements DataType { + + private final boolean isArray; + + MockContentFragment_MockDataType(boolean isArray) { + this.isArray = isArray; + } + + public @Nullable String getSemanticType() { + return StringUtils.EMPTY; + } + + @Override + public boolean isMultiValue() { + return isArray; + } + + // --- unsupported operations --- + + @Override + public @NotNull String getTypeString() { + throw new UnsupportedOperationException(); + } + + public @NotNull String getValueType() { + throw new UnsupportedOperationException(); + } + +} diff --git a/core/src/main/java/io/wcm/testing/mock/aem/MockContentFragment_MockFragmentData.java b/core/src/main/java/io/wcm/testing/mock/aem/MockContentFragment_MockFragmentData.java new file mode 100644 index 000000000..ead63bcf --- /dev/null +++ b/core/src/main/java/io/wcm/testing/mock/aem/MockContentFragment_MockFragmentData.java @@ -0,0 +1,86 @@ +/* + * #%L + * wcm.io + * %% + * Copyright (C) 2023 wcm.io + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package io.wcm.testing.mock.aem; + +import java.util.Calendar; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import com.adobe.cq.dam.cfm.ContentFragmentException; +import com.adobe.cq.dam.cfm.DataType; +import com.adobe.cq.dam.cfm.FragmentData; + +class MockContentFragment_MockFragmentData implements FragmentData { + + private Object value; + private String contentType; + private final MockContentFragment_MockDataType mockDataType; + + MockContentFragment_MockFragmentData(Object value, boolean isArray) { + this.value = value; + this.mockDataType = new MockContentFragment_MockDataType(isArray); + } + + @Override + public @NotNull DataType getDataType() { + return mockDataType; + } + + @Override + public @Nullable T getValue(Class 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; + } + + public @Nullable Calendar getLastModified() { + return null; + } + +} diff --git a/core/src/test/java/io/wcm/testing/mock/aem/MockContentFragmentTest.java b/core/src/test/java/io/wcm/testing/mock/aem/MockContentFragmentTest.java index 31460bf5..90cfcb9c 100644 --- a/core/src/test/java/io/wcm/testing/mock/aem/MockContentFragmentTest.java +++ b/core/src/test/java/io/wcm/testing/mock/aem/MockContentFragmentTest.java @@ -21,16 +21,20 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; - -import org.apache.commons.collections4.IteratorUtils; -import org.junit.Rule; -import org.junit.Test; +import static org.junit.Assert.assertFalse; import com.adobe.cq.dam.cfm.ContentElement; import com.adobe.cq.dam.cfm.ContentFragment; import com.adobe.cq.dam.cfm.ContentVariation; +import com.adobe.cq.dam.cfm.DataType; +import com.adobe.cq.dam.cfm.FragmentData; import com.adobe.cq.dam.cfm.VariationTemplate; +import org.apache.commons.collections4.IteratorUtils; +import org.junit.Rule; +import org.junit.Test; + import com.day.cq.dam.api.DamConstants; import io.wcm.testing.mock.aem.context.TestAemContext; @@ -66,6 +70,27 @@ public void testContentFragmentStructure() throws Exception { assertEquals("true", cf.getElement("param3").getContent()); assertEquals("v1\nv2", cf.getElement("param4").getContent()); + // get fragmentData and dataType + FragmentData fragmentData = cf.getElement("param1").getValue(); + assertNotNull(fragmentData); + assertEquals("value1", fragmentData.getValue()); + assertTrue(fragmentData.isTypeSupported(String.class)); + assertEquals("value1", fragmentData.getValue(String.class)); + assertFalse(fragmentData.isTypeSupported(Boolean.class)); + assertNull(fragmentData.getValue(Boolean.class)); + + DataType dataType = fragmentData.getDataType(); + assertNotNull(dataType); + + assertFalse(dataType.isMultiValue()); + assertTrue(cf.getElement("param4").getValue().getDataType().isMultiValue()); + + //update fragmentData value and contentType + fragmentData.setValue("newvalue"); + fragmentData.setContentType("contentType"); + assertEquals("newvalue", fragmentData.getValue()); + assertEquals("contentType", fragmentData.getContentType()); + // update data ContentElement param1 = cf.getElement("param1"); param1.setContent("new_value", null);