<commit_before>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 <NAME> <EMAIL>
 *
 */
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));
	}
}

<commit_msg>Add an unit test for the expansion<commit_after>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 <NAME> <EMAIL>
 *
 */
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_VER_PARAM = "${JIRA_VER}";
	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));
	}
	
	@Test
	public void buildDidNotFailWhenVersionExistsExpanded() throws IOException, InterruptedException, ServiceException {
		// Same test as the previous one but here the version is contained in a Jenkins parameter
		JiraVersionCreator jvc = spy(new JiraVersionCreator(JIRA_VER_PARAM, JIRA_PRJ));
		doReturn(site).when(jvc).getSiteForProject((AbstractProject<?, ?>) Mockito.any());
		
		when(build.getProject()).thenReturn(project);
		when(build.getEnvironment(listener)).thenReturn(env);
		when(env.expand(JIRA_VER_PARAM)).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));
	}	
}


{'fraction_non_alphanumeric': 0.1244061757719715, 'fraction_numerical': 0.0004453681710213777, 'mean_word_length': 6.578177727784027, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5709777', 'n_tokens_mistral': 2309, 'n_tokens_neox': 2175, 'n_words': 384}