[DMAAP-DR] Remove cadi/aaf from dr-node
[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.List;
40 import java.util.Vector;
41 import org.apache.commons.lang3.reflect.FieldUtils;
42 import org.jetbrains.annotations.NotNull;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mock;
47 import org.onap.dmaap.datarouter.node.delivery.DeliveryQueue;
48 import org.onap.dmaap.datarouter.node.delivery.DeliveryQueueHelper;
49 import org.onap.dmaap.datarouter.node.delivery.DeliveryTask;
50 import org.powermock.api.mockito.PowerMockito;
51 import org.powermock.core.classloader.annotations.PowerMockIgnore;
52 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
53 import org.powermock.modules.junit4.PowerMockRunner;
54
55 @RunWith(PowerMockRunner.class)
56 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeConfigManager")
57 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"})
58 public class DeliveryQueueTest {
59
60     @Mock
61     DeliveryQueueHelper deliveryQueueHelper;
62     private DeliveryQueue deliveryQueue;
63     @Mock
64     private DestInfo destInfo;
65     private String dirPath = "/tmp/dir001/";
66     private String fileName = "10000000000004.fileName.M";
67
68     @Before
69     public void setUp() throws IllegalAccessException {
70         when(destInfo.getSpool()).thenReturn(dirPath);
71         when(destInfo.isPrivilegedSubscriber()).thenReturn(true);
72         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
73         NodeConfigManager configManager = mockNodeConfigManager();
74         FieldUtils.writeDeclaredStaticField(NodeConfigManager.class, "base", configManager, true);
75     }
76
77     @Test
78     public void Given_New_DeliveryQueue_Directory_Is_Created_As_Defined_By_DestInfo() {
79         File file = new File("/tmp");
80         assertTrue(file.exists());
81     }
82
83     @Test
84     public void Given_Delivery_Task_Failed_And_Resume_Time_Not_Reached_Return_Null() throws Exception {
85         FieldUtils.writeField(deliveryQueue, "failed", true, true);
86         FieldUtils.writeField(deliveryQueue, "resumetime", System.currentTimeMillis() * 2, true);
87         assertNull(deliveryQueue.peekNext());
88     }
89
90     @Test
91     public void Given_Delivery_Task_Return_Next_Delivery_Task_Id() throws Exception {
92         prepareFiles();
93         when(destInfo.getSpool()).thenReturn(dirPath);
94         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
95         DeliveryTask nt = deliveryQueue.getNext();
96         assertEquals("10000000000004.fileName", nt.getPublishId());
97         deleteFile(dirPath + fileName);
98         deleteFile(dirPath);
99     }
100
101     @Test
102     public void Given_Task_In_Todo_Is_Already_Cleaned_GetNext_Returns_Null() throws Exception {
103         when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L);
104         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
105         Vector<DeliveryTask> tasks = new Vector<>();
106         DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com");
107         task.clean();
108         tasks.add(task);
109         FieldUtils.writeField(deliveryQueue, "todoList", tasks, true);
110         DeliveryTask nt = deliveryQueue.getNext();
111         assertNull(nt);
112     }
113
114     @Test
115     public void Given_Task_In_Todo_Has_Resume_Time_In_Future_GetNext_Returns_Null() throws Exception {
116         when(destInfo.isPrivilegedSubscriber()).thenReturn(true);
117         when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L);
118         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
119         Vector<DeliveryTask> tasks = new Vector<>();
120         DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com");
121         long timeInFuture = 2558366240223L;
122         task.setResumeTime(timeInFuture);
123         tasks.add(task);
124         FieldUtils.writeField(deliveryQueue, "todoList", tasks, true);
125         DeliveryTask nt = deliveryQueue.getNext();
126         assertNull(nt);
127     }
128
129     @Test
130     public void Given_Task_In_Todo_Is_Expired_GetNext_Returns_Null() throws Exception {
131         when(destInfo.isPrivilegedSubscriber()).thenReturn(true);
132         when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L);
133         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
134         Vector<DeliveryTask> tasks = new Vector<>();
135         DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com");
136         long timeInPast = 1058366240223L;
137         task.setResumeTime(timeInPast);
138         tasks.add(task);
139         FieldUtils.writeField(deliveryQueue, "todoList", tasks, true);
140         DeliveryTask nt = deliveryQueue.getNext();
141         assertNull(nt);
142     }
143
144     @Test
145     public void Given_Delivery_Task_Cancel_And_FileId_Is_Null_Return_Zero() {
146         long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
147         assertEquals(0, rc);
148     }
149
150     @Test
151     public void Given_Delivery_Task_Is_Working_Cancel_Task_Returns_Zero() throws IllegalAccessException {
152         HashMap<String, DeliveryTask> tasks = new HashMap<>();
153         tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
154         FieldUtils.writeField(deliveryQueue, "working", tasks, true);
155         long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
156         assertEquals(0, rc);
157     }
158
159     @Test
160     public void Given_Delivery_Task_In_Todo_Cancel_Task_Returns_Zero() throws IllegalAccessException {
161         List<DeliveryTask> tasks = new ArrayList<>();
162         tasks.add(new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
163         FieldUtils.writeField(deliveryQueue, "todoList", tasks, true);
164         long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
165         assertEquals(0, rc);
166     }
167
168     @Test
169     public void Given_Ok_Status_And_Privileged_Subscriber_Then_Set_Resume_Time_Is_Called_On_DeliveryTask() {
170         DeliveryTask deliveryTask = mockDeliveryTask();
171         deliveryQueue.reportStatus(deliveryTask, 200, "123456789.dmaap-dr-node", "delivery");
172         verify(deliveryTask, times(1)).setResumeTime(anyLong());
173         cleanUpLogging();
174     }
175
176     @Test
177     public void Given_Ok_Status_And_Not_Privileged_Subscriber_Then_Clean_Is_Called_On_DeliveryTask() {
178         DeliveryTask deliveryTask = mockDeliveryTask();
179         when(destInfo.isPrivilegedSubscriber()).thenReturn(false);
180         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
181         deliveryQueue.reportStatus(deliveryTask, 200, "123456789.dmaap-dr-node", "delivery");
182         verify(deliveryTask, times(1)).clean();
183         cleanUpLogging();
184     }
185
186     @Test
187     public void Given_Not_Ok_Status_Then_Clean_Is_Called_On_DeliveryTask() {
188         DeliveryTask deliveryTask = mockDeliveryTask();
189         deliveryQueue.reportStatus(deliveryTask, 400, "123456789.dmaap-dr-node", "delivery");
190         verify(deliveryTask, times(1)).clean();
191         cleanUpLogging();
192     }
193
194     @Test
195     public void Given_Task_In_Working_MarkTaskSuccess_Returns_True() throws IllegalAccessException {
196         HashMap<String, DeliveryTask> tasks = new HashMap<>();
197         tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
198         FieldUtils.writeField(deliveryQueue, "working", tasks, true);
199         assertTrue(deliveryQueue.markTaskSuccess("123.node.datarouternew.com"));
200     }
201
202     @Test
203     public void Given_Task_In_Retry_MarkTaskSuccess_Returns_True() throws IllegalAccessException {
204         HashMap<String, DeliveryTask> tasks = new HashMap<>();
205         tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
206         FieldUtils.writeField(deliveryQueue, "retry", tasks, true);
207         assertTrue(deliveryQueue.markTaskSuccess("123.node.datarouternew.com"));
208     }
209
210     @Test
211     public void Given_Task_Does_Not_Exist_MarkTaskSuccess_Returns_False() {
212         assertFalse(deliveryQueue.markTaskSuccess("false.pubId.com"));
213     }
214
215     private void cleanUpLogging() {
216         final File currentDir = new File(System.getProperty("user.dir"));
217         final File[] files = currentDir.listFiles((file, name) -> name.matches("null.*"));
218         if (files != null) {
219             for (final File file : files) {
220                 file.delete();
221             }
222         }
223     }
224
225     @NotNull
226     private DeliveryTask mockDeliveryTask() {
227         DeliveryTask deliveryTask = mock(DeliveryTask.class);
228         when(deliveryTask.getPublishId()).thenReturn("123456789.dmaap-dr-node");
229         when(deliveryTask.getFeedId()).thenReturn("1");
230         when(deliveryTask.getSubId()).thenReturn("1");
231         when(deliveryTask.getURL()).thenReturn("http://subcriber.com:7070/delivery");
232         when(deliveryTask.getCType()).thenReturn("application/json");
233         when(deliveryTask.getLength()).thenReturn(486L);
234         return deliveryTask;
235     }
236
237     private NodeConfigManager mockNodeConfigManager() {
238         NodeConfigManager config = mock(NodeConfigManager.class);
239         PowerMockito.when(config.getEventLogInterval()).thenReturn("30000");
240         return config;
241     }
242
243     private void prepareFiles() throws Exception {
244         createFolder(dirPath);
245         createFile(fileName, dirPath);
246     }
247
248     private void createFolder(String dirName) {
249         File newDirectory = new File(dirName);
250         newDirectory.mkdirs();
251     }
252
253     private void createFile(String file, String dir) throws Exception {
254         File newFile = new File(dir + File.separator + file);
255         newFile.createNewFile();
256     }
257
258     private void deleteFile(String fileName) {
259         File file = new File(fileName);
260
261         if (file.exists()) {
262             file.delete();
263         }
264     }
265
266 }