use of com.google.gerrit.extensions.api.config.Server in project gerrit by GerritCodeReview.
the class IndexHtmlUtilTest method usePreloadRest.
@Test
public void usePreloadRest() throws Exception {
Accounts accountsApi = mock(Accounts.class);
when(accountsApi.self()).thenThrow(new AuthException("user needs to be authenticated"));
Server serverApi = mock(Server.class);
when(serverApi.getVersion()).thenReturn("123");
when(serverApi.topMenus()).thenReturn(ImmutableList.of());
ServerInfo serverInfo = new ServerInfo();
serverInfo.defaultTheme = "my-default-theme";
when(serverApi.getInfo()).thenReturn(serverInfo);
Config configApi = mock(Config.class);
when(configApi.server()).thenReturn(serverApi);
GerritApi gerritApi = mock(GerritApi.class);
when(gerritApi.accounts()).thenReturn(accountsApi);
when(gerritApi.config()).thenReturn(configApi);
assertThat(dynamicTemplateData(gerritApi, "/c/project/+/123")).containsAtLeast("defaultChangeDetailHex", "1916314", "changeRequestsPath", "changes/project~123");
}
use of com.google.gerrit.extensions.api.config.Server in project gerrit by GerritCodeReview.
the class IndexHtmlUtil method dynamicTemplateData.
/**
* Returns dynamic parameters of {@code index.html}.
*/
public static ImmutableMap<String, Object> dynamicTemplateData(GerritApi gerritApi, String requestedURL) throws RestApiException, URISyntaxException {
ImmutableMap.Builder<String, Object> data = ImmutableMap.builder();
Map<String, SanitizedContent> initialData = new HashMap<>();
Server serverApi = gerritApi.config().server();
initialData.put("\"/config/server/info\"", serializeObject(GSON, serverApi.getInfo()));
initialData.put("\"/config/server/version\"", serializeObject(GSON, serverApi.getVersion()));
initialData.put("\"/config/server/top-menus\"", serializeObject(GSON, serverApi.topMenus()));
String requestedPath = IndexPreloadingUtil.getPath(requestedURL);
IndexPreloadingUtil.RequestedPage page = IndexPreloadingUtil.parseRequestedPage(requestedPath);
switch(page) {
case CHANGE:
data.put("defaultChangeDetailHex", ListOption.toHex(IndexPreloadingUtil.CHANGE_DETAIL_OPTIONS));
data.put("changeRequestsPath", IndexPreloadingUtil.computeChangeRequestsPath(requestedPath, page).get());
break;
case DIFF:
data.put("defaultChangeDetailHex", ListOption.toHex(IndexPreloadingUtil.CHANGE_DETAIL_OPTIONS));
data.put("changeRequestsPath", IndexPreloadingUtil.computeChangeRequestsPath(requestedPath, page).get());
break;
case DASHBOARD:
// Dashboard is preloaded queries are added later when we check user is authenticated.
case PAGE_WITHOUT_PRELOADING:
break;
}
try {
AccountApi accountApi = gerritApi.accounts().self();
initialData.put("\"/accounts/self/detail\"", serializeObject(GSON, accountApi.get()));
initialData.put("\"/accounts/self/preferences\"", serializeObject(GSON, accountApi.getPreferences()));
initialData.put("\"/accounts/self/preferences.diff\"", serializeObject(GSON, accountApi.getDiffPreferences()));
initialData.put("\"/accounts/self/preferences.edit\"", serializeObject(GSON, accountApi.getEditPreferences()));
data.put("userIsAuthenticated", true);
if (page == RequestedPage.DASHBOARD) {
data.put("defaultDashboardHex", ListOption.toHex(IndexPreloadingUtil.DASHBOARD_OPTIONS));
data.put("dashboardQuery", IndexPreloadingUtil.computeDashboardQueryList(serverApi));
}
} catch (AuthException e) {
logger.atFine().log("Can't inline account-related data because user is unauthenticated");
// Don't render data
}
data.put("gerritInitialData", initialData);
return data.build();
}
use of com.google.gerrit.extensions.api.config.Server in project gerrit by GerritCodeReview.
the class IndexServletTest method renderTemplate.
@Test
public void renderTemplate() throws Exception {
Accounts accountsApi = mock(Accounts.class);
when(accountsApi.self()).thenThrow(new AuthException("user needs to be authenticated"));
Server serverApi = mock(Server.class);
when(serverApi.getVersion()).thenReturn("123");
when(serverApi.topMenus()).thenReturn(ImmutableList.of());
ServerInfo serverInfo = new ServerInfo();
serverInfo.defaultTheme = "my-default-theme";
when(serverApi.getInfo()).thenReturn(serverInfo);
Config configApi = mock(Config.class);
when(configApi.server()).thenReturn(serverApi);
GerritApi gerritApi = mock(GerritApi.class);
when(gerritApi.accounts()).thenReturn(accountsApi);
when(gerritApi.config()).thenReturn(configApi);
String testCanonicalUrl = "foo-url";
String testCdnPath = "bar-cdn";
String testFaviconURL = "zaz-url";
// Pick any known experiment enabled by default;
String disabledDefault = ExperimentFeaturesConstants.UI_FEATURE_PATCHSET_COMMENTS;
assertThat(ExperimentFeaturesConstants.DEFAULT_ENABLED_FEATURES).contains(disabledDefault);
org.eclipse.jgit.lib.Config serverConfig = new org.eclipse.jgit.lib.Config();
serverConfig.setStringList("experiments", null, "enabled", ImmutableList.of("NewFeature", "DisabledFeature"));
serverConfig.setStringList("experiments", null, "disabled", ImmutableList.of("DisabledFeature", disabledDefault));
ExperimentFeatures experimentFeatures = new ConfigExperimentFeatures(serverConfig);
IndexServlet servlet = new IndexServlet(testCanonicalUrl, testCdnPath, testFaviconURL, gerritApi, experimentFeatures);
FakeHttpServletResponse response = new FakeHttpServletResponse();
servlet.doGet(new FakeHttpServletRequest(), response);
String output = response.getActualBodyString();
assertThat(output).contains("<!DOCTYPE html>");
assertThat(output).contains("window.CANONICAL_PATH = '" + testCanonicalUrl);
assertThat(output).contains("<link rel=\"preload\" href=\"" + testCdnPath);
assertThat(output).contains("<link rel=\"icon\" type=\"image/x-icon\" href=\"" + testCanonicalUrl + "/" + testFaviconURL);
assertThat(output).contains("window.INITIAL_DATA = JSON.parse(" + "'\\x7b\\x22\\/config\\/server\\/version\\x22: \\x22123\\x22, " + "\\x22\\/config\\/server\\/info\\x22: \\x7b\\x22default_theme\\x22:" + "\\x22my-default-theme\\x22\\x7d, \\x22\\/config\\/server\\/top-menus\\x22: " + "\\x5b\\x5d\\x7d');");
ImmutableSet<String> enabledDefaults = ExperimentFeaturesConstants.DEFAULT_ENABLED_FEATURES.stream().filter(e -> !e.equals(disabledDefault)).collect(ImmutableSet.toImmutableSet());
List<String> expectedEnabled = new ArrayList<>();
expectedEnabled.add("NewFeature");
expectedEnabled.addAll(enabledDefaults);
assertThat(output).contains("window.ENABLED_EXPERIMENTS = JSON.parse('\\x5b\\x22" + String.join("\\x22,", expectedEnabled) + "\\x22\\x5d');</script>");
}
Aggregations