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 FileName1 = "10000000000004.fileName.M";
52 when(destInfo.getSpool()).thenReturn("tmp");
53 deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
57 public void Given_New_DeliveryQueue_Directory_Is_Created_As_Defined_By_DestInfo() {
58 when(destInfo.getSpool()).thenReturn("tmp");
59 File file = new File("tmp");
60 assertTrue(file.exists());
65 public void Given_Delivery_Task_Failed_And_Resume_Time_Not_Reached_Return_Null() throws Exception {
66 FieldUtils.writeField(deliveryQueue,"failed",true,true);
67 FieldUtils.writeField(deliveryQueue,"resumetime",System.currentTimeMillis()*2,true);
68 assertNull(deliveryQueue.peekNext());
72 public void Given_Delivery_Task_Return_Next_Delivery_Task_Id() throws Exception {
74 when(destInfo.getSpool()).thenReturn(dirPath);
75 deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
76 DeliveryTask nt = deliveryQueue.getNext();
77 assertEquals("10000000000004.fileName", nt.getPublishId());
78 deleteFile(dirPath + FileName1);
83 public void Given_Delivery_Task_Cancel_And_FileId_Is_Null_Return_Zero() {
84 long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
88 private void prepareFiles() throws Exception {
89 createFolder(dirPath);
90 createFile(FileName1, dirPath);
91 String[] files = new String[2];
92 files[0] = dirPath + FileName1;
95 private void createFolder(String dirName) throws Exception {
96 String dirPath = dirName;
98 File newDirectory = new File(dirPath);
99 boolean isCreated = newDirectory.mkdirs();
101 System.out.println("1. Successfully created directories, path: " + newDirectory.getCanonicalPath());
102 } else if (newDirectory.exists()) {
103 System.out.print("1. Directory path already exist, path: " + newDirectory.getCanonicalPath());
105 System.out.println("1. Unable to create directory");
109 private void createFile(String file, String dir) throws Exception {
110 String FileName = file;
111 String dirPath = dir;
113 File newFile = new File(dirPath + File.separator + FileName);
114 boolean isCreated = newFile.createNewFile();
116 System.out.println("\n2. Successfully created new file, path: " + newFile.getCanonicalPath());
117 } else { //File may already exist
118 System.out.println("\n2. Unable to create new file: " + newFile.getCanonicalPath());
122 private void deleteFile(String fileName) {
123 File file = new File(fileName);