Search in sources :

Example 46 with SearchFilter

use of org.apache.ranger.plugin.util.SearchFilter in project ranger by apache.

the class TestRangerValidator method test_getPolicies.

@Test
public void test_getPolicies() throws Exception {
    // returns null when store returns null
    String policyName = "aPolicy";
    String serviceName = "aService";
    SearchFilter filter = new SearchFilter();
    filter.setParam(SearchFilter.POLICY_NAME, policyName);
    filter.setParam(SearchFilter.SERVICE_NAME, serviceName);
    when(_store.getPolicies(filter)).thenReturn(null);
    List<RangerPolicy> result = _validator.getPolicies(serviceName, policyName);
    // validate store is queried with both parameters
    verify(_store).getPolicies(filter);
    Assert.assertNull(result);
    // returns null if store throws an exception
    when(_store.getPolicies(filter)).thenThrow(new Exception());
    result = _validator.getPolicies(serviceName, policyName);
    Assert.assertNull(result);
    // does not shove policy into search filter if policy name passed in is "blank"
    filter = new SearchFilter();
    filter.setParam(SearchFilter.SERVICE_NAME, serviceName);
    List<RangerPolicy> policies = new ArrayList<>();
    RangerPolicy policy = mock(RangerPolicy.class);
    policies.add(policy);
    when(_store.getPolicies(filter)).thenReturn(policies);
    for (String aName : new String[] { null, "", "  " }) {
        result = _validator.getPolicies(serviceName, aName);
        Assert.assertTrue(result.iterator().next() == policy);
    }
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) ArrayList(java.util.ArrayList) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) Test(org.junit.Test)

Example 47 with SearchFilter

use of org.apache.ranger.plugin.util.SearchFilter in project ranger by apache.

the class RangerValidator method getPolicies.

List<RangerPolicy> getPolicies(final String serviceName, final String policyName) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> RangerValidator.getPolicies(" + serviceName + ", " + policyName + ")");
    }
    List<RangerPolicy> policies = null;
    try {
        SearchFilter filter = new SearchFilter();
        if (StringUtils.isNotBlank(policyName)) {
            filter.setParam(SearchFilter.POLICY_NAME, policyName);
        }
        filter.setParam(SearchFilter.SERVICE_NAME, serviceName);
        policies = _store.getPolicies(filter);
    } catch (Exception e) {
        LOG.debug("Encountred exception while retrieving service from service store!", e);
    }
    if (LOG.isDebugEnabled()) {
        int count = policies == null ? 0 : policies.size();
        LOG.debug("<== RangerValidator.getPolicies(" + serviceName + ", " + policyName + "): count[" + count + "], " + policies);
    }
    return policies;
}
Also used : RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) SearchFilter(org.apache.ranger.plugin.util.SearchFilter)

Example 48 with SearchFilter

use of org.apache.ranger.plugin.util.SearchFilter in project ranger by apache.

the class TestServiceREST method test23getServicePoliciesByName.

@Test
public void test23getServicePoliciesByName() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    RangerPolicy rangerPolicy = rangerPolicy();
    List<RangerPolicy> ret = Mockito.mock(List.class);
    SearchFilter filter = new SearchFilter();
    filter.setParam(SearchFilter.POLICY_NAME, "policyName");
    filter.setParam(SearchFilter.SERVICE_NAME, "serviceName");
    Mockito.when(searchUtil.getSearchFilter(request, policyService.sortFields)).thenReturn(filter);
    Mockito.when(svcStore.getServicePolicies(rangerPolicy.getName(), filter)).thenReturn(ret);
    RangerPolicyList dbRangerPolicy = serviceREST.getServicePoliciesByName(rangerPolicy.getName(), request);
    Assert.assertNotNull(dbRangerPolicy);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) RangerPolicyList(org.apache.ranger.view.RangerPolicyList) Test(org.junit.Test)

Example 49 with SearchFilter

use of org.apache.ranger.plugin.util.SearchFilter in project ranger by apache.

the class TestServiceREST method test44getPolicyLabels.

@Test
public void test44getPolicyLabels() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    SearchFilter filter = new SearchFilter();
    Mockito.when(searchUtil.getSearchFilter(request, policyLabelsService.sortFields)).thenReturn(filter);
    List<String> ret = new ArrayList<String>();
    Mockito.when(svcStore.getPolicyLabels(filter)).thenReturn(ret);
    ret = serviceREST.getPolicyLabels(request);
    Assert.assertNotNull(ret);
    Mockito.verify(searchUtil).getSearchFilter(request, policyLabelsService.sortFields);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ArrayList(java.util.ArrayList) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) VXString(org.apache.ranger.view.VXString) Test(org.junit.Test)

Example 50 with SearchFilter

use of org.apache.ranger.plugin.util.SearchFilter in project ranger by apache.

the class TestServiceREST method test7getServiceDefs.

@Test
public void test7getServiceDefs() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    SearchFilter filter = new SearchFilter();
    filter.setParam(SearchFilter.POLICY_NAME, "policyName");
    filter.setParam(SearchFilter.SERVICE_NAME, "serviceName");
    Mockito.when(searchUtil.getSearchFilter(request, serviceDefService.sortFields)).thenReturn(filter);
    List<RangerServiceDef> serviceDefsList = new ArrayList<RangerServiceDef>();
    RangerServiceDef serviceDef = rangerServiceDef();
    serviceDefsList.add(serviceDef);
    PList<RangerServiceDef> serviceDefList = new PList<RangerServiceDef>();
    serviceDefList.setPageSize(0);
    serviceDefList.setResultSize(1);
    serviceDefList.setSortBy("asc");
    serviceDefList.setSortType("1");
    serviceDefList.setStartIndex(0);
    serviceDefList.setTotalCount(10);
    serviceDefList.setList(serviceDefsList);
    Mockito.when(svcStore.getPaginatedServiceDefs(filter)).thenReturn(serviceDefList);
    RangerServiceDefList dbRangerServiceDef = serviceREST.getServiceDefs(request);
    Assert.assertNotNull(dbRangerServiceDef);
    Mockito.verify(searchUtil).getSearchFilter(request, serviceDefService.sortFields);
    Mockito.verify(svcStore).getPaginatedServiceDefs(filter);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PList(org.apache.ranger.plugin.store.PList) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) ArrayList(java.util.ArrayList) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) RangerServiceDefList(org.apache.ranger.view.RangerServiceDefList) Test(org.junit.Test)

Aggregations

SearchFilter (org.apache.ranger.plugin.util.SearchFilter)61 Test (org.junit.Test)32 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)30 ArrayList (java.util.ArrayList)27 RangerService (org.apache.ranger.plugin.model.RangerService)24 Path (javax.ws.rs.Path)13 Produces (javax.ws.rs.Produces)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 GET (javax.ws.rs.GET)12 WebApplicationException (javax.ws.rs.WebApplicationException)11 RangerPolicyList (org.apache.ranger.view.RangerPolicyList)11 VXString (org.apache.ranger.view.VXString)10 RangerPerfTracer (org.apache.ranger.plugin.util.RangerPerfTracer)9 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)7 RangerServiceDefList (org.apache.ranger.view.RangerServiceDefList)6 RangerServiceList (org.apache.ranger.view.RangerServiceList)6 HashMap (java.util.HashMap)5 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 XXTrxLog (org.apache.ranger.entity.XXTrxLog)4 RangerExportPolicyList (org.apache.ranger.view.RangerExportPolicyList)4