596d56a7ca6681688bd8422f501fd44f8c414aee
[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 java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.TimeUnit;
29 import org.junit.After;
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.PrepareForTest;
38 import org.powermock.modules.junit4.PowerMockRunner;
39
40
41 @RunWith(PowerMockRunner.class)
42 @PrepareForTest({URL.class})
43 public class DeliveryTaskTest {
44
45     @Mock
46     private DeliveryQueue deliveryQueue;
47
48     private ExecutorService executorService;
49
50     @Before
51     public void setUp() throws Exception {
52         DestInfo destInfo = getPrivDestInfo();
53         deliveryQueue = mockDelvieryQueue(destInfo);
54
55         URL url = PowerMockito.mock(URL.class);
56         HttpURLConnection urlConnection = PowerMockito.mock(HttpURLConnection.class);
57
58         PowerMockito.whenNew(URL.class).withParameterTypes(String.class).withArguments(Mockito.anyString()).thenReturn(url);
59
60         PowerMockito.when(urlConnection.getOutputStream()).thenReturn(new ByteArrayOutputStream());
61         PowerMockito.when(urlConnection.getHeaderField(0)).thenReturn("PUT");
62         PowerMockito.when(urlConnection.getResponseCode()).thenReturn(200);
63         PowerMockito.when(url.openConnection()).thenReturn(urlConnection);
64     }
65
66     @After
67     public void tearDown() {
68     }
69
70
71     @Test
72     public void Validate_Delivery_Task_Equals() {
73         DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node");
74         DeliveryTask task2 = new DeliveryTask(deliveryQueue, "123456789.test-dr-node");
75         Assert.assertEquals(task, task2);
76         Assert.assertEquals(task.hashCode(), task2.hashCode());
77         Assert.assertEquals(task.toString(), task2.toString());
78         Assert.assertEquals(task.getPublishId(), task2.getPublishId());
79         Assert.assertEquals(task.getSubId(), task2.getSubId());
80         Assert.assertEquals(task.getFeedId(), task2.getFeedId());
81         Assert.assertEquals(task.getLength(), task2.getLength());
82         Assert.assertEquals(task.isCleaned(), task2.isCleaned());
83         Assert.assertEquals(task.getDate(), task2.getDate());
84         Assert.assertEquals(task.getURL(), task2.getURL());
85         Assert.assertEquals(task.getCType(), task2.getCType());
86         Assert.assertEquals(task.getMethod(), task2.getMethod());
87         Assert.assertEquals(task.getFileId(), task2.getFileId());
88         Assert.assertEquals(task.getAttempts(), task2.getAttempts());
89         Assert.assertEquals(task.getFollowRedirects(), task2.getFollowRedirects());
90         Assert.assertEquals(0, task.compareTo(task2));
91     }
92
93     @Test
94     public void Validate_Delivery_Tasks_Success_For_Standard_File() throws InterruptedException {
95         DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node");
96         executorService = Executors.newSingleThreadExecutor();
97         executorService.execute(task);
98
99         executorService.shutdown();
100         executorService.awaitTermination(2, TimeUnit.SECONDS);
101     }
102
103     @Test
104     public void Validate_Delivery_Tasks_Success_For_Compressed_File() throws InterruptedException {
105         DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node.gz");
106         executorService = Executors.newSingleThreadExecutor();
107         executorService.execute(task);
108
109         executorService.shutdown();
110         executorService.awaitTermination(2, TimeUnit.SECONDS);
111     }
112
113     private DestInfo getPrivDestInfo() {
114         return new DestInfoBuilder().setName("n:" + "dmaap-dr-node")
115                        .setSpool(System.getProperty("user.dir") + "/src/test/resources/delivery_files")
116                        .setSubid("1").setLogdata("n2n-dmaap-dr-node").setUrl("https://dmaap-dr-node:8443/internal/publish")
117                        .setAuthuser("dmaap-dr-node").setAuthentication("Auth").setMetaonly(false).setUse100(true)
118                        .setPrivilegedSubscriber(true).setFollowRedirects(false).setDecompress(true).createDestInfo();
119     }
120
121     private DeliveryQueue mockDelvieryQueue(DestInfo destInfo) {
122         DeliveryQueue mockedDeliveryQueue = PowerMockito.mock(DeliveryQueue.class);
123         PowerMockito.when(mockedDeliveryQueue.getDestinationInfo()).thenReturn(destInfo);
124         PowerMockito.when(mockedDeliveryQueue.getDestURL(Mockito.anyString())).thenReturn("https://dmaap-dr-node:8443/internal/publish");
125         return mockedDeliveryQueue;
126     }
127
128 }