ContentAnalysis implements SynchronousDataSetAnalyzer { @Override public void analyze(String dataSetId) { if (StringUtils.isEmpty(dataSetId)) { throw new IllegalArgumentException("Data set id cannot be null or empty."); } DistributedLock datasetLock = repository.createDatasetMetadataLock(dataSetId); datasetLock.lock(); try { DataSetMetadata metadata = repository.get(dataSetId); if (metadata != null) { LOG.info("Indexing content of data set #{}...", metadata.getId()); updateHeaderAndFooter(metadata); updateLimit(metadata); metadata.getLifecycle().contentIndexed(true); DataSetMetadata savedDataSetMetadata = repository.get(dataSetId); if (savedDataSetMetadata != null) { repository.save(metadata); } LOG.info("Indexed content of data set #{}.", dataSetId); } else { LOG.info("Data set #{} no longer exists.", dataSetId); } } finally { datasetLock.unlock(); } }  @Override void analyze(String dataSetId); @Override int order();  }
@Test public void testNoDataSetFound() { String dataSetId = UUID.randomUUID().toString(); contentAnalysis.analyze(dataSetId); assertThat(dataSetMetadataRepository.get(dataSetId), nullValue()); }
@Test(expected = IllegalArgumentException.class) public void testEmptyDataSetId() { contentAnalysis.analyze(""); }
@Test public void testAnalysisNoHeaderParameter() { final DataSetMetadata metadata = metadataBuilder.metadata().id(UUID.randomUUID().toString()).build(); createCsvDataSet(metadata, "5_lines.csv"); contentAnalysis.analyze(metadata.getId()); assertThat(dataSetMetadataRepository.get(metadata.getId()), notNullValue()); final DataSetMetadata actual = dataSetMetadataRepository.get(metadata.getId()); assertThat(actual.getContent().getNbLinesInHeader(), is(1)); assertThat(actual.getContent().getNbLinesInFooter(), is(0)); assertThat(actual.getLifecycle().contentIndexed(), is(true)); }
@Test public void testAnalysisWithHeaderParameter() { final DataSetMetadata metadata = metadataBuilder .metadata() .id(UUID.randomUUID().toString()) .parameter(CSVFormatFamily.HEADER_NB_LINES_PARAMETER, "56") .build(); createCsvDataSet(metadata, "5_lines.csv"); contentAnalysis.analyze(metadata.getId()); assertThat(dataSetMetadataRepository.get(metadata.getId()), notNullValue()); final DataSetMetadata actual = dataSetMetadataRepository.get(metadata.getId()); assertThat(actual.getContent().getNbLinesInHeader(), is(56)); assertThat(actual.getContent().getNbLinesInFooter(), is(0)); assertThat(actual.getLifecycle().contentIndexed(), is(true)); }
@Test public void testAnalysisWithLimit() { final DataSetMetadata metadata = createMetadata(UUID.randomUUID().toString(), Arrays.asList("id", "first_name", "last_name", "email", "gender", "ip_address")); metadataBuilder.metadata().id("3548").build(); createCsvDataSet(metadata, "100_lines.csv"); final Long newLimit = 16L; final Long originalLimit = (Long) ReflectionTestUtils.getField(contentAnalysis, ContentAnalysis.class, "sizeLimit"); ReflectionTestUtils.setField(contentAnalysis, "sizeLimit", newLimit); contentAnalysis.analyze(metadata.getId()); ReflectionTestUtils.setField(contentAnalysis, "sizeLimit", originalLimit); final DataSetMetadata actual = dataSetMetadataRepository.get(metadata.getId()); final Optional<Long> limit = actual.getContent().getLimit(); assertTrue(limit.isPresent()); assertThat(limit.get(), is(newLimit)); }
@Test public void testAnalysisWithoutLimit() { final DataSetMetadata metadata = metadataBuilder.metadata().id(UUID.randomUUID().toString()).build(); createCsvDataSet(metadata, "5_lines.csv"); contentAnalysis.analyze(metadata.getId()); final DataSetMetadata actual = dataSetMetadataRepository.get(metadata.getId()); assertFalse(actual.getContent().getLimit().isPresent()); }