From 87af42e7ed5cfe9dfe57c84e2010033eecf20f0d Mon Sep 17 00:00:00 2001 From: Christine Shen Date: Wed, 6 May 2020 10:27:16 -0400 Subject: [PATCH] add v2 add/update/delete widget logic for new UI (#143) * update dashboard updatedAt timestamp * add v2 add/update/delete widget logic for new UI * update dashboardservice spec * update dashboardservice spec again --- .../dashboard/rest/DashboardController.java | 53 ++++++++++++++++-- .../service/DashboardRemoteServiceImpl.java | 2 +- .../dashboard/service/DashboardService.java | 6 ++- .../service/DashboardServiceImpl.java | 12 ++--- .../rest/DashboardControllerTest.java | 2 +- .../service/DashboardServiceTest.java | 54 ++++++++++++++++--- 6 files changed, 108 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/capitalone/dashboard/rest/DashboardController.java b/src/main/java/com/capitalone/dashboard/rest/DashboardController.java index 528511c0..e244bd4b 100644 --- a/src/main/java/com/capitalone/dashboard/rest/DashboardController.java +++ b/src/main/java/com/capitalone/dashboard/rest/DashboardController.java @@ -164,7 +164,22 @@ public ResponseEntity 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 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()); @@ -178,7 +193,23 @@ public ResponseEntity 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 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)); @@ -313,7 +344,23 @@ public ResponseEntity 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 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); diff --git a/src/main/java/com/capitalone/dashboard/service/DashboardRemoteServiceImpl.java b/src/main/java/com/capitalone/dashboard/service/DashboardRemoteServiceImpl.java index 86051226..c78d5ffd 100644 --- a/src/main/java/com/capitalone/dashboard/service/DashboardRemoteServiceImpl.java +++ b/src/main/java/com/capitalone/dashboard/service/DashboardRemoteServiceImpl.java @@ -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()); diff --git a/src/main/java/com/capitalone/dashboard/service/DashboardService.java b/src/main/java/com/capitalone/dashboard/service/DashboardService.java index 1231c9df..4de554c4 100644 --- a/src/main/java/com/capitalone/dashboard/service/DashboardService.java +++ b/src/main/java/com/capitalone/dashboard/service/DashboardService.java @@ -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 collectorItemIds); + Component associateCollectorToComponent(ObjectId componentId, List 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 collectorItemIds,Component component); + Component associateCollectorToComponent(ObjectId componentId, List collectorItemIds,Component component, boolean cleanupQuality); /** * Creates a new Widget and adds it to the Dashboard indicated by the dashboardId parameter. diff --git a/src/main/java/com/capitalone/dashboard/service/DashboardServiceImpl.java b/src/main/java/com/capitalone/dashboard/service/DashboardServiceImpl.java index 59778f81..a8bb06e2 100644 --- a/src/main/java/com/capitalone/dashboard/service/DashboardServiceImpl.java +++ b/src/main/java/com/capitalone/dashboard/service/DashboardServiceImpl.java @@ -295,27 +295,27 @@ private boolean isSafeTeamDashboardDelete(Dashboard dashboard) { } @Override - public Component associateCollectorToComponent(ObjectId componentId, List collectorItemIds) { + public Component associateCollectorToComponent(ObjectId componentId, List 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 collectorItemIds,Component component) { + public Component associateCollectorToComponent(ObjectId componentId, List 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 collectorItemIds, boolean save, Component component) { + private void associateCollectorItemsToComponent(List 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 @@ -352,7 +352,7 @@ private void associateCollectorItemsToComponent(List 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); } diff --git a/src/test/java/com/capitalone/dashboard/rest/DashboardControllerTest.java b/src/test/java/com/capitalone/dashboard/rest/DashboardControllerTest.java index 3568d73a..42be194b 100644 --- a/src/test/java/com/capitalone/dashboard/rest/DashboardControllerTest.java +++ b/src/test/java/com/capitalone/dashboard/rest/DashboardControllerTest.java @@ -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) diff --git a/src/test/java/com/capitalone/dashboard/service/DashboardServiceTest.java b/src/test/java/com/capitalone/dashboard/service/DashboardServiceTest.java index c280283b..ce633ec6 100644 --- a/src/test/java/com/capitalone/dashboard/service/DashboardServiceTest.java +++ b/src/test/java/com/capitalone/dashboard/service/DashboardServiceTest.java @@ -213,7 +213,7 @@ 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)); @@ -221,6 +221,44 @@ public void associateCollectorToComponent() { 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 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(); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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));