summaryrefslogtreecommitdiffstats
path: root/framework/src/onos/core/store/dist/src/test/java/org/onosproject/store/consistent/impl/UpdateResultTest.java
blob: ab53710bde86b0add48da9112eb4ad7ace02d768 (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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.store.consistent.impl;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;

import org.junit.Test;
import org.onosproject.store.service.MapEvent;
import org.onosproject.store.service.Versioned;

/**
 * Unit tests for UpdateResult.
 */
public class UpdateResultTest {

    @Test
    public void testGetters() {
        Versioned<String> oldValue = new Versioned<>("a", 1);
        Versioned<String> newValue = new Versioned<>("b", 2);
        UpdateResult<String, String> ur =
                new UpdateResult<>(true, "foo", "k", oldValue, newValue);

        assertTrue(ur.updated());
        assertEquals("foo", ur.mapName());
        assertEquals("k", ur.key());
        assertEquals(oldValue, ur.oldValue());
        assertEquals(newValue, ur.newValue());
    }

    @Test
    public void testToMapEvent() {
        Versioned<String> oldValue = new Versioned<>("a", 1);
        Versioned<String> newValue = new Versioned<>("b", 2);
        UpdateResult<String, String> ur1 =
                new UpdateResult<>(true, "foo", "k", oldValue, newValue);
        MapEvent<String, String> event1 = ur1.toMapEvent();
        assertEquals(MapEvent.Type.UPDATE, event1.type());
        assertEquals("k", event1.key());
        assertEquals(newValue, event1.value());

        UpdateResult<String, String> ur2 =
                new UpdateResult<>(true, "foo", "k", null, newValue);
        MapEvent<String, String> event2 = ur2.toMapEvent();
        assertEquals(MapEvent.Type.INSERT, event2.type());
        assertEquals("k", event2.key());
        assertEquals(newValue, event2.value());

        UpdateResult<String, String> ur3 =
                new UpdateResult<>(true, "foo", "k", oldValue, null);
        MapEvent<String, String> event3 = ur3.toMapEvent();
        assertEquals(MapEvent.Type.REMOVE, event3.type());
        assertEquals("k", event3.key());
        assertEquals(oldValue, event3.value());

        UpdateResult<String, String> ur4 =
                new UpdateResult<>(false, "foo", "k", oldValue, oldValue);
        assertNull(ur4.toMapEvent());
    }

    @Test
    public void testMap() {
        Versioned<String> oldValue = new Versioned<>("a", 1);
        Versioned<String> newValue = new Versioned<>("b", 2);
        UpdateResult<String, String> ur1 =
                new UpdateResult<>(true, "foo", "k", oldValue, newValue);
        UpdateResult<Integer, Integer> ur2 = ur1.map(s -> s.length(), s -> s.length());

        assertEquals(ur2.updated(), ur1.updated());
        assertEquals(ur1.mapName(), ur2.mapName());
        assertEquals(new Integer(1), ur2.key());
        assertEquals(oldValue.map(s -> s.length()), ur2.oldValue());
        assertEquals(newValue.map(s -> s.length()), ur2.newValue());

        UpdateResult<String, String> ur3 =
                new UpdateResult<>(true, "foo", "k", null, newValue);
        UpdateResult<Integer, Integer> ur4 = ur3.map(s -> s.length(), s -> s.length());

        assertEquals(ur3.updated(), ur4.updated());
        assertEquals(ur3.mapName(), ur4.mapName());
        assertEquals(new Integer(1), ur4.key());
        assertNull(ur4.oldValue());
        assertEquals(newValue.map(s -> s.length()), ur4.newValue());
    }
}