/**
 * 
 */
package org.csstudio.utility.channel.actions;

import static gov.bnl.channelfinder.api.Channel.Builder.channel;
import static gov.bnl.channelfinder.api.Tag.Builder.tag;
import static org.junit.Assert.assertTrue;
import gov.bnl.channelfinder.api.Channel;
import gov.bnl.channelfinder.api.ChannelFinderClient;
import gov.bnl.channelfinder.api.Tag;

import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Logger;

import org.csstudio.apputil.test.TestProperties;
import org.csstudio.utility.channel.ICSSChannel;
import org.csstudio.utility.channel.nsls2.CSSChannelFactory;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * @author shroffk
 * 
 */
public class AddTagsJobUnitTest {

	private static Logger logger = Logger
			.getLogger("org.csstudio.utility.channel" + "AddTagsJobUnitTest");
	private ChannelFinderClient client;
	private Channel.Builder ch1;
	private Channel.Builder ch2;
	private Tag tag;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		final TestProperties settings = new TestProperties();
		try {
			client = ChannelFinderClient.getInstance();
			ch1 = channel("cssUnitTestChannel1").owner("css");
			ch2 = channel("cssUnitTestChannel1").owner("css");
			client.add(ch1);
			client.add(ch2);
			client.add(tag("cssUnitTestTag", "tagOwner"));
		} catch (Exception e) {
			logger.info("Failed to create the channelfinder client"
					+ e.getMessage());
		}
	}

	@Test
	public void addTagsJobTest() {
		Collection<ICSSChannel> channels = new ArrayList<ICSSChannel>();
		channels.add(CSSChannelFactory.getInstance().getCSSChannel(ch1.build()));
		channels.add(CSSChannelFactory.getInstance().getCSSChannel(ch2.build()));
		Job job = new AddTagsJob("addTags", channels, tag("cssUnitTestTag",
				"tagOwner"));
		job.schedule();
		try {
			job.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if(job.getResult() == Status.OK_STATUS){
			Collection<Channel> chs = new ArrayList<Channel>();
			chs.add(ch1.build());
			chs.add(ch2.build());
			assertTrue("Failed to Add tags: ", 
					client.findChannelsByTag("cssUnitTestTag").containsAll(chs));
		}
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
		client.remove(channel("cssUnitTestChannel1").owner("css"));
		client.remove(channel("cssUnitTestChannel1").owner("css"));
		client.deleteTag("cssUnitTestTag");
	}

}

@@ -789,22 +789,83 @@
 g.junit.
-Before
+AfterClass;%0D%0Aimport org.junit.Before;%0D%0Aimport org.junit.BeforeClass
 ;%0D%0Aimpor
@@ -1242,16 +1242,21 @@
 %09@Before
+Class
 %0D%0A%09publi
@@ -1715,24 +1715,47 @@
 essage());%0D%0A
+%09%09%09assertTrue(false);%0D%0A
 %09%09%7D%0D%0A%09%7D%0D%0A%0D%0A%09
@@ -2141,17 +2141,128 @@
 %0A%09%09%09
-job.join(
+// TODO not have a static def for the amount of time needed to%0D%0A%09%09%09// complete the update%0D%0A%09%09%09job.getThread().join(30000
 );%0D%0A
@@ -2320,24 +2320,78 @@
 ckTrace();%0D%0A
+%09%09%09assertTrue(%22The Job is taking too long.%22, false);%0D%0A
 %09%09%7D%0D%0A%09%09if(jo
@@ -2387,16 +2387,17 @@
 %09%7D%0D%0A%09%09if
+ 
 (job.get
@@ -2425,16 +2425,17 @@
 _STATUS)
+ 
 %7B%0D%0A%09%09%09Co
@@ -2574,17 +2574,16 @@
 tags: %22,
- 
 %0D%0A%09%09%09%09%09c
@@ -2707,16 +2707,21 @@
 %0A%09@After
+Class
 %0D%0A%09publi

update test of addtagjob to timeout