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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.dmaap.datarouter.node;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
26 import java.io.OutputStream;
27 import java.net.HttpURLConnection;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31 import java.util.concurrent.TimeUnit;
32 import org.junit.After;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.powermock.api.mockito.PowerMockito;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
44 @RunWith(PowerMockRunner.class)
45 @PrepareForTest({DeliveryTask.class})
46 public class DeliveryTaskTest {
49 private DeliveryQueue deliveryQueue;
51 private ExecutorService executorService;
54 public void setUp() throws Exception {
55 DestInfo destInfo = getPrivDestInfo();
56 deliveryQueue = mockDelvieryQueue(destInfo);
58 URL url = PowerMockito.mock(URL.class);
59 HttpURLConnection urlConnection = PowerMockito.mock(HttpURLConnection.class);
60 OutputStream outputStream = PowerMockito.mock(OutputStream.class);
62 PowerMockito.whenNew(URL.class).withParameterTypes(String.class).withArguments(Mockito.anyString())
64 PowerMockito.when(urlConnection.getOutputStream()).thenReturn(outputStream);
65 PowerMockito.when(url.openConnection()).thenReturn(urlConnection);
66 PowerMockito.when(urlConnection.getHeaderField(0)).thenReturn("PUT");
67 PowerMockito.when(urlConnection.getResponseCode()).thenReturn(200);
71 public void tearDown() {
76 public void Validate_Delivery_Task_Equals() {
77 DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-datafile");
78 DeliveryTask task2 = new DeliveryTask(deliveryQueue, "123456789.test-dr-datafile");
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());
95 Assert.assertEquals(0, task.compareTo(task2));
99 public void Validate_Delivery_Tasks_Success_For_Standard_File() throws InterruptedException {
100 DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node");
101 executorService = Executors.newSingleThreadExecutor();
102 executorService.execute(task);
104 executorService.shutdown();
105 executorService.awaitTermination(2, TimeUnit.SECONDS);
109 public void Validate_Delivery_Tasks_Success_For_Compressed_File() throws InterruptedException {
111 DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node.gz");
112 executorService = Executors.newSingleThreadExecutor();
113 executorService.execute(task);
115 executorService.shutdown();
116 executorService.awaitTermination(2, TimeUnit.SECONDS);
119 private DestInfo getPrivDestInfo() {
120 return new DestInfoBuilder().setName("n:" + "dmaap-dr-node")
121 .setSpool(System.getProperty("user.dir") + "/src/test/resources/delivery_files")
122 .setSubid("1").setLogdata("n2n-dmaap-dr-node").setUrl("https://dmaap-dr-node:8443/internal/publish")
123 .setAuthuser("dmaap-dr-node").setAuthentication("Auth").setMetaonly(false).setUse100(true)
124 .setPrivilegedSubscriber(true).setFollowRedirects(false).setDecompress(true).createDestInfo();
127 private DeliveryQueue mockDelvieryQueue(DestInfo destInfo) {
128 DeliveryQueue mockedDeliveryQueue = mock(DeliveryQueue.class);
129 when(mockedDeliveryQueue.getDestinationInfo()).thenReturn(destInfo);
130 return mockedDeliveryQueue;