SafesService { public ResponseEntity<String> createfolder(String token, String path){ path = (path != null) ? path.toLowerCase() : path; if(ControllerUtil.isPathValid(path)){ String jsonStr ="{\"path\":\""+path +"\",\"data\":{\"default\":\"default\"}}"; log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create Folder"). put(LogMessage.MESSAGE, String.format ("Trying to Create folder [%s]", path)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); Response response = reqProcessor.process("/sdb/createfolder",jsonStr,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, "Create Folder"). put(LogMessage.MESSAGE, "Create Folder completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Folder created \"]}"); } return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); }else{ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "Create Folder"). put(LogMessage.MESSAGE, "Create Folder 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\"]}"); } }  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_createfolder_successfully() { String responseJson = "{ \"messages\": [ \"Folder created \" ]}"; String path = "shared/mysafe01"; String jsonStr ="{\"path\":\""+path +"\",\"data\":{\"default\":\"default\"}}"; String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; Response response = getMockResponse(HttpStatus.OK, true, responseJson); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body(responseJson); when(ControllerUtil.isPathValid(any())).thenReturn(true); when(reqProcessor.process("/sdb/createfolder",jsonStr,token)).thenReturn(response); ResponseEntity<String> responseEntity = safesService.createfolder(token, path); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_createfolder_successfully_noContent() { String responseJson = "{\"messages\":[\"Folder created \"]}"; String path = "shared/mysafe01"; String jsonStr ="{\"path\":\""+path +"\",\"data\":{\"default\":\"default\"}}"; String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; Response response = getMockResponse(HttpStatus.NO_CONTENT, true, responseJson); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body(responseJson); when(ControllerUtil.isPathValid(any())).thenReturn(true); when(reqProcessor.process("/sdb/createfolder",jsonStr,token)).thenReturn(response); ResponseEntity<String> responseEntity = safesService.createfolder(token, path); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_createfolder_failure_400() { String responseJson = "{\"errors\":[\"Invalid path\"]}"; String path = "shared/mysafe01"; String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(responseJson); when(ControllerUtil.isPathValid(any())).thenReturn(false); ResponseEntity<String> responseEntity = safesService.createfolder(token, path); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }