blob: 66173d0e2c489e3372240feef06cd8d27990b697 [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 java.util.Collection;
Yuta HIGUCHI1edc36b2018-01-24 23:39:06 -080019import java.util.Collections;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070020
21import io.atomix.protocols.raft.service.ServiceId;
22import io.atomix.protocols.raft.service.impl.DefaultCommit;
23import io.atomix.protocols.raft.session.impl.RaftSessionContext;
24import io.atomix.protocols.raft.storage.RaftStorage;
25import io.atomix.protocols.raft.storage.snapshot.Snapshot;
26import io.atomix.protocols.raft.storage.snapshot.SnapshotReader;
27import io.atomix.protocols.raft.storage.snapshot.SnapshotStore;
28import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter;
29import io.atomix.storage.StorageLevel;
30import io.atomix.time.WallClockTimestamp;
31import org.junit.Test;
32import org.onlab.util.Match;
33import org.onosproject.store.service.Versioned;
34
35import static org.easymock.EasyMock.mock;
36import static org.junit.Assert.assertArrayEquals;
37import static org.junit.Assert.assertEquals;
38import static org.junit.Assert.assertNotNull;
39import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.GET;
40import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.PUT;
41
42/**
43 * Consistent set multimap service test.
44 */
45public class AtomixConsistentSetMultimapServiceTest {
46 @Test
47 @SuppressWarnings("unchecked")
48 public void testSnapshot() throws Exception {
49 SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder()
50 .withPrefix("test")
51 .withStorageLevel(StorageLevel.MEMORY)
52 .build());
Jordan Haltermane372d852017-11-02 15:00:06 -070053 Snapshot snapshot = store.newSnapshot(ServiceId.from(1), "test", 2, new WallClockTimestamp());
Jordan Halterman2bf177c2017-06-29 01:49:08 -070054
55 AtomixConsistentSetMultimapService service = new AtomixConsistentSetMultimapService();
56 service.put(new DefaultCommit<>(
57 2,
58 PUT,
59 new AtomixConsistentSetMultimapOperations.Put(
Yuta HIGUCHI1edc36b2018-01-24 23:39:06 -080060 "foo", Collections.singletonList("Hello world!".getBytes()), Match.ANY),
Jordan Halterman2bf177c2017-06-29 01:49:08 -070061 mock(RaftSessionContext.class),
62 System.currentTimeMillis()));
63
64 try (SnapshotWriter writer = snapshot.openWriter()) {
65 service.snapshot(writer);
66 }
67
68 snapshot.complete();
69
70 service = new AtomixConsistentSetMultimapService();
71 try (SnapshotReader reader = snapshot.openReader()) {
72 service.install(reader);
73 }
74
75 Versioned<Collection<? extends byte[]>> value = service.get(new DefaultCommit<>(
76 2,
77 GET,
78 new AtomixConsistentSetMultimapOperations.Get("foo"),
79 mock(RaftSessionContext.class),
80 System.currentTimeMillis()));
81 assertNotNull(value);
82 assertEquals(1, value.value().size());
83 assertArrayEquals("Hello world!".getBytes(), value.value().iterator().next());
84 }
85}