Skip to content

Commit

Permalink
adding tests and cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
skublik committed Jul 8, 2019
1 parent 6bd1574 commit fedb5d4
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 79 deletions.
Expand Up @@ -281,7 +281,8 @@ private String generateFullQuery(Map<String, Object> parameters, boolean ordered
private String generateFullQuery(String origQuery, boolean ordered, boolean isCount) {
String query = origQuery;
if (isCount) {
query = AUDIT_RECORDS_QUERY_COUNT + query;
int index = query.toLowerCase().indexOf("from");
query = AUDIT_RECORDS_QUERY_COUNT + query.substring(index);
}
if (ordered) {
query += AUDIT_RECORDS_ORDER_BY;
Expand Down
Expand Up @@ -19,8 +19,8 @@
oid="00000000-0000-0000-0001-000000000004">
<name>Error audit record</name>
<auditSearch>
<recordQuery>from RAuditEventRecord as aer where aer.outcome=3 and aer.eventStage=1</recordQuery>
<domainQuery>from RAuditEventRecord as aer where aer.eventStage=1</domainQuery>
<recordQuery>select * from m_audit_event as aer where aer.outcome=3 and aer.eventStage=1</recordQuery>
<domainQuery>select * from m_audit_event as aer where aer.eventStage=1</domainQuery>
<interval>P1D</interval>
</auditSearch>
</objectCollection>
Expand Up @@ -19,8 +19,8 @@
oid="00000000-0000-0000-0001-000000000002">
<name>Modification audit record</name>
<auditSearch>
<recordQuery>from RAuditEventRecord as aer where aer.eventType=2 and aer.eventStage=1</recordQuery>
<domainQuery>from RAuditEventRecord as aer where aer.eventStage=1</domainQuery>
<recordQuery>select * from m_audit_event as aer where aer.eventType=2 and aer.eventStage=1</recordQuery>
<domainQuery>select * from m_audit_event as aer where aer.eventStage=1</domainQuery>
<interval>P1D</interval>
</auditSearch>
</objectCollection>
Expand Up @@ -41,8 +41,8 @@ public class DashboardUtils {

private static final Trace LOGGER = TraceManager.getTrace(DashboardUtils.class);

private static final String AUDIT_RECORDS_ORDER_BY = " order by aer.timestamp desc";
private static final String TIMESTAMP_VALUE_NAME = "aer.timestamp";
private static final String AUDIT_RECORDS_ORDER_BY = " order by aer.timestampValue desc";
private static final String TIMESTAMP_VALUE_NAME = "aer.timestampValue";
private static final String PARAMETER_FROM = "from";

public static DashboardWidgetSourceTypeType getSourceType(DashboardWidgetType widget) {
Expand Down
Expand Up @@ -254,7 +254,8 @@ private String generateNumberMessageForAuditSearch(DashboardWidgetType widget, D
}

private String getQueryForCount(String query) {
query = "select count (*) " + query;
int index = query.toLowerCase().indexOf("from");
query = "select count (*) " + query.substring(index);
query = query.split("order")[0];
LOGGER.debug("Query for select: " + query);
return query;
Expand Down
Expand Up @@ -1303,7 +1303,7 @@ private <F extends ObjectType> void auditEvent(LensContext<F> context, AuditEven
AuditEventRecord auditRecord = new AuditEventRecord(eventType, stage);
auditRecord.setRequestIdentifier(context.getRequestIdentifier());

PrismObject<SystemConfigurationType> systemConfiguration = systemObjectCache.getSystemConfiguration(result);
PrismObject<SystemConfigurationType> systemConfiguration = context.getSystemConfiguration();

SystemConfigurationAuditEventRecordingType auditEventRecordingType;
if (systemConfiguration != null) {
Expand All @@ -1329,18 +1329,6 @@ private <F extends ObjectType> void auditEvent(LensContext<F> context, AuditEven
if (projectionContext != null && StringUtils.isNotBlank(projectionContext.getResourceOid())) {
auditRecord.addResourceOid(projectionContext.getResourceOid());
}
// PrismObject<ShadowType> shadow;
// try {
//
// shadow = repositoryService.getObject(ShadowType.class, shadowRef.getOid(),
// null, task.getResult());
// ObjectReferenceType resource = shadow.getRealValue().getResourceRef();
// if (resource != null && resource.getOid() != null) {
// auditRecord.addResourceOid(resource.getOid());
// }
// } catch (ObjectNotFoundException e) {
// LOGGER.error("Couldn't load shadow from reference " + shadowRef, e);
// }
}
} else if (primaryObject.getRealValue() instanceof ShadowType) {
ObjectReferenceType resource = ((ShadowType)primaryObject.getRealValue()).getResourceRef();
Expand Down
Expand Up @@ -1187,6 +1187,8 @@ public void test131ModifyUserJackAssignAccount() throws Exception {
dummyAuditService.assertHasDelta(ChangeType.MODIFY, UserType.class);
dummyAuditService.assertHasDelta(ChangeType.ADD, ShadowType.class);
dummyAuditService.assertTarget(USER_JACK_OID);
// dummyAuditService.assertCustomColumn("foo", "test");
// dummyAuditService.assertResourceOid(RESOURCE_DUMMY_OID);
dummyAuditService.assertExecutionSuccess();

// Check notifications
Expand Down
Expand Up @@ -612,5 +612,17 @@
<left>take</left>
</default>
</mergeConfiguration>

<audit>
<eventRecording>
<recordResourceOids>true</recordResourceOids>
<property>
<name>foo</name>
<expression>
<script>
<code> return "test"</code>
</script>
</expression>
</property>
</eventRecording>
</audit>
</systemConfiguration>
Expand Up @@ -84,6 +84,7 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.DisplayType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ExportType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectCollectionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectDeltaOperationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType;
Expand Down Expand Up @@ -119,6 +120,8 @@ public class ReportHTMLCreateTaskHandler extends ReportJasperCreateTaskHandler {
private static final String EVENT_STAGE_COLUMN = "Event stage";
private static final String EVENT_TYPE_COLUMN = "Event type";
private static final String TARGET_COLUMN = "Target";
private static final String DELTA_COLUMN = "Delta";
private static final String MESSAGE_COLUMN = "Message";
private static final String TARGET_OWNER_COLUMN = "Target owner";
private static final String CHANNEL_COLUMN = "Channel";
private static final String OUTCOME_COLUMN = "Outcome";
Expand Down Expand Up @@ -229,9 +232,12 @@ public class ReportHTMLCreateTaskHandler extends ReportJasperCreateTaskHandler {
add(EVENT_STAGE_COLUMN);
add(EVENT_TYPE_COLUMN);
add(TARGET_COLUMN);
add(TARGET_OWNER_COLUMN);
add(CHANNEL_COLUMN);
// add(TARGET_OWNER_COLUMN);
// add(TARGET_OWNER_COLUMN);
// add(CHANNEL_COLUMN);
add(OUTCOME_COLUMN);
add(MESSAGE_COLUMN);
add(DELTA_COLUMN);
}
};
}
Expand Down Expand Up @@ -405,7 +411,7 @@ private ContainerTag createTableBoxForWidget(DashboardWidget widgetData, Task ta
auditRecordList.add(record.createAuditEventRecordType());
}

ContainerTag table = createTable();
ContainerTag table = createTable(true);
table.with(createTHead(getHeadsOfAuditEventRecords()));
ContainerTag tBody = TagCreator.tbody();
auditRecordList.forEach(record -> {
Expand Down Expand Up @@ -450,6 +456,27 @@ private String getColumnForAuditEventRecord(String column, AuditEventRecordType
return "";
}
return record.getOutcome().toString();
case MESSAGE_COLUMN:
if (record.getMessage() == null) {
return "";
}
return record.getMessage();
case DELTA_COLUMN:
if (record.getDelta() == null || record.getDelta().isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
List<ObjectDeltaOperationType> deltas = record.getDelta();
for (int i = 0; i < deltas.size(); i++) {
ObjectDeltaOperationType delta = deltas.get(i);
sb.append(ReportUtils.printDelta(delta.getObjectDelta(),
(delta.getObjectName() == null ? null : delta.getObjectName().toString()),
(delta.getResourceName()) == null ? null : delta.getResourceName().toString()));
if ((i+1)!=deltas.size()) {
sb.append("\n");
}
}
return sb.toString();
}
return "";

Expand Down Expand Up @@ -549,8 +576,15 @@ private PrismObject<ObjectType> getObjectFromReference(Referencable ref) {
}
return object;
}

private ContainerTag createTable() {
return createTable(false);
}

private ContainerTag createTable(boolean isAudit) {
if(isAudit) {
return TagCreator.table().withClasses("table", "table-striped", "table-hover", "table-bordered", "table-audit");
}
return TagCreator.table().withClasses("table", "table-striped", "table-hover", "table-bordered");
}

Expand All @@ -564,7 +598,25 @@ private ContainerTag createTableBox(ContainerTag table, String nameOfTable, int

private ContainerTag createTHead(Set<String> set) {
return TagCreator.thead(TagCreator.tr(TagCreator.each(set,
header -> TagCreator.th(TagCreator.div(TagCreator.span(header).withClass("sortableLabel"))))));
header -> TagCreator.th(TagCreator.div(TagCreator.span(header).withClass("sortableLabel"))).withStyle(getStyleForColumn(header)))).withStyle("width: 100%;"));
}

private String getStyleForColumn(String column) {
switch (column) {
case TIME_COLUMN:
return "width: 10%;";
case INITIATOR_COLUMN:
return "width: 8%;";
case EVENT_STAGE_COLUMN:
return "width: 5%;";
case EVENT_TYPE_COLUMN:
return "width: 10%;";
case TARGET_COLUMN:
return "width: 8%;";
case OUTCOME_COLUMN:
return "width: 7%;";
}
return "";
}

private ContainerTag createTBodyRow(DashboardWidget data) {
Expand Down
Expand Up @@ -102,3 +102,7 @@ h1 {
p {
padding-left:15px;
}

.table-audit {
font-size:smaller;
}
Expand Up @@ -586,6 +586,7 @@ public AuditEventRecord clone() {
clone.properties.putAll(properties); // TODO deep clone?
clone.references.putAll(references); // TODO deep clone?
clone.resourceOids.addAll(resourceOids);
clone.customColumnProperty.putAll(customColumnProperty);
return clone;
}

Expand All @@ -598,6 +599,7 @@ public String toString() {
+ ", T=" + formatReference(target) + ", TO=" + formatObject(targetOwner) + ", et=" + eventType
+ ", es=" + eventStage + ", D=" + deltas + ", ch="+ channel +", o=" + outcome + ", r=" + result + ", p=" + parameter
+ ", m=" + message
+ ", cuscolprop=" + customColumnProperty
+ ", prop=" + properties
+ ", roid=" + resourceOids
+ ", ref=" + references + "]";
Expand Down Expand Up @@ -678,6 +680,7 @@ public String debugDump(int indent) {
DebugUtil.debugDumpWithLabelToStringLn(sb, "Properties", properties, indent + 1);
DebugUtil.debugDumpWithLabelToStringLn(sb, "Resource OIDs", resourceOids, indent + 1);
DebugUtil.debugDumpWithLabelToStringLn(sb, "References", references, indent + 1);
DebugUtil.debugDumpWithLabelToStringLn(sb, "Custom column properties", customColumnProperty, indent + 1);
DebugUtil.debugDumpLabel(sb, "Deltas", indent + 1);
if (deltas.isEmpty()) {
sb.append(" none");
Expand Down
Expand Up @@ -236,60 +236,17 @@ private void listRecordsIterativeAttempt(String query, Map<String, Object> param
LOGGER.trace("List records attempt\n query: {}\n params:\n{}", query,
DebugUtil.debugDump(params, 2));
}

session = baseHelper.beginReadOnlyTransaction();

// try {
session = baseHelper.beginReadOnlyTransaction();
//
// Query q;
//
// if (StringUtils.isBlank(query)) {
// query = "from RAuditEventRecord as aer where 1=1 order by aer.timestamp desc";
// q = session.createQuery(query);
// setParametersToQuery(q, params);
// } else {
// q = session.createQuery(query);
// setParametersToQuery(q, params);
// }
// // q.setResultTransformer(Transformers.aliasToBean(RAuditEventRecord.class));
//
// if (LOGGER.isTraceEnabled()) {
// LOGGER.trace("List records attempt\n processed query: {}", q);
// }
//
// ScrollableResults resultList = q.scroll();
//
// while (resultList.next()) {
// Object o = resultList.get(0);
// if (!(o instanceof RAuditEventRecord)) {
// throw new DtoTranslationException(
// "Unexpected object in result set. Expected audit record, but got "
// + o.getClass().getSimpleName());
// }
// RAuditEventRecord raudit = (RAuditEventRecord) o;
//
// AuditEventRecord audit = RAuditEventRecord.fromRepo(raudit, getPrismContext(), getConfiguration().isUsingSQLServer());
//
// // TODO what if original name (in audit log) differs from the current one (in repo) ?
// audit.setInitiator(resolve(session, raudit.getInitiatorOid(), raudit.getInitiatorName(), defaultIfNull(raudit.getInitiatorType(), RObjectType.USER)));
// audit.setAttorney(resolve(session, raudit.getAttorneyOid(), raudit.getAttorneyName(), RObjectType.USER));
// audit.setTarget(resolve(session, raudit.getTargetOid(), raudit.getTargetName(), raudit.getTargetType()),
// getPrismContext());
// audit.setTargetOwner(resolve(session, raudit.getTargetOwnerOid(), raudit.getTargetOwnerName(), raudit.getTargetOwnerType()));
// count++;
// if (!handler.handle(audit)) {
// LOGGER.trace("Skipping handling of objects after {} was handled. ", audit);
// break;
// }
// }
try {
Session localSession = session;
try {
Session localSession = session;
session.doWork(new Work() {

@Override
public void execute(Connection con) throws SQLException {

Database database = baseHelper.getConfiguration().getDatabase();

int count = 0;
String basicQuery = query;
if (StringUtils.isBlank(query)) {
Expand Down
Expand Up @@ -720,10 +720,6 @@ public static SingleSqlQuery toRepo(AuditEventRecord record, Map<String, String>
queryBulder.addParameter(TASK_IDENTIFIER_COLUMN_NAME, record.getTaskIdentifier());
queryBulder.addParameter(TASK_OID_COLUMN_NAME, record.getTaskOID());
queryBulder.addParameter(RESULT_COLUMN_NAME, record.getResult());
//
// for(String resourceOid : record.getResourceOids()) {
// repo.getResourceOids().add(RTargetResourceOid.toRepo(repo, resourceOid));
// }

try {
if (record.getTarget() != null) {
Expand Down
Expand Up @@ -334,7 +334,27 @@ public void assertExecutionDeltas(int expectedNumber) {
public void assertExecutionDeltas(int index, int expectedNumber) {
assertEquals("Wrong number of execution deltas in audit trail (index "+index+")", expectedNumber, getExecutionDeltas(index).size());
}


public void assertCustomColumn(String nameOfProperty, String value) {
for (AuditEventRecord record: records) {
Map<String, String> properties = record.getCustomColumnProperty();
if (properties.containsKey(nameOfProperty) && properties.get(nameOfProperty).equals(value)) {
return;
}
}
assert false : "Custom column property "+nameOfProperty+" with value " +value+ " not found in audit records";
}

public void assertResourceOid(String oid) {
for (AuditEventRecord record: records) {
Set<String> resourceOid = record.getResourceOids();
if (resourceOid.contains(oid)) {
return;
}
}
assert false : "Resource oid "+oid+" not found in audit records";
}

public void assertTarget(String expectedOid, AuditEventStage stage) {
Collection<PrismReferenceValue> targets = new ArrayList<>();
for(AuditEventRecord record: records) {
Expand Down

0 comments on commit fedb5d4

Please sign in to comment.