From b8e8fc9fb9e85a921f7632fe7826570a61323340 Mon Sep 17 00:00:00 2001 From: Zhanpeng Chen Date: Wed, 18 Mar 2020 16:52:11 -0400 Subject: [PATCH 1/2] Add exception handling for bad data --- .../dashboard/rest/PipelineController.java | 4 +++- .../dashboard/service/PipelineService.java | 3 ++- .../dashboard/service/PipelineServiceImpl.java | 14 +++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/capitalone/dashboard/rest/PipelineController.java b/src/main/java/com/capitalone/dashboard/rest/PipelineController.java index 95297e11..718157f8 100644 --- a/src/main/java/com/capitalone/dashboard/rest/PipelineController.java +++ b/src/main/java/com/capitalone/dashboard/rest/PipelineController.java @@ -1,9 +1,11 @@ package com.capitalone.dashboard.rest; +import com.capitalone.dashboard.misc.HygieiaException; import com.capitalone.dashboard.model.PipelineResponse; import com.capitalone.dashboard.request.PipelineSearchRequest; import com.capitalone.dashboard.service.PipelineService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -22,7 +24,7 @@ public PipelineController(PipelineService pipelineService) { } @RequestMapping(value = "/pipeline", method = GET, produces = APPLICATION_JSON_VALUE) - public Iterable searchPipelines(@Valid PipelineSearchRequest searchRequest) { + public Iterable searchPipelines(@Valid @RequestBody PipelineSearchRequest searchRequest) throws HygieiaException { return pipelineService.search(searchRequest); } } diff --git a/src/main/java/com/capitalone/dashboard/service/PipelineService.java b/src/main/java/com/capitalone/dashboard/service/PipelineService.java index 5606edd6..bb9f8f44 100644 --- a/src/main/java/com/capitalone/dashboard/service/PipelineService.java +++ b/src/main/java/com/capitalone/dashboard/service/PipelineService.java @@ -1,5 +1,6 @@ package com.capitalone.dashboard.service; +import com.capitalone.dashboard.misc.HygieiaException; import com.capitalone.dashboard.model.PipelineResponse; import com.capitalone.dashboard.request.PipelineSearchRequest; @@ -11,5 +12,5 @@ public interface PipelineService { * @param searchRequest search request * @return all pipelines for team dashboards */ - Iterable search(PipelineSearchRequest searchRequest); + Iterable search(PipelineSearchRequest searchRequest) throws HygieiaException; } diff --git a/src/main/java/com/capitalone/dashboard/service/PipelineServiceImpl.java b/src/main/java/com/capitalone/dashboard/service/PipelineServiceImpl.java index 3f8926a7..6e4011f3 100644 --- a/src/main/java/com/capitalone/dashboard/service/PipelineServiceImpl.java +++ b/src/main/java/com/capitalone/dashboard/service/PipelineServiceImpl.java @@ -1,5 +1,6 @@ package com.capitalone.dashboard.service; +import com.capitalone.dashboard.misc.HygieiaException; import com.capitalone.dashboard.settings.ApiSettings; import com.capitalone.dashboard.model.CollectorItem; import com.capitalone.dashboard.model.Dashboard; @@ -48,7 +49,7 @@ public PipelineServiceImpl(PipelineRepository pipelineRepository, DashboardRepos } @Override - public Iterable search(PipelineSearchRequest searchRequest) { + public Iterable search(PipelineSearchRequest searchRequest) throws HygieiaException { List pipelineResponses = new ArrayList<>(); for(ObjectId collectorItemId : searchRequest.getCollectorItemId()){ Pipeline pipeline = getOrCreatePipeline(collectorItemId); @@ -67,7 +68,7 @@ protected Pipeline getOrCreatePipeline(ObjectId collectorItemId) { return pipeline; } - private PipelineResponse buildPipelineResponse(Pipeline pipeline, Long beginDate, Long endDate){ + private PipelineResponse buildPipelineResponse(Pipeline pipeline, Long beginDate, Long endDate) throws HygieiaException { Long lowerBound = beginDate; if(beginDate == null){ Calendar cal = new GregorianCalendar(); @@ -80,7 +81,14 @@ private PipelineResponse buildPipelineResponse(Pipeline pipeline, Long beginDate * get the collector item and dashboard */ CollectorItem dashboardCollectorItem = collectorItemRepository.findOne(pipeline.getCollectorItemId()); - Dashboard dashboard = dashboardRepository.findOne(new ObjectId((String)dashboardCollectorItem.getOptions().get("dashboardId"))); + if(dashboardCollectorItem.getOptions().get("dashboardId") == null) { + throw new HygieiaException(" Collector Item: " + dashboardCollectorItem.getId() + " is not associated to a dashboard. ", HygieiaException.BAD_DATA); + } + String dashboardId = (String) dashboardCollectorItem.getOptions().get("dashboardId"); + Dashboard dashboard = dashboardRepository.findOne(new ObjectId(dashboardId)); + if(dashboard == null) { + throw new HygieiaException(" Dashboard " + dashboardId + " is not found for collectorItem: " + dashboardCollectorItem.getId() + " ", HygieiaException.BAD_DATA); + } PipelineResponse pipelineResponse = new PipelineResponse(); pipelineResponse.setCollectorItemId(dashboardCollectorItem.getId()); pipelineResponse.setProdStage(PipelineUtils.getProdStage(dashboard)); From d7e8954be977a465660f07dc7978bf8eeb6d58f8 Mon Sep 17 00:00:00 2001 From: Zhanpeng Chen Date: Wed, 18 Mar 2020 20:19:30 -0400 Subject: [PATCH 2/2] Add exception handling --- .../dashboard/service/DynamicPipelineServiceImpl.java | 11 ++++++----- .../dashboard/service/PipelineServiceImpl.java | 10 +++++----- .../service/DynamicPipelineServiceImplTest.java | 3 ++- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/capitalone/dashboard/service/DynamicPipelineServiceImpl.java b/src/main/java/com/capitalone/dashboard/service/DynamicPipelineServiceImpl.java index e6e28854..2419a970 100644 --- a/src/main/java/com/capitalone/dashboard/service/DynamicPipelineServiceImpl.java +++ b/src/main/java/com/capitalone/dashboard/service/DynamicPipelineServiceImpl.java @@ -14,6 +14,7 @@ import java.util.Set; import java.util.stream.Collectors; +import com.capitalone.dashboard.misc.HygieiaException; import org.apache.commons.lang.ObjectUtils; import org.apache.log4j.Logger; import org.bson.types.ObjectId; @@ -106,7 +107,7 @@ public DynamicPipelineServiceImpl(PipelineRepository pipelineRepository, Dashboa } @Override - public Iterable search(PipelineSearchRequest searchRequest) { + public Iterable search(PipelineSearchRequest searchRequest) throws HygieiaException { //sets the lower and upper bound for the prod bucket's commits. uses constant for lower bound limit and today as default for upper bound Long lowerBound = searchRequest.getBeginDate(); //if(lowerBound == null){ @@ -140,7 +141,7 @@ protected Pipeline getOrCreatePipeline(ObjectId collectorItemId) { } // Creates the response that is returned to the client - private PipelineResponse buildPipelineResponse(Pipeline pipeline, Long lowerBound, Long upperBound){ + private PipelineResponse buildPipelineResponse(Pipeline pipeline, Long lowerBound, Long upperBound) throws HygieiaException { /** * get the collector item and dashboard */ @@ -870,7 +871,7 @@ private boolean isBetween(Long commitTimestamp, Long lowerBound, Long upperBound * @param pipeline * @return */ - private PipelineResponseCommit applyStageTimestamps(PipelineResponseCommit commit, Dashboard dashboard, Pipeline pipeline,List pipelineStageList){ + private PipelineResponseCommit applyStageTimestamps(PipelineResponseCommit commit, Dashboard dashboard, Pipeline pipeline,List pipelineStageList) throws HygieiaException { PipelineResponseCommit returnCommit = new PipelineResponseCommit(commit); for(PipelineStage systemStage : pipelineStageList) { @@ -894,7 +895,7 @@ private PipelineResponseCommit applyStageTimestamps(PipelineResponseCommit commi * @param stageType * @return */ - private Map findCommitsForStage(Dashboard dashboard, Pipeline pipeline, PipelineStage stage) { + private Map findCommitsForStage(Dashboard dashboard, Pipeline pipeline, PipelineStage stage) throws HygieiaException { Map commitMap = new HashMap<>(); // The environment name including the pseudo environments "Build" and "Commit" @@ -915,7 +916,7 @@ private Map findCommitsForStage(Dashboard dashboard, Pip * @param stage current stage * @return a list of all commits as pipeline response commits that havent moved past the current stage */ - public List findNotPropagatedCommits(Dashboard dashboard, Pipeline pipeline, PipelineStage stage,List pipelineStageList){ + public List findNotPropagatedCommits(Dashboard dashboard, Pipeline pipeline, PipelineStage stage,List pipelineStageList) throws HygieiaException { Map startingStage = findCommitsForStage(dashboard, pipeline, stage); List notPropagatedCommits = new ArrayList<>(); diff --git a/src/main/java/com/capitalone/dashboard/service/PipelineServiceImpl.java b/src/main/java/com/capitalone/dashboard/service/PipelineServiceImpl.java index 6e4011f3..a9b6ec42 100644 --- a/src/main/java/com/capitalone/dashboard/service/PipelineServiceImpl.java +++ b/src/main/java/com/capitalone/dashboard/service/PipelineServiceImpl.java @@ -134,7 +134,7 @@ private PipelineResponse buildPipelineResponse(Pipeline pipeline, Long beginDate * @param dashboard * @return a list of deploy PipelineStages that are not mapped */ - private List findUnmappedStages(Dashboard dashboard,List pipelineStageList){ + private List findUnmappedStages(Dashboard dashboard,List pipelineStageList) throws HygieiaException { List unmappedStages = new ArrayList<>(); Map stageToEnvironmentNameMap = PipelineUtils.getStageToEnvironmentNameMap(dashboard); @@ -158,7 +158,7 @@ private List findUnmappedStages(Dashboard dashboard,List getCommitsAfterStage(PipelineStage stage, Pipeline pipeline, Dashboard dashboard,List pipelineStageList,Map orderMap){ + private Map getCommitsAfterStage(PipelineStage stage, Pipeline pipeline, Dashboard dashboard,List pipelineStageList,Map orderMap) throws HygieiaException { Map unionOfAllSets = new HashMap<>(); // get key(ordinal) for stage name List list = getKeysByValue(orderMap,stage.getName()); @@ -202,7 +202,7 @@ private boolean isBetween(Long commitTimestamp, Long lowerBound, Long upperBound * @param pipeline * @return */ - private PipelineResponseCommit applyStageTimestamps(PipelineResponseCommit commit, Dashboard dashboard, Pipeline pipeline,List pipelineStageList){ + private PipelineResponseCommit applyStageTimestamps(PipelineResponseCommit commit, Dashboard dashboard, Pipeline pipeline,List pipelineStageList) throws HygieiaException { PipelineResponseCommit returnCommit = new PipelineResponseCommit(commit); for(PipelineStage systemStage : pipelineStageList) { @@ -226,7 +226,7 @@ private PipelineResponseCommit applyStageTimestamps(PipelineResponseCommit commi * @param stage * @return */ - private Map findCommitsForStage(Dashboard dashboard, Pipeline pipeline, PipelineStage stage) { + private Map findCommitsForStage(Dashboard dashboard, Pipeline pipeline, PipelineStage stage) throws HygieiaException { Map commitMap = new HashMap<>(); String pseudoEnvironmentName = @@ -246,7 +246,7 @@ private Map findCommitsForStage(Dashboard dashboard, Pip * @param stage current stage * @return a list of all commits as pipeline response commits that havent moved past the current stage */ - public List findNotPropagatedCommits(Dashboard dashboard, Pipeline pipeline, PipelineStage stage,List pipelineStageList,Map orderMap){ + public List findNotPropagatedCommits(Dashboard dashboard, Pipeline pipeline, PipelineStage stage,List pipelineStageList,Map orderMap) throws HygieiaException { Map startingStage = findCommitsForStage(dashboard, pipeline, stage); Map commitsInLaterStages = getCommitsAfterStage(stage, pipeline, dashboard,pipelineStageList,orderMap); diff --git a/src/test/java/com/capitalone/dashboard/service/DynamicPipelineServiceImplTest.java b/src/test/java/com/capitalone/dashboard/service/DynamicPipelineServiceImplTest.java index 58f6db40..a0be0ffb 100644 --- a/src/test/java/com/capitalone/dashboard/service/DynamicPipelineServiceImplTest.java +++ b/src/test/java/com/capitalone/dashboard/service/DynamicPipelineServiceImplTest.java @@ -14,6 +14,7 @@ import java.util.Map; import java.util.Set; +import com.capitalone.dashboard.misc.HygieiaException; import org.bson.types.ObjectId; import org.junit.Before; import org.junit.Test; @@ -137,7 +138,7 @@ public void setUp() { } @Test - public void testSearch() { + public void testSearch() throws HygieiaException { CollectorItem pipelineCI = setupPipelineCollectorItem(); CollectorItem scmCI = setupScmCollectorItem(); Component component = setupComponent(scmCI);