blob: 098c1933ad5f03db45e5247b9f379f9305a3e5e7 [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.AtomixConsistentMapOperations.GET;
36import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.UPDATE_AND_GET;
37
38/**
39 * Consistent map service test.
40 */
41public 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());
49 Snapshot snapshot = store.newSnapshot(ServiceId.from(1), 2, new WallClockTimestamp());
50
51 AtomixConsistentMapService service = new AtomixConsistentMapService();
52 service.updateAndGet(new DefaultCommit<>(
53 2,
54 UPDATE_AND_GET,
55 new AtomixConsistentMapOperations.UpdateAndGet("foo", "Hello world!".getBytes(), Match.ANY, Match.ANY),
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}