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 org.apache.commons.lang3.reflect.FieldUtils;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.powermock.modules.junit4.PowerMockRunner;
35 import static org.junit.Assert.*;
36 import static org.mockito.Mockito.when;
38 @RunWith(PowerMockRunner.class)
39 public class DeliveryQueueTest {
41 private DeliveryQueue deliveryQueue;
43 private DestInfo destInfo;
45 DeliveryQueueHelper deliveryQueueHelper;
47 private String dirPath = "/tmp/dir001/";
48 private String fileName = "10000000000004.fileName.M";
52 when(destInfo.getSpool()).thenReturn(dirPath);
53 deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
57 public void Given_New_DeliveryQueue_Directory_Is_Created_As_Defined_By_DestInfo() {
58 File file = new File("/tmp");
59 assertTrue(file.exists());
63 public void Given_Delivery_Task_Failed_And_Resume_Time_Not_Reached_Return_Null() throws Exception {
64 FieldUtils.writeField(deliveryQueue,"failed",true,true);
65 FieldUtils.writeField(deliveryQueue,"resumetime",System.currentTimeMillis()*2,true);
66 assertNull(deliveryQueue.peekNext());
70 public void Given_Delivery_Task_Return_Next_Delivery_Task_Id() throws Exception {
72 when(destInfo.getSpool()).thenReturn(dirPath);
73 deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
74 DeliveryTask nt = deliveryQueue.getNext();
75 assertEquals("10000000000004.fileName", nt.getPublishId());
76 deleteFile(dirPath + fileName);
81 public void Given_Delivery_Task_Cancel_And_FileId_Is_Null_Return_Zero() {
82 long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
86 private void prepareFiles() throws Exception {
87 createFolder(dirPath);
88 createFile(fileName, dirPath);
91 private void createFolder(String dirName) {
92 File newDirectory = new File(dirName);
93 newDirectory.mkdirs();
96 private void createFile(String file, String dir) throws Exception {
97 File newFile = new File(dir + File.separator + file);
98 newFile.createNewFile();
101 private void deleteFile(String fileName) {
102 File file = new File(fileName);