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 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;
38 @RunWith(PowerMockRunner.class)
39 @PrepareForTest({URL.class})
40 public class DeliveryTaskTest {
43 private DeliveryQueue deliveryQueue;
46 public void setUp() throws Exception {
47 DestInfo destInfo = getPrivDestInfo();
48 deliveryQueue = mockDelvieryQueue(destInfo);
50 URL url = PowerMockito.mock(URL.class);
51 HttpURLConnection urlConnection = PowerMockito.mock(HttpURLConnection.class);
53 PowerMockito.whenNew(URL.class).withParameterTypes(String.class).withArguments(Mockito.anyString()).thenReturn(url);
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);
62 public void tearDown() {
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));
89 public void Validate_Delivery_Tasks_Success_For_Standard_File() {
90 DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node");
95 public void Validate_Delivery_Tasks_Success_For_Compressed_File() {
96 DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node.gz");
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();
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;