blob: fd92e2bfe16b34e300bc53db231234ff5d44382b [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
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;
Jordan Haltermand0d80352017-08-10 15:08:27 -070033import org.onosproject.store.service.Ordering;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070034import org.onosproject.store.service.Versioned;
35
36import static org.easymock.EasyMock.mock;
37import static org.junit.Assert.assertArrayEquals;
38import static org.junit.Assert.assertNotNull;
39import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.GET;
40import static org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeOperations.UPDATE;
41
42/**
43 * Document tree service test.
44 */
45public class AtomixDocumentTreeServiceTest {
Jordan Haltermand0d80352017-08-10 15:08:27 -070046
Jordan Halterman2bf177c2017-06-29 01:49:08 -070047 @Test
Jordan Haltermand0d80352017-08-10 15:08:27 -070048 public void testNaturalOrderedSnapshot() throws Exception {
49 testSnapshot(Ordering.NATURAL);
50 }
51
52 @Test
53 public void testInsertionOrderedSnapshot() throws Exception {
54 testSnapshot(Ordering.INSERTION);
55 }
56
57 private void testSnapshot(Ordering ordering) throws Exception {
Jordan Halterman2bf177c2017-06-29 01:49:08 -070058 SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder()
59 .withPrefix("test")
60 .withStorageLevel(StorageLevel.MEMORY)
61 .build());
Jordan Halterman19486e32017-11-02 15:00:06 -070062 Snapshot snapshot = store.newSnapshot(ServiceId.from(1), "test", 2, new WallClockTimestamp());
Jordan Halterman2bf177c2017-06-29 01:49:08 -070063
Jordan Haltermand0d80352017-08-10 15:08:27 -070064 AtomixDocumentTreeService service = new AtomixDocumentTreeService(ordering);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070065 service.update(new DefaultCommit<>(
66 2,
67 UPDATE,
68 new AtomixDocumentTreeOperations.Update(
69 DocumentPath.from("root|foo"),
70 Optional.of("Hello world!".getBytes()),
71 Match.any(),
72 Match.ifNull()),
73 mock(RaftSessionContext.class),
74 System.currentTimeMillis()));
75
76 try (SnapshotWriter writer = snapshot.openWriter()) {
77 service.snapshot(writer);
78 }
79
80 snapshot.complete();
81
Jordan Haltermand0d80352017-08-10 15:08:27 -070082 service = new AtomixDocumentTreeService(ordering);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070083 try (SnapshotReader reader = snapshot.openReader()) {
84 service.install(reader);
85 }
86
87 Versioned<byte[]> value = service.get(new DefaultCommit<>(
88 2,
89 GET,
90 new AtomixDocumentTreeOperations.Get(DocumentPath.from("root|foo")),
91 mock(RaftSessionContext.class),
92 System.currentTimeMillis()));
93 assertNotNull(value);
94 assertArrayEquals("Hello world!".getBytes(), value.value());
95 }
96}