@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 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 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 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();
    }
  }