Skip to content

Commit

Permalink
Fix FieldsetComponent generating a null value
Browse files Browse the repository at this point in the history
ValueMaps should never contain null values. They should not report a key
as present if there is no corresponding value.
  • Loading branch information
csaboka committed Feb 22, 2024
1 parent ae96011 commit 33af5a4
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
19 changes: 19 additions & 0 deletions bundle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit-addons</groupId>
<artifactId>junit-addons</artifactId>
Expand Down Expand Up @@ -660,6 +665,20 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>oak-jcr</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.commons.testing</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public void init() {

@Override
public Resource buildComponentResource() {
getProperties().put(CLASS, getCssClass());
if (getCssClass() != null) {
getProperties().put(CLASS, getCssClass());
}
return super.buildComponentResource();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.adobe.acs.commons.mcp.form;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.when;

import java.lang.reflect.Field;

import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.scripting.SlingScriptHelper;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith({MockitoExtension.class, AemContextExtension.class})
class FieldsetTest {

private final AemContext ctx = new AemContext(ResourceResolverType.JCR_MOCK);

@Mock
private FormField formField;

@Mock
private SlingScriptHelper slingScriptHelper;

@BeforeEach
public void setup() {
ctx.currentResource(ctx.create().resource("/dummy/resource"));
when(slingScriptHelper.getRequest()).thenReturn(ctx.request());
}

@Test
public void classNotSpecified() {
final FieldsetComponent fieldsetComponent = new FieldsetComponent();
fieldsetComponent.setup("dummy", null, formField, slingScriptHelper);

Resource resource = fieldsetComponent.buildComponentResource();
ValueMap map = resource.getValueMap();

System.out.println(map);
assertFalse(map.containsKey("class"));
}
}
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,12 @@ Service-Component: OSGI-INF/*.xml
<version>4.11.0</version><!-- >= 5.x requires Java 11 -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.11.0</version><!-- >= 5.x requires Java 11 -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
Expand Down Expand Up @@ -682,6 +688,18 @@ Service-Component: OSGI-INF/*.xml
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
<version>5.1.2</version><!-- newer versions require Java 11, https://github.com/wcm-io/io.wcm.testing.aem-mock/issues/13 -->
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
Expand Down

0 comments on commit 33af5a4

Please sign in to comment.