blob: 04698e0b1ff92ea849551f4800ed2edaca415531 [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 io.atomix.protocols.raft.service.ServiceId;
19import io.atomix.protocols.raft.service.impl.DefaultCommit;
20import io.atomix.protocols.raft.session.impl.RaftSessionContext;
21import io.atomix.protocols.raft.storage.RaftStorage;
22import io.atomix.protocols.raft.storage.snapshot.Snapshot;
23import io.atomix.protocols.raft.storage.snapshot.SnapshotReader;
24import io.atomix.protocols.raft.storage.snapshot.SnapshotStore;
25import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter;
26import io.atomix.storage.StorageLevel;
27import io.atomix.time.WallClockTimestamp;
28import org.junit.Test;
29import org.onlab.util.Match;
30import org.onosproject.store.service.Versioned;
31
32import static org.easymock.EasyMock.mock;
33import static org.junit.Assert.assertArrayEquals;
34import static org.junit.Assert.assertNotNull;
35import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.GET;
36import static org.onosproject.store.primitives.resources.impl.AtomixConsistentTreeMapOperations.UPDATE_AND_GET;
37
38/**
39 * Consistent tree map service test.
40 */
41public class AtomixConsistentTreeMapServiceTest {
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());
49 Snapshot snapshot = store.newSnapshot(ServiceId.from(1), 2, new WallClockTimestamp());
50
51 AtomixConsistentTreeMapService service = new AtomixConsistentTreeMapService();
52 service.updateAndGet(new DefaultCommit<>(
53 2,
54 UPDATE_AND_GET,
55 new AtomixConsistentTreeMapOperations.UpdateAndGet(
56 "foo", "Hello world!".getBytes(), Match.ANY, Match.ANY),
57 mock(RaftSessionContext.class),
58 System.currentTimeMillis()));
59
60 try (SnapshotWriter writer = snapshot.openWriter()) {
61 service.snapshot(writer);
62 }
63
64 snapshot.complete();
65
66 service = new AtomixConsistentTreeMapService();
67 try (SnapshotReader reader = snapshot.openReader()) {
68 service.install(reader);
69 }
70
71 Versioned<byte[]> value = service.get(new DefaultCommit<>(
72 2,
73 GET,
74 new AtomixConsistentTreeMapOperations.Get("foo"),
75 mock(RaftSessionContext.class),
76 System.currentTimeMillis()));
77 assertNotNull(value);
78 assertArrayEquals("Hello world!".getBytes(), value.value());
79 }
80}