2 * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.messaging;
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.anyInt;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
15 import akka.actor.ActorSystem;
16 import akka.testkit.JavaTestKit;
17 import akka.testkit.TestProbe;
18 import com.google.common.io.ByteSource;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import org.junit.AfterClass;
22 import org.junit.Before;
23 import org.junit.BeforeClass;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.controller.cluster.io.FileBackedOutputStream;
27 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
30 * Abstract base class for messaging tests.
32 * @author Thomas Pantelis
34 public class AbstractMessagingTest {
35 protected static final StringIdentifier IDENTIFIER = new StringIdentifier("test");
36 protected static ActorSystem ACTOR_SYSTEM;
38 protected final TestProbe testProbe = TestProbe.apply(ACTOR_SYSTEM);
41 protected FileBackedOutputStreamFactory mockFiledBackedStreamFactory;
44 protected FileBackedOutputStream mockFiledBackedStream;
47 protected ByteSource mockByteSource;
50 protected InputStream mockInputStream;
53 public static void setupClass() throws IOException {
54 ACTOR_SYSTEM = ActorSystem.create("test");
58 public void setup() throws IOException {
59 MockitoAnnotations.initMocks(this);
61 doReturn(mockFiledBackedStream).when(mockFiledBackedStreamFactory).newInstance();
62 setupMockFiledBackedStream(mockFiledBackedStream);
63 doReturn(mockByteSource).when(mockFiledBackedStream).asByteSource();
65 doReturn(mockInputStream).when(mockByteSource).openStream();
66 doReturn(mockInputStream).when(mockByteSource).openBufferedStream();
67 doReturn(10L).when(mockByteSource).size();
69 doReturn(0).when(mockInputStream).read(any(byte[].class));
73 public static void tearDownClass() {
74 JavaTestKit.shutdownActorSystem(ACTOR_SYSTEM, Boolean.TRUE);
77 void setupMockFiledBackedStream(final FileBackedOutputStream mockOutputStream) throws IOException {
78 doNothing().when(mockOutputStream).write(any(byte[].class), anyInt(), anyInt());
79 doNothing().when(mockOutputStream).write(any(byte[].class));
80 doNothing().when(mockOutputStream).write(anyInt());
81 doNothing().when(mockOutputStream).close();
82 doNothing().when(mockOutputStream).cleanup();
83 doNothing().when(mockOutputStream).flush();
84 doReturn(mockByteSource).when(mockOutputStream).asByteSource();