Skip to content

Commit

Permalink
Implementing model.importObjectsFromFile. Lookup table reimport test.
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Mar 27, 2015
1 parent 196fbea commit 83bf757
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 14 deletions.
Expand Up @@ -16,6 +16,7 @@
package com.evolveum.midpoint.model.api;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -522,7 +523,7 @@ void importFromResource(String shadowOid, Task task, OperationResult parentResul
* @param input
* @param task
*/
void importObjectsFromFile(File input, ImportOptionsType options, Task task, OperationResult parentResult);
void importObjectsFromFile(File input, ImportOptionsType options, Task task, OperationResult parentResult) throws FileNotFoundException;

/**
* Import objects from stream.
Expand Down
Expand Up @@ -16,6 +16,9 @@
package com.evolveum.midpoint.model.impl.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -47,6 +50,7 @@
import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -1806,13 +1810,29 @@ public void importFromResource(String shadowOid, Task task, OperationResult pare

@Override
public void importObjectsFromFile(File input, ImportOptionsType options, Task task,
OperationResult parentResult) {
// OperationResult result =
// parentResult.createSubresult(IMPORT_OBJECTS_FROM_FILE);
// TODO Auto-generated method stub
RepositoryCache.enter();
RepositoryCache.exit();
throw new NotImplementedException();
OperationResult parentResult) throws FileNotFoundException {
OperationResult result = parentResult.createSubresult(IMPORT_OBJECTS_FROM_FILE);
FileInputStream fis;
try {
fis = new FileInputStream(input);
} catch (FileNotFoundException e) {
String msg = "Error reading from file "+input+": "+e.getMessage();
result.recordFatalError(msg, e);
throw e;
}
try {
importObjectsFromStream(fis, options, task, parentResult);
} catch (RuntimeException e) {
result.recordFatalError(e);
throw e;
} finally {
try {
fis.close();
} catch (IOException e) {
Log.error("Error closing file "+input+": "+e.getMessage(), e);
}
}
result.computeStatus();
}

@Override
Expand Down
Expand Up @@ -258,10 +258,10 @@ public void handleGlobalError(OperationResult currentResult) {
if (options.getStopAfterErrors() != null) {
validator.setStopAfterErrors(options.getStopAfterErrors().longValue());
}
if (options.isSummarizeErrors()) {
if (BooleanUtils.isTrue(options.isSummarizeErrors())) {
parentResult.setSummarizeErrors(true);
}
if (options.isSummarizeSucceses()) {
if (BooleanUtils.isTrue(options.isSummarizeSucceses())) {
parentResult.setSummarizeSuccesses(true);
}
}
Expand Down
Expand Up @@ -1736,7 +1736,7 @@ private <O extends ObjectType> void assertDeleteAllow(Class<O> type, String oid,
logAllow("delete", type, oid, null);
}

private void assertImportDeny(File file) {
private void assertImportDeny(File file) throws FileNotFoundException {
Task task = taskManager.createTaskInstance(TestSecurity.class.getName() + ".assertImportDeny");
OperationResult result = task.getResult();
// This does not throw exception, failure is indicated in the result
Expand All @@ -1745,7 +1745,7 @@ private void assertImportDeny(File file) {
TestUtil.assertFailure(result);
}

private void assertImportAllow(File file) {
private void assertImportAllow(File file) throws FileNotFoundException {
Task task = taskManager.createTaskInstance(TestSecurity.class.getName() + ".assertImportAllow");
OperationResult result = task.getResult();
modelService.importObjectsFromFile(file, null, task, result);
Expand Down
Expand Up @@ -47,12 +47,14 @@
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.ImportOptionsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.CredentialsType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableRowType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.PasswordType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;

import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
Expand Down Expand Up @@ -915,7 +917,46 @@ public void test180LookupLanguagesReplaceObject() throws Exception {
assertNotNull("Table container missing", tableContainer);
assertEquals("Unexpected table container size", 1, tableContainer.size());

assertLookupRow(tableContainer, "fr_FR", "fr", "Francais");
assertLookupRow(tableContainer, "fr_FR", "fr", "Français");
assertSteadyResources();
}

@Test
public void test182LookupLanguagesReimport() throws Exception {
final String TEST_NAME="test182LookupLanguagesReimport";
TestUtil.displayTestTile(this, TEST_NAME);

// GIVEN
Task task = taskManager.createTaskInstance(TestModelServiceContract.class.getName() + "." + TEST_NAME);
OperationResult result = task.getResult();

ImportOptionsType options = new ImportOptionsType();
options.setOverwrite(true);
options.setKeepOid(true);

// WHEN
TestUtil.displayWhen(TEST_NAME);
modelService.importObjectsFromFile(LOOKUP_LANGUAGES_FILE, options, task, result);

// THEN
TestUtil.displayThen(TEST_NAME);
result.computeStatus();
TestUtil.assertSuccess(result);

PrismObject<LookupTableType> lookup = getLookupTableAll(LOOKUP_LANGUAGES_OID, task, result);

result.computeStatus();
TestUtil.assertSuccess(result);

IntegrationTestTools.display("Languages", lookup);

assertEquals("Wrong lang lookup name", "Languages", lookup.asObjectable().getName().getOrig());

checkLookupResult(lookup, new String[]{"en_US", "en", "English (US)"},
new String[]{"en_PR", "en", "English (pirate)"},
new String[]{"sk_SK", "sk", "Slovak"},
new String[]{"tr_TR", "tr", "Turkish"});

assertSteadyResources();
}

Expand Down
Expand Up @@ -28,7 +28,7 @@
<row>
<key>fr_FR</key>
<value>fr</value>
<label>Francais</label>
<label>Français</label>
</row>

</lookupTable>

0 comments on commit 83bf757

Please sign in to comment.