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