blob: a34cf98e6fd0ccd45ddefa16ab879ac14fd6a758 [file] [log] [blame]
Sbhat357c90e4b2017-07-24 16:49:47 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sbhat357c90e4b2017-07-24 16:49:47 -07003 *
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 org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.util.Tools;
Sbhat357c90e4b2017-07-24 16:49:47 -070022import org.onosproject.store.serializers.KryoNamespaces;
23import org.onosproject.store.service.AsyncConsistentMap;
Sbhat357c90e4b2017-07-24 16:49:47 -070024import org.onosproject.store.service.Serializer;
Sbhat357c90e4b2017-07-24 16:49:47 -070025import org.onosproject.utils.MeteringAgent;
26
Sbhat357c90e4b2017-07-24 16:49:47 -070027import static org.hamcrest.core.Is.is;
28import static org.junit.Assert.assertNull;
29import static org.junit.Assert.assertThat;
30
31
32public class DefaultAsyncAtomicValueTest {
Sbhat35ffe02342017-08-09 16:31:17 -070033 DefaultAsyncAtomicValue<String> defaultAsyncAtomicValue;
Sbhat357c90e4b2017-07-24 16:49:47 -070034
35 private AsyncConsistentMap<String, byte[]> asyncMap;
Sbhat357c90e4b2017-07-24 16:49:47 -070036
37 private Serializer serializer;
38 private MeteringAgent meteringAgent;
39
Sbhat35ffe02342017-08-09 16:31:17 -070040 private static final String MAPNAME = "map1";
41
Sbhat357c90e4b2017-07-24 16:49:47 -070042
43 private static final String NAME = "atomicValue";
44 private static final String NAME1 = "atomicValue1";
45 private static final String TEST = "foo";
46 private static final String TEST1 = "bar";
47 private static final int INTNAME = 20;
48 private static final long VERSION1 = 1;
49
50 private final byte[] value1 = Tools.getBytesUtf8(NAME);
51 private final byte[] value2 = Tools.getBytesUtf8(NAME1);
52 private final byte[] value3 = Tools.getBytesUtf8("tester");
53 private final byte[] defaultValue = Tools.getBytesUtf8("default");
54
55 @Before
56 public void setUp() throws Exception {
Sbhat35ffe02342017-08-09 16:31:17 -070057 asyncMap = new AsyncConsistentMapMock<>();
Sbhat357c90e4b2017-07-24 16:49:47 -070058 serializer = Serializer.using(KryoNamespaces.BASIC);
59 meteringAgent = new MeteringAgent(NAME, "*", false);
Sbhat35ffe02342017-08-09 16:31:17 -070060 defaultAsyncAtomicValue = new DefaultAsyncAtomicValue(MAPNAME, serializer,
Sbhat357c90e4b2017-07-24 16:49:47 -070061 asyncMap, meteringAgent);
Sbhat357c90e4b2017-07-24 16:49:47 -070062 }
63
64 @After
65 public void tearDown() throws Exception {
66 defaultAsyncAtomicValue.destroy();
67 }
68
69 @Test
70 public void testAsyncMapping() {
71 assertThat(asyncMap.size().join(), is(0));
72 asyncMap.put(TEST, value1);
73 asyncMap.put(TEST1, value2);
74 asyncMap.put("default", defaultValue);
75
76 assertThat(asyncMap.getOrDefault("noMatch", defaultValue).join().value(),
77 is(asyncMap.get("default").join().value()));
78
79 assertThat(asyncMap.size().join(), is(3));
80 assertThat(asyncMap.get(TEST).join().value(), is(value1));
81
82 assertThat(asyncMap.getOrDefault(TEST, Tools.getBytesUtf8("newTest")).join().value(),
83 is(asyncMap.get(TEST).join().value()));
84
85 assertThat(asyncMap.containsKey(TEST).join(), is(true));
86
87 asyncMap.put(TEST, value3);
88 assertThat(asyncMap.get(TEST).join().value(), is(value3));
89 asyncMap.putIfAbsent(TEST, value3);
90 assertThat(asyncMap.size().join(), is(3));
91
92 asyncMap.replace(TEST, value3, value1);
93 assertThat(asyncMap.get(TEST).join().value(), is(value1));
94
95 asyncMap.replace(TEST, VERSION1, value3);
96 assertThat(asyncMap.get(TEST).join().value(), is(value3));
97
98 asyncMap.replace(TEST, value3, defaultValue);
99 assertThat(asyncMap.get(TEST).join().value(), is(defaultValue));
100 asyncMap.replace(TEST, value1);
101 assertThat(asyncMap.get(TEST).join().value(), is(value1));
102
103 asyncMap.remove(TEST, value2);
104
105 assertThat(asyncMap.size().join(), is(3));
106 }
107
108 @Test
109 public void testAsync() {
110 asyncMap.put(TEST, value1);
111 asyncMap.put(TEST1, value2);
112
113 assertNull(defaultAsyncAtomicValue.get().join());
114 defaultAsyncAtomicValue = new DefaultAsyncAtomicValue(NAME, serializer,
115 asyncMap, meteringAgent);
116 assertThat(defaultAsyncAtomicValue.name(), is(NAME));
Sbhat35ffe02342017-08-09 16:31:17 -0700117 defaultAsyncAtomicValue.set(null).join();
Sbhat357c90e4b2017-07-24 16:49:47 -0700118 assertNull(defaultAsyncAtomicValue.get().join());
119
Sbhat35ffe02342017-08-09 16:31:17 -0700120 defaultAsyncAtomicValue.set(NAME).join();
121 assertThat(defaultAsyncAtomicValue.get().join(), is(NAME));
Sbhat357c90e4b2017-07-24 16:49:47 -0700122
Sbhat35ffe02342017-08-09 16:31:17 -0700123 defaultAsyncAtomicValue.set(NAME1).join();
124 assertThat(defaultAsyncAtomicValue.get().join(), is(NAME1));
125 defaultAsyncAtomicValue.compareAndSet(NAME1, NAME).join();
126 assertThat(defaultAsyncAtomicValue.get().join(), is(NAME));
Sbhat357c90e4b2017-07-24 16:49:47 -0700127
Sbhat357c90e4b2017-07-24 16:49:47 -0700128
Sbhat35ffe02342017-08-09 16:31:17 -0700129 defaultAsyncAtomicValue.getAndSet(null).join();
Sbhat357c90e4b2017-07-24 16:49:47 -0700130 assertNull(defaultAsyncAtomicValue.get().join());
131
Sbhat35ffe02342017-08-09 16:31:17 -0700132 defaultAsyncAtomicValue.set(NAME1).join();
133 assertThat(defaultAsyncAtomicValue.getAndSet(NAME).join(), is(NAME1));
134 assertThat(defaultAsyncAtomicValue.getAndSet("new").join(), is(NAME));
135
136 assertThat(defaultAsyncAtomicValue.get().join(), is("new"));
Sbhat357c90e4b2017-07-24 16:49:47 -0700137 }
138}