Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
skublik committed Sep 12, 2023
2 parents f01ab0c + d2e034c commit 7faf2dc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,79 +11,25 @@

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

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

import org.apache.commons.io.FileUtils;
import org.assertj.core.api.Assertions;
import org.testng.annotations.Test;

import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.schema.AbstractSchemaTest;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

public class TestMerger extends AbstractSchemaTest {

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

private static final File TEST_ROOT_DIR = new File("./src/test/resources/merger");

// not commited, contains inital objects for 4.4 and master in separate folders
private static final File TEST_DIR = new File("../../_mess/_init-objects-diff");

@Test(enabled = false)
public <O extends ObjectType> void testInitialObjects() throws Exception {
Map<String, PrismObject<O>> source = loadPrismObjects(new File(TEST_DIR, "master"));
Map<String, PrismObject<O>> target = loadPrismObjects(new File(TEST_DIR, "support-4.4"));

int successCount = 0;
int errorCount = 0;
for (String oid : source.keySet()) {
PrismObject<O> sourceObject = source.get(oid);
PrismObject<O> targetObject = target.get(oid);
if (targetObject == null) {
continue;
}

try {
SimpleObjectMergeOperation.merge(targetObject, sourceObject);
successCount++;
} catch ( Exception ex) {
errorCount++;
ex.printStackTrace();
System.out.println("Couldn't merge object " + sourceObject.toDebugName());
}
}

Assertions.assertThat(errorCount)
.withFailMessage("Successfully merged <%s> objects, <%s> errors", successCount, errorCount)
.isZero();
}

private <O extends ObjectType> Map<String, PrismObject<O>> loadPrismObjects(File dir) {
Map<String, PrismObject<O>> result = new HashMap<>();

Collection<File> files = FileUtils.listFiles(dir, new String[]{"xml"}, true);
files.stream()
.map(f -> {
try {
return (PrismObject) getPrismContext().parseObject(f);
} catch (Exception e) {
throw new IllegalStateException(e);
}
})
.forEach(o -> result.put(o.getOid(), o));

return result;
}

@Test
public void test10LookupTableMergeOperation() throws Exception {
testMergeOperation("lookup-table/lookup-table");
Expand Down Expand Up @@ -149,17 +95,21 @@ public void test130ValuePolicyMergeOperation() throws Exception {
testMergeOperation("value-policy/value-policy");
}

private void testMergeOperation(String fileNamePrefix) throws IOException, SchemaException, ConfigurationException {
PrismObject<LookupTableType> source = getPrismContext().parseObject(new File(TEST_ROOT_DIR, fileNamePrefix + "-source.xml"));
PrismObject<LookupTableType> target = getPrismContext().parseObject(new File(TEST_ROOT_DIR, fileNamePrefix + "-target.xml"));
PrismObject<LookupTableType> result = getPrismContext().parseObject(new File(TEST_ROOT_DIR, fileNamePrefix + "-result.xml"));
private <O extends ObjectType> void testMergeOperation(String fileNamePrefix) throws IOException, SchemaException, ConfigurationException {
PrismObject<O> source = getPrismContext().parseObject(new File(TEST_ROOT_DIR, fileNamePrefix + "-source.xml"));
PrismObject<O> target = getPrismContext().parseObject(new File(TEST_ROOT_DIR, fileNamePrefix + "-target.xml"));
PrismObject<O> result = getPrismContext().parseObject(new File(TEST_ROOT_DIR, fileNamePrefix + "-result.xml"));

SimpleObjectMergeOperation.merge(target, source);

LOGGER.trace("Merged object:\n{}", target.debugDump());
LOGGER.trace("Result object:\n{}", result.debugDump());

ObjectDelta<O> delta = target.diff(source);

Assertions.assertThat(target)
.matches(t -> t.equivalent(result));
.matches(
t -> t.equivalent(result),
"Merged object is not equivalent to expected result\n" + delta.debugDump());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ private <O extends ObjectType> boolean mergeObject(

boolean mergeExecuted = mergeObject(merged, initial);
if (!mergeExecuted) {
log.error("Merge operation not supported for object {}, skipping", NinjaUtils.printObjectNameOidAndType(existing));
log.error("Skipping object update, merge operation not supported for object {}.", NinjaUtils.printObjectNameOidAndType(existing));
actionResult.incrementError();
return false;
}

ObjectDelta<O> delta = existing.diff(merged);
if (delta.isEmpty()) {
log.info("Object {} merged, no differences found. Skipping object update.", NinjaUtils.printObjectNameOidAndType(existing));
log.info("Skipping object update, object {} merged, no differences found.", NinjaUtils.printObjectNameOidAndType(existing));

actionResult.incrementUnchanged();
return false;
Expand Down

0 comments on commit 7faf2dc

Please sign in to comment.