Jordan Halterman | 4922a06 | 2017-07-31 15:55:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.util.Arrays; |
| 19 | |
| 20 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 21 | import io.atomix.protocols.raft.service.RaftService; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.util.Tools; |
| 24 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap; |
| 25 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapService; |
| 26 | import org.onosproject.store.primitives.resources.impl.AtomixTestBase; |
| 27 | import org.onosproject.store.service.AsyncConsistentMap; |
| 28 | |
| 29 | import static org.junit.Assert.assertFalse; |
| 30 | import static org.junit.Assert.assertNotNull; |
| 31 | import static org.junit.Assert.assertNull; |
| 32 | import static org.junit.Assert.assertTrue; |
| 33 | |
| 34 | /** |
| 35 | * Unit tests for {@link AtomixConsistentMap}. |
| 36 | */ |
| 37 | public class NotNullConsistentMapTest extends AtomixTestBase<AtomixConsistentMap> { |
| 38 | |
| 39 | @Override |
| 40 | protected RaftService createService() { |
| 41 | return new AtomixConsistentMapService(); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | protected AtomixConsistentMap createPrimitive(RaftProxy proxy) { |
| 46 | return new AtomixConsistentMap(proxy); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Tests not null values. |
| 51 | */ |
| 52 | @Test |
| 53 | public void testNotNullValues() throws Throwable { |
| 54 | final byte[] rawFooValue = Tools.getBytesUtf8("Hello foo!"); |
| 55 | final byte[] rawBarValue = Tools.getBytesUtf8("Hello bar!"); |
| 56 | |
| 57 | AsyncConsistentMap<String, byte[]> map = |
| 58 | DistributedPrimitives.newNotNullMap(newPrimitive("testNotNullValues")); |
| 59 | |
| 60 | map.get("foo") |
| 61 | .thenAccept(v -> assertNull(v)).join(); |
| 62 | map.put("foo", null) |
| 63 | .thenAccept(v -> assertNull(v)).join(); |
| 64 | map.put("foo", rawFooValue).thenAccept(v -> assertNull(v)).join(); |
| 65 | map.get("foo").thenAccept(v -> { |
| 66 | assertNotNull(v); |
| 67 | assertTrue(Arrays.equals(v.value(), rawFooValue)); |
| 68 | }).join(); |
| 69 | map.put("foo", null).thenAccept(v -> { |
| 70 | assertNotNull(v); |
| 71 | assertTrue(Arrays.equals(v.value(), rawFooValue)); |
| 72 | }).join(); |
| 73 | map.get("foo").thenAccept(v -> assertNull(v)).join(); |
| 74 | map.replace("foo", rawFooValue, null) |
| 75 | .thenAccept(replaced -> assertFalse(replaced)).join(); |
| 76 | map.replace("foo", null, rawBarValue) |
| 77 | .thenAccept(replaced -> assertTrue(replaced)).join(); |
| 78 | map.get("foo").thenAccept(v -> { |
| 79 | assertNotNull(v); |
| 80 | assertTrue(Arrays.equals(v.value(), rawBarValue)); |
| 81 | }).join(); |
| 82 | map.replace("foo", rawBarValue, null) |
| 83 | .thenAccept(replaced -> assertTrue(replaced)).join(); |
| 84 | map.get("foo").thenAccept(v -> assertNull(v)).join(); |
| 85 | } |
| 86 | } |