FINAL unit test covergae commit
[dmaap/datarouter.git] / datarouter-node / src / test / java / org / onap / dmaap / datarouter / node / DeliveryTaskTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.datarouter.node;
22
23 import java.io.ByteArrayOutputStream;
24 import java.net.HttpURLConnection;
25 import java.net.URL;
26 import org.junit.After;
27 import org.junit.Assert;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.powermock.api.mockito.PowerMockito;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
35 import org.powermock.modules.junit4.PowerMockRunner;
36
37
38 @RunWith(PowerMockRunner.class)
39 @PrepareForTest({URL.class})
40 public class DeliveryTaskTest {
41
42     @Mock
43     private DeliveryQueue deliveryQueue;
44
45     @Before
46     public void setUp() throws Exception {
47         DestInfo destInfo = getPrivDestInfo();
48         deliveryQueue = mockDelvieryQueue(destInfo);
49
50         URL url = PowerMockito.mock(URL.class);
51         HttpURLConnection urlConnection = PowerMockito.mock(HttpURLConnection.class);
52
53         PowerMockito.whenNew(URL.class).withParameterTypes(String.class).withArguments(Mockito.anyString()).thenReturn(url);
54
55         PowerMockito.when(urlConnection.getOutputStream()).thenReturn(new ByteArrayOutputStream());
56         PowerMockito.when(urlConnection.getHeaderField(0)).thenReturn("PUT");
57         PowerMockito.when(urlConnection.getResponseCode()).thenReturn(200);
58         PowerMockito.when(url.openConnection()).thenReturn(urlConnection);
59     }
60
61     @After
62     public void tearDown() {
63     }
64
65
66     @Test
67     public void Validate_Delivery_Task_Equals() {
68         DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node");
69         DeliveryTask task2 = new DeliveryTask(deliveryQueue, "123456789.test-dr-node");
70         Assert.assertEquals(task, task2);
71         Assert.assertEquals(task.hashCode(), task2.hashCode());
72         Assert.assertEquals(task.toString(), task2.toString());
73         Assert.assertEquals(task.getPublishId(), task2.getPublishId());
74         Assert.assertEquals(task.getSubId(), task2.getSubId());
75         Assert.assertEquals(task.getFeedId(), task2.getFeedId());
76         Assert.assertEquals(task.getLength(), task2.getLength());
77         Assert.assertEquals(task.isCleaned(), task2.isCleaned());
78         Assert.assertEquals(task.getDate(), task2.getDate());
79         Assert.assertEquals(task.getURL(), task2.getURL());
80         Assert.assertEquals(task.getCType(), task2.getCType());
81         Assert.assertEquals(task.getMethod(), task2.getMethod());
82         Assert.assertEquals(task.getFileId(), task2.getFileId());
83         Assert.assertEquals(task.getAttempts(), task2.getAttempts());
84         Assert.assertEquals(task.getFollowRedirects(), task2.getFollowRedirects());
85         Assert.assertEquals(0, task.compareTo(task2));
86     }
87
88     @Test
89     public void Validate_Delivery_Tasks_Success_For_Standard_File() {
90         DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node");
91         task.run();
92     }
93
94     @Test
95     public void Validate_Delivery_Tasks_Success_For_Compressed_File() {
96         DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node.gz");
97         task.run();
98     }
99
100     private DestInfo getPrivDestInfo() {
101         return new DestInfoBuilder().setName("n:" + "dmaap-dr-node")
102                        .setSpool(System.getProperty("user.dir") + "/src/test/resources/delivery_files")
103                        .setSubid("1").setLogdata("n2n-dmaap-dr-node").setUrl("https://dmaap-dr-node:8443/internal/publish")
104                        .setAuthuser("dmaap-dr-node").setAuthentication("Auth").setMetaonly(false).setUse100(true)
105                        .setPrivilegedSubscriber(true).setFollowRedirects(false).setDecompress(true).createDestInfo();
106     }
107
108     private DeliveryQueue mockDelvieryQueue(DestInfo destInfo) {
109         DeliveryQueue mockedDeliveryQueue = PowerMockito.mock(DeliveryQueue.class);
110         PowerMockito.when(mockedDeliveryQueue.getDestinationInfo()).thenReturn(destInfo);
111         PowerMockito.when(mockedDeliveryQueue.getDestURL(Mockito.anyString())).thenReturn("https://dmaap-dr-node:8443/internal/publish");
112         return mockedDeliveryQueue;
113     }
114
115 }