1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package org.onap.dmaap.datarouter.node;
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;
37 import java.util.Hashtable;
38 import java.util.Vector;
39 import org.apache.commons.lang3.reflect.FieldUtils;
40 import org.jetbrains.annotations.NotNull;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.powermock.api.mockito.PowerMockito;
46 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
47 import org.powermock.modules.junit4.PowerMockRunner;
49 @RunWith(PowerMockRunner.class)
50 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeConfigManager")
51 public class DeliveryQueueTest {
54 DeliveryQueueHelper deliveryQueueHelper;
55 private DeliveryQueue deliveryQueue;
57 private DestInfo destInfo;
58 private String dirPath = "/tmp/dir001/";
59 private String fileName = "10000000000004.fileName.M";
62 public void setUp() throws IllegalAccessException {
63 when(destInfo.getSpool()).thenReturn(dirPath);
64 when(destInfo.isPrivilegedSubscriber()).thenReturn(true);
65 deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
66 NodeConfigManager configManager = mockNodeConfigManager();
67 FieldUtils.writeDeclaredStaticField(StatusLog.class, "config", configManager, true);
71 public void Given_New_DeliveryQueue_Directory_Is_Created_As_Defined_By_DestInfo() {
72 File file = new File("/tmp");
73 assertTrue(file.exists());
77 public void Given_Delivery_Task_Failed_And_Resume_Time_Not_Reached_Return_Null() throws Exception {
78 FieldUtils.writeField(deliveryQueue, "failed", true, true);
79 FieldUtils.writeField(deliveryQueue, "resumetime", System.currentTimeMillis() * 2, true);
80 assertNull(deliveryQueue.peekNext());
84 public void Given_Delivery_Task_Return_Next_Delivery_Task_Id() throws Exception {
86 when(destInfo.getSpool()).thenReturn(dirPath);
87 deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
88 DeliveryTask nt = deliveryQueue.getNext();
89 assertEquals("10000000000004.fileName", nt.getPublishId());
90 deleteFile(dirPath + fileName);
95 public void Given_Task_In_Todo_Is_Already_Cleaned_GetNext_Returns_Null() throws Exception {
96 when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L);
97 deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
98 Vector<DeliveryTask> tasks = new Vector<>();
99 DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com");
102 FieldUtils.writeField(deliveryQueue, "todo", tasks, true);
103 DeliveryTask nt = deliveryQueue.getNext();
108 public void Given_Task_In_Todo_Has_Resume_Time_In_Future_GetNext_Returns_Null() throws Exception {
109 when(destInfo.isPrivilegedSubscriber()).thenReturn(true);
110 when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L);
111 deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
112 Vector<DeliveryTask> tasks = new Vector<>();
113 DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com");
114 long timeInFuture = 2558366240223L;
115 task.setResumeTime(timeInFuture);
117 FieldUtils.writeField(deliveryQueue, "todo", tasks, true);
118 DeliveryTask nt = deliveryQueue.getNext();
123 public void Given_Task_In_Todo_Is_Expired_GetNext_Returns_Null() throws Exception {
124 when(destInfo.isPrivilegedSubscriber()).thenReturn(true);
125 when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L);
126 deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
127 Vector<DeliveryTask> tasks = new Vector<>();
128 DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com");
129 long timeInPast = 1058366240223L;
130 task.setResumeTime(timeInPast);
132 FieldUtils.writeField(deliveryQueue, "todo", tasks, true);
133 DeliveryTask nt = deliveryQueue.getNext();
138 public void Given_Delivery_Task_Cancel_And_FileId_Is_Null_Return_Zero() {
139 long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
144 public void Given_Delivery_Task_Is_Working_Cancel_Task_Returns_Zero() throws IllegalAccessException {
145 Hashtable<String, DeliveryTask> tasks = new Hashtable<>();
146 tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
147 FieldUtils.writeField(deliveryQueue, "working", tasks, true);
148 long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
153 public void Given_Delivery_Task_In_Todo_Cancel_Task_Returns_Zero() throws IllegalAccessException {
154 Vector<DeliveryTask> tasks = new Vector<>();
155 tasks.add(new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
156 FieldUtils.writeField(deliveryQueue, "todo", tasks, true);
157 long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
162 public void Given_Ok_Status_And_Privileged_Subscriber_Then_Set_Resume_Time_Is_Called_On_DeliveryTask() {
163 DeliveryTask deliveryTask = mockDeliveryTask();
164 deliveryQueue.reportStatus(deliveryTask, 200, "123456789.dmaap-dr-node", "delivery");
165 verify(deliveryTask, times(1)).setResumeTime(anyLong());
170 public void Given_Ok_Status_And_Not_Privileged_Subscriber_Then_Clean_Is_Called_On_DeliveryTask() {
171 DeliveryTask deliveryTask = mockDeliveryTask();
172 when(destInfo.isPrivilegedSubscriber()).thenReturn(false);
173 deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
174 deliveryQueue.reportStatus(deliveryTask, 200, "123456789.dmaap-dr-node", "delivery");
175 verify(deliveryTask, times(1)).clean();
180 public void Given_Not_Ok_Status_Then_Clean_Is_Called_On_DeliveryTask() {
181 DeliveryTask deliveryTask = mockDeliveryTask();
182 deliveryQueue.reportStatus(deliveryTask, 400, "123456789.dmaap-dr-node", "delivery");
183 verify(deliveryTask, times(1)).clean();
188 public void Given_Task_In_Working_MarkTaskSuccess_Returns_True() throws IllegalAccessException {
189 Hashtable<String, DeliveryTask> tasks = new Hashtable<>();
190 tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
191 FieldUtils.writeField(deliveryQueue, "working", tasks, true);
192 assertTrue(deliveryQueue.markTaskSuccess("123.node.datarouternew.com"));
196 public void Given_Task_In_Retry_MarkTaskSuccess_Returns_True() throws IllegalAccessException {
197 Hashtable<String, DeliveryTask> tasks = new Hashtable<>();
198 tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
199 FieldUtils.writeField(deliveryQueue, "retry", tasks, true);
200 assertTrue(deliveryQueue.markTaskSuccess("123.node.datarouternew.com"));
204 public void Given_Task_Does_Not_Exist_MarkTaskSuccess_Returns_False() {
205 assertFalse(deliveryQueue.markTaskSuccess("false.pubId.com"));
208 private void cleanUpLogging() {
209 final File currentDir = new File(System.getProperty("user.dir"));
210 final File[] files = currentDir.listFiles((file, name) -> name.matches("null.*"));
212 for (final File file : files) {
219 private DeliveryTask mockDeliveryTask() {
220 DeliveryTask deliveryTask = mock(DeliveryTask.class);
221 when(deliveryTask.getPublishId()).thenReturn("123456789.dmaap-dr-node");
222 when(deliveryTask.getFeedId()).thenReturn("1");
223 when(deliveryTask.getSubId()).thenReturn("1");
224 when(deliveryTask.getURL()).thenReturn("http://subcriber.com:7070/delivery");
225 when(deliveryTask.getCType()).thenReturn("application/json");
226 when(deliveryTask.getLength()).thenReturn(486L);
230 private NodeConfigManager mockNodeConfigManager() {
231 NodeConfigManager config = mock(NodeConfigManager.class);
232 PowerMockito.when(config.getEventLogInterval()).thenReturn("30000");
236 private void prepareFiles() throws Exception {
237 createFolder(dirPath);
238 createFile(fileName, dirPath);
241 private void createFolder(String dirName) {
242 File newDirectory = new File(dirName);
243 newDirectory.mkdirs();
246 private void createFile(String file, String dir) throws Exception {
247 File newFile = new File(dir + File.separator + file);
248 newFile.createNewFile();
251 private void deleteFile(String fileName) {
252 File file = new File(fileName);