-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MID-8842 upgrade, verify files implementation started
- Loading branch information
Showing
7 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
tools/ninja/src/main/java/com/evolveum/midpoint/ninja/action/VerifyFilesAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.evolveum.midpoint.ninja.action; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.io.Writer; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
|
||
import com.evolveum.midpoint.prism.ParsingContext; | ||
import com.evolveum.midpoint.prism.PrismContext; | ||
import com.evolveum.midpoint.prism.PrismObject; | ||
import com.evolveum.midpoint.prism.PrismParser; | ||
import com.evolveum.midpoint.schema.validator.ObjectValidator; | ||
import com.evolveum.midpoint.schema.validator.ValidationItem; | ||
import com.evolveum.midpoint.schema.validator.ValidationResult; | ||
import com.evolveum.midpoint.util.LocalizableMessage; | ||
|
||
// todo finish | ||
public class VerifyFilesAction extends Action<VerifyFilesOptions, Void> { | ||
|
||
@Override | ||
public Void execute() throws Exception { | ||
for (File file : options.getFiles()) { | ||
if (!file.isDirectory()) { | ||
validateFile(file); | ||
} else { | ||
Collection<File> children = FileUtils.listFiles(file, new String[] { "xml" }, true); | ||
for (File child : children) { | ||
if (child.isDirectory()) { | ||
continue; | ||
} | ||
|
||
validateFile(child); | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private void validateFile(File file) { | ||
PrismContext prismContext = context.getPrismContext(); | ||
ParsingContext parsingContext = prismContext.createParsingContextForCompatibilityMode(); | ||
PrismParser parser = prismContext.parserFor(file).language(PrismContext.LANG_XML).context(parsingContext); | ||
|
||
ObjectValidator validator = new ObjectValidator(context.getPrismContext()); | ||
validator.setAllWarnings(); | ||
|
||
try (Writer writer = new PrintWriter(System.out)) { | ||
List<PrismObject<?>> objects = parser.parseObjects(); | ||
|
||
for (PrismObject<?> object : objects) { | ||
ValidationResult validationResult = validator.validate(object); | ||
|
||
for (ValidationItem validationItem : validationResult.getItems()) { | ||
writeValidationItem(writer, object, validationItem); | ||
} | ||
} | ||
} catch (Exception ex) { | ||
// todo handle error | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
private void writeValidationItem(Writer writer, PrismObject<?> object, ValidationItem validationItem) throws IOException { | ||
if (validationItem.getStatus() != null) { | ||
writer.append(validationItem.getStatus().toString()); | ||
writer.append(" "); | ||
} else { | ||
writer.append("INFO "); | ||
} | ||
writer.append(object.toString()); | ||
writer.append(" "); | ||
if (validationItem.getItemPath() != null) { | ||
writer.append(validationItem.getItemPath().toString()); | ||
writer.append(" "); | ||
} | ||
writeMessage(writer, validationItem.getMessage()); | ||
writer.append("\n"); | ||
} | ||
|
||
private void writeMessage(Writer writer, LocalizableMessage message) throws IOException { | ||
if (message == null) { | ||
return; | ||
} | ||
// TODO: localization? | ||
writer.append(message.getFallbackMessage()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tools/ninja/src/main/java/com/evolveum/midpoint/ninja/action/VerifyFilesOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.evolveum.midpoint.ninja.action; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.beust.jcommander.Parameter; | ||
import com.beust.jcommander.Parameters; | ||
|
||
@Parameters(resourceBundle = "messages", commandDescriptionKey = "verifyFiles") | ||
public class VerifyFilesOptions extends VerifyOptions { | ||
|
||
public static final String P_FILES = "--files"; | ||
|
||
@Parameter(names = { P_FILES }, descriptionKey = "verifyFiles.files", variableArity = true) | ||
private List<File> files = new ArrayList<>(); | ||
|
||
public List<File> getFiles() { | ||
return files; | ||
} | ||
|
||
public void setFiles(List<File> files) { | ||
this.files = files; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
tools/ninja/src/test/java/com/evolveum/midpoint/ninja/upgrade/VerifyFilesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.evolveum.midpoint.ninja.upgrade; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import com.evolveum.midpoint.ninja.action.BaseOptions; | ||
import com.evolveum.midpoint.ninja.action.ConnectionOptions; | ||
import com.evolveum.midpoint.ninja.action.VerifyFilesAction; | ||
import com.evolveum.midpoint.ninja.action.VerifyFilesOptions; | ||
import com.evolveum.midpoint.ninja.impl.NinjaContext; | ||
|
||
public class VerifyFilesTest { | ||
|
||
protected static final File UPGRADE_MIDPOINT_HOME = new File("./target/midpoint-home-upgrade"); | ||
|
||
@Test | ||
public void verifyFilesTest() throws Exception { | ||
BaseOptions baseOptions = new BaseOptions(); | ||
baseOptions.setVerbose(true); | ||
|
||
ConnectionOptions connectionOptions = new ConnectionOptions(); | ||
connectionOptions.setMidpointHome(UPGRADE_MIDPOINT_HOME.getPath()); | ||
connectionOptions.setOffline(true); | ||
|
||
VerifyFilesOptions verifyFilesOptions = new VerifyFilesOptions(); | ||
verifyFilesOptions.setFiles(List.of(new File("./src/test/resources/upgrade/objects"))); | ||
verifyFilesOptions.setReport(new File("./target/verify-files.csv")); | ||
|
||
try (NinjaContext context = new NinjaContext(List.of(baseOptions, connectionOptions, verifyFilesOptions))) { | ||
|
||
VerifyFilesAction action = new VerifyFilesAction(); | ||
action.init(context, verifyFilesOptions); | ||
|
||
action.execute(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<case xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"> | ||
<taskRef oid="0e5b7304-ea5c-438e-84d1-2b0ce40517ce"/> | ||
</case> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<resource xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3" | ||
xmlns:cap="http://midpoint.evolveum.com/xml/ns/public/resource/capabilities-3"> | ||
|
||
<capabilities> | ||
<configured> | ||
<cap:addRemoveAttributeValues/> | ||
</configured> | ||
</capabilities> | ||
</resource> |
7 changes: 7 additions & 0 deletions
7
tools/ninja/src/test/resources/upgrade/objects/system-configuration.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<systemConfiguration xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"> | ||
<adminGuiConfiguration> | ||
<userDashboardLink> | ||
<targetUrl>https://google.com</targetUrl> | ||
</userDashboardLink> | ||
</adminGuiConfiguration> | ||
</systemConfiguration> |