package hudson.plugins.jira;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import hudson.EnvVars;
import hudson.Launcher;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;

import java.io.IOException;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.Set;

import javax.xml.rpc.ServiceException;

import org.junit.Test;
import org.mockito.Mockito;

/**
 * 
 * @author Artem Koshelev artkoshelev@gmail.com
 *
 */
public class VersionCreatorTest {
	AbstractBuild build = mock(AbstractBuild.class);;
	Launcher launcher = mock(Launcher.class);
	BuildListener listener = mock(BuildListener.class);
	EnvVars env = mock(EnvVars.class);
	AbstractProject project = mock(AbstractProject.class);
	JiraSite site = mock(JiraSite.class);

	private static final String JIRA_VER = Long.toString(System.currentTimeMillis());
	private static final String JIRA_PRJ = "TEST_PRJ";
	
	@Test
	public void jiraApiCalledWithSpecifiedParameters() throws InterruptedException, IOException, ServiceException {
		JiraVersionCreator jvc = spy(new JiraVersionCreator(JIRA_VER, JIRA_PRJ));
		doReturn(site).when(jvc).getSiteForProject((AbstractProject<?, ?>) Mockito.any());
		
		when(build.getProject()).thenReturn(project);
		when(build.getEnvironment(listener)).thenReturn(env);
		when(env.expand(Mockito.anyString())).thenReturn(JIRA_VER);
		when(site.getVersions(JIRA_PRJ)).thenReturn(new HashSet<JiraVersion>());
		
		boolean result = jvc.perform(build, launcher, listener);
		verify(site).addVersion(JIRA_VER, JIRA_PRJ);
		assertThat(result, is(true));
	}
	
	@Test
	public void buildDidNotFailWhenVersionExists() throws IOException, InterruptedException, ServiceException {
		JiraVersionCreator jvc = spy(new JiraVersionCreator(JIRA_VER, JIRA_PRJ));
		doReturn(site).when(jvc).getSiteForProject((AbstractProject<?, ?>) Mockito.any());
		
		when(build.getProject()).thenReturn(project);
		when(build.getEnvironment(listener)).thenReturn(env);
		when(env.expand(Mockito.anyString())).thenReturn(JIRA_VER);
		
		Set<JiraVersion> existingVersions = new HashSet<JiraVersion>();
		existingVersions.add(new JiraVersion(JIRA_VER, null, false, false));
		
		when(site.getVersions(JIRA_PRJ)).thenReturn(existingVersions);
		
		PrintStream logger = mock(PrintStream.class);
		when(listener.getLogger()).thenReturn(logger);
		
		boolean result = jvc.perform(build, launcher, listener);
		verify(site, times(0)).addVersion(JIRA_VER, JIRA_PRJ);
		assertThat(result, is(true));
	}
}


@@ -662,16 +662,41 @@
 ption;%0A%0A
+import org.junit.Before;%0A
 import o
@@ -841,35 +841,146 @@
  %7B%0A%09
-AbstractBuild build = mock(
+private static final String JIRA_VER = Long.toString(System.currentTimeMillis());%0A%09private static final String JIRA_PRJ = %22TEST_PRJ%22;%09%0A%09%0A%09
 Abst
@@ -988,24 +988,22 @@
 actBuild
-.class);
+ build
 ;%0A%09Launc
@@ -1018,31 +1018,8 @@
 cher
- = mock(Launcher.class)
 ;%0A%09B
@@ -1043,448 +1043,396 @@
 ener
- = mock(BuildListener.class);%0A%09EnvVars env = mock(EnvVars.class);%0A%09AbstractProject project = mock(AbstractProject.class);%0A%09JiraSite site = mock(JiraSite.class);%0A%0A%09private static final String JIRA_VER = Long.toString(System.currentTimeMillis());%0A%09private static final String JIRA_PRJ = %22TEST_PRJ%22;%0A%09%0A%09@Test%0A%09public void jiraApiCalledWithSpecifiedParameters() throws InterruptedException, IOException, ServiceException %7B%0A%09%09JiraVersionCreator 
+;%0A%09EnvVars env;%0A%09AbstractProject project;%0A%09JiraSite site;%0A%09JiraVersionCreator jvc;%0A%09%0A%09@Before%0A%09public void createCommonMocks() throws IOException, InterruptedException %7B%0A%09%09build = mock(AbstractBuild.class);;%0A%09%09launcher = mock(Launcher.class);%0A%09%09listener = mock(BuildListener.class);%0A%09%09env = mock(EnvVars.class);%0A%09%09project = mock(AbstractProject.class);%0A%09%09site = mock(JiraSite.class);%09%09%0A%09%09
 jvc 
@@ -1474,32 +1474,35 @@
 ER, JIRA_PRJ));%0A
+%09%09%0A
 %09%09doReturn(site)
@@ -1739,16 +1739,141 @@
 A_VER);%0A
+%09%7D%0A%09%0A%09@Test%0A%09public void jiraApiCalledWithSpecifiedParameters() throws InterruptedException, IOException, ServiceException %7B%0A
 %09%09when(s
@@ -2205,341 +2205,8 @@
 n %7B%0A
-%09%09JiraVersionCreator jvc = spy(new JiraVersionCreator(JIRA_VER, JIRA_PRJ));%0A%09%09doReturn(site).when(jvc).getSiteForProject((AbstractProject%3C?, ?%3E) Mockito.any());%0A%09%09%0A%09%09when(build.getProject()).thenReturn(project);%0A%09%09when(build.getEnvironment(listener)).thenReturn(env);%0A%09%09when(env.expand(Mockito.anyString())).thenReturn(JIRA_VER);%0A%09%09%0A
 %09%09Se

use @Before method to init test fixtures