1 package org.onosproject.store.consistent.impl;
3 import static junit.framework.TestCase.assertEquals;
4 import static junit.framework.TestCase.assertNull;
5 import static junit.framework.TestCase.assertTrue;
8 import org.onosproject.store.service.MapEvent;
9 import org.onosproject.store.service.Versioned;
12 * Unit tests for UpdateResult.
14 public class UpdateResultTest {
17 public void testGetters() {
18 Versioned<String> oldValue = new Versioned<String>("a", 1);
19 Versioned<String> newValue = new Versioned<String>("b", 2);
20 UpdateResult<String, String> ur =
21 new UpdateResult<>(true, "foo", "k", oldValue, newValue);
23 assertTrue(ur.updated());
24 assertEquals("foo", ur.mapName());
25 assertEquals("k", ur.key());
26 assertEquals(oldValue, ur.oldValue());
27 assertEquals(newValue, ur.newValue());
31 public void testToMapEvent() {
32 Versioned<String> oldValue = new Versioned<String>("a", 1);
33 Versioned<String> newValue = new Versioned<String>("b", 2);
34 UpdateResult<String, String> ur1 =
35 new UpdateResult<>(true, "foo", "k", oldValue, newValue);
36 MapEvent<String, String> event1 = ur1.toMapEvent();
37 assertEquals(MapEvent.Type.UPDATE, event1.type());
38 assertEquals("k", event1.key());
39 assertEquals(newValue, event1.value());
41 UpdateResult<String, String> ur2 =
42 new UpdateResult<>(true, "foo", "k", null, newValue);
43 MapEvent<String, String> event2 = ur2.toMapEvent();
44 assertEquals(MapEvent.Type.INSERT, event2.type());
45 assertEquals("k", event2.key());
46 assertEquals(newValue, event2.value());
48 UpdateResult<String, String> ur3 =
49 new UpdateResult<>(true, "foo", "k", oldValue, null);
50 MapEvent<String, String> event3 = ur3.toMapEvent();
51 assertEquals(MapEvent.Type.REMOVE, event3.type());
52 assertEquals("k", event3.key());
53 assertEquals(oldValue, event3.value());
55 UpdateResult<String, String> ur4 =
56 new UpdateResult<>(false, "foo", "k", oldValue, oldValue);
57 assertNull(ur4.toMapEvent());
61 public void testMap() {
62 Versioned<String> oldValue = new Versioned<String>("a", 1);
63 Versioned<String> newValue = new Versioned<String>("b", 2);
64 UpdateResult<String, String> ur1 =
65 new UpdateResult<>(true, "foo", "k", oldValue, newValue);
66 UpdateResult<Integer, Integer> ur2 = ur1.map(s -> s.length(), s -> s.length());
68 assertEquals(ur2.updated(), ur1.updated());
69 assertEquals(ur1.mapName(), ur2.mapName());
70 assertEquals(new Integer(1), ur2.key());
71 assertEquals(oldValue.map(s -> s.length()), ur2.oldValue());
72 assertEquals(newValue.map(s -> s.length()), ur2.newValue());
74 UpdateResult<String, String> ur3 =
75 new UpdateResult<>(true, "foo", "k", null, newValue);
76 UpdateResult<Integer, Integer> ur4 = ur3.map(s -> s.length(), s -> s.length());
78 assertEquals(ur3.updated(), ur4.updated());
79 assertEquals(ur3.mapName(), ur4.mapName());
80 assertEquals(new Integer(1), ur4.key());
81 assertNull(ur4.oldValue());
82 assertEquals(newValue.map(s -> s.length()), ur4.newValue());