@Override public TopicDto saveTopic(TopicDto topicDto) { validateSqlObject(topicDto, "Can't save topic. Invalid topic object"); if (StringUtils.isBlank(topicDto.getId())) { LOG.debug("Save new topic."); topicDto.setCreatedTime(System.currentTimeMillis()); Topic topic = topicDao.findTopicByAppIdAndName(topicDto.getApplicationId(), topicDto.getName()); if (topic != null) { throw new IllegalArgumentException("Topic with the same name already present!"); } } return getDto(topicDao.save(new Topic(topicDto))); }
@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; }
@Override public TopicDto getTopic(String topicId) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { TopicDto topic = controlService.getTopic(topicId); Utils.checkNotNull(topic); checkApplicationId(topic.getApplicationId()); return topic; } catch (Exception ex) { throw Utils.handleException(ex); } }
@Test public void removeTopicsByAppIdTest() { TopicDto topic = generateTopicDto(null, null); topicService.removeTopicsByAppId(topic.getApplicationId()); List<TopicDto> found = topicService.findTopicsByAppId(topic.getApplicationId()); Assert.assertNotNull(found); Assert.assertTrue(found.isEmpty()); }
@Test public void findTopicsByAppIdAndTypeTest() { TopicDto topic = generateTopicDto(null, TopicTypeDto.OPTIONAL); List<TopicDto> found = topicService.findTopicsByAppIdAndType(topic.getApplicationId(), TopicTypeDto.OPTIONAL); Assert.assertEquals(1, found.size()); Assert.assertEquals(topic, found.get(0)); }
@Test public void findTopicsByAppIdTest() { TopicDto topic = generateTopicDto(null, null); List<TopicDto> found = topicService.findTopicsByAppId(topic.getApplicationId()); Assert.assertEquals(1, found.size()); Assert.assertEquals(topic, found.get(0)); }
@Override public void deleteTopic(String topicId) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { checkTopicId(topicId); TopicDto topic = controlService.getTopic(topicId); Utils.checkNotNull(topic); checkApplicationId(topic.getApplicationId()); controlService.deleteTopicById(topicId); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Override public void addTopicToEndpointGroup(String endpointGroupId, String topicId) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { checkEndpointGroupId(endpointGroupId); checkTopicId(topicId); TopicDto topic = controlService.getTopic(topicId); Utils.checkNotNull(topic); checkApplicationId(topic.getApplicationId()); controlService.addTopicsToEndpointGroup(endpointGroupId, topicId); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Override public void removeTopicFromEndpointGroup(String endpointGroupId, String topicId) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { checkEndpointGroupId(endpointGroupId); checkTopicId(topicId); TopicDto topic = controlService.getTopic(topicId); Utils.checkNotNull(topic); checkApplicationId(topic.getApplicationId()); controlService.removeTopicsFromEndpointGroup(endpointGroupId, topicId); } catch (Exception ex) { throw Utils.handleException(ex); } }
/** * Instantiates new topic. */ public Topic(TopicDto dto) { if (dto != null) { this.id = getLongId(dto); this.name = dto.getName(); this.description = dto.getDescription(); this.createdUsername = dto.getCreatedUsername(); this.createdTime = dto.getCreatedTime(); this.sequenceNumber = dto.getSecNum(); this.type = dto.getType(); Long appId = getLongId(dto.getApplicationId()); this.application = appId != null ? new Application(appId) : null; } }
@Override public TopicDto editTopic(TopicDto topic) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { if (isEmpty(topic.getId())) { topic.setCreatedUsername(getCurrentUser().getUsername()); checkApplicationId(topic.getApplicationId()); } else { throw new KaaAdminServiceException( "Unable to edit existing topic!", ServiceErrorCode.INVALID_ARGUMENTS); } return controlService.editTopic(topic); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Test public void testRemoveUnicastNotificationsByKeyHash() { TopicDto topicDto = generateTopicDto(null, null); EndpointProfileDto profile = generateEndpointProfileDto(topicDto.getApplicationId(), Arrays.asList(topicDto.getId())); byte[] keyHash = profile.getEndpointKeyHash(); EndpointNotificationDto notification = generateUnicastNotificationDto(null, topicDto.getId(), keyHash); Assert.assertTrue(isNotBlank(notification.getId())); notificationService.removeUnicastNotificationsByKeyHash(keyHash); List<EndpointNotificationDto> notifications = notificationService.findUnicastNotificationsByKeyHash(keyHash); Assert.assertTrue(notifications.isEmpty()); }
@Override public EndpointNotificationDto sendUnicastNotification(NotificationDto notification, String clientKeyHash, byte[] body) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { checkExpiredDate(notification); notification.setBody(body); checkApplicationId(notification.getApplicationId()); TopicDto topic = controlService.getTopic(notification.getTopicId()); Utils.checkNotNull(topic); checkApplicationId(topic.getApplicationId()); EndpointNotificationDto unicastNotification = new EndpointNotificationDto(); unicastNotification.setEndpointKeyHash( Base64.decode(clientKeyHash.getBytes(Charsets.UTF_8))); unicastNotification.setNotificationDto(notification); return controlService.editUnicastNotification(unicastNotification); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Override public NotificationDto sendNotification(NotificationDto notification, byte[] body) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { checkExpiredDate(notification); notification.setBody(body); checkApplicationId(notification.getApplicationId()); TopicDto topic = controlService.getTopic(notification.getTopicId()); Utils.checkNotNull(topic); checkApplicationId(topic.getApplicationId()); return controlService.editNotification(notification); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Test public void removeTopicByIdTest() { TopicDto topic = generateTopicDto(null, null); topicService.removeTopicById(topic.getId()); TopicDto found = topicService.findTopicById(topic.getId()); Assert.assertNull(found); List<EndpointGroupDto> groups = endpointService.findEndpointGroupsByAppId(topic.getApplicationId()); Assert.assertFalse(groups.isEmpty()); }
@Test public void testFindUnicastNotificationById() { TopicDto topicDto = generateTopicDto(null, null); EndpointProfileDto profile = generateEndpointProfileDto(topicDto.getApplicationId(), Arrays.asList(topicDto.getId())); byte[] keyHash = profile.getEndpointKeyHash(); EndpointNotificationDto notification = generateUnicastNotificationDto(null, topicDto.getId(), keyHash); Assert.assertTrue(isNotBlank(notification.getId())); EndpointNotificationDto found = notificationService.findUnicastNotificationById(notification.getId()); Assert.assertEquals(notification, found); }
@Test public void testRemoveUnicastNotificationById() { TopicDto topicDto = generateTopicDto(null, null); EndpointProfileDto profile = generateEndpointProfileDto(topicDto.getApplicationId(), Arrays.asList(topicDto.getId())); byte[] keyHash = profile.getEndpointKeyHash(); EndpointNotificationDto notification = generateUnicastNotificationDto(null, topicDto.getId(), keyHash); Assert.assertTrue(isNotBlank(notification.getId())); notificationService.removeUnicastNotificationById(notification.getId()); EndpointNotificationDto notif = notificationService.findUnicastNotificationById(notification.getId()); Assert.assertNull(notif); }
@Override public void sendNotification(NotificationDto notification, RecordField notificationData) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { checkExpiredDate(notification); GenericRecord record = FormAvroConverter.createGenericRecordFromRecordField( notificationData); GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>( record.getSchema()); byte[] body = converter.encodeToJsonBytes(record); notification.setBody(body); checkApplicationId(notification.getApplicationId()); TopicDto topic = controlService.getTopic(notification.getTopicId()); Utils.checkNotNull(topic); checkApplicationId(topic.getApplicationId()); controlService.editNotification(notification); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Test public void removeTopicByIdAddedToGroupTest() { TopicDto topic = generateTopicDto(null, null); EndpointGroupDto groupDto = generateEndpointGroupDto(topic.getApplicationId()); endpointService.addTopicToEndpointGroup(groupDto.getId(), topic.getId()); topicService.removeTopicById(topic.getId()); TopicDto found = topicService.findTopicById(topic.getId()); Assert.assertNull(found); }