aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryQueueTest.java
blob: ea221cbda7a23bb55de234f196eea50a408b79f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*******************************************************************************
 * ============LICENSE_START==================================================
 * * org.onap.dmaap
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * *
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/

package org.onap.dmaap.datarouter.node;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.onap.dmaap.datarouter.node.delivery.DeliveryQueue;
import org.onap.dmaap.datarouter.node.delivery.DeliveryQueueHelper;
import org.onap.dmaap.datarouter.node.delivery.DeliveryTask;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor("org.onap.dmaap.datarouter.node.NodeConfigManager")
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"})
public class DeliveryQueueTest {

    @Mock
    DeliveryQueueHelper deliveryQueueHelper;
    private DeliveryQueue deliveryQueue;
    @Mock
    private DestInfo destInfo;
    private String dirPath = "/tmp/dir001/";
    private String fileName = "10000000000004.fileName.M";

    @Before
    public void setUp() throws IllegalAccessException {
        when(destInfo.getSpool()).thenReturn(dirPath);
        when(destInfo.isPrivilegedSubscriber()).thenReturn(true);
        deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
        NodeConfigManager configManager = mockNodeConfigManager();
        FieldUtils.writeDeclaredStaticField(NodeConfigManager.class, "base", configManager, true);
    }

    @Test
    public void Given_New_DeliveryQueue_Directory_Is_Created_As_Defined_By_DestInfo() {
        File file = new File("/tmp");
        assertTrue(file.exists());
    }

    @Test
    public void Given_Delivery_Task_Failed_And_Resume_Time_Not_Reached_Return_Null() throws Exception {
        FieldUtils.writeField(deliveryQueue, "failed", true, true);
        FieldUtils.writeField(deliveryQueue, "resumetime", System.currentTimeMillis() * 2, true);
        assertNull(deliveryQueue.peekNext());
    }

    @Test
    public void Given_Delivery_Task_Return_Next_Delivery_Task_Id() throws Exception {
        prepareFiles();
        when(destInfo.getSpool()).thenReturn(dirPath);
        deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
        DeliveryTask nt = deliveryQueue.getNext();
        assertEquals("10000000000004.fileName", nt.getPublishId());
        deleteFile(dirPath + fileName);
        deleteFile(dirPath);
    }

    @Test
    public void Given_Task_In_Todo_Is_Already_Cleaned_GetNext_Returns_Null() throws Exception {
        when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L);
        deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
        Vector<DeliveryTask> tasks = new Vector<>();
        DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com");
        task.clean();
        tasks.add(task);
        FieldUtils.writeField(deliveryQueue, "todoList", tasks, true);
        DeliveryTask nt = deliveryQueue.getNext();
        assertNull(nt);
    }

    @Test
    public void Given_Task_In_Todo_Has_Resume_Time_In_Future_GetNext_Returns_Null() throws Exception {
        when(destInfo.isPrivilegedSubscriber()).thenReturn(true);
        when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L);
        deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
        Vector<DeliveryTask> tasks = new Vector<>();
        DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com");
        long timeInFuture = 2558366240223L;
        task.setResumeTime(timeInFuture);
        tasks.add(task);
        FieldUtils.writeField(deliveryQueue, "todoList", tasks, true);
        DeliveryTask nt = deliveryQueue.getNext();
        assertNull(nt);
    }

    @Test
    public void Given_Task_In_Todo_Is_Expired_GetNext_Returns_Null() throws Exception {
        when(destInfo.isPrivilegedSubscriber()).thenReturn(true);
        when(deliveryQueueHelper.getExpirationTimer()).thenReturn(10000L);
        deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
        Vector<DeliveryTask> tasks = new Vector<>();
        DeliveryTask task = new DeliveryTask(deliveryQueue, "123.node.datarouternew.com");
        long timeInPast = 1058366240223L;
        task.setResumeTime(timeInPast);
        tasks.add(task);
        FieldUtils.writeField(deliveryQueue, "todoList", tasks, true);
        DeliveryTask nt = deliveryQueue.getNext();
        assertNull(nt);
    }

    @Test
    public void Given_Delivery_Task_Cancel_And_FileId_Is_Null_Return_Zero() {
        long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
        assertEquals(0, rc);
    }

    @Test
    public void Given_Delivery_Task_Is_Working_Cancel_Task_Returns_Zero() throws IllegalAccessException {
        HashMap<String, DeliveryTask> tasks = new HashMap<>();
        tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
        FieldUtils.writeField(deliveryQueue, "working", tasks, true);
        long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
        assertEquals(0, rc);
    }

    @Test
    public void Given_Delivery_Task_In_Todo_Cancel_Task_Returns_Zero() throws IllegalAccessException {
        List<DeliveryTask> tasks = new ArrayList<>();
        tasks.add(new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
        FieldUtils.writeField(deliveryQueue, "todoList", tasks, true);
        long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
        assertEquals(0, rc);
    }

    @Test
    public void Given_Ok_Status_And_Privileged_Subscriber_Then_Set_Resume_Time_Is_Called_On_DeliveryTask() {
        DeliveryTask deliveryTask = mockDeliveryTask();
        deliveryQueue.reportStatus(deliveryTask, 200, "123456789.dmaap-dr-node", "delivery");
        verify(deliveryTask, times(1)).setResumeTime(anyLong());
        cleanUpLogging();
    }

    @Test
    public void Given_Ok_Status_And_Not_Privileged_Subscriber_Then_Clean_Is_Called_On_DeliveryTask() {
        DeliveryTask deliveryTask = mockDeliveryTask();
        when(destInfo.isPrivilegedSubscriber()).thenReturn(false);
        deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
        deliveryQueue.reportStatus(deliveryTask, 200, "123456789.dmaap-dr-node", "delivery");
        verify(deliveryTask, times(1)).clean();
        cleanUpLogging();
    }

    @Test
    public void Given_Not_Ok_Status_Then_Clean_Is_Called_On_DeliveryTask() {
        DeliveryTask deliveryTask = mockDeliveryTask();
        deliveryQueue.reportStatus(deliveryTask, 400, "123456789.dmaap-dr-node", "delivery");
        verify(deliveryTask, times(1)).clean();
        cleanUpLogging();
    }

    @Test
    public void Given_Task_In_Working_MarkTaskSuccess_Returns_True() throws IllegalAccessException {
        HashMap<String, DeliveryTask> tasks = new HashMap<>();
        tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
        FieldUtils.writeField(deliveryQueue, "working", tasks, true);
        assertTrue(deliveryQueue.markTaskSuccess("123.node.datarouternew.com"));
    }

    @Test
    public void Given_Task_In_Retry_MarkTaskSuccess_Returns_True() throws IllegalAccessException {
        HashMap<String, DeliveryTask> tasks = new HashMap<>();
        tasks.put("123.node.datarouternew.com", new DeliveryTask(deliveryQueue, "123.node.datarouternew.com"));
        FieldUtils.writeField(deliveryQueue, "retry", tasks, true);
        assertTrue(deliveryQueue.markTaskSuccess("123.node.datarouternew.com"));
    }

    @Test
    public void Given_Task_Does_Not_Exist_MarkTaskSuccess_Returns_False() {
        assertFalse(deliveryQueue.markTaskSuccess("false.pubId.com"));
    }

    private void cleanUpLogging() {
        final File currentDir = new File(System.getProperty("user.dir"));
        final File[] files = currentDir.listFiles((file, name) -> name.matches("null.*"));
        if (files != null) {
            for (final File file : files) {
                file.delete();
            }
        }
    }

    @NotNull
    private DeliveryTask mockDeliveryTask() {
        DeliveryTask deliveryTask = mock(DeliveryTask.class);
        when(deliveryTask.getPublishId()).thenReturn("123456789.dmaap-dr-node");
        when(deliveryTask.getFeedId()).thenReturn("1");
        when(deliveryTask.getSubId()).thenReturn("1");
        when(deliveryTask.getURL()).thenReturn("http://subcriber.com:7070/delivery");
        when(deliveryTask.getCType()).thenReturn("application/json");
        when(deliveryTask.getLength()).thenReturn(486L);
        return deliveryTask;
    }

    private NodeConfigManager mockNodeConfigManager() {
        NodeConfigManager config = mock(NodeConfigManager.class);
        PowerMockito.when(config.getEventLogInterval()).thenReturn("30000");
        return config;
    }

    private void prepareFiles() throws Exception {
        createFolder(dirPath);
        createFile(fileName, dirPath);
    }

    private void createFolder(String dirName) {
        File newDirectory = new File(dirName);
        newDirectory.mkdirs();
    }

    private void createFile(String file, String dir) throws Exception {
        File newFile = new File(dir + File.separator + file);
        newFile.createNewFile();
    }

    private void deleteFile(String fileName) {
        File file = new File(fileName);

        if (file.exists()) {
            file.delete();
        }
    }

}