@Test public void editConfigurationObjectTest() { List<ConfigurationDto> configs = generateConfigurationDto(null, null, 1, false, false); ConfigurationDto saved = configurationService.findConfigurationById(configs.get(0).getId()); ConfigurationDto configurationToEdit = new ConfigurationDto(); configurationToEdit.setId(saved.getId()); configurationToEdit.setApplicationId(saved.getApplicationId()); configurationToEdit.setEndpointGroupId(saved.getEndpointGroupId()); configurationToEdit.setSchemaId(saved.getSchemaId()); configurationToEdit.setVersion(saved.getVersion()); configurationToEdit.setBody(saved.getBody()); ConfigurationDto updated = configurationService.saveConfiguration(configurationToEdit); Assert.assertNotNull(saved); Assert.assertNotNull(updated); Assert.assertEquals(saved.getSchemaVersion(), updated.getSchemaVersion()); Assert.assertEquals(saved.getCreatedTime(), updated.getCreatedTime()); Assert.assertEquals(saved.getCreatedUsername(), updated.getCreatedUsername()); }
@Override public ConfigurationDto saveConfiguration(ConfigurationDto configurationDto) { validateConfiguration(configurationDto); String id = configurationDto.getId(); ConfigurationSchemaDto configurationSchemaDto; Configuration oldActiveConfiguration = null; if (StringUtils.isNotBlank(id)) { ConfigurationDto oldConfiguration = findConfigurationById(configurationDto.getId()); if (oldConfiguration != null && oldConfiguration.getStatus() != INACTIVE) { throw new UpdateStatusConflictException("Can't update configuration, invalid id " + id); configurationSchemaDto = findConfSchemaById(configurationDto.getSchemaId()); configurationDto.setSchemaVersion(configurationSchemaDto.getVersion()); configurationDto.setCreatedTime(oldConfiguration.getCreatedTime()); configurationDto.setCreatedUsername(oldConfiguration.getCreatedUsername()); LOG.debug("Update existing configuration with id: [{}]", configurationDto.getId()); } else { String schemaId = configurationDto.getSchemaId(); String groupId = configurationDto.getEndpointGroupId(); configurationSchemaDto = findConfSchemaById(schemaId); if (configurationSchemaDto != null) { schemaId, groupId); if (oldInactiveConfiguration != null) { configurationDto.setId(idToString(oldInactiveConfiguration.getId())); configurationDto.setSequenceNumber(oldInactiveConfiguration.getSequenceNumber()); } else if (oldActiveConfiguration != null) { configurationDto.setSequenceNumber(oldActiveConfiguration.getSequenceNumber()); configurationDto.setApplicationId(configurationSchemaDto.getApplicationId());
private void validateConfiguration(ConfigurationDto dto) { validateSqlObject(dto, "Can't save configuration, object is invalid."); validateSqlId(dto.getSchemaId(), "Configuration object invalid. " + "Incorrect configuration schema id : " + dto.getSchemaId()); validateSqlId(dto.getEndpointGroupId(), "Configuration object invalid. " + "Incorrect endpoint group id : " + dto.getEndpointGroupId()); }
/** * Instantiates the Configuration. */ public Configuration(ConfigurationDto dto) { super(dto); if (dto != null) { Long schemaId = getLongId(dto.getSchemaId()); this.configurationSchema = schemaId != null ? new ConfigurationSchema(schemaId) : null; this.configurationBody = stringToBinary(dto.getBody()); this.schemaVersion = dto.getSchemaVersion(); } }
@Override public ConfigurationDto toDto() { ConfigurationDto dto = super.toDto(); dto.setBody(binaryToString(configurationBody)); dto.setSchemaId(ModelUtils.getStringId(configurationSchema.getId())); dto.setSchemaVersion(schemaVersion); dto.setProtocolSchema(configurationSchema != null ? configurationSchema.getProtocolSchema() : null); return dto; }
KaaData body = configurationProcessor.getRootData(); LOG.debug("Default configuration {} ", body.getRawData()); ConfigurationDto configurationDto = new ConfigurationDto(); configurationDto.setBody(body.getRawData()); configurationDto.setSchemaId(savedSchema.getId()); configurationDto.setDescription(DEFAULT_STRUCT_DESC); configurationDto.setEndpointGroupId(groupId); configurationDto.setCreatedUsername(savedSchema.getCreatedUsername()); ConfigurationDto savedConfiguration = saveConfiguration(configurationDto); if (savedConfiguration != null) { activateConfiguration(savedConfiguration.getId(), savedSchema.getCreatedUsername()); } else { LOG.warn("Can't save default configuration.");
ids = new ArrayList<>(); for (int i = 0; i < count; i++) { ConfigurationDto dto = new ConfigurationDto(); dto.setId(null); dto.setStatus(null); dto.setBody(configurationProcessor.getRootData().getRawData()); dto.setSchemaId(schemaDto.getId()); if (isBlank(groupId)) { groupId = generateEndpointGroupDto(schemaDto.getApplicationId()).getId(); dto.setEndpointGroupId(groupId); ConfigurationDto saved = configurationService.saveConfiguration(dto); Assert.assertNotNull(saved); if (activate) { ChangeConfigurationNotification notification = configurationService.activateConfiguration(saved.getId(), schemaDto.getCreatedUsername()); saved = notification.getConfigurationDto();
private HistoryDto addHistory(ConfigurationDto dto, ChangeType type) { LOG.debug("Add history information about configuration update"); HistoryDto history = new HistoryDto(); history.setApplicationId(dto.getApplicationId()); ChangeDto change = new ChangeDto(); change.setConfigurationId(dto.getId()); change.setCfVersion(dto.getSchemaVersion()); change.setEndpointGroupId(dto.getEndpointGroupId()); change.setType(type); history.setChange(change); return historyService.saveHistory(history); }
@Test public void saveConfigurationObjectWithoutInactiveConfigurationTest() throws SchemaCreationException { List<ConfigurationDto> configs = generateConfigurationDto(null, null, 3, true, false); ConfigurationDto saved = configurationService.findConfigurationById(configs.get(2).getId()); String id = saved.getId(); saved.setId(null); ConfigurationDto updated = configurationService.saveConfiguration(saved); Assert.assertNotNull(updated); Assert.assertEquals(updated.getStatus(), UpdateStatus.INACTIVE); Assert.assertEquals(saved.getSequenceNumber(), updated.getSequenceNumber()); Assert.assertNotEquals(id, updated.getId()); }
@Override public ConfigurationDto editConfiguration(ConfigurationDto configuration) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { String username = getCurrentUser().getUsername(); if (isEmpty(configuration.getId())) { configuration.setCreatedUsername(username); checkEndpointGroupId(configuration.getEndpointGroupId()); } else { configuration.setModifiedUsername(username); ConfigurationDto storedConfiguration = controlService.getConfiguration( configuration.getId()); Utils.checkNotNull(storedConfiguration); checkEndpointGroupId(storedConfiguration.getEndpointGroupId()); } return controlService.editConfiguration(configuration); } catch (StaleObjectStateException ex) { LOG.error( "Someone has already updated the configuration. Reload page to be able to edit it. ", ex); throw new KaaAdminServiceException( "Someone has already updated the configuration. Reload page to be able to edit it.", ServiceErrorCode.GENERAL_ERROR); } catch (Exception ex) { throw Utils.handleException(ex); } }
try { EndpointGroup endpointGroup = endpointGroupDao.findById( currentConfiguration.getEndpointGroupId()); GenericAvroConverter<GenericRecord> avroConverter; Schema avroSchema; previousRecord = avroConverter.decodeJson(previousConfiguration.getBody()); currentConfiguration.getBody(), avroSchema)); body = uuidValidator.validateUuidFields(currentRecord, previousRecord); currentConfiguration.setBody(body.getRawData()); } else { throw new RuntimeException("Can't generate json configuration body."); // NOSONAR
/** * Converts a configuration into a configuration record. * * @param configurations the configurations for converting * @return converted configurations */ public static List<ConfigurationRecordDto> convertToConfigurationRecords( Collection<ConfigurationDto> configurations) { Map<String, ConfigurationRecordDto> configurationRecordsMap = new HashMap<>(); for (ConfigurationDto configuration : configurations) { ConfigurationRecordDto configurationRecord = configurationRecordsMap .get(configuration.getSchemaId()); if (configurationRecord == null) { configurationRecord = new ConfigurationRecordDto(); configurationRecordsMap.put(configuration.getSchemaId(), configurationRecord); } if (configuration.getStatus() == UpdateStatus.ACTIVE) { configurationRecord.setActiveStructureDto(configuration); } else if (configuration.getStatus() == UpdateStatus.INACTIVE) { configurationRecord.setInactiveStructureDto(configuration); } } return new ArrayList<>(configurationRecordsMap.values()); }
@Test public void saveConfigurationObjectWithoutIdTest() throws SchemaCreationException { List<ConfigurationDto> configs = generateConfigurationDto(null, null, 1, false, false); ConfigurationDto saved = configurationService.findConfigurationById(configs.get(0).getId()); String inactiveId = saved.getId(); saved.setId(null); ConfigurationDto updated = configurationService.saveConfiguration(saved); Assert.assertNotNull(saved); Assert.assertEquals(saved.getStatus(), UpdateStatus.INACTIVE); Assert.assertEquals(inactiveId, updated.getId()); }
@Override public String compute(ConfigurationIdKey key) { LOG.debug("Fetching result for getConfIdByKey"); String confId = null; List<ConfigurationDto> configurations = configurationService.findConfigurationsByEndpointGroupId(key.getEndpointGroupId()); for (ConfigurationDto confDto : configurations) { if (confDto.getSchemaVersion() == key.getConfigSchemaVersion()) { confId = confDto.getId(); break; } } return confId; } });
@Test public void activateConfiguration() { List<ConfigurationDto> configurations = generateConfigurationDto(null, null, 1, false, false); String configId = configurations.get(0).getId(); ConfigurationDto found = configurationService.findConfigurationById(configId); ChangeConfigurationNotification notification = configurationService.activateConfiguration(configId, null); Assert.assertNotNull(notification); ConfigurationDto dto = notification.getConfigurationDto(); Assert.assertNotNull(dto); Assert.assertEquals(dto.getStatus(), UpdateStatus.ACTIVE); Assert.assertEquals(dto.getId(), configId); Assert.assertNotEquals(dto.getSequenceNumber(), found.getSequenceNumber()); }
@Test public void deactivateConfigurationTest() { ConfigurationDto config = generateConfigurationDto(null, null, 1, true, false).get(0); configurationService.deactivateConfiguration(config.getId(), null); config = configurationService.findConfigurationById(config.getId()); Assert.assertNotNull(config); Assert.assertEquals(UpdateStatus.DEPRECATED, config.getStatus()); }
@Test(expected = IncorrectParameterException.class) public void saveConfigurationObjectWithIncorrectSchemaIdTest() throws SchemaCreationException { List<ConfigurationDto> configurations = generateConfigurationDto(null, null, 1, false, false); ConfigurationDto configurationDto = configurationService.findConfigurationById(configurations.get(0).getId()); configurationDto.setId(null); configurationDto.setSchemaId(100500 + ""); configurationService.saveConfiguration(configurationDto); }
private ConfigurationDto toConfigurationDto(ConfigurationRecordFormDto configuration) throws KaaAdminServiceException, IOException { String body = null; RecordField configurationRecord = configuration.getConfigurationRecord(); if (configurationRecord != null) { GenericRecord record = FormAvroConverter.createGenericRecordFromRecordField( configurationRecord); GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>( record.getSchema()); body = converter.encodeToJson(record); } ConfigurationDto result = new ConfigurationDto(configuration); result.setBody(body); return result; }
boolean endpointGroupFound = false; for (ConfigurationDto configuration : configurations) { if (configuration.getEndpointGroupId().equals(endpointGroup.getId())) { if (endpointGroup.getWeight() != 0) { overrideConfigs.add(new OverrideData(overrideSchema, configuration.getBody())); } else { baseConfig = new BaseData(baseSchema, configuration.getBody());
@Test(expected = IncorrectParameterException.class) public void saveConfigurationWithIncorrectIdTestFail() throws SchemaCreationException { ConfigurationDto configurationDto = new ConfigurationDto(); configurationDto.setId(INCORRECT_SQL_ID); configurationService.saveConfiguration(configurationDto); }