Esempio n. 1
0
 @Test
 public void testColumnAtEnd() throws Exception {
   DataSet dataSet = from(this.getClass().getResourceAsStream("test4.json"));
   // There are 4 columns, but Jackson doesn't take them into account if at end of content. This is
   // not "expected"
   // but known. This test ensure the known behavior remains the same.
   assertThat(dataSet.getMetadata(), nullValue());
 }
Esempio n. 2
0
 @Test
 public void testRoundTrip() throws Exception {
   DataSet dataSet = from(DataSetJSONTest.class.getResourceAsStream("test3.json"));
   final DataSetMetadata metadata = dataSet.getMetadata();
   metadata.getContent().addParameter(CSVFormatGuess.SEPARATOR_PARAMETER, ",");
   metadata.getContent().setFormatGuessId(new CSVFormatGuess().getBeanId());
   assertNotNull(metadata);
   StringWriter writer = new StringWriter();
   to(dataSet, writer);
   assertThat(
       writer.toString(), sameJSONAsFile(DataSetJSONTest.class.getResourceAsStream("test3.json")));
 }
Esempio n. 3
0
  @Test
  public void should_iterate_row_with_metadata() throws IOException {
    // given
    String[] columnNames =
        new String[] {
          "id",
          "firstname",
          "lastname",
          "state",
          "registration",
          "city",
          "birth",
          "nbCommands",
          "avgAmount"
        };

    final InputStream input = this.getClass().getResourceAsStream("dataSetRowMetadata.json");
    final ObjectMapper mapper = builder.build();
    try (JsonParser parser = mapper.getFactory().createParser(input)) {
      final DataSet dataSet = mapper.readerFor(DataSet.class).readValue(parser);
      final Iterator<DataSetRow> iterator = dataSet.getRecords().iterator();

      List<ColumnMetadata> actualColumns = new ArrayList<>();
      int recordCount = 0;
      while (iterator.hasNext()) {
        final DataSetRow next = iterator.next();
        actualColumns = next.getRowMetadata().getColumns();
        assertThat(actualColumns, not(empty()));
        recordCount++;
      }

      // then
      assertEquals(10, recordCount);
      for (int i = 0; i < actualColumns.size(); i++) {
        final ColumnMetadata column = actualColumns.get(i);
        assertEquals(columnNames[i], column.getId());
      }
    } catch (Exception e) {
      throw new TDPException(CommonErrorCodes.UNABLE_TO_PARSE_JSON, e);
    }
  }
Esempio n. 4
0
  @Test
  public void testWrite1() throws Exception {
    final ColumnMetadata.Builder columnBuilder =
        ColumnMetadata.Builder //
            .column() //
            .id(5) //
            .name("column1") //
            .type(Type.STRING) //
            .empty(0) //
            .invalid(10) //
            .valid(50);

    DataSetMetadata metadata =
        metadataBuilder
            .metadata()
            .id("1234")
            .name("name")
            .author("author")
            .created(0)
            .row(columnBuilder)
            .build();

    final DataSetContent content = metadata.getContent();
    content.addParameter(CSVFormatGuess.SEPARATOR_PARAMETER, ",");
    content.setFormatGuessId(new CSVFormatGuess().getBeanId());
    content.setMediaType("text/csv");
    metadata.getLifecycle().qualityAnalyzed(true);
    metadata.getLifecycle().schemaAnalyzed(true);
    HttpLocation location = new HttpLocation();
    location.setUrl("http://estcequecestbientotleweekend.fr");
    metadata.setLocation(location);
    StringWriter writer = new StringWriter();
    DataSet dataSet = new DataSet();
    dataSet.setMetadata(metadata);
    to(dataSet, writer);
    assertThat(
        writer.toString(), sameJSONAsFile(DataSetJSONTest.class.getResourceAsStream("test2.json")));
  }
Esempio n. 5
0
  @Test
  public void testRead1() throws Exception {

    DataSet dataSet = from(this.getClass().getResourceAsStream("test1.json"));
    assertNotNull(dataSet);

    final DataSetMetadata metadata = dataSet.getMetadata();
    assertEquals("410d2196-8f90-478f-a817-7e8b6694ac91", metadata.getId());
    assertEquals("test", metadata.getName());
    assertEquals("anonymousUser", metadata.getAuthor());
    assertEquals(2, metadata.getContent().getNbRecords());
    assertEquals(1, metadata.getContent().getNbLinesInHeader());
    assertEquals(0, metadata.getContent().getNbLinesInFooter());

    final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

    Date expectedDate = dateFormat.parse("02-17-2015 09:02");
    assertEquals(expectedDate, new Date(metadata.getCreationDate()));

    List<ColumnMetadata> columns = dataSet.getMetadata().getRowMetadata().getColumns();
    assertEquals(6, columns.size());

    ColumnMetadata firstColumn = columns.get(0);
    assertEquals("0001", firstColumn.getId());
    assertEquals("id", firstColumn.getName());
    assertEquals("integer", firstColumn.getType());
    assertEquals(20, firstColumn.getQuality().getEmpty());
    assertEquals(26, firstColumn.getQuality().getInvalid());
    assertEquals(54, firstColumn.getQuality().getValid());

    ColumnMetadata lastColumn = columns.get(5);
    assertEquals("0007", lastColumn.getId());
    assertEquals("string", lastColumn.getType());
    assertEquals(8, lastColumn.getQuality().getEmpty());
    assertEquals(25, lastColumn.getQuality().getInvalid());
    assertEquals(67, lastColumn.getQuality().getValid());
  }