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."); } } }
/** * 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 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 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()); } }
@Test public void testFindSystemCTLSchemasMetaInfo() { List<CtlSchemaMetaInfoDto> appSchemas = ctlService.findSystemCtlSchemasMetaInfo(); Comparator<HasId> comparator = new Comparator<HasId>() { @Override public int compare(HasId o1, HasId o2) { return o1.getId().compareTo(o2.getId()); } }; Collections.sort(appSchemas, comparator); List<CtlSchemaMetaInfoDto> expectedSchemas = Arrays.asList(defaultSystemSchema.getMetaInfo(), systemSchema.getMetaInfo()); Collections.sort(expectedSchemas, comparator); Assert.assertEquals(expectedSchemas, appSchemas); }
@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)); }
@Override public CTLSchemaDto getCtlSchemaById(String schemaId) throws KaaAdminServiceException { this.checkAuthority(KaaAuthorityDto.values()); try { this.checkCtlSchemaId(schemaId); CTLSchemaDto schemaFound = controlService.getCtlSchemaById(schemaId); Utils.checkNotNull(schemaFound); checkCtlSchemaReadScope( schemaFound.getMetaInfo().getTenantId(), schemaFound.getMetaInfo().getApplicationId()); return schemaFound; } catch (Exception cause) { throw Utils.handleException(cause); } }
@Override public CtlSchemaFormDto getLatestCtlSchemaForm(String metaInfoId) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.values()); try { this.checkCtlSchemaMetaInfoId(metaInfoId); CTLSchemaDto ctlSchema = controlService.getLatestCtlSchemaByMetaInfoId(metaInfoId); Utils.checkNotNull(ctlSchema); checkCtlSchemaReadScope( ctlSchema.getMetaInfo().getTenantId(), ctlSchema.getMetaInfo().getApplicationId()); return toCtlSchemaForm(ctlSchema, ConverterType.FORM_AVRO_CONVERTER); } catch (Exception cause) { throw Utils.handleException(cause); } }
@Override public String prepareCtlSchemaExport(String ctlSchemaId, CTLSchemaExportMethod method) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.values()); try { CTLSchemaDto schemaFound = controlService.getCtlSchemaById(ctlSchemaId); Utils.checkNotNull(schemaFound); checkCtlSchemaReadScope( schemaFound.getMetaInfo().getTenantId(), schemaFound.getMetaInfo().getApplicationId()); CtlSchemaExportKey key = new CtlSchemaExportKey(ctlSchemaId, method); return Base64.encodeObject(key, Base64.URL_SAFE); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Test public void testFindByFqnAndVerAndTenantIdAndApplicationId() { CtlSchema found = ctlSchemaDao.findByFqnAndVerAndTenantIdAndApplicationId(firstSchema.getMetaInfo().getFqn(), firstSchema.getVersion(), firstSchema.getMetaInfo().getTenantId(), firstSchema.getMetaInfo().getApplicationId()); Assert.assertEquals(firstSchema, found.toDto()); }
@Override public CtlSchemaFormDto getCtlSchemaFormByMetaInfoIdAndVer(String metaInfoId, int version) throws KaaAdminServiceException { this.checkAuthority(KaaAuthorityDto.values()); try { this.checkCtlSchemaMetaInfoId(metaInfoId); this.checkCtlSchemaVersion(version); CTLSchemaDto schemaFound = controlService.getCtlSchemaByMetaInfoIdAndVer( metaInfoId, version); Utils.checkNotNull(schemaFound); checkCtlSchemaReadScope( schemaFound.getMetaInfo().getTenantId(), schemaFound.getMetaInfo().getApplicationId()); return toCtlSchemaForm(schemaFound, ConverterType.FORM_AVRO_CONVERTER); } catch (Exception cause) { throw Utils.handleException(cause); } }
@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(expected = DatabaseProcessingException.class) public void testScopeUpdateForbidden() { CtlSchemaMetaInfoDto metaInfo = appSchema.getMetaInfo(); metaInfo.setApplicationId(null); ctlService.updateCtlSchemaMetaInfoScope(metaInfo); }
/** * 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); }
@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 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()); }
@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); }
@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); }