AppRoleService { public String readRoleId(String token, String rolename){ String roleId = null; if (rolename.equals(TVaultConstants.SELF_SERVICE_APPROLE_NAME)) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "readRoleId"). put(LogMessage.MESSAGE, "Access denied: no permission to read roleID of this AppRole"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return roleId; } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "readRoleId"). put(LogMessage.MESSAGE, String.format("Trying to read role_id for [%s]", rolename)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); Response readResponse = reqProcessor.process("/auth/approle/role/readRoleID","{\"role_name\":\""+rolename+"\"}",token); Map<String, Object> responseMap = null; if(HttpStatus.OK.equals(readResponse.getHttpstatus())) { responseMap = ControllerUtil.parseJson(readResponse.getResponse()); if(responseMap.isEmpty()) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "readRoleId"). put(LogMessage.MESSAGE, "Reading role_id for AppRole failed"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return roleId; } Map<String,Object> roleIdDataMap = (Map<String,Object>) responseMap.get("data"); if (roleIdDataMap != null) { roleId=(String)roleIdDataMap.get("role_id"); } return roleId; } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "readRoleId"). put(LogMessage.MESSAGE, "Reading role_id for AppRole failed"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return roleId; }  ResponseEntity<String> createAppRole(String token, AppRole appRole, UserDetails userDetails); ResponseEntity<String> readAppRole(String token, String rolename); ResponseEntity<String> readAppRoles(String token); ResponseEntity<String> listAppRoles(String token,  UserDetails userDetails); ResponseEntity<String> readAppRoleRoleId(String token, String rolename); String readRoleId(String token, String rolename); List<String> readAccessorIds(String token, String rolename); AppRoleMetadata readAppRoleMetadata(String token, String rolename); AppRole readAppRoleBasicDetails(String token, String rolename); ResponseEntity<String> readAppRoleRoleId(String token, String rolename, UserDetails userDetails); ResponseEntity<String> readAppRoleSecretId(String token, String rolename, UserDetails userDetails); ResponseEntity<String> readSecretIdAccessors(String token, String rolename, UserDetails userDetails); ResponseEntity<String> readAppRoleDetails(String token, String rolename, UserDetails userDetails); ResponseEntity<String> createsecretId(String token, AppRoleSecretData appRoleSecretData); ResponseEntity<String> readAppRoleSecretId(String token, String rolename); ResponseEntity<String> deleteSecretId(String token, AppRoleNameSecretId appRoleNameSecretId); ResponseEntity<String> deleteAppRole(String token, AppRole appRole, UserDetails userDetails); ResponseEntity<String> deleteSecretIds(String token, AppRoleAccessorIds appRoleAccessorIds, UserDetails userDetails); ResponseEntity<String> login(AppRoleIdSecretId appRoleIdSecretId); ResponseEntity<String> associateApprole(String token, SafeAppRoleAccess safeAppRoleAccess); ResponseEntity<String> updateAppRole(String token, AppRole appRole, UserDetails userDetails); Response configureApprole(String rolename,String policies,String token );  }
@Test public void test_readRoleId_successfully() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; String role_name = "testapprole01"; String roleId = "generated-role-id"; String responseJson = "{\"data\":{ \"role_id\": \"generated-role-id\"}}"; Response response = getMockResponse(HttpStatus.OK, true, responseJson); Map<String, Object> responseMap = new HashMap<>(); Map<String,Object> roleIdDataMap = new HashMap<>(); roleIdDataMap.put("role_id", roleId); responseMap.put("data", roleIdDataMap); when(ControllerUtil.parseJson("{\"data\":{ \"role_id\": \"generated-role-id\"}}")).thenReturn(responseMap); when(reqProcessor.process("/auth/approle/role/readRoleID", "{\"role_name\":\""+role_name+"\"}",token)).thenReturn(response); String actualRoleId = appRoleService.readRoleId(token, role_name); assertEquals(roleId, actualRoleId); }
@Test public void test_readRoleId_failure() { String token = "5PDrOhsy4ig8L3EpsJZSLAMg"; String role_name = "testapprole01"; String roleId = "generated-role-id"; String responseJson = "{\"data\":{ \"role_id\": \"generated-role-id\"}}"; Response response = getMockResponse(HttpStatus.NOT_FOUND, true, responseJson); Map<String, Object> responseMap = new HashMap<>(); Map<String,Object> roleIdDataMap = new HashMap<>(); roleIdDataMap.put("role_id", roleId); responseMap.put("data", roleIdDataMap); when(ControllerUtil.parseJson("{\"data\":{ \"role_id\": \"generated-role-id\"}}")).thenReturn(responseMap); when(reqProcessor.process("/auth/approle/role/readRoleID", "{\"role_name\":\""+role_name+"\"}",token)).thenReturn(response); String actualRoleId = appRoleService.readRoleId(token, role_name); assertEquals(null, actualRoleId); }