*/
package org.sonar.server.ui.ws;
-import java.util.List;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.ResourceType;
import org.sonar.api.resources.ResourceTypes;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.api.web.NavigationSection;
import org.sonar.api.web.Page;
-import org.sonar.db.dashboard.ActiveDashboardDao;
-import org.sonar.db.dashboard.DashboardDto;
import org.sonar.server.ui.ViewProxy;
import org.sonar.server.ui.Views;
-import org.sonar.server.user.UserSession;
public class GlobalNavigationAction implements NavigationWsAction {
- private static final String ANONYMOUS = null;
-
- private final ActiveDashboardDao activeDashboardDao;
private final Views views;
private final Settings settings;
private final ResourceTypes resourceTypes;
- private final UserSession userSession;
- public GlobalNavigationAction(ActiveDashboardDao activeDashboardDao, Views views, Settings settings, ResourceTypes resourceTypes, UserSession userSession) {
- this.activeDashboardDao = activeDashboardDao;
+ public GlobalNavigationAction(Views views, Settings settings, ResourceTypes resourceTypes) {
this.views = views;
this.settings = settings;
this.resourceTypes = resourceTypes;
- this.userSession = userSession;
}
@Override
@Override
public void handle(Request request, Response response) throws Exception {
- List<DashboardDto> dashboards = activeDashboardDao.selectGlobalDashboardsForUserLogin(userSession.getLogin());
- if (dashboards.isEmpty()) {
- dashboards = activeDashboardDao.selectGlobalDashboardsForUserLogin(ANONYMOUS);
- }
-
JsonWriter json = response.newJsonWriter().beginObject();
- writeDashboards(json, dashboards);
writePages(json);
writeLogoProperties(json);
writeQualifiers(json);
json.endObject().close();
}
- private static void writeDashboards(JsonWriter json, List<DashboardDto> dashboards) {
- json.name("globalDashboards").beginArray();
- for (DashboardDto dashboard : dashboards) {
- json.beginObject()
- .prop("key", dashboard.getKey())
- .prop("name", dashboard.getName())
- .endObject();
- }
- json.endArray();
- }
-
private void writePages(JsonWriter json) {
json.name("globalPages").beginArray();
for (ViewProxy<Page> page : views.getPages(NavigationSection.HOME)) {
*/
package org.sonar.server.ui.ws;
-import org.junit.After;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.sonar.api.config.Settings;
import org.sonar.api.config.MapSettings;
+import org.sonar.api.config.Settings;
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.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
-import org.sonar.db.dashboard.ActiveDashboardDao;
-import org.sonar.db.dashboard.ActiveDashboardDto;
-import org.sonar.db.dashboard.DashboardDao;
-import org.sonar.db.dashboard.DashboardDto;
-import org.sonar.db.user.UserDao;
-import org.sonar.db.user.UserDto;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ui.Views;
import org.sonar.server.ws.WsTester;
-import static org.mockito.Mockito.mock;
-
public class GlobalNavigationActionTest {
@Rule
@Rule
public UserSessionRule userSessionRule = UserSessionRule.standalone();
- private DbSession session;
+ private DbSession session = dbTester.getSession();
private WsTester wsTester;
- private UserDao userDao;
-
- private DashboardDao dashboardDao;
-
- private ActiveDashboardDao activeDashboardDao;
-
- @Before
- public void before() {
- userDao = new UserDao(dbTester.myBatis(), mock(System2.class));
- dashboardDao = new DashboardDao(dbTester.myBatis());
- activeDashboardDao = new ActiveDashboardDao(dbTester.myBatis());
- DbClient dbClient = new DbClient(
- dbTester.database(), dbTester.myBatis(), userDao, dashboardDao, activeDashboardDao);
-
- session = dbClient.openSession(false);
- }
-
- @After
- public void after() {
- session.close();
- }
-
@Test
public void empty_call() throws Exception {
- wsTester = new WsTester(new NavigationWs(new GlobalNavigationAction(activeDashboardDao, new Views(userSessionRule), new MapSettings(), new ResourceTypes(), userSessionRule)));
+ wsTester = new WsTester(new NavigationWs(new GlobalNavigationAction(new Views(userSessionRule), new MapSettings(), new ResourceTypes())));
wsTester.newGetRequest("api/navigation", "global").execute().assertJson(getClass(), "empty.json");
}
.addRelations("PAL", "LAP")
.build()
});
- wsTester = new WsTester(new NavigationWs(new GlobalNavigationAction(activeDashboardDao, new Views(userSessionRule), new MapSettings(), resourceTypes, userSessionRule)));
+ wsTester = new WsTester(new NavigationWs(new GlobalNavigationAction(new Views(userSessionRule), new MapSettings(), resourceTypes)));
wsTester.newGetRequest("api/navigation", "global").execute().assertJson(getClass(), "with_qualifiers.json");
}
@Test
public void only_logo() throws Exception {
- wsTester = new WsTester(new NavigationWs(new GlobalNavigationAction(activeDashboardDao, new Views(userSessionRule),
+ wsTester = new WsTester(new NavigationWs(new GlobalNavigationAction(new Views(userSessionRule),
new MapSettings()
.setProperty("sonar.lf.logoUrl", "http://some-server.tld/logo.png")
.setProperty("sonar.lf.logoWidthPx", "123"),
- new ResourceTypes(), userSessionRule)));
+ new ResourceTypes())));
wsTester.newGetRequest("api/navigation", "global").execute().assertJson(getClass(), "only_logo.json");
}
}
private void nominalSetup() {
- createAndConfigureDashboardForUser();
- createAndConfigureDashboardForAnonymous();
-
session.commit();
Settings settings = new MapSettings()
.setProperty("sonar.lf.logoUrl", "http://some-server.tld/logo.png")
.setProperty("sonar.lf.logoWidthPx", "123");
- wsTester = new WsTester(new NavigationWs(new GlobalNavigationAction(activeDashboardDao, createViews(), settings, new ResourceTypes(), userSessionRule)));
- }
-
- private void createAndConfigureDashboardForUser() {
- UserDto user = createUser("obiwan", 42L);
- userDao.insert(session, user);
- DashboardDto defaultDashboardForUser = createDashboard(1L, "Default Dashboard for User");
- dashboardDao.insert(defaultDashboardForUser);
- activeDashboardDao.insert(createActiveDashboard(user.getId(), defaultDashboardForUser.getId(), 1));
- }
-
- private void createAndConfigureDashboardForAnonymous() {
- DashboardDto defaultDashboardForAnonymous = createDashboard(2L, "Default Dashboard for Anonymous");
- dashboardDao.insert(defaultDashboardForAnonymous);
- activeDashboardDao.insert(createActiveDashboard(null, defaultDashboardForAnonymous.getId(), 1));
- }
-
- private UserDto createUser(String login, Long userId) {
- return new UserDto()
- .setActive(true)
- .setId(userId)
- .setLogin(login);
- }
-
- private DashboardDto createDashboard(long id, String name) {
- return new DashboardDto()
- .setId(id)
- .setName(name)
- .setShared(true);
- }
-
- private ActiveDashboardDto createActiveDashboard(Long userId, Long dashboardId, int orderIndex) {
- return new ActiveDashboardDto()
- .setOrderIndex(orderIndex)
- .setDashboardId(dashboardId)
- .setUserId(userId);
+ wsTester = new WsTester(new NavigationWs(new GlobalNavigationAction(createViews(), settings, new ResourceTypes())));
}
private Views createViews() {
public String getTitle() {
return "My Plugin Page";
}
+
@Override
public String getId() {
return "my_plugin_page";
public String getTitle() {
return "My Rails App";
}
+
@Override
public String getId() {
return "/my_rails_app";