Skip to content

Commit

Permalink
tests for ninja, work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
1azyman committed Mar 16, 2018
1 parent 4790618 commit 10c7879
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 42 deletions.
106 changes: 81 additions & 25 deletions tools/ninja/src/test/java/com/evolveum/midpoint/ninja/BaseTest.java
Expand Up @@ -5,15 +5,18 @@
import com.evolveum.midpoint.ninja.opts.ConnectionOptions;
import com.evolveum.midpoint.ninja.util.NinjaUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.AssertJUnit;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import java.io.File;
import java.io.IOException;
import java.io.*;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Viliam Repan (lazyman).
Expand All @@ -24,6 +27,11 @@ public class BaseTest {

private static final File TARGET_HOME = new File("./target/home");

public static final String RESOURCES_FOLDER = "./target/test-classes/xml";

private List<String> systemOut;
private List<String> systemErr;

@BeforeMethod
public final void beforeMethod(Method method) throws Exception {
LOG.info(">>>>>>>>>>>>>>>> Start " + method.getDeclaringClass().getSimpleName() + "."
Expand Down Expand Up @@ -77,35 +85,83 @@ protected void executeTest(String... args) {

protected void executeTest(ExecutionValidator preExecutionValidator,
ExecutionValidator postExecutionValidator, String... args) {
executeTest(null, preExecutionValidator, postExecutionValidator, false, false, args);
}

protected void executeTest(ExecutionValidator preInit,
ExecutionValidator preExecution,
ExecutionValidator postExecution,
boolean saveOut, boolean saveErr, String... args) {

systemOut = new ArrayList<>();
systemErr = new ArrayList<>();

Main main = new Main() {

@Override
protected void preExecute(NinjaContext context) {
try {
if (preExecutionValidator != null) {
LOG.info(">>>>>>>>>>>>>>>> Starting pre execution validation");
preExecutionValidator.validate(context);
}
} catch (Exception ex) {
logTestExecutionFail("Pre execution test failed with exception", ex);
ByteArrayOutputStream bosOut = new ByteArrayOutputStream();
ByteArrayOutputStream bosErr = new ByteArrayOutputStream();

if (saveOut) {
System.setOut(new PrintStream(bosOut));
}

if (saveErr) {
System.setErr(new PrintStream(bosErr));
}

try {
Main main = new Main() {

@Override
protected void preInit(NinjaContext context) {
validate(preInit, context, "pre init");
}
}

@Override
protected void postExecute(NinjaContext context) {
try {
if (postExecutionValidator != null) {
LOG.info(">>>>>>>>>>>>>>>> Starting post execution validation");
postExecutionValidator.validate(context);
}
} catch (Exception ex) {
logTestExecutionFail("Post execution test failed with exception", ex);
@Override
protected void preExecute(NinjaContext context) {
validate(preExecution, context, "pre execution");
}

@Override
protected void postExecute(NinjaContext context) {
validate(postExecution, context, "post execution");
}
};

main.run(args);
} finally {
try {
if (saveOut) {
System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
systemOut = IOUtils.readLines(new ByteArrayInputStream(bosOut.toByteArray()), StandardCharsets.UTF_8);
}

if (saveErr) {
System.setErr(new PrintStream(new FileOutputStream(FileDescriptor.err)));
systemErr = IOUtils.readLines(new ByteArrayInputStream(bosErr.toByteArray()), StandardCharsets.UTF_8);
}
} catch (IOException ex) {
}
};
}
}

protected List<String> getSystemOut() {
return systemOut;
}

protected List<String> getSystemErr() {
return systemErr;
}

main.run(args);
private void validate(ExecutionValidator validator, NinjaContext context, String message) {
if (validator == null) {
return;
}

try {
LOG.info("Starting {}", message);
validator.validate(context);
} catch (Exception ex) {
logTestExecutionFail("Validation '" + message + "' failed with exception", ex);
}
}

private void logTestExecutionFail(String message, Exception ex) {
Expand Down
@@ -1,15 +1,55 @@
package com.evolveum.midpoint.ninja;

import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;

import java.io.ByteArrayOutputStream;
import java.lang.reflect.Method;
import java.util.List;

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

@Override
protected void beforeMethodInternal(Method method) throws Exception {
setupMidpointHome();
}

@Test
public void importByOid() throws Exception {
public void importByOid() {
String[] input = new String[]{"-m", getMidpointHome(), "import", "-o", "00000000-0000-0000-0000-111100000002",
"-i", RESOURCES_FOLDER + "/objects.zip", "-z"};

ByteArrayOutputStream bos = new ByteArrayOutputStream();

executeTest(null,
context -> {
RepositoryService repo = context.getRepository();

OperationResult result = new OperationResult("count objects");
int count = repo.countObjects(ObjectType.class, null, null, result);

AssertJUnit.assertEquals(0, count);
},
context -> {
RepositoryService repo = context.getRepository();

OperationResult result = new OperationResult("count users");
int count = repo.countObjects(UserType.class, null, null, result);

AssertJUnit.assertEquals(1, count);
},
true, true, input);

List<String> out = getSystemOut();
AssertJUnit.assertEquals(out.toString(), 5, out.size());
AssertJUnit.assertTrue(getSystemErr().isEmpty());
}

@Test
Expand Down
16 changes: 0 additions & 16 deletions tools/ninja/src/test/resources/logback-test.xml

This file was deleted.

Binary file not shown.
Binary file not shown.

0 comments on commit 10c7879

Please sign in to comment.