2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 package org.onap.appc.sdc.listener;
24 import com.att.eelf.configuration.EELFLogger;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mockito;
30 import org.onap.sdc.api.IDistributionClient;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
33 import org.powermock.reflect.Whitebox;
35 import java.util.concurrent.CountDownLatch;
36 import java.util.concurrent.TimeUnit;
38 import static org.mockito.Matchers.any;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.spy;
41 import static org.mockito.Mockito.times;
43 @RunWith(PowerMockRunner.class)
44 @PrepareForTest(Thread.class)
45 public class SdcListenerTest {
46 private SdcListener sdcListener;
47 private EELFLogger mockLogger = mock(EELFLogger.class);
50 public void setUp() throws Exception {
51 sdcListener = new SdcListener();
53 // to avoid operation on logger fail, mock up the logger
54 Whitebox.setInternalState(sdcListener, "logger", mockLogger);
58 public void testStart() throws Exception {
60 Assert.assertTrue("Should created startThread",
61 Whitebox.getInternalState(sdcListener, "startThread") != null);
65 public void testStop() throws Exception {
66 // test interrupt thread and other null case
67 MockThread mockThread = spy(new MockThread());
68 mockThread.setNewState(Thread.State.TIMED_WAITING);
69 Whitebox.setInternalState(sdcListener, "startThread", mockThread);
72 Mockito.verify(mockThread, times(1)).interrupt();
73 Assert.assertTrue("Should reset startThread",
74 Whitebox.getInternalState(sdcListener, "startThread") == null);
76 // test other non-null case and thread null case
77 IDistributionClient mockClient = mock(IDistributionClient.class);
78 Whitebox.setInternalState(sdcListener, "client", mockClient);
79 SdcCallback mockCallback = mock(SdcCallback.class);
80 Whitebox.setInternalState(sdcListener, "callback", mockCallback);
81 CountDownLatch mockLatch = mock(CountDownLatch.class);
82 Whitebox.setInternalState(sdcListener, "latch", mockLatch);
86 Mockito.verify(mockLatch, times(1)).await(10, TimeUnit.SECONDS);
87 Mockito.verify(mockCallback, times(1)).stop();
88 Mockito.verify(mockClient, times(1)).stop();
89 Assert.assertTrue("Should reset latch",
90 Whitebox.getInternalState(sdcListener, "latch") == null);
91 Assert.assertTrue("Should reset callback",
92 Whitebox.getInternalState(sdcListener, "callback") == null);
93 Assert.assertTrue("Should reset client",
94 Whitebox.getInternalState(sdcListener, "client") == null);
98 public void testStopStartThread() throws Exception {
100 sdcListener.stopStartThread(123);
101 //Mockito.verify(mockLogger, times(0)).debug(String.valueOf(any()));
103 MockThread mockThread = spy(new MockThread());
105 // thread terminated case
106 Whitebox.setInternalState(sdcListener, "startThread", mockThread);
107 mockThread.setNewState(Thread.State.TERMINATED);
108 sdcListener.stopStartThread(123);
109 Mockito.verify(mockThread, times(0)).interrupt();
110 //Mockito.verify(mockLogger, times(1)).debug(String.valueOf(any()));
111 Assert.assertTrue("Should reset startThread",
112 Whitebox.getInternalState(sdcListener, "startThread") == null);
114 // thread not termianted case
115 int timesCallThread = 0;
116 int timesCallLogger = 1;
117 for(Thread.State state : Thread.State.values()) {
118 if (state == Thread.State.TERMINATED) {
121 Whitebox.setInternalState(sdcListener, "startThread", mockThread);
122 mockThread.setNewState(state);
123 sdcListener.stopStartThread(123);
124 Mockito.verify(mockThread, times(++ timesCallThread)).interrupt();
125 //Mockito.verify(mockLogger, times(timesCallLogger += 2)).debug(String.valueOf(any()));
126 Assert.assertTrue("Should reset startThread",
127 Whitebox.getInternalState(sdcListener, "startThread") == null);
132 * I have used the following PowerMockito (due to Thread.getName() is a final method)
133 * try to mock up the thread behavior. But the mock Thread.getName() always returns null
134 * which works in intelliJ Junit test, but not Jenkins build:
135 * Thread mockThread = PowerMockito.mock(Thread.class);
136 * PowerMockito.doReturn(Thread.State.TERMINATED).when(mockThread).getState();
137 * PowerMockito.doReturn("testing").when(mockThread).getName();
138 * Hence, here goes the MockThread class to override Thread to my expected behavior.
140 class MockThread extends Thread {
143 private MockThread() {
144 super.setName("testing");
147 void setNewState(State newState) {
152 public State getState() {