diff --git a/app/src/main/java/io/apicurio/registry/storage/ArtifactStateExt.java b/app/src/main/java/io/apicurio/registry/storage/ArtifactStateExt.java index b650d97687..c80b0d53d8 100644 --- a/app/src/main/java/io/apicurio/registry/storage/ArtifactStateExt.java +++ b/app/src/main/java/io/apicurio/registry/storage/ArtifactStateExt.java @@ -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 */ @@ -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)); } diff --git a/app/src/test/java/io/apicurio/registry/ArtifactStateTest.java b/app/src/test/java/io/apicurio/registry/ArtifactStateTest.java index 02b7dbf7fa..3e71cc2ade 100644 --- a/app/src/test/java/io/apicurio/registry/ArtifactStateTest.java +++ b/app/src/test/java/io/apicurio/registry/ArtifactStateTest.java @@ -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; @@ -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 @@ -130,11 +128,21 @@ public void testSmoke(Supplier 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); @@ -200,4 +208,48 @@ void testEnableDisableArtifact(Supplier supplier) throws Except Assertions.assertEquals(ArtifactState.ENABLED, vmd.getState()); } + @RegistryServiceTest + void testDeprecateDisableArtifact(Supplier supplier) throws Exception { + RegistryService service = supplier.get(); + String artifactId = generateArtifactId(); + + // Create the artifact + CompletionStage 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()); + } + } \ No newline at end of file diff --git a/storage/asyncmem/src/main/java/io/apicurio/registry/asyncmem/AsyncInMemoryRegistryStorage.java b/storage/asyncmem/src/main/java/io/apicurio/registry/asyncmem/AsyncInMemoryRegistryStorage.java index 986b4afffa..128038e7e2 100644 --- a/storage/asyncmem/src/main/java/io/apicurio/registry/asyncmem/AsyncInMemoryRegistryStorage.java +++ b/storage/asyncmem/src/main/java/io/apicurio/registry/asyncmem/AsyncInMemoryRegistryStorage.java @@ -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(() -> { @@ -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); }