Skip to content

Commit

Permalink
Allow transition from Deprecated state to Enabled or Disabled states (#…
Browse files Browse the repository at this point in the history
…783)

- This commit allows an artifact to transition from the `Deprecated`
state to either the `Enabled` or `Disabled` states. This allows users
who have accidentally deprecated a schema to re-enable it, or it allows
a deprecated schema to become disabled so that clients can no longer use
it.

Signed-off-by: Andrew Borley <[email protected]>
  • Loading branch information
ajborley authored Aug 25, 2020
1 parent 9a21827 commit 1bfb558
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static io.apicurio.registry.storage.MetaDataKeys.STATE;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import static io.apicurio.registry.storage.MetaDataKeys.STATE;

/**
* @author Ales Justin
*/
Expand All @@ -39,7 +39,7 @@ public class ArtifactStateExt {
transitions = new HashMap<>();
transitions.put(ArtifactState.ENABLED, EnumSet.of(ArtifactState.DISABLED, ArtifactState.DEPRECATED, ArtifactState.DELETED));
transitions.put(ArtifactState.DISABLED, EnumSet.of(ArtifactState.ENABLED, ArtifactState.DEPRECATED, ArtifactState.DELETED));
transitions.put(ArtifactState.DEPRECATED, EnumSet.of(ArtifactState.DELETED));
transitions.put(ArtifactState.DEPRECATED, EnumSet.of(ArtifactState.ENABLED, ArtifactState.DISABLED, ArtifactState.DELETED));
transitions.put(ArtifactState.DELETED, EnumSet.noneOf(ArtifactState.class));
}

Expand Down
84 changes: 68 additions & 16 deletions app/src/test/java/io/apicurio/registry/ArtifactStateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@

package io.apicurio.registry;

import static io.apicurio.registry.utils.tests.TestUtils.assertWebError;
import static io.apicurio.registry.utils.tests.TestUtils.retry;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletionStage;
import java.util.function.Supplier;

import javax.ws.rs.core.Response;

import org.junit.jupiter.api.Assertions;

import io.apicurio.registry.client.RegistryService;
import io.apicurio.registry.rest.beans.ArtifactMetaData;
import io.apicurio.registry.rest.beans.EditableMetaData;
Expand All @@ -39,6 +26,17 @@
import io.apicurio.registry.utils.ConcurrentUtil;
import io.apicurio.registry.utils.tests.RegistryServiceTest;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Assertions;

import javax.ws.rs.core.Response;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletionStage;
import java.util.function.Supplier;

import static io.apicurio.registry.utils.tests.TestUtils.assertWebError;
import static io.apicurio.registry.utils.tests.TestUtils.retry;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author Ales Justin
Expand Down Expand Up @@ -130,11 +128,21 @@ public void testSmoke(Supplier<RegistryService> supplier) throws Exception {
avr = service.getArtifactVersion(2, artifactId);
Assertions.assertEquals(200, avr.getStatus());

// cannot go back from deprecated ...
assertWebError(400, () -> service.updateArtifactState(artifactId, toUpdateState(ArtifactState.ENABLED)));

service.updateArtifactMetaData(artifactId, emd); // should be allowed for deprecated

retry(() -> {
service.reset();
ArtifactMetaData innerAmd = service.getArtifactMetaData(artifactId);
Assertions.assertEquals(3, innerAmd.getVersion());
Assertions.assertEquals(description, innerAmd.getDescription());
Assertions.assertEquals(ArtifactState.DEPRECATED, innerAmd.getState());
return null;
});

// can revert back to enabled from deprecated
service.updateArtifactVersionState(3, artifactId, toUpdateState(ArtifactState.ENABLED));
this.waitForVersionState(artifactId, 3, ArtifactState.ENABLED);

retry(() -> {
service.reset();
ArtifactMetaData innerAmd = service.getArtifactMetaData(artifactId);
Expand Down Expand Up @@ -200,4 +208,48 @@ void testEnableDisableArtifact(Supplier<RegistryService> supplier) throws Except
Assertions.assertEquals(ArtifactState.ENABLED, vmd.getState());
}

@RegistryServiceTest
void testDeprecateDisableArtifact(Supplier<RegistryService> supplier) throws Exception {
RegistryService service = supplier.get();
String artifactId = generateArtifactId();

// Create the artifact
CompletionStage<ArtifactMetaData> a1 = service.createArtifact(
ArtifactType.JSON,
artifactId,
null,
new ByteArrayInputStream("{\"type\": \"string\"}".getBytes(StandardCharsets.UTF_8))
);
ArtifactMetaData md = ConcurrentUtil.result(a1);

retry(() -> {
// Get the meta-data
ArtifactMetaData actualMD = service.getArtifactMetaData(artifactId);
assertEquals(md.getGlobalId(), actualMD.getGlobalId());
});

// Set to deprecated
UpdateState state = new UpdateState();
state.setState(ArtifactState.DEPRECATED);
service.updateArtifactState(artifactId, state);
this.waitForArtifactState(artifactId, ArtifactState.DEPRECATED);

retry(() -> {
// Get the meta-data again - should be DEPRECATED
ArtifactMetaData actualMD = service.getArtifactMetaData(artifactId);
assertEquals(md.getGlobalId(), actualMD.getGlobalId());
Assertions.assertEquals(ArtifactState.DEPRECATED, actualMD.getState());
});

// Set to disabled
state.setState(ArtifactState.DISABLED);
service.updateArtifactState(artifactId, state);
this.waitForArtifactState(artifactId, ArtifactState.DISABLED);

// Get the meta-data again - should be a 404
assertWebError(404, () -> service.getArtifactMetaData(artifactId), true);
VersionMetaData vmd = service.getArtifactVersionMetaData(md.getVersion(), artifactId);
Assertions.assertEquals(ArtifactState.DISABLED, vmd.getState());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,6 @@ public void updateArtifactMetaData(String artifactId, EditableArtifactMetaDataDt
*/
@Override
public void updateArtifactState(String artifactId, ArtifactState state, Integer version) {
if (state == ArtifactState.ENABLED && this.isDeprecated(artifactId, version)) {
throw new InvalidArtifactStateException(ArtifactState.DEPRECATED, state);
}
this.executor.execute(() -> {
preUpdateSleep();
runWithErrorSuppression(() -> {
Expand Down Expand Up @@ -519,36 +516,6 @@ private boolean hasArtifactRule(String artifactId, RuleType rule) {
}
}

private boolean isArtifactDeprecated(String artifactId) {
try {
ArtifactMetaDataDto metaData = this.getArtifactMetaData(artifactId);
if (metaData.getState() == ArtifactState.DEPRECATED) {
return true;
}
} catch (ArtifactNotFoundException | RegistryStorageException e) {
}
return false;
}

private boolean isVersionDeprecated(String artifactId, Integer version) {
try {
ArtifactVersionMetaDataDto metaData = this.getArtifactVersionMetaData(artifactId, version.longValue());
if (metaData.getState() == ArtifactState.DEPRECATED) {
return true;
}
} catch (ArtifactNotFoundException | RegistryStorageException e) {
}
return false;
}

private boolean isDeprecated(String artifactId, Integer version) {
if (version == null) {
return this.isArtifactDeprecated(artifactId);
} else {
return this.isVersionDeprecated(artifactId, version);
}
}

private void preDeleteSleep() {
doSleep(this.deleteDelay);
}
Expand Down

0 comments on commit 1bfb558

Please sign in to comment.