blob: c292959ead9345fc7e73fdc1c2640865313dac63 [file] [log] [blame]
Jordan Halterman2bf177c2017-06-29 01:49:08 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jordan Halterman2bf177c2017-06-29 01:49:08 -07003 *
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
Jordan Halterman2bf177c2017-06-29 01:49:08 -070018import io.atomix.protocols.raft.service.impl.DefaultCommit;
19import io.atomix.protocols.raft.session.impl.RaftSessionContext;
20import io.atomix.protocols.raft.storage.RaftStorage;
21import io.atomix.protocols.raft.storage.snapshot.Snapshot;
22import io.atomix.protocols.raft.storage.snapshot.SnapshotReader;
23import io.atomix.protocols.raft.storage.snapshot.SnapshotStore;
24import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter;
25import io.atomix.storage.StorageLevel;
26import io.atomix.time.WallClockTimestamp;
27import org.junit.Test;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070028import org.onosproject.store.service.Versioned;
29
30import static org.easymock.EasyMock.mock;
31import static org.junit.Assert.assertArrayEquals;
32import static org.junit.Assert.assertNotNull;
33import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.GET;
Jordan Halterman71635ae2017-07-28 10:35:43 -070034import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.PUT;
35import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.Put;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070036
37/**
38 * Consistent map service test.
39 */
40public class AtomixConsistentMapServiceTest {
41 @Test
42 @SuppressWarnings("unchecked")
43 public void testSnapshot() throws Exception {
44 SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder()
45 .withPrefix("test")
46 .withStorageLevel(StorageLevel.MEMORY)
47 .build());
Jordan Halterman4ce65e82018-02-15 18:09:38 -080048 Snapshot snapshot = store.newSnapshot(2, new WallClockTimestamp());
Jordan Halterman2bf177c2017-06-29 01:49:08 -070049
50 AtomixConsistentMapService service = new AtomixConsistentMapService();
Jordan Halterman71635ae2017-07-28 10:35:43 -070051 service.put(new DefaultCommit<>(
Jordan Halterman2bf177c2017-06-29 01:49:08 -070052 2,
Jordan Halterman71635ae2017-07-28 10:35:43 -070053 PUT,
54 new Put("foo", "Hello world!".getBytes()),
Jordan Halterman2bf177c2017-06-29 01:49:08 -070055 mock(RaftSessionContext.class),
56 System.currentTimeMillis()));
57
58 try (SnapshotWriter writer = snapshot.openWriter()) {
59 service.snapshot(writer);
60 }
61
62 snapshot.complete();
63
64 service = new AtomixConsistentMapService();
65 try (SnapshotReader reader = snapshot.openReader()) {
66 service.install(reader);
67 }
68
69 Versioned<byte[]> value = service.get(new DefaultCommit<>(
70 2,
71 GET,
72 new AtomixConsistentMapOperations.Get("foo"),
73 mock(RaftSessionContext.class),
74 System.currentTimeMillis()));
75 assertNotNull(value);
76 assertArrayEquals("Hello world!".getBytes(), value.value());
77 }
78}