*/
package org.sonar.server.ui.ws;
+import com.google.common.collect.ImmutableSet;
+import java.util.Set;
import org.sonar.api.config.Settings;
+import org.sonar.api.platform.Server;
import org.sonar.api.resources.ResourceType;
import org.sonar.api.resources.ResourceTypes;
import org.sonar.api.server.ws.Request;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.api.web.NavigationSection;
import org.sonar.api.web.Page;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.H2;
import org.sonar.server.ui.ViewProxy;
import org.sonar.server.ui.Views;
+import static org.sonar.api.CoreProperties.CORE_ALLOW_USERS_TO_SIGNUP_PROPERTY;
+import static org.sonar.api.CoreProperties.HOURS_IN_DAY;
+import static org.sonar.api.CoreProperties.RATING_GRID;
+import static org.sonar.core.config.WebConstants.SONAR_LF_ENABLE_GRAVATAR;
+import static org.sonar.core.config.WebConstants.SONAR_LF_GRAVATAR_SERVER_URL;
import static org.sonar.core.config.WebConstants.SONAR_LF_LOGO_URL;
import static org.sonar.core.config.WebConstants.SONAR_LF_LOGO_WIDTH_PX;
+import static org.sonar.core.config.WebConstants.SONAR_UPDATECENTER_ACTIVATE;
public class GlobalNavigationAction implements NavigationWsAction {
+ private static final Set<String> SETTING_KEYS = ImmutableSet.of(
+ SONAR_LF_LOGO_URL,
+ SONAR_LF_LOGO_WIDTH_PX,
+ SONAR_LF_ENABLE_GRAVATAR,
+ SONAR_LF_GRAVATAR_SERVER_URL,
+ SONAR_UPDATECENTER_ACTIVATE,
+ HOURS_IN_DAY,
+ RATING_GRID,
+ CORE_ALLOW_USERS_TO_SIGNUP_PROPERTY);
+
private final Views views;
private final Settings settings;
private final ResourceTypes resourceTypes;
+ private final Server server;
+ private final Database database;
- public GlobalNavigationAction(Views views, Settings settings, ResourceTypes resourceTypes) {
+ public GlobalNavigationAction(Views views, Settings settings, ResourceTypes resourceTypes, Server server, Database database) {
this.views = views;
this.settings = settings;
this.resourceTypes = resourceTypes;
+ this.server = server;
+ this.database = database;
}
@Override
public void handle(Request request, Response response) throws Exception {
JsonWriter json = response.newJsonWriter().beginObject();
writePages(json);
- writeLogoProperties(json);
+ writeSettings(json);
+ writeDeprecatedLogoProperties(json);
writeQualifiers(json);
+ writeVersion(json);
+ writeDatabaseProduction(json);
json.endObject().close();
}
json.endArray();
}
- private void writeLogoProperties(JsonWriter json) {
+ private void writeSettings(JsonWriter json) {
+ json.name("settings").beginObject();
+ for (String settingKey : SETTING_KEYS) {
+ json.prop(settingKey, settings.getString(settingKey));
+ }
+ json.endObject();
+ }
+
+ private void writeDeprecatedLogoProperties(JsonWriter json) {
json.prop("logoUrl", settings.getString(SONAR_LF_LOGO_URL));
json.prop("logoWidth", settings.getString(SONAR_LF_LOGO_WIDTH_PX));
}
}
json.endArray();
}
+
+ private void writeVersion(JsonWriter json) {
+ json.prop("version", server.getVersion());
+ }
+
+ private void writeDatabaseProduction(JsonWriter json) {
+ json.prop("productionDatabase", !database.getDialect().getId().equals(H2.ID));
+ }
}
import org.junit.Test;
import org.sonar.api.config.MapSettings;
import org.sonar.api.config.Settings;
+import org.sonar.api.platform.Server;
import org.sonar.api.resources.ResourceType;
import org.sonar.api.resources.ResourceTypeTree;
import org.sonar.api.resources.ResourceTypes;
import org.sonar.api.web.UserRole;
import org.sonar.api.web.View;
import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.H2;
+import org.sonar.db.dialect.MySql;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ui.Views;
import org.sonar.server.ws.WsActionTester;
import org.sonar.test.JsonAssert;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
public class GlobalNavigationActionTest {
@Rule
private Settings settings = new MapSettings();
+ private Server server = mock(Server.class);
+ private Database database = mock(Database.class);
+
private WsActionTester ws;
@Test
}
@Test
- public void with_root_qualifiers() throws Exception {
+ public void return_qualifiers() throws Exception {
init(new View[] {}, new ResourceTypeTree[] {
ResourceTypeTree.builder()
.addType(ResourceType.builder("POL").build())
.build()
});
- executeAndVerify("with_qualifiers.json");
+ executeAndVerify("qualifiers.json");
+ }
+
+ @Test
+ public void return_settings() throws Exception {
+ init();
+ settings.setProperty("sonar.lf.logoUrl", "http://example.com/my-custom-logo.png");
+ settings.setProperty("sonar.lf.logoWidthPx", 135);
+ settings.setProperty("sonar.lf.gravatarServerUrl", "https://secure.gravatar.com/avatar/{EMAIL_MD5}.jpg?s={SIZE}&d=identicon");
+ settings.setProperty("sonar.lf.enableGravatar", true);
+ settings.setProperty("sonar.updatecenter.activate", false);
+ settings.setProperty("sonar.technicalDebt.hoursInDay", "10");
+ settings.setProperty("sonar.technicalDebt.ratingGrid", "0.05,0.1,0.2,0.5");
+ settings.setProperty("sonar.allowUsersToSignUp", true);
+ // This setting should be ignored as it's not needed
+ settings.setProperty("sonar.defaultGroup", "sonar-users");
+
+ executeAndVerify("settings.json");
}
@Test
- public void only_logo() throws Exception {
+ public void return_deprecated_logo_settings() throws Exception {
init();
- settings.setProperty("sonar.lf.logoUrl", "http://some-server.tld/logo.png");
- settings.setProperty("sonar.lf.logoWidthPx", "123");
+ settings.setProperty("sonar.lf.logoUrl", "http://example.com/my-custom-logo.png");
+ settings.setProperty("sonar.lf.logoWidthPx", 135);
- executeAndVerify("only_logo.json");
+ executeAndVerify("deprecated_logo_settings.json");
}
@Test
- public void nominal_call_for_anonymous() throws Exception {
+ public void return_global_pages_for_anonymous() throws Exception {
init(createViews(), new ResourceTypeTree[] {});
- addNominalSettings();
- executeAndVerify("anonymous.json");
+ executeAndVerify("global_pages_for_anonymous.json");
}
@Test
- public void nominal_call_for_user() throws Exception {
+ public void return_global_pages_for_user() throws Exception {
init(createViews(), new ResourceTypeTree[] {});
- addNominalSettings();
-
userSessionRule.login("obiwan");
- executeAndVerify("user.json");
+ executeAndVerify("global_pages_for_user.json");
}
@Test
- public void nominal_call_for_admin() throws Exception {
+ public void return_global_pages_for_admin() throws Exception {
init(createViews(), new ResourceTypeTree[] {});
- addNominalSettings();
-
userSessionRule.login("obiwan").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
- executeAndVerify("admin.json");
+ executeAndVerify("global_pages_for_admin.json");
}
@Test
- public void nominal_call_for_user_without_configured_dashboards() throws Exception {
- init(createViews(), new ResourceTypeTree[] {});
- addNominalSettings();
+ public void return_sonarqube_version() throws Exception {
+ init();
+ when(server.getVersion()).thenReturn("6.2");
+
+ executeAndVerify("version.json");
+ }
+
+ @Test
+ public void return_if_production_database_or_not() throws Exception {
+ init();
+ when(database.getDialect()).thenReturn(new MySql());
- userSessionRule.login("anakin");
+ executeAndVerify("production_database.json");
+ }
- executeAndVerify("anonymous.json");
+ @Test
+ public void test_example_response() throws Exception {
+ init(createViews(), new ResourceTypeTree[] {
+ ResourceTypeTree.builder()
+ .addType(ResourceType.builder("POL").build())
+ .addType(ResourceType.builder("LOP").build())
+ .addRelations("POL", "LOP")
+ .build(),
+ ResourceTypeTree.builder()
+ .addType(ResourceType.builder("PAL").build())
+ .addType(ResourceType.builder("LAP").build())
+ .addRelations("PAL", "LAP")
+ .build()
+ });
+ settings.setProperty("sonar.lf.logoUrl", "http://example.com/my-custom-logo.png");
+ settings.setProperty("sonar.lf.logoWidthPx", 135);
+ settings.setProperty("sonar.lf.gravatarServerUrl", "http://some-server.tld/logo.png");
+ settings.setProperty("sonar.lf.enableGravatar", true);
+ settings.setProperty("sonar.updatecenter.activate", false);
+ settings.setProperty("sonar.technicalDebt.hoursInDay", "10");
+ settings.setProperty("sonar.technicalDebt.ratingGrid", "0.05,0.1,0.2,0.5");
+ settings.setProperty("sonar.allowUsersToSignUp", true);
+ when(server.getVersion()).thenReturn("6.2");
+ when(database.getDialect()).thenReturn(new MySql());
+
+ String result = ws.newRequest().execute().getInput();
+ JsonAssert.assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(result);
}
private void init() {
}
private void init(View[] views, ResourceTypeTree[] resourceTypeTrees) {
- ws = new WsActionTester(new GlobalNavigationAction(new Views(userSessionRule, views), settings, new ResourceTypes(resourceTypeTrees)));
+ when(database.getDialect()).thenReturn(new H2());
+ ws = new WsActionTester(new GlobalNavigationAction(new Views(userSessionRule, views), settings, new ResourceTypes(resourceTypeTrees), server, database));
}
private void executeAndVerify(String json) {
JsonAssert.assertJson(ws.newRequest().execute().getInput()).isSimilarTo(getClass().getResource(GlobalNavigationActionTest.class.getSimpleName() + "/" + json));
}
- private void addNominalSettings() {
- settings.setProperty("sonar.lf.logoUrl", "http://some-server.tld/logo.png");
- settings.setProperty("sonar.lf.logoWidthPx", "123");
- }
-
private View[] createViews() {
Page page = new Page() {
@Override