SecurityZoneDBStore implements SecurityZoneStore { @Override public RangerSecurityZone updateSecurityZoneById(RangerSecurityZone securityZone) throws Exception { XXSecurityZone xxSecurityZone = daoMgr.getXXSecurityZoneDao().findByZoneId(securityZone.getId()); if (xxSecurityZone == null) { throw restErrorUtil.createRESTException("security-zone with id: " + securityZone.getId() + " does not exist"); } Gson gsonBuilder = new GsonBuilder().setDateFormat("yyyyMMdd-HH:mm:ss.SSS-Z").create(); RangerSecurityZone oldSecurityZone = gsonBuilder.fromJson(xxSecurityZone.getJsonData(), RangerSecurityZone.class); daoMgr.getXXGlobalState().onGlobalStateChange(RANGER_GLOBAL_STATE_NAME); RangerSecurityZone updatedSecurityZone = securityZoneService.update(securityZone); if (updatedSecurityZone == null) { throw new Exception("Cannot update security zone:[" + securityZone + "]"); } securityZoneRefUpdater.createNewZoneMappingForRefTable(updatedSecurityZone); List<XXTrxLog> trxLogList = securityZoneService.getTransactionLog(updatedSecurityZone, oldSecurityZone, "update"); bizUtil.createTrxLog(trxLogList); return securityZone; }  void init(); @PostConstruct void initStore(); @Override RangerSecurityZone createSecurityZone(RangerSecurityZone securityZone); @Override RangerSecurityZone updateSecurityZoneById(RangerSecurityZone securityZone); @Override void deleteSecurityZoneByName(String zoneName); @Override void deleteSecurityZoneById(Long zoneId); @Override RangerSecurityZone getSecurityZone(Long id); @Override RangerSecurityZone getSecurityZoneByName(String name); @Override List<RangerSecurityZone> getSecurityZones(SearchFilter filter); @Override Map<String, RangerSecurityZone.RangerSecurityZoneService> getSecurityZonesForService(String serviceName);  }
@Test public void test2updateSecurityZoneById() throws Exception { XXSecurityZone xxSecurityZone = new XXSecurityZone(); xxSecurityZone.setId(2L); RangerSecurityZone securityZone = new RangerSecurityZone(); securityZone.setId(2L); RangerSecurityZone updateSecurityZone = new RangerSecurityZone(); updateSecurityZone.setId(2L); XXSecurityZoneDao xXSecurityZoneDao = Mockito.mock(XXSecurityZoneDao.class); XXGlobalStateDao xXGlobalStateDao = Mockito.mock(XXGlobalStateDao.class); Mockito.when(daoManager.getXXSecurityZoneDao()).thenReturn(xXSecurityZoneDao); Mockito.when(xXSecurityZoneDao.findByZoneId(securityZone.getId())).thenReturn(xxSecurityZone); Mockito.when(daoManager.getXXGlobalState()).thenReturn(xXGlobalStateDao); Mockito.doNothing().when(xXGlobalStateDao).onGlobalStateChange(RANGER_GLOBAL_STATE_NAME); Mockito.when(securityZoneService.update(securityZone)).thenReturn(updateSecurityZone); Mockito.doNothing().when(securityZoneRefUpdater).createNewZoneMappingForRefTable(updateSecurityZone); List<XXTrxLog> trxLogList = new ArrayList<XXTrxLog>(); Mockito.doNothing().when(bizUtil).createTrxLog(trxLogList); RangerSecurityZone expectedSecurityZone = securityZoneDBStore.updateSecurityZoneById(securityZone); Assert.assertNotNull(xxSecurityZone); Assert.assertEquals(updateSecurityZone.getId(), expectedSecurityZone.getId()); Mockito.verify(daoManager).getXXSecurityZoneDao(); Mockito.verify(daoManager).getXXGlobalState(); Mockito.verify(securityZoneService).update(securityZone); }
@Test public void test9updateSecurityZoneByUnknownId() throws Exception { RangerSecurityZone securityZoneToUpdate = new RangerSecurityZone(); securityZoneToUpdate.setId(2L); XXSecurityZoneDao xXSecurityZoneDao = Mockito.mock(XXSecurityZoneDao.class); Mockito.when(daoManager.getXXSecurityZoneDao()).thenReturn(xXSecurityZoneDao); Mockito.when(xXSecurityZoneDao.findByZoneId(securityZoneToUpdate.getId())).thenReturn(null); Mockito.when(restErrorUtil.createRESTException(Mockito.anyString())).thenThrow(new WebApplicationException()); thrown.expect(WebApplicationException.class); securityZoneDBStore.updateSecurityZoneById(securityZoneToUpdate); Mockito.verify(daoManager, times(1)).getXXSecurityZoneDao(); Mockito.verify(xXSecurityZoneDao, times(1)).findByZoneId(securityZoneToUpdate.getId()); }