Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 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.resources.impl; |
| 17 | |
| 18 | import io.atomix.protocols.raft.service.ServiceId; |
| 19 | import io.atomix.protocols.raft.service.impl.DefaultCommit; |
| 20 | import io.atomix.protocols.raft.session.impl.RaftSessionContext; |
| 21 | import io.atomix.protocols.raft.storage.RaftStorage; |
| 22 | import io.atomix.protocols.raft.storage.snapshot.Snapshot; |
| 23 | import io.atomix.protocols.raft.storage.snapshot.SnapshotReader; |
| 24 | import io.atomix.protocols.raft.storage.snapshot.SnapshotStore; |
| 25 | import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter; |
| 26 | import io.atomix.storage.StorageLevel; |
| 27 | import io.atomix.time.WallClockTimestamp; |
| 28 | import org.junit.Test; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 29 | import org.onosproject.store.service.Versioned; |
| 30 | |
| 31 | import static org.easymock.EasyMock.mock; |
| 32 | import static org.junit.Assert.assertArrayEquals; |
| 33 | import static org.junit.Assert.assertNotNull; |
| 34 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.GET; |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 35 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.PUT; |
| 36 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.Put; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Consistent map service test. |
| 40 | */ |
| 41 | public class AtomixConsistentMapServiceTest { |
| 42 | @Test |
| 43 | @SuppressWarnings("unchecked") |
| 44 | public void testSnapshot() throws Exception { |
| 45 | SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder() |
| 46 | .withPrefix("test") |
| 47 | .withStorageLevel(StorageLevel.MEMORY) |
| 48 | .build()); |
Jordan Halterman | e372d85 | 2017-11-02 15:00:06 -0700 | [diff] [blame] | 49 | Snapshot snapshot = store.newSnapshot(ServiceId.from(1), "test", 2, new WallClockTimestamp()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 50 | |
| 51 | AtomixConsistentMapService service = new AtomixConsistentMapService(); |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 52 | service.put(new DefaultCommit<>( |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 53 | 2, |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 54 | PUT, |
| 55 | new Put("foo", "Hello world!".getBytes()), |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 56 | mock(RaftSessionContext.class), |
| 57 | System.currentTimeMillis())); |
| 58 | |
| 59 | try (SnapshotWriter writer = snapshot.openWriter()) { |
| 60 | service.snapshot(writer); |
| 61 | } |
| 62 | |
| 63 | snapshot.complete(); |
| 64 | |
| 65 | service = new AtomixConsistentMapService(); |
| 66 | try (SnapshotReader reader = snapshot.openReader()) { |
| 67 | service.install(reader); |
| 68 | } |
| 69 | |
| 70 | Versioned<byte[]> value = service.get(new DefaultCommit<>( |
| 71 | 2, |
| 72 | GET, |
| 73 | new AtomixConsistentMapOperations.Get("foo"), |
| 74 | mock(RaftSessionContext.class), |
| 75 | System.currentTimeMillis())); |
| 76 | assertNotNull(value); |
| 77 | assertArrayEquals("Hello world!".getBytes(), value.value()); |
| 78 | } |
| 79 | } |