blob: 9c9e55d3f3e1a673c61a1ce18915a5a0a7373752 [file] [log] [blame]
Jordan Halterman4922a062017-07-31 15:55:36 -07001/*
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 */
16package org.onosproject.store.primitives.impl;
17
18import java.util.Arrays;
19
20import io.atomix.protocols.raft.proxy.RaftProxy;
21import io.atomix.protocols.raft.service.RaftService;
22import org.junit.Test;
23import org.onlab.util.Tools;
24import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap;
25import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapService;
26import org.onosproject.store.primitives.resources.impl.AtomixTestBase;
27import org.onosproject.store.service.AsyncConsistentMap;
28
29import static org.junit.Assert.assertFalse;
30import static org.junit.Assert.assertNotNull;
31import static org.junit.Assert.assertNull;
32import static org.junit.Assert.assertTrue;
33
34/**
35 * Unit tests for {@link AtomixConsistentMap}.
36 */
37public 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}