Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Commit

Permalink
add v2 add/update/delete widget logic for new UI (#143)
Browse files Browse the repository at this point in the history
* update dashboard updatedAt timestamp

* add v2 add/update/delete widget logic for new UI

* update dashboardservice spec

* update dashboardservice spec again
  • Loading branch information
cschristine authored May 6, 2020
1 parent 9e24156 commit 87af42e
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,22 @@ public ResponseEntity<WidgetResponse> addWidget(@PathVariable ObjectId id, @Requ
Dashboard dashboard = dashboardService.get(id);

Component component = dashboardService.associateCollectorToComponent(
request.getComponentId(), request.getCollectorItemIds());
request.getComponentId(), request.getCollectorItemIds(), true);

Widget widget = dashboardService.addWidget(dashboard, request.widget());

return ResponseEntity.status(HttpStatus.CREATED).body(new WidgetResponse(component, widget));
}

@DashboardOwnerOrAdmin
@RequestMapping(value = "/v2/dashboard/{id}/widget", method = POST,
consumes = APPLICATION_JSON_VALUE)
public ResponseEntity<WidgetResponse> addWidgetV2(@PathVariable ObjectId id, @RequestBody WidgetRequest request) {

Dashboard dashboard = dashboardService.get(id);

Component component = dashboardService.associateCollectorToComponent(
request.getComponentId(), request.getCollectorItemIds(), false);

Widget widget = dashboardService.addWidget(dashboard, request.widget());

Expand All @@ -178,7 +193,23 @@ public ResponseEntity<WidgetResponse> updateWidget(@PathVariable ObjectId id,
@PathVariable ObjectId widgetId,
@RequestBody WidgetRequest request) {
Component component = dashboardService.associateCollectorToComponent(
request.getComponentId(), request.getCollectorItemIds());
request.getComponentId(), request.getCollectorItemIds(), true);

Dashboard dashboard = dashboardService.get(id);
Widget widget = request.updateWidget(dashboardService.getWidget(dashboard, widgetId));
widget = dashboardService.updateWidget(dashboard, widget);

return ResponseEntity.ok().body(new WidgetResponse(component, widget));
}

@DashboardOwnerOrAdmin
@RequestMapping(value = "/v2/dashboard/{id}/widget/{widgetId}", method = PUT,
consumes = APPLICATION_JSON_VALUE)
public ResponseEntity<WidgetResponse> updateWidgetV2(@PathVariable ObjectId id,
@PathVariable ObjectId widgetId,
@RequestBody WidgetRequest request) {
Component component = dashboardService.associateCollectorToComponent(
request.getComponentId(), request.getCollectorItemIds(), false);

Dashboard dashboard = dashboardService.get(id);
Widget widget = request.updateWidget(dashboardService.getWidget(dashboard, widgetId));
Expand Down Expand Up @@ -313,7 +344,23 @@ public ResponseEntity<WidgetResponse> deleteWidget(@PathVariable ObjectId id,
@PathVariable ObjectId widgetId,
@RequestBody WidgetRequest request) {
Component component = dashboardService.associateCollectorToComponent(
request.getComponentId(), request.getCollectorItemIds());
request.getComponentId(), request.getCollectorItemIds(), true);

Dashboard dashboard = dashboardService.get(id);
Widget widget =dashboardService.getWidget(dashboard, widgetId);
dashboardService.deleteWidget(dashboard, widget,request.getComponentId());

return ResponseEntity.ok().body(new WidgetResponse(component, null));
}

@DashboardOwnerOrAdmin
@RequestMapping(value = "/v2/dashboard/{id}/deleteWidget/{widgetId}", method = PUT,
consumes = APPLICATION_JSON_VALUE)
public ResponseEntity<WidgetResponse> deleteWidgetV2(@PathVariable ObjectId id,
@PathVariable ObjectId widgetId,
@RequestBody WidgetRequest request) {
Component component = dashboardService.associateCollectorToComponent(
request.getComponentId(), request.getCollectorItemIds(), false);

Dashboard dashboard = dashboardService.get(id);
Widget widget =dashboardService.getWidget(dashboard, widgetId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public Dashboard remoteCreate(DashboardRemoteRequest request, boolean isUpdate)
for (String key : allWidgetRequests.keySet()) {
WidgetRequest widgetRequest = allWidgetRequests.get(key);

component = dashboardService.associateCollectorToComponent(dashboard.getApplication().getComponents().get(0).getId(), widgetRequest.getCollectorItemIds(),component);
component = dashboardService.associateCollectorToComponent(dashboard.getApplication().getComponents().get(0).getId(), widgetRequest.getCollectorItemIds(),component,true);
Widget newWidget = widgetRequest.widget();
if (isUpdate) {
Widget oldWidget = existingWidgets.get(newWidget.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,20 @@ public interface DashboardService {
*
* @param componentId unique identifier of the Component
* @param collectorItemIds List of unique identifier of the CollectorItem
* @param cleanupQuality flag to cleanup remaining empty fields for Quality CollectorItem types
* @return Component
*/
Component associateCollectorToComponent(ObjectId componentId, List<ObjectId> collectorItemIds);
Component associateCollectorToComponent(ObjectId componentId, List<ObjectId> collectorItemIds, boolean cleanupQuality);

/**
* Associate a CollectorItem to a Component
*
* @param componentId unique identifier of the Component
* @param collectorItemIds List of unique identifier of the CollectorItem
* @param cleanupQuality flag to cleanup remaining empty fields for Quality CollectorItem types
* @return Component
*/
Component associateCollectorToComponent(ObjectId componentId, List<ObjectId> collectorItemIds,Component component);
Component associateCollectorToComponent(ObjectId componentId, List<ObjectId> collectorItemIds,Component component, boolean cleanupQuality);

/**
* Creates a new Widget and adds it to the Dashboard indicated by the dashboardId parameter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,27 +295,27 @@ private boolean isSafeTeamDashboardDelete(Dashboard dashboard) {
}

@Override
public Component associateCollectorToComponent(ObjectId componentId, List<ObjectId> collectorItemIds) {
public Component associateCollectorToComponent(ObjectId componentId, List<ObjectId> collectorItemIds, boolean cleanupQuality) {
if (componentId == null || collectorItemIds == null) {
// Not all widgets gather data from collectors
return null;
}
com.capitalone.dashboard.model.Component component = componentRepository.findOne(componentId); //NOPMD - using fully qualified name for clarity
associateCollectorItemsToComponent(collectorItemIds, true, component);
associateCollectorItemsToComponent(collectorItemIds, true, component, cleanupQuality);
return component;
}

@Override
public Component associateCollectorToComponent(ObjectId componentId, List<ObjectId> collectorItemIds,Component component) {
public Component associateCollectorToComponent(ObjectId componentId, List<ObjectId> collectorItemIds,Component component, boolean cleanupQuality) {
if (componentId == null || collectorItemIds == null) {
// Not all widgets gather data from collectors
return null;
}
associateCollectorItemsToComponent(collectorItemIds, false, component);
associateCollectorItemsToComponent(collectorItemIds, false, component, cleanupQuality);
return component;
}

private void associateCollectorItemsToComponent(List<ObjectId> collectorItemIds, boolean save, Component component) {
private void associateCollectorItemsToComponent(List<ObjectId> collectorItemIds, boolean save, Component component, boolean cleanupQuality) {
final String METHOD_NAME = "DashboardServiceImpl.associateCollectorToComponent :";
//First: disable all collectorItems of the Collector TYPEs that came in with the request.
//Second: remove all the collectorItem association of the Collector Type that came in
Expand Down Expand Up @@ -352,7 +352,7 @@ private void associateCollectorItemsToComponent(List<ObjectId> collectorItemIds,
}

// If a collector type is within the code analysis widget, check to see if any of the remaining fields were passed values
if(incomingTypes.stream().anyMatch(QualityWidget::contains)){
if(incomingTypes.stream().anyMatch(QualityWidget::contains) && cleanupQuality){
if(!incomingTypes.contains(CollectorType.Test)){
component.getCollectorItems().remove(CollectorType.Test);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public void addWidget() throws Exception {
widgetWithId.setId(ObjectId.get());
Component component = makeComponent(compId, "Component", CollectorType.Build, collId);
when(dashboardService.get(dashId)).thenReturn(d1);
when(dashboardService.associateCollectorToComponent(compId, collIds)).thenReturn(component);
when(dashboardService.associateCollectorToComponent(compId, collIds, true)).thenReturn(component);
when(dashboardService.addWidget(Matchers.any(Dashboard.class), Matchers.any(Widget.class))).thenReturn(widgetWithId);
mockMvc.perform(post("/dashboard/" + dashId.toString() + "/widget")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,52 @@ public void associateCollectorToComponent() {
when(collectorRepository.findOne(collId)).thenReturn(collector);
when(componentRepository.findOne(compId)).thenReturn(component);

dashboardService.associateCollectorToComponent(compId, collItemIds);
dashboardService.associateCollectorToComponent(compId, collItemIds, true);

assertThat(component.getCollectorItems().get(CollectorType.Build), contains(item));

verify(componentRepository).save(component);
verify(collectorItemRepository, never()).save(item);
}

@Test
public void associateCollectorToComponentWithoutCleanUpQuality() {
ObjectId compId = ObjectId.get();
ObjectId collId1 = ObjectId.get();
ObjectId collId2 = ObjectId.get();
ObjectId collItemId1 = ObjectId.get();
ObjectId collItemId2 = ObjectId.get();
List<ObjectId> collItemIds = Arrays.asList(collItemId1,collItemId2);

CollectorItem item1 = new CollectorItem();
item1.setCollectorId(collId1);
item1.setEnabled(true);
CollectorItem item2 = new CollectorItem();
item2.setCollectorId(collId2);
item2.setEnabled(true);

Collector collector1 = new Collector();
collector1.setCollectorType(CollectorType.CodeQuality);
Collector collector2 = new Collector();
collector2.setCollectorType(CollectorType.LibraryPolicy);
Component component = new Component();

when(collectorItemRepository.findOne(collItemId1)).thenReturn(item1);
when(collectorItemRepository.findOne(collItemId2)).thenReturn(item2);
when(collectorRepository.findOne(collId1)).thenReturn(collector1);
when(collectorRepository.findOne(collId2)).thenReturn(collector2);
when(componentRepository.findOne(compId)).thenReturn(component);

dashboardService.associateCollectorToComponent(compId, collItemIds, false);

assertThat(component.getCollectorItems().get(CollectorType.CodeQuality), contains(item1));
assertThat(component.getCollectorItems().get(CollectorType.LibraryPolicy), contains(item2));

verify(componentRepository).save(component);
verify(collectorItemRepository, never()).save(item1);
verify(collectorItemRepository, never()).save(item2);
}

@Test
public void associateCollectorToComponentWithDisabledCollectorItem() {
ObjectId compId = ObjectId.get();
Expand All @@ -241,7 +279,7 @@ public void associateCollectorToComponentWithDisabledCollectorItem() {
when(collectorRepository.findOne(collId)).thenReturn(collector);
when(componentRepository.findOne(compId)).thenReturn(component);

dashboardService.associateCollectorToComponent(compId, collItemIds);
dashboardService.associateCollectorToComponent(compId, collItemIds, true);

assertThat(component.getCollectorItems().get(CollectorType.Build), contains(item));

Expand Down Expand Up @@ -273,7 +311,7 @@ public void associateCollectorToComponent_Item_with_two_components() {
when(componentRepository.findOne(compId)).thenReturn(component1);
when(customRepositoryQuery.findComponents(collector, item)).thenReturn(Arrays.asList(component1, component2));

dashboardService.associateCollectorToComponent(compId, collItemIds);
dashboardService.associateCollectorToComponent(compId, collItemIds, true);

assertThat(component1.getCollectorItems().get(CollectorType.Build), contains(item));
assertThat(item.isEnabled(), is(true));
Expand Down Expand Up @@ -301,7 +339,7 @@ public void associateCollectorToComponent_collectorItemDisabled_willBecomeEnable
when(collectorRepository.findOne(collId)).thenReturn(collector);
when(componentRepository.findOne(compId)).thenReturn(component);

dashboardService.associateCollectorToComponent(compId, collItemIds);
dashboardService.associateCollectorToComponent(compId, collItemIds, true);

assertThat(component.getCollectorItems().get(CollectorType.Build), contains(item));
assertThat(item.isEnabled(), is(true));
Expand Down Expand Up @@ -345,7 +383,7 @@ public void associateCollectorToComponent_switch_Item1_with_Item2() {
when(collectorRepository.findOne(collId)).thenReturn(collector);
when(componentRepository.findOne(compId)).thenReturn(component);

dashboardService.associateCollectorToComponent(compId, collItemIds);
dashboardService.associateCollectorToComponent(compId, collItemIds, true);

assertThat(component.getCollectorItems().get(CollectorType.Build), contains(item2));
assertThat(item1.isEnabled(), is(false));
Expand Down Expand Up @@ -393,7 +431,7 @@ public void associateCollectorToComponent_switch_Item1_with_Item2_Multiple_Compo
when(componentRepository.findOne(compId)).thenReturn(component1);
when(customRepositoryQuery.findComponents(collector, item1)).thenReturn(Arrays.asList(component1, component2));

dashboardService.associateCollectorToComponent(compId, collItemIds);
dashboardService.associateCollectorToComponent(compId, collItemIds, true);

assertThat(component1.getCollectorItems().get(CollectorType.Build), contains(item2));
assertThat(item1.isEnabled(), is(true));
Expand Down Expand Up @@ -443,7 +481,7 @@ public void associateCollectorToComponent_replace_Item1_with_Item2_and_3() {
when(collectorRepository.findOne(collId)).thenReturn(collector);
when(componentRepository.findOne(compId)).thenReturn(component);

dashboardService.associateCollectorToComponent(compId, collItemIds);
dashboardService.associateCollectorToComponent(compId, collItemIds, true);

assertThat(component.getCollectorItems().get(CollectorType.Build), contains(item2, item3));
assertThat(item1.isEnabled(), is(false));
Expand Down Expand Up @@ -497,7 +535,7 @@ public void associateCollectorToComponent_replace_Item1_and_2_with_Item3() {
when(collectorRepository.findOne(collId)).thenReturn(collector);
when(componentRepository.findOne(compId)).thenReturn(component);

dashboardService.associateCollectorToComponent(compId, collItemIds);
dashboardService.associateCollectorToComponent(compId, collItemIds, true);

assertThat(component.getCollectorItems().get(CollectorType.Build), contains(item3));
assertThat(item1.isEnabled(), is(false));
Expand Down

0 comments on commit 87af42e

Please sign in to comment.