Search in sources :

Example 1 with RangerSecurityZoneList

use of org.apache.ranger.view.RangerSecurityZoneList in project ranger by apache.

the class TestSecurityZoneREST method testGetAllSecurityZone.

@Test(expected = WebApplicationException.class)
public void testGetAllSecurityZone() throws Exception {
    RangerSecurityZone securityZone = createRangerSecurityZone();
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    SearchFilter filter = new SearchFilter();
    when(searchUtil.getSearchFilter(request, securityZoneService.sortFields)).thenReturn(filter);
    Long securityZoneId = 2L;
    securityZone.setId(securityZoneId);
    List<RangerSecurityZone> zonesList = new ArrayList<>();
    zonesList.add(securityZone);
    RangerSecurityZoneList rangerZoneList = new RangerSecurityZoneList();
    rangerZoneList.setSecurityZoneList(zonesList);
    when(securityZoneStore.getSecurityZones(filter)).thenReturn(zonesList);
    when(rangerBizUtil.hasModuleAccess(Mockito.anyString())).thenReturn(true);
    RangerSecurityZoneList returnedZonesList = securityZoneREST.getAllZones(request);
    assertEquals(returnedZonesList.getResultSize(), rangerZoneList.getList().size());
    verify(securityZoneStore, times(1)).getSecurityZones(filter);
    // No access
    when(rangerBizUtil.hasModuleAccess(Mockito.anyString())).thenReturn(false);
    when(restErrorUtil.createRESTException(Mockito.anyString(), Mockito.any())).thenReturn(new WebApplicationException());
    securityZoneREST.getAllZones(request);
    verify(securityZoneStore, times(0)).getSecurityZones(filter);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RangerSecurityZone(org.apache.ranger.plugin.model.RangerSecurityZone) WebApplicationException(javax.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) RangerSecurityZoneList(org.apache.ranger.view.RangerSecurityZoneList) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) Test(org.junit.Test)

Example 2 with RangerSecurityZoneList

use of org.apache.ranger.view.RangerSecurityZoneList in project ranger by apache.

the class SecurityZoneREST method getAllZones.

@GET
@Path("/zones")
public RangerSecurityZoneList getAllZones(@Context HttpServletRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> getAllZones()");
    }
    if (!bizUtil.hasModuleAccess(RangerConstants.MODULE_SECURITY_ZONE)) {
        throw restErrorUtil.createRESTException(STR_USER_NOT_AUTHORIZED_TO_ACCESS_ZONE, MessageEnums.OPER_NO_PERMISSION);
    }
    RangerSecurityZoneList ret = new RangerSecurityZoneList();
    SearchFilter filter = searchUtil.getSearchFilter(request, securityZoneService.sortFields);
    try {
        List<RangerSecurityZone> securityZones = securityZoneStore.getSecurityZones(filter);
        ret.setSecurityZoneList(securityZones);
        if (securityZones != null) {
            ret.setTotalCount(securityZones.size());
            ret.setSortBy(filter.getSortBy());
            ret.setSortType(filter.getSortType());
            ret.setResultSize(securityZones.size());
        }
    } catch (WebApplicationException excp) {
        throw excp;
    } catch (Throwable excp) {
        LOG.error("getSecurityZones() failed", excp);
        throw restErrorUtil.createRESTException(excp.getMessage());
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== getAllZones():" + ret);
    }
    return ret;
}
Also used : RangerSecurityZone(org.apache.ranger.plugin.model.RangerSecurityZone) WebApplicationException(javax.ws.rs.WebApplicationException) RangerSecurityZoneList(org.apache.ranger.view.RangerSecurityZoneList) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

WebApplicationException (javax.ws.rs.WebApplicationException)2 RangerSecurityZone (org.apache.ranger.plugin.model.RangerSecurityZone)2 SearchFilter (org.apache.ranger.plugin.util.SearchFilter)2 RangerSecurityZoneList (org.apache.ranger.view.RangerSecurityZoneList)2 ArrayList (java.util.ArrayList)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Test (org.junit.Test)1