コード例 #1
0
  @Test
  public void testMetadataToDelete() throws Exception {
    Record record = repository.newRecord();
    record.setMetadata(
        new QName("ns", "field1"),
        new MetadataBuilder()
            .value("mfield1", "value1")
            .delete("mfield2")
            .delete("mfield3")
            .build());

    record.setMetadata(new QName("ns", "field2"), new MetadataBuilder().delete("mfield4").build());

    WriteOptions options = new WriteOptions();
    options.setUseNamespacePrefixes(false);
    ObjectNode recordNode = RecordWriter.INSTANCE.toJson(record, options, repositoryManager);

    // Go through ser/deser
    String recordJson = JsonFormat.serializeAsString(recordNode);
    recordNode = (ObjectNode) JsonFormat.deserializeNonStd(recordJson);

    assertNotNull(recordNode.get("metadataToDelete"));
    assertEquals(2, recordNode.get("metadataToDelete").size());

    assertEquals(2, recordNode.get("metadataToDelete").get("{ns}field1").size());
    assertEquals(1, recordNode.get("metadataToDelete").get("{ns}field2").size());

    // Now parse json again to API objects
    record = RecordReader.INSTANCE.fromJson(recordNode, repositoryManager);
    assertEquals(2, record.getMetadataMap().size());

    Metadata metadata = record.getMetadata(new QName("ns", "field1"));
    assertTrue(metadata.getFieldsToDelete().contains("mfield2"));
    assertTrue(metadata.getFieldsToDelete().contains("mfield3"));
    assertEquals("value1", metadata.get("mfield1"));

    metadata = record.getMetadata(new QName("ns", "field2"));
    assertTrue(metadata.getFieldsToDelete().contains("mfield4"));
    assertEquals(1, metadata.getFieldsToDelete().size());
  }
コード例 #2
0
  @Test
  public void testMetadata() throws Exception {
    Record record = repository.newRecord();
    record.setMetadata(
        new QName("ns", "field1"),
        new MetadataBuilder()
            .value("stringfield", "string")
            .value("intfield", 55)
            .value("longfield", 999999999999L)
            .value("booleanfield", Boolean.TRUE)
            .value("floatfield", 33.33f)
            .value("doublefield", 66.66d)
            .value("binaryfield", new ByteArray("foo".getBytes()))
            .build());

    record.setMetadata(
        new QName("ns", "field2"),
        new MetadataBuilder().value("stringfield", "another_string").build());

    ObjectNode recordNode = RecordWriter.INSTANCE.toJson(record, null, repositoryManager);

    // go through ser/deser
    String recordJson = JsonFormat.serializeAsString(recordNode);
    recordNode = (ObjectNode) JsonFormat.deserializeNonStd(recordJson);

    // Do some structural validations of the json
    ObjectNode metadataNode = (ObjectNode) recordNode.get("metadata");
    assertNotNull(metadataNode);
    assertNull(recordNode.get("metadataToDelete"));

    String prefix = recordNode.get("namespaces").get("ns").getTextValue();

    assertEquals(2, metadataNode.size());

    assertTrue(metadataNode.get(prefix + "$field1").get("stringfield").isTextual());
    assertEquals("string", metadataNode.get(prefix + "$field1").get("stringfield").getTextValue());

    assertTrue(metadataNode.get(prefix + "$field1").get("intfield").isInt());
    assertEquals(55, metadataNode.get(prefix + "$field1").get("intfield").getIntValue());

    assertTrue(metadataNode.get(prefix + "$field1").get("longfield").isLong());
    assertEquals(
        999999999999L, metadataNode.get(prefix + "$field1").get("longfield").getLongValue());

    assertTrue(metadataNode.get(prefix + "$field1").get("booleanfield").isBoolean());
    assertEquals(
        Boolean.TRUE, metadataNode.get(prefix + "$field1").get("booleanfield").getBooleanValue());

    // in JSON, no distinction between floats & doubles
    assertTrue(metadataNode.get(prefix + "$field1").get("floatfield").isFloatingPointNumber());
    assertEquals(
        33.33d, metadataNode.get(prefix + "$field1").get("floatfield").getDoubleValue(), 0.001);

    assertTrue(metadataNode.get(prefix + "$field1").get("doublefield").isFloatingPointNumber());
    assertEquals(
        33.33d, metadataNode.get(prefix + "$field1").get("floatfield").getDoubleValue(), 0.001);

    assertTrue(metadataNode.get(prefix + "$field1").get("binaryfield").isObject());
    ObjectNode binaryNode = (ObjectNode) metadataNode.get(prefix + "$field1").get("binaryfield");
    assertEquals("binary", binaryNode.get("type").getTextValue());
    assertArrayEquals("foo".getBytes(), binaryNode.get("value").getBinaryValue());

    assertTrue(metadataNode.get(prefix + "$field2").get("stringfield").isTextual());
    assertEquals(
        "another_string", metadataNode.get(prefix + "$field2").get("stringfield").getTextValue());

    // Now parse json again to API objects
    record = RecordReader.INSTANCE.fromJson(recordNode, repositoryManager);
    assertEquals(2, record.getMetadataMap().size());

    Metadata metadata = record.getMetadata(new QName("ns", "field1"));
    assertEquals("string", metadata.get("stringfield"));
    assertEquals(55, metadata.getInt("intfield", null).intValue());
    assertEquals(999999999999L, metadata.getLong("longfield", null).longValue());
    assertEquals(Boolean.TRUE, metadata.getBoolean("booleanfield", null).booleanValue());
    assertEquals(33.33f, metadata.getFloat("floatfield", null).floatValue(), 0.001);
    assertEquals(66.66d, metadata.getDouble("doublefield", null).doubleValue(), 0.001);
    assertEquals(new ByteArray("foo".getBytes()), metadata.getBytes("binaryfield"));

    metadata = record.getMetadata(new QName("ns", "field2"));
    assertEquals(1, metadata.getMap().size());
    assertEquals(0, metadata.getFieldsToDelete().size());
  }