Search in sources :

Example 41 with Document

use of com.yahoo.document.Document in project vespa by vespa-engine.

the class JsonWriterTestCase method oldMapTest.

@Test
public void oldMapTest() throws IOException {
    String fields = "{ \"actualmap\": [" + " { \"key\": \"nalle\", \"value\": \"kalle\"}," + " { \"key\": \"tralle\", \"value\": \"skalle\"} ]}";
    String docId = "id:unittest:testmap::whee";
    Document doc = readDocumentFromJson(docId, fields);
    // we have to do everything by hand to check, as maps are unordered, but
    // are serialized as an ordered structure
    ObjectMapper m = new ObjectMapper();
    Map<?, ?> generated = m.readValue(JsonWriter.toByteArray(doc), Map.class);
    assertEquals(docId, generated.get("id"));
    // and from here on down there will be lots of unchecked casting and
    // other fun. This is OK here, because if the casts fail, the should and
    // will fail anyway
    List<?> inputMap = (List<?>) m.readValue(Utf8.toBytes(fields), Map.class).get("actualmap");
    Map<?, ?> generatedMap = (Map<?, ?>) ((Map<?, ?>) generated.get("fields")).get("actualmap");
    assertEquals(populateMap(inputMap), generatedMap);
}
Also used : List(java.util.List) Document(com.yahoo.document.Document) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 42 with Document

use of com.yahoo.document.Document in project vespa by vespa-engine.

the class JsonWriterTestCase method hideEmptyStringsTest.

@Test
public void hideEmptyStringsTest() throws IOException {
    String fields = "{" + " \"something\": \"\"," + " \"nalle\": \"bamse\"" + "}";
    String filteredFields = "{" + " \"nalle\": \"bamse\"" + "}";
    Document doc = readDocumentFromJson("id:unittest:smoke::whee", fields);
    assertEqualJson(asDocument("id:unittest:smoke::whee", filteredFields), JsonWriter.toByteArray(doc));
}
Also used : Document(com.yahoo.document.Document) Test(org.junit.Test)

Example 43 with Document

use of com.yahoo.document.Document in project vespa by vespa-engine.

the class PredicateFieldValueSerializationTestCase method assertDeserialize.

private static void assertDeserialize(String fileName, Predicate expected) throws IOException {
    Document document = docFactory.createDocument();
    document.setFieldValue(PREDICATE_FIELD, new PredicateFieldValue(expected));
    SerializationTestUtils.assertSerializationMatchesCpp(PREDICATE_FILES, fileName, document, docFactory);
}
Also used : Document(com.yahoo.document.Document) SerializationTestUtils.serializeDocument(com.yahoo.document.serialization.SerializationTestUtils.serializeDocument) SerializationTestUtils.deserializeDocument(com.yahoo.document.serialization.SerializationTestUtils.deserializeDocument) PredicateFieldValue(com.yahoo.document.datatypes.PredicateFieldValue)

Example 44 with Document

use of com.yahoo.document.Document in project vespa by vespa-engine.

the class Bug4475379TestCase method testClone.

@Test
public void testClone() {
    DocumentTypeManager manager = new DocumentTypeManager();
    DocumentTypeManagerConfigurer.configure(manager, "file:src/test/java/com/yahoo/document/annotation/documentmanager.bug4475379.cfg");
    DocumentType type = manager.getDocumentType("blog");
    Document doc = new Document(type, "doc:this:is:a:test");
    doc.setFieldValue("body", new StringFieldValue(""));
    annotate(manager, doc);
    Document anotherDoc = doc.clone();
    assertThat(doc, equalTo(anotherDoc));
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) DocumentTypeManager(com.yahoo.document.DocumentTypeManager) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) Test(org.junit.Test)

Example 45 with Document

use of com.yahoo.document.Document in project vespa by vespa-engine.

the class XMLNumericFieldErrorMsgTestCase method requireDescriptiveErrorMsgForIntegers.

@Test
public void requireDescriptiveErrorMsgForIntegers() throws Exception {
    DocumentTypeManager dtm = setupTypes();
    try {
        VespaXMLDocumentReader documentReader = new VespaXMLDocumentReader(new ByteArrayInputStream(("<document id=\"doc:foo:bar\" type=\"doctype\">" + "  <intfield></intfield>" + "</document>").getBytes(StandardCharsets.UTF_8)), dtm);
        new Document(documentReader);
        fail("Sorry mac");
    } catch (DeserializationException e) {
        assertThat(e.getMessage(), e.getMessage().contains("Field 'intfield': Invalid integer \"\""), is(true));
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentTypeManager(com.yahoo.document.DocumentTypeManager) Document(com.yahoo.document.Document) DeserializationException(com.yahoo.document.serialization.DeserializationException) Test(org.junit.Test)

Aggregations

Document (com.yahoo.document.Document)109 Test (org.junit.Test)63 DocumentType (com.yahoo.document.DocumentType)41 DocumentPut (com.yahoo.document.DocumentPut)40 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)33 ByteArrayInputStream (java.io.ByteArrayInputStream)23 InputStream (java.io.InputStream)18 DocumentId (com.yahoo.document.DocumentId)16 DocumentParseInfo (com.yahoo.document.json.readers.DocumentParseInfo)16 VespaJsonDocumentReader (com.yahoo.document.json.readers.VespaJsonDocumentReader)16 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)13 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)13 HashMap (java.util.HashMap)13 DocumentTypeManager (com.yahoo.document.DocumentTypeManager)12 FieldValue (com.yahoo.document.datatypes.FieldValue)12 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)12 DocumentOperation (com.yahoo.document.DocumentOperation)10 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)10 DocumentUpdate (com.yahoo.document.DocumentUpdate)8 Array (com.yahoo.document.datatypes.Array)8