Skip to content

Commit

Permalink
MID-8842 ninja, trying to finally fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed Jun 22, 2023
1 parent 79d8845 commit 9252882
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class UpgradeObjectsOptions extends ExportOptions {

public static final String P_VERIFICATION_LONG = "--verification-file";

@ParametersDelegate
private OutputOptions outputOptions = new OutputOptions();

@ParametersDelegate
private SearchOptions searchOptions = new SearchOptions();
// @ParametersDelegate
// private OutputOptions outputOptions = new OutputOptions();
//
// @ParametersDelegate
// private SearchOptions searchOptions = new SearchOptions();

@Parameter(names = { P_VERIFICATION_LONG }, descriptionKey = "upgradeObjects.verification")
private File verification;
Expand All @@ -35,19 +35,19 @@ public void setVerification(File verification) {
this.verification = verification;
}

public OutputOptions getOutputOptions() {
return outputOptions;
}

public void setOutputOptions(OutputOptions outputOptions) {
this.outputOptions = outputOptions;
}

public SearchOptions getSearchOptions() {
return searchOptions;
}

public void setSearchOptions(SearchOptions searchOptions) {
this.searchOptions = searchOptions;
}
// public OutputOptions getOutputOptions() {
// return outputOptions;
// }
//
// public void setOutputOptions(OutputOptions outputOptions) {
// this.outputOptions = outputOptions;
// }
//
// public SearchOptions getSearchOptions() {
// return searchOptions;
// }
//
// public void setSearchOptions(SearchOptions searchOptions) {
// this.searchOptions = searchOptions;
// }
}
5 changes: 5 additions & 0 deletions tools/ninja/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,8 @@ upgradeObjects.verification=Verification results file
output.output=Output file path. If file is not defined data will be printed out to SYSOUT.
output.overwrite=Overwrite output file if it exists.
upgradeFiles=Upgrade files
search.raw=Use raw option
search.oid=Object OID
search.type=Object type, case insensitive value, e.g. "user". Do not use "Type" suffix.
search.filter=Value of object filter used to search objects. If you start the filter with the \
letter @, the rest should be a filename. Start the filter with % to use Axiom query language.
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,29 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType;

import java.io.File;
import java.io.IOException;

/**
* Created by Viliam Repan (lazyman).
*/
public class ImportInvalidRepositoryTest extends BaseTest {

private static final String PATH_UNKNOWN_NODES_ZIP = "./target/unknown-nodes.zip";

@BeforeClass
public void beforeClass() throws IOException {
TestUtils.zipFile(new File("./src/test/resources/unknown-nodes"), new File(PATH_UNKNOWN_NODES_ZIP));
}

@BeforeClass
public void initMidpointHome() throws Exception {
setupMidpointHome();
}

@Test
public void test100Import() throws Exception {
String[] args = new String[] { "-m", getMidpointHome(), "import", "-i", RESOURCES_DIRECTORY_PATH + "/unknown-nodes.zip", "-z" };
String[] args = new String[] { "-m", getMidpointHome(), "import", "-i", PATH_UNKNOWN_NODES_ZIP, "-z" };

ActionStateListener listener = new ActionStateListener() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
*/
package com.evolveum.midpoint.ninja;

import java.io.File;
import java.io.IOException;

import org.assertj.core.api.Assertions;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

Expand All @@ -25,18 +29,22 @@
*/
public class ImportRepositoryTest extends BaseTest {

private static final String PATH_MONKEY_ISLAND_SIMPLE_ZIP = "./target/org-monkey-island-simple.zip";

@BeforeClass
public void beforeClass() throws IOException {
TestUtils.zipFile(new File("./src/test/resources/org-monkey-island-simple.xml"), new File(PATH_MONKEY_ISLAND_SIMPLE_ZIP));
}

@BeforeMethod
public void initMidpointHome() throws Exception {
setupMidpointHome();
}

@Test
public void test100ImportByOid() throws Exception {
// Try this line to be sure what the config is (assuming init method runs fine):
// new Main().run(new String[] { "-m", getMidpointHome(), "info" });

String[] args = new String[] { "-m", getMidpointHome(), "import", "-o", "00000000-8888-6666-0000-100000000001",
"-i", RESOURCES_DIRECTORY_PATH + "/org-monkey-island-simple.xml.zip", "-z" };
String[] args = new String[] { "-m", getMidpointHome(), "import", "--oid", "00000000-8888-6666-0000-100000000001",
"-i", PATH_MONKEY_ISLAND_SIMPLE_ZIP, "-z" };

ActionStateListener listener = new ActionStateListener() {

Expand Down Expand Up @@ -84,7 +92,7 @@ public void onAfterExecution(NinjaContext context) {
@Test
public void test110ImportByFilterAsOption() throws Exception {
String[] args = new String[] { "-m", getMidpointHome(), "import", "-f", "<equal><path>name</path><value>F0002</value></equal>",
"-i", RESOURCES_DIRECTORY_PATH + "/org-monkey-island-simple.xml.zip", "-z" };
"-i", PATH_MONKEY_ISLAND_SIMPLE_ZIP, "-z" };

ActionStateListener listener = new ActionStateListener() {

Expand Down Expand Up @@ -128,7 +136,7 @@ public void onAfterExecution(NinjaContext context) {
@Test
public void test120ImportByFilterAsFile() throws Exception {
String[] args = new String[] { "-m", getMidpointHome(), "import", "-f", "@src/test/resources/filter.xml",
"-i", RESOURCES_DIRECTORY_PATH + "/org-monkey-island-simple.xml.zip", "-z" };
"-i", PATH_MONKEY_ISLAND_SIMPLE_ZIP, "-z" };

ActionStateListener listener = new ActionStateListener() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,16 @@ private <R> R executeWithWrappedPrintStreams(

return function.apply(out, err);
} finally {
processTestOutputStream(bosOut, validateOut, "OUT");
processTestOutputStream(bosErr, validateErr, "ERR");
List<String> outLines = processTestOutputStream(bosOut, "OUT");
List<String> errLines = processTestOutputStream(bosErr, "ERR");

if (validateOut != null) {
validateOut.accept(outLines);
}

if (validateErr != null) {
validateErr.accept(errLines);
}
}
}

Expand All @@ -87,19 +95,14 @@ default void executeTest(
}, validateOut, validateErr);
}

private void processTestOutputStream(
ByteArrayOutputStream bos, Consumer<List<String>> validator, String prefix)
throws IOException {

private List<String> processTestOutputStream(ByteArrayOutputStream bos, String prefix) throws IOException {
final Trace logger = TraceManager.getTrace(getClass());

List<String> lines = IOUtils.readLines(new ByteArrayInputStream(bos.toByteArray()), StandardCharsets.UTF_8);
if (logger.isDebugEnabled()) {
lines.forEach(line -> logger.debug("{}: {}", prefix, line));
}

if (validator != null) {
validator.accept(lines);
}
return lines;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.evolveum.midpoint.ninja;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

public class TestUtils {

public static void zipFile(File input, File output) throws IOException {
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output))) {
zipFile(input, input.toPath(), zos);
}
}

private static void zipFile(File input, Path rootPath, ZipOutputStream zos) throws IOException {
if (input.isFile()) {
Path filePath = input.toPath();
String relativePath = rootPath.relativize(filePath).toString();
if (StringUtils.isEmpty(relativePath)) {
relativePath = input.getName();
}

ZipEntry ze = new ZipEntry(relativePath);
zos.putNextEntry(ze);

try (FileInputStream fis = new FileInputStream(input)) {
IOUtils.copy(fis, zos);
}
zos.closeEntry();
} else {
File[] fileList = input.listFiles();
for (File file : fileList) {
zipFile(file, rootPath, zos);
}
}

// File[] fileList = input.listFiles();
// for (File file : fileList) {
// if (file.isDirectory()) {
// zipFile(file, rootPath, zos);
// } else {
// Path filePath = file.toPath();
// String relativePath = rootPath.relativize(filePath).toString();
// ZipEntry ze = new ZipEntry(relativePath);
// zos.putNextEntry(ze);
//
// try (FileInputStream fis = new FileInputStream(file)) {
// IOUtils.copy(fis, zos);
// }
// zos.closeEntry();
// }
// }
}
}

0 comments on commit 9252882

Please sign in to comment.