From 63412d5fcedfefabd337e08be7feca7fc8d5d98b Mon Sep 17 00:00:00 2001 From: Benjamin Xu <32624725+benj58xu@users.noreply.github.com> Date: Mon, 10 Aug 2020 19:23:58 -0400 Subject: [PATCH] cmdb remoteCreate accepting business service ID as alternative; dashboard remoteCreate behaves as remoteUpdate when existing (#155) * cmdb remoteCreate accepting business service ID as alternative; dashboard remoteCreate behaves as remoteUpdate when existing * Up version to 3.2.7-SNAPSHOT * Fix review comments --- pom.xml | 2 +- .../dashboard/request/CmdbRequest.java | 10 ++++++ .../dashboard/rest/CmdbController.java | 5 +++ .../rest/DashboardRemoteController.java | 13 ++++--- .../service/CmdbRemoteServiceImpl.java | 25 ++++++++++--- .../service/DashboardRemoteServiceImpl.java | 35 ++++++++++++++++--- .../service/DashboardServiceImpl.java | 4 +-- .../service/CmdbRemoteServiceTest.java | 33 ++++++++++++++--- .../service/DashboardRemoteServiceTest.java | 14 ++++---- 9 files changed, 113 insertions(+), 28 deletions(-) diff --git a/pom.xml b/pom.xml index 5bdd9127..99be9b1d 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ api jar ${project.groupId}:${project.artifactId} - 3.2.6-SNAPSHOT + 3.2.7-SNAPSHOT Hygieia Rest API Layer https://github.com/Hygieia/api diff --git a/src/main/java/com/capitalone/dashboard/request/CmdbRequest.java b/src/main/java/com/capitalone/dashboard/request/CmdbRequest.java index cf7c2e21..a6859896 100644 --- a/src/main/java/com/capitalone/dashboard/request/CmdbRequest.java +++ b/src/main/java/com/capitalone/dashboard/request/CmdbRequest.java @@ -16,6 +16,9 @@ public class CmdbRequest { private String assignmentGroup; @NotNull private String ownerDept; + + private String businessService; + /** * commonName Human readable value of the configurationItem */ @@ -92,4 +95,11 @@ public String getToolName() { public void setToolName(String toolName) { this.toolName = toolName; } + + public String getBusinessService() { + return businessService; + } + + public void setBusinessService(String businessService) { this.businessService = businessService; } + } diff --git a/src/main/java/com/capitalone/dashboard/rest/CmdbController.java b/src/main/java/com/capitalone/dashboard/rest/CmdbController.java index f2786954..c07b8d3e 100644 --- a/src/main/java/com/capitalone/dashboard/rest/CmdbController.java +++ b/src/main/java/com/capitalone/dashboard/rest/CmdbController.java @@ -6,6 +6,8 @@ import com.capitalone.dashboard.service.CmdbRemoteService; import com.capitalone.dashboard.service.CmdbService; import com.capitalone.dashboard.util.PaginationHeaderUtility; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -31,6 +33,8 @@ public class CmdbController { private final CmdbRemoteService cmdbRemoteService; private PaginationHeaderUtility paginationHeaderUtility; + private static final Logger LOGGER = LoggerFactory.getLogger(CmdbController.class); + @Autowired public CmdbController(CmdbService cmdbService, PaginationHeaderUtility paginationHeaderUtility, CmdbRemoteService cmdbRemoteService ) { @@ -62,6 +66,7 @@ public ResponseEntity remoteCreateDashboard( @Valid @RequestBody CmdbReq .status( HttpStatus.CREATED ) .body( cmdbRemoteService.remoteCreate( request ) ); } catch (HygieiaException he) { + LOGGER.error("Failed to create cmdb entry", he); return ResponseEntity .status( HttpStatus.BAD_REQUEST ) .body( he.getMessage() ); diff --git a/src/main/java/com/capitalone/dashboard/rest/DashboardRemoteController.java b/src/main/java/com/capitalone/dashboard/rest/DashboardRemoteController.java index 997e55e0..b1901da6 100644 --- a/src/main/java/com/capitalone/dashboard/rest/DashboardRemoteController.java +++ b/src/main/java/com/capitalone/dashboard/rest/DashboardRemoteController.java @@ -5,6 +5,9 @@ import com.capitalone.dashboard.model.Dashboard; import com.capitalone.dashboard.request.DashboardRemoteRequest; import com.capitalone.dashboard.service.DashboardRemoteService; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -20,9 +23,9 @@ @RestController public class DashboardRemoteController { -// private static final Logger LOGGER = LoggerFactory.getLogger(DashboardRemoteController.class); - private final DashboardRemoteService dashboardRemoteService; + private static final Logger LOGGER = LoggerFactory.getLogger(DashboardRemoteController.class); + private final DashboardRemoteService dashboardRemoteService; @Autowired public DashboardRemoteController(DashboardRemoteService dashboardRemoteService) { @@ -37,7 +40,8 @@ public ResponseEntity remoteCreateDashboard(@Valid @RequestBody Dashboar return ResponseEntity .status(HttpStatus.CREATED) .body("Successfully created dashboard: id =" + dashboard.getId()); - } catch (HygieiaException he) { + } catch (Exception he) { + LOGGER.error("RemoteCreate receives exception", he); return ResponseEntity .status(HttpStatus.BAD_REQUEST) .body("Failed to create dashboard. Error: " + he.getMessage()); @@ -52,7 +56,8 @@ public ResponseEntity remoteUpdateDashboard(@Valid @RequestBody Dashboar return ResponseEntity .status(HttpStatus.CREATED) .body("Successfully updated dashboard: id =" + dashboard.getId()); - } catch (HygieiaException he) { + } catch (Exception he) { + LOGGER.error("RemoteUpdate receives exception", he); return ResponseEntity .status(HttpStatus.BAD_REQUEST) .body("Failed to update dashboard. Error: " + he.getMessage()); diff --git a/src/main/java/com/capitalone/dashboard/service/CmdbRemoteServiceImpl.java b/src/main/java/com/capitalone/dashboard/service/CmdbRemoteServiceImpl.java index d1527871..e2a7f21e 100644 --- a/src/main/java/com/capitalone/dashboard/service/CmdbRemoteServiceImpl.java +++ b/src/main/java/com/capitalone/dashboard/service/CmdbRemoteServiceImpl.java @@ -44,6 +44,8 @@ public CmdbRemoteServiceImpl( @Override public Cmdb remoteCreate(CmdbRequest request ) throws HygieiaException { + Cmdb businessService = findBusinessService(request); + validateRequest(request); updateRelationship(request); @@ -77,18 +79,33 @@ private void updateRelationship( CmdbRequest request ) { } } + private Cmdb findBusinessService(CmdbRequest request) throws HygieiaException { + String businessService = request.getBusinessService(); + String busServiceName = request.getConfigurationItemBusServName(); + Cmdb cmdb = null; + if (!StringUtils.isEmpty(businessService)) { + cmdb = cmdbRepository.findByConfigurationItemAndItemType(businessService, APP_TYPE); + if (cmdb == null) { + throw new HygieiaException("Configuration Item " + businessService + " does not exist", HygieiaException.BAD_DATA); + } + } else if (!StringUtils.isEmpty(busServiceName)) { + cmdb = cmdbRepository.findByConfigurationItemAndItemType(busServiceName, APP_TYPE); + if (cmdb == null) { + throw new HygieiaException("Configuration Item " + busServiceName + " does not exist", HygieiaException.BAD_DATA); + } + } + return cmdb; + } + /** * Validates CmdbRequest for errors * @param request * @throws HygieiaException */ private void validateRequest(CmdbRequest request) throws HygieiaException { - String busServiceName = request.getConfigurationItemBusServName(); - if(!StringUtils.isEmpty( busServiceName ) && cmdbRepository.findByConfigurationItemAndItemType( busServiceName, APP_TYPE ) == null){ - throw new HygieiaException("Configuration Item " + busServiceName + " does not exist", HygieiaException.BAD_DATA); - } Cmdb cmdb = cmdbRepository.findByConfigurationItemIgnoreCaseOrCommonNameIgnoreCase(request.getConfigurationItem(), request.getCommonName()); + if(cmdb != null){ throw new HygieiaException("Configuration Item " + cmdb.getConfigurationItem() + " already exists", HygieiaException.DUPLICATE_DATA); } diff --git a/src/main/java/com/capitalone/dashboard/service/DashboardRemoteServiceImpl.java b/src/main/java/com/capitalone/dashboard/service/DashboardRemoteServiceImpl.java index c78d5ffd..dc64fc60 100644 --- a/src/main/java/com/capitalone/dashboard/service/DashboardRemoteServiceImpl.java +++ b/src/main/java/com/capitalone/dashboard/service/DashboardRemoteServiceImpl.java @@ -115,15 +115,19 @@ public Dashboard remoteCreate(DashboardRemoteRequest request, boolean isUpdate) List dashboards = findExistingDashboardsFromRequest( request ); if (!CollectionUtils.isEmpty(dashboards)) { - dashboard = dashboards.get(0); + if (dashboards.size()==1) { + dashboard = dashboards.get(0); + } else { + dashboard = chooseDashboard(dashboards, request); + } Set uniqueOwners = new HashSet(validOwners); uniqueOwners.addAll(dashboard.getOwners()); dashboard.setOwners(new ArrayList(uniqueOwners)); dashboard.setConfigurationItemBusAppName(request.getMetaData().getBusinessApplication()); dashboard.setConfigurationItemBusServName(request.getMetaData().getBusinessService()); - if (!isUpdate) { - throw new HygieiaException("Dashboard " + dashboard.getTitle() + " (id =" + dashboard.getId() + ") already exists", HygieiaException.DUPLICATE_DATA); - } +// if (!isUpdate) { +// throw new HygieiaException("Dashboard " + dashboard.getTitle() + " (id =" + dashboard.getId() + ") already exists", HygieiaException.DUPLICATE_DATA); +// } dashboardService.update(dashboard); //Save the widgets for (Widget w : dashboard.getWidgets()) { @@ -194,6 +198,27 @@ public Dashboard remoteCreate(DashboardRemoteRequest request, boolean isUpdate) return (dashboard != null) ? dashboardService.get(dashboard.getId()) : null; } + private Dashboard chooseDashboard(List dashboards, DashboardRemoteRequest request) { + Dashboard dashboard = null; + String businessService = request.getMetaData().getBusinessService(); + String businessApplication = request.getMetaData().getBusinessApplication(); + String title = request.getMetaData().getTitle(); + for (Dashboard one : dashboards) { + if (dashboard==null) { + dashboard = one; + } else if (one.getUpdatedAt()>dashboard.getUpdatedAt()) { + dashboard = one; + } else if (one.getUpdatedAt()==dashboard.getUpdatedAt()) { + if (one.getCreatedAt()>dashboard.getCreatedAt()) { + dashboard = one; + } + } + } + LOG.warn(String.format("MultipleDashboards=%d, businessService=%s, businessApplication=%s, title=%s, selected=%s", + dashboards.size(), businessService, businessApplication, title, dashboard.getId())); + return dashboard; + } + /** * Generates a Widget Request list of Widgets to be created from the request * @param entries @@ -244,7 +269,7 @@ private List< Dashboard > findExistingDashboardsFromRequest( DashboardRemoteRequ List existing = new ArrayList<>(); if( !StringUtils.isEmpty( businessService ) && !StringUtils.isEmpty( businessApplication ) ){ existing.addAll(dashboardRepository.findAllByConfigurationItemBusServNameContainingIgnoreCaseAndConfigurationItemBusAppNameContainingIgnoreCase( businessService, businessApplication )); - }if(StringUtils.isNotEmpty(title)) { + } else if (StringUtils.isNotEmpty(title)) { existing.addAll(dashboardRepository.findByTitle( request.getMetaData().getTitle() )); } return existing; diff --git a/src/main/java/com/capitalone/dashboard/service/DashboardServiceImpl.java b/src/main/java/com/capitalone/dashboard/service/DashboardServiceImpl.java index 9a4b3b4a..25c973c6 100644 --- a/src/main/java/com/capitalone/dashboard/service/DashboardServiceImpl.java +++ b/src/main/java/com/capitalone/dashboard/service/DashboardServiceImpl.java @@ -181,10 +181,10 @@ private Dashboard create(Dashboard dashboard, boolean isUpdate) throws HygieiaEx Iterable components = null; if(!isUpdate) { + dashboard.setCreatedAt(System.currentTimeMillis()); components = componentRepository.save(dashboard.getApplication().getComponents()); - } else { - dashboard.setUpdatedAt(System.currentTimeMillis()); } + dashboard.setUpdatedAt(System.currentTimeMillis()); try { duplicateDashboardErrorCheck(dashboard); diff --git a/src/test/java/com/capitalone/dashboard/service/CmdbRemoteServiceTest.java b/src/test/java/com/capitalone/dashboard/service/CmdbRemoteServiceTest.java index 83ec7453..e8f2d777 100644 --- a/src/test/java/com/capitalone/dashboard/service/CmdbRemoteServiceTest.java +++ b/src/test/java/com/capitalone/dashboard/service/CmdbRemoteServiceTest.java @@ -49,7 +49,7 @@ public void remoteCreate() throws HygieiaException { ObjectId objectId = ObjectId.get(); expected.setId(objectId); CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype", - "type", "assignmentgroup","owner", "BAPTEST", "", "cmdbCollector"); + "type", "assignmentgroup","owner", "BAPTEST", null, "", "cmdbCollector"); when(cmdbRepository.findByConfigurationItemAndItemType("","" )).thenReturn(null); when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem()); when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB)); @@ -69,7 +69,7 @@ public void remoteCreateInvalidBusService() throws HygieiaException { ObjectId objectId = ObjectId.get(); expected.setId(objectId); CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype", - "type", "assignmentgroup","owner", "BAPTEST", "ASVTEST", "cmdbCollector"); + "type", "assignmentgroup","owner", "BAPTEST", null,"ASVTEST", "cmdbCollector"); when(cmdbRepository.findByConfigurationItemAndItemType("","" )).thenReturn(null); when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem()); when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB)); @@ -84,6 +84,29 @@ public void remoteCreateInvalidBusService() throws HygieiaException { assertEquals(excep.getMessage(), e.getMessage()); } } + + /** + * Test the use of businessService instead of configurationItemBusServName in the request. + * @throws HygieiaException + */ + @Test + public void remoteCreateUsingBusinessService() throws HygieiaException { + Cmdb businessServiceItem = makeCmdbItem("CI123456", "subtype", + "type", "assignmentgroup","owner", "ASVTEST"); + Cmdb expected = makeCmdbItem("BAPTEST", "subtype", + "type", "assignmentgroup","owner", "BAPTEST"); + ObjectId objectId = ObjectId.get(); + expected.setId(objectId); + CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype", + "type", "assignmentgroup","owner", "BAPTEST", "CI123456", "", "cmdbCollector"); + when(cmdbRepository.findByConfigurationItemAndItemType("CI123456","app" )).thenReturn(businessServiceItem); + when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem()); + when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB)); + when(cmdbRepository.save(Matchers.any(Cmdb.class))).thenReturn(expected); + + assertThat(cmdbRemoteService.remoteCreate(request), is(expected)); + } + /** * Tests remoteCreate functionality ConfigurationItemBusServName doesn't have existing relationships * @throws HygieiaException @@ -97,7 +120,7 @@ public void remoteCreateRelationshipUpdateExistingCompsNull() throws HygieiaExce ObjectId objectId = ObjectId.get(); expected.setId(objectId); CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype", - "type", "assignmentgroup","owner", "BAPTEST", "ASVTEST", "cmdbCollector"); + "type", "assignmentgroup","owner", "BAPTEST", null,"ASVTEST", "cmdbCollector"); when(cmdbRepository.findByConfigurationItemAndItemType("","" )).thenReturn(null); when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem()); when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB)); @@ -122,7 +145,7 @@ public void remoteCreateRelationshipUpdateExistingComps() throws HygieiaExceptio ObjectId objectId = ObjectId.get(); expected.setId(objectId); CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype", - "type", "assignmentgroup","owner", "BAPTEST", "ASVTEST", "cmdbCollector"); + "type", "assignmentgroup","owner", "BAPTEST", null,"ASVTEST", "cmdbCollector"); when(cmdbRepository.findByConfigurationItemAndItemType("","" )).thenReturn(null); when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem()); when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB)); @@ -154,6 +177,7 @@ private CmdbRequest makeCmdbRequest(String configurationItem, String assignmentGroup, String ownerDept, String commonName, + String businessService, String configurationItemBusServName, String toolName){ @@ -164,6 +188,7 @@ private CmdbRequest makeCmdbRequest(String configurationItem, request.setAssignmentGroup(assignmentGroup); request.setOwnerDept(ownerDept); request.setCommonName(commonName); + request.setBusinessService(businessService); request.setConfigurationItemBusServName(configurationItemBusServName); request.setToolName(toolName); return request; diff --git a/src/test/java/com/capitalone/dashboard/service/DashboardRemoteServiceTest.java b/src/test/java/com/capitalone/dashboard/service/DashboardRemoteServiceTest.java index 99db8d5b..d1e99593 100644 --- a/src/test/java/com/capitalone/dashboard/service/DashboardRemoteServiceTest.java +++ b/src/test/java/com/capitalone/dashboard/service/DashboardRemoteServiceTest.java @@ -40,9 +40,10 @@ import java.util.Set; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.junit.Assert.assertNotNull; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {ApiTestConfig.class, FongoConfig.class}) @@ -210,18 +211,15 @@ public void remoteCreateInvalidCompAndApp() throws IOException { } } @Test - public void remoteCreateDuplicateDashboard() throws IOException { + public void remoteCreateDuplicateDashboard() throws HygieiaException, IOException { DashboardRemoteRequest request = getRemoteRequest("./dashboardRemoteRequests/0-Remote-Update-Repo.json"); Dashboard dashboard = dashboardRepository.findByTitle(request.getMetaData().getTitle()).get(0); Throwable t = new Throwable(); RuntimeException excep = new RuntimeException("Dashboard "+dashboard.getTitle()+" (id =" + dashboard.getId() + ") already exists", t); - try { - dashboardRemoteService.remoteCreate(request, false); - fail("Should throw RuntimeException"); - } catch(Exception e) { - assertEquals(excep.getMessage(), e.getMessage()); - } + Dashboard found = dashboardRemoteService.remoteCreate(request, false); + assertEquals(dashboard.getId(), found.getId()); + assertTrue(found.getUpdatedAt()>dashboard.getUpdatedAt()); } @Test public void remoteCreate() throws HygieiaException, IOException {