7b60ce460a90178dfca76aba4ffc7392fd3260de
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / test / java / org / onap / appc / sdc / listener / SdcListenerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24 package org.onap.appc.sdc.listener;
25
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;
36
37 import java.util.concurrent.CountDownLatch;
38 import java.util.concurrent.TimeUnit;
39
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;
44
45 @RunWith(PowerMockRunner.class)
46 @PrepareForTest(Thread.class)
47 public class SdcListenerTest {
48     private SdcListener sdcListener;
49     private EELFLogger mockLogger = mock(EELFLogger.class);
50
51     @Before
52     public void setUp() throws Exception {
53         sdcListener = new SdcListener();
54
55         // to avoid operation on logger fail, mock up the logger
56         Whitebox.setInternalState(sdcListener, "logger", mockLogger);
57     }
58
59     @Test
60     public void testStart() throws Exception {
61         sdcListener.start();
62         Assert.assertTrue("Should created startThread",
63                 Whitebox.getInternalState(sdcListener, "startThread") != null);
64     }
65
66     @Test
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);
72
73         sdcListener.stop();
74         Mockito.verify(mockThread, times(1)).interrupt();
75         Assert.assertTrue("Should reset startThread",
76                 Whitebox.getInternalState(sdcListener, "startThread") == null);
77
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);
85
86         sdcListener.stop();
87
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);
97     }
98
99     @Test
100     public void testStopStartThread() throws Exception {
101         // null case
102         sdcListener.stopStartThread(123);
103         //Mockito.verify(mockLogger, times(0)).debug(String.valueOf(any()));
104
105         MockThread mockThread = spy(new MockThread());
106
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);
115
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) {
121                 continue;
122             }
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);
130         }
131     }
132
133     /*
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.
141      */
142     class MockThread extends Thread {
143         private State state;
144
145         private MockThread() {
146             super.setName("testing");
147         }
148
149         void setNewState(State newState) {
150             state = newState;
151         }
152
153         @Override
154         public State getState() {
155             return state;
156         }
157     }
158 }