Skip to content

Commit

Permalink
SqaleRepoBaseTest: repositorySearchObjects logs query by default
Browse files Browse the repository at this point in the history
Query is not recorded/displayed if queryRecorder is already recording.
Convenience method withQueryRecorded(CheckedRunnable) added.
  • Loading branch information
virgo47 committed Jun 10, 2022
1 parent 47f61a1 commit ee47a25
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.testng.TestException;
import org.testng.annotations.BeforeClass;

import com.evolveum.midpoint.audit.api.AuditService;
Expand Down Expand Up @@ -48,6 +49,7 @@
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.test.util.AbstractSpringTest;
import com.evolveum.midpoint.test.util.InfraTestMixin;
import com.evolveum.midpoint.util.CheckedRunnable;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
Expand Down Expand Up @@ -76,6 +78,7 @@ public class SqaleRepoBaseTest extends AbstractSpringTest

@Autowired protected AuditService auditService;

/** Also see convenient method {@link #withQueryRecorded(CheckedRunnable)}. */
protected SqlRecorder queryRecorder;

@BeforeClass
Expand Down Expand Up @@ -408,13 +411,24 @@ protected final <T extends ObjectType> SearchResultList<T> repositorySearchObjec
OperationResult operationResult,
SelectorOptions<GetOperationOptions>... selectorOptions)
throws SchemaException {
return repositoryService.searchObjects(
type,
query,
selectorOptions != null && selectorOptions.length != 0
? List.of(selectorOptions) : null,
operationResult)
.map(p -> p.asObjectable());
boolean record = !queryRecorder.isRecording();
if (record) {
queryRecorder.clearBufferAndStartRecording();
}
try {
return repositoryService.searchObjects(
type,
query,
selectorOptions != null && selectorOptions.length != 0
? List.of(selectorOptions) : null,
operationResult)
.map(p -> p.asObjectable());
} finally {
if (record) {
queryRecorder.stopRecording();
display(queryRecorder.getQueryBuffer().toString());
}
}
}

/** Search objects using Axiom query language. */
Expand Down Expand Up @@ -633,4 +647,16 @@ protected JdbcSession startReadOnlyTransaction() {
//noinspection resource
return sqlRepoContext.newJdbcSession().startReadOnlyTransaction();
}

protected void withQueryRecorded(CheckedRunnable block) {
queryRecorder.clearBufferAndStartRecording();
try {
block.run();
} catch (Exception e) {
throw new TestException(e);
} finally {
queryRecorder.stopRecording();
display(queryRecorder.getQueryBuffer().toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1107,16 +1107,11 @@ public void test328ExistsFilterWithSizeColumn() throws SchemaException {

@Test
public void test350ExistsWithEmbeddedContainer() throws SchemaException {
queryRecorder.clearBufferAndStartRecording();
try {
searchUsersTest("matching the exists filter for metadata (embedded mapping)",
f -> f.exists(UserType.F_METADATA)
.item(ItemPath.create(MetadataType.F_CREATOR_REF, T_OBJECT_REFERENCE, UserType.F_NAME))
.eqPoly("creator"),
user1Oid);
} finally {
display(queryRecorder.getQueryBuffer().peek().toString());
}
searchUsersTest("matching the exists filter for metadata (embedded mapping)",
f -> f.exists(UserType.F_METADATA)
.item(ItemPath.create(MetadataType.F_CREATOR_REF, T_OBJECT_REFERENCE, UserType.F_NAME))
.eqPoly("creator"),
user1Oid);
}

// endregion
Expand Down Expand Up @@ -1996,12 +1991,9 @@ public void test615SearchAssignmentByApproverNameWithOrder() throws SchemaExcept

@Test
public void test616SearchInducements() throws SchemaException {
queryRecorder.clearBufferAndStartRecording();
SearchResultList<AssignmentType> result = searchContainerTest(
"search inducements", AssignmentType.class,
f -> f.ownedBy(AbstractRoleType.class, AbstractRoleType.F_INDUCEMENT).block().endBlock());
queryRecorder.stopRecording();
display(queryRecorder.getQueryBuffer().toString());
assertThat(result)
.singleElement()
.matches(a -> a.getLifecycleState().equals("role-ind-lc"));
Expand Down Expand Up @@ -2464,60 +2456,38 @@ public void test971IsDescendant() throws Exception {
}

@Test
public void test980FindOrgByUser() throws Exception {
queryRecorder.clearBufferAndStartRecording();
try {
searchObjectTest("Org by User", OrgType.class,
f -> f.referencedBy(UserType.class, UserType.F_PARENT_ORG_REF)
.id(user4Oid),
org111Oid);
} finally {
queryRecorder.stopRecording();
display(queryRecorder.getQueryBuffer().toString());
}
public void test980FindOrgByUser() throws SchemaException {
searchObjectTest("Org by User", OrgType.class,
f -> f.referencedBy(UserType.class, UserType.F_PARENT_ORG_REF)
.id(user4Oid),
org111Oid);
}

@Test
public void test981FindRoleByUser() throws Exception {
queryRecorder.clearBufferAndStartRecording();
try {
searchObjectTest("Org by User", RoleType.class,
f -> f.referencedBy(UserType.class, ItemPath.create(UserType.F_ASSIGNMENT, AssignmentType.F_TARGET_REF))
.id(user3Oid),
roleAvIOid);
} finally {
queryRecorder.stopRecording();
display(queryRecorder.getQueryBuffer().toString());
}
public void test981FindRoleByUser() throws SchemaException {
searchObjectTest("Org by User", RoleType.class,
f -> f.referencedBy(UserType.class,
ItemPath.create(UserType.F_ASSIGNMENT, AssignmentType.F_TARGET_REF))
.id(user3Oid),
roleAvIOid);
}

@Test
public void test982FindRoleByAssignmentOfUser() throws Exception {
queryRecorder.clearBufferAndStartRecording();
try {
searchObjectTest("Org by Assignment ownedBy user", RoleType.class,
f -> f.referencedBy(AssignmentType.class, AssignmentType.F_TARGET_REF)
.ownedBy(UserType.class)
.id(user3Oid),
roleAvIOid);
} finally {
queryRecorder.stopRecording();
display(queryRecorder.getQueryBuffer().toString());
}
public void test982FindRoleByAssignmentOfUser() throws SchemaException {
searchObjectTest("Org by Assignment ownedBy user", RoleType.class,
f -> f.referencedBy(AssignmentType.class, AssignmentType.F_TARGET_REF)
.ownedBy(UserType.class)
.id(user3Oid),
roleAvIOid);
}

@Test
public void test983FindUserByAssignmentTarget() throws Exception {
queryRecorder.clearBufferAndStartRecording();
try {
searchObjectTest("User by Assignment targetRef with ref and target subfilter", UserType.class,
f -> f.ref(ItemPath.create(F_ASSIGNMENT, AssignmentType.F_TARGET_REF), RoleType.COMPLEX_TYPE, relation2)
.item(RoleType.F_NAME).eq("role-ass-vs-ind"),
user3Oid);
} finally {
queryRecorder.stopRecording();
display(queryRecorder.getQueryBuffer().toString());
}
public void test983FindUserByAssignmentTarget() throws SchemaException {
searchObjectTest("User by Assignment targetRef with ref and target subfilter", UserType.class,
f -> f.ref(ItemPath.create(F_ASSIGNMENT, AssignmentType.F_TARGET_REF),
RoleType.COMPLEX_TYPE, relation2)
.item(RoleType.F_NAME).eq("role-ass-vs-ind"),
user3Oid);
}
// endregion
}

0 comments on commit ee47a25

Please sign in to comment.