package org.sagebionetworks.repo.manager.asynch;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Collections;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sagebionetworks.repo.model.asynch.AsynchronousJobStatus;
import org.sagebionetworks.repo.model.dbo.asynch.AsynchJobType;
import org.sagebionetworks.repo.model.table.TableUpdateTransactionRequest;
import org.sagebionetworks.repo.model.table.UploadToTableRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.amazonaws.services.sqs.model.Message;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-context.xml" })
public class AsynchJobQueuePublisherImplTest {
	
	private static long MAX_WAIT = 1000*60;
	
	@Autowired
	AsynchJobQueuePublisher asynchJobQueuePublisher;
	
	@Before
	public void before(){
		// Start with empty queues
		asynchJobQueuePublisher.emptyAllQueues();
	}
	
	@Test
	public void testPublishRoundTrip() throws Exception{
		AsynchronousJobStatus status = new AsynchronousJobStatus();
		status.setJobId("123");
		UploadToTableRequest uploadToTableRequest = new UploadToTableRequest();
		uploadToTableRequest.setTableId("syn8786");
		uploadToTableRequest.setUploadFileHandleId("333");
		TableUpdateTransactionRequest body = new TableUpdateTransactionRequest();
		body.setChanges(Collections.singletonList(uploadToTableRequest));
		status.setRequestBody(body);
		// publish it
		asynchJobQueuePublisher.publishMessage(status);
		// There should be one message on the queue
		Message message = waitForOneMessage();
		assertNotNull(message);
		assertEquals(status.getJobId(), message.getBody());
		// Delete the message
		asynchJobQueuePublisher.deleteMessage(AsynchJobType.TABLE_UPDATE_TRANSACTION, message);
	}

	/**
	 * @return
	 * @throws InterruptedException 
	 */
	public Message waitForOneMessage() throws InterruptedException {
		long start = System.currentTimeMillis();
		while(true){
			Message message = asynchJobQueuePublisher.recieveOneMessage(AsynchJobType.TABLE_UPDATE_TRANSACTION);
			if(message != null){
				return message;
			}else{
				assertTrue("Timed out waiting for message",System.currentTimeMillis() - start < MAX_WAIT);
				System.out.println("Waiting for message...");
				Thread.sleep(1000L);
			}
		}
	}

}

@@ -137,51 +137,8 @@
 ull;
-%0Aimport static org.junit.Assert.assertTrue;
 %0A%0Aim
@@ -183,22 +183,38 @@
 g.junit.
+jupiter.api.
 Before
+Each
 ;%0Aimport
@@ -224,16 +224,28 @@
 g.junit.
+jupiter.api.
 Test;%0Aim
@@ -263,18 +263,36 @@
 nit.
-runner.Run
+jupiter.api.extension.Extend
 With
@@ -558,32 +558,113 @@
 ToTableRequest;%0A
+import org.sagebionetworks.util.Pair;%0Aimport org.sagebionetworks.util.TimeUtils;%0A
 import org.sprin
@@ -821,33 +821,32 @@
 unit
-4.SpringJUnit4ClassRunner
+.jupiter.SpringExtension
 ;%0A%0Ai
@@ -895,19 +895,22 @@
 sage;%0A%0A@
-Run
+Extend
 With(Spr
@@ -916,25 +916,17 @@
 ring
-JUnit4ClassRunner
+Extension
 .cla
@@ -1085,16 +1085,18 @@
  1000*60
+*2
 ;%0A%09%0A%09@Au
@@ -1104,16 +1104,24 @@
 owired%0A%09
+private 
 AsynchJo
@@ -1171,16 +1171,20 @@
 %09@Before
+Each
 %0A%09public
@@ -2150,35 +2150,24 @@
 %09 * @throws 
-Interrupted
 Exception %0A%09
@@ -2217,19 +2217,8 @@
 ows 
-Interrupted
 Exce
@@ -2231,62 +2231,56 @@
 %7B%0A%09%09
-long start = System.currentTimeMillis();%0A%09%09while(true)
+return TimeUtils.waitFor(MAX_WAIT, 1000L, () -%3E 
 %7B%0A%09%09
@@ -2388,197 +2388,50 @@
 %0A%09%09%09
-if(message != null)%7B%0A%09%09%09%09return message;%0A%09%09%09%7Delse%7B%0A%09%09%09%09assertTrue(%22Timed out waiting for message%22,System.currentTimeMillis() - start %3C MAX_WAIT);%0A%09%09%09%09System.out.println(%22Waiting for
+return new Pair%3C%3E(message != null,
  message
 ...%22
@@ -2430,48 +2430,16 @@
 sage
-...%22
 );%0A%09%09
-%09%09Thread.sleep(1000L);%0A%09%09%09%7D%0A%09%09%7D
+%7D);
 %0A%09%7D%0A

Increase timeout for failing test