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;
25 import org.apache.commons.lang3.reflect.FieldUtils;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.powermock.modules.junit4.PowerMockRunner;
32 import static org.junit.Assert.*;
37 import static org.mockito.Mockito.*;
39 @RunWith(PowerMockRunner.class)
40 public class DeliveryQueueTest {
42 private DeliveryQueue deliveryQueue;
44 private DestInfo destInfo;
46 DeliveryQueueHelper deliveryQueueHelper;
48 private String dirPath = "/tmp/dir001/";
49 private String FileName1 = "10000000000004.fileName.M";
53 when(destInfo.getSpool()).thenReturn("tmp");
54 deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
58 public void Given_New_DeliveryQueue_Directory_Is_Created_As_Defined_By_DestInfo() throws Exception {
59 when(destInfo.getSpool()).thenReturn("tmp");
60 File file = new File("tmp");
61 assertTrue(file.exists());
66 public void Given_Delivery_Task_Failed_And_Resume_Time_Not_Reached_Return_Null() throws Exception{
67 FieldUtils.writeField(deliveryQueue,"failed",true,true);
68 FieldUtils.writeField(deliveryQueue,"resumetime",System.currentTimeMillis()*2,true);
69 assertNull(deliveryQueue.peekNext());
73 public void Given_Delivery_Task_Return_Next_Delivery_Task_Id() throws Exception{
75 when(destInfo.getSpool()).thenReturn(dirPath);
76 deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
77 DeliveryTask nt = deliveryQueue.getNext();
78 assertEquals("10000000000004.fileName", nt.getPublishId());
79 deleteFile(dirPath + FileName1);
84 public void Given_Delivery_Task_Cancel_And_FileId_Is_Null_Return_Zero() throws Exception{
85 long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
89 private void prepareFiles() throws Exception{
90 createFolder(dirPath);
91 createFile(FileName1, dirPath);
92 String[] files = new String[2];
93 files[0] = dirPath + FileName1;
96 private void createFolder(String dirName) throws Exception{
97 String dirPath = dirName;
99 File newDirectory = new File(dirPath);
100 boolean isCreated = newDirectory.mkdirs();
102 System.out.println("1. Successfully created directories, path: " + newDirectory.getCanonicalPath());
103 } else if (newDirectory.exists()) {
104 System.out.printf("1. Directory path already exist, path: " + newDirectory.getCanonicalPath());
106 System.out.println("1. Unable to create directory");
110 private void createFile( String file, String dir) throws Exception{
111 String FileName = file;
112 String dirPath = dir;
114 File newFile = new File(dirPath + File.separator + FileName);
115 boolean isCreated = newFile.createNewFile();
117 System.out.println("\n2. Successfully created new file, path: " + newFile.getCanonicalPath());
118 } else { //File may already exist
119 System.out.println("\n2. Unable to create new file: " + newFile.getCanonicalPath());
123 private void deleteFile(String fileName) {
124 File file = new File(fileName);