Skip to content
Permalink
Browse files

Fix AdjustDashboardPosition Migration (#5763)

* Fix AdjustDasbboardPostion Migration

Prior to this change, the migration expected that all postions
have a "width", "heigth", "row" and "col" field.
Some users did not have a "width" or "height" field.

This change will add default values for the fields in the
getPositions functions of DashboardImpl.

Also fix some warnings.

Fixes #5737

* Add more dashboards to the test

Also: Fix annotations made by @dennisoelkers

* Fix error of maven licencs module

* Fix further annotations from @dennisoelkers
  • Loading branch information...
kmerz authored and dennisoelkers committed Mar 12, 2019
1 parent 4d98bd7 commit 714fb665f185a4190b7dcd661268244b02cf3fd8
@@ -91,10 +91,10 @@ public void setDescription(String description) {
final List<WidgetPosition> result = new ArrayList<>(positions.size());
for ( String positionId : positions.keySet() ) {
final BasicDBObject position = (BasicDBObject) positions.get(positionId);
final int width = parseInt(position.get("width").toString());
final int height = parseInt(position.get("height").toString());
final int col = parseInt(position.get("col").toString());
final int row = parseInt(position.get("row").toString());
final int width = parseInt(position.getString("width", "1"));
final int height = parseInt(position.getString("height", "1"));
final int col = parseInt(position.getString("col", "1"));
final int row = parseInt(position.getString("row","1"));
final WidgetPosition widgetPosition = WidgetPosition.builder()
.id(positionId)
.width(width)
@@ -112,7 +112,7 @@ public void setPositions(List<WidgetPosition> widgetPositions) {
checkNotNull(widgetPositions, "widgetPositions must be given");
final Map<String, Map<String, Integer>> positions = new HashMap<>(widgetPositions.size());
for (WidgetPosition widgetPosition : widgetPositions) {
Map<String, Integer> position = new HashMap(4);
Map<String, Integer> position = new HashMap<>(4);
position.put("width", widgetPosition.width());
position.put("height", widgetPosition.height());
position.put("col", widgetPosition.col());
@@ -64,7 +64,7 @@ public void upgrade() {
return;
}

Map<String, String> dashboardIds = new HashMap();
Map<String, String> dashboardIds = new HashMap<>();
for (Dashboard dashboard : dashboardService.all()) {
final List<WidgetPosition> oldPositions = dashboard.getPositions();
if (oldPositions.isEmpty()) {
@@ -74,10 +74,10 @@ public void upgrade() {
final List<WidgetPosition> widgetPositions = new ArrayList<>(oldPositions.size());

for (WidgetPosition position : oldPositions) {
Integer newWidth = position.width() * 2;
Integer newHeight = position.height() * 2;
Integer newCol = adjustPosition(position.col());
Integer newRow = adjustPosition(position.row());
int newWidth = position.width() * 2;
int newHeight = position.height() * 2;
int newCol = adjustPosition(position.col());
int newRow = adjustPosition(position.row());
widgetPositions.add(WidgetPosition.builder()
.id(position.id())
.width(newWidth)
@@ -99,7 +99,7 @@ public void upgrade() {

/* We double the resolution in space starting with 1.
To keep widgets on the same position we need to subtract 1 from the result. */
private Integer adjustPosition(Integer value) {
private int adjustPosition(int value) {
return value * 2 - 1;
}

@@ -25,7 +25,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

@@ -36,11 +35,8 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class V20180214093600_AdjustDashboardPositionToNewResolutionTest {
@Rule
@@ -78,7 +74,7 @@ public void doNotMigrateAnythingWithDashboardsWithoutPositions() throws Exceptio
final Dashboard dashboard = mock(Dashboard.class);
when(dashboard.getId()).thenReturn("uuu-iii-ddd");
when(dashboard.getPositions()).thenReturn(Collections.emptyList());
when(this.dashboardService.all()).thenReturn(ImmutableList.of(dashboard));
when(this.dashboardService.all()).thenReturn(Collections.singletonList(dashboard));

this.adjustDashboardResolutionMigration.upgrade();

@@ -87,20 +83,51 @@ public void doNotMigrateAnythingWithDashboardsWithoutPositions() throws Exceptio

@Test
public void doMigrateOneDashboardsPositions() throws Exception {
final List<WidgetPosition> oldPositions = new ArrayList<>(1);
oldPositions.add(WidgetPosition.builder().id("my-position-id").width(5).height(4).col(2).row(2).build());
/* Dashboard 1 */
final List<WidgetPosition> oldPositions1 = Collections.singletonList(
WidgetPosition.builder().id("my-position-id").width(5).height(4).col(2).row(2).build());

final List<WidgetPosition> newPositions = new ArrayList<>(1);
newPositions.add(WidgetPosition.builder().id("my-position-id").width(10).height(8).col(3).row(3).build());
final List<WidgetPosition> newPositions1 = Collections.singletonList(
WidgetPosition.builder().id("my-position-id").width(10).height(8).col(3).row(3).build());

final Dashboard dashboard = mock(Dashboard.class, Mockito.RETURNS_DEEP_STUBS);
when(dashboard.getPositions()).thenReturn(oldPositions);
when(dashboard.getId()).thenReturn("uuu-iii-ddd");
when(this.dashboardService.all()).thenReturn(ImmutableList.of(dashboard));
final Dashboard dashboard1 = mock(Dashboard.class);
when(dashboard1.getPositions()).thenReturn(oldPositions1);
when(dashboard1.getId()).thenReturn("uuu-iii-ddd");

/* Dashboard 2 */
final List<WidgetPosition> oldPositions2 = Collections.singletonList(
WidgetPosition.builder().id("your-position-id").width(1).height(1).col(1).row(1).build());

final List<WidgetPosition> newPositions2 = Collections.singletonList(
WidgetPosition.builder().id("your-position-id").width(2).height(2).col(1).row(1).build());

final Dashboard dashboard2 = mock(Dashboard.class);
when(dashboard2.getPositions()).thenReturn(oldPositions2);
when(dashboard2.getId()).thenReturn("uuu-iii-eee");

/* Dashboard 3 */
final List<WidgetPosition> oldPositions3 = ImmutableList.of(
WidgetPosition.builder().id("his-position-id").width(1).height(1).col(1).row(1).build(),
WidgetPosition.builder().id("her-position-id").width(2).height(2).col(2).row(2).build());

final List<WidgetPosition> newPositions3 = ImmutableList.of(
WidgetPosition.builder().id("his-position-id").width(2).height(2).col(1).row(1).build(),
WidgetPosition.builder().id("her-position-id").width(4).height(4).col(3).row(3).build());

final Dashboard dashboard3 = mock(Dashboard.class);
when(dashboard3.getPositions()).thenReturn(oldPositions3);
when(dashboard3.getId()).thenReturn("uuu-iii-eee");

List<Dashboard> dashboards = ImmutableList.of(dashboard1, dashboard2, dashboard3);
when(this.dashboardService.all()).thenReturn(dashboards);

this.adjustDashboardResolutionMigration.upgrade();

verify(dashboard).setPositions(newPositions);
verify(this.dashboardService, times(1)).save(dashboard);
verify(dashboard1).setPositions(newPositions1);
verify(this.dashboardService, times(1)).save(dashboard1);
verify(dashboard2).setPositions(newPositions2);
verify(this.dashboardService, times(1)).save(dashboard2);
verify(dashboard3).setPositions(newPositions3);
verify(this.dashboardService, times(1)).save(dashboard3);
}
}

0 comments on commit 714fb66

Please sign in to comment.
You can’t perform that action at this time.