chibenwa commented on code in PR #2542: URL: https://github.com/apache/james-project/pull/2542#discussion_r1867110221
########## server/blob/blob-storage-strategy/src/test/java/org/apache/james/server/blob/deduplication/MinIOGenerationAwareBlobIdTest.java: ########## @@ -0,0 +1,295 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.server.blob.deduplication; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.UUID; + +import org.apache.james.blob.api.BlobId; +import org.apache.james.blob.api.PlainBlobId; +import org.apache.james.utils.UpdatableTickingClock; +import org.assertj.core.api.SoftAssertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import nl.jqno.equalsverifier.EqualsVerifier; + +class MinIOGenerationAwareBlobIdTest { + private static final Instant NOW = Instant.parse("2021-08-19T10:15:30.00Z"); + + private BlobId.Factory delegate; + private UpdatableTickingClock clock; + private MinIOGenerationAwareBlobId.Factory testee; + + @BeforeEach + void setUp() { + delegate = new PlainBlobId.Factory(); + clock = new UpdatableTickingClock(NOW); + testee = new MinIOGenerationAwareBlobId.Factory(clock, GenerationAwareBlobId.Configuration.DEFAULT, delegate); + } + + @Nested + class BlobIdGeneration { + @Test + void ofShouldGenerateABlobIdOfTheRightGeneration() { + String key = UUID.randomUUID().toString(); + MinIOGenerationAwareBlobId actual = testee.of(key); + + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(actual.getFamily()).isEqualTo(GenerationAwareBlobId.Configuration.DEFAULT.getFamily()); + soft.assertThat(actual.getGeneration()).isEqualTo(628L); + soft.assertThat(actual.getDelegate()).isEqualTo(delegate.of(key)); + }); + } + } + + @Nested + class BlobIdParsing { + + @Test + void asStringValueShouldBeParsable() { + String key = UUID.randomUUID().toString(); + MinIOGenerationAwareBlobId minioBlobId = testee.of(key); + String minioBlobIdAsString = minioBlobId.asString(); + + BlobId blobId = testee.parse(minioBlobIdAsString); + assertThat(blobId).isEqualTo(minioBlobId); + } + + @Test + void previousBlobIdsShouldBeParsable() { + String blobIdString = delegate.of("abcdef").asString(); + + BlobId actual = testee.parse(blobIdString); + assertThat(actual) + .isInstanceOfSatisfying(GenerationAwareBlobId.class, actualBlobId -> { + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(actualBlobId.getFamily()).isEqualTo(0); + soft.assertThat(actualBlobId.getGeneration()).isEqualTo(0L); + soft.assertThat(actualBlobId.getDelegate().asString()).isEqualTo(blobIdString); + }); + }); + } + + @Test + void noFamilyShouldBeParsable() { + String originalBlobId = "abcdef"; + String blobIdString = "0/0/" + delegate.of(originalBlobId).asString(); + + BlobId actual = testee.parse(blobIdString); + assertThat(actual) + .isInstanceOfSatisfying(MinIOGenerationAwareBlobId.class, actualBlobId -> { + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(actualBlobId.getFamily()).isEqualTo(0); + soft.assertThat(actualBlobId.getGeneration()).isEqualTo(0L); + soft.assertThat(actualBlobId.getDelegate()).isEqualTo(delegate.of(originalBlobId)); + }); + }); + } + + @Test + void generationBlobIdShouldBeParsable() { + String originalBlobId = "abcdef"; + String blobIdString = "12/126/" + delegate.of(originalBlobId).asString(); + + BlobId actual = testee.parse(blobIdString); + assertThat(actual) + .isInstanceOfSatisfying(MinIOGenerationAwareBlobId.class, actualBlobId -> { + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(actualBlobId.getFamily()).isEqualTo(12); + soft.assertThat(actualBlobId.getGeneration()).isEqualTo(126L); + soft.assertThat(actualBlobId.getDelegate()).isEqualTo(delegate.of(originalBlobId)); + }); + }); + } + + @Test + void wrappedBlobIdCanContainSeparator() { + String blobIdString = "12/126/ab/c"; + + BlobId actual = testee.parse(blobIdString); + assertThat(actual) + .isInstanceOfSatisfying(MinIOGenerationAwareBlobId.class, actualBlobId -> { + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(actualBlobId.getFamily()).isEqualTo(12); + soft.assertThat(actualBlobId.getGeneration()).isEqualTo(126L); + soft.assertThat(actualBlobId.getDelegate()).isEqualTo(delegate.of("ab/c")); + }); + }); + } + + @ParameterizedTest + @ValueSource(strings = { + "abcdefgh", + "abcdefgh/", + "1/abcdefgh", + "1/2", + "1/2/", + "/abcdefgh" + }) + void fromShouldFallbackWhenNotApplicable(String blobIdString) { + BlobId actual = testee.parse(blobIdString); + assertThat(actual) + .isInstanceOfSatisfying(GenerationAwareBlobId.class, actualBlobId -> { + SoftAssertions.assertSoftly(soft -> { + soft.assertThat(actualBlobId.getFamily()).isEqualTo(0); + soft.assertThat(actualBlobId.getGeneration()).isEqualTo(0L); + soft.assertThat(actualBlobId.getDelegate()).isEqualTo(delegate.parse(blobIdString)); + }); + }); + } + + @Test + void fallbackGenerationAwareBlobIdShouldSuccess() { Review Comment: No we do not need this fallback. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
