navinko commented on code in PR #10211: URL: https://github.com/apache/ozone/pull/10211#discussion_r3219488584
########## hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSequenceIdType.java: ########## @@ -0,0 +1,93 @@ +/* + * 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.hadoop.hdds.scm.ha; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import org.junit.jupiter.api.Test; + +/** + * Tests for {@link SequenceIdType}. + */ +public class TestSequenceIdType { + + @Test + @SuppressWarnings("deprecation") + public void testStringSyncWithEnumConstants() { + // Even though we changed to an Enum, these underlying strings are + // persisted in RocksDB. Do not change them. + assertEquals("localId", SequenceIdType.LOCAL_ID.getDbKey()); + assertEquals("delTxnId", SequenceIdType.DEL_TXN_ID.getDbKey()); + assertEquals("containerId", SequenceIdType.CONTAINER_ID.getDbKey()); + assertEquals("CertificateId", SequenceIdType.CERTIFICATE_ID.getDbKey()); + assertEquals("rootCertificateId", SequenceIdType.ROOT_CERTIFICATE_ID.getDbKey()); + } + + @Test + @SuppressWarnings("deprecation") + public void testDeprecatedStringSyncWithEnumConstants() { + assertEquals(SequenceIdType.ROOT_CERTIFICATE_ID.getDbKey(), + SequenceIdGenerator.ROOT_CERTIFICATE_ID); + } + + @Test + public void testNumberOfEnumConstants() { + // If a new SequenceIdType is added, this test will fail. + // This serves as a reminder to the developer to verify RocksDB backward + // compatibility and update this test class accordingly. + assertEquals(5, SequenceIdType.values().length); + } + + @Test + public void testIfNewEnumConstantGetsAdded() { + Set<String> expectedNames = new HashSet<>(Arrays.asList( + "LOCAL_ID", "DEL_TXN_ID", "CONTAINER_ID", + "CERTIFICATE_ID", "ROOT_CERTIFICATE_ID")); + + Set<String> actualNames = new HashSet<>(); + for (SequenceIdType type : SequenceIdType.values()) { + actualNames.add(type.name()); + } + + // Filter exactly what changed to make the failure message extremely clear + Set<String> added = new HashSet<>(actualNames); + added.removeAll(expectedNames); + + Set<String> removed = new HashSet<>(expectedNames); + removed.removeAll(actualNames); + + assertTrue(added.isEmpty() && removed.isEmpty(), + () -> "SequenceIdType constants changed!\n" + + "Unexpectedly Added: " + added + "\n" + + "Unexpectedly Removed: " + removed + "\n" + + "ACTION REQUIRED: If this change is intentional, you MUST verify " + + "RocksDB backward compatibility and update this test's expectedNames."); + } + + @Test + public void testReturnsNullIfEnumConstantNotAvailable() { + assertNull(SequenceIdType.fromDbKey("unmapped-key-string")); + assertNull(SequenceIdType.fromDbKey(null)); + } Review Comment: Thanks @sreejasahithi for review, to align with current review comments i ahve removed the unused method fromDbKey completely. -- 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]
