Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Workflow Excel file reading Junit5

Avatar

Level 4

Hi @All,

 

Team In my Actual class holds Input Stream, XSSFWorkbook, and am iterating all the Excel column values and getting it via this snippet 'row.getCell(columnNames.get("path")).toString().trim()' code is working fine as expected. When I come to  Junits I couldn't able to cover the junits for the complete class. Can anyone help here please. How to write the junits for Excel reading class in AEM?

 

Thanks,

Imad

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

 

Check below code

private static final String CONTENT_PAGES_PATH = "/content/pages";
protected static final String SHEET_NAME = "Page Meta Data";
protected static final String PATH = "path";

@BeforeEach
public void beforeEach() throws Exception {
ctx.load().json("/com/project/company/export/page-structure.json", CONTENT_PAGES_PATH);
ctx.request().setParameterMap(ImmutableMap.of("paths", new String[]{CONTENT_PAGES_PATH}));
//For reading service user, we created a util
ctx.registerService(Util.class, util);

metaDataExporterService = ctx.registerInjectActivateService(new MetaDataExporterService());

when(util.getSysUserResourceResolver()).thenReturn(ctx.resourceResolver().clone(new HashMap<>()));
}

@test
public void testExcelExportIsCreated() throws Exception {
XSSFWorkbook metaDataWorkbookFromPage =
metaDataExporterService.createMetaDataWorkbookFromPage(CONTENT_PAGES_PATH);

XSSFSheet sheet = metaDataWorkbookFromPage.getSheet(SHEET_NAME);
assertNotNull(sheet);

XSSFRow row = sheet.getRow(0);

assertEquals(PATH, row.getCell(0).toString());

XSSFRow row1 = sheet.getRow(1);

assertEquals("/content/pages/jcr:content", row1.getCell(0).toString());
}

 

page-structure.json - will have json of /content/pages - dummy pages with jcr:content node for every page.

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi,

 

Check below code

private static final String CONTENT_PAGES_PATH = "/content/pages";
protected static final String SHEET_NAME = "Page Meta Data";
protected static final String PATH = "path";

@BeforeEach
public void beforeEach() throws Exception {
ctx.load().json("/com/project/company/export/page-structure.json", CONTENT_PAGES_PATH);
ctx.request().setParameterMap(ImmutableMap.of("paths", new String[]{CONTENT_PAGES_PATH}));
//For reading service user, we created a util
ctx.registerService(Util.class, util);

metaDataExporterService = ctx.registerInjectActivateService(new MetaDataExporterService());

when(util.getSysUserResourceResolver()).thenReturn(ctx.resourceResolver().clone(new HashMap<>()));
}

@test
public void testExcelExportIsCreated() throws Exception {
XSSFWorkbook metaDataWorkbookFromPage =
metaDataExporterService.createMetaDataWorkbookFromPage(CONTENT_PAGES_PATH);

XSSFSheet sheet = metaDataWorkbookFromPage.getSheet(SHEET_NAME);
assertNotNull(sheet);

XSSFRow row = sheet.getRow(0);

assertEquals(PATH, row.getCell(0).toString());

XSSFRow row1 = sheet.getRow(1);

assertEquals("/content/pages/jcr:content", row1.getCell(0).toString());
}

 

page-structure.json - will have json of /content/pages - dummy pages with jcr:content node for every page.