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 java.io.ByteArrayOutputStream;
24 import java.net.HttpURLConnection;
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.PowerMockIgnore;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
42 @RunWith(PowerMockRunner.class)
43 @PrepareForTest({URL.class})
44 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"})
45 public class DeliveryTaskTest {
48 private DeliveryQueue deliveryQueue;
50 private ExecutorService executorService;
53 public void setUp() throws Exception {
54 DestInfo destInfo = getPrivDestInfo();
55 deliveryQueue = mockDelvieryQueue(destInfo);
57 URL url = PowerMockito.mock(URL.class);
58 HttpURLConnection urlConnection = PowerMockito.mock(HttpURLConnection.class);
60 PowerMockito.whenNew(URL.class).withParameterTypes(String.class).withArguments(Mockito.anyString()).thenReturn(url);
62 PowerMockito.when(urlConnection.getOutputStream()).thenReturn(new ByteArrayOutputStream());
63 PowerMockito.when(urlConnection.getHeaderField(0)).thenReturn("PUT");
64 PowerMockito.when(urlConnection.getResponseCode()).thenReturn(200);
65 PowerMockito.when(url.openConnection()).thenReturn(urlConnection);
69 public void tearDown() {
74 public void Validate_Delivery_Task_Equals() {
75 DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node");
76 DeliveryTask task2 = new DeliveryTask(deliveryQueue, "123456789.test-dr-node");
77 Assert.assertEquals(task, task2);
78 Assert.assertEquals(task.hashCode(), task2.hashCode());
79 Assert.assertEquals(task.toString(), task2.toString());
80 Assert.assertEquals(task.getPublishId(), task2.getPublishId());
81 Assert.assertEquals(task.getSubId(), task2.getSubId());
82 Assert.assertEquals(task.getFeedId(), task2.getFeedId());
83 Assert.assertEquals(task.getLength(), task2.getLength());
84 Assert.assertEquals(task.isCleaned(), task2.isCleaned());
85 Assert.assertEquals(task.getDate(), task2.getDate());
86 Assert.assertEquals(task.getURL(), task2.getURL());
87 Assert.assertEquals(task.getCType(), task2.getCType());
88 Assert.assertEquals(task.getMethod(), task2.getMethod());
89 Assert.assertEquals(task.getFileId(), task2.getFileId());
90 Assert.assertEquals(task.getAttempts(), task2.getAttempts());
91 Assert.assertEquals(task.getFollowRedirects(), task2.getFollowRedirects());
92 Assert.assertEquals(0, task.compareTo(task2));
96 public void Validate_Delivery_Tasks_Success_For_Standard_File() throws InterruptedException {
97 DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node");
98 executorService = Executors.newSingleThreadExecutor();
99 executorService.execute(task);
101 executorService.shutdown();
102 executorService.awaitTermination(2, TimeUnit.SECONDS);
106 public void Validate_Delivery_Tasks_Success_For_Compressed_File() throws InterruptedException {
107 DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node.gz");
108 executorService = Executors.newSingleThreadExecutor();
109 executorService.execute(task);
111 executorService.shutdown();
112 executorService.awaitTermination(2, TimeUnit.SECONDS);
115 private DestInfo getPrivDestInfo() {
116 return new DestInfoBuilder().setName("n:" + "dmaap-dr-node")
117 .setSpool(System.getProperty("user.dir") + "/src/test/resources/delivery_files")
118 .setSubid("1").setLogdata("n2n-dmaap-dr-node").setUrl("https://dmaap-dr-node:8443/internal/publish")
119 .setAuthuser("dmaap-dr-node").setAuthentication("Auth").setMetaonly(false).setUse100(true)
120 .setPrivilegedSubscriber(true).setFollowRedirects(false).setDecompress(true).createDestInfo();
123 private DeliveryQueue mockDelvieryQueue(DestInfo destInfo) {
124 DeliveryQueue mockedDeliveryQueue = PowerMockito.mock(DeliveryQueue.class);
125 PowerMockito.when(mockedDeliveryQueue.getDestinationInfo()).thenReturn(destInfo);
126 PowerMockito.when(mockedDeliveryQueue.getDestURL(Mockito.anyString())).thenReturn("https://dmaap-dr-node:8443/internal/publish");
127 return mockedDeliveryQueue;