@Test public void testUpdateSecurityZoneWithMisMatchId() throws Exception { RangerSecurityZone rangerSecurityZoneToUpdate = createRangerSecurityZone(); Long securityZoneId = 2L; XXServiceDefDao xServiceDefDao = Mockito.mock(XXServiceDefDao.class); XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class); XXService xService = Mockito.mock(XXService.class); rangerSecurityZoneToUpdate.setId(securityZoneId); when(rangerBizUtil.isAdmin()).thenReturn(true); when(daoManager.getXXService()).thenReturn(xServiceDao); when(xServiceDao.findByName("test_service_1")).thenReturn(xService); when(daoManager.getXXServiceDef()).thenReturn(xServiceDefDao); when(xServiceDefDao.getById(xService.getType())).thenReturn(xServiceDef); when(validatorFactory.getSecurityZoneValidator(svcStore, securityZoneStore)).thenReturn(validator); doNothing().when(validator).validate(rangerSecurityZoneToUpdate, RangerValidator.Action.UPDATE); when(securityZoneStore.updateSecurityZoneById(rangerSecurityZoneToUpdate)) .thenReturn(rangerSecurityZoneToUpdate); when(restErrorUtil.createRESTException(Mockito.anyString())).thenThrow(new WebApplicationException()); thrown.expect(WebApplicationException.class); RangerSecurityZone updatedRangerSecurityZone = securityZoneREST.updateSecurityZone(9L, rangerSecurityZoneToUpdate); assertEquals(rangerSecurityZoneToUpdate.getId(), updatedRangerSecurityZone.getId()); verify(validator, times(1)).validate(rangerSecurityZoneToUpdate, RangerValidator.Action.UPDATE); }
@PUT @Path("/zones/{id}") public RangerSecurityZone updateSecurityZone(@PathParam("id") Long zoneId, RangerSecurityZone securityZone) { if (LOG.isDebugEnabled()) { LOG.debug("==> updateSecurityZone(id=" + zoneId +", " + securityZone + ")"); } if (zoneId != null && zoneId.equals(RangerSecurityZone.RANGER_UNZONED_SECURITY_ZONE_ID)) { throw restErrorUtil.createRESTException("Cannot update unzoned zone"); } ensureUserAllowOperationOnServiceForZone(securityZone); removeEmptyEntries(securityZone); if (securityZone.getId() != null && !zoneId.equals(securityZone.getId())) { throw restErrorUtil.createRESTException("zoneId mismatch!!"); } else { securityZone.setId(zoneId); } RangerSecurityZone ret; try { RangerSecurityZoneValidator validator = validatorFactory.getSecurityZoneValidator(svcStore, securityZoneStore); validator.validate(securityZone, RangerValidator.Action.UPDATE); ret = securityZoneStore.updateSecurityZoneById(securityZone); } catch(WebApplicationException excp) { throw excp; } catch(Throwable excp) { LOG.error("updateSecurityZone(" + securityZone + ") failed", excp); throw restErrorUtil.createRESTException(excp.getMessage()); } if (LOG.isDebugEnabled()) { LOG.debug("<== updateSecurityZone(id=" + zoneId +", " + securityZone + "):" + ret); } return ret; }
SecurityZoneREST { @PUT @Path("/zones/{id}") public RangerSecurityZone updateSecurityZone(@PathParam("id") Long zoneId, RangerSecurityZone securityZone) { if (LOG.isDebugEnabled()) { LOG.debug("==> updateSecurityZone(id=" + zoneId +", " + securityZone + ")"); } if (zoneId != null && zoneId.equals(RangerSecurityZone.RANGER_UNZONED_SECURITY_ZONE_ID)) { throw restErrorUtil.createRESTException("Cannot update unzoned zone"); } ensureUserAllowOperationOnServiceForZone(securityZone); removeEmptyEntries(securityZone); if (securityZone.getId() != null && !zoneId.equals(securityZone.getId())) { throw restErrorUtil.createRESTException("zoneId mismatch!!"); } else { securityZone.setId(zoneId); } RangerSecurityZone ret; try { RangerSecurityZoneValidator validator = validatorFactory.getSecurityZoneValidator(svcStore, securityZoneStore); validator.validate(securityZone, RangerValidator.Action.UPDATE); ret = securityZoneStore.updateSecurityZoneById(securityZone); } catch(WebApplicationException excp) { throw excp; } catch(Throwable excp) { LOG.error("updateSecurityZone(" + securityZone + ") failed", excp); throw restErrorUtil.createRESTException(excp.getMessage()); } if (LOG.isDebugEnabled()) { LOG.debug("<== updateSecurityZone(id=" + zoneId +", " + securityZone + "):" + ret); } return ret; } }
SecurityZoneREST { @PUT @Path("/zones/{id}") public RangerSecurityZone updateSecurityZone(@PathParam("id") Long zoneId, RangerSecurityZone securityZone) { if (LOG.isDebugEnabled()) { LOG.debug("==> updateSecurityZone(id=" + zoneId +", " + securityZone + ")"); } if (zoneId != null && zoneId.equals(RangerSecurityZone.RANGER_UNZONED_SECURITY_ZONE_ID)) { throw restErrorUtil.createRESTException("Cannot update unzoned zone"); } ensureUserAllowOperationOnServiceForZone(securityZone); removeEmptyEntries(securityZone); if (securityZone.getId() != null && !zoneId.equals(securityZone.getId())) { throw restErrorUtil.createRESTException("zoneId mismatch!!"); } else { securityZone.setId(zoneId); } RangerSecurityZone ret; try { RangerSecurityZoneValidator validator = validatorFactory.getSecurityZoneValidator(svcStore, securityZoneStore); validator.validate(securityZone, RangerValidator.Action.UPDATE); ret = securityZoneStore.updateSecurityZoneById(securityZone); } catch(WebApplicationException excp) { throw excp; } catch(Throwable excp) { LOG.error("updateSecurityZone(" + securityZone + ") failed", excp); throw restErrorUtil.createRESTException(excp.getMessage()); } if (LOG.isDebugEnabled()) { LOG.debug("<== updateSecurityZone(id=" + zoneId +", " + securityZone + "):" + ret); } return ret; }  }
SecurityZoneREST { @PUT @Path("/zones/{id}") public RangerSecurityZone updateSecurityZone(@PathParam("id") Long zoneId, RangerSecurityZone securityZone) { if (LOG.isDebugEnabled()) { LOG.debug("==> updateSecurityZone(id=" + zoneId +", " + securityZone + ")"); } if (zoneId != null && zoneId.equals(RangerSecurityZone.RANGER_UNZONED_SECURITY_ZONE_ID)) { throw restErrorUtil.createRESTException("Cannot update unzoned zone"); } ensureUserAllowOperationOnServiceForZone(securityZone); removeEmptyEntries(securityZone); if (securityZone.getId() != null && !zoneId.equals(securityZone.getId())) { throw restErrorUtil.createRESTException("zoneId mismatch!!"); } else { securityZone.setId(zoneId); } RangerSecurityZone ret; try { RangerSecurityZoneValidator validator = validatorFactory.getSecurityZoneValidator(svcStore, securityZoneStore); validator.validate(securityZone, RangerValidator.Action.UPDATE); ret = securityZoneStore.updateSecurityZoneById(securityZone); } catch(WebApplicationException excp) { throw excp; } catch(Throwable excp) { LOG.error("updateSecurityZone(" + securityZone + ") failed", excp); throw restErrorUtil.createRESTException(excp.getMessage()); } if (LOG.isDebugEnabled()) { LOG.debug("<== updateSecurityZone(id=" + zoneId +", " + securityZone + "):" + ret); } return ret; }  @POST @Path("/zones") RangerSecurityZone createSecurityZone(RangerSecurityZone securityZone); @PUT @Path("/zones/{id}") RangerSecurityZone updateSecurityZone(@PathParam("id") Long zoneId,
                                                 RangerSecurityZone securityZone); @DELETE @Path("/zones/name/{name}") void deleteSecurityZone(@PathParam("name") String zoneName); @DELETE @Path("/zones/{id}") void deleteSecurityZone(@PathParam("id") Long zoneId); @GET @Path("/zones/name/{name}") RangerSecurityZone getSecurityZone(@PathParam("name") String zoneName); @GET @Path("/zones/{id}") RangerSecurityZone getSecurityZone(@PathParam("id") Long id); @GET @Path("/zones") RangerSecurityZoneList getAllZones(@Context HttpServletRequest request); }
SecurityZoneREST { @PUT @Path("/zones/{id}") public RangerSecurityZone updateSecurityZone(@PathParam("id") Long zoneId, RangerSecurityZone securityZone) { if (LOG.isDebugEnabled()) { LOG.debug("==> updateSecurityZone(id=" + zoneId +", " + securityZone + ")"); } if (zoneId != null && zoneId.equals(RangerSecurityZone.RANGER_UNZONED_SECURITY_ZONE_ID)) { throw restErrorUtil.createRESTException("Cannot update unzoned zone"); } ensureUserAllowOperationOnServiceForZone(securityZone); removeEmptyEntries(securityZone); if (securityZone.getId() != null && !zoneId.equals(securityZone.getId())) { throw restErrorUtil.createRESTException("zoneId mismatch!!"); } else { securityZone.setId(zoneId); } RangerSecurityZone ret; try { RangerSecurityZoneValidator validator = validatorFactory.getSecurityZoneValidator(svcStore, securityZoneStore); validator.validate(securityZone, RangerValidator.Action.UPDATE); ret = securityZoneStore.updateSecurityZoneById(securityZone); } catch(WebApplicationException excp) { throw excp; } catch(Throwable excp) { LOG.error("updateSecurityZone(" + securityZone + ") failed", excp); throw restErrorUtil.createRESTException(excp.getMessage()); } if (LOG.isDebugEnabled()) { LOG.debug("<== updateSecurityZone(id=" + zoneId +", " + securityZone + "):" + ret); } return ret; }  @POST @Path("/zones") RangerSecurityZone createSecurityZone(RangerSecurityZone securityZone); @PUT @Path("/zones/{id}") RangerSecurityZone updateSecurityZone(@PathParam("id") Long zoneId,
                                                 RangerSecurityZone securityZone); @DELETE @Path("/zones/name/{name}") void deleteSecurityZone(@PathParam("name") String zoneName); @DELETE @Path("/zones/{id}") void deleteSecurityZone(@PathParam("id") Long zoneId); @GET @Path("/zones/name/{name}") RangerSecurityZone getSecurityZone(@PathParam("name") String zoneName); @GET @Path("/zones/{id}") RangerSecurityZone getSecurityZone(@PathParam("id") Long id); @GET @Path("/zones") RangerSecurityZoneList getAllZones(@Context HttpServletRequest request);  }