Committed by
Gerrit Code Review
Unit tests for the flow objective store
Change-Id: I6223d973bba5e680396c338a3d8d0663a028d02b
Showing
4 changed files
with
166 additions
and
2 deletions
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 | + */ | ||
16 | +package org.onosproject.store.service; | ||
17 | + | ||
18 | +import java.util.concurrent.ScheduledExecutorService; | ||
19 | +import java.util.concurrent.atomic.AtomicLong; | ||
20 | + | ||
21 | +/** | ||
22 | + * Test implementation of atomic counter. | ||
23 | + */ | ||
24 | +public final class TestAtomicCounter implements AtomicCounter { | ||
25 | + final AtomicLong value; | ||
26 | + | ||
27 | + private TestAtomicCounter() { | ||
28 | + value = new AtomicLong(); | ||
29 | + } | ||
30 | + | ||
31 | + @Override | ||
32 | + public long incrementAndGet() { | ||
33 | + return value.incrementAndGet(); | ||
34 | + } | ||
35 | + | ||
36 | + @Override | ||
37 | + public long getAndIncrement() { | ||
38 | + return value.getAndIncrement(); | ||
39 | + } | ||
40 | + | ||
41 | + @Override | ||
42 | + public long getAndAdd(long delta) { | ||
43 | + return value.getAndAdd(delta); | ||
44 | + } | ||
45 | + | ||
46 | + @Override | ||
47 | + public long addAndGet(long delta) { | ||
48 | + return value.addAndGet(delta); | ||
49 | + } | ||
50 | + | ||
51 | + @Override | ||
52 | + public long get() { | ||
53 | + return value.get(); | ||
54 | + } | ||
55 | + | ||
56 | + public static AtomicCounterBuilder builder() { | ||
57 | + return new Builder(); | ||
58 | + } | ||
59 | + | ||
60 | + public static class Builder implements AtomicCounterBuilder { | ||
61 | + @Override | ||
62 | + public AtomicCounterBuilder withName(String name) { | ||
63 | + return this; | ||
64 | + } | ||
65 | + | ||
66 | + @Override | ||
67 | + public AtomicCounterBuilder withPartitionsDisabled() { | ||
68 | + return this; | ||
69 | + } | ||
70 | + | ||
71 | + @Override | ||
72 | + public AtomicCounterBuilder withRetryOnFailure() { | ||
73 | + return this; | ||
74 | + } | ||
75 | + | ||
76 | + @Override | ||
77 | + public AtomicCounterBuilder withMeteringDisabled() { | ||
78 | + return this; | ||
79 | + } | ||
80 | + | ||
81 | + @Override | ||
82 | + public AtomicCounterBuilder withRetryExecutor(ScheduledExecutorService executor) { | ||
83 | + return this; | ||
84 | + } | ||
85 | + | ||
86 | + @Override | ||
87 | + public AsyncAtomicCounter buildAsyncCounter() { | ||
88 | + throw new UnsupportedOperationException("Async Counter is not supported"); | ||
89 | + } | ||
90 | + | ||
91 | + @Override | ||
92 | + public AtomicCounter build() { | ||
93 | + return new TestAtomicCounter(); | ||
94 | + } | ||
95 | + } | ||
96 | +} |
... | @@ -82,7 +82,12 @@ public final class TestConsistentMap<K, V> extends ConsistentMapAdapter<K, V> { | ... | @@ -82,7 +82,12 @@ public final class TestConsistentMap<K, V> extends ConsistentMapAdapter<K, V> { |
82 | 82 | ||
83 | @Override | 83 | @Override |
84 | public Versioned<V> get(K key) { | 84 | public Versioned<V> get(K key) { |
85 | - return version(map.get(key)); | 85 | + V value = map.get(key); |
86 | + if (value != null) { | ||
87 | + return version(value); | ||
88 | + } else { | ||
89 | + return null; | ||
90 | + } | ||
86 | } | 91 | } |
87 | 92 | ||
88 | @Override | 93 | @Override | ... | ... |
... | @@ -40,7 +40,7 @@ public class TestStorageService extends StorageServiceAdapter { | ... | @@ -40,7 +40,7 @@ public class TestStorageService extends StorageServiceAdapter { |
40 | 40 | ||
41 | @Override | 41 | @Override |
42 | public AtomicCounterBuilder atomicCounterBuilder() { | 42 | public AtomicCounterBuilder atomicCounterBuilder() { |
43 | - throw new UnsupportedOperationException("atomicCounterBuilder"); | 43 | + return TestAtomicCounter.builder(); |
44 | } | 44 | } |
45 | 45 | ||
46 | @Override | 46 | @Override | ... | ... |
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 | + */ | ||
16 | +package org.onosproject.store.flowobjective.impl; | ||
17 | + | ||
18 | +import org.junit.After; | ||
19 | +import org.junit.Before; | ||
20 | +import org.junit.Test; | ||
21 | +import org.onosproject.net.behaviour.DefaultNextGroup; | ||
22 | +import org.onosproject.net.behaviour.NextGroup; | ||
23 | +import org.onosproject.net.flowobjective.FlowObjectiveStore; | ||
24 | +import org.onosproject.store.service.TestStorageService; | ||
25 | + | ||
26 | +import com.google.common.base.Charsets; | ||
27 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
28 | +import static org.hamcrest.Matchers.*; | ||
29 | + | ||
30 | +/** | ||
31 | + * Unit tests for distributed flow objective store. | ||
32 | + */ | ||
33 | +public class DistributedFlowObjectiveStoreTest { | ||
34 | + DistributedFlowObjectiveStore storeImpl; | ||
35 | + FlowObjectiveStore store; | ||
36 | + | ||
37 | + @Before | ||
38 | + public void setUp() { | ||
39 | + storeImpl = new DistributedFlowObjectiveStore(); | ||
40 | + storeImpl.storageService = new TestStorageService(); | ||
41 | + storeImpl.activate(); | ||
42 | + store = storeImpl; | ||
43 | + } | ||
44 | + | ||
45 | + @After | ||
46 | + public void tearDown() { | ||
47 | + storeImpl.deactivate(); | ||
48 | + } | ||
49 | + | ||
50 | + @Test | ||
51 | + public void testFlowObjectiveStore() { | ||
52 | + NextGroup group2 = new DefaultNextGroup("2".getBytes(Charsets.US_ASCII)); | ||
53 | + int group1Id = store.allocateNextId(); | ||
54 | + int group2Id = store.allocateNextId(); | ||
55 | + | ||
56 | + NextGroup group1add = store.getNextGroup(group1Id); | ||
57 | + assertThat(group1add, nullValue()); | ||
58 | + | ||
59 | + store.putNextGroup(group2Id, group2); | ||
60 | + NextGroup group2Query = store.getNextGroup(group2Id); | ||
61 | + assertThat(group2Query.data(), is(group2.data())); | ||
62 | + } | ||
63 | +} |
-
Please register or login to post a comment