97904a5e7f664ed6e64f50309d0d50e571bb60f2
[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 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;
31
32 import static org.junit.Assert.*;
33 import java.io.File;
34
35
36
37 import static org.mockito.Mockito.*;
38
39 @RunWith(PowerMockRunner.class)
40 public class DeliveryQueueTest {
41
42     private DeliveryQueue deliveryQueue;
43     @Mock
44     private DestInfo destInfo;
45     @Mock
46     DeliveryQueueHelper deliveryQueueHelper;
47
48     private String dirPath = "/tmp/dir001/";
49     private String FileName1 = "10000000000004.fileName.M";
50
51     @Before
52     public void setUp() {
53         when(destInfo.getSpool()).thenReturn("tmp");
54         deliveryQueue = new DeliveryQueue(deliveryQueueHelper, destInfo);
55     }
56
57     @Test
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());
62         deleteFile("tmp");
63     }
64
65     @Test
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());
70     }
71
72     @Test
73     public void Given_Delivery_Task_Return_Next_Delivery_Task_Id() throws Exception{
74         prepareFiles();
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);
80         deleteFile(dirPath);
81     }
82
83     @Test
84     public void Given_Delivery_Task_Cancel_And_FileId_Is_Null_Return_Zero() throws Exception{
85         long rc = deliveryQueue.cancelTask("123.node.datarouternew.com");
86         assertEquals(0, rc);
87     }
88
89     private void prepareFiles() throws Exception{
90         createFolder(dirPath);
91         createFile(FileName1, dirPath);
92         String[] files = new String[2];
93         files[0] = dirPath + FileName1;
94     }
95
96     private void createFolder(String dirName) throws Exception{
97         String dirPath = dirName;
98
99         File newDirectory = new File(dirPath);
100         boolean isCreated = newDirectory.mkdirs();
101         if (isCreated) {
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());
105         } else {
106             System.out.println("1. Unable to create directory");
107         }
108     }
109
110     private void createFile( String file, String dir) throws Exception{
111         String FileName = file;
112         String dirPath = dir;
113
114         File newFile = new File(dirPath + File.separator + FileName);
115         boolean isCreated = newFile.createNewFile();
116         if (isCreated) {
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());
120         }
121     }
122
123     private void deleteFile(String fileName) {
124         File file = new File(fileName);
125
126         if (file.exists()) {
127             file.delete();
128         }
129     }
130
131 }