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