aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/com/vaadin/tests/data/converter/ConverterTest.java
blob: 81304885c37710f40d018b441bf1ca11ea21ab9c (plain)
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
package com.vaadin.tests.data.converter;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.vaadin.data.Converter;
import com.vaadin.data.Result;
import com.vaadin.data.ValueContext;
import com.vaadin.server.SerializableFunction;

public class ConverterTest {

    SerializableFunction<String, Result<String>> toModel = presentation -> {
        if (presentation.startsWith("presentation-")) {
            return Result.ok(presentation.substring("presentation-".length()));
        }
        return Result.error("invalid prefix: " + presentation);
    };

    SerializableFunction<String, String> toPresentation = model -> "presentation-"
            + model;

    Converter<String, String> converter = Converter.from(toModel,
            toPresentation);

    @Test
    public void basicConversion() {
        assertEquals("presentation-123",
                converter.convertToPresentation("123", new ValueContext()));
        assertEquals("123",
                converter.convertToModel("presentation-123", new ValueContext())
                        .getOrThrow(msg -> new AssertionError(msg)));
    }
}