Skip to content

Commit

Permalink
MID-8842 ninja - verification with output now dumps deltas to xml (no…
Browse files Browse the repository at this point in the history
…t for files, yet)
  • Loading branch information
1azyman committed Jul 13, 2023
1 parent c0daae1 commit 3988a57
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
*/
package com.evolveum.midpoint.schema.validator;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;

import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.schema.result.OperationResultStatus;
import com.evolveum.midpoint.util.LocalizableMessage;
Expand Down Expand Up @@ -125,22 +129,29 @@ private <V extends PrismValue, D extends ItemDefinition<?>> void visitItem(Item<
return;
}

List<String> messages = new ArrayList<>();
if (warnDeprecated && definition.isDeprecated()) {
warn(result, item, "deprecated");
messages.add("deprecated");
}

if (warnPlannedRemoval) {
String plannedRemoval = definition.getPlannedRemoval();
if (plannedRemoval != null) {
if (warnPlannedRemovalVersion == null || plannedRemoval.equals(warnPlannedRemovalVersion)) {
warn(result, item, "planned for removal in version " + plannedRemoval);
messages.add("planned for removal in version " + plannedRemoval);
}
}
}

if (warnRemoved && definition.isRemoved()) {
warn(result, item, "removed");
messages.add("removed");
}

if (messages.isEmpty()) {
return;
}

warn(result, item, StringUtils.join(messages, ", "));
}

private void checkOid(ValidationResult result, PrismValue item, String oid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private static CSVFormat createCsvFormat() {
.build();
}

public <T extends Objectable> void verify(Writer writer, PrismObject<T> object) throws IOException {
public <T extends Objectable> UpgradeValidationResult verify(Writer writer, PrismObject<T> object) throws IOException {
UpgradeValidationResult result = validator.validate((PrismObject) object);

for (UpgradeValidationItem item : result.getItems()) {
Expand All @@ -143,6 +143,8 @@ public <T extends Objectable> void verify(Writer writer, PrismObject<T> object)
default:
throw new IllegalArgumentException("Unknown report style " + options.getReportStyle());
}

return result;
}

public static String getIdentifierFromRecord(CSVRecord record) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package com.evolveum.midpoint.ninja.action.worker;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.concurrent.BlockingQueue;
Expand All @@ -15,16 +17,30 @@
import com.evolveum.midpoint.ninja.action.verify.VerificationReporter;
import com.evolveum.midpoint.ninja.impl.NinjaContext;
import com.evolveum.midpoint.ninja.util.OperationStatus;
import com.evolveum.midpoint.prism.DeepCloneOperation;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.schema.DeltaConversionOptions;
import com.evolveum.midpoint.schema.DeltaConvertor;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.validator.UpgradeValidationResult;
import com.evolveum.midpoint.util.DOMUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

import org.apache.commons.io.IOUtils;

/**
* TODO from design point of view handling of main writer and deltaWriter is just nasty. Fix this.
*
* @author Radovan Semancik
*/
public class VerifyConsumerWorker extends AbstractWriterConsumerWorker<VerifyOptions, ObjectType> {

private VerificationReporter reporter;

private Writer deltaWriter;

public VerifyConsumerWorker(NinjaContext context, VerifyOptions options,
BlockingQueue<ObjectType> queue, OperationStatus operation) {
super(context, options, queue, operation);
Expand All @@ -33,10 +49,38 @@ public VerifyConsumerWorker(NinjaContext context, VerifyOptions options,
@Override
protected void init() {
reporter = new VerificationReporter(options, context.getPrismContext());

if (options.getOutput() != null && VerifyOptions.ReportStyle.CSV.equals(options.getReportStyle())) {
try {
File deltaFile = new File(options.getOutput() + ".delta.xml");
if (deltaFile.exists()) {
deltaFile.delete();
}

deltaFile.createNewFile();

deltaWriter = new FileWriter(deltaFile, context.getCharset());
deltaWriter.write(
"<deltas "
+ "xmlns=\"http://midpoint.evolveum.com/xml/ns/public/common/api-types-3\" "
+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
+ "xsi:type=\"ObjectDeltaListType\">\n");
} catch (IOException ex) {
// todo handle exception
ex.printStackTrace();
}
}
}

@Override
public void destroy() {
try {
deltaWriter.write("</deltas>\n");
} catch (IOException ex) {
// todo handle exception
ex.printStackTrace();
}
IOUtils.closeQuietly(deltaWriter);
}

@Override
Expand All @@ -51,9 +95,23 @@ protected String getEpilog() {

@Override
protected void write(Writer writer, ObjectType object) throws IOException {
PrismObject<?> prismObject = object.asPrismObject();
PrismObject prismObject = object.asPrismObject();

try {
PrismObject<?> cloned = prismObject.clone();

reporter.verify(writer, cloned);
ObjectDelta delta = prismObject.diff(cloned);

if (delta.isEmpty()) {
return;
}

reporter.verify(writer, prismObject);
writer.write(DeltaConvertor.serializeDelta(delta, DeltaConversionOptions.createSerializeReferenceNames(), "xml"));
} catch (Exception ex) {
// todo handle exception
ex.printStackTrace();
}
}

public VerifyResult getResult() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

/**
* TODO enable under some extra profile, since this will download newest version of midpoint (takes some time to execute + bandwidth)
*/
public class DistributionManagerTest {

private static final Trace LOGGER = TraceManager.getTrace(DistributionManagerTest.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import com.evolveum.midpoint.test.util.AbstractSpringTest;
import com.evolveum.midpoint.test.util.InfraTestMixin;

/**
* Base class for Ninja tests that need Spring context, e.g. for repository state initialization.
*/
public abstract class NinjaSpringTest extends AbstractSpringTest implements InfraTestMixin, NinjaTestMixin {

@Qualifier("repositoryService")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

/**
* Mixin that contains helper methods for Ninja tests.
* Gives user flexibility to execute test using action class or through full execution via cmd parameters.
* This mixin can be used for tests where spring context is not needed.
*/
public interface NinjaTestMixin {

StreamValidator EMPTY_STREAM_VALIDATOR = list -> Assertions.assertThat(list).isEmpty();
Expand Down Expand Up @@ -120,6 +125,9 @@ default <O, R, A extends Action<O, R>> R executeAction(
return executeAction(actionClass, actionOptions, allOptions, null, null);
}

/**
* Allows to test specific action using via java configuration, skips cmd parsing.
*/
default <O, R, A extends Action<O, R>> R executeAction(
@NotNull Class<A> actionClass, @NotNull O actionOptions, @NotNull List<Object> allOptions,
@Nullable StreamValidator validateOut, @Nullable StreamValidator validateErr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.fusesource.jansi.AnsiConsole;

/**
* TODO remove
* Created by Viliam Repan (lazyman).
*/
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ public void verifyFilesTest() throws Exception {
VerifyOptions verifyOptions = new VerifyOptions();
verifyOptions.setFiles(List.of(new File("./src/test/resources/upgrade/objects")));
verifyOptions.setReportStyle(VerifyOptions.ReportStyle.CSV);
verifyOptions.setOverwrite(true);
verifyOptions.setOutput(new File("./target/verify.csv"));

List<Object> options = List.of(baseOptions, connectionOptions, verifyOptions);

VerifyResult result = executeAction(VerifyAction.class, verifyOptions, options);

Assertions.assertThat(result).isNotNull();
Assertions.assertThat(result.getItemPriorityCount(UpgradePriority.OPTIONAL)).isEqualTo(2L);
Assertions.assertThat(result.getItemPriorityCount(UpgradePriority.OPTIONAL)).isEqualTo(1L);
}

@Test(enabled = false)
Expand Down

0 comments on commit 3988a57

Please sign in to comment.