ServiceDBStore extends AbstractServiceStore { @Override public RangerServiceDef updateServiceDef(RangerServiceDef serviceDef) throws Exception { if (LOG.isDebugEnabled()) { LOG.debug("==> ServiceDBStore.updateServiceDef(" + serviceDef + ")"); } Long serviceDefId = serviceDef.getId(); XXServiceDef existing = daoMgr.getXXServiceDef().getById(serviceDefId); if (existing == null) { throw restErrorUtil.createRESTException("no service-def exists with ID=" + serviceDef.getId(), MessageEnums.DATA_NOT_FOUND); } String existingName = existing.getName(); boolean renamed = !StringUtils.equalsIgnoreCase(serviceDef.getName(), existingName); if (renamed) { XXServiceDef renamedSVCDef = daoMgr.getXXServiceDef().findByName(serviceDef.getName()); if (renamedSVCDef != null) { throw restErrorUtil.createRESTException( "another service-def already exists with name '" + serviceDef.getName() + "'. ID=" + renamedSVCDef.getId(), MessageEnums.DATA_NOT_UPDATABLE); } } List<RangerServiceConfigDef> configs = serviceDef.getConfigs() != null ? serviceDef.getConfigs() : new ArrayList<RangerServiceConfigDef>(); List<RangerResourceDef> resources = serviceDef.getResources() != null ? serviceDef.getResources() : new ArrayList<RangerResourceDef>(); List<RangerAccessTypeDef> accessTypes = serviceDef.getAccessTypes() != null ? serviceDef.getAccessTypes() : new ArrayList<RangerAccessTypeDef>(); List<RangerPolicyConditionDef> policyConditions = serviceDef.getPolicyConditions() != null ? serviceDef.getPolicyConditions() : new ArrayList<RangerPolicyConditionDef>(); List<RangerContextEnricherDef> contextEnrichers = serviceDef.getContextEnrichers() != null ? serviceDef.getContextEnrichers() : new ArrayList<RangerContextEnricherDef>(); List<RangerEnumDef> enums = serviceDef.getEnums() != null ? serviceDef.getEnums() : new ArrayList<RangerEnumDef>(); RangerDataMaskDef dataMaskDef = serviceDef.getDataMaskDef(); RangerRowFilterDef rowFilterDef = serviceDef.getRowFilterDef(); RangerServiceDefHelper defHelper = new RangerServiceDefHelper(serviceDef, false); defHelper.patchServiceDefWithDefaultValues(); serviceDef.setCreateTime(existing.getCreateTime()); serviceDef.setGuid(existing.getGuid()); serviceDef.setVersion(existing.getVersion()); serviceDef = serviceDefService.update(serviceDef); XXServiceDef createdSvcDef = daoMgr.getXXServiceDef().getById(serviceDefId); updateChildObjectsOfServiceDef(createdSvcDef, configs, resources, accessTypes, policyConditions, contextEnrichers, enums, dataMaskDef, rowFilterDef); RangerServiceDef updatedSvcDef = getServiceDef(serviceDefId); dataHistService.createObjectDataHistory(updatedSvcDef, RangerDataHistService.ACTION_UPDATE); postUpdate(updatedSvcDef); if (LOG.isDebugEnabled()) { LOG.debug("<== ServiceDBStore.updateServiceDef(" + serviceDef + "): " + serviceDef); } return updatedSvcDef; }  @Override void init(); @PostConstruct void initStore(); @Override RangerServiceDef createServiceDef(RangerServiceDef serviceDef); @Override RangerServiceDef updateServiceDef(RangerServiceDef serviceDef); void deleteServiceDef(Long serviceDefId, Boolean forceDelete); void deleteXXAccessTypeDef(XXAccessTypeDef xAccess); void deleteXXResourceDef(XXResourceDef xRes); @Override RangerServiceDef getServiceDef(Long id); @Override RangerServiceDef getServiceDefByName(String name); @Override RangerServiceDef getServiceDefByDisplayName(String displayName); @Override List<RangerServiceDef> getServiceDefs(SearchFilter filter); @Override PList<RangerServiceDef> getPaginatedServiceDefs(SearchFilter filter); @Override RangerService createService(RangerService service); @Override RangerService updateService(RangerService service, Map<String, Object> options); @Override void deleteService(Long id); @Override List<RangerPolicy> getPoliciesByResourceSignature(String serviceName, String policySignature, Boolean isPolicyEnabled); @Override RangerService getService(Long id); @Override RangerService getServiceByName(String name); @Override RangerService getServiceByDisplayName(String displayName); RangerService getServiceByNameForDP(String name); @Override List<RangerService> getServices(SearchFilter filter); PList<RangerService> getPaginatedServices(SearchFilter filter); @Override RangerPolicy createPolicy(RangerPolicy policy); void createOrMapLabels(XXPolicy xPolicy, Set<String> uniquePolicyLabels); @Override RangerPolicy updatePolicy(RangerPolicy policy); @Override void deletePolicy(RangerPolicy policy); @Override void deletePolicy(RangerPolicy policy, RangerService service); @Override boolean policyExists(Long id); @Override RangerPolicy getPolicy(Long id); @Override List<RangerPolicy> getPolicies(SearchFilter filter); @Override Long getPolicyId(final Long serviceId, final String policyName, final Long zoneId); void getPoliciesInExcel(List<RangerPolicy> policies, HttpServletResponse response); void getPoliciesInCSV(List<RangerPolicy> policies,
			HttpServletResponse response); void getPoliciesInJson(List<RangerPolicy> policies,
			HttpServletResponse response); PList<RangerPolicy> getPaginatedPolicies(SearchFilter filter); @Override List<RangerPolicy> getServicePolicies(Long serviceId, SearchFilter filter); List<RangerPolicy> noZoneFilter(List<RangerPolicy> servicePolicies); PList<RangerPolicy> getPaginatedServicePolicies(Long serviceId, SearchFilter filter); @Override List<RangerPolicy> getServicePolicies(String serviceName, SearchFilter filter); PList<RangerPolicy> getPaginatedServicePolicies(String serviceName, SearchFilter filter); @Override ServicePolicies getServicePoliciesIfUpdated(String serviceName, Long lastKnownVersion, boolean needsBackwardCompatibility); @Override Long getServicePolicyVersion(String serviceName); @Override ServicePolicies getServicePolicyDeltasOrPolicies(String serviceName, Long lastKnownVersion); @Override ServicePolicies getServicePolicyDeltas(String serviceName, Long lastKnownVersion); @Override ServicePolicies getServicePolicies(String serviceName, Long lastKnownVersion); void createZoneDefaultPolicies(Collection<String> serviceNames, RangerSecurityZone zone); void deleteZonePolicies(Collection<String> serviceNames, Long zoneId); static void persistVersionChange(ServiceVersionUpdater serviceVersionUpdater); @Override Boolean getPopulateExistingBaseFields(); @Override void setPopulateExistingBaseFields(Boolean populateExistingBaseFields); RangerPolicy getPolicyFromEventTime(String eventTime, Long policyId); VXString getPolicyVersionList(Long policyId); RangerPolicy getPolicyForVersionNumber(Long policyId, int versionNo); @Override RangerSecurityZone getSecurityZone(Long id); @Override RangerSecurityZone getSecurityZone(String name); @Override long getPoliciesCount(final String serviceName); void putMetaDataInfo(RangerExportPolicyList rangerExportPolicyList); Map<String, String> getMapFromInputStream(InputStream mapStream); Map<String, RangerPolicy> setPolicyMapKeyValue(Map<String, RangerPolicy> policiesMap, RangerPolicy policy); Map<String, RangerPolicy> createPolicyMap(Map<String, String> zoneMappingMap, List<String> sourceZones,
			String destinationZoneName, Map<String, String> servicesMappingMap, List<String> sourceServices,
			List<String> destinationServices, RangerPolicy policy, Map<String, RangerPolicy> policiesMap); void getServiceUpgraded(); void resetPolicyUpdateLog(int retentionInDays, Integer policyChangeType); void resetTagUpdateLog(int retentionInDays, ServiceTags.TagsChangeType tagChangeType); List<String> getPolicyLabels(SearchFilter searchFilter); String getMetricByType(String type); boolean isServiceAdminUser(String serviceName, String userName); static boolean isSupportsPolicyDeltas(); static boolean isSupportsRolesDownloadByService(); @Override Map<String, String> getServiceConfigForPlugin(Long serviceId); static final String SERVICE_ADMIN_USERS; static final String CRYPT_ALGO; static final String ENCRYPT_KEY; static final String SALT; static final Integer ITERATION_COUNT; static boolean SUPPORTS_POLICY_DELTAS; static Integer RETENTION_PERIOD_IN_DAYS; static Integer TAG_RETENTION_PERIOD_IN_DAYS; static final String HIDDEN_PASSWORD_STR; static final String CONFIG_KEY_PASSWORD; static final String ACCESS_TYPE_DECRYPT_EEK; static final String ACCESS_TYPE_GENERATE_EEK; static final String ACCESS_TYPE_GET_METADATA; }
@Test public void test12updateServiceDef() throws Exception { setup(); XXServiceDefDao xServiceDefDao = Mockito.mock(XXServiceDefDao.class); XXServiceDef xServiceDef = Mockito.mock(XXServiceDef.class); XXServiceConfigDefDao xServiceConfigDefDao = Mockito .mock(XXServiceConfigDefDao.class); XXResourceDefDao xResourceDefDao = Mockito.mock(XXResourceDefDao.class); XXAccessTypeDefDao xAccessTypeDefDao = Mockito .mock(XXAccessTypeDefDao.class); XXPolicyConditionDefDao xPolicyConditionDefDao = Mockito .mock(XXPolicyConditionDefDao.class); XXContextEnricherDefDao xContextEnricherDefDao = Mockito .mock(XXContextEnricherDefDao.class); XXEnumDefDao xEnumDefDao = Mockito.mock(XXEnumDefDao.class); XXDataMaskTypeDefDao xDataMaskDefDao = Mockito.mock(XXDataMaskTypeDefDao.class); XXServiceDao xServiceDao = Mockito.mock(XXServiceDao.class); RangerServiceDef rangerServiceDef = rangerServiceDef(); Long serviceDefId = rangerServiceDef.getId(); List<XXServiceConfigDef> svcConfDefList = new ArrayList<XXServiceConfigDef>(); XXServiceConfigDef serviceConfigDefObj = new XXServiceConfigDef(); serviceConfigDefObj.setId(Id); serviceConfigDefObj.setType("1"); svcConfDefList.add(serviceConfigDefObj); Mockito.when( serviceDefService.populateRangerServiceConfigDefToXX( Mockito.any(RangerServiceConfigDef.class), Mockito.any(XXServiceConfigDef.class), Mockito.any(XXServiceDef.class), Mockito.eq(RangerServiceDefService.OPERATION_CREATE_CONTEXT))).thenReturn(serviceConfigDefObj); Mockito.when(xServiceConfigDefDao.create(serviceConfigDefObj)) .thenReturn(serviceConfigDefObj); List<XXResourceDef> resDefList = new ArrayList<XXResourceDef>(); XXResourceDef resourceDef = new XXResourceDef(); resourceDef.setAddedByUserId(Id); resourceDef.setCreateTime(new Date()); resourceDef.setDefid(Id); resourceDef.setDescription("test"); resourceDef.setId(Id); resDefList.add(resourceDef); List<XXAccessTypeDef> accessTypeDefList = new ArrayList<XXAccessTypeDef>(); XXAccessTypeDef accessTypeDefObj = new XXAccessTypeDef(); accessTypeDefObj.setAddedByUserId(Id); accessTypeDefObj.setCreateTime(new Date()); accessTypeDefObj.setDefid(Id); accessTypeDefObj.setId(Id); accessTypeDefObj.setLabel("Read"); accessTypeDefObj.setName("read"); accessTypeDefObj.setOrder(null); accessTypeDefObj.setRbkeylabel(null); accessTypeDefObj.setUpdatedByUserId(Id); accessTypeDefObj.setUpdateTime(new Date()); accessTypeDefList.add(accessTypeDefObj); List<XXPolicyConditionDef> policyConditionDefList = new ArrayList<XXPolicyConditionDef>(); XXPolicyConditionDef policyConditionDefObj = new XXPolicyConditionDef(); policyConditionDefObj.setAddedByUserId(Id); policyConditionDefObj.setCreateTime(new Date()); policyConditionDefObj.setDefid(Id); policyConditionDefObj.setDescription("policy"); policyConditionDefObj.setId(Id); policyConditionDefObj.setName("country"); policyConditionDefObj.setOrder(0); policyConditionDefObj.setUpdatedByUserId(Id); policyConditionDefObj.setUpdateTime(new Date()); policyConditionDefList.add(policyConditionDefObj); List<XXContextEnricherDef> contextEnricherDefList = new ArrayList<XXContextEnricherDef>(); XXContextEnricherDef contextEnricherDefObj = new XXContextEnricherDef(); contextEnricherDefObj.setAddedByUserId(Id); contextEnricherDefObj.setCreateTime(new Date()); contextEnricherDefObj.setDefid(Id); contextEnricherDefObj.setId(Id); contextEnricherDefObj.setName("country-provider"); contextEnricherDefObj .setEnricherOptions("contextName=COUNTRY;dataFile=/etc/ranger/data/userCountry.properties"); contextEnricherDefObj.setEnricher("RangerCountryProvider"); contextEnricherDefObj.setOrder(null); contextEnricherDefObj.setUpdatedByUserId(Id); contextEnricherDefObj.setUpdateTime(new Date()); contextEnricherDefList.add(contextEnricherDefObj); List<XXEnumDef> enumDefList = new ArrayList<XXEnumDef>(); XXEnumDef enumDefObj = new XXEnumDef(); enumDefObj.setAddedByUserId(Id); enumDefObj.setCreateTime(new Date()); enumDefObj.setDefaultindex(0); enumDefObj.setDefid(Id); enumDefObj.setId(Id); enumDefObj.setName("authnType"); enumDefObj.setUpdatedByUserId(Id); enumDefObj.setUpdateTime(new Date()); enumDefList.add(enumDefObj); Mockito.when(daoManager.getXXServiceDef()).thenReturn(xServiceDefDao); Mockito.when(xServiceDefDao.getById(serviceDefId)).thenReturn( xServiceDef); Mockito.when(daoManager.getXXServiceConfigDef()).thenReturn( xServiceConfigDefDao); Mockito.when(daoManager.getXXResourceDef()).thenReturn(xResourceDefDao); Mockito.when(daoManager.getXXAccessTypeDef()).thenReturn( xAccessTypeDefDao); Mockito.when(daoManager.getXXPolicyConditionDef()).thenReturn( xPolicyConditionDefDao); Mockito.when(daoManager.getXXContextEnricherDef()).thenReturn( xContextEnricherDefDao); Mockito.when(daoManager.getXXEnumDef()).thenReturn(xEnumDefDao); Mockito.when(daoManager.getXXDataMaskTypeDef()).thenReturn(xDataMaskDefDao); Mockito.when(daoManager.getXXService()).thenReturn(xServiceDao); Mockito.when(xServiceDao.findByServiceDefId(serviceDefId)).thenReturn(null); Mockito.when(serviceDefService.read(Id)).thenReturn(rangerServiceDef); RangerServiceDef dbServiceDef = serviceDBStore .updateServiceDef(rangerServiceDef); Assert.assertNotNull(dbServiceDef); Assert.assertEquals(dbServiceDef, rangerServiceDef); Assert.assertEquals(dbServiceDef.getId(), rangerServiceDef.getId()); Assert.assertEquals(dbServiceDef.getCreatedBy(), rangerServiceDef.getCreatedBy()); Assert.assertEquals(dbServiceDef.getDescription(), rangerServiceDef.getDescription()); Assert.assertEquals(dbServiceDef.getGuid(), rangerServiceDef.getGuid()); Assert.assertEquals(dbServiceDef.getImplClass(), rangerServiceDef.getImplClass()); Assert.assertEquals(dbServiceDef.getLabel(), rangerServiceDef.getLabel()); Assert.assertEquals(dbServiceDef.getName(), rangerServiceDef.getName()); Assert.assertEquals(dbServiceDef.getRbKeyDescription(), rangerServiceDef.getRbKeyDescription()); Assert.assertEquals(dbServiceDef.getConfigs(), rangerServiceDef.getConfigs()); Assert.assertEquals(dbServiceDef.getVersion(), rangerServiceDef.getVersion()); Assert.assertEquals(dbServiceDef.getResources(), rangerServiceDef.getResources()); }