Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement MockContentFragment_ContentElement_Structured getValue() me… #26

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 MockContentFragment_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
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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 <T> @Nullable T getValue(Class<T> type) {
if (type.isInstance(value)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is too simplified. according to https://developer.adobe.com/experience-manager/reference-materials/cloud-service/javadoc/com/adobe/cq/dam/cfm/FragmentData.html#getValue(java.lang.Class) this should support any conversion. what about using OSGi converter e.g.

  public <T> @Nullable T getValue(Class<T> type) {
    return Converters.standardConverter().convert(value).defaultValue(null).targetAs(type);
  }

probably also update the unit tests a bit to test one or two actual conversions.

return (T)value;
}
else {
return null;
}
}

@Override
public boolean isTypeSupported(Class type) {
return type.isInstance(value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is too simplfied as well? following https://developer.adobe.com/experience-manager/reference-materials/cloud-service/javadoc/com/adobe/cq/dam/cfm/FragmentData.html#isTypeSupported(java.lang.Class) it should return true for any type supported to set as value (supported by the internal converters). it's not related to the value currently set. for sake of simplicity we could just return true here with a comment that this is simplified.

}

@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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down