5b2b9ea17d2dedad6e94d672e15e2a4e007464e7
[dmaap/datarouter.git] / datarouter-node / src / test / java / org / onap / dmaap / datarouter / node / DeliveryQueueTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * * ===========================================================================
7  * * Licensed under the Apache License, Version 2.0 (the "License");
8  * * you may not use this file except in compliance with the License.
9  * * You may obtain a copy of the License at
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24 package org.onap.dmaap.datarouter.node;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Matchers.anyLong;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.times;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35
36 import java.io.File;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.Hashtable;
40 import java.util.List;
41 import java.util.Vector;
42 import org.apache.commons.lang3.reflect.FieldUtils;
43 import org.jetbrains.annotations.NotNull;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mock;
48 import org.powermock.api.mockito.PowerMockito;
49 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
50 import org.powermock.modules.junit4.PowerMockRunner;
51
52 @RunWith(PowerMockRunner.class)
53 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeConfigManager")
54 public class DeliveryQueueTest {
55
56     @Mock
57     DeliveryQueueHelper deliveryQueueHelper;
58     private DeliveryQueue deliveryQueue;
59     @Mock
60     private DestInfo destInfo;
61     private String dirPath = "/tmp/dir001/";
62     private String fileName = "10000000000004.fileName.M";
63
64     @Before
65     public void setUp() throws IllegalAccessException {
66         when(destInfo.getSpool()).thenReturn(dirPath);
67         when(destInfo.isPrivilegedSubscriber()).thenReturn(true);
68         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
69         NodeConfigManager configManager = mockNodeConfigManager();
70         FieldUtils.writeDeclaredStaticField(StatusLog.class, "config", configManager, true);
71     }
72
73     @Test
74     public void Given_New_DeliveryQueue_Directory_Is_Created_As_Defined_By_DestInfo() {
75         File file = new File("/tmp");
76         assertTrue(file.exists());
77     }
78
79     @Test
80     public void Given_Delivery_Task_Failed_And_Resume_Time_Not_Reached_Return_Null() throws Exception {
81         FieldUtils.writeField(deliveryQueue, "failed", true, true);
82         FieldUtils.writeField(deliveryQueue, "resumetime", System.currentTimeMillis() * 2, true);
83         assertNull(deliveryQueue.peekNext());
84     }
85
86     @Test
87     public void Given_Delivery_Task_Return_Next_Delivery_Task_Id() throws Exception {
88         prepareFiles();
89         when(destInfo.getSpool()).thenReturn(dirPath);
90         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
91         DeliveryTask nt = deliveryQueue.getNext();
92         assertEquals("10000000000004.fileName", nt.getPublishId());
93         deleteFile(dirPath + fileName);
94         deleteFile(dirPath);
95     }
96
97     @Test
98     public void Given_Task_In_Todo_Is_Already_Cleaned_GetNext_Returns_Null() throws Exception {
99         when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L);
100         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
101         Vector<DeliveryTask> tasks = new Vector<>();
102         DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com");
103         task.clean();
104         tasks.add(task);
105         FieldUtils.writeField(deliveryQueue, "todoList", tasks, true);
106         DeliveryTask nt = deliveryQueue.getNext();
107         assertNull(nt);
108     }
109
110     @Test
111     public void Given_Task_In_Todo_Has_Resume_Time_In_Future_GetNext_Returns_Null() throws Exception {
112         when(destInfo.isPrivilegedSubscriber()).thenReturn(true);
113         when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L);
114         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
115         Vector<DeliveryTask> tasks = new Vector<>();
116         DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com");
117         long timeInFuture = 2558366240223L;
118         task.setResumeTime(timeInFuture);
119         tasks.add(task);
120         FieldUtils.writeField(deliveryQueue, "todoList", tasks, true);
121         DeliveryTask nt = deliveryQueue.getNext();
122         assertNull(nt);
123     }
124
125     @Test
126     public void Given_Task_In_Todo_Is_Expired_GetNext_Returns_Null() throws Exception {
127         when(destInfo.isPrivilegedSubscriber()).thenReturn(true);
128         when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L);
129         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
130         Vector<DeliveryTask> tasks = new Vector<>();
131         DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com");
132         long timeInPast = 1058366240223L;
133         task.setResumeTime(timeInPast);
134         tasks.add(task);
135         FieldUtils.writeField(deliveryQueue, "todoList", tasks, true);
136         DeliveryTask nt = deliveryQueue.getNext();
137         assertNull(nt);
138     }
139
140     @Test
141     public void Given_Delivery_Task_Cancel_And_FileId_Is_Null_Return_Zero() {
142         long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
143         assertEquals(0, rc);
144     }
145
146     @Test
147     public void Given_Delivery_Task_Is_Working_Cancel_Task_Returns_Zero() throws IllegalAccessException {
148         HashMap<String, DeliveryTask> tasks = new HashMap<>();
149         tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
150         FieldUtils.writeField(deliveryQueue, "working", tasks, true);
151         long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
152         assertEquals(0, rc);
153     }
154
155     @Test
156     public void Given_Delivery_Task_In_Todo_Cancel_Task_Returns_Zero() throws IllegalAccessException {
157         List<DeliveryTask> tasks = new ArrayList<>();
158         tasks.add(new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
159         FieldUtils.writeField(deliveryQueue, "todoList", tasks, true);
160         long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
161         assertEquals(0, rc);
162     }
163
164     @Test
165     public void Given_Ok_Status_And_Privileged_Subscriber_Then_Set_Resume_Time_Is_Called_On_DeliveryTask() {
166         DeliveryTask deliveryTask = mockDeliveryTask();
167         deliveryQueue.reportStatus(deliveryTask, 200, "123456789.dmaap-dr-node", "delivery");
168         verify(deliveryTask, times(1)).setResumeTime(anyLong());
169         cleanUpLogging();
170     }
171
172     @Test
173     public void Given_Ok_Status_And_Not_Privileged_Subscriber_Then_Clean_Is_Called_On_DeliveryTask() {
174         DeliveryTask deliveryTask = mockDeliveryTask();
175         when(destInfo.isPrivilegedSubscriber()).thenReturn(false);
176         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
177         deliveryQueue.reportStatus(deliveryTask, 200, "123456789.dmaap-dr-node", "delivery");
178         verify(deliveryTask, times(1)).clean();
179         cleanUpLogging();
180     }
181
182     @Test
183     public void Given_Not_Ok_Status_Then_Clean_Is_Called_On_DeliveryTask() {
184         DeliveryTask deliveryTask = mockDeliveryTask();
185         deliveryQueue.reportStatus(deliveryTask, 400, "123456789.dmaap-dr-node", "delivery");
186         verify(deliveryTask, times(1)).clean();
187         cleanUpLogging();
188     }
189
190     @Test
191     public void Given_Task_In_Working_MarkTaskSuccess_Returns_True() throws IllegalAccessException {
192         HashMap<String, DeliveryTask> tasks = new HashMap<>();
193         tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
194         FieldUtils.writeField(deliveryQueue, "working", tasks, true);
195         assertTrue(deliveryQueue.markTaskSuccess("123.node.datarouternew.com"));
196     }
197
198     @Test
199     public void Given_Task_In_Retry_MarkTaskSuccess_Returns_True() throws IllegalAccessException {
200         HashMap<String, DeliveryTask> tasks = new HashMap<>();
201         tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
202         FieldUtils.writeField(deliveryQueue, "retry", tasks, true);
203         assertTrue(deliveryQueue.markTaskSuccess("123.node.datarouternew.com"));
204     }
205
206     @Test
207     public void Given_Task_Does_Not_Exist_MarkTaskSuccess_Returns_False() {
208         assertFalse(deliveryQueue.markTaskSuccess("false.pubId.com"));
209     }
210
211     private void cleanUpLogging() {
212         final File currentDir = new File(System.getProperty("user.dir"));
213         final File[] files = currentDir.listFiles((file, name) -> name.matches("null.*"));
214         if (files != null) {
215             for (final File file : files) {
216                 file.delete();
217             }
218         }
219     }
220
221     @NotNull
222     private DeliveryTask mockDeliveryTask() {
223         DeliveryTask deliveryTask = mock(DeliveryTask.class);
224         when(deliveryTask.getPublishId()).thenReturn("123456789.dmaap-dr-node");
225         when(deliveryTask.getFeedId()).thenReturn("1");
226         when(deliveryTask.getSubId()).thenReturn("1");
227         when(deliveryTask.getURL()).thenReturn("http://subcriber.com:7070/delivery");
228         when(deliveryTask.getCType()).thenReturn("application/json");
229         when(deliveryTask.getLength()).thenReturn(486L);
230         return deliveryTask;
231     }
232
233     private NodeConfigManager mockNodeConfigManager() {
234         NodeConfigManager config = mock(NodeConfigManager.class);
235         PowerMockito.when(config.getEventLogInterval()).thenReturn("30000");
236         return config;
237     }
238
239     private void prepareFiles() throws Exception {
240         createFolder(dirPath);
241         createFile(fileName, dirPath);
242     }
243
244     private void createFolder(String dirName) {
245         File newDirectory = new File(dirName);
246         newDirectory.mkdirs();
247     }
248
249     private void createFile(String file, String dir) throws Exception {
250         File newFile = new File(dir + File.separator + file);
251         newFile.createNewFile();
252     }
253
254     private void deleteFile(String fileName) {
255         File file = new File(fileName);
256
257         if (file.exists()) {
258             file.delete();
259         }
260     }
261
262 }