aboutsummaryrefslogtreecommitdiffstats
path: root/config-model-api/src/test/java/com/yahoo/config/model/api/QuotaTest.java
blob: 23502fb9acee816e1bd04161463607cd8f428739 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.model.api;

import com.yahoo.slime.SlimeUtils;
import org.junit.Test;


import java.math.BigDecimal;

import static org.junit.Assert.assertEquals;

/**
 * @author ogronnesby
 */
public class QuotaTest {

    @Test
    public void test_serialization_with_integers() {
        var json = "{\"budget\": 123}";
        var slime = SlimeUtils.jsonToSlime(json);
        var quota = Quota.fromSlime(slime.get());
        assertEquals((Integer) 123, quota.budget().get());
        assertEquals(123, quota.budgetAsDecimal().get().intValueExact());
    }

    @Test
    public void test_serialization_with_floats() {
        var json = "{\"budget\": 123.4}";
        var slime = SlimeUtils.jsonToSlime(json);
        var quota = Quota.fromSlime(slime.get());
        assertEquals((Integer) 123, quota.budget().get());
        assertEquals(123.4, quota.budgetAsDecimal().get().doubleValue(), 0.01);
    }

    @Test
    public void test_serialization_with_string() {
        var json = "{\"budget\": \"123.4\"}";
        var slime = SlimeUtils.jsonToSlime(json);
        var quota = Quota.fromSlime(slime.get());
        assertEquals((Integer) 123, quota.budget().get());
        assertEquals(new BigDecimal("123.4"), quota.budgetAsDecimal().get());
    }

    @Test
    public void test_serde() {
        var quota = Quota.unlimited().withBudget(BigDecimal.valueOf(23.5)).withClusterSize(11);
        var serialized = quota.toSlime();
        var deserialized = Quota.fromSlime(serialized.get());
        assertEquals(quota, deserialized);
    }
}