RangerClient { public RangerService getService(long serviceId) throws RangerServiceException { return callAPI(GET_SERVICE_BY_ID.applyUrlFormat(serviceId), null, null, RangerService.class); } RangerClient(String configFile);  RangerClient(String hostname, String username, String password);  RangerClient(RangerRESTClient restClient); RangerServiceDef createServiceDef(RangerServiceDef serviceDef); RangerServiceDef updateServiceDef(long serviceDefId, RangerServiceDef serviceDef); RangerServiceDef updateServiceDef(String serviceDefName, RangerServiceDef serviceDef); void deleteServiceDef(long serviceDefId); void deleteServiceDef(String serviceDefName); RangerServiceDef getServiceDef(long serviceDefId); RangerServiceDef getServiceDef(String serviceDefName); List<RangerServiceDef> findServiceDefs(Map<String, String> filter); RangerService createService(RangerService service); RangerService updateService(long serviceId, RangerService service); RangerService updateService(String serviceName, RangerService service); void deleteService(long serviceId); void deleteService(String serviceName); RangerService getService(long serviceId); RangerService getService(String serviceName); List<RangerService> findServices(Map<String, String> filter); RangerPolicy createPolicy(RangerPolicy policy); RangerPolicy updatePolicy(long policyId, RangerPolicy policy); RangerPolicy updatePolicy(String serviceName, String policyName, RangerPolicy policy); RangerPolicy applyPolicy(RangerPolicy policy); void deletePolicy(long policyId); void deletePolicy(String serviceName, String policyName); RangerPolicy getPolicy(long policyId); RangerPolicy getPolicy(String serviceName, String policyName); List<RangerPolicy> getPoliciesInService(String serviceName); List<RangerPolicy> findPolicies(Map<String, String> filter); RangerSecurityZone createSecurityZone(RangerSecurityZone securityZone); RangerSecurityZone updateSecurityZone(long zoneId, RangerSecurityZone securityZone); RangerSecurityZone updateSecurityZone(String zoneName, RangerSecurityZone securityZone); void deleteSecurityZone(long zoneId); void deleteSecurityZone(String zoneName); RangerSecurityZone getSecurityZone(long zoneId); RangerSecurityZone getSecurityZone(String zoneName); List<RangerSecurityZone> findSecurityZones(Map<String, String> filter); RangerRole createRole(String serviceName, RangerRole role); RangerRole updateRole(long roleId, RangerRole role); void deleteRole(long roleId); void deleteRole(String roleName, String execUser, String serviceName); RangerRole getRole(long roleId); RangerRole getRole(String roleName, String execUser, String serviceName); List<String> getAllRoleNames(String execUser, String serviceName); List<String> getUserRoles(String user); List<RangerRole> findRoles(Map<String, String> filter); RESTResponse grantRole(String serviceName, GrantRevokeRoleRequest request); RESTResponse revokeRole(String serviceName, GrantRevokeRoleRequest request); List<RangerPluginInfo> getPluginsInfo(); void deletePolicyDeltas(int days, boolean reloadServicePoliciesCache); static final API CREATE_SERVICEDEF; static final API UPDATE_SERVICEDEF_BY_ID; static final API UPDATE_SERVICEDEF_BY_NAME; static final API DELETE_SERVICEDEF_BY_ID; static final API DELETE_SERVICEDEF_BY_NAME; static final API GET_SERVICEDEF_BY_ID; static final API GET_SERVICEDEF_BY_NAME; static final API FIND_SERVICEDEFS; static final API CREATE_SERVICE; static final API UPDATE_SERVICE_BY_ID; static final API UPDATE_SERVICE_BY_NAME; static final API DELETE_SERVICE_BY_ID; static final API DELETE_SERVICE_BY_NAME; static final API GET_SERVICE_BY_ID; static final API GET_SERVICE_BY_NAME; static final API FIND_SERVICES; static final API CREATE_POLICY; static final API UPDATE_POLICY_BY_ID; static final API UPDATE_POLICY_BY_NAME; static final API APPLY_POLICY; static final API DELETE_POLICY_BY_ID; static final API DELETE_POLICY_BY_NAME; static final API GET_POLICY_BY_ID; static final API GET_POLICY_BY_NAME; static final API GET_POLICIES_IN_SERVICE; static final API FIND_POLICIES; static final API CREATE_ZONE; static final API UPDATE_ZONE_BY_ID; static final API UPDATE_ZONE_BY_NAME; static final API DELETE_ZONE_BY_ID; static final API DELETE_ZONE_BY_NAME; static final API GET_ZONE_BY_ID; static final API GET_ZONE_BY_NAME; static final API FIND_ZONES; static final API CREATE_ROLE; static final API UPDATE_ROLE_BY_ID; static final API DELETE_ROLE_BY_ID; static final API DELETE_ROLE_BY_NAME; static final API GET_ROLE_BY_ID; static final API GET_ROLE_BY_NAME; static final API GET_ALL_ROLE_NAMES; static final API GET_USER_ROLES; static final API GRANT_ROLE; static final API REVOKE_ROLE; static final API FIND_ROLES; static final API GET_PLUGIN_INFO; static final API DELETE_POLICY_DELTAS; }
@Test public void apiGet_Success() throws Exception { try { RangerRESTClient restClient = mock(RangerRESTClient.class); ClientResponse response = mock(ClientResponse.class); RangerClient client = new RangerClient(restClient); RangerService service = new RangerService("testType", "testService", "MockedService", "testTag", new HashMap<>()); when(restClient.get(anyString(), any())).thenReturn(response); when(response.getStatus()).thenReturn(GET_TEST_API.getExpectedStatus().getStatusCode()); when(response.getEntity(RangerService.class)).thenReturn(service); RangerService ret = client.getService(service.getName()); Assert.assertNotNull(ret); Assert.assertEquals(ret.getName(), service.getName()); } catch(RangerServiceException excp){ Assert.fail("Not expected to fail! Found exception: " + excp); } }
@Test public void apiGet_ServiceUnavailable() throws Exception { try { RangerRESTClient restClient = mock(RangerRESTClient.class); ClientResponse response = mock(ClientResponse.class); RangerClient client = new RangerClient(restClient); when(restClient.get(anyString(), any())).thenReturn(response); when(response.getStatus()).thenReturn(ClientResponse.Status.SERVICE_UNAVAILABLE.getStatusCode()); RangerService ret = client.getService(1L); Assert.assertNull(ret); } catch(RangerServiceException excp){ Assert.fail("Not expected to fail! Found exception: " + excp); } }
@Test public void apiGet_FailWithUnexpectedStatusCode() throws Exception { try { RangerRESTClient restClient = mock(RangerRESTClient.class); ClientResponse response = mock(ClientResponse.class); RangerClient client = new RangerClient(restClient); when(restClient.get(anyString(), any())).thenReturn(response); when(response.getStatus()).thenReturn(ClientResponse.Status.INTERNAL_SERVER_ERROR.getStatusCode()); client.getService(1L); Assert.fail("supposed to fail with RangerServiceException"); } catch(RangerServiceException excp) { Assert.assertTrue(excp.getMessage().contains("statusCode=" + ClientResponse.Status.INTERNAL_SERVER_ERROR.getStatusCode())); Assert.assertTrue(excp.getMessage().contains("status=" + ClientResponse.Status.INTERNAL_SERVER_ERROR.getReasonPhrase())); } }
@Test public void apiGet_FailWithNullResponse() throws Exception { try { RangerRESTClient restClient = mock(RangerRESTClient.class); RangerClient client = new RangerClient(restClient); when(restClient.get(anyString(), any())).thenReturn(null); client.getService(1L); Assert.fail("supposed to fail with RangerServiceException"); } catch(RangerServiceException excp) { Assert.assertTrue(excp.getMessage().contains("statusCode=null")); Assert.assertTrue(excp.getMessage().contains("status=null")); } }