Skip to content

Commit

Permalink
fixed report tests, however it revealed a bug in task handling report…
Browse files Browse the repository at this point in the history
… data refs, will be fixed shortly
  • Loading branch information
1azyman committed Feb 7, 2023
1 parent f64ee4e commit b49188c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.test.annotation.DirtiesContext;
Expand All @@ -32,9 +29,7 @@
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.test.TestResource;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.exception.CommonException;
import com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException;
import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;

/**
Expand Down Expand Up @@ -202,44 +197,47 @@ void createUsers(int users, Task initTask, OperationResult initResult) throws Co
System.out.printf("%d users created", users);
}

List<String> getLinesOfOutputFile(PrismObject<ReportType> report) throws IOException, ParseException {
File outputFile = findOutputFile(report);
List<String> getLinesOfOutputFile(PrismObject<TaskType> task) throws IOException, ParseException, SchemaException,
ExpressionEvaluationException, SecurityViolationException, CommunicationException, ConfigurationException, ObjectNotFoundException {

File outputFile = findOutputFile(task);
displayValue("Found report file", outputFile);
assertNotNull("No output file for " + report, outputFile);
assertNotNull("No output file for " + task, outputFile);
List<String> lines = Files.readAllLines(Paths.get(outputFile.getPath()));
displayValue("Report content (" + lines.size() + " lines)", String.join("\n", lines));
assertThat(outputFile.renameTo(new File(outputFile.getParentFile(), "processed-" + outputFile.getName())))
.isTrue();
return lines;
}

File findOutputFile(PrismObject<ReportType> report) throws ParseException {
// We should use a more robust way of finding the file names, e.g. by looking at ReportDataType repo objects.
String expectedFilePrefix =
MiscUtil.replaceIllegalCharInFileNameOnWindows(
report.getName().getOrig());
File[] matchingFiles = EXPORT_DIR.listFiles((dir, name) -> name.startsWith(expectedFilePrefix));
if (matchingFiles == null || matchingFiles.length == 0) {
File findOutputFile(PrismObject<TaskType> taskObject) throws ParseException, SchemaException, ExpressionEvaluationException,
SecurityViolationException, CommunicationException, ConfigurationException, ObjectNotFoundException {

TaskType task = taskObject.asObjectable();
ActivityStateType activity = task.getActivityState() != null ? task.getActivityState().getActivity() : null;
if (activity == null) {
return null;
}

if (!(activity.getWorkState() instanceof ReportExportWorkStateType)) {
return null;
}

ReportExportWorkStateType state = (ReportExportWorkStateType) activity.getWorkState();
ObjectReferenceType reportDataRef = state.getReportDataRef();
if (reportDataRef == null) {
return null;
}
if (matchingFiles.length > 1) {
Date date = null;
for (File file : matchingFiles) {
String name = file.getName();
String stringDate = name.substring(0, name.lastIndexOf("."))
.substring(name.substring(0, name.lastIndexOf(" ")).lastIndexOf(" "));

Date fileDate = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss.SSS").parse(stringDate);
if (date == null || date.before(fileDate)) {
date = fileDate;
}
}
if (date == null) {
throw new IllegalStateException(
"Found more than one output files for " + report + ": " + Arrays.toString(matchingFiles));
}

PrismObject<ReportDataType> reportDataObject = getObject(ReportDataType.class, reportDataRef.getOid());
ReportDataType reportData = reportDataObject.asObjectable();

String filePath = reportData.getFilePath();
if (filePath == null) {
return null;
}
return matchingFiles[0];

return new File(filePath);
}

void changeTaskReport(TestResource<ReportType> reportResource, ItemPath reportRefPath, TestResource<TaskType> taskResource) throws CommonException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import java.text.ParseException;
import java.util.List;

import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType;

import org.apache.commons.lang3.StringUtils;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -33,15 +36,16 @@ public void initSystem(Task initTask, OperationResult initResult) throws Excepti
addObject(USER_JACK, initTask, initResult);
}

List<String> basicCheckOutputFile(PrismObject<ReportType> report, int expectedRows, int expectedColumns, String lastLine)
throws IOException, ParseException {
List<String> basicCheckOutputFile(PrismObject<TaskType> task, int expectedRows, int expectedColumns, String lastLine)
throws IOException, ParseException, SchemaException, ExpressionEvaluationException, SecurityViolationException, CommunicationException,
ConfigurationException, ObjectNotFoundException {

if (expectedRows != DONT_COUNT_ROWS) {
// +1 for the final line with the subscription appeal
expectedRows += 1;
}

List<String> lines = getLinesOfOutputFile(report);
List<String> lines = getLinesOfOutputFile(task);

if (expectedRows != DONT_COUNT_ROWS && lines.size() != expectedRows) {
fail("Unexpected count of rows of csv report. Expected: " + expectedRows + ", Actual: " + lines.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,13 @@
*/
package com.evolveum.midpoint.report;

import static org.testng.AssertJUnit.assertTrue;

import java.io.File;
import javax.xml.namespace.QName;

import com.evolveum.midpoint.model.api.ModelExecuteOptions;
import com.evolveum.midpoint.prism.delta.ChangeType;
import com.evolveum.midpoint.prism.delta.ObjectDelta;

import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test;

import com.evolveum.midpoint.prism.MutablePrismPropertyDefinition;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.prism.delta.ChangeType;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.report.api.ReportConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
Expand All @@ -32,6 +22,15 @@
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;

import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test;

import javax.xml.namespace.QName;
import java.io.File;

import static org.testng.AssertJUnit.assertTrue;

@ContextConfiguration(locations = { "classpath:ctx-report-test-main.xml" })
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class TestCsvReportExportClassic extends TestCsvReport {
Expand Down Expand Up @@ -268,8 +267,8 @@ private void runTest(TestResource<ReportType> reportResource, int expectedRows,
.display()
.assertHasArchetype(SystemObjectsType.ARCHETYPE_REPORT_EXPORT_CLASSIC_TASK.value());

PrismObject<ReportType> report = getObject(ReportType.class, reportResource.oid);
basicCheckOutputFile(report, expectedRows, expectedColumns, lastLine);
PrismObject<TaskType> reportTask = getObject(TaskType.class, TASK_EXPORT_CLASSIC.oid);
basicCheckOutputFile(reportTask, expectedRows, expectedColumns, lastLine);

assertNotificationMessage(reportResource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ public void test101ExportAndImportUser() throws Exception {
ItemPath.create(ReportType.F_BEHAVIOR, ReportBehaviorType.F_DIRECTION), task, result, DirectionTypeType.IMPORT);
deleteObject(UserType.class, USER_WILL.oid);

PrismObject<ReportType> report = getObject(ReportType.class, REPORT_REIMPORT_USERS_CLASSIC.oid);
File outputFile = findOutputFile(report);
PrismObject<TaskType> reportTask = getObject(TaskType.class, TASK_EXPORT_CLASSIC.oid);
File outputFile = findOutputFile(reportTask);

ReportDataType reportData = new ReportDataType();
PolyStringType name = new PolyStringType(getTestNameShort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import java.io.File;
import java.util.List;

import com.evolveum.midpoint.xml.ns._public.common.common_3.*;

import org.testng.annotations.Test;

import com.evolveum.midpoint.prism.PrismContext;
Expand All @@ -22,6 +20,7 @@
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.test.TestResource;
import com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.query_3.SearchFilterType;

public class TestCsvReportMultiNode extends TestCsvReport {
Expand Down Expand Up @@ -74,8 +73,8 @@ public void test100ExportUsers() throws Exception {
.display()
.assertHasArchetype(SystemObjectsType.ARCHETYPE_REPORT_EXPORT_DISTRIBUTED_TASK.value());

PrismObject<ReportType> report = getObject(ReportType.class, REPORT_OBJECT_COLLECTION_USERS.oid);
basicCheckOutputFile(report, 1004, 2, null);
PrismObject<TaskType> reportTask = getObject(TaskType.class, TASK_DISTRIBUTED_EXPORT_USERS.oid);
basicCheckOutputFile(reportTask, 1004, 2, null);

assertNotificationMessage(REPORT_OBJECT_COLLECTION_USERS);
}
Expand All @@ -84,8 +83,8 @@ public void test100ExportUsers() throws Exception {
public void test101ExportAuditRecords() throws Exception {
auditTest();

PrismObject<ReportType> report = getObject(ReportType.class, REPORT_AUDIT_COLLECTION_WITH_DEFAULT_COLUMN.oid);
List<String> rows = basicCheckOutputFile(report, DONT_COUNT_ROWS, 8, null);
PrismObject<TaskType> reportTask = getObject(TaskType.class, TASK_DISTRIBUTED_EXPORT_AUDIT.oid);
List<String> rows = basicCheckOutputFile(reportTask, DONT_COUNT_ROWS, 8, null);
assertTrue(rows.size() > 1000 && rows.size() <= 1012,
"Unexpected number of rows in report. Expected:1000-1012, Actual:" + rows.size());
}
Expand All @@ -108,8 +107,8 @@ public void test102ExportAuditRecordsInsideTwoTimestamps() throws Exception {

auditTest();

PrismObject<ReportType> report = getObject(ReportType.class, REPORT_AUDIT_COLLECTION_WITH_DEFAULT_COLUMN.oid);
List<String> rows = basicCheckOutputFile(report, DONT_COUNT_ROWS, 8, null);
PrismObject<TaskType> reportTask = getObject(TaskType.class, TASK_DISTRIBUTED_EXPORT_AUDIT.oid);
List<String> rows = basicCheckOutputFile(reportTask, DONT_COUNT_ROWS, 8, null);
assertTrue(rows.size() > 800 && rows.size() <= 810,
"Unexpected number of rows in report. Expected:800-810, Actual:" + rows.size());
}
Expand All @@ -132,8 +131,8 @@ public void test103ExportAuditRecordsOutsideTwoTimestamps() throws Exception {

auditTest();

PrismObject<ReportType> report = getObject(ReportType.class, REPORT_AUDIT_COLLECTION_WITH_DEFAULT_COLUMN.oid);
List<String> rows = basicCheckOutputFile(report, DONT_COUNT_ROWS, 8, null);
PrismObject<TaskType> reportTask = getObject(TaskType.class, TASK_DISTRIBUTED_EXPORT_AUDIT.oid);
List<String> rows = basicCheckOutputFile(reportTask, DONT_COUNT_ROWS, 8, null);
assertTrue(rows.size() > 1200 && rows.size() <= 1250,
"Unexpected number of rows in report. Expected:1200-1250, Actual:" + rows.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ private void testStoredWidgetData(StoreExportedWidgetDataType storeDataType) thr
.assertSuccess()
.display();

PrismObject<ReportType> report = getObject(ReportType.class, REPORT_DASHBOARD_WITH_DEFAULT_COLUMN.oid);
File outputFile = findOutputFile(report);
PrismObject<TaskType> reportTask = getObject(TaskType.class, TASK_EXPORT_CLASSIC.oid);
File outputFile = findOutputFile(reportTask);
displayValue("Found report file", outputFile);
if (StoreExportedWidgetDataType.ONLY_WIDGET.equals(storeDataType)) {
assertThat(outputFile).withFailMessage("Output file for " + report + " exists").isNull();
assertThat(outputFile).withFailMessage("Output file for " + reportTask + " exists").isNull();
} else {
assertThat(outputFile).withFailMessage("Output file for " + report + " does not exist").isNotNull();
assertThat(outputFile).withFailMessage("Output file for " + reportTask + " does not exist").isNotNull();
}
if (outputFile != null) {
assertThat(outputFile.renameTo(new File(outputFile.getParentFile(), "processed-" + outputFile.getName())))
Expand Down Expand Up @@ -295,8 +295,8 @@ private void runTest(TestResource<ReportType> reportResource) throws Exception {

assertNotificationMessage(reportResource.getObjectable(), "text/html");

PrismObject<ReportType> report = getObject(ReportType.class, reportResource.oid);
List<String> lines = getLinesOfOutputFile(report);
PrismObject<TaskType> reportTask = getObject(TaskType.class, TASK_EXPORT_CLASSIC.oid);
List<String> lines = getLinesOfOutputFile(reportTask);

if (lines.size() < 10) {
fail("Html report too short (" + lines.size() + " lines)");
Expand Down

0 comments on commit b49188c

Please sign in to comment.