Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Madan Jampani | f4c8850 | 2016-01-21 12:35:36 -0800 | [diff] [blame] | 16 | package org.onosproject.store.primitives.impl; |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 17 | |
| 18 | import static junit.framework.TestCase.assertEquals; |
| 19 | import static junit.framework.TestCase.assertNull; |
| 20 | import static junit.framework.TestCase.assertTrue; |
| 21 | |
| 22 | import org.junit.Test; |
| 23 | import org.onosproject.store.service.MapEvent; |
| 24 | import org.onosproject.store.service.Versioned; |
| 25 | |
| 26 | /** |
| 27 | * Unit tests for UpdateResult. |
| 28 | */ |
| 29 | public class UpdateResultTest { |
| 30 | |
| 31 | @Test |
| 32 | public void testGetters() { |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 33 | Versioned<String> oldValue = new Versioned<>("a", 1); |
| 34 | Versioned<String> newValue = new Versioned<>("b", 2); |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 35 | UpdateResult<String, String> ur = |
| 36 | new UpdateResult<>(true, "foo", "k", oldValue, newValue); |
| 37 | |
| 38 | assertTrue(ur.updated()); |
| 39 | assertEquals("foo", ur.mapName()); |
| 40 | assertEquals("k", ur.key()); |
| 41 | assertEquals(oldValue, ur.oldValue()); |
| 42 | assertEquals(newValue, ur.newValue()); |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | public void testToMapEvent() { |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 47 | Versioned<String> oldValue = new Versioned<>("a", 1); |
| 48 | Versioned<String> newValue = new Versioned<>("b", 2); |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 49 | UpdateResult<String, String> ur1 = |
| 50 | new UpdateResult<>(true, "foo", "k", oldValue, newValue); |
| 51 | MapEvent<String, String> event1 = ur1.toMapEvent(); |
| 52 | assertEquals(MapEvent.Type.UPDATE, event1.type()); |
| 53 | assertEquals("k", event1.key()); |
| 54 | assertEquals(newValue, event1.value()); |
| 55 | |
| 56 | UpdateResult<String, String> ur2 = |
| 57 | new UpdateResult<>(true, "foo", "k", null, newValue); |
| 58 | MapEvent<String, String> event2 = ur2.toMapEvent(); |
| 59 | assertEquals(MapEvent.Type.INSERT, event2.type()); |
| 60 | assertEquals("k", event2.key()); |
| 61 | assertEquals(newValue, event2.value()); |
| 62 | |
| 63 | UpdateResult<String, String> ur3 = |
| 64 | new UpdateResult<>(true, "foo", "k", oldValue, null); |
| 65 | MapEvent<String, String> event3 = ur3.toMapEvent(); |
| 66 | assertEquals(MapEvent.Type.REMOVE, event3.type()); |
| 67 | assertEquals("k", event3.key()); |
| 68 | assertEquals(oldValue, event3.value()); |
| 69 | |
| 70 | UpdateResult<String, String> ur4 = |
| 71 | new UpdateResult<>(false, "foo", "k", oldValue, oldValue); |
| 72 | assertNull(ur4.toMapEvent()); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testMap() { |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 77 | Versioned<String> oldValue = new Versioned<>("a", 1); |
| 78 | Versioned<String> newValue = new Versioned<>("b", 2); |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 79 | UpdateResult<String, String> ur1 = |
| 80 | new UpdateResult<>(true, "foo", "k", oldValue, newValue); |
| 81 | UpdateResult<Integer, Integer> ur2 = ur1.map(s -> s.length(), s -> s.length()); |
| 82 | |
| 83 | assertEquals(ur2.updated(), ur1.updated()); |
| 84 | assertEquals(ur1.mapName(), ur2.mapName()); |
| 85 | assertEquals(new Integer(1), ur2.key()); |
| 86 | assertEquals(oldValue.map(s -> s.length()), ur2.oldValue()); |
| 87 | assertEquals(newValue.map(s -> s.length()), ur2.newValue()); |
| 88 | |
| 89 | UpdateResult<String, String> ur3 = |
| 90 | new UpdateResult<>(true, "foo", "k", null, newValue); |
| 91 | UpdateResult<Integer, Integer> ur4 = ur3.map(s -> s.length(), s -> s.length()); |
| 92 | |
| 93 | assertEquals(ur3.updated(), ur4.updated()); |
| 94 | assertEquals(ur3.mapName(), ur4.mapName()); |
| 95 | assertEquals(new Integer(1), ur4.key()); |
| 96 | assertNull(ur4.oldValue()); |
| 97 | assertEquals(newValue.map(s -> s.length()), ur4.newValue()); |
| 98 | } |
| 99 | } |