aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo/document/ReferenceDataTypeTestCase.java
blob: c5f28c84701584843fe3b2411731e20f172784c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document;

import com.yahoo.document.datatypes.ReferenceFieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * @author vekterli
 */
public class ReferenceDataTypeTestCase {

    @Test
    public void parameters_are_propagated_to_base_data_type() {
        DocumentType docType = new DocumentType("bjarne");
        ReferenceDataType refType = new ReferenceDataType(docType, 1234);
        assertEquals("Reference<bjarne>", refType.getName());
        assertEquals(1234, refType.getId());
        assertEquals(docType, refType.getTargetType());
    }

    @Test
    public void empty_reference_field_value_instance_can_be_created_from_type() {
        ReferenceDataType refType = new ReferenceDataType(new DocumentType("foo"), 123);
        ReferenceFieldValue fv = refType.createFieldValue();
        assertNotNull(fv);
        assertEquals(refType, fv.getDataType());
    }

    @Test
    public void reference_data_type_has_reference_field_value_class() {
        ReferenceDataType refType = new ReferenceDataType(new DocumentType("foo"), 123);
        assertEquals(ReferenceFieldValue.class, refType.getValueClass());
    }

    private static class MultiTypeFixture {
        final DocumentType docType                        = new DocumentType("bar");
        final ReferenceDataType  refType                  = new ReferenceDataType(docType, 123);
        final ReferenceDataType  refTypeClone             = new ReferenceDataType(docType, 123);
        final ReferenceDataType  typeWithDifferentId      = new ReferenceDataType(docType, 456);
        final ReferenceDataType  typeWithDifferentDocType = new ReferenceDataType(new DocumentType("stuff"), 123);
    }

    @Test
    public void equals_checks_document_type_and_type_id() {
        final MultiTypeFixture fixture = new MultiTypeFixture();

        // Note: the default DataType.equals method actually satisfies this, since we already
        // give it a type-parameterized name and id
        assertFalse(fixture.refType.equals(null));
        assertFalse(fixture.refType.equals(DataType.STRING));
        assertFalse(fixture.refType.equals(fixture.typeWithDifferentId));
        assertFalse(fixture.refType.equals(fixture.typeWithDifferentDocType));
        assertTrue(fixture.refType.equals(fixture.refType));
        assertTrue(fixture.refType.equals(fixture.refTypeClone));
    }

    @Test
    public void type_value_compatibility_checks_target_type() {
        final MultiTypeFixture fixture = new MultiTypeFixture();

        assertFalse(fixture.refType.isValueCompatible(null));
        assertFalse(fixture.refType.isValueCompatible(new StringFieldValue("baz")));
        assertFalse(fixture.refType.isValueCompatible(fixture.typeWithDifferentId.createFieldValue()));
        assertFalse(fixture.refType.isValueCompatible(fixture.typeWithDifferentDocType.createFieldValue()));
        assertTrue(fixture.refType.isValueCompatible(fixture.refType.createFieldValue()));
        assertTrue(fixture.refType.isValueCompatible(fixture.refTypeClone.createFieldValue()));
    }

}