b0e0c55f18e4428557e029738c10df6dc3b38935
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / messaging / AbstractMessagingTest.java
1 /*
2  * Copyright (c) 2017 Inocybe Technologies and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.messaging;
9
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;
14
15 import akka.actor.ActorSystem;
16 import akka.testkit.TestProbe;
17 import akka.testkit.javadsl.TestKit;
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;
28
29 /**
30  * Abstract base class for messaging tests.
31  *
32  * @author Thomas Pantelis
33  */
34 public class AbstractMessagingTest {
35     protected static final StringIdentifier IDENTIFIER = new StringIdentifier("test");
36     protected static ActorSystem ACTOR_SYSTEM;
37
38     protected final TestProbe testProbe = TestProbe.apply(ACTOR_SYSTEM);
39
40     @Mock
41     protected FileBackedOutputStreamFactory mockFiledBackedStreamFactory;
42
43     @Mock
44     protected FileBackedOutputStream mockFiledBackedStream;
45
46     @Mock
47     protected ByteSource mockByteSource;
48
49     @Mock
50     protected InputStream mockInputStream;
51
52     @BeforeClass
53     public static void setupClass() {
54         ACTOR_SYSTEM = ActorSystem.create("test");
55     }
56
57     @Before
58     public void setup() throws IOException {
59         MockitoAnnotations.initMocks(this);
60
61         doReturn(mockFiledBackedStream).when(mockFiledBackedStreamFactory).newInstance();
62         setupMockFiledBackedStream(mockFiledBackedStream);
63         doReturn(mockByteSource).when(mockFiledBackedStream).asByteSource();
64
65         doReturn(mockInputStream).when(mockByteSource).openStream();
66         doReturn(mockInputStream).when(mockByteSource).openBufferedStream();
67         doReturn(10L).when(mockByteSource).size();
68
69         doReturn(0).when(mockInputStream).read(any(byte[].class));
70     }
71
72     @AfterClass
73     public static void tearDownClass() {
74         TestKit.shutdownActorSystem(ACTOR_SYSTEM, true);
75     }
76
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();
85     }
86 }