@Test
  public void shouldImportScriptWithLegacyModeOffWhenGlobalSwitchIsOnUsingImportConfig() {
    // given
    Config.setParameter(ImpExConstants.Params.LEGACY_MODE_KEY, "true");
    final ImpExResource mediaRes =
        new StreamBasedImpExResource(
            new ByteArrayInputStream("INSERT Language;isocode;active\n;test;true".getBytes()),
            CSVConstants.HYBRIS_ENCODING);
    final ImportConfig config = new ImportConfig();
    config.setLegacyMode(Boolean.FALSE);
    config.setSynchronous(true);
    config.setFailOnError(true);
    config.setScript(mediaRes);
    config.setRemoveOnSuccess(false);

    // when
    final ImportResult importResult = importService.importData(config);

    // then
    assertThat(importResult.isFinished()).isTrue();
    assertThat(importResult.isError()).isFalse();
    assertThat(Boolean.parseBoolean(Config.getParameter(ImpExConstants.Params.LEGACY_MODE_KEY)))
        .isTrue();
    assertThat(importResult.getCronJob().getLegacyMode()).isFalse();
  }
  @Test
  public void shouldSeeFinishedStateForAsncImport() throws InterruptedException {
    // given
    final ImportConfig config = new ImportConfig();
    config.setScript( //
        "INSERT Title;code[unique=true]\n" //
            + ";foo;\n" //
            + "\"#% java.lang.Thread.sleep(2000);\";\n" //
            + ";bar;\n" //
        );
    config.setSynchronous(false);
    config.setFailOnError(true);
    config.setRemoveOnSuccess(false);
    config.setEnableCodeExecution(Boolean.TRUE);

    // when
    final long now = System.currentTimeMillis();
    final ImportResult importResult = importService.importData(config);
    final long maxWait = System.currentTimeMillis() + (30 * 1000);
    do {
      Thread.sleep(500);
    } while (!importResult.isFinished() && System.currentTimeMillis() < maxWait);

    // then
    assertThat(System.currentTimeMillis() - now).isGreaterThanOrEqualTo(2000);
    assertThat(importResult.isFinished()).isTrue();
    assertThat(importResult.isError()).isFalse();
  }
  @Test
  public void testImportByConfig() {
    final ImpExResource mediaRes =
        new StreamBasedImpExResource(
            new ByteArrayInputStream("INSERT Language;isocode;active\n;test;true".getBytes()),
            CSVConstants.HYBRIS_ENCODING);

    final ImportConfig config = new ImportConfig();
    config.setScript(mediaRes);

    final ImportResult result = importService.importData(config);
    assertNotNull(result);
    assertTrue(result.isSuccessful());
    assertFalse(result.isError());
    assertFalse(result.hasUnresolvedLines());
    assertTrue(result.isFinished());
    assertNotNull("Language 'test' was not imported", getOrCreateLanguage("test"));
  }
  private void doArchiveContent(
      UserEntity importer, ContentKey contentKey, ImportResult importResult) {
    final ContentEntity content = contentDao.findByKey(contentKey);

    if (content == null) {
      return;
    }

    boolean contentArchived = contentStorer.archiveMainVersion(importer, content);
    if (contentArchived) {
      importResult.addArchived(content);

      UnassignContentCommand unassignContentCommand = new UnassignContentCommand();
      unassignContentCommand.setContentKey(content.getKey());
      unassignContentCommand.setUnassigner(importer.getKey());
      contentStorer.unassignContent(unassignContentCommand);
    } else {
      importResult.addAlreadyArchived(content);
    }
  }
  private void doDeleteContent(
      UserEntity importer, ContentKey contentKey, ImportResult importResult) {
    final ContentEntity content = contentDao.findByKey(contentKey);

    if (content == null) {
      // content must have been removed by another process during the import
    } else {
      contentStorer.deleteContent(importer, content);
      importResult.addDeleted(content);
    }
  }
Exemple #6
0
  public XMLDocument getReport(final ImportResult importResult) {
    final Element root = new Element("importreport");
    root.setAttribute(
        "elapsedTimeInSeconds", String.valueOf(importResult.getElapsedTimeInSeconds()));
    final Document doc = new Document(root);

    root.addContent(createReportElement("inserted", importResult.getInserted()));
    root.addContent(createReportElement("updated", importResult.getUpdated()));
    root.addContent(createReportElement("skipped", importResult.getSkipped()));
    root.addContent(createReportElement("archived", importResult.getArchived()));
    root.addContent(createReportElement("deleted", importResult.getDeleted()));
    root.addContent(createReportElement("remaining", importResult.getRemaining()));
    root.addContent(createReportElement("alreadyArchived", importResult.getAlreadyArchived()));

    return XMLDocumentFactory.create(doc);
  }
  private void internalImportByConfigWithError(final boolean failOnError) {
    try {
      TestUtils.disableFileAnalyzer();
      final ImpExResource mediaRes =
          new StreamBasedImpExResource(
              new ByteArrayInputStream(
                  "UPDATE Language;isocode[unique=true];active\n;test;true".getBytes()),
              CSVConstants.HYBRIS_ENCODING);

      final ImportConfig config = new ImportConfig();
      config.setScript(mediaRes);
      config.setFailOnError(failOnError);

      final ImportResult result = importService.importData(config);
      assertNotNull(result);
      assertFalse(result.isSuccessful());
      assertTrue(result.isError());
      assertTrue(result.hasUnresolvedLines());
      assertNotNull(result.getUnresolvedLines());
      assertTrue(result.isFinished());
    } finally {
      TestUtils.enableFileAnalyzer();
    }
  }