InstanceConfigAuditUtil implements InitializingBean { void doAudit(InstanceConfigAuditModel auditModel) { String instanceCacheKey = assembleInstanceKey(auditModel.getAppId(), auditModel .getClusterName(), auditModel.getIp(), auditModel.getDataCenter()); Long instanceId = instanceCache.getIfPresent(instanceCacheKey); if (instanceId == null) { instanceId = prepareInstanceId(auditModel); instanceCache.put(instanceCacheKey, instanceId); } String instanceConfigCacheKey = assembleInstanceConfigKey(instanceId, auditModel .getConfigAppId(), auditModel.getConfigNamespace()); String cacheReleaseKey = instanceConfigReleaseKeyCache.getIfPresent(instanceConfigCacheKey); if (cacheReleaseKey != null && Objects.equals(cacheReleaseKey, auditModel.getReleaseKey())) { return; } instanceConfigReleaseKeyCache.put(instanceConfigCacheKey, auditModel.getReleaseKey()); InstanceConfig instanceConfig = instanceService.findInstanceConfig(instanceId, auditModel .getConfigAppId(), auditModel.getConfigNamespace()); if (instanceConfig != null) { if (!Objects.equals(instanceConfig.getReleaseKey(), auditModel.getReleaseKey())) { instanceConfig.setConfigClusterName(auditModel.getConfigClusterName()); instanceConfig.setReleaseKey(auditModel.getReleaseKey()); instanceConfig.setReleaseDeliveryTime(auditModel.getOfferTime()); } else if (offerTimeAndLastModifiedTimeCloseEnough(auditModel.getOfferTime(), instanceConfig.getDataChangeLastModifiedTime())) { return; } instanceConfig.setDataChangeLastModifiedTime(auditModel.getOfferTime()); instanceService.updateInstanceConfig(instanceConfig); return; } instanceConfig = new InstanceConfig(); instanceConfig.setInstanceId(instanceId); instanceConfig.setConfigAppId(auditModel.getConfigAppId()); instanceConfig.setConfigClusterName(auditModel.getConfigClusterName()); instanceConfig.setConfigNamespaceName(auditModel.getConfigNamespace()); instanceConfig.setReleaseKey(auditModel.getReleaseKey()); instanceConfig.setReleaseDeliveryTime(auditModel.getOfferTime()); instanceConfig.setDataChangeCreatedTime(auditModel.getOfferTime()); try { instanceService.createInstanceConfig(instanceConfig); } catch (DataIntegrityViolationException ex) { } } InstanceConfigAuditUtil(final InstanceService instanceService); boolean audit(String appId, String clusterName, String dataCenter, String
      ip, String configAppId, String configClusterName, String configNamespace, String releaseKey); @Override void afterPropertiesSet();  }
@Test public void testDoAudit() throws Exception { long someInstanceId = 1; Instance someInstance = mock(Instance.class); when(someInstance.getId()).thenReturn(someInstanceId); when(instanceService.createInstance(any(Instance.class))).thenReturn(someInstance); instanceConfigAuditUtil.doAudit(someAuditModel); verify(instanceService, times(1)).findInstance(someAppId, someClusterName, someDataCenter, someIp); verify(instanceService, times(1)).createInstance(any(Instance.class)); verify(instanceService, times(1)).findInstanceConfig(someInstanceId, someConfigAppId, someConfigNamespace); verify(instanceService, times(1)).createInstanceConfig(any(InstanceConfig.class)); }