public List<SchemaVersionInfo> getSortedSchemaVersions(SchemaBranch schemaBranch) throws SchemaNotFoundException { try { return getSortedSchemaVersions(schemaBranch.getId()); } catch (SchemaBranchNotFoundException e) { throw new RuntimeException(e); } }
public SchemaVersionInfo getRootVersion(SchemaBranch schemaBranch) throws SchemaNotFoundException { if (schemaBranch.getName().equals(SchemaBranch.MASTER_BRANCH)) { throw new SchemaNotFoundException(String.format("There is no root schema version attached to the schema branch '%s'", schemaBranch.getName())); } List<SchemaVersionInfo> sortedVersionInfo; try { sortedVersionInfo = getSortedSchemaVersions(schemaBranch.getId()); } catch (SchemaBranchNotFoundException e) { throw new RuntimeException(e); } if (sortedVersionInfo == null) throw new SchemaNotFoundException(String.format("There were no schema versions attached to schema branch '%s'", schemaBranch.getName())); return sortedVersionInfo.iterator().next(); }
@Override public SchemaBranch load(Key key) throws Exception { SchemaBranch schemaBranch; Key otherKey; if (key.getSchemaBranchKey() != null) { schemaBranch = schemaBranchFetcher.getSchemaBranch(key.getSchemaBranchKey()); otherKey = Key.of(schemaBranch.getId()); schemaBranchNameToIdMap.put(key.getSchemaBranchKey(), schemaBranch.getId()); } else if (key.getId() != null) { schemaBranch = schemaBranchFetcher.getSchemaBranch(key.getId()); otherKey = Key.of(new SchemaBranchKey(schemaBranch.getName(), schemaBranch.getSchemaMetadataName())); schemaBranchNameToIdMap.put(otherKey.schemaBranchKey, schemaBranch.getId()); } else { throw new IllegalArgumentException("Given argument is not valid: " + key); } loadingCache.put(otherKey, schemaBranch); return schemaBranch; } });
@Override public SchemaBranch load(Key key) throws Exception { SchemaBranch schemaBranch; Key otherKey; if (key.getSchemaBranchKey() != null) { schemaBranch = schemaBranchFetcher.getSchemaBranch(key.getSchemaBranchKey()); otherKey = Key.of(schemaBranch.getId()); schemaBranchNameToIdMap.put(key.getSchemaBranchKey(), schemaBranch.getId()); } else if (key.getId() != null) { schemaBranch = schemaBranchFetcher.getSchemaBranch(key.getId()); otherKey = Key.of(new SchemaBranchKey(schemaBranch.getName(), schemaBranch.getSchemaMetadataName())); schemaBranchNameToIdMap.put(otherKey.schemaBranchKey, schemaBranch.getId()); } else { throw new IllegalArgumentException("Given argument is not valid: " + key); } loadingCache.put(otherKey, schemaBranch); return schemaBranch; } });
schemaVersionMappingStorableQueryParams.add(new QueryParam(SchemaBranchVersionMapping.SCHEMA_BRANCH_ID, schemaBranch.getId().toString())); List<OrderByField> schemaVersionMappingOrderbyFields = new ArrayList<>(); schemaVersionMappingOrderbyFields.add(OrderByField.of(SchemaBranchVersionMapping.SCHEMA_VERSION_INFO_ID, false));
public static SchemaBranchStorable from(SchemaBranch schemaBranch) { SchemaBranchStorable schemaBranchStorable = new SchemaBranchStorable(); schemaBranchStorable.setId(schemaBranch.getId()); schemaBranchStorable.setName(schemaBranch.getName()); schemaBranchStorable.setDescription(schemaBranch.getDescription()); schemaBranchStorable.setTimestamp(schemaBranch.getTimestamp() == null ? System.currentTimeMillis() : schemaBranch.getTimestamp()); return schemaBranchStorable; }
Long schemaBranchId = schemaBranches.iterator().next().getId(); SchemaBranch schemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchId));
@Override public SchemaBranch createSchemaBranch(Long schemaVersionId, SchemaBranch schemaBranch) throws SchemaBranchAlreadyExistsException, SchemaNotFoundException { Preconditions.checkNotNull(schemaBranch.getName(), "Schema branch name can't be null"); SchemaVersionInfo schemaVersionInfo = schemaVersionLifecycleManager.getSchemaVersionInfo(new SchemaIdVersion(schemaVersionId)); SchemaBranchKey schemaBranchKey = new SchemaBranchKey(schemaBranch.getName(), schemaVersionInfo.getName()); SchemaBranch existingSchemaBranch = null; try { existingSchemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchKey)); } catch (SchemaBranchNotFoundException e) { // Ignore this error } if (existingSchemaBranch != null) throw new SchemaBranchAlreadyExistsException(String.format("A schema branch with name : '%s' already exists", schemaBranch.getName())); SchemaBranchStorable schemaBranchStorable = SchemaBranchStorable.from(schemaBranch); schemaBranchStorable.setSchemaMetadataName(schemaVersionInfo.getName()); schemaBranchStorable.setId(storageManager.nextId(SchemaBranchStorable.NAME_SPACE)); storageManager.add(schemaBranchStorable); SchemaBranch persistedSchemaBranch; try { persistedSchemaBranch = schemaBranchCache.get(SchemaBranchCache.Key.of(schemaBranchKey)); } catch (SchemaBranchNotFoundException e) { throw new RuntimeException(String.format("Failed to fetch persisted schema branch : '%s' from the database", schemaBranch.getName())); } SchemaBranchVersionMapping schemaBranchVersionMapping = new SchemaBranchVersionMapping(persistedSchemaBranch.getId(), schemaVersionInfo.getId()); storageManager.add(schemaBranchVersionMapping); return persistedSchemaBranch; }
@Test (expected = SchemaBranchNotFoundException.class) public void deleteInvalidSchemaBranch() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException, InvalidSchemaBranchDeletionException { SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.NONE); SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc"); SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId()); addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc"); schemaRegistryClient.deleteSchemaBranch(schemaBranch1.getId() + 1); }
@Test public void deleteSchemaBranch() throws SchemaNotFoundException, SchemaBranchAlreadyExistsException, IOException, InvalidSchemaException, IncompatibleSchemaException, SchemaBranchNotFoundException, InvalidSchemaBranchDeletionException { SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.NONE); SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc"); SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId()); addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc"); addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-compat.avsc"); Assert.assertTrue(schemaRegistryClient.getSchemaBranches(schemaMetadata.getName()).size() == 2); schemaRegistryClient.deleteSchemaBranch(schemaBranch1.getId()); Collection<SchemaVersionInfo> masterSchemaVersionInfos = schemaRegistryClient.getAllVersions(schemaMetadata.getName()); Assert.assertTrue(masterSchemaVersionInfos.size() == 1); Assert.assertTrue(schemaRegistryClient.getSchemaBranches(schemaMetadata.getName()).size() == 1); }
SchemaBranchVersionMapping schemaBranchVersionMapping = new SchemaBranchVersionMapping(schemaBranch.getId(), schemaInstanceId); storageManager.add(schemaBranchVersionMapping);
@Test (expected = InvalidSchemaBranchDeletionException.class) public void deleteBranchWithEnabledSchema() throws IOException, SchemaBranchNotFoundException, InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException, InvalidSchemaBranchDeletionException, SchemaLifecycleException { SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.NONE); SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc"); SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId()); SchemaIdVersion schemaBranch1Version1 = addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc"); schemaRegistryClient.enableSchemaVersion(schemaBranch1Version1.getSchemaVersionId()); schemaRegistryClient.deleteSchemaBranch(schemaBranch1.getId()); }
@Test public void deleteBranchWithArchivedSchema() throws InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, IOException, SchemaBranchAlreadyExistsException, SchemaLifecycleException, InvalidSchemaBranchDeletionException { SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.NONE); SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc"); SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId()); SchemaIdVersion schemaBranch1Version1 = addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc"); schemaRegistryClient.enableSchemaVersion(schemaBranch1Version1.getSchemaVersionId()); schemaRegistryClient.archiveSchemaVersion(schemaBranch1Version1.getSchemaVersionId()); schemaRegistryClient.deleteSchemaBranch(schemaBranch1.getId()); }
@Test (expected = InvalidSchemaBranchDeletionException.class) public void deleteBranchWithRootVersionOfAnotherBranch() throws InvalidSchemaException, SchemaNotFoundException, IncompatibleSchemaException, IOException, SchemaBranchAlreadyExistsException, SchemaLifecycleException, InvalidSchemaBranchDeletionException { SchemaMetadata schemaMetadata = addSchemaMetadata(testNameRule.getMethodName(), SchemaCompatibility.NONE); SchemaIdVersion masterSchemaIdVersion1 = addSchemaVersion(SchemaBranch.MASTER_BRANCH, schemaMetadata, "/device.avsc"); SchemaBranch schemaBranch1 = addSchemaBranch("BRANCH1", schemaMetadata, masterSchemaIdVersion1.getSchemaVersionId()); SchemaIdVersion schemaBranch1Version1 = addSchemaVersion(schemaBranch1.getName(), schemaMetadata, "/device-incompat.avsc"); addSchemaBranch("CHILD-BRANCH", schemaMetadata,schemaBranch1Version1.getSchemaVersionId()); schemaRegistryClient.enableSchemaVersion(schemaBranch1Version1.getSchemaVersionId()); schemaRegistryClient.archiveSchemaVersion(schemaBranch1Version1.getSchemaVersionId()); schemaRegistryClient.deleteSchemaBranch(schemaBranch1.getId()); }