The dashboard is created but not activated.
*/
package org.sonar.plugins.core;
+import org.sonar.plugins.core.dashboards.MyFavouritesDashboard;
+
import com.google.common.collect.Lists;
import org.sonar.api.CoreProperties;
import org.sonar.api.Extension;
extensions.add(TimeMachineDashboard.class);
extensions.add(ProjectsDashboard.class);
extensions.add(TreemapDashboard.class);
+ extensions.add(MyFavouritesDashboard.class);
// chart
extensions.add(XradarChart.class);
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.core.dashboards;
+
+import org.sonar.plugins.core.widgets.FilterWidget;
+
+import org.sonar.api.web.Dashboard.Widget;
+
+import org.sonar.api.web.Dashboard;
+import org.sonar.api.web.DashboardLayout;
+import org.sonar.api.web.DashboardTemplate;
+
+/**
+ * Base class for global dashboard containing a single
+ * filter widget.
+ *
+ * @since 3.1
+ */
+abstract class AbstractFilterDashboard extends DashboardTemplate {
+ protected abstract String getFilterKey();
+
+ @Override
+ public Dashboard createDashboard() {
+ Dashboard dashboard = Dashboard.create()
+ .setGlobal(true)
+ .setLayout(DashboardLayout.ONE_COLUMN);
+
+ Widget filterWidget = dashboard.addWidget("filter", 1)
+ .setProperty(FilterWidget.FILTER, getFilterKey());
+
+ return dashboard;
+ }
+}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.core.dashboards;
+
+import org.sonar.api.web.Dashboard;
+import org.sonar.plugins.core.filters.MyFavouritesFilter;
+
+/**
+ * My favorites global dashboard for Sonar
+ *
+ * @since 3.1
+ */
+public final class MyFavouritesDashboard extends AbstractFilterDashboard {
+ @Override
+ public String getName() {
+ return "My Favourites";
+ }
+
+ @Override
+ protected String getFilterKey() {
+ return new MyFavouritesFilter().getName();
+ }
+
+ @Override
+ public Dashboard createDashboard() {
+ return super.createDashboard().setActivated(false);
+ }
+}
\ No newline at end of file
*/
package org.sonar.plugins.core.dashboards;
-import org.sonar.plugins.core.widgets.FilterWidget;
-
-import org.sonar.api.web.Dashboard.Widget;
-
-import org.sonar.api.web.Dashboard;
-import org.sonar.api.web.DashboardLayout;
-import org.sonar.api.web.DashboardTemplate;
+import org.sonar.plugins.core.filters.ProjectFilter;
/**
* Projects global dashboard for Sonar
*
* @since 3.1
*/
-public final class ProjectsDashboard extends DashboardTemplate {
+public final class ProjectsDashboard extends AbstractFilterDashboard {
@Override
public String getName() {
return "Projects";
}
@Override
- public Dashboard createDashboard() {
- Dashboard dashboard = Dashboard.create();
- dashboard.setGlobal(true);
- dashboard.setLayout(DashboardLayout.ONE_COLUMN);
-
- Widget filterWidget = dashboard.addWidget("filter", 1);
- filterWidget.setProperty(FilterWidget.FILTER, "Projects");
-
- return dashboard;
+ protected String getFilterKey() {
+ return new ProjectFilter().getName();
}
}
\ No newline at end of file
*/
package org.sonar.plugins.core.dashboards;
-import org.sonar.plugins.core.widgets.FilterWidget;
-
-import org.sonar.api.web.Dashboard.Widget;
-
-import org.sonar.api.web.Dashboard;
-import org.sonar.api.web.DashboardLayout;
-import org.sonar.api.web.DashboardTemplate;
+import org.sonar.plugins.core.filters.TreeMapFilter;
/**
* Treemap global dashboard for Sonar
*
* @since 3.1
*/
-public final class TreemapDashboard extends DashboardTemplate {
+public final class TreemapDashboard extends AbstractFilterDashboard {
@Override
public String getName() {
return "Treemap";
}
@Override
- public Dashboard createDashboard() {
- Dashboard dashboard = Dashboard.create();
- dashboard.setGlobal(true);
- dashboard.setLayout(DashboardLayout.ONE_COLUMN);
-
- Widget filterWidget = dashboard.addWidget("filter", 1);
- filterWidget.setProperty(FilterWidget.FILTER, "Treemap");
-
- return dashboard;
+ protected String getFilterKey() {
+ return new TreeMapFilter().getName();
}
}
\ No newline at end of file
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.core.dashboards;
+
+import com.google.common.collect.Iterables;
+import org.junit.Test;
+import org.sonar.api.web.Dashboard;
+import org.sonar.api.web.Dashboard.Widget;
+import org.sonar.plugins.core.CorePlugin;
+import org.sonar.plugins.core.filters.MyFavouritesFilter;
+import org.sonar.plugins.core.widgets.FilterWidget;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class MyFavouritesDashboardTest {
+ MyFavouritesDashboard template = new MyFavouritesDashboard();
+
+ @Test
+ public void should_have_a_name() {
+ assertThat(template.getName()).isEqualTo("My Favourites");
+ }
+
+ @Test
+ public void should_be_registered_as_an_extension() {
+ assertThat(new CorePlugin().getExtensions()).contains(template.getClass());
+ }
+
+ @Test
+ public void should_create_global_dashboard_with_one_filter() {
+ Dashboard dashboard = template.createDashboard();
+ Widget widget = Iterables.getOnlyElement(dashboard.getWidgets());
+
+ assertThat(dashboard.isGlobal()).isTrue();
+ assertThat(dashboard.isActivated()).isFalse();
+ assertThat(widget.getId()).isEqualTo(new FilterWidget().getId());
+ assertThat(widget.getProperty("filter")).isEqualTo(new MyFavouritesFilter().getName());
+ }
+}
*/
package org.sonar.core.dashboard;
-public final class ActiveDashboardDto {
+import com.google.common.base.Objects;
+public final class ActiveDashboardDto {
private Long id;
private Long dashboardId;
private Long userId;
/**
* @param id the id to set
*/
- public void setId(Long id) {
+ public ActiveDashboardDto setId(Long id) {
this.id = id;
+ return this;
}
/**
/**
* @param dashboardId the dashboardId to set
*/
- public void setDashboardId(Long dashboardId) {
+ public ActiveDashboardDto setDashboardId(Long dashboardId) {
this.dashboardId = dashboardId;
+ return this;
}
/**
/**
* @param userId the userId to set
*/
- public void setUserId(Long userId) {
+ public ActiveDashboardDto setUserId(Long userId) {
this.userId = userId;
+ return this;
}
/**
/**
* @param orderIndex the orderIndex to set
*/
- public void setOrderIndex(Integer orderIndex) {
+ public ActiveDashboardDto setOrderIndex(Integer orderIndex) {
this.orderIndex = orderIndex;
+ return this;
}
@Override
}
ActiveDashboardDto that = (ActiveDashboardDto) o;
return !(id != null ? !id.equals(that.id) : that.id != null);
-
}
@Override
public int hashCode() {
- return id != null ? id.hashCode() : 0;
+ return Objects.hashCode(id);
}
}
import java.util.List;
public final class WidgetDto {
-
private Long id;
private Long dashboardId;
private String key;
/**
* @param id the id to set
*/
- public void setId(Long id) {
+ public WidgetDto setId(Long id) {
this.id = id;
+ return this;
}
/**
/**
* @param dashboardId the dashboardId to set
*/
- public void setDashboardId(Long dashboardId) {
+ public WidgetDto setDashboardId(Long dashboardId) {
this.dashboardId = dashboardId;
+ return this;
}
/**
/**
* @param key the key to set
*/
- public void setKey(String key) {
+ public WidgetDto setKey(String key) {
this.key = key;
+ return this;
}
/**
/**
* @param name the name to set
*/
- public void setName(String name) {
+ public WidgetDto setName(String name) {
this.name = name;
+ return this;
}
/**
/**
* @param description the description to set
*/
- public void setDescription(String description) {
+ public WidgetDto setDescription(String description) {
this.description = description;
+ return this;
}
/**
/**
* @param columnIndex the columnIndex to set
*/
- public void setColumnIndex(Integer columnIndex) {
+ public WidgetDto setColumnIndex(Integer columnIndex) {
this.columnIndex = columnIndex;
+ return this;
}
/**
/**
* @param rowIndex the rowIndex to set
*/
- public void setRowIndex(Integer rowIndex) {
+ public WidgetDto setRowIndex(Integer rowIndex) {
this.rowIndex = rowIndex;
+ return this;
}
/**
/**
* @param configured the configured to set
*/
- public void setConfigured(boolean configured) {
+ public WidgetDto setConfigured(boolean configured) {
this.configured = configured;
+ return this;
}
/**
/**
* @param createdAt the createdAt to set
*/
- public void setCreatedAt(Date createdAt) {
+ public WidgetDto setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
+ return this;
}
/**
/**
* @param updatedAt the updatedAt to set
*/
- public void setUpdatedAt(Date updatedAt) {
+ public WidgetDto setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
+ return this;
}
/**
/**
* @param widgetPropertyDto the widgetProperty to set
*/
- public void addWidgetProperty(WidgetPropertyDto widgetPropertyDto) {
+ public WidgetDto addWidgetProperty(WidgetPropertyDto widgetPropertyDto) {
widgetPropertyDtos.add(widgetPropertyDto);
+ return this;
}
/**
* @param resourceId the resourceId to set
* @since 3.1
*/
- public void setResourceId(Integer resourceId) {
+ public WidgetDto setResourceId(Integer resourceId) {
this.resourceId = resourceId;
+ return this;
}
-
}
package org.sonar.core.dashboard;
public final class WidgetPropertyDto {
-
private Long id;
private Long widgetId;
private String key;
/**
* @param id the id to set
*/
- public void setId(Long id) {
+ public WidgetPropertyDto setId(Long id) {
this.id = id;
+ return this;
}
/**
/**
* @param widgetId the widgetId to set
*/
- public void setWidgetId(Long widgetId) {
+ public WidgetPropertyDto setWidgetId(Long widgetId) {
this.widgetId = widgetId;
+ return this;
}
/**
/**
* @param key the key to set
*/
- public void setKey(String key) {
+ public WidgetPropertyDto setKey(String key) {
this.key = key;
+ return this;
}
/**
/**
* @param value the value to set
*/
- public void setValue(String value) {
+ public WidgetPropertyDto setValue(String value) {
this.value = value;
+ return this;
}
-
}
-- All the rows inserted during Rails migrations. Rows inserted during server startup tasks (Java) are excluded : rules, profiles, metrics, ...
-INSERT INTO DASHBOARDS(ID, USER_ID, NAME, DESCRIPTION, COLUMN_LAYOUT, SHARED, IS_GLOBAL) VALUES (1, null, 'My favourites', null, '100%', true, true);
-ALTER TABLE DASHBOARDS ALTER COLUMN ID RESTART WITH 2;
-
-INSERT INTO WIDGETS(ID, DASHBOARD_ID, WIDGET_KEY, NAME, DESCRIPTION, COLUMN_INDEX, ROW_INDEX, CONFIGURED, RESOURCE_ID) VALUES (1, 1, 'filter', 'Filter', null, 1, 1, true , null);
-ALTER TABLE WIDGETS ALTER COLUMN ID RESTART WITH 2;
-
-INSERT INTO WIDGET_PROPERTIES(ID, WIDGET_ID, KEE, TEXT_VALUE) VALUES (1, 1, 'filter', 'My favourites');
-ALTER TABLE WIDGET_PROPERTIES ALTER COLUMN ID RESTART WITH 2;
-
INSERT INTO GROUP_ROLES(ID, GROUP_ID, RESOURCE_ID, ROLE) VALUES (1, 1, null, 'admin');
INSERT INTO GROUP_ROLES(ID, GROUP_ID, RESOURCE_ID, ROLE) VALUES (2, 1, null, 'default-admin');
INSERT INTO GROUP_ROLES(ID, GROUP_ID, RESOURCE_ID, ROLE) VALUES (3, 2, null, 'default-user');
private DashboardLayout layout = DashboardLayout.TWO_COLUMNS;
private ListMultimap<Integer, Widget> widgetsByColumn = ArrayListMultimap.create();
private boolean global = false;
+ private boolean activated = true;
private Dashboard() {
}
return this;
}
+ /**
+ * A dashboard can be activated for all anonymous users or users who haven't configured their own dashboards.
+ * <p>Before version 3.1 every dashboard created through this extension point was automatically activated.
+ * This is still the default behavior.</p>
+ *
+ * @since 3.1
+ */
+ public boolean isActivated() {
+ return activated;
+ }
+
+ /**
+ * Set whether the dashboard is activated for all anonymous users or users who haven't configured their own dashboards.
+ *
+ * @since 3.1
+ */
+ public Dashboard setActivated(boolean activated) {
+ this.activated = activated;
+ return this;
+ }
+
/**
* Note that this class is an inner class to avoid confusion with the extension point org.sonar.api.web.Widget.
*/
return id;
}
}
-
}
package org.sonar.server.startup;
import com.google.common.collect.Lists;
+import com.google.common.collect.Ordering;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.utils.TimeProfiler;
import org.sonar.api.web.Dashboard;
import org.sonar.api.web.DashboardTemplate;
-import org.sonar.core.dashboard.*;
+import org.sonar.core.dashboard.ActiveDashboardDao;
+import org.sonar.core.dashboard.ActiveDashboardDto;
+import org.sonar.core.dashboard.DashboardDao;
+import org.sonar.core.dashboard.DashboardDto;
+import org.sonar.core.dashboard.WidgetDto;
+import org.sonar.core.dashboard.WidgetPropertyDto;
import org.sonar.core.template.LoadedTemplateDao;
import org.sonar.core.template.LoadedTemplateDto;
-import java.util.Collections;
-import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;
* @since 2.13
*/
public final class RegisterNewDashboards {
-
private static final Logger LOG = LoggerFactory.getLogger(RegisterNewDashboards.class);
static final String DEFAULT_DASHBOARD_NAME = "Dashboard";
- private List<DashboardTemplate> dashboardTemplates;
- private DashboardDao dashboardDao;
- private ActiveDashboardDao activeDashboardDao;
- private LoadedTemplateDao loadedTemplateDao;
-
- public RegisterNewDashboards(DashboardDao dashboardDao, ActiveDashboardDao activeDashboardDao, LoadedTemplateDao loadedTemplateDao) {
- this(new DashboardTemplate[] {}, dashboardDao, activeDashboardDao, loadedTemplateDao);
- }
+ private final List<DashboardTemplate> dashboardTemplates;
+ private final DashboardDao dashboardDao;
+ private final ActiveDashboardDao activeDashboardDao;
+ private final LoadedTemplateDao loadedTemplateDao;
public RegisterNewDashboards(DashboardTemplate[] dashboardTemplatesArray, DashboardDao dashboardDao,
ActiveDashboardDao activeDashboardDao, LoadedTemplateDao loadedTemplateDao) {
if (shouldRegister(template.getName())) {
Dashboard dashboard = template.createDashboard();
DashboardDto dto = register(template.getName(), dashboard);
- if (dto != null) {
+ if ((dto != null) && (dashboard.isActivated())) {
registeredDashboards.add(dto);
}
}
protected void activate(List<DashboardDto> loadedDashboards) {
int nextOrderIndex = activeDashboardDao.selectMaxOrderIndexForNullUser() + 1;
- Collections.sort(loadedDashboards, new DashboardComparator());
- for (DashboardDto dashboardDto : loadedDashboards) {
+
+ for (DashboardDto dashboardDto : new DashboardOrdering().sortedCopy(loadedDashboards)) {
activate(dashboardDto, nextOrderIndex++);
}
}
private void activate(DashboardDto dashboardDto, int index) {
- ActiveDashboardDto activeDashboardDto = new ActiveDashboardDto();
- activeDashboardDto.setDashboardId(dashboardDto.getId());
- activeDashboardDto.setOrderIndex(index);
+ ActiveDashboardDto activeDashboardDto = new ActiveDashboardDto()
+ .setDashboardId(dashboardDto.getId())
+ .setOrderIndex(index);
activeDashboardDao.insert(activeDashboardDto);
+
LOG.info("New dashboard '" + dashboardDto.getName() + "' registered");
}
protected DashboardDto createDtoFromExtension(String name, Dashboard dashboard) {
Date now = new Date();
- DashboardDto dashboardDto = new DashboardDto();
- dashboardDto.setName(name);
- dashboardDto.setDescription(dashboard.getDescription());
- dashboardDto.setColumnLayout(dashboard.getLayout().getCode());
- dashboardDto.setShared(true);
- dashboardDto.setGlobal(dashboard.isGlobal());
- dashboardDto.setCreatedAt(now);
- dashboardDto.setUpdatedAt(now);
+
+ DashboardDto dashboardDto = new DashboardDto()
+ .setName(name)
+ .setDescription(dashboard.getDescription())
+ .setColumnLayout(dashboard.getLayout().getCode())
+ .setShared(true)
+ .setGlobal(dashboard.isGlobal())
+ .setCreatedAt(now)
+ .setUpdatedAt(now);
for (int columnIndex = 1; columnIndex <= dashboard.getLayout().getColumns(); columnIndex++) {
List<Dashboard.Widget> widgets = dashboard.getWidgetsOfColumn(columnIndex);
for (int rowIndex = 1; rowIndex <= widgets.size(); rowIndex++) {
Dashboard.Widget widget = widgets.get(rowIndex - 1);
- WidgetDto widgetDto = new WidgetDto();
- widgetDto.setKey(widget.getId());
- widgetDto.setColumnIndex(columnIndex);
- widgetDto.setRowIndex(rowIndex);
- widgetDto.setConfigured(true);
- widgetDto.setCreatedAt(now);
- widgetDto.setUpdatedAt(now);
+ WidgetDto widgetDto = new WidgetDto()
+ .setKey(widget.getId())
+ .setColumnIndex(columnIndex)
+ .setRowIndex(rowIndex)
+ .setConfigured(true)
+ .setCreatedAt(now)
+ .setUpdatedAt(now);
dashboardDto.addWidget(widgetDto);
for (Entry<String, String> property : widget.getProperties().entrySet()) {
- WidgetPropertyDto propDto = new WidgetPropertyDto();
- propDto.setKey(property.getKey());
- propDto.setValue(property.getValue());
+ WidgetPropertyDto propDto = new WidgetPropertyDto()
+ .setKey(property.getKey())
+ .setValue(property.getValue());
widgetDto.addWidgetProperty(propDto);
}
}
-
}
return dashboardDto;
}
return loadedTemplateDao.countByTypeAndKey(LoadedTemplateDto.DASHBOARD_TYPE, dashboardName) == 0;
}
- protected static class DashboardComparator implements Comparator<DashboardDto> {
+ private static class DashboardOrdering extends Ordering<DashboardDto> {
+ @Override
public int compare(DashboardDto d1, DashboardDto d2) {
// the default dashboard must be the first one to be activated
if (d1.getName().equals(DEFAULT_DASHBOARD_NAME)) {
return d1.getName().compareTo(d2.getName());
}
}
-
}
private final FilterDao filterDao;
private final LoadedTemplateDao loadedTemplateDao;
- /**
- * Constructor used in case there is no {@link FilterTemplate}.
- */
- public RegisterNewFilters(FilterDao filterDao, LoadedTemplateDao loadedTemplateDao) {
- this(new FilterTemplate[0], filterDao, loadedTemplateDao);
- }
-
public RegisterNewFilters(FilterTemplate[] filterTemplates, FilterDao filterDao, LoadedTemplateDao loadedTemplateDao) {
this.filterTemplates = ImmutableList.copyOf(filterTemplates);
this.filterDao = filterDao;
def self.up
mark_dashboard_as_loaded('Projects')
mark_dashboard_as_loaded('Treemap')
+ mark_dashboard_as_loaded('My Favourites')
end
def self.mark_dashboard_as_loaded(name)
*/
package org.sonar.server.startup;
-import com.google.common.collect.Lists;
+import com.google.common.collect.Iterables;
import org.hamcrest.BaseMatcher;
-import org.hamcrest.Description;
import org.junit.Before;
import org.junit.Test;
+import org.mockito.ArgumentMatcher;
import org.sonar.api.web.Dashboard;
import org.sonar.api.web.DashboardLayout;
import org.sonar.api.web.DashboardTemplate;
import org.sonar.core.template.LoadedTemplateDao;
import org.sonar.core.template.LoadedTemplateDto;
-import java.util.Collections;
+import java.util.Arrays;
import java.util.List;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import static org.sonar.test.MoreConditions.contains;
public class RegisterNewDashboardsTest {
-
private RegisterNewDashboards task;
private DashboardDao dashboardDao;
private ActiveDashboardDao activeDashboardDao;
dashboardDao = mock(DashboardDao.class);
activeDashboardDao = mock(ActiveDashboardDao.class);
loadedTemplateDao = mock(LoadedTemplateDao.class);
+ fakeDashboardTemplate = mock(DashboardTemplate.class);
- fakeDashboardTemplate = new FakeDashboard();
-
- task = new RegisterNewDashboards(new DashboardTemplate[]{fakeDashboardTemplate}, dashboardDao,
+ task = new RegisterNewDashboards(new DashboardTemplate[] {fakeDashboardTemplate}, dashboardDao,
activeDashboardDao, loadedTemplateDao);
}
@Test
public void testStart() {
+ when(fakeDashboardTemplate.createDashboard()).thenReturn(Dashboard.create());
+
task.start();
+
verify(dashboardDao).insert(any(DashboardDto.class));
verify(loadedTemplateDao).insert(any(LoadedTemplateDto.class));
verify(activeDashboardDao).insert(any(ActiveDashboardDto.class));
@Test
public void shouldNotRegister() {
when(loadedTemplateDao.countByTypeAndKey(LoadedTemplateDto.DASHBOARD_TYPE, "Fake")).thenReturn(1);
- assertThat(task.shouldRegister("Fake"), is(false));
+
+ assertThat(task.shouldRegister("Fake")).isFalse();
}
@Test
public void shouldRegisterDashboard() {
when(loadedTemplateDao.countByTypeAndKey(LoadedTemplateDto.DASHBOARD_TYPE, "Fake")).thenReturn(0);
- assertThat(task.shouldRegister("Fake"), is(true));
+
+ assertThat(task.shouldRegister("Fake")).isTrue();
}
@Test
- public void testRegisterDashboard() {
+ public void should_register_and_activate_dashboard() {
+ when(fakeDashboardTemplate.createDashboard()).thenReturn(Dashboard.create());
+
DashboardDto dashboardDto = task.register("Fake", fakeDashboardTemplate.createDashboard());
- assertNotNull(dashboardDto);
- verify(dashboardDao).insert(dashboardDto);
+ verify(dashboardDao).insert(any(DashboardDto.class));
verify(loadedTemplateDao).insert(eq(new LoadedTemplateDto("Fake", LoadedTemplateDto.DASHBOARD_TYPE)));
}
@Test
- public void shouldCreateDtoFromExtension() {
- DashboardDto dto = task.createDtoFromExtension("Fake", fakeDashboardTemplate.createDashboard());
- assertThat(dto.getUserId(), is(nullValue()));
- assertThat(dto.getName(), is("Fake"));
- assertThat(dto.getDescription(), nullValue());
- assertThat(dto.getColumnLayout(), is("30%-70%"));
- assertThat(dto.getShared(), is(true));
- assertThat(dto.getGlobal(), is(true));
- assertNotNull(dto.getCreatedAt());
- assertNotNull(dto.getUpdatedAt());
-
- WidgetDto widgetDto = dto.getWidgets().iterator().next();
- assertThat(widgetDto.getKey(), is("fake-widget"));
- assertThat(widgetDto.getDescription(), is(nullValue()));
- assertThat(widgetDto.getColumnIndex(), is(1));
- assertThat(widgetDto.getRowIndex(), is(1));
- assertThat(widgetDto.getConfigured(), is(true));
- assertNotNull(widgetDto.getCreatedAt());
- assertNotNull(widgetDto.getUpdatedAt());
-
- WidgetPropertyDto widgetPropertyDto = widgetDto.getWidgetProperties().iterator().next();
- assertThat(widgetPropertyDto.getKey(), is("fake-property"));
- assertThat(widgetPropertyDto.getValue(), is("fake_metric"));
- }
+ public void should_activate_dashboard() {
+ Dashboard dashboard = Dashboard.create();
+ when(fakeDashboardTemplate.createDashboard()).thenReturn(dashboard);
- @Test
- public void shouldCompareDashboards() throws Exception {
- DashboardDto d1 = new DashboardDto().setName("Foo");
- DashboardDto d2 = new DashboardDto().setName("Bar");
- List<DashboardDto> dashboardDtos = Lists.newArrayList(d1, d2);
- Collections.sort(dashboardDtos, new RegisterNewDashboards.DashboardComparator());
+ task.start();
- assertThat(dashboardDtos.get(0).getName(), is("Bar"));
+ verify(activeDashboardDao).insert(any(ActiveDashboardDto.class));
}
@Test
- public void shouldActivateDashboards() throws Exception {
- DashboardDto d1 = new DashboardDto().setName("Foo").setId(14L);
- DashboardDto d2 = new DashboardDto().setName("Bar").setId(16L);
- List<DashboardDto> loadedDashboards = Lists.newArrayList(d1, d2);
+ public void should_disable_activation() {
+ Dashboard dashboard = Dashboard.create();
+ dashboard.setActivated(false);
+ when(fakeDashboardTemplate.createDashboard()).thenReturn(dashboard);
+
+ task.start();
- when(activeDashboardDao.selectMaxOrderIndexForNullUser()).thenReturn(4);
+ verify(activeDashboardDao, never()).insert(any(ActiveDashboardDto.class));
+ }
- task.activate(loadedDashboards);
+ @Test
+ public void shouldCreateDtoFromExtension() {
+ Dashboard dashboard = Dashboard.create()
+ .setGlobal(true)
+ .setLayout(DashboardLayout.TWO_COLUMNS_30_70);
+ Dashboard.Widget widget = dashboard.addWidget("fake-widget", 1);
+ widget.setProperty("fake-property", "fake_metric");
+ when(fakeDashboardTemplate.createDashboard()).thenReturn(dashboard);
- verify(activeDashboardDao).insert(argThat(matchActiveDashboard(16L, 5)));
- verify(activeDashboardDao).insert(argThat(matchActiveDashboard(14L, 6)));
+ DashboardDto dto = task.createDtoFromExtension("Fake", fakeDashboardTemplate.createDashboard());
+ assertThat(dto.getUserId()).isNull();
+ assertThat(dto.getName()).isEqualTo("Fake");
+ assertThat(dto.getDescription()).isNull();
+ assertThat(dto.getColumnLayout()).isEqualTo("30%-70%");
+ assertThat(dto.getShared()).isTrue();
+ assertThat(dto.getGlobal()).isTrue();
+ assertThat(dto.getCreatedAt()).isNotNull();
+ assertThat(dto.getUpdatedAt()).isNotNull();
+
+ WidgetDto widgetDto = Iterables.getOnlyElement(dto.getWidgets());
+ assertThat(widgetDto.getKey()).isEqualTo("fake-widget");
+ assertThat(widgetDto.getDescription()).isNull();
+ assertThat(widgetDto.getColumnIndex()).isEqualTo(1);
+ assertThat(widgetDto.getRowIndex()).isEqualTo(1);
+ assertThat(widgetDto.getConfigured()).isTrue();
+ assertThat(widgetDto.getCreatedAt()).isNotNull();
+ assertThat(widgetDto.getUpdatedAt()).isNotNull();
+
+ assertThat(widgetDto.getWidgetProperties()).satisfies(contains(new WidgetPropertyDto().setKey("fake-property").setValue("fake_metric")));
}
@Test
- public void defaultDashboardShouldBeTheFirstActivatedDashboard() throws Exception {
- DashboardDto defaultDashboard = new DashboardDto()
- .setName(RegisterNewDashboards.DEFAULT_DASHBOARD_NAME)
- .setId(10L);
- DashboardDto other = new DashboardDto()
- .setName("Bar")
- .setId(11L);
- List<DashboardDto> dashboards = Lists.newArrayList(other, defaultDashboard);
+ public void defaultDashboardShouldBeTheFirstActivatedDashboard() {
+ DashboardDto defaultDashboard = new DashboardDto().setId(10L).setName(RegisterNewDashboards.DEFAULT_DASHBOARD_NAME);
+ DashboardDto second = new DashboardDto().setId(11L).setName("Bar");
+ DashboardDto third = new DashboardDto().setId(12L).setName("Foo");
+ List<DashboardDto> dashboards = Arrays.asList(third, defaultDashboard, second);
task.activate(dashboards);
- verify(activeDashboardDao).insert(argThat(matchActiveDashboard(10L, 1)));
- verify(activeDashboardDao).insert(argThat(matchActiveDashboard(11L, 2)));
+ verify(activeDashboardDao).insert(argThat(matchActiveDashboardDto(10L, 1)));
+ verify(activeDashboardDao).insert(argThat(matchActiveDashboardDto(11L, 2)));
+ verify(activeDashboardDao).insert(argThat(matchActiveDashboardDto(12L, 3)));
}
- private BaseMatcher<ActiveDashboardDto> matchActiveDashboard(final long dashboardId, final int orderId) {
- return new BaseMatcher<ActiveDashboardDto>() {
+ private BaseMatcher<ActiveDashboardDto> matchActiveDashboardDto(final long dashboardId, final int orderId) {
+ return new ArgumentMatcher<ActiveDashboardDto>() {
+ @Override
public boolean matches(Object o) {
ActiveDashboardDto dto = (ActiveDashboardDto) o;
return dto.getDashboardId() == dashboardId && dto.getOrderIndex() == orderId;
}
-
- public void describeTo(Description description) {
- }
};
}
-
- public class FakeDashboard extends DashboardTemplate {
- @Override
- public String getName() {
- return "Fake";
- }
-
- @Override
- public Dashboard createDashboard() {
- Dashboard dashboard = Dashboard.create();
- dashboard.setGlobal(true);
- dashboard.setLayout(DashboardLayout.TWO_COLUMNS_30_70);
- Dashboard.Widget widget = dashboard.addWidget("fake-widget", 1);
- widget.setProperty("fake-property", "fake_metric");
- return dashboard;
- }
- }
}
verify(loadedTemplateDao).insert(any(LoadedTemplateDto.class));
}
- @Test
- public void should_insert_nothing_if_no_template_is_available() {
- register = new RegisterNewFilters(filterDao, loadedTemplateDao);
- register.start();
-
- verify(filterDao, never()).insert(any(FilterDto.class));
- verify(loadedTemplateDao, never()).insert(any(LoadedTemplateDto.class));
- }
-
@Test
public void should_insert_nothing_if_templates_are_alreday_loaded() {
when(loadedTemplateDao.countByTypeAndKey(eq(LoadedTemplateDto.FILTER_TYPE), anyString())).thenReturn(1);
}
};
}
+
+ public static Condition<Object> reflectionEqualTo(final Object expected) {
+ return new Condition<Object>() {
+ @Override
+ public boolean matches(Object actual) {
+ return EqualsBuilder.reflectionEquals(expected, actual);
+ }
+ };
+ }
}
import java.util.Arrays;
import java.util.Collection;
+import static org.sonar.test.MoreConditions.reflectionEqualTo;
+
import static org.fest.assertions.Assertions.assertThat;
import static org.sonar.test.MoreConditions.contains;
import static org.sonar.test.MoreConditions.equalsIgnoreEOL;
assertThat(collection).doesNotSatisfy(contains(new Bean("", "")));
}
+ @Test
+ public void should_compare_using_reflection() {
+ Bean bean1 = new Bean("key1", "value1");
+ Bean bean2 = new Bean("key2", "value2");
+
+ assertThat(bean1).is(reflectionEqualTo(bean1));
+ assertThat(bean2).is(reflectionEqualTo(bean2));
+ assertThat(bean1).isNot(reflectionEqualTo(bean2));
+ assertThat(bean2).isNot(reflectionEqualTo(bean1));
+ assertThat(bean1).isNot(reflectionEqualTo(null));
+ assertThat(bean2).isNot(reflectionEqualTo(null));
+ assertThat(bean1).isNot(reflectionEqualTo(new Object()));
+ assertThat(bean2).isNot(reflectionEqualTo(new Object()));
+ assertThat(bean1).isNot(reflectionEqualTo(new Bean("key1", "value2")));
+ assertThat(bean1).isNot(reflectionEqualTo(new Bean("key2", "value1")));
+ assertThat(bean1).isNot(reflectionEqualTo(new Bean("", "")));
+ }
+
static final class Bean {
final String key;
final String value;