@Override public String getValue(CtlSchemaMetaInfoDto item) { return item.getFqn(); } },
private Map<Fqn, List<Integer>> extractCtlSchemaVersionsInfo( List<CtlSchemaMetaInfoDto> ctlSchemaInfos) { Map<Fqn, List<Integer>> ctlSchemaVersions = new HashMap<>(); for (CtlSchemaMetaInfoDto ctlSchemaInfo : ctlSchemaInfos) { ctlSchemaVersions.put(new Fqn(ctlSchemaInfo.getFqn()), ctlSchemaInfo.getVersions()); } return ctlSchemaVersions; }
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."); } } }
@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); } }
@Override public void setAcceptableValues(Collection<CtlSchemaReferenceDto> values) { fqnVersionsMap.clear(); fqnVersionToReferenceMap.clear(); Map<FqnKey, Fqn> declaredFqns = new HashMap<>(); for (CtlSchemaReferenceDto ctlReference : values) { Fqn fqn = new Fqn(ctlReference.getMetaInfo().getFqn()); FqnKey fqnKey = new FqnKey(fqn); declaredFqns.put(fqnKey, fqn); List<Integer> versions = fqnVersionsMap.get(fqn); if (versions == null) { versions = new ArrayList<>(); fqnVersionsMap.put(fqn, versions); } versions.add(ctlReference.getVersion()); fqnVersionToReferenceMap.put(new FqnVersion(fqn, ctlReference.getVersion()), ctlReference); } fqnReferenceBox.updateDeclaredFqns(declaredFqns); updateBox(); }
/** * 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()); } }
private void updateBox() { if (value != null) { Fqn fqn = new Fqn(value.getMetaInfo().getFqn()); fqnReferenceBox.setValue(new FqnKey(fqn)); List<Integer> versions = fqnVersionsMap.get(fqn); Collections.sort(versions); versionsBox.setValue(value.getVersion()); versionsBox.setAcceptableValues(versions); } else { fqnReferenceBox.setValue(null); versionsBox.reset(); } }
/** * Create new instance of CTL schema meta info. * * @param dto data transfer object that used for creating new instance, * it hold id, full qualified name, tenant id and application id * of new instance */ public CtlSchemaMetaInfo(CtlSchemaMetaInfoDto dto) { this.id = ModelUtils.getLongId(dto.getId()); this.fqn = dto.getFqn(); Long tenantId = getLongId(dto.getTenantId()); this.tenant = tenantId != null ? new Tenant(tenantId) : null; Long appId = getLongId(dto.getApplicationId()); this.application = appId != null ? new Application(appId) : null; }
@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()); }
CtlSchemaFormDto toCtlSchemaForm(CTLSchemaDto ctlSchema, ConverterType converterType) throws KaaAdminServiceException { try { CtlSchemaFormDto ctlSchemaForm = new CtlSchemaFormDto(ctlSchema); SchemaFormAvroConverter converter = getCtlSchemaConverterForScope( ctlSchemaForm.getMetaInfo().getTenantId(), ctlSchemaForm.getMetaInfo().getApplicationId(), converterType); RecordField form = converter.createSchemaFormFromSchema(ctlSchema.getBody()); ctlSchemaForm.setSchema(form); List<Integer> availableVersions = controlService.getAllCtlSchemaVersionsByFqnTenantIdAndApplicationId( ctlSchema.getMetaInfo().getFqn(), ctlSchema.getMetaInfo().getTenantId(), ctlSchema.getMetaInfo().getApplicationId()); availableVersions = availableVersions == null ? Collections.<Integer>emptyList() : availableVersions; Collections.sort(availableVersions); ctlSchemaForm.getMetaInfo().setVersions(availableVersions); return ctlSchemaForm; } 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 testFindSiblingsFqns() { List<CtlSchemaMetaInfoDto> siblingSchemas = ctlService.findSiblingsByFqnTenantIdAndApplicationId(appSchema.getMetaInfo().getFqn(), appSchema.getMetaInfo().getTenantId(), appSchema.getMetaInfo().getApplicationId()); Assert.assertNotNull(siblingSchemas); Assert.assertEquals(1, siblingSchemas.size()); Assert.assertEquals(app2Schema.getMetaInfo(), siblingSchemas.get(0)); }
@Test public void testFindCTLSchemaByFqnAndVerAndTenantIdAndApplicationId() { CtlSchemaMetaInfoDto metaInfo = firstSchema.getMetaInfo(); CTLSchemaDto found = ctlService.findCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(metaInfo.getFqn(), firstSchema.getVersion(), metaInfo.getTenantId(), metaInfo.getApplicationId()); Assert.assertEquals(firstSchema, found); }
@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 testRemoveCTLSchemaByFqnAndVerAndWithoutTenantId() { String schemaId = systemSchema.getId(); ctlService.removeCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(systemSchema.getMetaInfo().getFqn(), systemSchema.getVersion(), systemSchema.getMetaInfo().getTenantId(), systemSchema.getMetaInfo().getApplicationId()); Assert.assertNull(ctlService.findCtlSchemaById(schemaId)); }
@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)); }