Skip to content

Commit

Permalink
repo-sqale: implemented getVersion + test
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Jun 29, 2021
1 parent 147d449 commit 0a59f1e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public class SqaleRepositoryService implements RepositoryService {
private final SqlQueryExecutor sqlQueryExecutor;
private final SqlPerformanceMonitorsCollection sqlPerformanceMonitorsCollection;

// TODO: see comment in the SystemConfigurationChangeDispatcherImpl for related issues
@Autowired private SystemConfigurationChangeDispatcher systemConfigurationChangeDispatcher;

private final ThreadLocal<List<ConflictWatcherImpl>> conflictWatchersThreadLocal =
Expand Down Expand Up @@ -209,8 +208,53 @@ private <S extends ObjectType> S readByOid(
public <T extends ObjectType> String getVersion(
Class<T> type, String oid, OperationResult parentResult)
throws ObjectNotFoundException, SchemaException {
return null;
// TODO
Validate.notNull(type, "Object type must not be null.");
UUID uuid = checkOid(oid);
Validate.notNull(parentResult, "Operation result must not be null.");

LOGGER.debug("Getting version for {} with oid '{}'.", type.getSimpleName(), oid);

OperationResult operationResult = parentResult.subresult(GET_VERSION)
.addQualifier(type.getSimpleName())
.addParam("type", type.getName())
.addParam("oid", oid)
.build();

try {
return executeGetVersion(type, uuid);
} catch (RuntimeException e) {
throw handledGeneralException(e, operationResult);
} catch (Throwable t) {
operationResult.recordFatalError(t);
throw t;
} finally {
operationResult.computeStatusIfUnknown();
}
}

private <T extends ObjectType> String executeGetVersion(Class<T> type, UUID oid)
throws ObjectNotFoundException {
long opHandle = registerOperationStart(OP_GET_OBJECT, type);
try (JdbcSession jdbcSession =
repositoryContext.newJdbcSession().startReadOnlyTransaction()) {
SqaleTableMapping<T, QObject<MObject>, MObject> rootMapping =
repositoryContext.getMappingBySchemaType(type);
QObject<MObject> root = rootMapping.defaultAlias();

Integer version = jdbcSession.newQuery().select(root.version)
.from(root)
.where(root.oid.eq(oid))
.fetchOne();
if (version == null) {
throw new ObjectNotFoundException(type, oid.toString());
}

String versionString = version.toString();
invokeConflictWatchers((w) -> w.afterGetVersion(oid.toString(), versionString));
return versionString;
} finally {
registerOperationFinish(opHandle, 1); // TODO attempt (separate try from JDBC session)
}
}

// Add/modify/delete
Expand Down Expand Up @@ -322,7 +366,8 @@ private <T extends ObjectType> String executeOverwriteObject(
throws SchemaException, RepositoryException, ObjectAlreadyExistsException {

String oid = newObject.getOid();
UUID oidUuid = UUID.fromString(oid);
UUID oidUuid = checkOid(oid);

long opHandle = registerOperationStart(OP_ADD_OBJECT_OVERWRITE, newObject);
// TODO use executeAttempts
try (JdbcSession jdbcSession = repositoryContext.newJdbcSession().startTransaction()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
package com.evolveum.midpoint.repo.sqale.func;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import static com.evolveum.midpoint.repo.sqlbase.querydsl.FlexibleRelationalPathBase.DEFAULT_SCHEMA_NAME;

import java.util.UUID;

import org.testng.annotations.Test;

import com.evolveum.midpoint.prism.PrismObject;
Expand Down Expand Up @@ -92,6 +95,31 @@ public void test200GetObject() throws SchemaException, ObjectNotFoundException {
assertSingleOperationRecorded(pm, RepositoryService.OP_GET_OBJECT);
}

@Test
public void test210GetVersion() throws SchemaException, ObjectNotFoundException {
OperationResult result = createOperationResult();

when("getVersion is called for known OID");
String version = repositoryService.getVersion(UserType.class, sanityUserOid, result);

then("non-null version string is obtained");
assertThatOperationResult(result).isSuccess();
assertThat(version).isNotNull();
}

@Test
public void test211GetVersionFailure() {
OperationResult result = createOperationResult();

expect("getVersion for non-existent OID throws exception");
assertThatThrownBy(() -> repositoryService.getVersion(
UserType.class, UUID.randomUUID().toString(), result))
.isInstanceOf(ObjectNotFoundException.class);

and("operation result is fatal error");
assertThatOperationResult(result).isFatalError();
}

// TODO test for getObject() with typical options (here or separate class?)
// - ObjectOperationOptions(jpegPhoto:retrieve=INCLUDE)
// - ObjectOperationOptions(/:resolveNames)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,6 @@ public <T extends ObjectType> String getVersion(
.addParam("oid", oid)
.build();

// TODO executeAttempts
SqlPerformanceMonitorImpl pm = getPerformanceMonitor();
long opHandle = pm.registerOperationStart(OP_GET_VERSION, type);

Expand Down

0 comments on commit 0a59f1e

Please sign in to comment.