SafesService { public ResponseEntity<String> updateSafe(String token, Safe safe) { safe.getSafeBasicDetails().setType(ControllerUtil.getSafeType(safe.getPath())); Map<String, Object> requestParams = ControllerUtil.parseJson(JSONUtil.getJSON(safe)); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Update SDB"). put(LogMessage.MESSAGE, String.format ("Trying to Update SDB ")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); if (!ControllerUtil.areSDBInputsValidForUpdate(requestParams)) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Update SDB"). put(LogMessage.MESSAGE, String.format ("Invalid input values ")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } if (safe.getSafeBasicDetails().getDescription().length() > 1024) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values: Description too long\"]}"); } @SuppressWarnings("unchecked") Map<Object,Object> data = (Map<Object,Object>)requestParams.get("data"); String path = safe.getPath(); String safeName = safe.getSafeBasicDetails().getName(); String safeNameFromPath = ControllerUtil.getSafeName(path); String safeType = ControllerUtil.getSafeType(path); int redundantSafeNamesCount = ControllerUtil.getCountOfSafesForGivenSafeName(safeName, token); if (redundantSafeNamesCount > 1) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Update SDB"). put(LogMessage.MESSAGE, String.format ("Safe can't be updated since duplicate safe names are found")). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Safe can't be updated since duplicate safe names are found\"]}"); } String safePath = ControllerUtil.generateSafePath(safeName, safeType); String _path = "metadata/"+safeType+"/"+safeNameFromPath; String _safePath = "metadata/"+safeType+"/"+safeName; String pathToBeUpdated = _path; if(ControllerUtil.isValidSafePath(path) || ControllerUtil.isValidSafePath(safePath)){ Response response = reqProcessor.process("/read","{\"path\":\""+_path+"\"}",token); Map<String, Object> responseMap = null; if(HttpStatus.OK.equals(response.getHttpstatus())) { responseMap = ControllerUtil.parseJson(response.getResponse()); if(responseMap.isEmpty()) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Error Fetching existing safe info \"]}"); } pathToBeUpdated = _path; } else{ response = reqProcessor.process("/read","{\"path\":\""+_safePath+"\"}",token); if(HttpStatus.OK.equals(response.getHttpstatus())){ responseMap = ControllerUtil.parseJson(response.getResponse()); if(responseMap.isEmpty()) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Error Fetching existing safe info \"]}"); } pathToBeUpdated = _safePath; } else { log.error("Could not fetch the safe information. Possible path issue"); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Error Fetching existing safe info. please check the path specified \"]}"); } } @SuppressWarnings("unchecked") Map<String,Object> metadataMap = (Map<String,Object>)responseMap.get("data"); Object awsroles = metadataMap.get(TVaultConstants.AWS_ROLES); Object groups = metadataMap.get(TVaultConstants.GROUPS); Object users = metadataMap.get(TVaultConstants.USERS); Object approles = metadataMap.get(TVaultConstants.APP_ROLES); data.put(TVaultConstants.AWS_ROLES,awsroles); data.put(TVaultConstants.GROUPS,groups); data.put(TVaultConstants.USERS,users); data.put(TVaultConstants.APP_ROLES,approles); requestParams.put("path",pathToBeUpdated); ((Map<String,Object>)requestParams.get("data")).put("name",(String) metadataMap.get("name")); ((Map<String,Object>)requestParams.get("data")).put("ownerid",(String) metadataMap.get("ownerid")); String metadataJson = ControllerUtil.convetToJson(requestParams) ; response = reqProcessor.process("/sdb/update",metadataJson,token); if(response.getHttpstatus().equals(HttpStatus.NO_CONTENT)){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Update SDB"). put(LogMessage.MESSAGE, "SDB Update Success"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Safe updated \"]}"); }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Update SDB"). put(LogMessage.MESSAGE, "SDB Update completed"). put(LogMessage.RESPONSE, response.getResponse()). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); } }else{ log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Update SDB"). put(LogMessage.MESSAGE, "SDB Update failed"). put(LogMessage.RESPONSE, "Invalid Path"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid 'path' specified\"]}"); } }  ResponseEntity<String> getFolders( String token, String path); ResponseEntity<String> getInfo(String token, String path); ResponseEntity<String> createfolder(String token, String path); ResponseEntity<String> createSafe(String token, Safe safe); ResponseEntity<String> getSafe(String token, String path); ResponseEntity<String> deleteSafe(String token, Safe safe, UserDetails userDetails); ResponseEntity<String> updateSafe(String token, Safe safe); ResponseEntity<String> addUserToSafe(String token, SafeUser safeUser, UserDetails userDetails); ResponseEntity<String> addGroupToSafe(String token, SafeGroup safeGroup, UserDetails userDetails); ResponseEntity<String> removeUserFromSafe(String token, SafeUser safeUser, UserDetails userDetails); ResponseEntity<String> removeGroupFromSafe(String token, SafeGroup safeGroup, UserDetails userDetails); ResponseEntity<String> addAwsRoleToSafe(String token, AWSRole awsRole); ResponseEntity<String> removeAWSRoleFromSafe(String token, AWSRole awsRole, boolean detachOnly, UserDetails userDetails); ResponseEntity<String> deletefolder(String token, String path, UserDetails userDetails); ResponseEntity<String> getFoldersRecursively(String token, String path); ResponseEntity<String> createNestedfolder(String token, String path, UserDetails userDetails); ResponseEntity<String> associateApproletoSDB(String token, SafeAppRoleAccess safeAppRoleAccess); ResponseEntity<String> removeApproleFromSafe(String token, String jsonstr);  }
@Test public void test_updateSafe_successfully() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; SafeBasicDetails safeBasicDetails = new SafeBasicDetails("mysafe01", "youremail@yourcompany.com", null, "My first safe"); Safe safe = new Safe("shared/mysafe01",safeBasicDetails); String jsonStr = "{\"path\":\"shared/mysafe01\",\"data\":{\"name\":\"mysafe01\",\"owner\":\"youremail@yourcompany.com\",\"type\":\"\",\"description\":\"My first safe\"}}"; String metadatajson = "{\"path\":\"metadata/shared/mysafe01\",\"data\":{\"name\":\"mysafe01\",\"owner\":\"youremail@yourcompany.com\",\"type\":\"\",\"description\":\"My first safe\",\"aws-roles\":null,\"groups\":null,\"users\":null}}"; Response responseNoContent = getMockResponse(HttpStatus.NO_CONTENT, true, ""); Response readResponse = getMockResponse(HttpStatus.OK, true, "{\"data\":{\"description\":\"My first safe\",\"name\":\"mysafe01\",\"owner\":\"youremail@yourcompany.com\",\"type\":\"\"}}"); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Safe updated \"]}"); Map<String,Object> reqparams = null; try { reqparams = new ObjectMapper().readValue(jsonStr, new TypeReference<Map<String, Object>>(){}); } catch (IOException e) { e.printStackTrace(); } when(ControllerUtil.parseJson(any())).thenReturn(reqparams); when(ControllerUtil.areSDBInputsValidForUpdate(reqparams)).thenReturn(true); when(ControllerUtil.getSafeName("shared/mysafe01")).thenReturn("mysafe01"); when(ControllerUtil.getSafeType("shared/mysafe01")).thenReturn("shared"); when(ControllerUtil.getCountOfSafesForGivenSafeName(safe.getSafeBasicDetails().getName(), token)).thenReturn(1); when(ControllerUtil.generateSafePath("mysafe01", "shared")).thenReturn("shared/mysafe01"); when(ControllerUtil.isValidSafePath(any())).thenReturn(true); when(reqProcessor.process("/read","{\"path\":\"metadata/shared/mysafe01\"}",token)).thenReturn(readResponse); when(ControllerUtil.convetToJson(any())).thenReturn(metadatajson); when(reqProcessor.process("/sdb/update",metadatajson,token)).thenReturn(responseNoContent); ResponseEntity<String> responseEntity = safesService.updateSafe(token, safe); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_updateSafe_failure_description_too_long() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; SafeBasicDetails safeBasicDetails = new SafeBasicDetails("mysafe01", "youremail@yourcompany.com", null, "My first safe My first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe" + "My first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safeMy first safe"); Safe safe = new Safe("shared/mysafe01",safeBasicDetails); String jsonStr = "{\"path\":\"shared/mysafe01\",\"data\":{\"name\":\"mysafe01\",\"owner\":\"youremail@yourcompany.com\",\"type\":\"\",\"description\":\"My first safe\"}}"; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values: Description too long\"]}"); Map<String,Object> reqparams = null; try { reqparams = new ObjectMapper().readValue(jsonStr, new TypeReference<Map<String, Object>>(){}); } catch (IOException e) { e.printStackTrace(); } when(ControllerUtil.parseJson(any())).thenReturn(reqparams); when(ControllerUtil.areSDBInputsValidForUpdate(reqparams)).thenReturn(true); ResponseEntity<String> responseEntity = safesService.updateSafe(token, safe); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_updateSafe_failure_400() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; SafeBasicDetails safeBasicDetails = new SafeBasicDetails("mysafe01", "youremail@yourcompany.com", null, "My first safe"); Safe safe = new Safe("shared/mysafe01",safeBasicDetails); String jsonStr = "{\"path\":\"shared/mysafe01\",\"data\":{\"name\":\"mysafe01\",\"owner\":\"youremail@yourcompany.com\",\"type\":\"\",\"description\":\"My first safe\"}}"; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid 'path' specified\"]}"); Map<String,Object> reqparams = null; try { reqparams = new ObjectMapper().readValue(jsonStr, new TypeReference<Map<String, Object>>(){}); } catch (IOException e) { e.printStackTrace(); } when(ControllerUtil.parseJson(any())).thenReturn(reqparams); when(ControllerUtil.areSDBInputsValidForUpdate(reqparams)).thenReturn(true); when(ControllerUtil.getSafeName("shared/mysafe01")).thenReturn("mysafe01"); when(ControllerUtil.getSafeType("shared/mysafe01")).thenReturn("shared"); when(ControllerUtil.getCountOfSafesForGivenSafeName(safe.getSafeBasicDetails().getName(), token)).thenReturn(1); when(ControllerUtil.generateSafePath("mysafe01", "shared")).thenReturn("shared/mysafe01"); when(ControllerUtil.isValidSafePath(any())).thenReturn(false); ResponseEntity<String> responseEntity = safesService.updateSafe(token, safe); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_updateSafe_failure_404() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; SafeBasicDetails safeBasicDetails = new SafeBasicDetails("mysafe01", "youremail@yourcompany.com", null, "My first safe"); Safe safe = new Safe("shared/mysafe01",safeBasicDetails); String jsonStr = "{\"path\":\"shared/mysafe01\",\"data\":{\"name\":\"mysafe01\",\"owner\":\"youremail@yourcompany.com\",\"type\":\"\",\"description\":\"My first safe\"}}"; Response readResponse = getMockResponse(HttpStatus.NOT_FOUND, true, ""); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Error Fetching existing safe info. please check the path specified \"]}"); Map<String,Object> reqparams = null; try { reqparams = new ObjectMapper().readValue(jsonStr, new TypeReference<Map<String, Object>>(){}); } catch (IOException e) { e.printStackTrace(); } when(ControllerUtil.parseJson(any())).thenReturn(reqparams); when(ControllerUtil.areSDBInputsValidForUpdate(reqparams)).thenReturn(true); when(ControllerUtil.getSafeName("shared/mysafe01")).thenReturn("mysafe01"); when(ControllerUtil.getSafeType("shared/mysafe01")).thenReturn("shared"); when(ControllerUtil.getCountOfSafesForGivenSafeName(safe.getSafeBasicDetails().getName(), token)).thenReturn(1); when(ControllerUtil.generateSafePath("mysafe01", "shared")).thenReturn("shared/mysafe01"); when(ControllerUtil.isValidSafePath(any())).thenReturn(true); when(reqProcessor.process("/read","{\"path\":\"metadata/shared/mysafe01\"}",token)).thenReturn(readResponse); ResponseEntity<String> responseEntity = safesService.updateSafe(token, safe); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_updateSafe_failure_invalidInput() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; SafeBasicDetails safeBasicDetails = new SafeBasicDetails("mysafe01", "youremail@yourcompany.com", null, "My first safe"); Safe safe = new Safe("shared/mysafe01",safeBasicDetails); String jsonStr = "{\"path\":\"shared/mysafe01\",\"data\":{\"name\":\"mysafe01\",\"owner\":\"youremail@yourcompany.com\",\"type\":\"\",\"description\":\"My first safe\"}}"; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); Map<String,Object> reqparams = null; try { reqparams = new ObjectMapper().readValue(jsonStr, new TypeReference<Map<String, Object>>(){}); } catch (IOException e) { e.printStackTrace(); } when(ControllerUtil.parseJson(any())).thenReturn(reqparams); when(ControllerUtil.areSDBInputsValid(reqparams)).thenReturn(false); ResponseEntity<String> responseEntity = safesService.updateSafe(token, safe); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }