Skip to content

Commit

Permalink
title extd model unit test add
Browse files Browse the repository at this point in the history
  • Loading branch information
Sady-Rifat committed Jul 6, 2023
1 parent 7f543e0 commit 81ce47f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/pom.xml
Expand Up @@ -169,6 +169,10 @@ Import-Package: javax.annotation;version=0.0.0,*
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.models.api</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>


<!-- Testing -->
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/com/aem/demo/core/models/TitleExtd.java
@@ -0,0 +1,7 @@
package com.aem.demo.core.models;

import com.adobe.cq.wcm.core.components.models.Title;

public interface TitleExtd extends Title {

}
33 changes: 33 additions & 0 deletions core/src/main/java/com/aem/demo/core/models/TitleExtdImpl.java
@@ -0,0 +1,33 @@
package com.aem.demo.core.models;

import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.adobe.cq.wcm.core.components.models.Title;
import lombok.experimental.Delegate;
import lombok.extern.slf4j.Slf4j;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.via.ResourceSuperType;

import javax.annotation.PostConstruct;

@Slf4j
@Model(adaptables = SlingHttpServletRequest.class, adapters = {TitleExtd.class, ComponentExporter.class},
resourceType = TitleExtdImpl.RESOURCE_TYPE, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class TitleExtdImpl implements TitleExtd {
public static final String RESOURCE_TYPE = "aem-demo/components/title";

@Delegate(types = com.adobe.cq.wcm.core.components.models.Title.class)
@Self
private com.adobe.cq.wcm.core.components.models.Title delegate;

@PostConstruct
protected void init() {
log.info("Start Extended Title Model");
}
}
37 changes: 37 additions & 0 deletions core/src/test/java/com/aem/demo/core/models/TitleExtdImplTest.java
@@ -0,0 +1,37 @@
package com.aem.demo.core.models;

import com.aem.demo.core.testcontext.AppAemContext;
import com.day.cq.wcm.api.Page;
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.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class TitleExtdImplTest {
private final AemContext context = AppAemContext.newAemContext();
private TitleExtd titleExtd;

@BeforeEach
void init() {
Resource pageResource = context.load().json("/com/aem/demo/core/models/page.json", "/content/uk");
Page page = pageResource.getChild("en/product").adaptTo(Page.class);

Map<String, Object> jsonObject = TestUtils.getJsonObject("/com/aem/demo/core/models/title.json");
context.currentResource(context.create().resource(page, "extendedTitle", jsonObject));
titleExtd = context.request().adaptTo(TitleExtd.class);
}

@Test
void idTest() {
assertNotNull(titleExtd.getId());
}

}
15 changes: 15 additions & 0 deletions core/src/test/resources/com/aem/demo/core/models/title.json
@@ -0,0 +1,15 @@
{
"jcr:primaryType": "nt:unstructured",
"jcr:createdBy": "admin",
"jcr:title": "Product",
"jcr:lastModifiedBy": "admin",
"jcr:created": "Fri Dec 07 2018 13:50:54 GMT+0100",
"type": "h1",
"linkURL": "/content/core-components-examples/library/title",
"linkAccessibilityLabel": "Link",
"jcr:lastModified": "Thu Jul 06 2023 12:55:24 GMT+0000",
"sling:resourceType": "aem-demo/components/title",
"cq:styleIds": [
"1544759629303"
]
}
7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -673,6 +673,13 @@ Bundle-DocURL:
<version>5.7.4</version>
<scope>provided</scope>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.adobe.cq</groupId>
<artifactId>core.wcm.components.core</artifactId>
Expand Down

0 comments on commit 81ce47f

Please sign in to comment.