6dc334fcabde1d05d4d14af134510eae28b394c5
[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 FileName1 = "10000000000004.fileName.M";
49
50     @Before
51     public void setUp() {
52         when(destInfo.getSpool()).thenReturn("tmp");
53         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
54     }
55
56     @Test
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());
61         deleteFile("tmp");
62     }
63
64     @Test
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());
69     }
70
71     @Test
72     public void Given_Delivery_Task_Return_Next_Delivery_Task_Id() throws Exception {
73         prepareFiles();
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);
79         deleteFile(dirPath);
80     }
81
82     @Test
83     public void Given_Delivery_Task_Cancel_And_FileId_Is_Null_Return_Zero() {
84         long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
85         assertEquals(0, rc);
86     }
87
88     private void prepareFiles() throws Exception {
89         createFolder(dirPath);
90         createFile(FileName1, dirPath);
91         String[] files = new String[2];
92         files[0] = dirPath + FileName1;
93     }
94
95     private void createFolder(String dirName) {
96         File newDirectory = new File(dirName);
97         newDirectory.mkdirs();
98     }
99
100     private void createFile(String file, String dir) throws Exception {
101         File newFile = new File(dir + File.separator + file);
102         newFile.createNewFile();
103     }
104
105     private void deleteFile(String fileName) {
106         File file = new File(fileName);
107
108         if (file.exists()) {
109             file.delete();
110         }
111     }
112
113 }