Skip to content

Commit

Permalink
SED-1021 Plan import fails when disabling "Import all entities"
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromecomte committed May 24, 2022
1 parent 869905b commit 92710a5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -577,18 +577,28 @@ public void testExportPlanByWithCustomFields() throws Exception {
testExportFile.delete();
}
}

@Test
public void testExportPlanRecursively() throws Exception {
testExportPlansRecursively(true);
testExportPlansRecursively(false, true);
}

@Test
public void testExportPlanRecursivelyNewReferences() throws Exception {
testExportPlansRecursively(false);
testExportPlansRecursively(false, false);
}

private void testExportPlansRecursively(boolean overwrite) throws Exception {

@Test
public void testExportPlanRecursivelyWithPansOnly() throws Exception {
testExportPlansRecursively(true, true);
}

@Test
public void testExportPlanRecursivelyNewReferencesWithPansOnly() throws Exception {
testExportPlansRecursively(true, false);
}

private void testExportPlansRecursively(boolean plansOnly, boolean overwrite) throws Exception {
Sequence rootSequence = sequence();
Plan plan = PlanBuilder.create().startBlock(rootSequence).add(sequence()).endBlock().build();
planAccessor.save(plan);
Expand All @@ -603,35 +613,35 @@ private void testExportPlansRecursively(boolean overwrite) throws Exception {
sequence.addChild(callFunction);
Plan plan2 = PlanBuilder.create().startBlock(rootSequence).add(sequence).endBlock().build();
planAccessor.save(plan2);

File testExportFile = new File("testExport.json");
try (FileOutputStream outputStream = new FileOutputStream(testExportFile)) {
ExportManager exportManager = newExportManager();
Map<String, String> metadata = buildMetadata();
ExportConfiguration exportConfig = new ExportConfiguration(outputStream, metadata, dummyObjectPredicate(), "plans", true, null);
exportManager.exportById(exportConfig, plan2.getId().toString());
planAccessor.getAll().forEachRemaining(p->planAccessor.remove(p.getId()));
functionAccessor.getAll().forEachRemaining(p->functionAccessor.remove(p.getId()));

planAccessor.getAll().forEachRemaining(p -> planAccessor.remove(p.getId()));
functionAccessor.getAll().forEachRemaining(p -> functionAccessor.remove(p.getId()));

ImportManager importManager = createNewContextAndGetImportManager();
importManager.importAll(new ImportConfiguration(testExportFile, dummyObjectEnricher(), null, overwrite));

AtomicInteger nbPlans = new AtomicInteger(0);
planAccessor.getAll().forEachRemaining(p-> nbPlans.incrementAndGet());
AtomicInteger nbFunctions = new AtomicInteger(0);
functionAccessor.getAll().forEachRemaining(f->nbFunctions.incrementAndGet());
assertEquals(2, nbPlans.intValue());
assertEquals(1, nbFunctions.intValue());

importManager.importAll(new ImportConfiguration(testExportFile, dummyObjectEnricher(), plansOnly ? List.of("plans") : null, overwrite));

assertEquals(2, planAccessor.stream().count());
assertEquals(plansOnly ? 0 : 1, functionAccessor.stream().count());

Plan actualPlan = planAccessor.get(plan.getId());
Plan actualPlan2 = planAccessor.get(plan2.getId());
Function actualFunction = functionAccessor.get(function.getId());
if (overwrite) {
assertEquals(plan.getId(), actualPlan.getId());
assertEquals(plan.getRoot(), actualPlan.getRoot());
assertEquals(plan2.getId(), actualPlan2.getId());
assertEquals(function.getId(), actualFunction.getId());
if (plansOnly) {
assertNull(actualFunction);
} else {
assertEquals(function.getId(), actualFunction.getId());
}
} else {
assertNull(actualPlan);
assertNull(actualPlan2);
Expand All @@ -641,7 +651,7 @@ private void testExportPlansRecursively(boolean overwrite) throws Exception {
testExportFile.delete();
}
}

@Test
public void testExportPlansWithCompo() throws Exception {
testExportPlansWithCompoFct(true);
Expand Down
27 changes: 14 additions & 13 deletions step-core/src/main/java/step/core/imports/ImportManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,25 +175,26 @@ private String importEntitiesToTemporaryCollection(ImportConfiguration importCon
FilesystemCollectionFactory tempCollectionFactory = importContext.getTempCollectionFactory();
Collection<Document> tempCollection = tempCollectionFactory.getCollection(name, Document.class);

if (!skip) {
if (entityByName == null) {
throw new RuntimeException(
"The entity type with name '" + name + "' is unsupported in this version or license of step.");
if (entityByName == null) {
throw new RuntimeException(
"The entity type with name '" + name + "' is unsupported in this version or license of step.");
}
if (jParser.nextToken().equals(JsonToken.START_ARRAY)) {
if (!skip) {
logger.info("Importing entities of type " + name);
}
logger.info("Importing entities of type " + name);
if (jParser.nextToken().equals(JsonToken.START_ARRAY)) {
while (!jParser.nextToken().equals(JsonToken.END_ARRAY)) {
while (!jParser.nextToken().equals(JsonToken.END_ARRAY)) {
if (!skip) {
importOne(tempCollection, jParser);
} else {
// consume the json object when skipped
mapper.readValue(jParser, Document.class);
}
} else {
throw new RuntimeException("A JSON array was expected for entity '" + name + "'");
}
} else {
// consume the json object when skipped
// TODO: fix this
// mapper.readValue(jParser, BasicDBObject.class);
throw new RuntimeException("A JSON array was expected for entity '" + name + "'");
}

return name;
}

Expand Down

0 comments on commit 92710a5

Please sign in to comment.