SecurityZoneDBStore implements SecurityZoneStore { @Override public RangerSecurityZone createSecurityZone(RangerSecurityZone securityZone) throws Exception { if (LOG.isDebugEnabled()) { LOG.debug("==> SecurityZoneDBStore.createSecurityZone()"); } XXSecurityZone xxSecurityZone = daoMgr.getXXSecurityZoneDao().findByZoneName(securityZone.getName()); if (xxSecurityZone != null) { throw restErrorUtil.createRESTException("security-zone with name: " + securityZone.getName() + " already exists", MessageEnums.ERROR_DUPLICATE_OBJECT); } daoMgr.getXXGlobalState().onGlobalStateChange(RANGER_GLOBAL_STATE_NAME); RangerSecurityZone createdSecurityZone = securityZoneService.create(securityZone); if (createdSecurityZone == null) { throw new Exception("Cannot create security zone:[" + securityZone + "]"); } securityZoneRefUpdater.createNewZoneMappingForRefTable(createdSecurityZone); List<XXTrxLog> trxLogList = securityZoneService.getTransactionLog(createdSecurityZone, null, "create"); bizUtil.createTrxLog(trxLogList); return createdSecurityZone; }  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 test1createSecurityZone() throws Exception { XXSecurityZone xxSecurityZone = null; RangerSecurityZone securityZone = new RangerSecurityZone(); RangerSecurityZone createdSecurityZone = new RangerSecurityZone(); createdSecurityZone.setId(2L); XXSecurityZoneDao xXSecurityZoneDao = Mockito.mock(XXSecurityZoneDao.class); XXGlobalStateDao xXGlobalStateDao = Mockito.mock(XXGlobalStateDao.class); Mockito.when(daoManager.getXXSecurityZoneDao()).thenReturn(xXSecurityZoneDao); Mockito.when(xXSecurityZoneDao.findByZoneName(securityZone.getName())).thenReturn(xxSecurityZone); Mockito.when(daoManager.getXXGlobalState()).thenReturn(xXGlobalStateDao); Mockito.doNothing().when(xXGlobalStateDao).onGlobalStateChange(RANGER_GLOBAL_STATE_NAME); Mockito.when(securityZoneService.create(securityZone)).thenReturn(createdSecurityZone); Mockito.doNothing().when(securityZoneRefUpdater).createNewZoneMappingForRefTable(createdSecurityZone); List<XXTrxLog> trxLogList = new ArrayList<XXTrxLog>(); Mockito.doNothing().when(bizUtil).createTrxLog(trxLogList); RangerSecurityZone expectedSecurityZone = securityZoneDBStore.createSecurityZone(securityZone); Assert.assertNull(xxSecurityZone); Assert.assertEquals(createdSecurityZone.getId(), expectedSecurityZone.getId()); Mockito.verify(daoManager).getXXSecurityZoneDao(); Mockito.verify(daoManager).getXXGlobalState(); Mockito.verify(securityZoneService).create(securityZone); }
@Test public void test8createSecurityZoneWithExistingName() throws Exception { XXSecurityZone xxSecurityZone = new XXSecurityZone(); xxSecurityZone.setId(2L); RangerSecurityZone securityZone = new RangerSecurityZone(); RangerSecurityZone createdSecurityZone = new RangerSecurityZone(); createdSecurityZone.setId(2L); XXSecurityZoneDao xXSecurityZoneDao = Mockito.mock(XXSecurityZoneDao.class); Mockito.when(daoManager.getXXSecurityZoneDao()).thenReturn(xXSecurityZoneDao); Mockito.when(xXSecurityZoneDao.findByZoneName(securityZone.getName())).thenReturn(xxSecurityZone); Mockito.when(restErrorUtil.createRESTException(Mockito.anyString(), Mockito.any(MessageEnums.class))) .thenThrow(new WebApplicationException()); thrown.expect(WebApplicationException.class); securityZoneDBStore.createSecurityZone(securityZone); Mockito.verify(daoManager, times(1)).getXXSecurityZoneDao(); Mockito.verify(xXSecurityZoneDao, times(1)).findByZoneName(securityZone.getName()); }