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