Skip to content

Commit

Permalink
Merge pull request #129 from jenkinsci/jason_cicd835
Browse files Browse the repository at this point in the history
Resolve the Cross-domain problem for getting localization file of coverage table
  • Loading branch information
rwang-parasoft authored Jun 3, 2024
2 parents 671b62b + 50b7618 commit 834f38b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import hudson.PluginWrapper;
import io.jenkins.plugins.datatables.TableConfiguration;
import io.jenkins.plugins.util.JenkinsFacade;
import jenkins.model.Jenkins;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -43,9 +44,21 @@ public void loadConfiguration() {

public void language(String i18nFileBasename) {
if (i18nFileBasename != null) {
String url = new JenkinsFacade().getAbsoluteUrl(StringUtils.removeStart(Jenkins.RESOURCE_PATH, "/"),
String.format("plugin/parasoft-findings/i18n/datatables.net/%s.json", i18nFileBasename));
customConfiguration.put("language", new Language(url));
Jenkins jenkins = Jenkins.get();
String jenkinsRootUrlFromRequest = jenkins.getRootUrlFromRequest();
PluginWrapper pluginWrapper = jenkins.getPluginManager().getPlugin("parasoft-findings");
if (pluginWrapper != null) {
String pluginDir = pluginWrapper.baseResourceURL.getFile();
File wantedLocalizationFile = new File(pluginDir, "/i18n/datatables.net/" + i18nFileBasename + ".json");
if(!wantedLocalizationFile.exists()) {
// Use English as default if the wanted language file does not exist
i18nFileBasename = "datatables";
}

String jenkinsResourcePath = StringUtils.removeStart(Jenkins.RESOURCE_PATH, "/");
String finalLocalizationRequestUrl = jenkinsRootUrlFromRequest + jenkinsResourcePath + "/plugin/parasoft-findings/i18n/datatables.net/" + i18nFileBasename + ".json";
customConfiguration.put("language", new Language(finalLocalizationRequestUrl));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@
import com.parasoft.findings.jenkins.coverage.api.metrics.steps.CoverageTableModel.RowRenderer;
import com.parasoft.findings.jenkins.coverage.model.*;
import hudson.Functions;
import hudson.Plugin;
import hudson.PluginManager;
import hudson.PluginWrapper;
import io.jenkins.plugins.datatables.DetailedCell;
import io.jenkins.plugins.datatables.TableColumn;
import io.jenkins.plugins.datatables.TableConfiguration;
import jenkins.model.Jenkins;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.jar.Manifest;

import static com.parasoft.findings.jenkins.coverage.api.metrics.steps.CoverageViewModel.MODIFIED_LINES_COVERAGE_TABLE_ID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.*;

public class CoverageTableModelTest extends AbstractCoverageTest {
CoverageTableModel coverageTableModel;
Expand All @@ -42,8 +52,27 @@ public void testGetRenderer() {

@Test
public void testGetTableConfiguration() {
TableConfiguration tableConfiguration = coverageTableModel.getTableConfiguration();
assertThat(tableConfiguration).isNotNull();
try(MockedStatic<Jenkins> jenkinsMocked = mockStatic(Jenkins.class)) {
Jenkins.RESOURCE_PATH = "/static/e64c2d52";
Jenkins jenkins = mock(Jenkins.class);
PluginManager pluginManager = mock(PluginManager.class);
Plugin plugin = mock(Plugin.class);
PluginWrapper pluginWrapper = new PluginWrapper(mock(PluginManager.class),new File("fake"), new Manifest(),
new URL("file:/D:/fake/jenkins/plugins/parasoft-findings-plugin/"),
mock(ClassLoader.class), new File("fake"), null, new ArrayList<>());

when(Jenkins.get()).thenReturn(jenkins);
when(jenkins.getRootUrlFromRequest()).thenReturn("http://localhost:8080/jenkins/");
when(jenkins.getPluginManager()).thenReturn(pluginManager);
when(pluginManager.getPlugin("parasoft-findings")).thenReturn(pluginWrapper);
when(plugin.getWrapper()).thenReturn(pluginWrapper);

TableConfiguration tableConfiguration = coverageTableModel.getTableConfiguration();
assertThat(tableConfiguration.getConfiguration().contains("\"language\":{\"url\":\"http://localhost:8080/jenkins/static/e64c2d52/plugin/parasoft-findings/i18n/datatables.net/datatables.json"));
assertThat(tableConfiguration).isNotNull();
} catch (Exception e) {
fail("Exception should not be thrown.", e);
}
}

@Test
Expand Down

0 comments on commit 834f38b

Please sign in to comment.