blob: 21c562126bf49dda7da75eac2d33701bda511bc7 [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
Jordan Halterman2bf177c2017-06-29 01:49:08 -070020import io.atomix.protocols.raft.service.impl.DefaultCommit;
21import io.atomix.protocols.raft.session.impl.RaftSessionContext;
22import io.atomix.protocols.raft.storage.RaftStorage;
23import io.atomix.protocols.raft.storage.snapshot.Snapshot;
24import io.atomix.protocols.raft.storage.snapshot.SnapshotReader;
25import io.atomix.protocols.raft.storage.snapshot.SnapshotStore;
26import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter;
27import io.atomix.storage.StorageLevel;
28import io.atomix.time.WallClockTimestamp;
29import org.junit.Test;
30import org.onlab.util.Match;
31import org.onosproject.store.service.DocumentPath;
Jordan Haltermand0d80352017-08-10 15:08:27 -070032import org.onosproject.store.service.Ordering;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070033import 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 {
Jordan Haltermand0d80352017-08-10 15:08:27 -070045
Jordan Halterman2bf177c2017-06-29 01:49:08 -070046 @Test
Jordan Haltermand0d80352017-08-10 15:08:27 -070047 public void testNaturalOrderedSnapshot() throws Exception {
48 testSnapshot(Ordering.NATURAL);
49 }
50
51 @Test
52 public void testInsertionOrderedSnapshot() throws Exception {
53 testSnapshot(Ordering.INSERTION);
54 }
55
56 private void testSnapshot(Ordering ordering) throws Exception {
Jordan Halterman2bf177c2017-06-29 01:49:08 -070057 SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder()
58 .withPrefix("test")
59 .withStorageLevel(StorageLevel.MEMORY)
60 .build());
Jordan Halterman4ce65e82018-02-15 18:09:38 -080061 Snapshot snapshot = store.newSnapshot(2, new WallClockTimestamp());
Jordan Halterman2bf177c2017-06-29 01:49:08 -070062
Jordan Haltermand0d80352017-08-10 15:08:27 -070063 AtomixDocumentTreeService service = new AtomixDocumentTreeService(ordering);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070064 service.update(new DefaultCommit<>(
65 2,
66 UPDATE,
67 new AtomixDocumentTreeOperations.Update(
68 DocumentPath.from("root|foo"),
69 Optional.of("Hello world!".getBytes()),
70 Match.any(),
71 Match.ifNull()),
72 mock(RaftSessionContext.class),
73 System.currentTimeMillis()));
74
75 try (SnapshotWriter writer = snapshot.openWriter()) {
76 service.snapshot(writer);
77 }
78
79 snapshot.complete();
80
Jordan Haltermand0d80352017-08-10 15:08:27 -070081 service = new AtomixDocumentTreeService(ordering);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070082 try (SnapshotReader reader = snapshot.openReader()) {
83 service.install(reader);
84 }
85
86 Versioned<byte[]> value = service.get(new DefaultCommit<>(
87 2,
88 GET,
89 new AtomixDocumentTreeOperations.Get(DocumentPath.from("root|foo")),
90 mock(RaftSessionContext.class),
91 System.currentTimeMillis()));
92 assertNotNull(value);
93 assertArrayEquals("Hello world!".getBytes(), value.value());
94 }
95}