Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tests for FolderCopyOperation #111

Merged
merged 1 commit into from
Jul 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/test/java/sp/sd/fileoperations/FolderCopyOperationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package sp.sd.fileoperations;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.List;

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

public class FolderCopyOperationTest {

@Rule
public JenkinsRule jenkins = new JenkinsRule();

@Test
public void testDefaults() {
String sourceFolderPath = "sourceFolder";
String destinationFolderPath = "destinationFolder";
FolderCopyOperation operation = new FolderCopyOperation(sourceFolderPath, destinationFolderPath);

assertEquals(sourceFolderPath, operation.getSourceFolderPath());
assertEquals(destinationFolderPath, operation.getDestinationFolderPath());
}

@Test
public void testRunFolderCopyOperation() throws Exception {
FreeStyleProject project = jenkins.createFreeStyleProject("folderCopyTest");

FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
FilePath sourceFolder = new FilePath(workspace, "sourceFolder");
FilePath destinationFolder = new FilePath(workspace, "destinationFolder");

sourceFolder.mkdirs();
FilePath fileInSource = new FilePath(sourceFolder, "file.txt");
fileInSource.write("Sample content", "UTF-8");

FolderCopyOperation operation = new FolderCopyOperation("sourceFolder", "destinationFolder");
project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));

FreeStyleBuild build = project.scheduleBuild2(0).get();
assertEquals(Result.SUCCESS, build.getResult());

FilePath copiedFile = new FilePath(destinationFolder, "file.txt");
assertTrue("The destination folder should have been created", destinationFolder.exists());
assertTrue("The file should have been copied to the destination folder", copiedFile.exists());
assertEquals("Sample content", copiedFile.readToString());
}

@Test
public void testRunFolderCopyOperationWithTokens() throws Exception {
EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
EnvVars envVars = prop.getEnvVars();
envVars.put("SOURCE_FOLDER", "sourceFolder");
envVars.put("DESTINATION_FOLDER", "destinationFolder");
jenkins.jenkins.getGlobalNodeProperties().add(prop);

FreeStyleProject project = jenkins.createFreeStyleProject("folderCopyTestWithTokens");

FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
FilePath sourceFolder = new FilePath(workspace, "sourceFolder");
FilePath destinationFolder = new FilePath(workspace, "destinationFolder");

sourceFolder.mkdirs();
FilePath fileInSource = new FilePath(sourceFolder, "file.txt");
fileInSource.write("Sample content", "UTF-8");

FolderCopyOperation operation = new FolderCopyOperation("$SOURCE_FOLDER", "$DESTINATION_FOLDER");
project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));

FreeStyleBuild build = project.scheduleBuild2(0).get();
assertEquals(Result.SUCCESS, build.getResult());

FilePath copiedFile = new FilePath(destinationFolder, "file.txt");
assertTrue("The destination folder should have been created", destinationFolder.exists());
assertTrue("The file should have been copied to the destination folder", copiedFile.exists());
assertEquals("Sample content", copiedFile.readToString());
}
}