@Override @Transactional public EndpointGroupDto findDefaultGroup(String appId) { validateSqlId(appId, "Can't find default endpoint group by app id. Incorrect app id " + appId); return getDto(endpointGroupDao.findByAppIdAndWeight(appId, DEFAULT_GROUP_WEIGHT)); }
@Override @Transactional public List<EndpointGroupDto> findEndpointGroupsByAppId(String applicationId) { validateSqlId(applicationId, "Can't find endpoint groups by application id. Incorrect " + "application id " + applicationId); return convertDtoList(endpointGroupDao.findByApplicationId(applicationId)); }
@Override @Transactional public EndpointGroupDto findEndpointGroupById(String id) { validateSqlId(id, "Can't find endpoint group by id. Incorrect id " + id); return getDto(endpointGroupDao.findById(id)); }
if (isValidId(appId)) { String id = endpointGroupDto.getId(); EndpointGroup groupWithSameWeight = endpointGroupDao.findByAppIdAndWeight(appId, endpointGroupDto.getWeight()); EndpointGroup groupWithSameName = endpointGroupDao.findByAppIdAndName(appId, endpointGroupDto.getName()); if (StringUtils.isBlank(id)) { } else { endpointGroupDto.setCreatedTime(System.currentTimeMillis()); savedGroup = getDto(endpointGroupDao.save(new EndpointGroup(endpointGroupDto))); EndpointGroup previousGroup = endpointGroupDao.findById(id); if (previousGroup != null) { + "application id"); } else { savedGroup = getDto(endpointGroupDao.save(new EndpointGroup(endpointGroupDto)));
EndpointGroup endpointGroup = endpointGroupDao.findById(id); ChangeNotificationDto changeDto = null; if (endpointGroup endpointGroupDao.removeById(id); EndpointGroupDto groupDto = endpointGroup.toDto(); HistoryDto historyDto = addHistory(groupDto, ChangeType.REMOVE_GROUP);
@Override public List<UpdateNotificationDto<EndpointGroupDto>> removeTopicById(String id) { validateId(id, "Can't remove topic. Invalid topic id " + id); TopicDto topic = findTopicById(id); List<UpdateNotificationDto<EndpointGroupDto>> notificationList = new LinkedList<>(); if (topic != null) { List<EndpointGroup> groups = endpointGroupDao.findEndpointGroupsByTopicIdAndAppId( topic.getApplicationId(), id); if (groups != null && !groups.isEmpty()) { for (EndpointGroup eg : groups) { notificationList.add(endpointService.removeTopicFromEndpointGroup(eg.getId().toString(), id)); } } topicDao.removeById(id); notificationDao.removeNotificationsByTopicId(id); } return notificationList; }
@Test public void addTopicToEndpointGroup() { Topic first = generateTopic(null, null, "first"); Topic second = generateTopic(first.getApplication(), null, "second"); Topic third = generateTopic(first.getApplication(), null, "third"); Set<Topic> topics = new HashSet<>(); topics.add(first); topics.add(second); topics.add(third); EndpointGroup endpointGroup = generateEndpointGroup(first.getApplication(), topics); endpointGroupDao.addTopicToEndpointGroup(endpointGroup.getId().toString(), first.getId().toString()); endpointGroupDao.addTopicToEndpointGroup(endpointGroup.getId().toString(), second.getId().toString()); EndpointGroup saved = endpointGroupDao.addTopicToEndpointGroup(endpointGroup.getId().toString(), third.getId().toString()); Assert.assertNotNull(saved); Assert.assertEquals(topics, saved.getTopics()); }
@Test public void removeTopicFromEndpointGroup() { Topic first = generateTopic(null, null, "first"); Topic second = generateTopic(first.getApplication(), null, "second"); Topic third = generateTopic(first.getApplication(), null, "third"); Set<Topic> topics = new HashSet<>(); topics.add(first); topics.add(second); topics.add(third); EndpointGroup endpointGroup = generateEndpointGroup(first.getApplication(), topics); Assert.assertEquals(3, endpointGroup.getTopics().size()); endpointGroupDao.removeTopicFromEndpointGroup(endpointGroup.getId().toString(), first.getId().toString()); EndpointGroup group = endpointGroupDao.removeTopicFromEndpointGroup(endpointGroup.getId().toString(), second.getId().toString()); Set<Topic> expected = new HashSet<>(); expected.add(third); Assert.assertEquals(expected, group.getTopics()); }
protected EndpointGroup generateEndpointGroup(Application app, Set<Topic> topics) { EndpointGroup group = new EndpointGroup(); if (app == null) { app = generateApplication(null); } group.setApplication(app); group.setName("GROUP_ALL_" + RANDOM.nextInt()); group.setWeight(RANDOM.nextInt()); group.setTopics(topics); return endpointGroupDao.save(group); }
@Test public void findByAppIdAndName() { EndpointGroup endpointGroup = generateEndpointGroup(null, null); EndpointGroup group = endpointGroupDao.findByAppIdAndName(endpointGroup.getApplicationId(), endpointGroup.getName()); Assert.assertEquals(endpointGroup, group); }
@Test public void removeEndpointGroupTest() { Topic first = generateTopic(null, null, "first"); Topic second = generateTopic(first.getApplication(), null, "second"); Set<Topic> topicsOne = new HashSet<>(); topicsOne.add(first); Set<Topic> topicsTwo = new HashSet<>(); topicsTwo.add(second); EndpointGroup endpointGroupOne = generateEndpointGroup(first.getApplication(), topicsOne); generateEndpointGroup(first.getApplication(), topicsTwo); endpointGroupDao.removeById(endpointGroupOne.getStringId()); Assert.assertNull(endpointGroupDao.findById(endpointGroupOne.getStringId())); Topic f = topicDao.findById(first.getStringId()); Assert.assertNotNull(f); Assert.assertTrue(f.getEndpointGroups().isEmpty()); }
@Test public void findEndpointGroupsByTopicIdAndAppId() throws InterruptedException { Topic topic = generateTopic(null, null, null); Set<Topic> topics = new HashSet<>(); topics.add(topic); EndpointGroup endpointGroup = generateEndpointGroup(topic.getApplication(), topics); List<EndpointGroup> found = endpointGroupDao.findEndpointGroupsByTopicIdAndAppId(topic.getApplication().getId().toString(), topic.getId().toString()); Assert.assertNotNull(found); Assert.assertEquals(1, found.size()); Assert.assertEquals(endpointGroup, found.get(0)); }
@Override @Transactional public UpdateNotificationDto<EndpointGroupDto> addTopicToEndpointGroup(String id, String topicId) { validateSqlId(id, "Can't add topics " + topicId + " to endpoint group . Incorrect endpoint group id." + id); UpdateNotificationDto<EndpointGroupDto> dto = null; EndpointGroup endpointGroup = endpointGroupDao.addTopicToEndpointGroup(id, topicId); if (endpointGroup != null) { dto = new UpdateNotificationDto<>(); HistoryDto history = addHistory(endpointGroup.toDto(), ChangeType.ADD_TOPIC, topicId); if (history != null) { dto.setAppId(history.getApplicationId()); dto.setAppSeqNumber(history.getSequenceNumber()); } dto.setChangeType(ChangeType.ADD_TOPIC); dto.setTopicId(topicId); dto.setGroupId(endpointGroup.getId().toString()); dto.setPayload(endpointGroup.toDto()); } else { LOG.debug("Can't add topic [{}] to endpoint group [{}]", topicId, id); } return dto; }
@Override @Transactional public UpdateNotificationDto<EndpointGroupDto> removeTopicFromEndpointGroup(String id, String topicId) { validateSqlId(id, "Can't remove endpoint group topics " + topicId + ". Incorrect endpoint group id." + id); UpdateNotificationDto<EndpointGroupDto> dto = null; EndpointGroup endpointGroup = endpointGroupDao.removeTopicFromEndpointGroup(id, topicId); if (endpointGroup != null) { dto = new UpdateNotificationDto<>(); HistoryDto history = addHistory(endpointGroup.toDto(), ChangeType.REMOVE_TOPIC, topicId); if (history != null) { dto.setAppId(history.getApplicationId()); dto.setAppSeqNumber(history.getSequenceNumber()); } dto.setChangeType(ChangeType.REMOVE_TOPIC); dto.setTopicId(topicId); dto.setGroupId(endpointGroup.getId().toString()); dto.setPayload(endpointGroup.toDto()); } else { LOG.debug("Can't remove topic [{}] from endpoint group [{}]", topicId, id); } return dto; }
@Override public List<TopicDto> findVacantTopicsByEndpointGroupId(String endpointGroupId) { validateId(endpointGroupId, "Can't find vacant topics. Invalid endpoint group id " + endpointGroupId); List<TopicDto> topics = Collections.emptyList(); EndpointGroup endpointGroup = endpointGroupDao.findById(endpointGroupId); String applicationId = null; if (endpointGroup != null) { applicationId = endpointGroup.getApplicationId(); } if (isNotBlank(applicationId)) { topics = convertDtoList(topicDao.findVacantTopicsByGroupId(applicationId, endpointGroupId)); } else { LOG.warn("Can't get application id from endpoint group."); } return topics; }
@Override public ConfigurationSchemaDto saveConfSchema(ConfigurationSchemaDto configurationSchema) { ConfigurationSchemaDto savedConfigSchema = null; if (configurationSchema != null) { String appId = configurationSchema.getApplicationId(); if (isValidId(appId)) { LOG.debug("Finding default endpoint group for application id [{}]", appId); EndpointGroup endpointGroup = endpointGroupDao.findByAppIdAndWeight(appId, 0); if (endpointGroup != null) { savedConfigSchema = saveConfSchema(configurationSchema, idToString(endpointGroup)); } else { LOG.warn("Can't find default group for application [{}]", appId); } } else { LOG.warn("Can't find endpoint group. Invalid application id [{}]", appId); } } else { LOG.warn("Configuration schema object is null"); } return savedConfigSchema; }
@Override @Transactional public void removeEndpointGroupByAppId(String applicationId) { validateSqlId(applicationId, "Can't remove endpoint groups by application id. Incorrect " + "application id " + applicationId); List<EndpointGroup> groups = endpointGroupDao.findByApplicationId(applicationId); if (groups != null && !groups.isEmpty()) { for (EndpointGroup group : groups) { removeEndpointGroup(group.getId().toString(), true); } } }
@Override public List<TopicDto> findTopicsByEndpointGroupId(String endpointGroupId) { validateId(endpointGroupId, "Can't find topics. Invalid endpoint group id " + endpointGroupId); EndpointGroup endpointGroup = endpointGroupDao.findById(endpointGroupId); Set<Topic> topics = endpointGroup.getTopics(); return convertDtoList(topics); }
@Override public ConfigurationDto findDefaultConfigurationBySchemaId(String schemaId) { validateId(schemaId, "Schema id is incorrect. Can't find default configuration by schema id " + schemaId); ConfigurationDto configuration = null; ConfigurationSchema configurationSchema = configurationSchemaDao.findById(schemaId); if (configurationSchema != null) { String appId = configurationSchema.getApplicationId(); EndpointGroup endpointGroup = endpointGroupDao.findByAppIdAndWeight(appId, 0); if (endpointGroup != null) { String endpointGroupId = String.valueOf(endpointGroup.getId()); configuration = getDto(configurationDao.findConfigurationByEndpointGroupIdAndVersion( endpointGroupId, configurationSchema.getVersion())); } else { LOG.warn("Can't find default group for application [{}]", appId); } } else { LOG.warn("Can't find configuration schema for id [{}]", schemaId); } return configuration; }
@Test public void findByApplicationId() { EndpointGroup endpointGroup = generateEndpointGroup(null, null); List<EndpointGroup> groups = endpointGroupDao.findByApplicationId(endpointGroup.getApplicationId()); Assert.assertEquals(1, groups.size()); }