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

Commit 63412d5

Browse files
authored
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
1 parent 380db7a commit 63412d5

File tree

9 files changed

+113
-28
lines changed

9 files changed

+113
-28
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<artifactId>api</artifactId>
66
<packaging>jar</packaging>
77
<name>${project.groupId}:${project.artifactId}</name>
8-
<version>3.2.6-SNAPSHOT</version>
8+
<version>3.2.7-SNAPSHOT</version>
99
<description>Hygieia Rest API Layer</description>
1010
<url>https://github.com/Hygieia/api</url>
1111

src/main/java/com/capitalone/dashboard/request/CmdbRequest.java

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class CmdbRequest {
1616
private String assignmentGroup;
1717
@NotNull
1818
private String ownerDept;
19+
20+
private String businessService;
21+
1922
/**
2023
* commonName Human readable value of the configurationItem
2124
*/
@@ -92,4 +95,11 @@ public String getToolName() {
9295
public void setToolName(String toolName) {
9396
this.toolName = toolName;
9497
}
98+
99+
public String getBusinessService() {
100+
return businessService;
101+
}
102+
103+
public void setBusinessService(String businessService) { this.businessService = businessService; }
104+
95105
}

src/main/java/com/capitalone/dashboard/rest/CmdbController.java

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.capitalone.dashboard.service.CmdbRemoteService;
77
import com.capitalone.dashboard.service.CmdbService;
88
import com.capitalone.dashboard.util.PaginationHeaderUtility;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
911
import org.springframework.beans.factory.annotation.Autowired;
1012
import org.springframework.data.domain.Page;
1113
import org.springframework.data.domain.Pageable;
@@ -31,6 +33,8 @@ public class CmdbController {
3133
private final CmdbRemoteService cmdbRemoteService;
3234
private PaginationHeaderUtility paginationHeaderUtility;
3335

36+
private static final Logger LOGGER = LoggerFactory.getLogger(CmdbController.class);
37+
3438
@Autowired
3539
public CmdbController(CmdbService cmdbService, PaginationHeaderUtility paginationHeaderUtility, CmdbRemoteService cmdbRemoteService ) {
3640

@@ -62,6 +66,7 @@ public ResponseEntity<Object> remoteCreateDashboard( @Valid @RequestBody CmdbReq
6266
.status( HttpStatus.CREATED )
6367
.body( cmdbRemoteService.remoteCreate( request ) );
6468
} catch (HygieiaException he) {
69+
LOGGER.error("Failed to create cmdb entry", he);
6570
return ResponseEntity
6671
.status( HttpStatus.BAD_REQUEST )
6772
.body( he.getMessage() );

src/main/java/com/capitalone/dashboard/rest/DashboardRemoteController.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import com.capitalone.dashboard.model.Dashboard;
66
import com.capitalone.dashboard.request.DashboardRemoteRequest;
77
import com.capitalone.dashboard.service.DashboardRemoteService;
8+
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
811
import org.springframework.beans.factory.annotation.Autowired;
912
import org.springframework.http.HttpStatus;
1013
import org.springframework.http.ResponseEntity;
@@ -20,9 +23,9 @@
2023
@RestController
2124
public class DashboardRemoteController {
2225

23-
// private static final Logger LOGGER = LoggerFactory.getLogger(DashboardRemoteController.class);
24-
private final DashboardRemoteService dashboardRemoteService;
26+
private static final Logger LOGGER = LoggerFactory.getLogger(DashboardRemoteController.class);
2527

28+
private final DashboardRemoteService dashboardRemoteService;
2629

2730
@Autowired
2831
public DashboardRemoteController(DashboardRemoteService dashboardRemoteService) {
@@ -37,7 +40,8 @@ public ResponseEntity<String> remoteCreateDashboard(@Valid @RequestBody Dashboar
3740
return ResponseEntity
3841
.status(HttpStatus.CREATED)
3942
.body("Successfully created dashboard: id =" + dashboard.getId());
40-
} catch (HygieiaException he) {
43+
} catch (Exception he) {
44+
LOGGER.error("RemoteCreate receives exception", he);
4145
return ResponseEntity
4246
.status(HttpStatus.BAD_REQUEST)
4347
.body("Failed to create dashboard. Error: " + he.getMessage());
@@ -52,7 +56,8 @@ public ResponseEntity<String> remoteUpdateDashboard(@Valid @RequestBody Dashboar
5256
return ResponseEntity
5357
.status(HttpStatus.CREATED)
5458
.body("Successfully updated dashboard: id =" + dashboard.getId());
55-
} catch (HygieiaException he) {
59+
} catch (Exception he) {
60+
LOGGER.error("RemoteUpdate receives exception", he);
5661
return ResponseEntity
5762
.status(HttpStatus.BAD_REQUEST)
5863
.body("Failed to update dashboard. Error: " + he.getMessage());

src/main/java/com/capitalone/dashboard/service/CmdbRemoteServiceImpl.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public CmdbRemoteServiceImpl(
4444
@Override
4545
public Cmdb remoteCreate(CmdbRequest request ) throws HygieiaException {
4646

47+
Cmdb businessService = findBusinessService(request);
48+
4749
validateRequest(request);
4850

4951
updateRelationship(request);
@@ -77,18 +79,33 @@ private void updateRelationship( CmdbRequest request ) {
7779
}
7880
}
7981

82+
private Cmdb findBusinessService(CmdbRequest request) throws HygieiaException {
83+
String businessService = request.getBusinessService();
84+
String busServiceName = request.getConfigurationItemBusServName();
85+
Cmdb cmdb = null;
86+
if (!StringUtils.isEmpty(businessService)) {
87+
cmdb = cmdbRepository.findByConfigurationItemAndItemType(businessService, APP_TYPE);
88+
if (cmdb == null) {
89+
throw new HygieiaException("Configuration Item " + businessService + " does not exist", HygieiaException.BAD_DATA);
90+
}
91+
} else if (!StringUtils.isEmpty(busServiceName)) {
92+
cmdb = cmdbRepository.findByConfigurationItemAndItemType(busServiceName, APP_TYPE);
93+
if (cmdb == null) {
94+
throw new HygieiaException("Configuration Item " + busServiceName + " does not exist", HygieiaException.BAD_DATA);
95+
}
96+
}
97+
return cmdb;
98+
}
99+
80100
/**
81101
* Validates CmdbRequest for errors
82102
* @param request
83103
* @throws HygieiaException
84104
*/
85105
private void validateRequest(CmdbRequest request) throws HygieiaException {
86-
String busServiceName = request.getConfigurationItemBusServName();
87-
if(!StringUtils.isEmpty( busServiceName ) && cmdbRepository.findByConfigurationItemAndItemType( busServiceName, APP_TYPE ) == null){
88-
throw new HygieiaException("Configuration Item " + busServiceName + " does not exist", HygieiaException.BAD_DATA);
89-
}
90106

91107
Cmdb cmdb = cmdbRepository.findByConfigurationItemIgnoreCaseOrCommonNameIgnoreCase(request.getConfigurationItem(), request.getCommonName());
108+
92109
if(cmdb != null){
93110
throw new HygieiaException("Configuration Item " + cmdb.getConfigurationItem() + " already exists", HygieiaException.DUPLICATE_DATA);
94111
}

src/main/java/com/capitalone/dashboard/service/DashboardRemoteServiceImpl.java

+30-5
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,19 @@ public Dashboard remoteCreate(DashboardRemoteRequest request, boolean isUpdate)
115115

116116
List<Dashboard> dashboards = findExistingDashboardsFromRequest( request );
117117
if (!CollectionUtils.isEmpty(dashboards)) {
118-
dashboard = dashboards.get(0);
118+
if (dashboards.size()==1) {
119+
dashboard = dashboards.get(0);
120+
} else {
121+
dashboard = chooseDashboard(dashboards, request);
122+
}
119123
Set<Owner> uniqueOwners = new HashSet<Owner>(validOwners);
120124
uniqueOwners.addAll(dashboard.getOwners());
121125
dashboard.setOwners(new ArrayList<Owner>(uniqueOwners));
122126
dashboard.setConfigurationItemBusAppName(request.getMetaData().getBusinessApplication());
123127
dashboard.setConfigurationItemBusServName(request.getMetaData().getBusinessService());
124-
if (!isUpdate) {
125-
throw new HygieiaException("Dashboard " + dashboard.getTitle() + " (id =" + dashboard.getId() + ") already exists", HygieiaException.DUPLICATE_DATA);
126-
}
128+
// if (!isUpdate) {
129+
// throw new HygieiaException("Dashboard " + dashboard.getTitle() + " (id =" + dashboard.getId() + ") already exists", HygieiaException.DUPLICATE_DATA);
130+
// }
127131
dashboardService.update(dashboard);
128132
//Save the widgets
129133
for (Widget w : dashboard.getWidgets()) {
@@ -194,6 +198,27 @@ public Dashboard remoteCreate(DashboardRemoteRequest request, boolean isUpdate)
194198
return (dashboard != null) ? dashboardService.get(dashboard.getId()) : null;
195199
}
196200

201+
private Dashboard chooseDashboard(List<Dashboard> dashboards, DashboardRemoteRequest request) {
202+
Dashboard dashboard = null;
203+
String businessService = request.getMetaData().getBusinessService();
204+
String businessApplication = request.getMetaData().getBusinessApplication();
205+
String title = request.getMetaData().getTitle();
206+
for (Dashboard one : dashboards) {
207+
if (dashboard==null) {
208+
dashboard = one;
209+
} else if (one.getUpdatedAt()>dashboard.getUpdatedAt()) {
210+
dashboard = one;
211+
} else if (one.getUpdatedAt()==dashboard.getUpdatedAt()) {
212+
if (one.getCreatedAt()>dashboard.getCreatedAt()) {
213+
dashboard = one;
214+
}
215+
}
216+
}
217+
LOG.warn(String.format("MultipleDashboards=%d, businessService=%s, businessApplication=%s, title=%s, selected=%s",
218+
dashboards.size(), businessService, businessApplication, title, dashboard.getId()));
219+
return dashboard;
220+
}
221+
197222
/**
198223
* Generates a Widget Request list of Widgets to be created from the request
199224
* @param entries
@@ -244,7 +269,7 @@ private List< Dashboard > findExistingDashboardsFromRequest( DashboardRemoteRequ
244269
List<Dashboard> existing = new ArrayList<>();
245270
if( !StringUtils.isEmpty( businessService ) && !StringUtils.isEmpty( businessApplication ) ){
246271
existing.addAll(dashboardRepository.findAllByConfigurationItemBusServNameContainingIgnoreCaseAndConfigurationItemBusAppNameContainingIgnoreCase( businessService, businessApplication ));
247-
}if(StringUtils.isNotEmpty(title)) {
272+
} else if (StringUtils.isNotEmpty(title)) {
248273
existing.addAll(dashboardRepository.findByTitle( request.getMetaData().getTitle() ));
249274
}
250275
return existing;

src/main/java/com/capitalone/dashboard/service/DashboardServiceImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ private Dashboard create(Dashboard dashboard, boolean isUpdate) throws HygieiaEx
181181
Iterable<Component> components = null;
182182

183183
if(!isUpdate) {
184+
dashboard.setCreatedAt(System.currentTimeMillis());
184185
components = componentRepository.save(dashboard.getApplication().getComponents());
185-
} else {
186-
dashboard.setUpdatedAt(System.currentTimeMillis());
187186
}
187+
dashboard.setUpdatedAt(System.currentTimeMillis());
188188

189189
try {
190190
duplicateDashboardErrorCheck(dashboard);

src/test/java/com/capitalone/dashboard/service/CmdbRemoteServiceTest.java

+29-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void remoteCreate() throws HygieiaException {
4949
ObjectId objectId = ObjectId.get();
5050
expected.setId(objectId);
5151
CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype",
52-
"type", "assignmentgroup","owner", "BAPTEST", "", "cmdbCollector");
52+
"type", "assignmentgroup","owner", "BAPTEST", null, "", "cmdbCollector");
5353
when(cmdbRepository.findByConfigurationItemAndItemType("","" )).thenReturn(null);
5454
when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem());
5555
when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB));
@@ -69,7 +69,7 @@ public void remoteCreateInvalidBusService() throws HygieiaException {
6969
ObjectId objectId = ObjectId.get();
7070
expected.setId(objectId);
7171
CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype",
72-
"type", "assignmentgroup","owner", "BAPTEST", "ASVTEST", "cmdbCollector");
72+
"type", "assignmentgroup","owner", "BAPTEST", null,"ASVTEST", "cmdbCollector");
7373
when(cmdbRepository.findByConfigurationItemAndItemType("","" )).thenReturn(null);
7474
when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem());
7575
when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB));
@@ -84,6 +84,29 @@ public void remoteCreateInvalidBusService() throws HygieiaException {
8484
assertEquals(excep.getMessage(), e.getMessage());
8585
}
8686
}
87+
88+
/**
89+
* Test the use of businessService instead of configurationItemBusServName in the request.
90+
* @throws HygieiaException
91+
*/
92+
@Test
93+
public void remoteCreateUsingBusinessService() throws HygieiaException {
94+
Cmdb businessServiceItem = makeCmdbItem("CI123456", "subtype",
95+
"type", "assignmentgroup","owner", "ASVTEST");
96+
Cmdb expected = makeCmdbItem("BAPTEST", "subtype",
97+
"type", "assignmentgroup","owner", "BAPTEST");
98+
ObjectId objectId = ObjectId.get();
99+
expected.setId(objectId);
100+
CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype",
101+
"type", "assignmentgroup","owner", "BAPTEST", "CI123456", "", "cmdbCollector");
102+
when(cmdbRepository.findByConfigurationItemAndItemType("CI123456","app" )).thenReturn(businessServiceItem);
103+
when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem());
104+
when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB));
105+
when(cmdbRepository.save(Matchers.any(Cmdb.class))).thenReturn(expected);
106+
107+
assertThat(cmdbRemoteService.remoteCreate(request), is(expected));
108+
}
109+
87110
/**
88111
* Tests remoteCreate functionality ConfigurationItemBusServName doesn't have existing relationships
89112
* @throws HygieiaException
@@ -97,7 +120,7 @@ public void remoteCreateRelationshipUpdateExistingCompsNull() throws HygieiaExce
97120
ObjectId objectId = ObjectId.get();
98121
expected.setId(objectId);
99122
CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype",
100-
"type", "assignmentgroup","owner", "BAPTEST", "ASVTEST", "cmdbCollector");
123+
"type", "assignmentgroup","owner", "BAPTEST", null,"ASVTEST", "cmdbCollector");
101124
when(cmdbRepository.findByConfigurationItemAndItemType("","" )).thenReturn(null);
102125
when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem());
103126
when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB));
@@ -122,7 +145,7 @@ public void remoteCreateRelationshipUpdateExistingComps() throws HygieiaExceptio
122145
ObjectId objectId = ObjectId.get();
123146
expected.setId(objectId);
124147
CmdbRequest request = makeCmdbRequest("BAPTEST", "subtype",
125-
"type", "assignmentgroup","owner", "BAPTEST", "ASVTEST", "cmdbCollector");
148+
"type", "assignmentgroup","owner", "BAPTEST", null,"ASVTEST", "cmdbCollector");
126149
when(cmdbRepository.findByConfigurationItemAndItemType("","" )).thenReturn(null);
127150
when(collectorService.createCollectorItem(Matchers.any(CollectorItem.class) )).thenReturn(makeCollectorItem());
128151
when(collectorRepository.findByCollectorTypeAndName(CollectorType.CMDB, request.getToolName())).thenReturn(makeCollector( request.getToolName(), CollectorType.CMDB));
@@ -154,6 +177,7 @@ private CmdbRequest makeCmdbRequest(String configurationItem,
154177
String assignmentGroup,
155178
String ownerDept,
156179
String commonName,
180+
String businessService,
157181
String configurationItemBusServName,
158182
String toolName){
159183

@@ -164,6 +188,7 @@ private CmdbRequest makeCmdbRequest(String configurationItem,
164188
request.setAssignmentGroup(assignmentGroup);
165189
request.setOwnerDept(ownerDept);
166190
request.setCommonName(commonName);
191+
request.setBusinessService(businessService);
167192
request.setConfigurationItemBusServName(configurationItemBusServName);
168193
request.setToolName(toolName);
169194
return request;

src/test/java/com/capitalone/dashboard/service/DashboardRemoteServiceTest.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@
4040
import java.util.Set;
4141

4242
import static org.junit.Assert.assertEquals;
43+
import static org.junit.Assert.assertNotNull;
4344
import static org.junit.Assert.assertNull;
45+
import static org.junit.Assert.assertTrue;
4446
import static org.junit.Assert.fail;
45-
import static org.junit.Assert.assertNotNull;
4647

4748
@RunWith(SpringJUnit4ClassRunner.class)
4849
@ContextConfiguration(classes = {ApiTestConfig.class, FongoConfig.class})
@@ -210,18 +211,15 @@ public void remoteCreateInvalidCompAndApp() throws IOException {
210211
}
211212
}
212213
@Test
213-
public void remoteCreateDuplicateDashboard() throws IOException {
214+
public void remoteCreateDuplicateDashboard() throws HygieiaException, IOException {
214215
DashboardRemoteRequest request = getRemoteRequest("./dashboardRemoteRequests/0-Remote-Update-Repo.json");
215216
Dashboard dashboard = dashboardRepository.findByTitle(request.getMetaData().getTitle()).get(0);
216217
Throwable t = new Throwable();
217218
RuntimeException excep = new RuntimeException("Dashboard "+dashboard.getTitle()+" (id =" + dashboard.getId() + ") already exists", t);
218219

219-
try {
220-
dashboardRemoteService.remoteCreate(request, false);
221-
fail("Should throw RuntimeException");
222-
} catch(Exception e) {
223-
assertEquals(excep.getMessage(), e.getMessage());
224-
}
220+
Dashboard found = dashboardRemoteService.remoteCreate(request, false);
221+
assertEquals(dashboard.getId(), found.getId());
222+
assertTrue(found.getUpdatedAt()>dashboard.getUpdatedAt());
225223
}
226224
@Test
227225
public void remoteCreate() throws HygieiaException, IOException {

0 commit comments

Comments
 (0)