2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
24 package org.openecomp.appc.sdc.listener;
26 import com.att.eelf.configuration.EELFLogger;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mockito;
32 import org.openecomp.sdc.api.IDistributionClient;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34 import org.powermock.modules.junit4.PowerMockRunner;
35 import org.powermock.reflect.Whitebox;
37 import java.util.concurrent.CountDownLatch;
38 import java.util.concurrent.TimeUnit;
40 import static org.mockito.Matchers.any;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.spy;
43 import static org.mockito.Mockito.times;
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest(Thread.class)
47 public class SdcListenerTest {
48 private SdcListener sdcListener;
49 private EELFLogger mockLogger = mock(EELFLogger.class);
52 public void setUp() throws Exception {
53 sdcListener = new SdcListener();
55 // to avoid operation on logger fail, mock up the logger
56 Whitebox.setInternalState(sdcListener, "logger", mockLogger);
60 public void testStart() throws Exception {
62 Assert.assertTrue("Should created startThread",
63 Whitebox.getInternalState(sdcListener, "startThread") != null);
67 public void testStop() throws Exception {
68 // test interrupt thread and other null case
69 MockThread mockThread = spy(new MockThread());
70 mockThread.setNewState(Thread.State.TIMED_WAITING);
71 Whitebox.setInternalState(sdcListener, "startThread", mockThread);
74 Mockito.verify(mockThread, times(1)).interrupt();
75 Assert.assertTrue("Should reset startThread",
76 Whitebox.getInternalState(sdcListener, "startThread") == null);
78 // test other non-null case and thread null case
79 IDistributionClient mockClient = mock(IDistributionClient.class);
80 Whitebox.setInternalState(sdcListener, "client", mockClient);
81 SdcCallback mockCallback = mock(SdcCallback.class);
82 Whitebox.setInternalState(sdcListener, "callback", mockCallback);
83 CountDownLatch mockLatch = mock(CountDownLatch.class);
84 Whitebox.setInternalState(sdcListener, "latch", mockLatch);
88 Mockito.verify(mockLatch, times(1)).await(10, TimeUnit.SECONDS);
89 Mockito.verify(mockCallback, times(1)).stop();
90 Mockito.verify(mockClient, times(1)).stop();
91 Assert.assertTrue("Should reset latch",
92 Whitebox.getInternalState(sdcListener, "latch") == null);
93 Assert.assertTrue("Should reset callback",
94 Whitebox.getInternalState(sdcListener, "callback") == null);
95 Assert.assertTrue("Should reset client",
96 Whitebox.getInternalState(sdcListener, "client") == null);
100 public void testStopStartThread() throws Exception {
102 sdcListener.stopStartThread(123);
103 //Mockito.verify(mockLogger, times(0)).debug(String.valueOf(any()));
105 MockThread mockThread = spy(new MockThread());
107 // thread terminated case
108 Whitebox.setInternalState(sdcListener, "startThread", mockThread);
109 mockThread.setNewState(Thread.State.TERMINATED);
110 sdcListener.stopStartThread(123);
111 Mockito.verify(mockThread, times(0)).interrupt();
112 //Mockito.verify(mockLogger, times(1)).debug(String.valueOf(any()));
113 Assert.assertTrue("Should reset startThread",
114 Whitebox.getInternalState(sdcListener, "startThread") == null);
116 // thread not termianted case
117 int timesCallThread = 0;
118 int timesCallLogger = 1;
119 for(Thread.State state : Thread.State.values()) {
120 if (state == Thread.State.TERMINATED) {
123 Whitebox.setInternalState(sdcListener, "startThread", mockThread);
124 mockThread.setNewState(state);
125 sdcListener.stopStartThread(123);
126 Mockito.verify(mockThread, times(++ timesCallThread)).interrupt();
127 //Mockito.verify(mockLogger, times(timesCallLogger += 2)).debug(String.valueOf(any()));
128 Assert.assertTrue("Should reset startThread",
129 Whitebox.getInternalState(sdcListener, "startThread") == null);
134 * I have used the following PowerMockito (due to Thread.getName() is a final method)
135 * try to mock up the thread behavior. But the mock Thread.getName() always returns null
136 * which works in intelliJ Junit test, but not Jenkins build:
137 * Thread mockThread = PowerMockito.mock(Thread.class);
138 * PowerMockito.doReturn(Thread.State.TERMINATED).when(mockThread).getState();
139 * PowerMockito.doReturn("testing").when(mockThread).getName();
140 * Hence, here goes the MockThread class to override Thread to my expected behavior.
142 class MockThread extends Thread {
145 private MockThread() {
146 super.setName("testing");
149 void setNewState(State newState) {
154 public State getState() {