fa868b26bcf2687649d935097f2c73a1a33eaa3f
[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 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;
32
33 import java.io.File;
34
35 import static org.junit.Assert.*;
36 import static org.mockito.Mockito.when;
37
38 @RunWith(PowerMockRunner.class)
39 public class DeliveryQueueTest {
40
41     private DeliveryQueue deliveryQueue;
42     @Mock
43     private DestInfo destInfo;
44     @Mock
45     DeliveryQueueHelper deliveryQueueHelper;
46
47     private String dirPath = "/tmp/dir001/";
48     private String fileName = "10000000000004.fileName.M";
49
50     @Before
51     public void setUp() {
52         when(destInfo.getSpool()).thenReturn(dirPath);
53         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
54     }
55
56     @Test
57     public void Given_New_DeliveryQueue_Directory_Is_Created_As_Defined_By_DestInfo() {
58         File file = new File("/tmp");
59         assertTrue(file.exists());
60     }
61
62     @Test
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());
67     }
68
69     @Test
70     public void Given_Delivery_Task_Return_Next_Delivery_Task_Id() throws Exception {
71         prepareFiles();
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);
77         deleteFile(dirPath);
78     }
79
80     @Test
81     public void Given_Delivery_Task_Cancel_And_FileId_Is_Null_Return_Zero() {
82         long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
83         assertEquals(0, rc);
84     }
85
86     private void prepareFiles() throws Exception {
87         createFolder(dirPath);
88         createFile(fileName, dirPath);
89     }
90
91     private void createFolder(String dirName) {
92         File newDirectory = new File(dirName);
93         newDirectory.mkdirs();
94     }
95
96     private void createFile(String file, String dir) throws Exception {
97         File newFile = new File(dir + File.separator + file);
98         newFile.createNewFile();
99     }
100
101     private void deleteFile(String fileName) {
102         File file = new File(fileName);
103
104         if (file.exists()) {
105             file.delete();
106         }
107     }
108
109 }