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

Implement UI tests #388

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package jenkins.plugins.foldericon;

import static jenkins.plugins.foldericon.utils.TestUtils.createCustomIconFile;
import static jenkins.plugins.foldericon.utils.TestUtils.mockStaplerRequest;
import static jenkins.plugins.foldericon.utils.TestUtils.validateResponse;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -90,15 +91,7 @@ void testGetRequiredGlobalConfigPagePermission(@SuppressWarnings("unused") Jenki
void testGetDiskUsage(JenkinsRule r) throws Exception {
CustomFolderIconConfiguration descriptor = new CustomFolderIconConfiguration();

FilePath parent = r.jenkins
.getRootPath()
.child(CustomFolderIconConfiguration.USER_CONTENT_PATH)
.child(CustomFolderIconConfiguration.PLUGIN_PATH);
parent.mkdirs();

FilePath file = parent.child(System.currentTimeMillis() + ".png");
file.touch(System.currentTimeMillis());
assertTrue(file.exists());
FilePath file = createCustomIconFile(r);

String usage = descriptor.getDiskUsage();
assertEquals(FileUtils.byteCountToDisplaySize(file.length()), usage);
Expand Down Expand Up @@ -142,12 +135,7 @@ void testGetDiskUsageWithException(JenkinsRule r) throws Exception {
CustomFolderIconConfiguration descriptor = new CustomFolderIconConfiguration();

FilePath userContent = r.jenkins.getRootPath().child(CustomFolderIconConfiguration.USER_CONTENT_PATH);
FilePath iconDir = userContent.child(CustomFolderIconConfiguration.PLUGIN_PATH);
iconDir.mkdirs();

String filename = System.currentTimeMillis() + ".png";
FilePath file = iconDir.child(filename);
file.touch(System.currentTimeMillis());
FilePath file = createCustomIconFile(r);

try (@SuppressWarnings("unused")
MockedConstruction<FilePath> mocked = mockConstructionWithAnswer(FilePath.class, invocation -> {
Expand All @@ -158,7 +146,7 @@ void testGetDiskUsageWithException(JenkinsRule r) throws Exception {
return true;
} else if (StringUtils.equals(call, "filePath.list();")) {
return List.of(file);
} else if (StringUtils.equals(call, "filePath.child(\"" + filename + "\");")) {
} else if (StringUtils.equals(call, "filePath.child(\n \"" + file.getName() + "\"\n);")) {
throw new IOException("Mocked Exception!");
}
return fail("Unexpected invocation '" + call + "' - Test is broken!");
Expand All @@ -180,15 +168,7 @@ void testDoCleanupNoItems(JenkinsRule r) throws Exception {
try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
StaplerRequest mockReq = mockStaplerRequest(stapler);

FilePath parent = r.jenkins
.getRootPath()
.child(CustomFolderIconConfiguration.USER_CONTENT_PATH)
.child(CustomFolderIconConfiguration.PLUGIN_PATH);
parent.mkdirs();

FilePath file = parent.child(System.currentTimeMillis() + ".png");
file.touch(System.currentTimeMillis());
assertTrue(file.exists());
FilePath file = createCustomIconFile(r);
HttpResponse response = descriptor.doCleanup(mockReq);

validateResponse(response, HttpServletResponse.SC_OK, null, null);
Expand Down Expand Up @@ -240,15 +220,7 @@ void testDoCleanupOnlyUsedIcons(JenkinsRule r) throws Exception {
project1.setIcon(customIcon);
project2.setIcon(customIcon);

FilePath parent = r.jenkins
.getRootPath()
.child(CustomFolderIconConfiguration.USER_CONTENT_PATH)
.child(CustomFolderIconConfiguration.PLUGIN_PATH);
parent.mkdirs();

FilePath dummy = parent.child(DUMMY_PNG);
dummy.touch(System.currentTimeMillis());
assertTrue(dummy.exists());
FilePath dummy = createCustomIconFile(r);

try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
StaplerRequest mockReq = mockStaplerRequest(stapler);
Expand All @@ -266,29 +238,18 @@ void testDoCleanupOnlyUsedIcons(JenkinsRule r) throws Exception {
*/
@Test
void testDoCleanupUsedAndUnusedIcons(JenkinsRule r) throws Exception {
FilePath dummy = createCustomIconFile(r);
FilePath unused = createCustomIconFile(r);

CustomFolderIconConfiguration descriptor = new CustomFolderIconConfiguration();
CustomFolderIcon customIcon = new CustomFolderIcon(DUMMY_PNG);
CustomFolderIcon customIcon = new CustomFolderIcon(dummy.getName());

Folder project1 = r.jenkins.createProject(Folder.class, "folder");
OrganizationFolder project2 = r.jenkins.createProject(OrganizationFolder.class, "org");

project1.setIcon(customIcon);
project2.setIcon(customIcon);

FilePath parent = r.jenkins
.getRootPath()
.child(CustomFolderIconConfiguration.USER_CONTENT_PATH)
.child(CustomFolderIconConfiguration.PLUGIN_PATH);
parent.mkdirs();

FilePath dummy = parent.child(DUMMY_PNG);
dummy.touch(System.currentTimeMillis());
assertTrue(dummy.exists());

FilePath unused = parent.child("unused.png");
unused.touch(System.currentTimeMillis());
assertTrue(unused.exists());

try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
StaplerRequest mockReq = mockStaplerRequest(stapler);
HttpResponse response = descriptor.doCleanup(mockReq);
Expand Down Expand Up @@ -336,13 +297,8 @@ void testDoCleanupFileNotDeleted(JenkinsRule r) throws Exception {
try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
StaplerRequest mockReq = mockStaplerRequest(stapler);

FilePath file = createCustomIconFile(r);
FilePath userContent = r.jenkins.getRootPath().child(CustomFolderIconConfiguration.USER_CONTENT_PATH);
FilePath iconDir = userContent.child(CustomFolderIconConfiguration.PLUGIN_PATH);
iconDir.mkdirs();

String filename = System.currentTimeMillis() + ".png";
FilePath file = iconDir.child(filename);
file.touch(System.currentTimeMillis());

try (@SuppressWarnings("unused")
MockedConstruction<FilePath> mocked = mockConstructionWithAnswer(FilePath.class, invocation -> {
Expand All @@ -353,7 +309,7 @@ void testDoCleanupFileNotDeleted(JenkinsRule r) throws Exception {
return true;
} else if (StringUtils.equals(call, "filePath.list();")) {
return List.of(file);
} else if (StringUtils.equals(call, "filePath.child(\"" + filename + "\");")) {
} else if (StringUtils.equals(call, "filePath.child(\n \"" + file.getName() + "\"\n);")) {
FilePath mock = mock(FilePath.class);
when(mock.delete()).thenReturn(false);
return mock;
Expand Down Expand Up @@ -382,14 +338,7 @@ void testDoCleanupFileNotDeletedWithException(JenkinsRule r) throws Exception {
try (MockedStatic<Stapler> stapler = mockStatic(Stapler.class)) {
StaplerRequest mockReq = mockStaplerRequest(stapler);

FilePath parent = r.jenkins
.getRootPath()
.child(CustomFolderIconConfiguration.USER_CONTENT_PATH)
.child(CustomFolderIconConfiguration.PLUGIN_PATH);
parent.mkdirs();

FilePath file = parent.child(System.currentTimeMillis() + ".png");
file.touch(System.currentTimeMillis());
FilePath file = createCustomIconFile(r);
File remoteFile = new File(file.getRemote());

// jenkins is pretty brutal when deleting files...
Expand Down Expand Up @@ -432,12 +381,7 @@ void testDoCleanupFileNotDeletedWithMockedException(JenkinsRule r) throws Except
StaplerRequest mockReq = mockStaplerRequest(stapler);

FilePath userContent = r.jenkins.getRootPath().child(CustomFolderIconConfiguration.USER_CONTENT_PATH);
FilePath iconDir = userContent.child(CustomFolderIconConfiguration.PLUGIN_PATH);
iconDir.mkdirs();

String filename = System.currentTimeMillis() + ".png";
FilePath file = iconDir.child(filename);
file.touch(System.currentTimeMillis());
FilePath file = createCustomIconFile(r);

try (@SuppressWarnings("unused")
MockedConstruction<FilePath> mocked = mockConstructionWithAnswer(FilePath.class, invocation -> {
Expand All @@ -448,7 +392,7 @@ void testDoCleanupFileNotDeletedWithMockedException(JenkinsRule r) throws Except
return true;
} else if (StringUtils.equals(call, "filePath.list();")) {
return List.of(file);
} else if (StringUtils.equals(call, "filePath.child(\"" + filename + "\");")) {
} else if (StringUtils.equals(call, "filePath.child(\n \"" + file.getName() + "\"\n);")) {
throw new IOException("Mocked Exception!");
}
return fail("Unexpected invocation '" + call + "' - Test is broken!");
Expand Down
Loading