@Override public List<Integer> getAllCtlSchemaVersionsByFqnTenantIdAndApplicationId( String fqn, String tenantId, String applicationId) throws ControlServiceException { List<CTLSchemaDto> schemas = ctlService.findAllCtlSchemasByFqnAndTenantIdAndApplicationId(fqn, tenantId, applicationId); List<Integer> versions = new ArrayList<>(schemas.size()); for (CTLSchemaDto schema : schemas) { versions.add(schema.getVersion()); } return versions; }
private void validateCtlSchemaObject(CTLSchemaDto ctlSchema) { validateObject(ctlSchema, "Incorrect ctl schema object"); CtlSchemaMetaInfoDto metaInfo = ctlSchema.getMetaInfo(); if (metaInfo == null) { throw new RuntimeException("Incorrect ctl schema object. CtlSchemaMetaInfoDto is mandatory " + "information."); } else { if (isBlank(metaInfo.getFqn()) || ctlSchema.getVersion() == null) { throw new RuntimeException("Incorrect CTL schema, please add correct version and fqn."); } } }
/** * Returns a string that contains fully qualified names and version numbers * of the given CTL schemas. * * @param types A collection of CTL schemas * @return A string that contains fully qualified names and version numbers of the given CTL * schemas */ private String asText(Collection<CTLSchemaDto> types) { StringBuilder message = new StringBuilder(); if (types != null) { for (CTLSchemaDto type : types) { CtlSchemaMetaInfoDto details = type.getMetaInfo(); message.append("\n").append("FQN: ") .append(details.getFqn()) .append(", version: ") .append(type.getVersion()); } } return message.toString(); }
@Override public FileData exportCtlSchemaFlatAsLibrary(CTLSchemaDto schema) throws ControlServiceException { try { Schema avroSchema = ctlService.flatExportAsSchema(schema); String fileName = MessageFormat.format(CTL_LIBRARY_EXPORT_TEMPLATE, schema.getMetaInfo().getFqn(), schema.getVersion()); return SchemaLibraryGenerator.generateSchemaLibrary(avroSchema, fileName); } catch (Exception ex) { LOG.error("Unable to export flat CTL schema as library", ex); throw new ControlServiceException(ex); } }
/** * Exports a CTL schema and all of its dependencies depending on the export method specified. */ public FileData downloadCtlSchemaByAppToken(CTLSchemaDto ctlSchemaDto, CTLSchemaExportMethod method, String appToken) { final FileDataResponseExtractor extractor = new FileDataResponseExtractor(); MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>(); parameters.add("fqn", ctlSchemaDto.getMetaInfo().getFqn()); parameters.add("version", Integer.toString(ctlSchemaDto.getVersion())); if (ctlSchemaDto.getMetaInfo().getApplicationId() != null) { parameters.add("applicationToken", appToken); } parameters.add("method", method.name()); RequestCallback request = new DataRequestCallback<>(parameters); return restTemplate.execute(restTemplate.getUrl() + "CTL/exportSchema", HttpMethod.POST, request, extractor); }
@Override public FileData shallowExport(CTLSchemaDto schema) { try { FileData result = new FileData(); result.setContentType(JSON); result.setFileName(MessageFormat.format(CTL_EXPORT_TEMPLATE, schema.getMetaInfo().getFqn(), schema.getVersion())); // Format schema body Object json = FORMATTER.readValue(schema.getBody(), Object.class); result.setFileData(FORMATTER.writerWithDefaultPrettyPrinter().writeValueAsString(json) .getBytes()); return result; } catch (Exception cause) { throw new RuntimeException("An unexpected exception occured: " + cause.toString()); } }
@Override public FileData flatExport(CTLSchemaDto schema) { try { FileData result = new FileData(); result.setContentType(JSON); result.setFileName(MessageFormat.format(CTL_EXPORT_TEMPLATE, schema.getMetaInfo().getFqn(), schema.getVersion())); // Get schema body String body = flatExportAsString(schema); // Format schema body Object json = FORMATTER.readValue(body, Object.class); result.setFileData(FORMATTER.writerWithDefaultPrettyPrinter().writeValueAsString(json) .getBytes()); return result; } catch (Exception cause) { throw new RuntimeException("An unexpected exception occured: " + cause.toString()); } }
@Test public void testFindLatestCTLSchemaByFqn() { CTLSchemaDto latestTenantScope = ctlService.findLatestCtlSchemaByFqnAndTenantIdAndApplicationId(DEFAULT_FQN + 7, tenant.getId(), null); Assert.assertEquals(Integer.valueOf(78), latestTenantScope.getVersion()); CTLSchemaDto latestAppScope = ctlService.findLatestCtlSchemaByFqnAndTenantIdAndApplicationId(DEFAULT_FQN + 8, tenant.getId(), appDto.getId()); Assert.assertEquals(Integer.valueOf(81), latestAppScope.getVersion()); }
/** * Create instance of <code>CtlSchema</code>. * * @param dto data transfer object */ public CtlSchema(CTLSchemaDto dto) { this.id = getLongId(dto.getId()); this.metaInfo = new CtlSchemaMetaInfo(dto.getMetaInfo()); this.version = dto.getVersion(); this.createdUsername = dto.getCreatedUsername(); this.createdTime = dto.getCreatedTime(); update(dto); }
try { Utils.checkNotNull(schema); checkCtlSchemaVersion(schema.getVersion()); checkCtlSchemaEditScope( schema.getMetaInfo().getTenantId(), schema.getMetaInfo().getApplicationId()); CTLSchemaDto schemaFound = controlService.getAnyCtlSchemaByFqnVersionTenantIdAndApplicationId( dependency.getMetaInfo().getFqn(), dependency.getVersion(), schema.getMetaInfo().getTenantId(), schema.getMetaInfo().getApplicationId()); if (schemaFound == null) { missingDependencies.add( new FqnVersion(dependency.getMetaInfo().getFqn(), dependency.getVersion())); } else { dependencies.add(schemaFound);
@Test public void testFindCTLSchemaDependentsByFqnVersionTenantId() { List<CTLSchemaDto> appSchemas = ctlService.findCtlSchemaDependents(firstSchema.getMetaInfo().getFqn(), firstSchema.getVersion(), tenant.getId(), null); Assert.assertEquals(Arrays.asList(mainSchema), appSchemas); appSchemas = ctlService.findCtlSchemaDependents(secondSchema.getMetaInfo().getFqn(), secondSchema.getVersion(), tenant.getId(), null); Assert.assertEquals(Arrays.asList(mainSchema), appSchemas); appSchemas = ctlService.findCtlSchemaDependents(thirdSchema.getMetaInfo().getFqn(), thirdSchema.getVersion(), tenant.getId(), null); Assert.assertEquals(Arrays.asList(mainSchema), appSchemas); appSchemas = ctlService.findCtlSchemaDependents(fourthSchema.getMetaInfo().getFqn(), fourthSchema.getVersion(), tenant.getId(), null); Assert.assertEquals(Arrays.asList(mainSchema), appSchemas); appSchemas = ctlService.findCtlSchemaDependents(mainSchema.getMetaInfo().getFqn(), mainSchema.getVersion(), tenant.getId(), null); Assert.assertTrue(appSchemas.isEmpty()); }
/** * Create new instance of <code>CtlSchemaFormDto</code>. * * @param ctlSchema is ctl schema, get data from it and put to new instance of * <code>CtlSchemaFormDto</code> */ public CtlSchemaFormDto(CTLSchemaDto ctlSchema) { super(); setId(ctlSchema.getId()); setMetaInfo(ctlSchema.getMetaInfo()); setVersion(ctlSchema.getVersion()); setCreatedTime(ctlSchema.getCreatedTime()); setCreatedUsername(ctlSchema.getCreatedUsername()); setHasDependencies(ctlSchema.getDependencySet() != null && !ctlSchema.getDependencySet().isEmpty()); }
@Override public CTLSchemaDto saveCtlSchema(String body, String tenantId, String applicationId) throws KaaAdminServiceException { this.checkAuthority(KaaAuthorityDto.values()); try { checkCtlSchemaEditScope(tenantId, applicationId); CtlSchemaParser parser = new CtlSchemaParser(controlService, tenantId); CTLSchemaDto schema = parser.parse(body, applicationId); checkCtlSchemaVersion(schema.getVersion()); // Check if the schema body is valid parser.validate(schema); CTLSchemaDto result = controlService.saveCtlSchema(schema); return result; } catch (Exception cause) { throw Utils.handleException(cause); } }
@Test public void testFindSystemByFqnAndVerAndTenantIdAndApplicationId() { CtlSchema found = ctlSchemaDao.findByFqnAndVerAndTenantIdAndApplicationId(systemSchema.getMetaInfo().getFqn(), systemSchema.getVersion(), null, null); Assert.assertEquals(systemSchema, found.toDto()); }
@Test public void testFindCTLSchemaByFqnAndVerAndTenantIdAndApplicationId() { CtlSchemaMetaInfoDto metaInfo = firstSchema.getMetaInfo(); CTLSchemaDto found = ctlService.findCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(metaInfo.getFqn(), firstSchema.getVersion(), metaInfo.getTenantId(), metaInfo.getApplicationId()); Assert.assertEquals(firstSchema, found); }
@Override public CTLSchemaDto updateCtlSchema(CTLSchemaDto ctlSchema) { validateCtlSchemaObject(ctlSchema); LOG.debug("Update ctl schema with id [{}]", ctlSchema.getId()); CtlSchema schema = ctlSchemaDao.findById(ctlSchema.getId()); if (schema != null) { synchronized (this) { if (ctlSchema.getVersion() != schema.getVersion()) { throw new DatabaseProcessingException("Can't change version of existing common type " + "version."); } CtlSchemaMetaInfo metaInfo = schema.getMetaInfo(); if (!ctlSchema.getMetaInfo().equals(metaInfo.toDto())) { throw new DatabaseProcessingException("Can't update scope of existing common type " + "version within update procedure."); } ctlSchemaMetaInfoDao.lockRequest(lockOptions).setScope(true).lock(metaInfo); schema.update(ctlSchema); return DaoUtil.getDto(ctlSchemaDao.save(schema, true)); } } else { throw new DatabaseProcessingException("Can't find common type version by id."); } }
@Test public void testScopeUpdate() { CtlSchemaMetaInfoDto metaInfo = appSchema3.getMetaInfo(); metaInfo.setApplicationId(null); ctlService.updateCtlSchemaMetaInfoScope(metaInfo); CTLSchemaDto found = ctlService.findCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(metaInfo.getFqn(), appSchema3.getVersion(), metaInfo.getTenantId(), null); Assert.assertEquals(appSchema3, found); }
@Test public void testFindByFqnAndVerAndTenantIdAndApplicationId() { CtlSchema found = ctlSchemaDao.findByFqnAndVerAndTenantIdAndApplicationId(firstSchema.getMetaInfo().getFqn(), firstSchema.getVersion(), firstSchema.getMetaInfo().getTenantId(), firstSchema.getMetaInfo().getApplicationId()); Assert.assertEquals(firstSchema, found.toDto()); }
@Test public void testRemoveCTLSchemaByFqnAndVerAndTenantIdAndApplicationId() { String schemaId = tenantSchema.getId(); ctlService.removeCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(tenantSchema.getMetaInfo().getFqn(), tenantSchema.getVersion(), tenantSchema.getMetaInfo().getTenantId(), tenantSchema.getMetaInfo().getApplicationId()); Assert.assertNull(ctlService.findCtlSchemaById(schemaId)); }
@Test public void testRemoveCTLSchemaByFqnAndVerAndWithoutTenantId() { String schemaId = systemSchema.getId(); ctlService.removeCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(systemSchema.getMetaInfo().getFqn(), systemSchema.getVersion(), systemSchema.getMetaInfo().getTenantId(), systemSchema.getMetaInfo().getApplicationId()); Assert.assertNull(ctlService.findCtlSchemaById(schemaId)); }