X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-node%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fnode%2FDeliveryTaskTest.java;h=596d56a7ca6681688bd8422f501fd44f8c414aee;hb=adb2ad2d16e851fbf8dcc71af68949a74463204d;hp=3d17e3e368c32cd323d505cd804c570fd196d0e4;hpb=5a55b790e8afa3131fd5f894e5d1b1e036dc4cd1;p=dmaap%2Fdatarouter.git diff --git a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryTaskTest.java b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryTaskTest.java index 3d17e3e3..596d56a7 100644 --- a/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryTaskTest.java +++ b/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/DeliveryTaskTest.java @@ -20,51 +20,108 @@ package org.onap.dmaap.datarouter.node; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - +import java.io.ByteArrayOutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({URL.class}) public class DeliveryTaskTest { @Mock private DeliveryQueue deliveryQueue; + private ExecutorService executorService; + + @Before + public void setUp() throws Exception { + DestInfo destInfo = getPrivDestInfo(); + deliveryQueue = mockDelvieryQueue(destInfo); + + URL url = PowerMockito.mock(URL.class); + HttpURLConnection urlConnection = PowerMockito.mock(HttpURLConnection.class); + + PowerMockito.whenNew(URL.class).withParameterTypes(String.class).withArguments(Mockito.anyString()).thenReturn(url); + + PowerMockito.when(urlConnection.getOutputStream()).thenReturn(new ByteArrayOutputStream()); + PowerMockito.when(urlConnection.getHeaderField(0)).thenReturn("PUT"); + PowerMockito.when(urlConnection.getResponseCode()).thenReturn(200); + PowerMockito.when(url.openConnection()).thenReturn(urlConnection); + } + + @After + public void tearDown() { + } + + @Test public void Validate_Delivery_Task_Equals() { - DestInfo destInfo = getDestInfo(); - deliveryQueue = mockDelvieryQueue(destInfo); - DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-datafile"); - DeliveryTask task2 = new DeliveryTask(deliveryQueue, "123456789.test-dr-datafile"); + DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node"); + DeliveryTask task2 = new DeliveryTask(deliveryQueue, "123456789.test-dr-node"); Assert.assertEquals(task, task2); Assert.assertEquals(task.hashCode(), task2.hashCode()); Assert.assertEquals(task.toString(), task2.toString()); + Assert.assertEquals(task.getPublishId(), task2.getPublishId()); + Assert.assertEquals(task.getSubId(), task2.getSubId()); + Assert.assertEquals(task.getFeedId(), task2.getFeedId()); + Assert.assertEquals(task.getLength(), task2.getLength()); + Assert.assertEquals(task.isCleaned(), task2.isCleaned()); + Assert.assertEquals(task.getDate(), task2.getDate()); + Assert.assertEquals(task.getURL(), task2.getURL()); + Assert.assertEquals(task.getCType(), task2.getCType()); + Assert.assertEquals(task.getMethod(), task2.getMethod()); + Assert.assertEquals(task.getFileId(), task2.getFileId()); + Assert.assertEquals(task.getAttempts(), task2.getAttempts()); + Assert.assertEquals(task.getFollowRedirects(), task2.getFollowRedirects()); Assert.assertEquals(0, task.compareTo(task2)); } @Test - public void Validate_Delivery_Tasks_Not_Equal() { - DestInfo destInfo = getDestInfo(); - deliveryQueue = mockDelvieryQueue(destInfo); + public void Validate_Delivery_Tasks_Success_For_Standard_File() throws InterruptedException { DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node"); - DeliveryTask task2 = new DeliveryTask(deliveryQueue, "123456789.test-dr-datafile"); - Assert.assertNotEquals(task, task2); - Assert.assertNotEquals(0, task.compareTo(task2)); + executorService = Executors.newSingleThreadExecutor(); + executorService.execute(task); + + executorService.shutdown(); + executorService.awaitTermination(2, TimeUnit.SECONDS); + } + + @Test + public void Validate_Delivery_Tasks_Success_For_Compressed_File() throws InterruptedException { + DeliveryTask task = new DeliveryTask(deliveryQueue, "123456789.test-dr-node.gz"); + executorService = Executors.newSingleThreadExecutor(); + executorService.execute(task); + + executorService.shutdown(); + executorService.awaitTermination(2, TimeUnit.SECONDS); } - private DestInfo getDestInfo() { + private DestInfo getPrivDestInfo() { return new DestInfoBuilder().setName("n:" + "dmaap-dr-node") - .setSpool(System.getProperty("user.dir") + "/src/test/resources") - .setSubid("1").setLogdata("n2n-dmaap-dr-node").setUrl("https://dmaap-dr-node:8443/internal/publish") - .setAuthuser("dmaap-dr-node").setAuthentication("Auth").setMetaonly(false).setUse100(true) - .setPrivilegedSubscriber(false).setFollowRedirects(false).setDecompress(false).createDestInfo(); + .setSpool(System.getProperty("user.dir") + "/src/test/resources/delivery_files") + .setSubid("1").setLogdata("n2n-dmaap-dr-node").setUrl("https://dmaap-dr-node:8443/internal/publish") + .setAuthuser("dmaap-dr-node").setAuthentication("Auth").setMetaonly(false).setUse100(true) + .setPrivilegedSubscriber(true).setFollowRedirects(false).setDecompress(true).createDestInfo(); } private DeliveryQueue mockDelvieryQueue(DestInfo destInfo) { - DeliveryQueue mockedDeliveryQueue = mock(DeliveryQueue.class); - when(mockedDeliveryQueue.getDestinationInfo()).thenReturn(destInfo); + DeliveryQueue mockedDeliveryQueue = PowerMockito.mock(DeliveryQueue.class); + PowerMockito.when(mockedDeliveryQueue.getDestinationInfo()).thenReturn(destInfo); + PowerMockito.when(mockedDeliveryQueue.getDestURL(Mockito.anyString())).thenReturn("https://dmaap-dr-node:8443/internal/publish"); return mockedDeliveryQueue; }