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 java.util.Optional; |
| 19 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 20 | import io.atomix.protocols.raft.service.impl.DefaultCommit; |
| 21 | import io.atomix.protocols.raft.session.impl.RaftSessionContext; |
| 22 | import io.atomix.protocols.raft.storage.RaftStorage; |
| 23 | import io.atomix.protocols.raft.storage.snapshot.Snapshot; |
| 24 | import io.atomix.protocols.raft.storage.snapshot.SnapshotReader; |
| 25 | import io.atomix.protocols.raft.storage.snapshot.SnapshotStore; |
| 26 | import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter; |
| 27 | import io.atomix.storage.StorageLevel; |
| 28 | import io.atomix.time.WallClockTimestamp; |
| 29 | import org.junit.Test; |
| 30 | import org.onlab.util.Match; |
| 31 | import org.onosproject.store.service.DocumentPath; |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 32 | import org.onosproject.store.service.Ordering; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 33 | import org.onosproject.store.service.Versioned; |
| 34 | |
| 35 | import static org.easymock.EasyMock.mock; |
| 36 | import static org.junit.Assert.assertArrayEquals; |
| 37 | import static org.junit.Assert.assertNotNull; |
| 38 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.GET; |
| 39 | import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.UPDATE; |
| 40 | |
| 41 | /** |
| 42 | * Document tree service test. |
| 43 | */ |
| 44 | public class AtomixDocumentTreeServiceTest { |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 45 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 46 | @Test |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 47 | public void testNaturalOrderedSnapshot() throws Exception { |
| 48 | testSnapshot(Ordering.NATURAL); |
| 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void testInsertionOrderedSnapshot() throws Exception { |
| 53 | testSnapshot(Ordering.INSERTION); |
| 54 | } |
| 55 | |
| 56 | private void testSnapshot(Ordering ordering) throws Exception { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 57 | SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder() |
| 58 | .withPrefix("test") |
| 59 | .withStorageLevel(StorageLevel.MEMORY) |
| 60 | .build()); |
Jordan Halterman | 4ce65e8 | 2018-02-15 18:09:38 -0800 | [diff] [blame] | 61 | Snapshot snapshot = store.newSnapshot(2, new WallClockTimestamp()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 62 | |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 63 | AtomixDocumentTreeService service = new AtomixDocumentTreeService(ordering); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 64 | service.update(new DefaultCommit<>( |
| 65 | 2, |
| 66 | UPDATE, |
| 67 | new AtomixDocumentTreeOperations.Update( |
| 68 | DocumentPath.from("root|foo"), |
| 69 | Optional.of("Hello world!".getBytes()), |
| 70 | Match.any(), |
| 71 | Match.ifNull()), |
| 72 | mock(RaftSessionContext.class), |
| 73 | System.currentTimeMillis())); |
| 74 | |
| 75 | try (SnapshotWriter writer = snapshot.openWriter()) { |
| 76 | service.snapshot(writer); |
| 77 | } |
| 78 | |
| 79 | snapshot.complete(); |
| 80 | |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 81 | service = new AtomixDocumentTreeService(ordering); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 82 | try (SnapshotReader reader = snapshot.openReader()) { |
| 83 | service.install(reader); |
| 84 | } |
| 85 | |
| 86 | Versioned<byte[]> value = service.get(new DefaultCommit<>( |
| 87 | 2, |
| 88 | GET, |
| 89 | new AtomixDocumentTreeOperations.Get(DocumentPath.from("root|foo")), |
| 90 | mock(RaftSessionContext.class), |
| 91 | System.currentTimeMillis())); |
| 92 | assertNotNull(value); |
| 93 | assertArrayEquals("Hello world!".getBytes(), value.value()); |
| 94 | } |
| 95 | } |