blob: c40853b7c2088a91788bab676c8daf4fbe69c9ac [file] [log] [blame]
Jordan Haltermanc955df72017-02-04 20:43:28 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jordan Haltermanc955df72017-02-04 20:43:28 -08003 *
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.resources.impl;
17
Jordan Halterman2bf177c2017-06-29 01:49:08 -070018import io.atomix.protocols.raft.proxy.RaftProxy;
19import io.atomix.protocols.raft.service.RaftService;
Jordan Haltermanc955df72017-02-04 20:43:28 -080020import org.junit.Test;
21
22import static org.junit.Assert.assertFalse;
23import static org.junit.Assert.assertTrue;
24
25/**
26 * Unit test for {@code AtomixCounterMap}.
27 */
Jordan Halterman2bf177c2017-06-29 01:49:08 -070028public class AtomixAtomicCounterMapTest extends AtomixTestBase<AtomixAtomicCounterMap> {
Jordan Haltermanc955df72017-02-04 20:43:28 -080029
Jordan Halterman2bf177c2017-06-29 01:49:08 -070030 @Override
31 protected RaftService createService() {
32 return new AtomixAtomicCounterMapService();
Jordan Haltermanc955df72017-02-04 20:43:28 -080033 }
34
35 @Override
Jordan Halterman2bf177c2017-06-29 01:49:08 -070036 protected AtomixAtomicCounterMap createPrimitive(RaftProxy proxy) {
37 return new AtomixAtomicCounterMap(proxy);
Jordan Haltermanc955df72017-02-04 20:43:28 -080038 }
39
40 /**
41 * Tests basic counter map operations.
42 */
43 @Test
44 public void testBasicCounterMapOperations() throws Throwable {
Jordan Halterman2bf177c2017-06-29 01:49:08 -070045 AtomixAtomicCounterMap map = newPrimitive("testBasicCounterMapOperationMap");
Jordan Haltermanc955df72017-02-04 20:43:28 -080046
47 map.isEmpty().thenAccept(isEmpty -> {
48 assertTrue(isEmpty);
49 }).join();
50
51 map.size().thenAccept(size -> {
52 assertTrue(size == 0);
53 }).join();
54
55 map.put("foo", 2).thenAccept(value -> {
56 assertTrue(value == 0);
57 }).join();
58
59 map.incrementAndGet("foo").thenAccept(value -> {
60 assertTrue(value == 3);
61 }).join();
62
63 map.getAndIncrement("foo").thenAccept(value -> {
64 assertTrue(value == 3);
65 }).join();
66
67 map.get("foo").thenAccept(value -> {
68 assertTrue(value == 4);
69 }).join();
70
71 map.getAndDecrement("foo").thenAccept(value -> {
72 assertTrue(value == 4);
73 }).join();
74
75 map.decrementAndGet("foo").thenAccept(value -> {
76 assertTrue(value == 2);
77 }).join();
78
79 map.size().thenAccept(size -> {
80 assertTrue(size == 1);
81 }).join();
82
83 map.isEmpty().thenAccept(isEmpty -> {
84 assertFalse(isEmpty);
85 }).join();
86
87 map.clear().join();
88
89 map.isEmpty().thenAccept(isEmpty -> {
90 assertTrue(isEmpty);
91 }).join();
92
93 map.size().thenAccept(size -> {
94 assertTrue(size == 0);
95 }).join();
96
97 map.get("foo").thenAccept(value -> {
98 assertTrue(value == 0);
99 }).join();
100
101 map.incrementAndGet("bar").thenAccept(value -> {
102 assertTrue(value == 1);
103 }).join();
104
105 map.addAndGet("bar", 2).thenAccept(value -> {
106 assertTrue(value == 3);
107 }).join();
108
109 map.getAndAdd("bar", 3).thenAccept(value -> {
110 assertTrue(value == 3);
111 }).join();
112
113 map.get("bar").thenAccept(value -> {
114 assertTrue(value == 6);
115 }).join();
116
117 map.putIfAbsent("bar", 1).thenAccept(value -> {
118 assertTrue(value == 6);
119 }).join();
120
121 map.replace("bar", 6, 1).thenAccept(succeeded -> {
122 assertTrue(succeeded);
123 }).join();
124
125 map.replace("bar", 6, 1).thenAccept(succeeded -> {
126 assertFalse(succeeded);
127 }).join();
128
129 map.size().thenAccept(size -> {
130 assertTrue(size == 1);
131 }).join();
132
133 map.remove("bar").thenAccept(value -> {
134 assertTrue(value == 1);
135 }).join();
136
137 map.size().thenAccept(size -> {
138 assertTrue(size == 0);
139 }).join();
140
141 map.put("baz", 3).thenAccept(value -> {
142 assertTrue(value == 0);
143 }).join();
144
145 map.remove("baz", 2).thenAccept(removed -> {
146 assertFalse(removed);
147 }).join();
148
149 map.put("baz", 2).thenAccept(value -> {
150 assertTrue(value == 3);
151 }).join();
152
153 map.remove("baz", 2).thenAccept(removed -> {
154 assertTrue(removed);
155 }).join();
156
157 map.isEmpty().thenAccept(isEmpty -> {
158 assertTrue(isEmpty);
159 }).join();
160
161 map.replace("baz", 0, 5).thenAccept(replaced -> {
162 assertTrue(replaced);
163 }).join();
164
165 map.get("baz").thenAccept(value -> {
166 assertTrue(value == 5);
167 }).join();
168 }
169}